6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 static bool relax_as_zero
;
26 static const struct message asnotation_mode_msg
[] = {
27 {ASNOTATION_PLAIN
, "plain"},
28 {ASNOTATION_DOT
, "dot"},
29 {ASNOTATION_DOTPLUS
, "dot+"},
30 {ASNOTATION_UNDEFINED
, "undefined"},
34 /* converts a string into an Autonomous system number
38 static bool asn_str2asn_internal(const char *asstring
, as_t
*asn
,
39 const char **next
, bool *partial
,
40 enum asnotation_mode
*mode
)
42 uint32_t high
= 0, low
= 0;
44 const char *p
= asstring
;
47 enum asnotation_mode val
= ASNOTATION_PLAIN
;
52 if (!isdigit((unsigned char)*p
))
55 /* leading zero is forbidden */
56 if (*p
== '0' && isdigit((unsigned char)*(p
+ 1)))
60 while (isdigit((unsigned char)*p
)) {
64 if (temp_val
> UINT32_MAX
)
69 high
= (uint32_t)temp_val
;
70 if (*p
== '.') { /* dot format */
73 if (*p
== '\0' && partial
) {
78 /* leading zero is forbidden */
79 if (*p
== '0' && isdigit((unsigned char)*(p
+ 1)))
83 while (isdigit((unsigned char)*p
)) {
87 if (temp_val
> UINT16_MAX
)
92 low
= (uint32_t)temp_val
;
94 if (!next
&& *p
!= '\0' && !isdigit((unsigned char)*p
))
96 /* AS <AS4B>.<AS4B> is forbidden */
97 if (high
> UINT16_MAX
)
99 /* AS 0.0 is authorised for some case only */
100 if (!relax_as_zero
&& high
== 0 && low
== 0) {
106 *asn
= (high
<< 16) + low
;
109 val
= ASNOTATION_DOTPLUS
;
111 val
= ASNOTATION_DOT
;
114 /* AS 0 is forbidden */
115 if (!relax_as_zero
&& high
== 0)
131 static void asn_asn2asdot(as_t asn
, char *asstring
, size_t len
)
135 high
= (asn
>> 16) & 0xffff;
137 snprintf(asstring
, len
, "%hu.%hu", high
, low
);
140 bool asn_str2asn(const char *asstring
, as_t
*asn
)
142 return asn_str2asn_internal(asstring
, asn
, NULL
, NULL
, NULL
);
145 const char *asn_asn2asplain(as_t asn
)
147 static char buf
[ASN_STRING_MAX_SIZE
];
149 snprintf(buf
, sizeof(buf
), "%u", asn
);
153 const char *asn_str2asn_parse(const char *asstring
, as_t
*asn
, bool *found_ptr
)
155 const char *p
= NULL
;
156 const char **next
= &p
;
159 found
= asn_str2asn_internal(asstring
, asn
, next
, NULL
, NULL
);
165 void asn_relax_as_zero(bool relax
)
167 relax_as_zero
= relax
;
170 enum match_type
asn_str2asn_match(const char *str
)
172 bool found
, partial
= false;
174 found
= asn_str2asn_internal(str
, NULL
, NULL
, &partial
, NULL
);
175 if (found
&& !partial
)
184 bool asn_str2asn_notation(const char *asstring
, as_t
*asn
,
185 enum asnotation_mode
*asnotation
)
187 return asn_str2asn_internal(asstring
, asn
, NULL
, NULL
, asnotation
);
190 const char *asn_mode2str(enum asnotation_mode asnotation
)
192 return lookup_msg(asnotation_mode_msg
, asnotation
,
193 "Unrecognized AS notation mode");
196 void asn_asn2json(json_object
*json
, const char *attr
,
197 as_t asn
, enum asnotation_mode asnotation
)
199 static char as_str
[ASN_STRING_MAX_SIZE
];
201 if ((asnotation
== ASNOTATION_PLAIN
) ||
202 ((asnotation
== ASNOTATION_DOT
) && asn
< UINT16_MAX
))
203 json_object_int_add(json
, attr
, asn
);
205 asn_asn2asdot(asn
, as_str
, sizeof(as_str
));
206 json_object_string_add(json
, attr
, as_str
);
210 void asn_asn2json_array(json_object
*jseg_list
, as_t asn
,
211 enum asnotation_mode asnotation
)
213 static char as_str
[ASN_STRING_MAX_SIZE
];
215 if ((asnotation
== ASNOTATION_PLAIN
) ||
216 ((asnotation
== ASNOTATION_DOT
) && asn
< UINT16_MAX
))
217 json_object_array_add(jseg_list
,
218 json_object_new_int64(asn
));
220 asn_asn2asdot(asn
, as_str
, sizeof(as_str
));
221 json_array_string_add(jseg_list
, as_str
);
225 char *asn_asn2string(const as_t
*asn
, char *buf
, size_t len
,
226 enum asnotation_mode asnotation
)
228 if ((asnotation
== ASNOTATION_PLAIN
) ||
229 ((asnotation
== ASNOTATION_DOT
) && *asn
< UINT16_MAX
))
230 snprintf(buf
, len
, "%u", *asn
);
232 asn_asn2asdot(*asn
, buf
, len
);
236 static ssize_t
printfrr_asnotation(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
238 enum asnotation_mode asnotation
)
240 /* for alignemnt up to 33 chars - %33pASD for instance - */
241 char as_str
[ASN_STRING_MAX_SIZE
*3];
245 return bputs(buf
, "(null)");
247 asn_asn2string(asn
, as_str
, sizeof(as_str
), asnotation
);
248 return bputs(buf
, as_str
);
251 printfrr_ext_autoreg_p("ASP", printfrr_asplain
);
252 static ssize_t
printfrr_asplain(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
255 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_PLAIN
);
258 printfrr_ext_autoreg_p("ASD", printfrr_asdot
);
259 static ssize_t
printfrr_asdot(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
262 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_DOT
);
265 printfrr_ext_autoreg_p("ASE", printfrr_asdotplus
);
266 static ssize_t
printfrr_asdotplus(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
269 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_DOTPLUS
);