Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/networking/udhcp/clientpacket.c

    r821 r1765  
     1/* vi: set sw=4 ts=4: */
    12/* clientpacket.c
    23 *
     
    89 */
    910
    10 #include <string.h>
    11 #include <sys/socket.h>
    1211#include <features.h>
    13 #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
     12#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
    1413#include <netpacket/packet.h>
    1514#include <net/ethernet.h>
     
    1918#include <linux/if_ether.h>
    2019#endif
    21 #include <stdlib.h>
    22 #include <time.h>
    23 #include <unistd.h>
    24 #include <netinet/in.h>
    25 #include <arpa/inet.h>
    26 #include <fcntl.h>
    27 
    28 
     20
     21#include "common.h"
    2922#include "dhcpd.h"
    30 #include "clientpacket.h"
     23#include "dhcpc.h"
    3124#include "options.h"
    32 #include "dhcpc.h"
    33 #include "common.h"
    3425
    3526
    3627/* Create a random xid */
    37 unsigned long random_xid(void)
    38 {
    39     static int initialized;
     28uint32_t random_xid(void)
     29{
     30    static smallint initialized;
     31
    4032    if (!initialized) {
    41         int fd;
    42         unsigned long seed;
    43 
    44         fd = open("/dev/urandom", 0);
    45         if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
    46             LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
    47             seed = time(0);
    48         }
    49         if (fd >= 0) close(fd);
    50         srand(seed);
    51         initialized++;
     33        srand(monotonic_us());
     34        initialized = 1;
    5235    }
    5336    return rand();
     
    6144    memcpy(packet->chaddr, client_config.arp, 6);
    6245    if (client_config.clientid)
    63         add_option_string(packet->options, client_config.clientid);
    64     if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
    65     if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
     46        add_option_string(packet->options, client_config.clientid);
     47    if (client_config.hostname)
     48        add_option_string(packet->options, client_config.hostname);
     49    if (client_config.fqdn)
     50        add_option_string(packet->options, client_config.fqdn);
    6651    add_option_string(packet->options, client_config.vendorclass);
    6752}
     
    8772
    8873/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
    89 int send_discover(unsigned long xid, unsigned long requested)
     74int send_discover(uint32_t xid, uint32_t requested)
    9075{
    9176    struct dhcpMessage packet;
     
    9782
    9883    add_requests(&packet);
    99     LOG(LOG_DEBUG, "Sending discover...");
     84    bb_info_msg("Sending discover...");
     85    return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
     86            SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
     87}
     88
     89
     90/* Broadcasts a DHCP request message */
     91int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
     92{
     93    struct dhcpMessage packet;
     94    struct in_addr addr;
     95
     96    init_packet(&packet, DHCPREQUEST);
     97    packet.xid = xid;
     98
     99    add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
     100    add_simple_option(packet.options, DHCP_SERVER_ID, server);
     101
     102    add_requests(&packet);
     103    addr.s_addr = requested;
     104    bb_info_msg("Sending select for %s...", inet_ntoa(addr));
    100105    return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
    101106                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
     
    103108
    104109
    105 /* Broadcasts a DHCP request message */
    106 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
    107 {
    108     struct dhcpMessage packet;
    109     struct in_addr addr;
    110 
    111     init_packet(&packet, DHCPREQUEST);
    112     packet.xid = xid;
    113 
    114     add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
    115     add_simple_option(packet.options, DHCP_SERVER_ID, server);
    116 
    117     add_requests(&packet);
    118     addr.s_addr = requested;
    119     LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
    120     return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
    121                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
    122 }
    123 
    124 
    125110/* Unicasts or broadcasts a DHCP renew message */
    126 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
    127 {
    128     struct dhcpMessage packet;
    129     int ret = 0;
     111int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
     112{
     113    struct dhcpMessage packet;
    130114
    131115    init_packet(&packet, DHCPREQUEST);
     
    134118
    135119    add_requests(&packet);
    136     LOG(LOG_DEBUG, "Sending renew...");
     120    bb_info_msg("Sending renew...");
    137121    if (server)
    138         ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
    139     else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
     122        return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
     123
     124    return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
    140125                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
    141     return ret;
    142126}
    143127
    144128
    145129/* Unicasts a DHCP release message */
    146 int send_release(unsigned long server, unsigned long ciaddr)
     130int send_release(uint32_t server, uint32_t ciaddr)
    147131{
    148132    struct dhcpMessage packet;
     
    155139    add_simple_option(packet.options, DHCP_SERVER_ID, server);
    156140
    157     LOG(LOG_DEBUG, "Sending release...");
     141    bb_info_msg("Sending release...");
    158142    return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
    159143}
     
    171155    bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
    172156    if (bytes < 0) {
    173         DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
     157        DEBUG("Cannot read on raw listening socket - ignoring");
    174158        usleep(500000); /* possible down interface, looping condition */
    175159        return -1;
     
    177161
    178162    if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
    179         DEBUG(LOG_INFO, "message too short, ignoring");
     163        DEBUG("Message too short, ignoring");
    180164        return -2;
    181165    }
    182166
    183167    if (bytes < ntohs(packet.ip.tot_len)) {
    184         DEBUG(LOG_INFO, "Truncated packet");
     168        DEBUG("Truncated packet");
    185169        return -2;
    186170    }
     
    190174
    191175    /* Make sure its the right packet for us, and that it passes sanity checks */
    192     if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
    193         packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
    194         bytes > (int) sizeof(struct udp_dhcp_packet) ||
    195         ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
    196         DEBUG(LOG_INFO, "unrelated/bogus packet");
     176    if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
     177     || packet.ip.ihl != sizeof(packet.ip) >> 2
     178     || packet.udp.dest != htons(CLIENT_PORT)
     179     || bytes > (int) sizeof(struct udp_dhcp_packet)
     180     || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
     181    ) {
     182        DEBUG("Unrelated/bogus packet");
    197183        return -2;
    198184    }
     
    202188    packet.ip.check = 0;
    203189    if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
    204         DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
     190        DEBUG("bad IP header checksum, ignoring");
    205191        return -1;
    206192    }
     
    218204    packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
    219205    if (check && check != udhcp_checksum(&packet, bytes)) {
    220         DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
     206        bb_error_msg("packet with bad UDP checksum received, ignoring");
    221207        return -2;
    222208    }
     
    225211
    226212    if (ntohl(payload->cookie) != DHCP_MAGIC) {
    227         LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
    228         return -2;
    229     }
    230     DEBUG(LOG_INFO, "oooooh!!! got some!");
     213        bb_error_msg("received bogus message (bad magic) - ignoring");
     214        return -2;
     215    }
     216    DEBUG("oooooh!!! got some!");
    231217    return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
    232 
    233 }
     218}
Note: See TracChangeset for help on using the changeset viewer.