Changeset View
Changeset View
Standalone View
Standalone View
sbin/dhclient/dispatch.c
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include "dhcpd.h" | #include "dhcpd.h" | ||||
#include "privsep.h" | #include "privsep.h" | ||||
#include <sys/ioctl.h> | #include <sys/ioctl.h> | ||||
#include <assert.h> | |||||
#include <net/if_media.h> | #include <net/if_media.h> | ||||
#include <ifaddrs.h> | #include <ifaddrs.h> | ||||
#include <poll.h> | #include <poll.h> | ||||
/* Assert that pointer p is aligned to at least align bytes */ | |||||
#define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0) | |||||
struct protocol *protocols; | struct protocol *protocols; | ||||
struct timeout *timeouts; | struct timeout *timeouts; | ||||
static struct timeout *free_timeouts; | static struct timeout *free_timeouts; | ||||
static int interfaces_invalidated; | static int interfaces_invalidated; | ||||
void (*bootp_packet_handler)(struct interface_info *, | void (*bootp_packet_handler)(struct interface_info *, | ||||
struct dhcp_packet *, int, unsigned int, | struct dhcp_packet *, int, unsigned int, | ||||
struct iaddr, struct hardware *); | struct iaddr, struct hardware *); | ||||
static int interface_status(struct interface_info *ifinfo); | static int interface_status(struct interface_info *ifinfo); | ||||
/* | /* | ||||
* Use getifaddrs() to get a list of all the attached interfaces. For | * Use getifaddrs() to get a list of all the attached interfaces. For | ||||
* each interface that's of type INET and not the loopback interface, | * each interface that's of type INET and not the loopback interface, | ||||
* register that interface with the network I/O software, figure out | * register that interface with the network I/O software, figure out | ||||
* what subnet it's on, and add it to the list of interfaces. | * what subnet it's on, and add it to the list of interfaces. | ||||
*/ | */ | ||||
void | void | ||||
discover_interfaces(struct interface_info *iface) | discover_interfaces(struct interface_info *iface) | ||||
{ | { | ||||
struct ifaddrs *ifap, *ifa; | struct ifaddrs *ifap, *ifa; | ||||
struct sockaddr_in foo; | |||||
struct ifreq *tif; | struct ifreq *tif; | ||||
if (getifaddrs(&ifap) != 0) | if (getifaddrs(&ifap) != 0) | ||||
error("getifaddrs failed"); | error("getifaddrs failed"); | ||||
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { | for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { | ||||
if ((ifa->ifa_flags & IFF_LOOPBACK) || | if ((ifa->ifa_flags & IFF_LOOPBACK) || | ||||
(ifa->ifa_flags & IFF_POINTOPOINT) || | (ifa->ifa_flags & IFF_POINTOPOINT) || | ||||
(!(ifa->ifa_flags & IFF_UP))) | (!(ifa->ifa_flags & IFF_UP))) | ||||
continue; | continue; | ||||
if (strcmp(iface->name, ifa->ifa_name)) | if (strcmp(iface->name, ifa->ifa_name)) | ||||
continue; | continue; | ||||
/* | /* | ||||
* If we have the capability, extract link information | * If we have the capability, extract link information | ||||
* and record it in a linked list. | * and record it in a linked list. | ||||
*/ | */ | ||||
if (ifa->ifa_addr->sa_family == AF_LINK) { | if (ifa->ifa_addr->sa_family == AF_LINK) { | ||||
struct sockaddr_dl *foo = | struct sockaddr_dl *foo; | ||||
(struct sockaddr_dl *)ifa->ifa_addr; | |||||
/* | |||||
* The implementation of getifaddrs should guarantee | |||||
* this alignment | |||||
*/ | |||||
assert_aligned(ifa->ifa_addr, | |||||
_Alignof(struct sockaddr_dl*)); | |||||
cem: Should this be the _Alignof the pointer type or the struct type? | |||||
asomersAuthorUnsubmitted Not Done Inline ActionsGood catch. asomers: Good catch. | |||||
#ifdef __clang__ | |||||
#pragma clang diagnostic push | |||||
#pragma clang diagnostic ignored "-Wcast-align" | |||||
cemUnsubmitted Not Done Inline ActionsIs this needed with the NO_WCAST_ALIGN Makefile variable? cem: Is this needed with the NO_WCAST_ALIGN Makefile variable? | |||||
asomersAuthorUnsubmitted Not Done Inline ActionsTechnically no. But it will be needed if anybody ever reenables WCAST_ALIGN and fixes the other violations. asomers: Technically no. But it will be needed if anybody ever reenables `WCAST_ALIGN` and fixes the… | |||||
#endif | |||||
foo = (struct sockaddr_dl *)ifa->ifa_addr; | |||||
#ifdef __clang__ | |||||
#pragma clang diagnostic pop | |||||
#endif | |||||
iface->index = foo->sdl_index; | iface->index = foo->sdl_index; | ||||
iface->hw_address.hlen = foo->sdl_alen; | iface->hw_address.hlen = foo->sdl_alen; | ||||
iface->hw_address.htype = HTYPE_ETHER; /* XXX */ | iface->hw_address.htype = HTYPE_ETHER; /* XXX */ | ||||
memcpy(iface->hw_address.haddr, | memcpy(iface->hw_address.haddr, | ||||
LLADDR(foo), foo->sdl_alen); | LLADDR(foo), foo->sdl_alen); | ||||
} else if (ifa->ifa_addr->sa_family == AF_INET) { | } else if (ifa->ifa_addr->sa_family == AF_INET) { | ||||
struct sockaddr_in foo; | |||||
struct iaddr addr; | struct iaddr addr; | ||||
memcpy(&foo, ifa->ifa_addr, sizeof(foo)); | memcpy(&foo, ifa->ifa_addr, sizeof(foo)); | ||||
if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) | if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) | ||||
continue; | continue; | ||||
if (!iface->ifp) { | if (!iface->ifp) { | ||||
if ((tif = calloc(1, sizeof(struct ifreq))) | if ((tif = calloc(1, sizeof(struct ifreq))) | ||||
== NULL) | == NULL) | ||||
▲ Show 20 Lines • Show All 319 Lines • ▼ Show 20 Lines | cancel_timeout(void (*where)(void *), void *what) | ||||
if (q) { | if (q) { | ||||
q->next = free_timeouts; | q->next = free_timeouts; | ||||
free_timeouts = q; | free_timeouts = q; | ||||
} | } | ||||
} | } | ||||
/* Add a protocol to the list of protocols... */ | /* Add a protocol to the list of protocols... */ | ||||
void | void | ||||
add_protocol(char *name, int fd, void (*handler)(struct protocol *), | add_protocol(const char *name, int fd, void (*handler)(struct protocol *), | ||||
void *local) | void *local) | ||||
{ | { | ||||
struct protocol *p; | struct protocol *p; | ||||
p = malloc(sizeof(*p)); | p = malloc(sizeof(*p)); | ||||
if (!p) | if (!p) | ||||
error("can't allocate protocol struct for %s", name); | error("can't allocate protocol struct for %s", name); | ||||
▲ Show 20 Lines • Show All 97 Lines • Show Last 20 Lines |
Should this be the _Alignof the pointer type or the struct type?