c - Why does this code produce an incomplete IP address? -
i reading buffer socket (af_packet, sock_dgram, htons(eth_p_arp)) following code. using arp_frame struct access component parts of contained arp reply. inet_ntoa() returns correct first octet of ip other octets 0 producing 172.0.0.0.
question 1 why might happen? question 2 how can print r bytes of msg buffer hex in host byte order debug packet?
unsigned char msg[65535]; struct ether_arp *arp_frame = (struct ether_arp *)msg; while ((r = recv(sock, msg, sizeof(msg), 0))) { // skip it's not arp reply if (ntohs(arp_frame->arp_op) != arpop_reply) continue; (i = 0; < sonos_prefix_num; i++) { if (!memcmp(sonos_prefixes[i], arp_frame->arp_sha, 3)) { struct in_addr addr; addr.s_addr = *arp_frame->arp_spa; printf("blah: %lu\n", ntohl(*arp_frame->arp_spa)); printf("sonos found @ %s\n", inet_ntoa(addr)); } } }
struct ether_arp
looks this:
struct ether_arp { struct arphdr ea_hdr; /* fixed-size header */ u_int8_t arp_sha[eth_alen]; /* sender hardware address */ u_int8_t arp_spa[4]; /* sender protocol address */ u_int8_t arp_tha[eth_alen]; /* target hardware address */ u_int8_t arp_tpa[4]; /* target protocol address */ };
with in mind, think addr.s_addr = *arp_frame->arp_spa;
looks little fishy. arp_frame->arp_spa
yields u_int8_t[4]
, dereference pointer. think memcpy()
might more appropriate there.
Comments
Post a Comment