summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jackson <ijackson@chiark.greenend.org.uk>2017-04-15 18:40:07 +0100
committerIan Jackson <ijackson@chiark.greenend.org.uk>2017-04-15 18:40:07 +0100
commit4f937f540734e6f5977b9c279f8f24035a259310 (patch)
tree26ee876375dfcd90c1c81e5a5b739d8c096f8145
parent5e91d4da205f42c4811954bf1145d59efb95327b (diff)
downloaduserv-utils-4f937f540734e6f5977b9c279f8f24035a259310.tar.gz
ipif: Introduce eat_optionalstr (and make protocol optional)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
-rw-r--r--ipif/service.c34
1 files 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:
*
- * <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
@@ -125,6 +125,7 @@
#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");
}