/*
* String to label conversion, labels separated by '/'.
+ *
+ * @param label_str labels separated by /
+ * @param num_labels number of labels; zero if conversion was unsuccessful
+ * @param labels preallocated mpls_label_t array of size MPLS_MAX_LABELS; only
+ * modified if the conversion succeeded
+ * @return 0 on success
+ * -1 if the string could not be parsed as integers
+ * -2 if a label was inside the reserved range (0-15)
+ * -3 if the number of labels given exceeds MPLS_MAX_LABELS
*/
int
mpls_str2label (const char *label_str, u_int8_t *num_labels,
mpls_label_t *labels)
{
- char *endp;
- int i;
+ char *ostr; // copy of label string (start)
+ char *lstr; // copy of label string
+ char *nump; // pointer to next segment
+ char *endp; // end pointer
+ int i; // for iterating label_str
+ int rc; // return code
+ mpls_label_t pl[MPLS_MAX_LABELS]; // parsed labels
+ /* labels to zero until we have a successful parse */
+ ostr = lstr = XSTRDUP (MTYPE_TMP, label_str);
*num_labels = 0;
- for (i = 0; i < MPLS_MAX_LABELS; i++)
- {
- mpls_label_t label;
+ rc = 0;
- label = strtoul(label_str, &endp, 0);
-
- /* validity checks */
- if (endp == label_str)
- return -1;
-
- if (!IS_MPLS_UNRESERVED_LABEL(label))
- return -1;
+ for (i = 0; i < MPLS_MAX_LABELS && lstr && !rc; i++)
+ {
+ nump = strsep (&lstr, "/");
+ pl[i] = strtoul(nump, &endp, 10);
- labels[i] = label;
- if (*endp == '\0')
- {
- *num_labels = i + 1;
- return 0;
- }
+ /* format check */
+ if (*endp != '\0')
+ rc = -1;
+ /* validity check */
+ else if (!IS_MPLS_UNRESERVED_LABEL(pl[i]))
+ rc = -2;
+ }
- /* Check separator. */
- if (*endp != '/')
- return -1;
+ /* excess labels */
+ if (!rc && i == MPLS_MAX_LABELS && lstr)
+ rc = -3;
- label_str = endp + 1;
+ if (!rc)
+ {
+ *num_labels = i + 1;
+ memcpy (labels, pl, *num_labels * sizeof (mpls_label_t));
}
- /* Too many labels. */
- return -1;
+ XFREE (MTYPE_TMP, ostr);
+
+ return rc;
}
/*
VTY_NEWLINE);
return CMD_WARNING;
}
- if (mpls_str2label (label_str, &snh_label.num_labels,
- snh_label.label))
+ int rc = mpls_str2label (label_str, &snh_label.num_labels,
+ snh_label.label);
+ if (rc < 0)
{
- vty_out (vty, "%% Malformed label(s)%s", VTY_NEWLINE);
+ switch (rc) {
+ case -1:
+ vty_out (vty, "%% Malformed label(s)%s", VTY_NEWLINE);
+ break;
+ case -2:
+ vty_out (vty, "%% Cannot use reserved label(s) (%d-%d)%s",
+ MPLS_MIN_RESERVED_LABEL, MPLS_MAX_RESERVED_LABEL,
+ VTY_NEWLINE);
+ break;
+ case -3:
+ vty_out (vty, "%% Too many labels. Enter %d or fewer%s",
+ MPLS_MAX_LABELS, VTY_NEWLINE);
+ break;
+ }
return CMD_WARNING;
}
}
VTY_NEWLINE);
return CMD_WARNING;
}
- if (mpls_str2label (label_str, &snh_label.num_labels,
- snh_label.label))
+ int rc = mpls_str2label (label_str, &snh_label.num_labels,
+ snh_label.label);
+ if (rc < 0)
{
- vty_out (vty, "%% Malformed label(s)%s", VTY_NEWLINE);
+ switch (rc) {
+ case -1:
+ vty_out (vty, "%% Malformed label(s)%s", VTY_NEWLINE);
+ break;
+ case -2:
+ vty_out (vty, "%% Cannot use reserved label(s) (%d-%d)%s",
+ MPLS_MIN_RESERVED_LABEL, MPLS_MAX_RESERVED_LABEL,
+ VTY_NEWLINE);
+ break;
+ case -3:
+ vty_out (vty, "%% Too many labels. Enter %d or fewer%s",
+ MPLS_MAX_LABELS, VTY_NEWLINE);
+ break;
+ }
return CMD_WARNING;
}
}