#include "ll_map.h"
#include "libgenl.h"
-static const char * const values_on_off[] = { "off", "on" };
-
static const char * const validate_str[] = {
[MACSEC_VALIDATE_DISABLED] = "disabled",
[MACSEC_VALIDATE_CHECK] = "check",
exit(-1);
}
-static int one_of(const char *msg, const char *realval, const char * const *list,
- size_t len, int *index)
-{
- int i;
-
- for (i = 0; i < len; i++) {
- if (matches(realval, list[i]) == 0) {
- *index = i;
- return 0;
- }
- }
-
- fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
- for (i = 0; i < len; i++)
- fprintf(stderr, "\"%s\", ", list[i]);
- fprintf(stderr, "not \"%s\"\n", realval);
- return -1;
-}
-
static int get_an(__u8 *val, const char *arg)
{
int ret = get_u8(val, arg, 0);
if (argc == 0)
ipmacsec_usage();
- ret = one_of("offload", *argv, offload_str, ARRAY_SIZE(offload_str),
- (int *)&offload);
+ offload = parse_one_of("offload", *argv, offload_str, ARRAY_SIZE(offload_str), &ret);
if (ret)
ipmacsec_usage();
NEXT_ARG();
int i;
- ret = one_of("encrypt", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("encrypt", *argv, &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN, IFLA_MACSEC_ENCRYPT, i);
NEXT_ARG();
int i;
- ret = one_of("send_sci", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("send_sci", *argv, &ret);
if (ret != 0)
return ret;
send_sci = i;
NEXT_ARG();
int i;
- ret = one_of("end_station", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("end_station", *argv, &ret);
if (ret != 0)
return ret;
es = i;
NEXT_ARG();
int i;
- ret = one_of("scb", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("scb", *argv, &ret);
if (ret != 0)
return ret;
scb = i;
NEXT_ARG();
int i;
- ret = one_of("protect", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("protect", *argv, &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN, IFLA_MACSEC_PROTECT, i);
NEXT_ARG();
int i;
- ret = one_of("replay", *argv, values_on_off,
- ARRAY_SIZE(values_on_off), &i);
+ i = parse_on_off("replay", *argv, &ret);
if (ret != 0)
return ret;
replay_protect = !!i;
invarg("expected replay window size", *argv);
} else if (strcmp(*argv, "validate") == 0) {
NEXT_ARG();
- ret = one_of("validate", *argv,
- validate_str, ARRAY_SIZE(validate_str),
- (int *)&validate);
+ validate = parse_one_of("validate", *argv, validate_str,
+ ARRAY_SIZE(validate_str), &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN,
invarg("expected an { 0..3 }", *argv);
} else if (strcmp(*argv, "offload") == 0) {
NEXT_ARG();
- ret = one_of("offload", *argv,
- offload_str, ARRAY_SIZE(offload_str),
- (int *)&offload);
+ offload = parse_one_of("offload", *argv, offload_str,
+ ARRAY_SIZE(offload_str), &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN,
return ret;
}
+
+int parse_one_of(const char *msg, const char *realval, const char * const *list,
+ size_t len, int *p_err)
+{
+ int i;
+
+ for (i = 0; i < len; i++) {
+ if (list[i] && matches(realval, list[i]) == 0) {
+ *p_err = 0;
+ return i;
+ }
+ }
+
+ fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
+ for (i = 0; i < len; i++)
+ if (list[i])
+ fprintf(stderr, "\"%s\", ", list[i]);
+ fprintf(stderr, "not \"%s\"\n", realval);
+ *p_err = -EINVAL;
+ return 0;
+}
+
+bool parse_on_off(const char *msg, const char *realval, int *p_err)
+{
+ static const char * const values_on_off[] = { "off", "on" };
+
+ return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
+}