int main(int argc, char **argv){ int dhcp_socket; int result; if(process_arguments(argc,argv)!=OK){ /*usage("Invalid command arguments supplied\n");*/ printf("Invalid command arguments supplied\n"); exit(STATE_UNKNOWN); } /* create socket for DHCP communications */ dhcp_socket=create_dhcp_socket(); /* get hardware address of client machine */ get_hardware_address(dhcp_socket,network_interface_name); /* send DHCPDISCOVER packet */ send_dhcp_discover(dhcp_socket); /* wait for a DHCPOFFER packet */ get_dhcp_offer(dhcp_socket); /* close socket we created */ close_dhcp_socket(dhcp_socket); /* determine state/plugin output to return */ result=get_results(); /* free allocated memory */ free_dhcp_offer_list(); free_requested_server_list(); return result; }
int main(int argc, char **argv){ int dhcp_socket; int result = STATE_UNKNOWN; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts(&argc, argv, progname); if(process_arguments(argc,argv)!=OK){ usage4 (_("Could not parse arguments")); } /* this plugin almost certainly needs root permissions. */ np_warn_if_not_root(); /* create socket for DHCP communications */ dhcp_socket=create_dhcp_socket(); /* get hardware address of client machine */ if(user_specified_mac!=NULL) memcpy(client_hardware_address,user_specified_mac,6); else get_hardware_address(dhcp_socket,network_interface_name); if(unicast) /* get IP address of client machine */ get_ip_address(dhcp_socket,network_interface_name); /* send DHCPDISCOVER packet */ send_dhcp_discover(dhcp_socket); /* wait for a DHCPOFFER packet */ get_dhcp_offer(dhcp_socket); /* close socket we created */ close_dhcp_socket(dhcp_socket); /* determine state/plugin output to return */ result=get_results(); /* free allocated memory */ free_dhcp_offer_list(); free_requested_server_list(); return result; }
// Initial_tid can be a random number for every board. E.g the last digit // of the mac address. It is not so important that the number is random. // It is more important that it is unique and no other board on the same // Lan has the same number. This is because there might be a power outage // and all boards reboot afterwards at the same time. At that moment they // must all have different TIDs otherwise there will be an IP address mess-up. // // The function returns 1 once we have a valid IP. // At this point you must not call the function again. uint8_t packetloop_dhcp_initial_ip_assignment(uint8_t *buf,uint16_t plen,uint8_t initial_tid){ static uint16_t init=0x5fff; // about 2 sec delay uint8_t cmd; // do nothing if the link is down if (!enc28j60linkup()) return(0); // first time that this function is called: if (plen==0){ if (init==2){ init=1; dhcp_6sec_cnt=0; dhcp_tid=initial_tid; // Reception of broadcast packets is turned off by default, but // the DHCP offer message that the DHCP server sends will be // a broadcast packet. Enable here and disable later. enc28j60EnableBroadcast(); send_dhcp_discover(buf,dhcp_tid); return(0); } if (dhcp_yiaddr[0]==0 && dhcp_6sec_cnt > 5){ // still no IP after 30 sec dhcp_tid++; dhcp_6sec_cnt=0; // Reception of broadcast packets is turned off by default, but // the DHCP offer message that the DHCP server sends will be // a broadcast packet. Enable here and disable later. enc28j60EnableBroadcast(); send_dhcp_discover(buf,dhcp_tid); return(0); } // We have a little delay (about 2sec) at startup. Sometimes // the power fluctuates or the programmer causes // the board to reset and then immediately reset again. // We wait with the sending of a send_dhcp_discover just in case // we did already so at the last reset which was possibly less // than a second ago. if (init>2){ init--; } return(0); } // plen > 0 if (is_dhcp_msg_for_me(buf,plen,dhcp_tid)){ // It's really a borderline case that we the the dhcp_is_renew_tid // function call for. It could only happen if the board is power cyceled // during operation. if (dhcp_is_renew_tid(buf,plen)==1) return(0); // should have been initial tid, just return cmd=dhcp_get_message_type(buf,plen); if (cmd==2){ // DHCPOFFER =2 init=1; // no more init needed dhcp_get_yiaddr(buf,plen); dhcp_option_parser(buf,plen); // answer offer with a request: send_dhcp_request(buf,dhcp_tid); } if (cmd==5){ // DHCPACK =5 // success, DHCPACK, we have the IP init=1; // no more init needed enc28j60DisableBroadcast(); return(1); } } return(0); }