Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/networking/udhcp/clientpacket.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/networking/udhcp/clientpacket.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* clientpacket.c 2 3 * … … 8 9 */ 9 10 10 #include <string.h>11 #include <sys/socket.h>12 11 #include <features.h> 13 #if ( __GLIBC__ >= 2 && __GLIBC_MINOR>= 1) || defined _NEWLIB_VERSION12 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION 14 13 #include <netpacket/packet.h> 15 14 #include <net/ethernet.h> … … 19 18 #include <linux/if_ether.h> 20 19 #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" 29 22 #include "dhcpd.h" 30 #include " clientpacket.h"23 #include "dhcpc.h" 31 24 #include "options.h" 32 #include "dhcpc.h"33 #include "common.h"34 25 35 26 36 27 /* Create a random xid */ 37 unsigned long random_xid(void) 38 { 39 static int initialized; 28 uint32_t random_xid(void) 29 { 30 static smallint initialized; 31 40 32 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; 52 35 } 53 36 return rand(); … … 61 44 memcpy(packet->chaddr, client_config.arp, 6); 62 45 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); 66 51 add_option_string(packet->options, client_config.vendorclass); 67 52 } … … 87 72 88 73 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ 89 int send_discover(u nsigned long xid, unsigned longrequested)74 int send_discover(uint32_t xid, uint32_t requested) 90 75 { 91 76 struct dhcpMessage packet; … … 97 82 98 83 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 */ 91 int 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)); 100 105 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 101 106 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); … … 103 108 104 109 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 125 110 /* 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; 111 int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) 112 { 113 struct dhcpMessage packet; 130 114 131 115 init_packet(&packet, DHCPREQUEST); … … 134 118 135 119 add_requests(&packet); 136 LOG(LOG_DEBUG,"Sending renew...");120 bb_info_msg("Sending renew..."); 137 121 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, 140 125 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); 141 return ret;142 126 } 143 127 144 128 145 129 /* Unicasts a DHCP release message */ 146 int send_release(u nsigned long server, unsigned longciaddr)130 int send_release(uint32_t server, uint32_t ciaddr) 147 131 { 148 132 struct dhcpMessage packet; … … 155 139 add_simple_option(packet.options, DHCP_SERVER_ID, server); 156 140 157 LOG(LOG_DEBUG,"Sending release...");141 bb_info_msg("Sending release..."); 158 142 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); 159 143 } … … 171 155 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); 172 156 if (bytes < 0) { 173 DEBUG( LOG_INFO, "couldn't read on raw listening socket -- ignoring");157 DEBUG("Cannot read on raw listening socket - ignoring"); 174 158 usleep(500000); /* possible down interface, looping condition */ 175 159 return -1; … … 177 161 178 162 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { 179 DEBUG( LOG_INFO, "message too short, ignoring");163 DEBUG("Message too short, ignoring"); 180 164 return -2; 181 165 } 182 166 183 167 if (bytes < ntohs(packet.ip.tot_len)) { 184 DEBUG( LOG_INFO,"Truncated packet");168 DEBUG("Truncated packet"); 185 169 return -2; 186 170 } … … 190 174 191 175 /* 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"); 197 183 return -2; 198 184 } … … 202 188 packet.ip.check = 0; 203 189 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"); 205 191 return -1; 206 192 } … … 218 204 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ 219 205 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"); 221 207 return -2; 222 208 } … … 225 211 226 212 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!"); 231 217 return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); 232 233 } 218 }
Note:
See TracChangeset
for help on using the changeset viewer.