Various cleanups I made while going through Erik Hovland's patch submissions,
[oweals/busybox.git] / networking / udhcp / clientpacket.c
index 7debac2ef106a63ab0185a754ad4d191ccc4b944..14e6c6678d9df4cb75a062a41edcc953500b90b4 100644 (file)
@@ -4,25 +4,13 @@
  *
  * Russ Dill <Russ.Dill@asu.edu> July 2001
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
+
 #include <string.h>
 #include <sys/socket.h>
 #include <features.h>
-#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
+#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
 #include <netpacket/packet.h>
 #include <net/ethernet.h>
 #else
@@ -39,6 +27,7 @@
 
 
 #include "dhcpd.h"
+#include "clientpacket.h"
 #include "options.h"
 #include "dhcpc.h"
 #include "common.h"
@@ -68,20 +57,17 @@ unsigned long random_xid(void)
 /* initialize a packet with the proper defaults */
 static void init_packet(struct dhcpMessage *packet, char type)
 {
-       struct vendor  {
-               char vendor, length;
-               char str[sizeof("udhcp "VERSION)];
-       } vendor_id = { DHCP_VENDOR,  sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
-       
-       init_header(packet, type);
+       udhcp_init_header(packet, type);
        memcpy(packet->chaddr, client_config.arp, 6);
-       add_option_string(packet->options, client_config.clientid);
+       if (client_config.clientid)
+           add_option_string(packet->options, client_config.clientid);
        if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
-       add_option_string(packet->options, (unsigned char *) &vendor_id);
+       if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
+       add_option_string(packet->options, client_config.vendorclass);
 }
 
 
-/* Add a paramater request list for stubborn DHCP servers. Pull the data
+/* Add a parameter request list for stubborn DHCP servers. Pull the data
  * from the struct in options.c. Don't do bounds checking here because it
  * goes towards the head of the packet. */
 static void add_requests(struct dhcpMessage *packet)
@@ -90,9 +76,9 @@ static void add_requests(struct dhcpMessage *packet)
        int i, len = 0;
 
        packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
-       for (i = 0; options[i].code; i++)
-               if (options[i].flags & OPTION_REQ)
-                       packet->options[end + OPT_DATA + len++] = options[i].code;
+       for (i = 0; dhcp_options[i].code; i++)
+               if (dhcp_options[i].flags & OPTION_REQ)
+                       packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
        packet->options[end + OPT_LEN] = len;
        packet->options[end + OPT_DATA + len] = DHCP_END;
 
@@ -111,7 +97,7 @@ int send_discover(unsigned long xid, unsigned long requested)
 
        add_requests(&packet);
        LOG(LOG_DEBUG, "Sending discover...");
-       return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
+       return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
                                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
@@ -127,11 +113,11 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques
 
        add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
        add_simple_option(packet.options, DHCP_SERVER_ID, server);
-       
+
        add_requests(&packet);
        addr.s_addr = requested;
        LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
-       return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
+       return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
                                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
@@ -148,12 +134,12 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
 
        add_requests(&packet);
        LOG(LOG_DEBUG, "Sending renew...");
-       if (server) 
-               ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
-       else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+       if (server)
+               ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+       else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
                                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
        return ret;
-}      
+}
 
 
 /* Unicasts a DHCP release message */
@@ -164,12 +150,12 @@ int send_release(unsigned long server, unsigned long ciaddr)
        init_packet(&packet, DHCPRELEASE);
        packet.xid = random_xid();
        packet.ciaddr = ciaddr;
-       
+
        add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
        add_simple_option(packet.options, DHCP_SERVER_ID, server);
 
        LOG(LOG_DEBUG, "Sending release...");
-       return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+       return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
 }
 
 
@@ -178,8 +164,8 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
 {
        int bytes;
        struct udp_dhcp_packet packet;
-       u_int32_t source, dest;
-       u_int16_t check;
+       uint32_t source, dest;
+       uint16_t check;
 
        memset(&packet, 0, sizeof(struct udp_dhcp_packet));
        bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
@@ -188,37 +174,37 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
                usleep(500000); /* possible down interface, looping condition */
                return -1;
        }
-       
+
        if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
                DEBUG(LOG_INFO, "message too short, ignoring");
                return -2;
        }
-       
+
        if (bytes < ntohs(packet.ip.tot_len)) {
                DEBUG(LOG_INFO, "Truncated packet");
                return -2;
        }
-       
+
        /* ignore any extra garbage bytes */
        bytes = ntohs(packet.ip.tot_len);
-       
+
        /* Make sure its the right packet for us, and that it passes sanity checks */
        if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
            packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
            bytes > (int) sizeof(struct udp_dhcp_packet) ||
-           ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
-               DEBUG(LOG_INFO, "unrelated/bogus packet");
-               return -2;
+           ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
+               DEBUG(LOG_INFO, "unrelated/bogus packet");
+               return -2;
        }
 
        /* check IP checksum */
        check = packet.ip.check;
        packet.ip.check = 0;
-       if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
+       if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
                DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
                return -1;
        }
-       
+
        /* verify the UDP checksum by replacing the header with a psuedo header */
        source = packet.ip.saddr;
        dest = packet.ip.daddr;
@@ -230,18 +216,18 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
        packet.ip.saddr = source;
        packet.ip.daddr = dest;
        packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
-       if (check && check != checksum(&packet, bytes)) {
+       if (check && check != udhcp_checksum(&packet, bytes)) {
                DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
                return -2;
        }
-       
+
        memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
-       
+
        if (ntohl(payload->cookie) != DHCP_MAGIC) {
                LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
                return -2;
        }
        DEBUG(LOG_INFO, "oooooh!!! got some!");
        return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
-       
+
 }