]>
git.proxmox.com Git - mirror_frr.git/blob - lib/mpls.c
4 * Copyright (C) 2018 Cumulus Networks, Inc.
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)
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
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
26 * String to label conversion, labels separated by '/'.
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
37 int mpls_str2label(const char *label_str
, uint8_t *num_labels
,
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
48 /* labels to zero until we have a successful parse */
49 ostr
= lstr
= XSTRDUP(MTYPE_TMP
, label_str
);
53 for (i
= 0; i
< MPLS_MAX_LABELS
&& lstr
&& !rc
; i
++) {
54 nump
= strsep(&lstr
, "/");
55 pl
[i
] = strtoul(nump
, &endp
, 10);
61 else if (!IS_MPLS_UNRESERVED_LABEL(pl
[i
]))
66 if (!rc
&& i
== MPLS_MAX_LABELS
&& lstr
)
71 memcpy(labels
, pl
, *num_labels
* sizeof(mpls_label_t
));
74 XFREE(MTYPE_TMP
, ostr
);
80 * Label to string conversion, labels in string separated by '/'.
82 char *mpls_label2str(uint8_t num_labels
, const mpls_label_t
*labels
, char *buf
,
85 char label_buf
[BUFSIZ
];
89 for (i
= 0; i
< num_labels
; i
++) {
91 strlcat(buf
, "/", len
);
93 label2str(labels
[i
], label_buf
, sizeof(label_buf
));
95 snprintf(label_buf
, sizeof(label_buf
), "%u", labels
[i
]);
96 strlcat(buf
, label_buf
, len
);