diff options
Diffstat (limited to 'networking/udhcp/socket.c')
-rw-r--r-- | networking/udhcp/socket.c | 75 |
1 files changed, 33 insertions, 42 deletions
diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index d1867e9a8..ea2913172 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -23,15 +23,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/ioctl.h> -#include <netinet/in.h> -#include <unistd.h> -#include <string.h> -#include <arpa/inet.h> #include <net/if.h> -#include <errno.h> #include <features.h> #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION #include <netpacket/packet.h> @@ -43,7 +35,7 @@ #endif #include "common.h" -#include "socket.h" + int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) { @@ -52,44 +44,42 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) struct sockaddr_in *our_ip; memset(&ifr, 0, sizeof(struct ifreq)); - if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { - ifr.ifr_addr.sa_family = AF_INET; - strcpy(ifr.ifr_name, interface); - - if (addr) { - if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { - our_ip = (struct sockaddr_in *) &ifr.ifr_addr; - *addr = our_ip->sin_addr.s_addr; - DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); - } else { - bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?"); - close(fd); - return -1; - } - } + fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (fd < 0) { + bb_perror_msg("socket failed"); + return -1; + } - if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { - DEBUG("adapter index %d", ifr.ifr_ifindex); - *ifindex = ifr.ifr_ifindex; - } else { - bb_perror_msg("SIOCGIFINDEX failed"); + ifr.ifr_addr.sa_family = AF_INET; + strcpy(ifr.ifr_name, interface); + if (addr) { + if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { + bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?"); close(fd); return -1; } - if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { - memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); - DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", - arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); - } else { - bb_perror_msg("SIOCGIFHWADDR failed"); - close(fd); - return -1; - } - } else { - bb_perror_msg("socket failed"); + our_ip = (struct sockaddr_in *) &ifr.ifr_addr; + *addr = our_ip->sin_addr.s_addr; + DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); + } + + if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { + bb_perror_msg("SIOCGIFINDEX failed"); + close(fd); + return -1; + } + + DEBUG("adapter index %d", ifr.ifr_ifindex); + *ifindex = ifr.ifr_ifindex; + if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) { + bb_perror_msg("SIOCGIFHWADDR failed"); + close(fd); return -1; } - close(fd); + + memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); + DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", + arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); return 0; } @@ -102,7 +92,8 @@ int listen_socket(uint32_t ip, int port, char *inf) int n = 1; DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf); - if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) { bb_perror_msg("socket"); return -1; } |