From: Roy Marples Date: Mon, 28 Jan 2008 16:32:04 +0000 (+0000) Subject: Use sizeof with the variable, not the declaration. X-Git-Tag: v3.2.3~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=692967eb0e3cc72c8579de467396b3deafe43356;p=thirdparty%2Fdhcpcd.git Use sizeof with the variable, not the declaration. --- diff --git a/client.c b/client.c index 8fd1c813..061a183c 100644 --- a/client.c +++ b/client.c @@ -195,7 +195,7 @@ static bool get_old_lease (state_t *state, const options_t *options) return (false); /* Vitaly important we remove the server information here */ - memset (&dhcp->serveraddress, 0, sizeof (struct in_addr)); + memset (&dhcp->serveraddress, 0, sizeof (dhcp->serveraddress)); memset (dhcp->servername, 0, sizeof (dhcp->servername)); #ifdef ENABLE_ARP @@ -205,9 +205,9 @@ static bool get_old_lease (state_t *state, const options_t *options) (! options->doipv4ll || arp_claim (iface, dhcp->address))))) { - memset (&dhcp->address, 0, sizeof (struct in_addr)); - memset (&dhcp->netmask, 0, sizeof (struct in_addr)); - memset (&dhcp->broadcast, 0, sizeof (struct in_addr)); + memset (&dhcp->address, 0, sizeof (dhcp->address)); + memset (&dhcp->netmask, 0, sizeof (dhcp->netmask)); + memset (&dhcp->broadcast, 0, sizeof (dhcp->broadcast)); return (false); } @@ -271,11 +271,11 @@ static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) if (! iroute) iroute = iface->previous_routes = - xmalloc (sizeof (route_t)); + xmalloc (sizeof (*iroute)); - memcpy (iroute, route, sizeof (route_t)); + memcpy (iroute, route, sizeof (*iroute)); if (route->next) { - iroute->next = xmalloc (sizeof (route_t)); + iroute->next = xmalloc (sizeof (*iroute)); iroute = iroute->next; } } @@ -397,7 +397,7 @@ static void drop_config (state_t *state, const options_t *options) configure (options, state->interface, state->dhcp, false); free_dhcp (state->dhcp); - memset (state->dhcp, 0, sizeof (dhcp_t)); + memset (state->dhcp, 0, sizeof (*state->dhcp)); } static int wait_for_packet (fd_set *rset, state_t *state, @@ -553,7 +553,7 @@ static int handle_timeout (state_t *state, const options_t *options) do_socket (state, SOCKET_CLOSED); free_dhcp (dhcp); - memset (dhcp, 0, sizeof (dhcp_t)); + memset (dhcp, 0, sizeof (*dhcp)); #ifdef ENABLE_INFO if (! options->test && @@ -567,7 +567,7 @@ static int handle_timeout (state_t *state, const options_t *options) if (options->dolastlease) return (-1); free_dhcp (dhcp); - memset (dhcp, 0, sizeof (dhcp_t)); + memset (dhcp, 0, sizeof (*dhcp)); } else if (errno == EINTR) return (0); } @@ -581,7 +581,7 @@ static int handle_timeout (state_t *state, const options_t *options) { logger (LOG_INFO, "probing for an IPV4LL address"); free_dhcp (dhcp); - memset (dhcp, 0, sizeof (dhcp_t)); + memset (dhcp, 0, sizeof (*dhcp)); if (ipv4ll_get_address (iface, dhcp) == -1) { if (! state->daemonised) return (-1); @@ -655,7 +655,7 @@ static int handle_timeout (state_t *state, const options_t *options) case STATE_BOUND: case STATE_RENEW_REQUESTED: if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) { - memset (&dhcp->address, 0, sizeof (struct in_addr)); + memset (&dhcp->address, 0, sizeof (dhcp->address)); state->state = STATE_INIT; state->xid = 0; break; @@ -674,7 +674,7 @@ static int handle_timeout (state_t *state, const options_t *options) break; case STATE_REBINDING: logger (LOG_ERR, "lost lease, attemping to rebind"); - memset (&dhcp->address, 0, sizeof (struct in_addr)); + memset (&dhcp->address, 0, sizeof (dhcp->address)); do_socket (state, SOCKET_OPEN); if (state->xid == 0) state->xid = (uint32_t) random (); @@ -710,7 +710,7 @@ static int handle_dhcp (state_t *state, int type, const options_t *options) state->timeout = 0; state->xid = 0; free_dhcp (dhcp); - memset (dhcp, 0, sizeof (dhcp_t)); + memset (dhcp, 0, sizeof (*dhcp)); /* If we constantly get NAKS then we should slowly back off */ if (state->nakoff > 0) { @@ -790,7 +790,7 @@ static int handle_dhcp (state_t *state, int type, const options_t *options) do_socket (state, SOCKET_CLOSED); free_dhcp (dhcp); - memset (dhcp, 0, sizeof (dhcp_t)); + memset (dhcp, 0, sizeof (*dhcp)); state->xid = 0; state->timeout = 0; state->state = STATE_INIT; @@ -922,7 +922,7 @@ static int handle_packet (state_t *state, const options_t *options) the first one fails for any reason, we can use the next. */ memset (&message, 0, sizeof (struct dhcpmessage_t)); - new_dhcp = xmalloc (sizeof (dhcp_t)); + new_dhcp = xmalloc (sizeof (*new_dhcp)); do { if (get_packet (iface, (unsigned char *) &message, @@ -938,7 +938,7 @@ static int handle_packet (state_t *state, const options_t *options) } logger (LOG_DEBUG, "got a packet with xid 0x%x", message.xid); - memset (new_dhcp, 0, sizeof (dhcp_t)); + memset (new_dhcp, 0, sizeof (*new_dhcp)); type = parse_dhcpmessage (new_dhcp, &message); if (type == -1) { logger (LOG_ERR, "failed to parse packet"); @@ -984,8 +984,8 @@ int dhcp_run (const options_t *options, int *pidfd) if (! iface) goto eexit; - state = xzalloc (sizeof (state_t)); - state->dhcp = xzalloc (sizeof (dhcp_t)); + state = xzalloc (sizeof (*state)); + state->dhcp = xzalloc (sizeof (*state->dhcp)); state->pidfd = pidfd; state->interface = iface; diff --git a/configure.c b/configure.c index 6e5b8ef8..476b38b6 100644 --- a/configure.c +++ b/configure.c @@ -426,7 +426,7 @@ static char *lookuphostname (char *hostname, const dhcp_t *dhcp, salen = sizeof (struct sockaddr); memset (&su.sa, 0, salen); su.sin.sin_family = AF_INET; - memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (struct in_addr)); + memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_addr)); if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) != 0) { @@ -551,9 +551,9 @@ int configure (const options_t *options, interface_t *iface, iface->previous_address, iface->previous_netmask); memset (&iface->previous_address, - 0, sizeof (struct in_addr)); + 0, sizeof (iface->previous_address)); memset (&iface->previous_netmask, - 0, sizeof (struct in_addr)); + 0, sizeof (iface->previous_netmask)); } } @@ -640,13 +640,13 @@ int configure (const options_t *options, interface_t *iface, if (remember >= 0) { if (! new_routes) { - new_routes = xmalloc (sizeof (route_t)); + new_routes = xmalloc (sizeof (*new_routes)); new_route = new_routes; } else { - new_route->next = xmalloc (sizeof (route_t)); + new_route->next = xmalloc (sizeof (*new_route)); new_route = new_route->next; } - memcpy (new_route, route, sizeof (route_t)); + memcpy (new_route, route, sizeof (*new_route)); new_route -> next = NULL; } #ifdef THERE_IS_NO_FORK @@ -693,10 +693,10 @@ int configure (const options_t *options, interface_t *iface, if (remember >= 0) { if (! new_routes) { - new_routes = xmalloc (sizeof (route_t)); + new_routes = xmalloc (sizeof (*new_routes)); new_route = new_routes; } else { - new_route->next = xmalloc (sizeof (route_t)); + new_route->next = xmalloc (sizeof (*new_route)); new_route = new_route->next; } new_route->destination.s_addr = dest.s_addr; @@ -767,9 +767,9 @@ int configure (const options_t *options, interface_t *iface, iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { memcpy (&iface->previous_address, - &dhcp->address, sizeof (struct in_addr)); + &dhcp->address, sizeof (iface->previous_address)); memcpy (&iface->previous_netmask, - &dhcp->netmask, sizeof (struct in_addr)); + &dhcp->netmask, sizeof (iface->previous_netmask)); exec_script (options->script, iface->infofile, "new"); } else exec_script (options->script, iface->infofile, "up"); diff --git a/dhcp.c b/dhcp.c index c5630470..42b99154 100644 --- a/dhcp.c +++ b/dhcp.c @@ -105,7 +105,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, if (type == DHCP_RELEASE) to.s_addr = dhcp->serveraddress.s_addr; - message = xzalloc (sizeof (dhcpmessage_t)); + message = xzalloc (sizeof (*message)); m = (unsigned char *) message; p = (unsigned char *) &message->options; @@ -377,7 +377,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, message_length = p - m; - packet = xzalloc (sizeof (struct udp_dhcp_packet)); + packet = xzalloc (sizeof (*packet)); make_dhcp_packet (packet, (unsigned char *) message, message_length, from, to); free (message); @@ -463,11 +463,11 @@ static route_t *decode_CSR(const unsigned char *p, int len) if (len < 5) return NULL; - first = xzalloc (sizeof (route_t)); + first = xzalloc (sizeof (*first)); route = first; while (q - p < len) { - memset (route, 0, sizeof (route_t)); + memset (route, 0, sizeof (*route)); cidr = *q++; if (cidr > 32) { @@ -498,7 +498,7 @@ static route_t *decode_CSR(const unsigned char *p, int len) /* We have another route */ if (q - p < len) { - route->next = xzalloc (sizeof (route_t)); + route->next = xzalloc (sizeof (*route)); route = route->next; } } @@ -637,10 +637,10 @@ static route_t *decode_routes (const unsigned char *data, int length) for (i = 0; i < length; i += 8) { if (routes) { - routes->next = xzalloc (sizeof (route_t)); + routes->next = xzalloc (sizeof (*routes)); routes = routes->next; } else - head = routes = xzalloc (sizeof (route_t)); + head = routes = xzalloc (sizeof (*head)); memcpy (&routes->destination.s_addr, data + i, 4); memcpy (&routes->gateway.s_addr, data + i + 4, 4); routes->netmask.s_addr = @@ -658,10 +658,10 @@ static route_t *decode_routers (const unsigned char *data, int length) for (i = 0; i < length; i += 4) { if (routes) { - routes->next = xzalloc (sizeof (route_t)); + routes->next = xzalloc (sizeof (*routes)); routes = routes->next; } else - head = routes = xzalloc (sizeof (route_t)); + head = routes = xzalloc (sizeof (*head)); memcpy (&routes->gateway.s_addr, data + i, 4); } diff --git a/dhcpcd.c b/dhcpcd.c index cb150fba..0f889257 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -167,8 +167,7 @@ int main(int argc, char **argv) openlog (PACKAGE, LOG_PID, LOG_LOCAL0); - options = xmalloc (sizeof (options_t)); - memset (options, 0, sizeof (options_t)); + options = xzalloc (sizeof (*options)); options->script = (char *) DEFAULT_SCRIPT; snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION); diff --git a/info.c b/info.c index 0e8f3baa..1c8302ee 100644 --- a/info.c +++ b/info.c @@ -424,7 +424,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp) } /* See if we can create a route */ - route = xzalloc (sizeof (route_t)); + route = xzalloc (sizeof (*route)); if (inet_aton (dest, &route->destination) == 0) { logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address", dest); @@ -456,7 +456,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp) } else if (strcmp (var, "GATEWAYS") == 0) { p = value; while ((value = strsep (&p, " "))) { - route_t *route = xzalloc (sizeof (route_t)); + route_t *route = xzalloc (sizeof (*route)); if (parse_address (&route->gateway, value, "GATEWAYS")) { if (dhcp->routes) { route_t *r = dhcp->routes; diff --git a/interface.c b/interface.c index b748a37b..b1e6542a 100644 --- a/interface.c +++ b/interface.c @@ -107,7 +107,7 @@ int inet_cidrtoaddr (int cidr, struct in_addr *addr) { } ocets = (cidr + 7) / 8; - memset (addr, 0, sizeof (struct in_addr)); + memset (addr, 0, sizeof (*addr)); if (ocets > 0) { memset (&addr->s_addr, 255, (size_t) ocets - 1); memset ((unsigned char *) &addr->s_addr + (ocets - 1), @@ -204,7 +204,7 @@ static int _do_interface (const char *ifname, /* Not all implementations return the needed buffer size for * SIOGIFCONF so we loop like so for all until it works */ - memset (&ifc, 0, sizeof (struct ifconf)); + memset (&ifc, 0, sizeof (ifc)); for (;;) { ifc.ifc_len = len; ifc.ifc_buf = xmalloc ((size_t) len); @@ -253,8 +253,7 @@ static int _do_interface (const char *ifname, if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) { struct sockaddr_dl sdl; - memcpy (&sdl, &ifr->ifr_addr, - sizeof (struct sockaddr_dl)); + memcpy (&sdl, &ifr->ifr_addr, sizeof (sdl)); *hwlen = sdl.sdl_alen; memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, (size_t) sdl.sdl_alen); @@ -264,8 +263,7 @@ static int _do_interface (const char *ifname, #endif if (ifr->ifr_addr.sa_family == AF_INET) { - memcpy (&address, &ifr->ifr_addr, - sizeof (struct sockaddr_in)); + memcpy (&address, &ifr->ifr_addr, sizeof (address)); if (flush) { struct sockaddr_in netmask; @@ -276,7 +274,7 @@ static int _do_interface (const char *ifname, continue; } memcpy (&netmask, &ifr->ifr_addr, - sizeof (struct sockaddr_in)); + sizeof (netmask)); if (del_address (ifname, address.sin_addr, @@ -315,7 +313,7 @@ interface_t *read_interface (const char *ifname, _unused int metric) if (! ifname) return NULL; - memset (&ifr, 0, sizeof (struct ifreq)); + memset (&ifr, 0, sizeof (ifr)); strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { @@ -406,7 +404,7 @@ interface_t *read_interface (const char *ifname, _unused int metric) } } - iface = xzalloc (sizeof (interface_t)); + iface = xzalloc (sizeof (*iface)); strlcpy (iface->name, ifname, IF_NAMESIZE); #ifdef ENABLE_INFO snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); @@ -441,7 +439,7 @@ int get_mtu (const char *ifname) return (-1); } - memset (&ifr, 0, sizeof (struct ifreq)); + memset (&ifr, 0, sizeof (ifr)); strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); r = ioctl (s, SIOCGIFMTU, &ifr); close (s); @@ -465,7 +463,7 @@ int set_mtu (const char *ifname, short int mtu) return (-1); } - memset (&ifr, 0, sizeof (struct ifreq)); + memset (&ifr, 0, sizeof (ifr)); logger (LOG_DEBUG, "setting MTU to %d", mtu); strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); ifr.ifr_mtu = mtu; @@ -554,8 +552,8 @@ static int do_address (const char *ifname, struct in_addr address, union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \ _s.sa = &_var; \ _s.sin->sin_family = AF_INET; \ - _s.sin->sin_len = sizeof (struct sockaddr_in); \ - memcpy (&_s.sin->sin_addr, &_addr, sizeof (struct in_addr)); \ + _s.sin->sin_len = sizeof (*_s.sin); \ + memcpy (&_s.sin->sin_addr, &_addr, sizeof (_s.sin->sin_addr)); \ } ADDADDR (ifa.ifra_addr, address); @@ -614,7 +612,7 @@ static int do_route (const char *ifname, return -1; } - memset (&rtm, 0, sizeof (struct rtm)); + memset (&rtm, 0, sizeof (rtm)); rtm.hdr.rtm_version = RTM_VERSION; rtm.hdr.rtm_seq = ++seq; @@ -709,7 +707,7 @@ static int send_netlink(struct nlmsghdr *hdr) return -1; } - memset (&nl, 0, sizeof (struct sockaddr_nl)); + memset (&nl, 0, sizeof (nl)); nl.nl_family = AF_NETLINK; if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) { logger (LOG_ERR, "bind: %s", strerror (errno)); @@ -717,11 +715,11 @@ static int send_netlink(struct nlmsghdr *hdr) return -1; } - memset (&iov, 0, sizeof (struct iovec)); + memset (&iov, 0, sizeof (iov)); iov.iov_base = hdr; iov.iov_len = hdr->nlmsg_len; - memset (&msg, 0, sizeof (struct msghdr)); + memset (&msg, 0, sizeof (msg)); msg.msg_name = &nl; msg.msg_namelen = sizeof (nl); msg.msg_iov = &iov; @@ -846,7 +844,7 @@ static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, uint32_t data) { - int len = RTA_LENGTH (sizeof (uint32_t)); + int len = RTA_LENGTH (sizeof (data)); struct rtattr *rta; if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) { @@ -858,7 +856,7 @@ static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, rta = NLMSG_TAIL (n); rta->rta_type = type; rta->rta_len = len; - memcpy (RTA_DATA (rta), &data, sizeof (uint32_t)); + memcpy (RTA_DATA (rta), &data, sizeof (data)); n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; return 0; @@ -888,7 +886,7 @@ static int do_address(const char *ifname, if (!ifname) return -1; - nlm = xzalloc (sizeof (struct nlma)); + nlm = xzalloc (sizeof (*nlm)); nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); nlm->hdr.nlmsg_flags = NLM_F_REQUEST; if (! del) @@ -905,13 +903,13 @@ static int do_address(const char *ifname, nlm->ifa.ifa_prefixlen = inet_ntocidr (netmask); /* This creates the aliased interface */ - add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_LABEL, + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LABEL, ifname, strlen (ifname) + 1); - add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_LOCAL, + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LOCAL, &address.s_addr, sizeof (address.s_addr)); if (! del) - add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_BROADCAST, + add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_BROADCAST, &broadcast.s_addr, sizeof (broadcast.s_addr)); retval = send_netlink (&nlm->hdr); @@ -940,7 +938,7 @@ static int do_route (const char *ifname, return -1; } - nlm = xzalloc (sizeof (struct nlmr)); + nlm = xzalloc (sizeof (*nlm)); nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); if (change) nlm->hdr.nlmsg_flags = NLM_F_REPLACE; @@ -965,15 +963,15 @@ static int do_route (const char *ifname, } nlm->rt.rtm_dst_len = inet_ntocidr (netmask); - add_attr_l (&nlm->hdr, sizeof (struct nlmr), RTA_DST, + add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_DST, &destination.s_addr, sizeof (destination.s_addr)); if (netmask.s_addr != INADDR_BROADCAST && destination.s_addr != gateway.s_addr) - add_attr_l (&nlm->hdr, sizeof (struct nlmr), RTA_GATEWAY, + add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_GATEWAY, &gateway.s_addr, sizeof (gateway.s_addr)); - add_attr_32 (&nlm->hdr, sizeof (struct nlmr), RTA_OIF, ifindex); - add_attr_32 (&nlm->hdr, sizeof (struct nlmr), RTA_PRIORITY, metric); + add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_OIF, ifindex); + add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_PRIORITY, metric); retval = send_netlink (&nlm->hdr); free (nlm); diff --git a/signal.c b/signal.c index cd6ea82c..a61cb234 100644 --- a/signal.c +++ b/signal.c @@ -142,7 +142,7 @@ int signal_setup (void) fcntl (signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1) logger (LOG_ERR ,"fcntl: %s", strerror (errno)); - memset (&sa, 0, sizeof (struct sigaction)); + memset (&sa, 0, sizeof (sa)); sa.sa_handler = signal_handler; sigemptyset (&sa.sa_mask); for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) diff --git a/socket.c b/socket.c index 76b8dc66..16daceec 100644 --- a/socket.c +++ b/socket.c @@ -188,22 +188,20 @@ void make_dhcp_packet(struct udp_dhcp_packet *packet, udp->uh_sport = htons (DHCP_CLIENT_PORT); udp->uh_dport = htons (DHCP_SERVER_PORT); - udp->uh_ulen = htons (sizeof (struct udphdr) + length); + udp->uh_ulen = htons (sizeof (*udp) + length); ip->ip_len = udp->uh_ulen; - udp->uh_sum = checksum ((unsigned char *) packet, - sizeof (struct udp_dhcp_packet)); + udp->uh_sum = checksum ((unsigned char *) packet, sizeof (*packet)); ip->ip_v = IPVERSION; ip->ip_hl = 5; ip->ip_id = 0; ip->ip_tos = IPTOS_LOWDELAY; - ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) + - length); + ip->ip_len = htons (sizeof (*ip) + sizeof (*udp) + length); ip->ip_id = 0; ip->ip_off = htons (IP_DF); /* Don't fragment */ ip->ip_ttl = IPDEFTTL; - ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip)); + ip->ip_sum = checksum ((unsigned char *) ip, sizeof (*ip)); } static int valid_dhcp_packet (unsigned char *data) @@ -230,21 +228,21 @@ static int valid_dhcp_packet (unsigned char *data) d.data = data; d.packet->ip.ip_sum = 0; if (ipsum != checksum ((unsigned char *) &d.packet->ip, - sizeof (struct ip))) + sizeof (d.packet->ip))) { logger (LOG_DEBUG, "bad IP header checksum, ignoring"); retval = -1; goto eexit; } - memcpy (&source, &d.packet->ip.ip_src, sizeof (struct in_addr)); - memcpy (&dest, &d.packet->ip.ip_dst, sizeof (struct in_addr)); - memset (&d.packet->ip, 0, sizeof (struct ip)); + memcpy (&source, &d.packet->ip.ip_src, sizeof (source)); + memcpy (&dest, &d.packet->ip.ip_dst, sizeof (dest)); + memset (&d.packet->ip, 0, sizeof (d.packet->ip)); d.packet->udp.uh_sum = 0; d.packet->ip.ip_p = IPPROTO_UDP; - memcpy (&d.packet->ip.ip_src, &source, sizeof (struct in_addr)); - memcpy (&d.packet->ip.ip_dst, &dest, sizeof (struct in_addr)); + memcpy (&d.packet->ip.ip_src, &source, sizeof (source)); + memcpy (&d.packet->ip.ip_dst, &dest, sizeof (dest)); d.packet->ip.ip_len = d.packet->udp.uh_ulen; if (udpsum && udpsum != checksum (d.data, bytes)) { logger (LOG_ERR, "bad UDP checksum, ignoring"); @@ -290,7 +288,7 @@ int open_socket (interface_t *iface, int protocol) return -1; } - memset (&ifr, 0, sizeof (struct ifreq)); + memset (&ifr, 0, sizeof (ifr)); strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); if (ioctl (fd, BIOCSETIF, &ifr) == -1) { logger (LOG_ERR, @@ -346,12 +344,12 @@ ssize_t send_packet (const interface_t *iface, int type, if (iface->family == ARPHRD_ETHER) { struct ether_header hw; - memset (&hw, 0, sizeof (struct ether_header)); + memset (&hw, 0, sizeof (hw)); memset (&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); hw.ether_type = htons (type); iov[0].iov_base = &hw; - iov[0].iov_len = sizeof (struct ether_header); + iov[0].iov_len = sizeof (hw); } else { logger (LOG_ERR, "unsupported interace type %d", iface->family); return -1; @@ -410,12 +408,12 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data, break; hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen; - payload = hdr.buffer + sizeof (struct ether_header); + payload = hdr.buffer + sizeof (*hdr.hw); /* If it's an ARP reply, then just send it back */ if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) { len = bpf.packet->bh_caplen - - sizeof (struct ether_header); + sizeof (*hdr.hw); memcpy (data, payload, len); have_data = true; } else { @@ -480,7 +478,7 @@ int open_socket (interface_t *iface, int protocol) return (-1); } - memset (&su, 0, sizeof (struct sockaddr_storage)); + memset (&su, 0, sizeof (su)); su.sll.sll_family = PF_PACKET; su.sll.sll_protocol = htons (protocol); if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { @@ -492,6 +490,7 @@ int open_socket (interface_t *iface, int protocol) } /* Install the DHCP filter */ + memset (&pf, 0, sizeof (pf)); if (protocol == ETHERTYPE_ARP) { pf.filter = arp_bpf_filter; pf.len = sizeof (arp_bpf_filter) / sizeof (arp_bpf_filter[0]); @@ -500,14 +499,14 @@ int open_socket (interface_t *iface, int protocol) pf.len = sizeof (dhcp_bpf_filter) / sizeof (dhcp_bpf_filter[0]); } if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, - &pf, sizeof (struct sock_fprog)) != 0) + &pf, sizeof (pf)) != 0) { logger (LOG_ERR, "SO_ATTACH_FILTER: %s", strerror (errno)); close (fd); return (-1); } - if (bind (fd, &su.sa, sizeof (struct sockaddr_storage)) == -1) + if (bind (fd, &su.sa, sizeof (su)) == -1) { logger (LOG_ERR, "bind: %s", strerror (errno)); close (fd); @@ -536,7 +535,7 @@ ssize_t send_packet (const interface_t *iface, int type, if (! iface) return (-1); - memset (&su, 0, sizeof (struct sockaddr_storage)); + memset (&su, 0, sizeof (su)); su.sll.sll_family = AF_PACKET; su.sll.sll_protocol = htons (type); @@ -555,7 +554,7 @@ ssize_t send_packet (const interface_t *iface, int type, memset (&su.sll.sll_addr, 0xff, iface->hwlen); if ((retval = sendto (iface->fd, data, len, 0, &su.sa, - sizeof (struct sockaddr_storage))) == -1) + sizeof (su))) == -1) logger (LOG_ERR, "sendto: %s", strerror (errno)); return (retval);