socket.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int read_interface (char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)
int listen_socket (unsigned int ip, int port, char *inf)
int raw_socket (int ifindex)


Function Documentation

int listen_socket ( unsigned int  ip,
int  port,
char *  inf 
)

Definition at line 93 of file socket.c.

References DEBUG, LOG_ERR, and LOG_INFO.

Referenced by udhcp().

00094 {
00095         struct ifreq interface;
00096         int fd;
00097         struct sockaddr_in addr;
00098         int n = 1;
00099 
00100         DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
00101         if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
00102                 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
00103                 return -1;
00104         }
00105         
00106         memset(&addr, 0, sizeof(addr));
00107         addr.sin_family = AF_INET;
00108         addr.sin_port = htons(port);
00109         addr.sin_addr.s_addr = ip;
00110 
00111         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
00112                 close(fd);
00113                 return -1;
00114         }
00115         if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
00116                 close(fd);
00117                 return -1;
00118         }
00119 
00120         strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
00121         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
00122                 close(fd);
00123                 return -1;
00124         }
00125 
00126         if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
00127                 close(fd);
00128                 return -1;
00129         }
00130         
00131         return fd;
00132 }

int raw_socket ( int  ifindex  ) 

Definition at line 135 of file socket.c.

References DEBUG, LOG_ERR, and LOG_INFO.

Referenced by udhcp().

00136 {
00137         int fd;
00138         struct sockaddr_ll sock;
00139 
00140         DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
00141         if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
00142                 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
00143                 return -1;
00144         }
00145         
00146         sock.sll_family = AF_PACKET;
00147         sock.sll_protocol = htons(ETH_P_IP);
00148         sock.sll_ifindex = ifindex;
00149         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
00150                 DEBUG(LOG_ERR, "bind call failed: %s", strerror(errno));
00151                 close(fd);
00152                 return -1;
00153         }
00154 
00155         return fd;
00156 }

int read_interface ( char *  interface,
int *  ifindex,
u_int32_t *  addr,
unsigned char *  arp 
)

Definition at line 46 of file socket.c.

References DEBUG, LOG, LOG_ERR, and LOG_INFO.

Referenced by udhcp().

00047 {
00048         int fd;
00049         struct ifreq ifr;
00050         struct sockaddr_in *our_ip;
00051 
00052         memset(&ifr, 0, sizeof(struct ifreq));
00053         if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
00054                 ifr.ifr_addr.sa_family = AF_INET;
00055                 strcpy(ifr.ifr_name, interface);
00056 
00057                 if (addr) { 
00058                         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
00059                                 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
00060                                 *addr = our_ip->sin_addr.s_addr;
00061                                 DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
00062                         } else {
00063                                 LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %s", 
00064                                                 strerror(errno));
00065                                 return -1;
00066                         }
00067                 }
00068                 
00069                 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
00070                         DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
00071                         *ifindex = ifr.ifr_ifindex;
00072                 } else {
00073                         LOG(LOG_ERR, "SIOCGIFINDEX failed!: %s", strerror(errno));
00074                         return -1;
00075                 }
00076                 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
00077                         memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
00078                         DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
00079                                 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
00080                 } else {
00081                         LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %s", strerror(errno));
00082                         return -1;
00083                 }
00084         } else {
00085                 LOG(LOG_ERR, "socket failed!: %s", strerror(errno));
00086                 return -1;
00087         }
00088         close(fd);
00089         return 0;
00090 }


Generated on Tue Jan 15 12:24:48 2008 for Dynamics 0.8.1.Dynamo.1 by  doxygen 1.5.1