summaryrefslogtreecommitdiffstats
path: root/src/usr/dhcpmgmt.c
diff options
context:
space:
mode:
authorMichael Brown2007-01-12 10:20:11 +0100
committerMichael Brown2007-01-12 10:20:11 +0100
commit39ede8c7e8dd4a6a839f22aeed322209d424e619 (patch)
tree7932b80430b92468f581843b3945fc34b4f0b140 /src/usr/dhcpmgmt.c
parentUse systematic names for XXX_IMAGE. (diff)
downloadipxe-39ede8c7e8dd4a6a839f22aeed322209d424e619.tar.gz
ipxe-39ede8c7e8dd4a6a839f22aeed322209d424e619.tar.xz
ipxe-39ede8c7e8dd4a6a839f22aeed322209d424e619.zip
Added dhcp() user-level command.
Diffstat (limited to 'src/usr/dhcpmgmt.c')
-rw-r--r--src/usr/dhcpmgmt.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/usr/dhcpmgmt.c b/src/usr/dhcpmgmt.c
new file mode 100644
index 00000000..a5a79fdf
--- /dev/null
+++ b/src/usr/dhcpmgmt.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <string.h>
+#include <byteswap.h>
+#include <vsprintf.h>
+#include <gpxe/in.h>
+#include <gpxe/dhcp.h>
+#include <gpxe/async.h>
+#include <gpxe/netdevice.h>
+#include <usr/dhcpmgmt.h>
+
+/** @file
+ *
+ * DHCP management
+ *
+ */
+
+/**
+ * Configure network device via DHCP
+ *
+ * @v netdev Network device
+ * @ret rc Return status code
+ */
+int dhcp ( struct net_device *netdev ) {
+ static struct dhcp_option_block *dhcp_options = NULL;
+ struct dhcp_session dhcp;
+ struct in_addr address = { htonl ( 0 ) };
+ struct in_addr netmask = { htonl ( 0 ) };
+ struct in_addr gateway = { INADDR_NONE };
+ int rc;
+
+ /* Free up any previously-acquired options */
+ if ( dhcp_options ) {
+ unregister_dhcp_options ( dhcp_options );
+ free_dhcp_options ( dhcp_options );
+ dhcp_options = NULL;
+ }
+
+ /* Issue DHCP request */
+ printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
+ memset ( &dhcp, 0, sizeof ( dhcp ) );
+ dhcp.netdev = netdev;
+ if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
+ printf ( "failed\n" );
+ return rc;
+ }
+ printf ( "done\n" );
+
+ /* Store and register options */
+ dhcp_options = dhcp.options;
+ register_dhcp_options ( dhcp_options );
+
+ /* Retrieve IP address configuration */
+ find_dhcp_ipv4_option ( dhcp_options, DHCP_EB_YIADDR, &address );
+ find_dhcp_ipv4_option ( dhcp_options, DHCP_SUBNET_MASK, &netmask );
+ find_dhcp_ipv4_option ( dhcp_options, DHCP_ROUTERS, &gateway );
+
+ /* Set up new IP address configuration */
+ if ( ( rc = add_ipv4_address ( netdev, address, netmask,
+ gateway ) ) != 0 ) {
+ printf ( "Could not configure %s with DHCP results: %s\n",
+ netdev->name, strerror ( rc ) );
+ return rc;
+ }
+
+ return 0;
+}