summaryrefslogtreecommitdiffstats
path: root/commands/dhcp.c
blob: d9e844b3be7d4e333fc7a0702a808dd4eec3162c (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
/*
 * Copyright (C) 2015 PHYTEC Messtechnik GmbH,
 * Author: Wadim Egorov <w.egorov@phytec.de>
 *
 * Based on work of Sascha Hauer <s.hauer@pengutronix.de>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <common.h>
#include <command.h>
#include <complete.h>
#include <environment.h>
#include <getopt.h>
#include <dhcp.h>
#include <net.h>

static int do_dhcp(int argc, char *argv[])
{
	int ret, opt;
	struct dhcp_req_param dhcp_param = {};
	struct eth_device *edev;
	const char *edevname;

	while ((opt = getopt(argc, argv, "H:v:c:u:U:r:o:")) > 0) {
		switch (opt) {
		case 'H':
			dhcp_param.hostname = optarg;
			break;
		case 'v':
			dhcp_param.vendor_id = optarg;
			break;
		case 'c':
			dhcp_param.client_id = optarg;
			break;
		case 'u':
			dhcp_param.client_uuid = optarg;
			break;
		case 'U':
			dhcp_param.user_class = optarg;
			break;
		case 'r':
			dhcp_param.retries = simple_strtoul(optarg, NULL, 10);
			break;
		case 'o':
			dhcp_param.option224 = optarg;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind == argc)
		edevname = "eth0";
	else
		edevname = argv[optind];

	edev = eth_get_byname(edevname);
	if (!edev) {
		printf("No such network device: %s\n", edevname);
		return 1;
	}

	ret = dhcp(edev, &dhcp_param);

	return ret;
}

BAREBOX_CMD_HELP_START(dhcp)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-H HOSTNAME", "hostname to send to the DHCP server")
BAREBOX_CMD_HELP_OPT("-v ID\t", "DHCP Vendor ID (code 60) submitted in DHCP requests")
BAREBOX_CMD_HELP_OPT("-c ID\t", "DHCP Client ID (code 61) submitted in DHCP requests")
BAREBOX_CMD_HELP_OPT("-u UUID\t", "DHCP Client UUID (code 97) submitted in DHCP requests")
BAREBOX_CMD_HELP_OPT("-U CLASS", "DHCP User class (code 77) submitted in DHCP requests")
BAREBOX_CMD_HELP_OPT("-r RETRY", "retry limit (default 20)")
BAREBOX_CMD_HELP_OPT("-o PRIVATE DATA", "private data (code 224) submitted in DHCP requests");
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(dhcp)
	.cmd		= do_dhcp,
	BAREBOX_CMD_DESC("DHCP client to obtain IP or boot params")
	BAREBOX_CMD_OPTS("[-HvcuUro] [device]")
	BAREBOX_CMD_GROUP(CMD_GRP_NET)
	BAREBOX_CMD_HELP(cmd_dhcp_help)
	BAREBOX_CMD_COMPLETE(eth_complete)
BAREBOX_CMD_END