]>
Commit | Line | Data |
---|---|---|
036fdaad DS |
1 | /* |
2 | * mpls functions | |
3 | * | |
4 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
5 | * Donald Sharp | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the Free | |
9 | * Software Foundation; either version 2 of the License, or (at your option) | |
10 | * any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 | * more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | #include <zebra.h> | |
22 | #include <mpls.h> | |
23 | #include <memory.h> | |
24 | ||
25 | /* | |
26 | * String to label conversion, labels separated by '/'. | |
27 | * | |
28 | * @param label_str labels separated by / | |
29 | * @param num_labels number of labels; zero if conversion was unsuccessful | |
30 | * @param labels preallocated mpls_label_t array of size MPLS_MAX_LABELS; only | |
31 | * modified if the conversion succeeded | |
32 | * @return 0 on success | |
33 | * -1 if the string could not be parsed as integers | |
34 | * -2 if a label was inside the reserved range (0-15) | |
35 | * -3 if the number of labels given exceeds MPLS_MAX_LABELS | |
36 | */ | |
37 | int mpls_str2label(const char *label_str, uint8_t *num_labels, | |
38 | mpls_label_t *labels) | |
39 | { | |
40 | char *ostr; // copy of label string (start) | |
41 | char *lstr; // copy of label string | |
42 | char *nump; // pointer to next segment | |
43 | char *endp; // end pointer | |
44 | int i; // for iterating label_str | |
45 | int rc; // return code | |
46 | mpls_label_t pl[MPLS_MAX_LABELS]; // parsed labels | |
47 | ||
48 | /* labels to zero until we have a successful parse */ | |
49 | ostr = lstr = XSTRDUP(MTYPE_TMP, label_str); | |
50 | *num_labels = 0; | |
51 | rc = 0; | |
52 | ||
53 | for (i = 0; i < MPLS_MAX_LABELS && lstr && !rc; i++) { | |
54 | nump = strsep(&lstr, "/"); | |
55 | pl[i] = strtoul(nump, &endp, 10); | |
56 | ||
57 | /* format check */ | |
58 | if (*endp != '\0') | |
59 | rc = -1; | |
60 | /* validity check */ | |
61 | else if (!IS_MPLS_UNRESERVED_LABEL(pl[i])) | |
62 | rc = -2; | |
63 | } | |
64 | ||
65 | /* excess labels */ | |
66 | if (!rc && i == MPLS_MAX_LABELS && lstr) | |
67 | rc = -3; | |
68 | ||
69 | if (!rc) { | |
70 | *num_labels = i; | |
71 | memcpy(labels, pl, *num_labels * sizeof(mpls_label_t)); | |
72 | } | |
73 | ||
74 | XFREE(MTYPE_TMP, ostr); | |
75 | ||
76 | return rc; | |
77 | } | |
78 | ||
79 | /* | |
80 | * Label to string conversion, labels in string separated by '/'. | |
81 | */ | |
82 | char *mpls_label2str(uint8_t num_labels, mpls_label_t *labels, char *buf, | |
83 | int len, int pretty) | |
84 | { | |
85 | char label_buf[BUFSIZ]; | |
86 | int i; | |
87 | ||
88 | buf[0] = '\0'; | |
89 | for (i = 0; i < num_labels; i++) { | |
90 | if (i != 0) | |
91 | strlcat(buf, "/", len); | |
92 | if (pretty) | |
93 | label2str(labels[i], label_buf, sizeof(label_buf)); | |
94 | else | |
95 | snprintf(label_buf, sizeof(label_buf), "%u", labels[i]); | |
96 | strlcat(buf, label_buf, len); | |
97 | } | |
98 | ||
99 | return buf; | |
100 | } |