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
))
56 while (isdigit((unsigned char)*p
)) {
60 if (temp_val
> UINT32_MAX
)
65 high
= (uint32_t)temp_val
;
66 if (*p
== '.') { /* dot format */
69 if (*p
== '\0' && partial
) {
73 while (isdigit((unsigned char)*p
)) {
77 if (temp_val
> UINT16_MAX
)
82 low
= (uint32_t)temp_val
;
84 if (!next
&& *p
!= '\0' && !isdigit((unsigned char)*p
))
86 /* AS <AS4B>.<AS4B> is forbidden */
87 if (high
> UINT16_MAX
)
89 /* AS 0.0 is authorised for some case only */
90 if (!relax_as_zero
&& high
== 0 && low
== 0) {
96 *asn
= (high
<< 16) + low
;
99 val
= ASNOTATION_DOTPLUS
;
101 val
= ASNOTATION_DOT
;
104 /* AS 0 is forbidden */
105 if (!relax_as_zero
&& high
== 0)
121 static void asn_asn2asdot(as_t asn
, char *asstring
, size_t len
)
125 high
= (asn
>> 16) & 0xffff;
127 snprintf(asstring
, len
, "%hu.%hu", high
, low
);
130 bool asn_str2asn(const char *asstring
, as_t
*asn
)
132 return asn_str2asn_internal(asstring
, asn
, NULL
, NULL
, NULL
);
135 const char *asn_asn2asplain(as_t asn
)
137 static char buf
[ASN_STRING_MAX_SIZE
];
139 snprintf(buf
, sizeof(buf
), "%u", asn
);
143 const char *asn_str2asn_parse(const char *asstring
, as_t
*asn
, bool *found_ptr
)
145 const char *p
= NULL
;
146 const char **next
= &p
;
149 found
= asn_str2asn_internal(asstring
, asn
, next
, NULL
, NULL
);
155 void asn_relax_as_zero(bool relax
)
157 relax_as_zero
= relax
;
160 enum match_type
asn_str2asn_match(const char *str
)
162 bool found
, partial
= false;
164 found
= asn_str2asn_internal(str
, NULL
, NULL
, &partial
, NULL
);
165 if (found
&& !partial
)
174 bool asn_str2asn_notation(const char *asstring
, as_t
*asn
,
175 enum asnotation_mode
*asnotation
)
177 return asn_str2asn_internal(asstring
, asn
, NULL
, NULL
, asnotation
);
180 const char *asn_mode2str(enum asnotation_mode asnotation
)
182 return lookup_msg(asnotation_mode_msg
, asnotation
,
183 "Unrecognized AS notation mode");
186 void asn_asn2json(json_object
*json
, const char *attr
,
187 as_t asn
, enum asnotation_mode asnotation
)
189 static char as_str
[ASN_STRING_MAX_SIZE
];
191 if ((asnotation
== ASNOTATION_PLAIN
) ||
192 ((asnotation
== ASNOTATION_DOT
) && asn
< UINT16_MAX
))
193 json_object_int_add(json
, attr
, asn
);
195 asn_asn2asdot(asn
, as_str
, sizeof(as_str
));
196 json_object_string_add(json
, attr
, as_str
);
200 void asn_asn2json_array(json_object
*jseg_list
, as_t asn
,
201 enum asnotation_mode asnotation
)
203 static char as_str
[ASN_STRING_MAX_SIZE
];
205 if ((asnotation
== ASNOTATION_PLAIN
) ||
206 ((asnotation
== ASNOTATION_DOT
) && asn
< UINT16_MAX
))
207 json_object_array_add(jseg_list
,
208 json_object_new_int64(asn
));
210 asn_asn2asdot(asn
, as_str
, sizeof(as_str
));
211 json_array_string_add(jseg_list
, as_str
);
215 char *asn_asn2string(const as_t
*asn
, char *buf
, size_t len
,
216 enum asnotation_mode asnotation
)
218 if ((asnotation
== ASNOTATION_PLAIN
) ||
219 ((asnotation
== ASNOTATION_DOT
) && *asn
< UINT16_MAX
))
220 snprintf(buf
, len
, "%u", *asn
);
222 asn_asn2asdot(*asn
, buf
, len
);
226 static ssize_t
printfrr_asnotation(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
228 enum asnotation_mode asnotation
)
230 /* for alignemnt up to 33 chars - %33pASD for instance - */
231 char as_str
[ASN_STRING_MAX_SIZE
*3];
235 return bputs(buf
, "(null)");
237 asn_asn2string(asn
, as_str
, sizeof(as_str
), asnotation
);
238 return bputs(buf
, as_str
);
241 printfrr_ext_autoreg_p("ASP", printfrr_asplain
);
242 static ssize_t
printfrr_asplain(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
245 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_PLAIN
);
248 printfrr_ext_autoreg_p("ASD", printfrr_asdot
);
249 static ssize_t
printfrr_asdot(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
252 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_DOT
);
255 printfrr_ext_autoreg_p("ASE", printfrr_asdotplus
);
256 static ssize_t
printfrr_asdotplus(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
259 return printfrr_asnotation(buf
, ea
, ptr
, ASNOTATION_DOTPLUS
);