From 03f46b26b602592faa57653e1bd94db0b94b987a Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Tue, 9 Aug 2011 14:49:07 -0700 Subject: Clarified types and sizes of DHCP/BOOTP chaddr, sname, and file. chaddr is a hardware address, with size specified seperately, whereas sname and file are null terminated strings. --- libnet/include/libnet/libnet-functions.h | 16 ++++++++-------- libnet/include/libnet/libnet-headers.h | 6 +++--- libnet/src/libnet_build_dhcp.c | 31 ++++++++++--------------------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/libnet/include/libnet/libnet-functions.h b/libnet/include/libnet/libnet-functions.h index eff534e..949aaa4 100644 --- a/libnet/include/libnet/libnet-functions.h +++ b/libnet/include/libnet/libnet-functions.h @@ -1476,9 +1476,9 @@ libnet_ptag_t ptag); * @param yip * @param sip * @param gip - * @param chaddr - * @param sname - * @param file + * @param chaddr, client hardware address, length is hlen + * @param sname, server host name, a null terminated string + * @param file, boot file name, a null terminated string * @param payload optional payload or NULL * @param payload_s payload length or 0 * @param l pointer to a libnet context @@ -1489,7 +1489,7 @@ libnet_ptag_t libnet_build_dhcpv4(uint8_t opcode, uint8_t htype, uint8_t hlen, uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags, uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr, -uint8_t *sname, const uint8_t *file, const uint8_t* payload, uint32_t payload_s, +const char *sname, const char *file, const uint8_t* payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag); /** @@ -1504,9 +1504,9 @@ libnet_t *l, libnet_ptag_t ptag); * @param yip * @param sip * @param gip - * @param chaddr - * @param sname - * @param file + * @param chaddr, client hardware address, length is hlen + * @param sname, server host name, a null terminated string + * @param file, boot file name, a null terminated string * @param payload optional payload or NULL * @param payload_s payload length or 0 * @param l pointer to a libnet context @@ -1517,7 +1517,7 @@ libnet_ptag_t libnet_build_bootpv4(uint8_t opcode, uint8_t htype, uint8_t hlen, uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags, uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr, -uint8_t *sname, const uint8_t *file, const uint8_t* payload, uint32_t payload_s, +const char *sname, const char *file, const uint8_t* payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag); /** diff --git a/libnet/include/libnet/libnet-headers.h b/libnet/include/libnet/libnet-headers.h index 009c6cf..11065c7 100644 --- a/libnet/include/libnet/libnet-headers.h +++ b/libnet/include/libnet/libnet-headers.h @@ -364,9 +364,9 @@ struct libnet_dhcpv4_hdr uint32_t dhcp_yip; /* your IP */ uint32_t dhcp_sip; /* server's IP */ uint32_t dhcp_gip; /* gateway IP */ - uint8_t dhcp_chaddr[16]; /* client hardware address */ - uint8_t dhcp_sname[64]; /* server host name */ - uint8_t dhcp_file[128]; /* boot file name */ + uint8_t dhcp_chaddr[16]; /* client hardware address, len is dhcp_hlen */ + char dhcp_sname[64]; /* server host name, null terminated string */ + char dhcp_file[128]; /* boot file name, null terminated string */ uint32_t dhcp_magic; /* BOOTP magic header */ #define DHCP_MAGIC 0x63825363 #define LIBNET_BOOTP_MIN_LEN 0x12c diff --git a/libnet/src/libnet_build_dhcp.c b/libnet/src/libnet_build_dhcp.c index d147958..7d3f172 100644 --- a/libnet/src/libnet_build_dhcp.c +++ b/libnet/src/libnet_build_dhcp.c @@ -43,7 +43,7 @@ libnet_ptag_t libnet_build_dhcpv4(uint8_t opcode, uint8_t htype, uint8_t hlen, uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags, uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr, -uint8_t *sname, const uint8_t *file, const uint8_t *payload, uint32_t payload_s, +const char *sname, const char *file, const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag) { uint32_t n, h; @@ -82,32 +82,21 @@ libnet_t *l, libnet_ptag_t ptag) dhcp_hdr.dhcp_gip = htonl(gip); if (chaddr) - { - strncpy((char *)dhcp_hdr.dhcp_chaddr, (const char *)chaddr, sizeof (dhcp_hdr.dhcp_chaddr) - 2); - } - else { - memset(dhcp_hdr.dhcp_chaddr, 0, sizeof (dhcp_hdr.dhcp_chaddr)); + size_t n = sizeof (dhcp_hdr.dhcp_chaddr); + if (hlen < n) + n = hlen; + memcpy(dhcp_hdr.dhcp_chaddr, chaddr, n); } - if (sname) { - strncpy((const char *)dhcp_hdr.dhcp_sname, (char *)sname, sizeof (dhcp_hdr.dhcp_sname) - 2); - } - else - { - memset(dhcp_hdr.dhcp_sname, 0, sizeof (dhcp_hdr.dhcp_sname)); + strncpy(dhcp_hdr.dhcp_sname, sname, sizeof (dhcp_hdr.dhcp_sname) - 1); } - if (file) { - strncpy(dhcp_hdr.dhcp_file, file, sizeof (dhcp_hdr.dhcp_file) - 2); - } - else - { - memset(dhcp_hdr.dhcp_file, 0, sizeof(dhcp_hdr.dhcp_file)); + strncpy(dhcp_hdr.dhcp_file, file, sizeof (dhcp_hdr.dhcp_file) - 1); } - dhcp_hdr.dhcp_magic = htonl(DHCP_MAGIC); /* should this be tunable? */ + dhcp_hdr.dhcp_magic = htonl(DHCP_MAGIC); n = libnet_pblock_append(l, p, (uint8_t *)&dhcp_hdr, LIBNET_DHCPV4_H); if (n == -1) @@ -142,7 +131,7 @@ libnet_ptag_t libnet_build_bootpv4(uint8_t opcode, uint8_t htype, uint8_t hlen, uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags, uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr, -uint8_t *sname, const uint8_t *file, const uint8_t *payload, uint32_t payload_s, +const char *sname, const char *file, const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag) { return (libnet_build_dhcpv4(opcode, htype, hlen, hopcount, xid, secs, @@ -150,4 +139,4 @@ libnet_t *l, libnet_ptag_t ptag) l, ptag)); } -/* EOF */ \ No newline at end of file +/* EOF */ -- cgit v1.2.1