summaryrefslogtreecommitdiffstats
path: root/lib/asn.c
blob: 4042f5846cc1f4719a27cdca279e174051e1d8e9 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * ASN functions
 *
 * Copyright 2022 6WIND
 *
 * 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 (at your option)
 * 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; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <zebra.h>
#include "log.h"
#include "asn.h"

static bool relax_as_zero;

static const struct message asnotation_mode_msg[] = {
	{ASNOTATION_PLAIN, "plain"},
	{ASNOTATION_DOT, "dot"},
	{ASNOTATION_DOTPLUS, "dot+"},
	{ASNOTATION_UNDEFINED, "undefined"},
	{0}
};

/* converts a string into an Autonomous system number
 * "1.1" => 65536
 * "65500" => 65500
 */
static bool asn_str2asn_internal(const char *asstring, as_t *asn,
				 const char **next, bool *partial,
				 enum asnotation_mode *mode)
{
	uint32_t high = 0, low = 0;
	uint64_t temp_val;
	const char *p = asstring;
	bool ret = false;
	uint32_t digit;
	enum asnotation_mode val = ASNOTATION_PLAIN;

	if (!asstring)
		goto end;

	if  (!isdigit((unsigned char)*p))
		goto end;

	/* leading zero is forbidden */
	if (*p == '0' && isdigit((unsigned char)*(p + 1)))
		goto end;

	temp_val = 0;
	while (isdigit((unsigned char)*p)) {
		digit = (*p) - '0';
		temp_val *= 10;
		temp_val += digit;
		if (temp_val > UINT32_MAX)
			/* overflow */
			goto end;
		p++;
	}
	high = (uint32_t)temp_val;
	if (*p == '.') { /* dot format */
		p++;

		if (*p == '\0' && partial) {
			*partial = true;
			goto end;
		}

		/* leading zero is forbidden */
		if (*p == '0' && isdigit((unsigned char)*(p + 1)))
			goto end;

		temp_val = 0;
		while (isdigit((unsigned char)*p)) {
			digit = (*p) - '0';
			temp_val *= 10;
			temp_val += digit;
			if (temp_val > UINT16_MAX)
				/* overflow */
				goto end;
			p++;
		}
		low = (uint32_t)temp_val;

		if (!next && *p != '\0' && !isdigit((unsigned char)*p))
			goto end;
		/* AS <AS4B>.<AS4B> is forbidden */
		if (high > UINT16_MAX)
			goto end;
		/* AS 0.0 is authorised for some case only */
		if (!relax_as_zero && high == 0 && low == 0) {
			if (partial)
				*partial = true;
			goto end;
		}
		if (asn)
			*asn = (high << 16) + low;
		ret = true;
		if (high == 0)
			val = ASNOTATION_DOTPLUS;
		else
			val = ASNOTATION_DOT;
		goto end;
	}
	/* AS 0 is forbidden */
	if (!relax_as_zero && high == 0)
		goto end;
	if (!asn) {
		ret = true;
		goto end;
	}
	*asn = high;
	ret = true;
 end:
	if (next)
		*next = p;
	if (mode)
		*mode = val;
	return ret;
}

static void asn_asn2asdot(as_t asn, char *asstring, size_t len)
{
	uint16_t low, high;

	high = (asn >> 16) & 0xffff;
	low = asn & 0xffff;
	snprintf(asstring, len, "%hu.%hu", high, low);
}

bool asn_str2asn(const char *asstring, as_t *asn)
{
	return asn_str2asn_internal(asstring, asn, NULL, NULL, NULL);
}

const char *asn_asn2asplain(as_t asn)
{
	static char buf[ASN_STRING_MAX_SIZE];

	snprintf(buf, sizeof(buf), "%u", asn);
	return buf;
}

const char *asn_str2asn_parse(const char *asstring, as_t *asn, bool *found_ptr)
{
	const char *p = NULL;
	const char **next = &p;
	bool found;

	found = asn_str2asn_internal(asstring, asn, next, NULL, NULL);
	if (found_ptr)
		*found_ptr = found;
	return *next;
}

void asn_relax_as_zero(bool relax)
{
	relax_as_zero = relax;
}

enum match_type asn_str2asn_match(const char *str)
{
	bool found, partial = false;

	found = asn_str2asn_internal(str, NULL, NULL, &partial, NULL);
	if (found && !partial)
		return exact_match;

	if (partial)
		return partly_match;

	return no_match;
}

bool asn_str2asn_notation(const char *asstring, as_t *asn,
			  enum asnotation_mode *asnotation)
{
	return asn_str2asn_internal(asstring, asn, NULL, NULL, asnotation);
}

const char *asn_mode2str(enum asnotation_mode asnotation)
{
	return lookup_msg(asnotation_mode_msg, asnotation,
			  "Unrecognized AS notation mode");
}

void asn_asn2json(json_object *json, const char *attr,
		  as_t asn, enum asnotation_mode asnotation)
{
	static char as_str[ASN_STRING_MAX_SIZE];

	if ((asnotation == ASNOTATION_PLAIN) ||
	    ((asnotation == ASNOTATION_DOT) && asn < UINT16_MAX))
		json_object_int_add(json, attr, asn);
	else {
		asn_asn2asdot(asn, as_str, sizeof(as_str));
		json_object_string_add(json, attr, as_str);
	}
}

void asn_asn2json_array(json_object *jseg_list, as_t asn,
			enum asnotation_mode asnotation)
{
	static char as_str[ASN_STRING_MAX_SIZE];

	if ((asnotation == ASNOTATION_PLAIN) ||
	    ((asnotation == ASNOTATION_DOT) && asn < UINT16_MAX))
		json_object_array_add(jseg_list,
				      json_object_new_int64(asn));
	else {
		asn_asn2asdot(asn, as_str, sizeof(as_str));
		json_array_string_add(jseg_list, as_str);
	}
}

char *asn_asn2string(const as_t *asn, char *buf, size_t len,
		     enum asnotation_mode asnotation)
{
	if ((asnotation == ASNOTATION_PLAIN) ||
	    ((asnotation == ASNOTATION_DOT) && *asn < UINT16_MAX))
		snprintf(buf, len, "%u", *asn);
	else
		asn_asn2asdot(*asn, buf, len);
	return buf;
}

static ssize_t printfrr_asnotation(struct fbuf *buf, struct printfrr_eargs *ea,
				   const void *ptr,
				   enum asnotation_mode asnotation)
{
	/* for alignemnt up to 33 chars - %33pASD for instance - */
	char as_str[ASN_STRING_MAX_SIZE*3];
	const as_t *asn;

	if (!ptr)
		return bputs(buf, "(null)");
	asn = ptr;
	asn_asn2string(asn, as_str, sizeof(as_str), asnotation);
	return bputs(buf, as_str);
}

printfrr_ext_autoreg_p("ASP", printfrr_asplain);
static ssize_t printfrr_asplain(struct fbuf *buf, struct printfrr_eargs *ea,
				const void *ptr)
{
	return printfrr_asnotation(buf, ea, ptr, ASNOTATION_PLAIN);
}

printfrr_ext_autoreg_p("ASD", printfrr_asdot);
static ssize_t printfrr_asdot(struct fbuf *buf, struct printfrr_eargs *ea,
				const void *ptr)
{
	return printfrr_asnotation(buf, ea, ptr, ASNOTATION_DOT);
}

printfrr_ext_autoreg_p("ASE", printfrr_asdotplus);
static ssize_t printfrr_asdotplus(struct fbuf *buf, struct printfrr_eargs *ea,
				  const void *ptr)
{
	return printfrr_asnotation(buf, ea, ptr, ASNOTATION_DOTPLUS);
}