/* waits for a DHCPOFFER message from one or more DHCP servers */ int get_dhcp_offer(int sock){ dhcp_packet offer_packet; struct sockaddr_in source; struct sockaddr_in via; int result=OK; int responses=0; int x; time_t start_time; time_t current_time; time(&start_time); /* receive as many responses as we can */ for(responses=0,valid_responses=0;;){ time(¤t_time); if((current_time-start_time)>=dhcpoffer_timeout) break; if(verbose) printf("\n\n"); bzero(&source,sizeof(source)); bzero(&via,sizeof(via)); bzero(&offer_packet,sizeof(offer_packet)); result=OK; result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source); if(result!=OK){ if(verbose) printf(_("Result=ERROR\n")); continue; } else{ if(verbose) printf(_("Result=OK\n")); responses++; } /* The "source" is either a server or a relay. */ /* Save a copy of "source" into "via" even if it's via itself */ memcpy(&via,&source,sizeof(source)) ; if(verbose){ printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr)); printf(_(" via %s\n"),inet_ntoa(via.sin_addr)); printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid)); } /* check packet xid to see if its the same as the one we used in the discover packet */ if(ntohl(offer_packet.xid)!=packet_xid){ if(verbose) printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid); continue; } /* check hardware address */ result=OK; if(verbose) printf("DHCPOFFER chaddr: "); for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){ if(verbose) printf("%02X",(unsigned char)offer_packet.chaddr[x]); if(offer_packet.chaddr[x]!=client_hardware_address[x]) result=ERROR; } if(verbose) printf("\n"); if(result==ERROR){ if(verbose) printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); continue; } if(verbose){ printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr)); printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr)); printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr)); printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr)); } add_dhcp_offer(source.sin_addr,&offer_packet); valid_responses++; } if(verbose){ printf(_("Total responses seen on the wire: %d\n"),responses); printf(_("Valid responses for this machine: %d\n"),valid_responses); } return OK; }
/* waits for a DHCPOFFER message from one or more DHCP servers */ int get_dhcp_offer(int sock){ dhcp_packet offer_packet; struct sockaddr_in source; int result=OK; int timeout=1; int responses=0; int x; time_t start_time; time_t current_time; time(&start_time); /* receive as many responses as we can */ for(responses=0,valid_responses=0;;){ time(¤t_time); if((current_time-start_time)>=dhcpoffer_timeout) break; #ifdef DEBUG printf("\n\n"); #endif bzero(&source,sizeof(source)); bzero(&offer_packet,sizeof(offer_packet)); result=OK; result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source); if(result!=OK){ #ifdef DEBUG printf("Result=ERROR\n"); #endif continue; } else{ #ifdef DEBUG printf("Result=OK\n"); #endif responses++; } #ifdef DEBUG printf("DHCPOFFER from IP address %s\n",inet_ntoa(source.sin_addr)); printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid)); #endif /* check packet xid to see if its the same as the one we used in the discover packet */ if(ntohl(offer_packet.xid)!=packet_xid){ #ifdef DEBUG printf("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n",ntohl(offer_packet.xid),packet_xid); #endif continue; } /* check hardware address */ result=OK; #ifdef DEBUG printf("DHCPOFFER chaddr: "); #endif for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){ #ifdef DEBUG printf("%02X",(unsigned char)offer_packet.chaddr[x]); #endif if(offer_packet.chaddr[x]!=client_hardware_address[x]){ result=ERROR; } } #ifdef DEBUG printf("\n"); #endif if(result==ERROR){ #ifdef DEBUG printf("DHCPOFFER hardware address did not match our own - ignoring packet\n"); #endif continue; } #ifdef DEBUG printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr)); printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr)); printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr)); printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr)); #endif add_dhcp_offer(source.sin_addr,&offer_packet); valid_responses++; } #ifdef DEBUG printf("Total responses seen on the wire: %d\n",responses); printf("Valid responses for this machine: %d\n",valid_responses); #endif return OK; }