dhcptest.c 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <string.h>
  2. #include <vsprintf.h>
  3. #include <byteswap.h>
  4. #include <gpxe/ip.h>
  5. #include <gpxe/dhcp.h>
  6. int test_dhcp ( struct net_device *netdev ) {
  7. struct dhcp_session dhcp;
  8. struct in_addr address = { htonl ( 0 ) };
  9. struct in_addr netmask = { htonl ( 0 ) };
  10. struct in_addr gateway = { INADDR_NONE };
  11. int rc;
  12. /* Bring IP interface up with address 0.0.0.0 */
  13. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  14. gateway ) ) != 0 )
  15. goto out_no_del_ipv4;
  16. /* Issue DHCP request */
  17. memset ( &dhcp, 0, sizeof ( dhcp ) );
  18. dhcp.netdev = netdev;
  19. if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 )
  20. goto out_no_options;
  21. /* Retrieve IP address configuration */
  22. find_dhcp_ipv4_option ( dhcp.options, DHCP_EB_YIADDR, &address );
  23. find_dhcp_ipv4_option ( dhcp.options, DHCP_SUBNET_MASK, &netmask );
  24. find_dhcp_ipv4_option ( dhcp.options, DHCP_ROUTERS, &gateway );
  25. /* Remove old IP address configuration */
  26. del_ipv4_address ( netdev );
  27. /* Set up new IP address configuration */
  28. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  29. gateway ) ) != 0 )
  30. goto out_no_del_ipv4;
  31. printf ( "IP %s", inet_ntoa ( address ) );
  32. printf ( " netmask %s", inet_ntoa ( netmask ) );
  33. printf ( " gateway %s\n", inet_ntoa ( gateway ) );
  34. /* Free DHCP options */
  35. free_dhcp_options ( dhcp.options );
  36. out_no_options:
  37. /* Take down IP interface */
  38. del_ipv4_address ( netdev );
  39. out_no_del_ipv4:
  40. return rc;
  41. }