Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

eth-ndd.c

Go to the documentation of this file.
00001 /*
00002  * eth-ndd.c
00003  *
00004  * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: eth-ndd.c,v 1.7 2005/01/25 21:30:40 dugsong Exp $
00007  */
00008 
00009 #include "config.h"
00010 
00011 #include <sys/types.h>
00012 #include <sys/socket.h>
00013 #include <sys/ndd_var.h>
00014 #include <sys/kinfo.h>
00015 
00016 #include <assert.h>
00017 #include <errno.h>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 
00023 #include "dnet.h"
00024 
00025 struct eth_handle {
00026         char    device[16];
00027         int     fd;
00028 };
00029 
00030 eth_t *
00031 eth_open(const char *device)
00032 {
00033         struct sockaddr_ndd_8022 sa;
00034         eth_t *e;
00035         
00036         if ((e = calloc(1, sizeof(*e))) == NULL)
00037                 return (NULL);
00038 
00039         if ((e->fd = socket(AF_NDD, SOCK_DGRAM, NDD_PROT_ETHER)) < 0)
00040                 return (eth_close(e));
00041         
00042         sa.sndd_8022_family = AF_NDD;
00043         sa.sndd_8022_len = sizeof(sa);
00044         sa.sndd_8022_filtertype = NS_ETHERTYPE;
00045         sa.sndd_8022_ethertype = ETH_TYPE_IP;
00046         sa.sndd_8022_filterlen = sizeof(struct ns_8022);
00047         strlcpy(sa.sndd_8022_nddname, device, sizeof(sa.sndd_8022_nddname));
00048         
00049         if (bind(e->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
00050                 return (eth_close(e));
00051         
00052         if (connect(e->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
00053                 return (eth_close(e));
00054         
00055         /* XXX - SO_BROADCAST needed? */
00056         
00057         return (e);
00058 }
00059 
00060 ssize_t
00061 eth_send(eth_t *e, const void *buf, size_t len)
00062 {
00063         return (write(e->fd, buf, len));
00064 }
00065 
00066 eth_t *
00067 eth_close(eth_t *e)
00068 {
00069         if (e != NULL) {
00070                 if (e->fd >= 0)
00071                         close(e->fd);
00072                 free(e);
00073         }
00074         return (NULL);
00075 }
00076 
00077 int
00078 eth_get(eth_t *e, eth_addr_t *ea)
00079 {
00080         struct kinfo_ndd *nddp;
00081         int size;
00082         void *end;
00083         
00084         if ((size = getkerninfo(KINFO_NDD, 0, 0, 0)) == 0) {
00085                 errno = ENOENT;
00086                 return (-1);
00087         } else if (size < 0)
00088                 return (-1);
00089         
00090         if ((nddp = malloc(size)) == NULL)
00091                 return (-1);
00092                      
00093         if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
00094                 free(nddp);
00095                 return (-1);
00096         }
00097         for (end = (void *)nddp + size; (void *)nddp < end; nddp++) {
00098                 if (strcmp(nddp->ndd_alias, e->device) == 0 ||
00099                     strcmp(nddp->ndd_name, e->device) == 0) {
00100                         memcpy(ea, nddp->ndd_addr, sizeof(*ea));
00101                 }
00102         }
00103         free(nddp);
00104         
00105         if ((void *)nddp >= end) {
00106                 errno = ESRCH;
00107                 return (-1);
00108         }
00109         return (0);
00110 }
00111 
00112 int
00113 eth_set(eth_t *e, const eth_addr_t *ea)
00114 {
00115         errno = ENOSYS;
00116         return (-1);
00117 }

Generated on Sun May 14 14:51:11 2006 by  doxygen 1.4.2