summaryrefslogtreecommitdiff
path: root/gpxe/src/hci/commands/dhcp_cmd.c
blob: 96aac8d4d4618f9278fc9bf35b140da4385e30d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <gpxe/netdevice.h>
#include <gpxe/in.h>
#include <gpxe/command.h>
#include <usr/dhcpmgmt.h>

/** @file
 *
 * DHCP management commands
 *
 */

/**
 * "dhcp" command syntax message
 *
 * @v argv		Argument list
 */
static void dhcp_syntax ( char **argv ) {
	printf ( "Usage:\n"
		 "  %s <interface>\n"
		 "\n"
		 "Configure a network interface using DHCP\n",
		 argv[0] );
}

/**
 * The "dhcp" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Exit code
 */
static int dhcp_exec ( int argc, char **argv ) {
	static struct option longopts[] = {
		{ "help", 0, NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};
	const char *netdev_txt;
	struct net_device *netdev;
	int c;
	int rc;

	/* Parse options */
	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
		switch ( c ) {
		case 'h':
			/* Display help text */
		default:
			/* Unrecognised/invalid option */
			dhcp_syntax ( argv );
			return 1;
		}
	}

	/* Need exactly one interface name remaining after the options */
	if ( optind != ( argc - 1 ) ) {
		dhcp_syntax ( argv );
		return 1;
	}
	netdev_txt = argv[optind];

	/* Parse arguments */
	netdev = find_netdev ( netdev_txt );
	if ( ! netdev ) {
		printf ( "No such interface: %s\n", netdev_txt );
		return 1;
	}

	/* Perform DHCP */
	if ( ( rc = dhcp ( netdev ) ) != 0 ) {
		printf ( "Could not configure %s: %s\n", netdev->name,
			 strerror ( rc ) );
		return 1;
	}

	return 0;
}

/**
 * "pxebs" command syntax message
 *
 * @v argv		Argument list
 */
static void pxebs_syntax ( char **argv ) {
	printf ( "Usage:\n"
		 "  %s <interface> <server_type>\n"
		 "\n"
		 "Perform PXE Boot Server discovery\n",
		 argv[0] );
}

/**
 * The "pxebs" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Exit code
 */
static int pxebs_exec ( int argc, char **argv ) {
	static struct option longopts[] = {
		{ "help", 0, NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};
	const char *netdev_txt;
	const char *pxe_type_txt;
	struct net_device *netdev;
	unsigned int pxe_type;
	char *end;
	int c;
	int rc;

	/* Parse options */
	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
		switch ( c ) {
		case 'h':
			/* Display help text */
		default:
			/* Unrecognised/invalid option */
			pxebs_syntax ( argv );
			return 1;
		}
	}
	if ( optind != ( argc - 2 ) ) {
		pxebs_syntax ( argv );
		return 1;
	}
	netdev_txt = argv[optind];
	pxe_type_txt = argv[ optind + 1 ];

	/* Parse arguments */
	netdev = find_netdev ( netdev_txt );
	if ( ! netdev ) {
		printf ( "No such interface: %s\n", netdev_txt );
		return 1;
	}
	pxe_type = strtoul ( pxe_type_txt, &end, 0 );
	if ( *end ) {
		printf ( "Bad server type: %s\n", pxe_type_txt );
		return 1;
	}

	/* Perform Boot Server Discovery */
	if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
		printf ( "Could not discover boot server on %s: %s\n",
			 netdev->name, strerror ( rc ) );
		return 1;
	}

	return 0;
}

/** DHCP management commands */
struct command dhcp_commands[] __command = {
	{
		.name = "dhcp",
		.exec = dhcp_exec,
	},
	{
		.name = "pxebs",
		.exec = pxebs_exec,
	},
};