24 |
*/ |
*/ |
25 |
|
|
26 |
#include <net/if.h> |
#include <net/if.h> |
27 |
#include <features.h> |
//#include <features.h> |
28 |
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION |
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION |
29 |
#include <netpacket/packet.h> |
#include <netpacket/packet.h> |
30 |
#include <net/ethernet.h> |
#include <net/ethernet.h> |
37 |
#include "common.h" |
#include "common.h" |
38 |
|
|
39 |
|
|
40 |
int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) |
int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) |
41 |
{ |
{ |
42 |
int fd; |
int fd; |
43 |
struct ifreq ifr; |
struct ifreq ifr; |
47 |
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
48 |
|
|
49 |
ifr.ifr_addr.sa_family = AF_INET; |
ifr.ifr_addr.sa_family = AF_INET; |
50 |
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); |
strncpy_IFNAMSIZ(ifr.ifr_name, interface); |
51 |
if (addr) { |
if (nip) { |
52 |
if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr, |
if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr, |
53 |
"is interface %s up and configured?", interface) |
"is interface %s up and configured?", interface) |
54 |
) { |
) { |
56 |
return -1; |
return -1; |
57 |
} |
} |
58 |
our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
59 |
*addr = our_ip->sin_addr.s_addr; |
*nip = our_ip->sin_addr.s_addr; |
60 |
DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); |
log1("IP %s", inet_ntoa(our_ip->sin_addr)); |
61 |
} |
} |
62 |
|
|
63 |
if (ifindex) { |
if (ifindex) { |
65 |
close(fd); |
close(fd); |
66 |
return -1; |
return -1; |
67 |
} |
} |
68 |
DEBUG("adapter index %d", ifr.ifr_ifindex); |
log1("Adapter index %d", ifr.ifr_ifindex); |
69 |
*ifindex = ifr.ifr_ifindex; |
*ifindex = ifr.ifr_ifindex; |
70 |
} |
} |
71 |
|
|
72 |
if (arp) { |
if (mac) { |
73 |
if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) { |
if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) { |
74 |
close(fd); |
close(fd); |
75 |
return -1; |
return -1; |
76 |
} |
} |
77 |
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); |
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6); |
78 |
DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", |
log1("MAC %02x:%02x:%02x:%02x:%02x:%02x", |
79 |
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); |
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
80 |
} |
} |
81 |
|
|
82 |
close(fd); |
close(fd); |
90 |
int fd; |
int fd; |
91 |
struct sockaddr_in addr; |
struct sockaddr_in addr; |
92 |
|
|
93 |
DEBUG("Opening listen socket on *:%d %s", port, inf); |
log1("Opening listen socket on *:%d %s", port, inf); |
94 |
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
95 |
|
|
96 |
setsockopt_reuseaddr(fd); |
setsockopt_reuseaddr(fd); |