#endif
#include <errno.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct msghdr msg;
static unsigned int seq;
char buffer[16384];
- struct nlmsghdr *h;
+ union
+ {
+ char *buffer;
+ struct nlmsghdr *nlm;
+ } h;
if ((s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
{
goto eexit;
}
- for (h = (struct nlmsghdr *) buffer; bytes >= (signed) sizeof (*h); )
+ for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); )
{
- int len = h->nlmsg_len;
- int l = len - sizeof (*h);
+ int len = h.nlm->nlmsg_len;
+ int l = len - sizeof (*h.nlm);
if (l < 0 || len > bytes)
{
}
if (nl.nl_pid != 0 ||
- (pid_t) h->nlmsg_pid != mypid ||
- h->nlmsg_seq != seq)
+ (pid_t) h.nlm->nlmsg_pid != mypid ||
+ h.nlm->nlmsg_seq != seq)
/* Message isn't for us, so skip it */
goto next;
/* We get an NLMSG_ERROR back with a code of zero for success */
- if (h->nlmsg_type == NLMSG_ERROR)
+ if (h.nlm->nlmsg_type == NLMSG_ERROR)
{
- struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
+ struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm);
if ((unsigned) l < sizeof (struct nlmsgerr))
logger (LOG_ERR, "truncated error message");
else
logger (LOG_ERR, "unexpected reply");
next:
bytes -= NLMSG_ALIGN (len);
- h = (struct nlmsghdr *) ((char *) h + NLMSG_ALIGN (len));
+ h.buffer += NLMSG_ALIGN (len);
}
if (msg.msg_flags & MSG_TRUNC)
}
#define NLMSG_TAIL(nmsg) \
- ((struct rtattr *) (((unsigned char *) (nmsg)) \
- + NLMSG_ALIGN((nmsg)->nlmsg_len)))
-static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, const void *data,
- int alen)
+ ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len)))
+
+static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
+ const void *data, int alen)
{
int len = RTA_LENGTH(alen);
struct rtattr *rta;
return 0;
}
-static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, uint32_t data)
+static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type,
+ uint32_t data)
{
int len = RTA_LENGTH (sizeof (uint32_t));
struct rtattr *rta;
+
if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
{
logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", maxlen);
unsigned char *buffer, int *buffer_len, int *buffer_pos)
{
long bytes;
- struct udp_dhcp_packet *dhcp;
+ union
+ {
+ unsigned char *buffer;
+ struct udp_dhcp_packet *packet;
+ } pay;
/* We don't use the given buffer, but we need to rewind the position */
*buffer_pos = 0;
return -1;
}
- dhcp = (struct udp_dhcp_packet *) buffer;
- if (bytes < ntohs (dhcp->ip.ip_len))
+ pay.buffer = buffer;
+ if (bytes < ntohs (pay.packet->ip.ip_len))
{
logger (LOG_DEBUG, "truncated packet, ignoring");
return -1;
}
- bytes = ntohs (dhcp->ip.ip_len);
+ bytes = ntohs (pay.packet->ip.ip_len);
/* This is like our BPF filter above */
- if (dhcp->ip.ip_p != IPPROTO_UDP || dhcp->ip.ip_v != IPVERSION ||
- dhcp->ip.ip_hl != sizeof (dhcp->ip) >> 2 ||
- dhcp->udp.uh_dport != htons (DHCP_CLIENT_PORT) ||
+ if (pay.packet->ip.ip_p != IPPROTO_UDP || pay.packet->ip.ip_v != IPVERSION ||
+ pay.packet->ip.ip_hl != sizeof (pay.packet->ip) >> 2 ||
+ pay.packet->udp.uh_dport != htons (DHCP_CLIENT_PORT) ||
bytes > (int) sizeof (struct udp_dhcp_packet) ||
- ntohs (dhcp->udp.uh_ulen) != (uint16_t) (bytes - sizeof (dhcp->ip)))
+ ntohs (pay.packet->udp.uh_ulen)
+ != (uint16_t) (bytes - sizeof (pay.packet->ip)))
{
return -1;
}
if (valid_dhcp_packet (buffer) < 0)
return -1;
- memcpy(data, &dhcp->dhcp, bytes - (sizeof (dhcp->ip) +
- sizeof (dhcp->udp)));
+ memcpy(data, &pay.packet->dhcp,
+ bytes - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)));
- return bytes - (sizeof (dhcp->ip) + sizeof (dhcp->udp));
+ return bytes - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp));
}
#else