summaryrefslogtreecommitdiffstats
path: root/commands/ip-route-get.c
blob: d3c15b779860dddf9014cc2bd5dba92f611396d1 (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
/*
 * 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; version 2.
 *
 * 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.
 */

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

static int do_ip_route_get(int argc, char *argv[])
{
	struct eth_device *edev;
	IPaddr_t ip, gw = 0;
	const char *variable = NULL;
	int opt, ret;
	bool bootarg = false;

	while ((opt = getopt(argc, argv, "b")) > 0) {
		switch (opt) {
		case 'b':
			bootarg = true;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (argc == optind)
		return COMMAND_ERROR_USAGE;

	if (argc == optind + 2)
		variable = argv[optind + 1];

	ret = resolv(argv[optind], &ip);
	if (ret) {
		printf("Cannot convert \"%s\" into a IP address: %s\n",
		       argv[optind], strerror(-ret));
		return 1;
	}

	edev = net_route(ip);
	if (!edev) {
		gw = net_get_gateway();
		if (gw)
			edev = net_route(gw);
	}

	if (!edev) {
		printf("IP %pI4 is not reachable\n", &ip);
		return 1;
	}

	if (variable) {
		if (bootarg)
			setenv(variable, edev->bootarg);
		else
			setenv(variable, edev->devname);
		return 0;
	}

	if (bootarg) {
		printf("%s\n", edev->bootarg);
	} else {
		if (gw)
			printf("%pI4 via %pI4 dev %s\n", &ip, &gw,
			       edev->devname);
		else
			printf("%pI4 dev %s\n", &ip, edev->devname);
	}

	return 0;
}

BAREBOX_CMD_HELP_START(ip_route_get)
BAREBOX_CMD_HELP_TEXT("get ethernet device used to reach given IP address")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-b", "Instead of ethernet device, show linux bootargs for that device")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ip_route_get)
	.cmd = do_ip_route_get,
	BAREBOX_CMD_DESC("get ethernet device name for an IP address")
	BAREBOX_CMD_OPTS("[-b] <IP> [variable]")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_COMPLETE(empty_complete)
	BAREBOX_CMD_HELP(cmd_ip_route_get_help)
BAREBOX_CMD_END