Quellcode durchsuchen

Added dhcp() user-level command.

tags/v0.9.3
Michael Brown vor 18 Jahren
Ursprung
Commit
39ede8c7e8
2 geänderte Dateien mit 97 neuen und 0 gelöschten Zeilen
  1. 14
    0
      src/include/usr/dhcpmgmt.h
  2. 83
    0
      src/usr/dhcpmgmt.c

+ 14
- 0
src/include/usr/dhcpmgmt.h Datei anzeigen

@@ -0,0 +1,14 @@
1
+#ifndef _USR_DHCPMGMT_H
2
+#define _USR_DHCPMGMT_H
3
+
4
+/** @file
5
+ *
6
+ * DHCP management
7
+ *
8
+ */
9
+
10
+struct net_device;
11
+
12
+int dhcp ( struct net_device *netdev );
13
+
14
+#endif /* _USR_DHCPMGMT_H */

+ 83
- 0
src/usr/dhcpmgmt.c Datei anzeigen

@@ -0,0 +1,83 @@
1
+/*
2
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <string.h>
20
+#include <byteswap.h>
21
+#include <vsprintf.h>
22
+#include <gpxe/in.h>
23
+#include <gpxe/dhcp.h>
24
+#include <gpxe/async.h>
25
+#include <gpxe/netdevice.h>
26
+#include <usr/dhcpmgmt.h>
27
+
28
+/** @file
29
+ *
30
+ * DHCP management
31
+ *
32
+ */
33
+
34
+/**
35
+ * Configure network device via DHCP
36
+ *
37
+ * @v netdev		Network device
38
+ * @ret rc		Return status code
39
+ */
40
+int dhcp ( struct net_device *netdev ) {
41
+	static struct dhcp_option_block *dhcp_options = NULL;
42
+	struct dhcp_session dhcp;
43
+	struct in_addr address = { htonl ( 0 ) };
44
+	struct in_addr netmask = { htonl ( 0 ) };
45
+	struct in_addr gateway = { INADDR_NONE };
46
+	int rc;
47
+
48
+	/* Free up any previously-acquired options */
49
+	if ( dhcp_options ) {
50
+		unregister_dhcp_options ( dhcp_options );
51
+		free_dhcp_options ( dhcp_options );
52
+		dhcp_options = NULL;
53
+	}
54
+
55
+	/* Issue DHCP request */
56
+	printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
57
+	memset ( &dhcp, 0, sizeof ( dhcp ) );
58
+	dhcp.netdev = netdev;
59
+	if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
60
+		printf ( "failed\n" );
61
+		return rc;
62
+	}
63
+	printf ( "done\n" );
64
+
65
+	/* Store and register options */
66
+	dhcp_options = dhcp.options;
67
+	register_dhcp_options ( dhcp_options );
68
+
69
+	/* Retrieve IP address configuration */
70
+	find_dhcp_ipv4_option ( dhcp_options, DHCP_EB_YIADDR, &address );
71
+	find_dhcp_ipv4_option ( dhcp_options, DHCP_SUBNET_MASK, &netmask );
72
+	find_dhcp_ipv4_option ( dhcp_options, DHCP_ROUTERS, &gateway );
73
+
74
+	/* Set up new IP address configuration */
75
+	if ( ( rc = add_ipv4_address ( netdev, address, netmask,
76
+				       gateway ) ) != 0 ) {
77
+		printf ( "Could not configure %s with DHCP results: %s\n",
78
+			 netdev->name, strerror ( rc ) );
79
+		return rc;
80
+	}
81
+
82
+	return 0;
83
+}

Laden…
Abbrechen
Speichern