Quellcode durchsuchen

Add "dhcp" command

tags/v0.9.3
Michael Brown vor 18 Jahren
Ursprung
Commit
9952df9b52
3 geänderte Dateien mit 110 neuen und 0 gelöschten Zeilen
  1. 3
    0
      src/core/config.c
  2. 103
    0
      src/hci/commands/dhcp_cmd.c
  3. 4
    0
      src/usr/dhcpmgmt.c

+ 3
- 0
src/core/config.c Datei anzeigen

@@ -163,6 +163,9 @@ REQUIRE_OBJECT ( route_cmd );
163 163
 #ifdef IMAGE_CMD
164 164
 REQUIRE_OBJECT ( image_cmd );
165 165
 #endif
166
+#ifdef DHCP_CMD
167
+REQUIRE_OBJECT ( dhcp_cmd );
168
+#endif
166 169
 
167 170
 /*
168 171
  * Drag in miscellaneous objects

+ 103
- 0
src/hci/commands/dhcp_cmd.c Datei anzeigen

@@ -0,0 +1,103 @@
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 <stdint.h>
20
+#include <stdlib.h>
21
+#include <getopt.h>
22
+#include <vsprintf.h>
23
+#include <gpxe/netdevice.h>
24
+#include <gpxe/command.h>
25
+#include <usr/dhcpmgmt.h>
26
+
27
+/** @file
28
+ *
29
+ * DHCP management commands
30
+ *
31
+ */
32
+
33
+/**
34
+ * "dhcp" command syntax message
35
+ *
36
+ * @v argv		Argument list
37
+ */
38
+static void dhcp_syntax ( char **argv ) {
39
+	printf ( "Usage:\n"
40
+		 "  %s <interface>\n"
41
+		 "\n"
42
+		 "Configure a network interface using DHCP\n",
43
+		 argv[0] );
44
+}
45
+
46
+/**
47
+ * The "dhcp" command
48
+ *
49
+ * @v argc		Argument count
50
+ * @v argv		Argument list
51
+ * @ret rc		Exit code
52
+ */
53
+static int dhcp_exec ( int argc, char **argv ) {
54
+	static struct option longopts[] = {
55
+		{ "help", 0, NULL, 'h' },
56
+		{ NULL, 0, NULL, 0 },
57
+	};
58
+	const char *name;
59
+	struct net_device *netdev;
60
+	int c;
61
+	int rc;
62
+
63
+	/* Parse options */
64
+	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
65
+		switch ( c ) {
66
+		case 'h':
67
+			/* Display help text */
68
+		default:
69
+			/* Unrecognised/invalid option */
70
+			dhcp_syntax ( argv );
71
+			return 1;
72
+		}
73
+	}
74
+
75
+	/* Need exactly one interface name remaining after the options */
76
+	if ( optind != ( argc - 1 ) ) {
77
+		dhcp_syntax ( argv );
78
+		return 1;
79
+	}
80
+	name = argv[optind];
81
+
82
+	/* Perform DHCP */
83
+	netdev = find_netdev ( name );
84
+	if ( ! netdev ) {
85
+		printf ( "No such interface: %s\n", name );
86
+		return 1;
87
+	}
88
+	if ( ( rc = dhcp ( netdev ) ) != 0 ) {
89
+		printf ( "Could not configure %s: %s\n", netdev->name,
90
+			 strerror ( rc ) );
91
+		return 1;
92
+	}
93
+
94
+	return 0;
95
+}
96
+
97
+/** DHCP management commands */
98
+struct command dhcp_commands[] __command = {
99
+	{
100
+		.name = "dhcp",
101
+		.exec = dhcp_exec,
102
+	},
103
+};

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

@@ -45,6 +45,10 @@ int dhcp ( struct net_device *netdev ) {
45 45
 	struct in_addr gateway = { INADDR_NONE };
46 46
 	int rc;
47 47
 
48
+	/* Check we can open the interface first */
49
+	if ( ( rc = ifopen ( netdev ) ) != 0 )
50
+		return rc;
51
+
48 52
 	/* Free up any previously-acquired options */
49 53
 	if ( dhcp_options ) {
50 54
 		unregister_dhcp_options ( dhcp_options );

Laden…
Abbrechen
Speichern