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 @@ | |||
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
24 | */ | 24 | */ |
25 | 25 | ||
26 | #include <sys/types.h> | ||
27 | #include <sys/socket.h> | ||
28 | #include <sys/ioctl.h> | ||
29 | #include <netinet/in.h> | ||
30 | #include <unistd.h> | ||
31 | #include <string.h> | ||
32 | #include <arpa/inet.h> | ||
33 | #include <net/if.h> | 26 | #include <net/if.h> |
34 | #include <errno.h> | ||
35 | #include <features.h> | 27 | #include <features.h> |
36 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION | 28 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION |
37 | #include <netpacket/packet.h> | 29 | #include <netpacket/packet.h> |
@@ -43,7 +35,7 @@ | |||
43 | #endif | 35 | #endif |
44 | 36 | ||
45 | #include "common.h" | 37 | #include "common.h" |
46 | #include "socket.h" | 38 | |
47 | 39 | ||
48 | int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) | 40 | int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) |
49 | { | 41 | { |
@@ -52,44 +44,42 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) | |||
52 | struct sockaddr_in *our_ip; | 44 | struct sockaddr_in *our_ip; |
53 | 45 | ||
54 | memset(&ifr, 0, sizeof(struct ifreq)); | 46 | memset(&ifr, 0, sizeof(struct ifreq)); |
55 | if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { | 47 | fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
56 | ifr.ifr_addr.sa_family = AF_INET; | 48 | if (fd < 0) { |
57 | strcpy(ifr.ifr_name, interface); | 49 | bb_perror_msg("socket failed"); |
58 | 50 | return -1; | |
59 | if (addr) { | 51 | } |
60 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { | ||
61 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; | ||
62 | *addr = our_ip->sin_addr.s_addr; | ||
63 | DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); | ||
64 | } else { | ||
65 | bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?"); | ||
66 | close(fd); | ||
67 | return -1; | ||
68 | } | ||
69 | } | ||
70 | 52 | ||
71 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { | 53 | ifr.ifr_addr.sa_family = AF_INET; |
72 | DEBUG("adapter index %d", ifr.ifr_ifindex); | 54 | strcpy(ifr.ifr_name, interface); |
73 | *ifindex = ifr.ifr_ifindex; | 55 | if (addr) { |
74 | } else { | 56 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
75 | bb_perror_msg("SIOCGIFINDEX failed"); | 57 | bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?"); |
76 | close(fd); | 58 | close(fd); |
77 | return -1; | 59 | return -1; |
78 | } | 60 | } |
79 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { | 61 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
80 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); | 62 | *addr = our_ip->sin_addr.s_addr; |
81 | DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", | 63 | DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); |
82 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); | 64 | } |
83 | } else { | 65 | |
84 | bb_perror_msg("SIOCGIFHWADDR failed"); | 66 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { |
85 | close(fd); | 67 | bb_perror_msg("SIOCGIFINDEX failed"); |
86 | return -1; | 68 | close(fd); |
87 | } | 69 | return -1; |
88 | } else { | 70 | } |
89 | bb_perror_msg("socket failed"); | 71 | |
72 | DEBUG("adapter index %d", ifr.ifr_ifindex); | ||
73 | *ifindex = ifr.ifr_ifindex; | ||
74 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) { | ||
75 | bb_perror_msg("SIOCGIFHWADDR failed"); | ||
76 | close(fd); | ||
90 | return -1; | 77 | return -1; |
91 | } | 78 | } |
92 | close(fd); | 79 | |
80 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); | ||
81 | DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", | ||
82 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); | ||
93 | return 0; | 83 | return 0; |
94 | } | 84 | } |
95 | 85 | ||
@@ -102,7 +92,8 @@ int listen_socket(uint32_t ip, int port, char *inf) | |||
102 | int n = 1; | 92 | int n = 1; |
103 | 93 | ||
104 | DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf); | 94 | DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf); |
105 | if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { | 95 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
96 | if (fd < 0) { | ||
106 | bb_perror_msg("socket"); | 97 | bb_perror_msg("socket"); |
107 | return -1; | 98 | return -1; |
108 | } | 99 | } |