From 4f937f540734e6f5977b9c279f8f24035a259310 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 15 Apr 2017 18:40:07 +0100 Subject: [PATCH] ipif: Introduce eat_optionalstr (and make protocol optional) Signed-off-by: Ian Jackson --- ipif/service.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/ipif/service.c b/ipif/service.c index df03926..b401a5c 100644 --- a/ipif/service.c +++ b/ipif/service.c @@ -30,7 +30,7 @@ * * The remaining arguments are supplied by the (untrusted) caller: * - * ,,, + * ,,[,[]] * * As for slattach. The only supported protocol is slip. * Alternatively, set to `debug' to print debugging info and @@ -125,6 +125,7 @@ #include #include #include +#include #include #include @@ -323,6 +324,30 @@ static void eat_prefixmask(const char **argp, const char *what, if (len_r) *len_r= len; } +static char *eat_optionalstr(const char **argp, + const char *what, + const char *def) { + ptrdiff_t len; + const char *start= *argp; + const char *comma= strchr(start, ','); + if (comma) { + len= comma - start; + *argp= comma + 1; + } else { + len= strlen(start); + *argp= start + len; + } + if (!len) { + start= def; + len= strlen(def); + } + char *r = malloc(len+1); + if (!r) sysfatal("malloc for command line string"); + memcpy(r,start,len); + r[len]= 0; + return r; +} + static int addrnet_isin(unsigned long prefix, unsigned long mask, unsigned long mprefix, unsigned long mmask) { return !(~mask & mmask) && (prefix & mmask) == mprefix; @@ -545,12 +570,13 @@ static void parseargs(int argc, const char *const *argv) { peeraddr= eat_addr(&carg,"peer-addr", ",",0); mtu= eat_number(&carg,"mtu", 576,65536, ",",0); localallow= peerallow= 0; - - if (!strcmp(carg,"debug")) { + + char *protostr = eat_optionalstr(&carg,"protocol","slip"); + if (!strcmp(protostr,"debug")) { proto= 0; } else { for (cprotop= protos_ok; - (proto= *cprotop) && strcmp(proto,carg); + (proto= *cprotop) && strcmp(proto,protostr); cprotop++); if (!proto) fatal("invalid protocol"); } -- 2.30.2