chiark / gitweb /
ipif: Introduce eat_optionalstr (and make protocol optional)
[userv-utils.git] / ipif / service.c
index df039269cf19297b2f09dceba9a0866ca88e39d4..b401a5cb39dd76a21d451548a91c87a90eacd398 100644 (file)
@@ -30,7 +30,7 @@
  *
  *  The remaining arguments are supplied by the (untrusted) caller:
  *
- *  <local-addr>,<peer-addr>,<mtu>,<proto>
+ *  <local-addr>,<peer-addr>,<mtu>[,[<proto>]]
  *
  *      As for slattach.  The only supported protocol is slip.
  *      Alternatively, set to `debug' to print debugging info and
 #include <unistd.h>
 #include <stdint.h>
 #include <poll.h>
+#include <stddef.h>
 
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -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");
   }