2006-07-19 19:49:31 +02:00
|
|
|
#include <string.h>
|
2006-07-20 04:49:59 +02:00
|
|
|
#include <vsprintf.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <gpxe/ip.h>
|
2006-07-19 19:49:31 +02:00
|
|
|
#include <gpxe/dhcp.h>
|
|
|
|
|
|
|
|
int test_dhcp ( struct net_device *netdev ) {
|
|
|
|
struct dhcp_session dhcp;
|
2006-07-20 04:49:59 +02:00
|
|
|
struct in_addr address = { htonl ( 0 ) };
|
|
|
|
struct in_addr netmask = { htonl ( 0 ) };
|
|
|
|
struct in_addr gateway = { INADDR_NONE };
|
|
|
|
int rc;
|
2006-07-19 19:49:31 +02:00
|
|
|
|
2006-07-20 04:49:59 +02:00
|
|
|
/* Bring IP interface up with address 0.0.0.0 */
|
|
|
|
if ( ( rc = add_ipv4_address ( netdev, address, netmask,
|
|
|
|
gateway ) ) != 0 )
|
|
|
|
goto out_no_del_ipv4;
|
|
|
|
|
|
|
|
/* Issue DHCP request */
|
2006-07-19 19:49:31 +02:00
|
|
|
memset ( &dhcp, 0, sizeof ( dhcp ) );
|
|
|
|
dhcp.netdev = netdev;
|
2006-07-20 04:49:59 +02:00
|
|
|
if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 )
|
|
|
|
goto out_no_options;
|
|
|
|
|
2006-07-20 05:02:34 +02:00
|
|
|
/* Register options received via DHCP */
|
|
|
|
register_dhcp_options ( dhcp.options );
|
|
|
|
|
2006-07-20 04:49:59 +02:00
|
|
|
/* Retrieve IP address configuration */
|
2006-07-20 05:02:34 +02:00
|
|
|
find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
|
|
|
|
find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
|
|
|
|
find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
|
|
|
|
|
|
|
|
printf ( "IP %s", inet_ntoa ( address ) );
|
|
|
|
printf ( " netmask %s", inet_ntoa ( netmask ) );
|
|
|
|
printf ( " gateway %s\n", inet_ntoa ( gateway ) );
|
|
|
|
|
|
|
|
printf ( "Lease time is %ld seconds\n",
|
|
|
|
find_global_dhcp_num_option ( DHCP_LEASE_TIME ) );
|
2006-07-20 04:49:59 +02:00
|
|
|
|
|
|
|
/* Remove old IP address configuration */
|
|
|
|
del_ipv4_address ( netdev );
|
|
|
|
|
|
|
|
/* Set up new IP address configuration */
|
|
|
|
if ( ( rc = add_ipv4_address ( netdev, address, netmask,
|
|
|
|
gateway ) ) != 0 )
|
|
|
|
goto out_no_del_ipv4;
|
|
|
|
|
2006-07-20 05:02:34 +02:00
|
|
|
/* Unregister and free DHCP options */
|
|
|
|
unregister_dhcp_options ( dhcp.options );
|
2006-07-20 04:49:59 +02:00
|
|
|
free_dhcp_options ( dhcp.options );
|
|
|
|
out_no_options:
|
|
|
|
/* Take down IP interface */
|
|
|
|
del_ipv4_address ( netdev );
|
|
|
|
out_no_del_ipv4:
|
|
|
|
return rc;
|
2006-07-19 19:49:31 +02:00
|
|
|
}
|