[tcp] Enable AF_INET6 transport for tcp connections
[gpxe.git] / src / hci / commands / dhcp_cmd.c
blob6f0a99ff594123eb4f48fbaae618b4026ea07233
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <getopt.h>
30 #include <gpxe/netdevice.h>
31 #include <gpxe/in.h>
32 #include <gpxe/command.h>
33 #include <usr/dhcpmgmt.h>
34 #include <usr/ifmgmt.h>
36 /** @file
38 * DHCP management commands
42 /**
43 * "dhcp" command syntax message
45 * @v argv Argument list
47 static void dhcp_syntax ( char **argv ) {
48 printf ( "Usage:\n"
49 " %s <interface> [<interface> ...]\n"
50 " %s any\n"
51 "\n"
52 "Configure a network interface using DHCP\n",
53 argv[0], argv[0] );
56 /**
57 * Attempt to configure a device with dhcp
59 * @v netdev Device to configure
60 * @ret rc Exit code
62 static int dhcp_one_device ( struct net_device *netdev ) {
63 int rc;
65 /* Perform DHCP */
66 if ( ( rc = dhcp ( netdev ) ) != 0 ) {
67 /* Close the device on error to avoid out-of-memory */
68 netdev_close ( netdev );
70 printf ( "Could not configure %s: %s\n", netdev->name,
71 strerror ( rc ) );
72 return 1;
75 return 0;
78 /**
79 * Call dhcp_one_device() for each name in argv
81 * @v argc Number of devices
82 * @v argv List of device names
83 * @ret rc Exit code
85 static int dhcp_each_device_name ( int argc, char **argv ) {
86 int i;
87 char *netdev_name;
88 struct net_device *netdev;
90 for ( i = 0; i < argc; i++ ) {
91 netdev_name = argv[i];
92 netdev = find_netdev ( netdev_name );
94 if ( ! netdev ) {
95 printf ( "No such interface: %s\n", netdev_name );
96 continue;
99 if ( dhcp_one_device ( netdev ) == 0 )
100 return 0;
103 printf ( "Could not configure any interface.\n" );
104 return 1;
108 * Call dhcp_one_device() for each device in net_devices
110 * @ret rc Exit code
112 static int dhcp_each_device ( void ) {
113 struct net_device *netdev;
115 for_each_netdev ( netdev ) {
116 if ( dhcp_one_device ( netdev ) == 0 )
117 return 0;
120 printf ( "Could not configure any interface.\n" );
121 return 1;
125 * The "dhcp" command
127 * @v argc Argument count
128 * @v argv Argument list
129 * @ret rc Exit code
131 static int dhcp_exec ( int argc, char **argv ) {
132 static struct option longopts[] = {
133 { "help", 0, NULL, 'h' },
134 { NULL, 0, NULL, 0 },
136 int c;
138 /* Parse options */
139 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
140 switch ( c ) {
141 case 'h':
142 /* Display help text */
143 default:
144 /* Unrecognised/invalid option */
145 dhcp_syntax ( argv );
146 return 1;
150 /* Need one or more interface names remaining after the options */
151 if ( ( argc - optind ) < 1 ) {
152 dhcp_syntax ( argv );
153 return 1;
156 if ( strcmp ( argv[optind], "any" ) == 0 )
157 return dhcp_each_device();
159 return dhcp_each_device_name ( argc - optind, argv + optind );
163 * "pxebs" command syntax message
165 * @v argv Argument list
167 static void pxebs_syntax ( char **argv ) {
168 printf ( "Usage:\n"
169 " %s <interface> <server_type>\n"
170 "\n"
171 "Perform PXE Boot Server discovery\n",
172 argv[0] );
176 * The "pxebs" command
178 * @v argc Argument count
179 * @v argv Argument list
180 * @ret rc Exit code
182 static int pxebs_exec ( int argc, char **argv ) {
183 static struct option longopts[] = {
184 { "help", 0, NULL, 'h' },
185 { NULL, 0, NULL, 0 },
187 const char *netdev_txt;
188 const char *pxe_type_txt;
189 struct net_device *netdev;
190 unsigned int pxe_type;
191 char *end;
192 int c;
193 int rc;
195 /* Parse options */
196 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
197 switch ( c ) {
198 case 'h':
199 /* Display help text */
200 default:
201 /* Unrecognised/invalid option */
202 pxebs_syntax ( argv );
203 return 1;
206 if ( optind != ( argc - 2 ) ) {
207 pxebs_syntax ( argv );
208 return 1;
210 netdev_txt = argv[optind];
211 pxe_type_txt = argv[ optind + 1 ];
213 /* Parse arguments */
214 netdev = find_netdev ( netdev_txt );
215 if ( ! netdev ) {
216 printf ( "No such interface: %s\n", netdev_txt );
217 return 1;
219 pxe_type = strtoul ( pxe_type_txt, &end, 0 );
220 if ( *end ) {
221 printf ( "Bad server type: %s\n", pxe_type_txt );
222 return 1;
225 /* Perform Boot Server Discovery */
226 if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
227 printf ( "Could not discover boot server on %s: %s\n",
228 netdev->name, strerror ( rc ) );
229 return 1;
232 return 0;
235 /** DHCP management commands */
236 struct command dhcp_commands[] __command = {
238 .name = "dhcp",
239 .exec = dhcp_exec,
242 .name = "pxebs",
243 .exec = pxebs_exec,