File: showroute.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,656 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (171 lines) | stat: -rw-r--r-- 4,181 bytes parent folder | download | duplicates (2)
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
/* routecheck, for libreswan
 *
 * Copyright (C) 2022 Andrew Cagney
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * This library 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 Library General Public
 * License for more details.
 */

#include <stdlib.h>
#include <getopt.h>


#include "lswtool.h"
#include "lswlog.h"
#include "addr_lookup.h"
#include "ip_address.h"
#include "ip_info.h"
#include "optarg.h"

enum opt {
	OPT_EOF = -1,
	OPT_FLAG = 0,
	OPT_MISSING = ':',
	OPT_INVALID = '?',

	OPT_HELP = 'h',
	OPT_VERBOSE = 'v',
	OPT_DEBUG = 'd',
	OPT_IPv4 = '4',
	OPT_IPv6 = '6',
};

static struct optarg_family family;

static int show_source = false;
static int show_gateway = false;
static int show_destination = false;

const struct option optarg_options[] = {
	{ "source", no_argument, &show_source, true, },
	{ "gateway", no_argument, &show_gateway, true, },
	{ "destination", no_argument, &show_destination, true, },
	{ "debug", no_argument, NULL, OPT_DEBUG, },
	{ "ipv4", no_argument, NULL, OPT_IPv4, },
	{ "ipv6", no_argument, NULL, OPT_IPv6, },
	{ "verbose", no_argument, NULL, OPT_VERBOSE, },
	{0},
};

int main(int argc, char **argv)
{
	struct logger *logger = tool_logger(argc, argv);
	optarg_init(logger);

	if (argc == 1) {
		llog(WHACK_STREAM|NO_PREFIX, logger, "Usage:");
		llog(WHACK_STREAM|NO_PREFIX, logger, "  ipsec showroute [-4|-6] [--debug] [--source|--gateway|--destination] <destination>");
		llog(WHACK_STREAM|NO_PREFIX, logger, "prints:");
		llog(WHACK_STREAM|NO_PREFIX, logger, "  <source-address> <gateway-address> <destination-address>");
		llog(WHACK_STREAM|NO_PREFIX, logger, "for the given <destination>");
		exit(1);
	}

	while (true) {
		enum opt c = getopt_long(argc, argv, "vd46", optarg_options, &optarg_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case OPT_DEBUG:
			optarg_debug(true);
			break;
		case OPT_IPv4:
			optarg_family(&family, &ipv4_info);
			break;
		case OPT_IPv6:
			optarg_family(&family, &ipv4_info);
			break;
		case OPT_VERBOSE:
			optarg_verbose(LEMPTY);
			break;
		case OPT_EOF:
			break;
		case OPT_FLAG:
			/* flag updated */
			break;
		case OPT_INVALID:
			llog(ERROR_STREAM, logger, "invalid option: %s", argv[optarg_index]);
			exit(1);
		case OPT_MISSING:
			llog(ERROR_STREAM, logger, "missing parameter: %s", argv[optarg_index]);
			exit(1);
		default:
			bad_case(c);
		}
	}

	if (optind == argc) {
		llog(ERROR_STREAM, logger, "missing destination");
		exit(1);
	}

	if (optind + 1 < argc) {
		llog(ERROR_STREAM, logger, "extra parameter %s", argv[optind + 1]);
		exit(1);
	}

	if (!show_source && !show_gateway && !show_destination) {
		show_source = show_gateway = show_destination = true;
	}

	ip_address dst;
	err_t e = ttoaddress_dns(shunk1(argv[optind]), family.type, &dst);
	if (e != NULL) {
		llog(WHACK_STREAM, logger, "%s: %s", argv[1], e);
		exit(1);
	}

	struct ip_route route;
	switch (get_route(dst, &route, logger)) {
	case ROUTE_SUCCESS:
	{
		LLOG_JAMBUF(WHACK_STREAM|NO_PREFIX, logger, buf) {
			const char *sep = "";
			if (show_source) {
				jam_string(buf, sep); sep = " ";
				jam_address(buf, &route.source);
			}
			if (show_gateway) {
				jam_string(buf, sep); sep = " ";
				jam_address(buf, &route.gateway);
			}
			if (show_destination) {
				jam_string(buf, sep); sep = " ";
				jam_address(buf, &dst);
			}
		}
		exit(0);
	}
	case ROUTE_GATEWAY_FAILED:
	{
		address_buf ab;
		llog(ERROR_STREAM, logger, "%s: gateway failed",
		     str_address(&dst, &ab));
		exit(1);
	}
	case ROUTE_SOURCE_FAILED:
	{
		address_buf ab;
		llog(ERROR_STREAM, logger, "%s: source failed",
		     str_address(&dst, &ab));
		exit(1);
	}
	case ROUTE_FATAL:
	{
		address_buf ab;
		llog(ERROR_STREAM, logger, "%s: fatal",
		     str_address(&dst, &ab));
		exit(1);
	}
	}

	exit(1);
}