Fix up bugs in IP query
authorRonald G. Minnich <rminnich@google.com>
Wed, 19 Mar 2014 00:21:18 +0000 (17:21 -0700)
committerRonald G. Minnich <rminnich@google.com>
Wed, 19 Mar 2014 00:21:18 +0000 (17:21 -0700)
the problem was we don't implement access(2) yet

Signed-off-by: Ronald G. Minnich <rminnich@google.com>
scripts/RUN
tests/cs.c
tests/query.c [new file with mode: 0644]

index 34e4fdb..1307880 100644 (file)
@@ -1,4 +1,6 @@
-sudo qemu-system-x86_64 -s -cpu Haswell,+hypervisor,+vmx -smp 4 -m 2048 -nographic  \
+#sudo qemu-system-x86_64 -s -cpu core2duo,+hypervisor,+vmx -smp 4 -m 2048 -nographic  \
+sudo qemu-system-x86_64 -s -cpu kvm64,+vmx -smp 4 -m 2048 -nographic  \
+--machine pc,accel=kvm \
 -net nic,model=rtl8139 \
 -net user,hostfwd=tcp::5555-:23 \
 -kernel obj/kern/akaros-kernel $*
index 1d4cdb4..203a47d 100644 (file)
@@ -848,7 +848,7 @@ printf("CS: request data is :%s:\n", job->request.data);
         *  refresh all state
         */
        if(strncmp(job->request.data, "refresh", 7)==0){
-               netinit(1);
+               netinit(0/*1*/);
                goto send;
        }
 
@@ -888,7 +888,8 @@ printf("CS: request data is :%s:\n", job->request.data);
                mf->net = strdup(field[0]);
                break;
        }
-
+printf("CS: net %s host %s serv %s rem %s\n", 
+mf->net, mf->host, mf->serv, mf->rem);
        /*
         *  do the first net worth of lookup
         */
@@ -965,7 +966,7 @@ sendmsg(Job *job, char *err)
        }
        job->reply.tag = job->request.tag;
        n = convS2M(&job->reply, mdata, sizeof mdata);
-       if(n == 0){
+       if(n == 1){
                fprintf(stderr,  "CS:sendmsg convS2M of %F returns 0", &job->reply);
                abort();
        }
@@ -1107,11 +1108,16 @@ netinit(int background)
 
        /* add the mounted networks to the default list */
        for(np = network; np->net; np++){
+               int fuckup;
                if(np->considered)
                        continue;
                snprintf(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
-               if(access(clone, R_OK))
+               fuckup = open(clone, O_RDONLY);
+               if (fuckup < 0)
                        continue;
+               close(fuckup);
+               //if(access(clone, R_OK))
+                       //continue;
                if(netlist)
                        last->next = np;
                else
@@ -1195,6 +1201,7 @@ lookup(Mfile *mf)
        if(mf->net == NULL)
                return 0;       /* must have been a genquery */
 
+printf("CS: Look up net %s \n", mf->net);
        if(strcmp(mf->net, "net") == 0){
                /*
                 *  go through set of default nets
@@ -1235,13 +1242,16 @@ lookup(Mfile *mf)
        /*
         *  look for a specific network
         */
+printf("CS: netlist is %p\n", netlist);
        for(np = netlist; np && np->net != NULL; np++){
                if(np->fasttimeouthack)
                        continue;
+printf("CS: compare net np->net %s mf->net %s\n", np->net, mf->net);
                if(strcmp(np->net, mf->net) == 0)
                        break;
        }
 
+printf("CS: np is %p np->net %p\n", np, np ? np->net : NULL);
        if(np && np->net != NULL){
                /*
                 *  known network
@@ -1249,6 +1259,7 @@ lookup(Mfile *mf)
                nt = (*np->lookup)(np, mf->host, mf->serv, 1);
                for(t = nt; mf->nreply < Nreply && t; t = t->entry){
                        cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
+printf("CS: Lookup %s\n", cp);
                        if(cp){
                                mf->replylen[mf->nreply] = strlen(cp);
                                mf->reply[mf->nreply++] = cp;
@@ -1258,6 +1269,7 @@ lookup(Mfile *mf)
                ndbfree(nt);
                return rv;
        } else {
+printf("UNKONW NET\n");
                /*
                 *  not a known network, don't translate host or service
                 */
@@ -1267,6 +1279,7 @@ lookup(Mfile *mf)
                else
                        snprintf(reply, sizeof(reply), "%s/%s/clone %s",
                                mntpt, mf->net, mf->host);
+printf("CS: reply is %s\n", reply);
                mf->reply[0] = strdup(reply);
                mf->replylen[0] = strlen(reply);
                mf->nreply = 1;
@@ -1290,6 +1303,7 @@ ipserv(Network *np, char *name, char *buf, int blen)
        struct ndbtuple *t, *nt;
        struct ndbs s;
 
+printf("CS: port %s\n", name);
        /* '*' means any service */
        if(strcmp(name, "*")==0){
                strcpy(buf, name);
@@ -1310,6 +1324,7 @@ ipserv(Network *np, char *name, char *buf, int blen)
        p = NULL;
        if(alpha){
                p = ndbgetvalue(db, &s, np->net, name, "port", &t);
+printf("CS: get value of port %s, p is (we hope not null)%p\n", name, p);
                if(p == NULL)
                        return 0;
        } else {
@@ -1330,7 +1345,7 @@ ipserv(Network *np, char *name, char *buf, int blen)
        }
        snprintf(buf, blen, "%s%s", p, restr ? "!r" : "");
        free(p);
-
+printf("CS: buf is %s\n", buf);
        return buf;
 }
 
diff --git a/tests/query.c b/tests/query.c
new file mode 100644 (file)
index 0000000..761ebf3
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ *  search the network database for matches
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <parlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <error.h>
+#include <iplib.h>
+#include <ndb.h>
+
+static int all, multiple;
+
+char *argv0;
+#define        ARGBEGIN        for((argv0||(argv0=*argv)),argv++,argc--;\
+                           argv[0] && argv[0][0]=='-' && argv[0][1];\
+                           argc--, argv++) {\
+                               char *_args, *_argt;\
+                               char _argc;             \
+                               _args = &argv[0][1];\
+                               if(_args[0]=='-' && _args[1]==0){\
+                                       argc--; argv++; break;\
+                               }\
+                               _argc = _args[0];\
+                               while(*_args && _args++)\
+                                       switch(_argc)
+#define        ARGEND          /*SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc);*/}
+#define        ARGF()          (_argt=_args, _args="",\
+                               (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
+#define        EARGF(x)        (_argt=_args, _args="",\
+                               (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
+
+#define        ARGC()          _argc
+
+void
+usage(void)
+{
+       fprintf(stderr, "usage: query [-am] [-f ndbfile] attr value "
+               "[returned-attr [reps]]\n");
+       fprintf(stderr, "usage");
+       exit(1);
+}
+
+/* print values of nt's attributes matching rattr */
+static void
+prmatch(struct ndbtuple *nt, char *rattr)
+{
+       for(; nt; nt = nt->entry)
+               if (strcmp(nt->attr, rattr) == 0)
+                       printf("%s\n", nt->val);
+}
+
+void
+search(struct ndb *db, char *attr, char *val, char *rattr)
+{
+       char *p;
+       struct ndbs s;
+       struct ndbtuple *t, *nt;
+
+       /* first entry with a matching rattr */
+       if(rattr && !all){
+               p = ndbgetvalue(db, &s, attr, val, rattr, &t);
+               if (multiple)
+                       prmatch(t, rattr);
+               else if(p)
+                       printf("%s\n", p);
+               ndbfree(t);
+               free(p);
+               return;
+       }
+
+       /* all entries with matching rattrs */
+       if(rattr) {
+               for(t = ndbsearch(db, &s, attr, val); t != NULL;
+                   t = ndbsnext(&s, attr, val)){
+                       prmatch(t, rattr);
+                       ndbfree(t);
+               }
+               return;
+       }
+
+       /* all entries */
+       for(t = ndbsearch(db, &s, attr, val); t; t = ndbsnext(&s, attr, val)){
+               for(nt = t; nt; nt = nt->entry)
+                       printf("%s=%s ", nt->attr, nt->val);
+               printf("\n");
+               ndbfree(t);
+       }
+}
+
+void
+main(int argc, char **argv)
+{
+       int reps = 1;
+       char *rattr = NULL, *dbfile = NULL;
+       struct ndb *db;
+       
+       ARGBEGIN{
+       case 'a':
+               all++;
+               break;
+       case 'm':
+               multiple++;
+               break;
+       case 'f':
+               dbfile = EARGF(usage());
+               break;
+       default:
+               usage();
+       }ARGEND;
+
+       switch(argc){
+       case 4:
+               reps = atoi(argv[3]);   /* wtf use is this? */
+               /* fall through */
+       case 3:
+               rattr = argv[2];
+               break;
+       case 2:
+               rattr = NULL;
+               break;
+       default:
+               usage();
+       }
+
+       db = ndbopen(dbfile);
+       if(db == NULL){
+               error(1, 0, "no db: %r");
+       }
+       while(reps--)
+               search(db, argv[0], argv[1], rattr);
+       ndbclose(db);
+}
+