Change-Id: I82fc00318101fb321112361dadda497f48861e8c Signed-off-by: Dan Cross <[email protected]> --- tests/cs.c | 1023 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 476 insertions(+), 547 deletions(-)
diff --git a/tests/cs.c b/tests/cs.c index 232c731..12c0e11 100644 --- a/tests/cs.c +++ b/tests/cs.c @@ -1,4 +1,4 @@ -/* +/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No @@ -8,170 +8,163 @@ */ #include <stdlib.h> -#include <stdio.h> -#include <parlib/parlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <signal.h> -#include <pthread.h> -#include <fcntl.h> #include <ctype.h> #include <error.h> +#include <fcall.h> +#include <fcntl.h> #include <iplib/iplib.h> -#include <ndblib/ndb.h> #include <ndblib/fcallfmt.h> -#include <fcall.h> +#include <ndblib/ndb.h> +#include <parlib/parlib.h> #include <parlib/spinlock.h> +#include <pthread.h> #include <ros/common.h> +#include <signal.h> +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> -enum -{ - Nreply= 20, - Maxreply= 256, - Maxrequest= 128, - Maxpath= 128, - Maxfdata= 8192, - Maxhost= 64, /* maximum host name size */ - Maxservice= 64, /* maximum service name size */ - - Qdir= 0, - Qcs= 1, +enum { + Nreply = 20, + Maxreply = 256, + Maxrequest = 128, + Maxpath = 128, + Maxfdata = 8192, + Maxhost = 64, /* maximum host name size */ + Maxservice = 64, /* maximum service name size */ + + Qdir = 0, + Qcs = 1, }; -typedef struct Mfile Mfile; -typedef struct Mlist Mlist; -typedef struct Network Network; -typedef struct Flushreq Flushreq; -typedef struct Job Job; +typedef struct Mfile Mfile; +typedef struct Mlist Mlist; +typedef struct Network Network; +typedef struct Flushreq Flushreq; +typedef struct Job Job; -int vers; /* incremented each clone/attach */ +int vers; /* incremented each clone/attach */ /* need to resolve the #inluce for all this stuff. */ -#define DMDIR 0x80000000 /* mode bit for directories */ +#define DMDIR 0x80000000 /* mode bit for directories */ #define QTDIR 0x80 #define QTFILE 0 #define ERRMAX 128 -struct Mfile -{ - int busy; +struct Mfile { + int busy; - char *user; - struct qid qid; - int fid; + char *user; + struct qid qid; + int fid; /* * current request */ - char *net; - char *host; - char *serv; - char *rem; + char *net; + char *host; + char *serv; + char *rem; /* * result of the last lookup */ - Network *nextnet; - int nreply; - char *reply[Nreply]; - int replylen[Nreply]; + Network *nextnet; + int nreply; + char *reply[Nreply]; + int replylen[Nreply]; }; -struct Mlist -{ - Mlist *next; - Mfile mf; +struct Mlist { + Mlist *next; + Mfile mf; }; - /* * active requests */ -struct Job -{ - Job *next; - int flushed; - struct fcall request; - struct fcall reply; +struct Job { + Job *next; + int flushed; + struct fcall request; + struct fcall reply; pthread_t thread; }; spinlock_t joblock = SPINLOCK_INITIALIZER; -Job *joblist; - -Mlist *mlist; -int mfd[2]; -int debug; -int paranoia; -int ipv6lookups = 1; -char *dbfile = "lib/ndb/local"; -struct ndb *db, *netdb; - -void rversion(Job*); -void rflush(Job*); -void rattach(Job*, Mfile*); -char* rwalk(Job*, Mfile*); -void ropen(Job*, Mfile*); -void rcreate(Job*, Mfile*); -void rread(Job*, Mfile*); -void rwrite(Job*, Mfile*); -void rclunk(Job*, Mfile*); -void rremove(Job*, Mfile*); -void rstat(Job*, Mfile*); -void rwstat(Job*, Mfile*); -void rauth(Job*); -void sendmsg(Job*, char*); -void mountinit(char*, char*); -void io(void); -void ndbinit(void); -void netinit(int); -void netadd(char*); -char *genquery(Mfile*, char*); -char* ipinfoquery(Mfile*, char**, int); -int needproto(Network*, struct ndbtuple*); -int lookup(Mfile*); -struct ndbtuple* reorder(struct ndbtuple*, struct ndbtuple*); -void ipid(void); -void readipinterfaces(void); -void* emalloc(int); -char* estrdup(char*); -Job* newjob(void); -void freejob(Job*); -void setext(char*, int, char*); -void cleanmf(Mfile*); - -extern void paralloc(void); - -spinlock_t dblock = SPINLOCK_INITIALIZER; /* mutex on database operations */ -spinlock_t netlock = SPINLOCK_INITIALIZER; /* mutex for netinit() */ - -char *logfile = "cs"; -char *paranoiafile = "cs.paranoia"; - -char mntpt[Maxpath]; -char netndb[Maxpath]; +Job *joblist; + +Mlist *mlist; +int mfd[2]; +int debug; +int paranoia; +int ipv6lookups = 1; +char *dbfile = "lib/ndb/local"; +struct ndb *db, *netdb; + +void rversion(Job *); +void rflush(Job *); +void rattach(Job *, Mfile *); +char *rwalk(Job *, Mfile *); +void ropen(Job *, Mfile *); +void rcreate(Job *, Mfile *); +void rread(Job *, Mfile *); +void rwrite(Job *, Mfile *); +void rclunk(Job *, Mfile *); +void rremove(Job *, Mfile *); +void rstat(Job *, Mfile *); +void rwstat(Job *, Mfile *); +void rauth(Job *); +void sendmsg(Job *, char *); +void mountinit(char *, char *); +void io(void); +void ndbinit(void); +void netinit(int); +void netadd(char *); +char *genquery(Mfile *, char *); +char *ipinfoquery(Mfile *, char **, int); +int needproto(Network *, struct ndbtuple *); +int lookup(Mfile *); +struct ndbtuple *reorder(struct ndbtuple *, struct ndbtuple *); +void ipid(void); +void readipinterfaces(void); +void *emalloc(int); +char *estrdup(char *); +Job *newjob(void); +void freejob(Job *); +void setext(char *, int, char *); +void cleanmf(Mfile *); + +extern void paralloc(void); + +spinlock_t dblock = SPINLOCK_INITIALIZER; /* mutex on database operations */ +spinlock_t netlock = SPINLOCK_INITIALIZER; /* mutex for netinit() */ + +char *logfile = "cs"; +char *paranoiafile = "cs.paranoia"; + +char mntpt[Maxpath]; +char netndb[Maxpath]; /* * Network specific translators */ -struct ndbtuple* iplookup(Network*, char*, char*, int); -char* iptrans(struct ndbtuple*, Network*, char*, char*, int); -struct ndbtuple* telcolookup(Network*, char*, char*, int); -char* telcotrans(struct ndbtuple*, Network*, char*, char*, int); -struct ndbtuple* dnsiplookup(char*, struct ndbs*); - -struct Network -{ - char *net; - struct ndbtuple *(*lookup)(Network*, char*, char*, int); - char *(*trans)(struct ndbtuple*, Network*, char*, char*, int); - int considered; /* flag: ignored for "net!"? */ - int fasttimeouthack; /* flag. was for IL */ - Network *next; +struct ndbtuple *iplookup(Network *, char *, char *, int); +char *iptrans(struct ndbtuple *, Network *, char *, char *, int); +struct ndbtuple *telcolookup(Network *, char *, char *, int); +char *telcotrans(struct ndbtuple *, Network *, char *, char *, int); +struct ndbtuple *dnsiplookup(char *, struct ndbs *); + +struct Network { + char *net; + struct ndbtuple *(*lookup)(Network *, char *, char *, int); + char *(*trans)(struct ndbtuple *, Network *, char *, char *, int); + int considered; /* flag: ignored for "net!"? */ + int fasttimeouthack; /* flag. was for IL */ + Network *next; }; -enum -{ +enum { Ntcp = 0, }; @@ -179,37 +172,31 @@ enum * net doesn't apply to (r)udp, icmp(v6), or telco (for speed). */ Network network[] = { -[Ntcp] { "tcp", iplookup, iptrans, 0 }, - { "udp", iplookup, iptrans, 1 }, - { "icmp", iplookup, iptrans, 1 }, - { "icmpv6", iplookup, iptrans, 1 }, - { "rudp", iplookup, iptrans, 1 }, - { "ssh", iplookup, iptrans, 1 }, - { "telco", telcolookup, telcotrans, 1 }, - { 0 }, + [Ntcp] { "tcp", iplookup, iptrans, 0 }, {"udp", iplookup, iptrans, 1}, + {"icmp", iplookup, iptrans, 1}, {"icmpv6", iplookup, iptrans, 1}, + {"rudp", iplookup, iptrans, 1}, {"ssh", iplookup, iptrans, 1}, + {"telco", telcolookup, telcotrans, 1}, {0}, }; spinlock_t ipifclock = SPINLOCK_INITIALIZER; struct ipifc *ipifcs; -char eaddr[16]; /* ascii ethernet address */ -char ipaddr[64]; /* ascii internet address */ -uint8_t ipa[IPaddrlen]; /* binary internet address */ -char *mysysname; +char eaddr[16]; /* ascii ethernet address */ +char ipaddr[64]; /* ascii internet address */ +uint8_t ipa[IPaddrlen]; /* binary internet address */ +char *mysysname; -Network *netlist; /* networks ordered by preference */ +Network *netlist; /* networks ordered by preference */ Network *last; -static void -nstrcpy(char *to, char *from, int len) +static void nstrcpy(char *to, char *from, int len) { strncpy(to, from, len); - to[len-1] = 0; + to[len - 1] = 0; } char *argv0; -void -usage(void) +void usage(void) { fprintf(stderr, "CS:usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0); fprintf(stderr, "CS:usage"); @@ -220,8 +207,7 @@ usage(void) * based on libthread's threadsetname, but drags in less library code. * actually just sets the arguments displayed. */ -void -procsetname(char *fmt, ...) +void procsetname(char *fmt, ...) { /* someday ... */ #if 0 @@ -244,8 +230,7 @@ procsetname(char *fmt, ...) #endif } -void -main(int argc, char *argv[]) +void main(int argc, char *argv[]) { int justsetname; char ext[Maxpath], servefile[Maxpath]; @@ -255,35 +240,35 @@ main(int argc, char *argv[]) register_printf_specifier('F', printf_fcall, printf_fcall_info); ext[0] = 0; argc--, argv++; - while (argc && **argv == '-'){ - switch(argv[0][1]){ - case '4': - ipv6lookups = 0; - break; - case 'd': - debug = 1; - break; - case 'f': - if (argc < 2) - usage(); - dbfile = argv[1]; - argc--,argv++; - break; - case 'n': - justsetname = 1; - break; - case 'x': - if (argc < 2) - usage(); - setnetmtpt(mntpt, sizeof(mntpt), argv[1]); - argc--,argv++; - setext(ext, sizeof(ext), mntpt); - break; + while (argc && **argv == '-') { + switch (argv[0][1]) { + case '4': + ipv6lookups = 0; + break; + case 'd': + debug = 1; + break; + case 'f': + if (argc < 2) + usage(); + dbfile = argv[1]; + argc--, argv++; + break; + case 'n': + justsetname = 1; + break; + case 'x': + if (argc < 2) + usage(); + setnetmtpt(mntpt, sizeof(mntpt), argv[1]); + argc--, argv++; + setext(ext, sizeof(ext), mntpt); + break; } - argc--,argv++; + argc--, argv++; } - //rfork(RFREND|RFNOTEG); + // rfork(RFREND|RFNOTEG); /* Make us an SCP with a 2LS */ parlib_wants_to_be_mcp = FALSE; @@ -296,7 +281,7 @@ main(int argc, char *argv[]) ndbinit(); netinit(0); - if(!justsetname){ + if (!justsetname) { mountinit(servefile, mntpt); io(); } @@ -307,25 +292,23 @@ main(int argc, char *argv[]) * if a mount point is specified, set the cs extention to be the mount point * with '_'s replacing '/'s */ -void -setext(char *ext, int n, char *p) +void setext(char *ext, int n, char *p) { int i, c; n--; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) { c = p[i]; - if(c == 0) + if (c == 0) break; - if(c == '/') + if (c == '/') c = '_'; ext[i] = c; } ext[i] = 0; } -void -mountinit(char *service, char *mntpt) +void mountinit(char *service, char *mntpt) { int f; int p[2]; @@ -333,20 +316,20 @@ mountinit(char *service, char *mntpt) int ret; ret = pipe(p); - if (ret < 0){ + if (ret < 0) { error(1, 0, "pipe: %r"); exit(1); } /* * make a /srv/cs - * ORCLOSE means remove on last close. Handy. Not here yet. + * ORCLOSE means remove on last close. Handy. Not here yet. */ - f = open(service, O_WRONLY|O_CREAT/*|ORCLOSE*/, 0666); - if(f < 0) - error(1, 0, "%s: %r",service); + f = open(service, O_WRONLY | O_CREAT /*|ORCLOSE*/, 0666); + if (f < 0) + error(1, 0, "%s: %r", service); snprintf(buf, sizeof(buf), "%d", p[1]); - if(write(f, buf, strlen(buf)) != strlen(buf)) + if (write(f, buf, strlen(buf)) != strlen(buf)) error(1, 0, "Write %s: %r", service); /* using #s: we create a pipe and drop it into #srv. * we no longer mount. That's up to you. @@ -357,33 +340,31 @@ mountinit(char *service, char *mntpt) mfd[0] = mfd[1] = p[0]; } -void -ndbinit(void) +void ndbinit(void) { db = ndbopen(dbfile); - if(db == NULL) - error(1, 0, "%s: %r","can't open network database"); + if (db == NULL) + error(1, 0, "%s: %r", "can't open network database"); netdb = ndbopen(netndb); - if(netdb != NULL){ + if (netdb != NULL) { netdb->nohash = 1; db = ndbcat(netdb, db); } } -Mfile* -newfid(int fid) +Mfile *newfid(int fid) { Mlist *f, *ff; Mfile *mf; ff = 0; - for(f = mlist; f; f = f->next) - if(f->mf.busy && f->mf.fid == fid) + for (f = mlist; f; f = f->next) + if (f->mf.busy && f->mf.fid == fid) return &f->mf; - else if(!ff && !f->mf.busy) + else if (!ff && !f->mf.busy) ff = f; - if(ff == 0){ + if (ff == 0) { ff = emalloc(sizeof *f); ff->next = mlist; mlist = ff; @@ -394,14 +375,13 @@ newfid(int fid) return mf; } -Job* -newjob(void) +Job *newjob(void) { Job *job; job = calloc(1, sizeof(Job)); - if (! job){ - error(1, 0, "%s: %r","job calloc"); + if (!job) { + error(1, 0, "%s: %r", "job calloc"); } spinlock_lock(&joblock); job->next = joblist; @@ -411,14 +391,13 @@ newjob(void) return job; } -void -freejob(Job *job) +void freejob(Job *job) { Job **l; Job *to_free = 0; spinlock_lock(&joblock); - for(l = &joblist; *l; l = &(*l)->next){ - if((*l) == job){ + for (l = &joblist; *l; l = &(*l)->next) { + if ((*l) == job) { *l = job->next; to_free = job; break; @@ -429,14 +408,13 @@ freejob(Job *job) free(to_free); } -void -flushjob(int tag) +void flushjob(int tag) { Job *job; spinlock_lock(&joblock); - for(job = joblist; job; job = job->next){ - if(job->request.tag == tag && job->request.type != Tflush){ + for (job = joblist; job; job = job->next) { + if (job->request.tag == tag && job->request.type != Tflush) { job->flushed = 1; break; } @@ -444,16 +422,16 @@ flushjob(int tag) spinlock_unlock(&joblock); } -void *job_thread(void* arg) +void *job_thread(void *arg) { Mfile *mf; Job *job = arg; spinlock_lock(&dblock); mf = newfid(job->request.fid); - if(debug) + if (debug) fprintf(stderr, "CS:%F", &job->request); - switch(job->request.type){ + switch (job->request.type) { default: fprintf(stderr, "CS:unknown request type %d", job->request.type); break; @@ -506,9 +484,7 @@ void *job_thread(void* arg) return 0; } - -void -io(void) +void io(void) { long n; @@ -516,20 +492,22 @@ io(void) Job *job; /* - * each request is handled via a thread. Somewhat less efficient than the old - * cs but way cleaner. + * each request is handled via a thread. Somewhat less efficient than the + * old + * cs but way cleaner. */ - for(;;){ + for (;;) { n = read9pmsg(mfd[0], mdata, sizeof mdata); - if(n<=0) - error(1, 0, "%s: %r","mount read"); + if (n <= 0) + error(1, 0, "%s: %r", "mount read"); job = newjob(); - if(convM2S(mdata, n, &job->request) != n){ - fprintf(stderr, "convM2S went south: format error %ux %ux %ux %ux %ux", - mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]); - error(1, 0, "format error %ux %ux %ux %ux %ux", - mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]); + if (convM2S(mdata, n, &job->request) != n) { + fprintf(stderr, + "convM2S went south: format error %ux %ux %ux %ux %ux", + mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]); + error(1, 0, "format error %ux %ux %ux %ux %ux", mdata[0], mdata[1], + mdata[2], mdata[3], mdata[4]); freejob(job); continue; } @@ -537,29 +515,27 @@ io(void) * later if we want to. */ if (pthread_create(&job->thread, NULL, &job_thread, job)) { - error(1, 0, "%s: %r","Failed to create job"); + error(1, 0, "%s: %r", "Failed to create job"); continue; } } } -void -rversion(Job *job) +void rversion(Job *job) { - if(job->request.msize > IOHDRSZ + Maxfdata) + if (job->request.msize > IOHDRSZ + Maxfdata) job->reply.msize = IOHDRSZ + Maxfdata; else job->reply.msize = job->request.msize; - if(strncmp(job->request.version, "9P2000", 6) != 0) + if (strncmp(job->request.version, "9P2000", 6) != 0) sendmsg(job, "unknown 9P version"); - else{ + else { job->reply.version = "9P2000"; sendmsg(job, 0); } } -void -rauth(Job *job) +void rauth(Job *job) { sendmsg(job, "cs: authentication not required"); } @@ -567,17 +543,15 @@ rauth(Job *job) /* * don't flush till all the threads are done */ -void -rflush(Job *job) +void rflush(Job *job) { flushjob(job->request.oldtag); sendmsg(job, 0); } -void -rattach(Job *job, Mfile *mf) +void rattach(Job *job, Mfile *mf) { - if(mf->busy == 0){ + if (mf->busy == 0) { mf->busy = 1; mf->user = estrdup(job->request.uname); } @@ -588,9 +562,7 @@ rattach(Job *job, Mfile *mf) sendmsg(job, 0); } - -char* -rwalk(Job *job, Mfile *mf) +char *rwalk(Job *job, Mfile *mf) { char *err; char **elems; @@ -605,10 +577,10 @@ rwalk(Job *job, Mfile *mf) nelems = job->request.nwname; job->reply.nwqid = 0; - if(job->request.newfid != job->request.fid){ + if (job->request.newfid != job->request.fid) { /* clone fid */ nmf = newfid(job->request.newfid); - if(nmf->busy){ + if (nmf->busy) { nmf = NULL; err = "clone to used channel"; goto send; @@ -622,22 +594,22 @@ rwalk(Job *job, Mfile *mf) /* else nmf will be nil */ qid = mf->qid; - if(nelems > 0){ + if (nelems > 0) { /* walk fid */ - for(i=0; i<nelems && i<MAXWELEM; i++){ - if((qid.type & QTDIR) == 0){ + for (i = 0; i < nelems && i < MAXWELEM; i++) { + if ((qid.type & QTDIR) == 0) { err = "not a directory"; break; } - if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){ + if (strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0) { qid.type = QTDIR; qid.path = Qdir; - Found: + Found: job->reply.wqid[i] = qid; job->reply.nwqid++; continue; } - if(strcmp(elems[i], "cs") == 0){ + if (strcmp(elems[i], "cs") == 0) { qid.type = QTFILE; qid.path = Qcs; goto Found; @@ -647,30 +619,29 @@ rwalk(Job *job, Mfile *mf) } } - send: - if(nmf != NULL && (err!=NULL || job->reply.nwqid<nelems)){ +send: + if (nmf != NULL && (err != NULL || job->reply.nwqid < nelems)) { cleanmf(nmf); free(nmf->user); nmf->user = 0; nmf->busy = 0; nmf->fid = 0; } - if(err == NULL) + if (err == NULL) mf->qid = qid; sendmsg(job, err); return err; } -void -ropen(Job *job, Mfile *mf) +void ropen(Job *job, Mfile *mf) { int mode; char *err; err = 0; mode = job->request.mode; - if(mf->qid.type & QTDIR){ - if(mode) + if (mf->qid.type & QTDIR) { + if (mode) err = "permission denied"; } job->reply.qid = mf->qid; @@ -678,14 +649,12 @@ ropen(Job *job, Mfile *mf) sendmsg(job, err); } -void -rcreate(Job *job, Mfile *mf) +void rcreate(Job *job, Mfile *mf) { sendmsg(job, "creation permission denied"); } -void -rread(Job *job, Mfile *mf) +void rread(Job *job, Mfile *mf) { int i, n, cnt; long off, toff, clock; @@ -697,9 +666,9 @@ rread(Job *job, Mfile *mf) err = 0; off = job->request.offset; cnt = job->request.count; - if(mf->qid.type & QTDIR){ + if (mf->qid.type & QTDIR) { // clock = time(0); - if(off == 0){ + if (off == 0) { memset(&dir, 0, sizeof dir); dir.name = "cs"; dir.qid.type = QTFILE; @@ -710,26 +679,26 @@ rread(Job *job, Mfile *mf) dir.uid = mf->user; dir.gid = mf->user; dir.muid = mf->user; - dir.atime = clock; /* wrong */ - dir.mtime = clock; /* wrong */ + dir.atime = clock; /* wrong */ + dir.mtime = clock; /* wrong */ n = convD2M(&dir, buf, sizeof buf); } - job->reply.data = (char*)buf; + job->reply.data = (char *)buf; } else { - for(;;){ + for (;;) { /* look for an answer at the right offset */ toff = 0; - for(i = 0; mf->reply[i] && i < mf->nreply; i++){ + for (i = 0; mf->reply[i] && i < mf->nreply; i++) { n = mf->replylen[i]; - if(off < toff + n) + if (off < toff + n) break; toff += n; } - if(i < mf->nreply) - break; /* got something to return */ + if (i < mf->nreply) + break; /* got something to return */ /* try looking up more answers */ - if(lookup(mf) == 0){ + if (lookup(mf) == 0) { /* no more */ n = 0; goto send; @@ -738,7 +707,7 @@ rread(Job *job, Mfile *mf) /* give back a single reply (or part of one) */ job->reply.data = mf->reply[i] + (off - toff); - if(cnt > toff - off + n) + if (cnt > toff - off + n) n = toff - off + n; else n = cnt; @@ -747,28 +716,27 @@ send: job->reply.count = n; sendmsg(job, err); } -void -cleanmf(Mfile *mf) +void cleanmf(Mfile *mf) { int i; - if(mf->net != NULL){ + if (mf->net != NULL) { free(mf->net); mf->net = NULL; } - if(mf->host != NULL){ + if (mf->host != NULL) { free(mf->host); mf->host = NULL; } - if(mf->serv != NULL){ + if (mf->serv != NULL) { free(mf->serv); mf->serv = NULL; } - if(mf->rem != NULL){ + if (mf->rem != NULL) { free(mf->rem); mf->rem = NULL; } - for(i = 0; i < mf->nreply; i++){ + for (i = 0; i < mf->nreply; i++) { free(mf->reply[i]); mf->reply[i] = NULL; mf->replylen[i] = 0; @@ -777,8 +745,7 @@ cleanmf(Mfile *mf) mf->nextnet = netlist; } -void -rwrite(Job *job, Mfile *mf) +void rwrite(Job *job, Mfile *mf) { int cnt, n; char *err; @@ -787,11 +754,11 @@ rwrite(Job *job, Mfile *mf) err = 0; cnt = job->request.count; - if(mf->qid.type & QTDIR){ + if (mf->qid.type & QTDIR) { err = "can't write directory"; goto send; } - if(cnt >= Maxrequest){ + if (cnt >= Maxrequest) { err = "request too long"; goto send; } @@ -799,7 +766,7 @@ rwrite(Job *job, Mfile *mf) /* * toggle debugging */ - if(strncmp(job->request.data, "debug", 5)==0){ + if (strncmp(job->request.data, "debug", 5) == 0) { debug ^= 1; fprintf(stderr, "CS:debug %d", debug); goto send; @@ -808,7 +775,7 @@ rwrite(Job *job, Mfile *mf) /* * toggle ipv6 lookups */ - if(strncmp(job->request.data, "ipv6", 4)==0){ + if (strncmp(job->request.data, "ipv6", 4) == 0) { ipv6lookups ^= 1; fprintf(stderr, "CS:ipv6lookups %d", ipv6lookups); goto send; @@ -817,19 +784,19 @@ rwrite(Job *job, Mfile *mf) /* * toggle debugging */ - if(strncmp(job->request.data, "paranoia", 8)==0){ + if (strncmp(job->request.data, "paranoia", 8) == 0) { paranoia ^= 1; - fprintf(stderr, "CS:paranoia %d", paranoia); + fprintf(stderr, "CS:paranoia %d", paranoia); goto send; } /* * add networks to the default list */ - if(strncmp(job->request.data, "add ", 4)==0){ - if(job->request.data[cnt-1] == '\n') - job->request.data[cnt-1] = 0; - netadd(job->request.data+4); + if (strncmp(job->request.data, "add ", 4) == 0) { + if (job->request.data[cnt - 1] == '\n') + job->request.data[cnt - 1] = 0; + netadd(job->request.data + 4); readipinterfaces(); goto send; } @@ -837,8 +804,8 @@ rwrite(Job *job, Mfile *mf) /* * refresh all state */ - if(strncmp(job->request.data, "refresh", 7)==0){ - netinit(0/*1*/); + if (strncmp(job->request.data, "refresh", 7) == 0) { + netinit(0 /*1*/); goto send; } @@ -848,31 +815,31 @@ rwrite(Job *job, Mfile *mf) /* * look for a general query */ - if(*job->request.data == '!'){ - err = genquery(mf, job->request.data+1); + if (*job->request.data == '!') { + err = genquery(mf, job->request.data + 1); goto send; } - if(debug) - fprintf(stderr, "CS:write %s", job->request.data); - if(paranoia) - fprintf(stderr, "CS:write %s by %s", job->request.data, mf->user); + if (debug) + fprintf(stderr, "CS:write %s", job->request.data); + if (paranoia) + fprintf(stderr, "CS:write %s by %s", job->request.data, mf->user); /* * break up name */ n = getfields(job->request.data, field, 4, 1, "!"); - switch(n){ + switch (n) { case 1: mf->net = strdup("net"); mf->host = strdup(field[0]); break; case 4: mf->rem = strdup(field[3]); - /* fall through */ + /* fall through */ case 3: mf->serv = strdup(field[2]); - /* fall through */ + /* fall through */ case 2: mf->host = strdup(field[1]); mf->net = strdup(field[0]); @@ -881,7 +848,7 @@ rwrite(Job *job, Mfile *mf) /* * do the first net worth of lookup */ - if(lookup(mf) == 0){ + if (lookup(mf) == 0) { snprintf(curerr, sizeof curerr, "%r"); err = curerr; } @@ -890,8 +857,7 @@ send: sendmsg(job, err); } -void -rclunk(Job *job, Mfile *mf) +void rclunk(Job *job, Mfile *mf) { cleanmf(mf); free(mf->user); @@ -901,22 +867,20 @@ rclunk(Job *job, Mfile *mf) sendmsg(job, 0); } -void -rremove(Job *job, Mfile *mf) +void rremove(Job *job, Mfile *mf) { sendmsg(job, "remove permission denied"); } -void -rstat(Job *job, Mfile *mf) +void rstat(Job *job, Mfile *mf) { struct dir dir; - uint8_t buf[IOHDRSZ+Maxfdata]; + uint8_t buf[IOHDRSZ + Maxfdata]; memset(&dir, 0, sizeof dir); - if(mf->qid.type & QTDIR){ + if (mf->qid.type & QTDIR) { dir.name = "."; - dir.mode = DMDIR|0555; + dir.mode = DMDIR | 0555; } else { dir.name = "cs"; dir.mode = 0666; @@ -926,70 +890,59 @@ rstat(Job *job, Mfile *mf) dir.uid = mf->user; dir.gid = mf->user; dir.muid = mf->user; - //dir.atime = dir.mtime = time(0); + // dir.atime = dir.mtime = time(0); job->reply.nstat = convD2M(&dir, buf, sizeof buf); job->reply.stat = buf; sendmsg(job, 0); } -void -rwstat(Job *job, Mfile *mf) +void rwstat(Job *job, Mfile *mf) { sendmsg(job, "wstat permission denied"); } -void -sendmsg(Job *job, char *err) +void sendmsg(Job *job, char *err) { int n; uint8_t mdata[IOHDRSZ + Maxfdata]; char ename[ERRMAX]; - if(err){ + if (err) { job->reply.type = Rerror; snprintf(ename, sizeof(ename), "cs: %s", err); job->reply.ename = ename; - }else{ - job->reply.type = job->request.type+1; + } else { + job->reply.type = job->request.type + 1; } job->reply.tag = job->request.tag; n = convS2M(&job->reply, mdata, sizeof mdata); - if(n == 1){ - fprintf(stderr, "CS:sendmsg convS2M of %F returns 0", &job->reply); + if (n == 1) { + fprintf(stderr, "CS:sendmsg convS2M of %F returns 0", &job->reply); abort(); } spinlock_lock(&joblock); - if(job->flushed == 0) - if(write(mfd[1], mdata, n)!=n) - error(1, 0, "%s: %r","mount write"); + if (job->flushed == 0) + if (write(mfd[1], mdata, n) != n) + error(1, 0, "%s: %r", "mount write"); spinlock_unlock(&joblock); - if(debug) - fprintf(stderr, "CS:%F %d", &job->reply, n); + if (debug) + fprintf(stderr, "CS:%F %d", &job->reply, n); } -static int -isvalidip(uint8_t *ip) +static int isvalidip(uint8_t *ip) { return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0; } -static uint8_t loopbacknet[IPaddrlen] = { - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0xff, 0xff, - 127, 0, 0, 0 -}; -static uint8_t loopbackmask[IPaddrlen] = { - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0, 0, 0 -}; +static uint8_t loopbacknet[IPaddrlen] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0xff, 0xff, 127, 0, 0, 0}; +static uint8_t loopbackmask[IPaddrlen] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0, 0, 0}; -void -readipinterfaces(void) +void readipinterfaces(void) { - if(myipaddr(ipa, mntpt) != 0) + if (myipaddr(ipa, mntpt) != 0) ipmove(ipa, IPnoaddr); snprintf(ipaddr, sizeof(ipaddr), "%I", ipa); if (debug) @@ -999,8 +952,7 @@ readipinterfaces(void) /* * get the system name */ -void -ipid(void) +void ipid(void) { uint8_t addr[6]; struct ndbtuple *t, *tt; @@ -1010,7 +962,7 @@ ipid(void) char buf[Maxpath]; /* use environment, ether addr, or ipaddr to get system name */ - if(mysysname == 0){ + if (mysysname == 0) { /* * environment has priority. * @@ -1019,9 +971,9 @@ ipid(void) * */ p = getenv("sysname"); - if(p && *p){ + if (p && *p) { attr = ipattr(p); - if(strcmp(attr, "ip") != 0) + if (strcmp(attr, "ip") != 0) mysysname = strdup(p); } @@ -1030,10 +982,10 @@ ipid(void) * figured out from DHCP. use that name if * there is one. */ - if(mysysname == 0 && netdb != NULL){ + if (mysysname == 0 && netdb != NULL) { ndbreopen(netdb); - for(tt = t = ndbparse(netdb); t != NULL; t = t->entry){ - if(strcmp(t->attr, "sys") == 0){ + for (tt = t = ndbparse(netdb); t != NULL; t = t->entry) { + if (strcmp(t->attr, "sys") == 0) { mysysname = strdup(t->val); break; } @@ -1041,24 +993,25 @@ ipid(void) ndbfree(tt); } - /* next network database, ip address, and ether address to find a name */ - if(mysysname == 0){ + /* next network database, ip address, and ether address to find a name + */ + if (mysysname == 0) { t = NULL; - if(isvalidip(ipa)) + if (isvalidip(ipa)) free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t)); - if(t == NULL){ - for(f = 0; f < 3; f++){ + if (t == NULL) { + for (f = 0; f < 3; f++) { snprintf(buf, sizeof buf, "%s/ether%d", mntpt, f); - if(myetheraddr(addr, buf) >= 0){ + if (myetheraddr(addr, buf) >= 0) { snprintf(eaddr, sizeof(eaddr), "%E", addr); free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t)); - if(t != NULL) + if (t != NULL) break; } } } - for(tt = t; tt != NULL; tt = tt->entry){ - if(strcmp(tt->attr, "sys") == 0){ + for (tt = t; tt != NULL; tt = tt->entry) { + if (strcmp(tt->attr, "sys") == 0) { mysysname = strdup(tt->val); break; } @@ -1067,14 +1020,13 @@ ipid(void) } /* nothing else worked, use the ip address */ - if(mysysname == 0 && isvalidip(ipa)) + if (mysysname == 0 && isvalidip(ipa)) mysysname = strdup(ipaddr); - /* set /dev/sysname if we now know it */ - if(mysysname){ + if (mysysname) { f = open("/dev/sysname", O_RDWR); - if(f >= 0){ + if (f >= 0) { write(f, mysysname, strlen(mysysname)); close(f); } @@ -1087,26 +1039,25 @@ ipid(void) * /net/^*^/clone. * For now, never background. */ -void -netinit(int background) +void netinit(int background) { char clone[Maxpath]; Network *np; static int working; /* add the mounted networks to the default list */ - for(np = network; np->net; np++){ + for (np = network; np->net; np++) { int fuckup; - if(np->considered) + if (np->considered) continue; snprintf(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net); fuckup = open(clone, O_RDONLY); if (fuckup < 0) continue; close(fuckup); - //if(access(clone, R_OK)) - //continue; - if(netlist) + // if(access(clone, R_OK)) + // continue; + if (netlist) last->next = np; else netlist = np; @@ -1121,30 +1072,28 @@ netinit(int background) /* set the system name if we need to, these days ip is all we have */ ipid(); - if(debug) + if (debug) fprintf(stderr, logfile, "CS:mysysname %s eaddr %s ipaddr %s ipa %I\n", - mysysname?mysysname:"???", eaddr, ipaddr, ipa); - + mysysname ? mysysname : "???", eaddr, ipaddr, ipa); } /* * add networks to the standard list */ -void -netadd(char *p) +void netadd(char *p) { Network *np; char *field[12]; int i, n; n = getfields(p, field, 12, 1, " "); - for(i = 0; i < n; i++){ - for(np = network; np->net; np++){ - if(strcmp(field[i], np->net) != 0) + for (i = 0; i < n; i++) { + for (np = network; np->net; np++) { + if (strcmp(field[i], np->net) != 0) continue; - if(np->considered) + if (np->considered) break; - if(netlist) + if (netlist) last->next = np; else netlist = np; @@ -1155,11 +1104,10 @@ netadd(char *p) } } -int -lookforproto(struct ndbtuple *t, char *proto) +int lookforproto(struct ndbtuple *t, char *proto) { - for(; t != NULL; t = t->entry) - if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0) + for (; t != NULL; t = t->entry) + if (strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0) return 1; return 0; } @@ -1168,8 +1116,7 @@ lookforproto(struct ndbtuple *t, char *proto) * lookup a request. the network "net" means we should pick the * best network to get there. */ -int -lookup(Mfile *mf) +int lookup(Mfile *mf) { Network *np; char *cp; @@ -1179,33 +1126,33 @@ lookup(Mfile *mf) int hack; /* open up the standard db files */ - if(db == 0) + if (db == 0) ndbinit(); - if(db == 0) - error(1, 0, "%s: %r","can't open mf->network database\n"); + if (db == 0) + error(1, 0, "%s: %r", "can't open mf->network database\n"); rv = 0; - if(mf->net == NULL) - return 0; /* must have been a genquery */ + if (mf->net == NULL) + return 0; /* must have been a genquery */ - if(strcmp(mf->net, "net") == 0){ + if (strcmp(mf->net, "net") == 0) { /* * go through set of default nets */ - for(np = mf->nextnet; np; np = np->next){ + for (np = mf->nextnet; np; np = np->next) { nt = (*np->lookup)(np, mf->host, mf->serv, 1); - if(nt == NULL) + if (nt == NULL) continue; hack = np->fasttimeouthack && !lookforproto(nt, np->net); - for(t = nt; mf->nreply < Nreply && t; t = t->entry){ + for (t = nt; mf->nreply < Nreply && t; t = t->entry) { cp = (*np->trans)(t, np, mf->serv, mf->rem, hack); - if(cp){ + if (cp) { /* avoid duplicates */ - for(i = 0; i < mf->nreply; i++) - if(strcmp(mf->reply[i], cp) == 0) + for (i = 0; i < mf->nreply; i++) + if (strcmp(mf->reply[i], cp) == 0) break; - if(i == mf->nreply){ + if (i == mf->nreply) { /* save the reply */ mf->replylen[mf->nreply] = strlen(cp); mf->reply[mf->nreply++] = cp; @@ -1224,26 +1171,26 @@ lookup(Mfile *mf) /* * if not /net, we only get one lookup */ - if(mf->nreply != 0) + if (mf->nreply != 0) return 0; /* * look for a specific network */ - for(np = netlist; np && np->net != NULL; np++){ - if(np->fasttimeouthack) + for (np = netlist; np && np->net != NULL; np++) { + if (np->fasttimeouthack) continue; - if(strcmp(np->net, mf->net) == 0) + if (strcmp(np->net, mf->net) == 0) break; } - if(np && np->net != NULL){ + if (np && np->net != NULL) { /* * known network */ nt = (*np->lookup)(np, mf->host, mf->serv, 1); - for(t = nt; mf->nreply < Nreply && t; t = t->entry){ + for (t = nt; mf->nreply < Nreply && t; t = t->entry) { cp = (*np->trans)(t, np, mf->serv, mf->rem, 0); - if(cp){ + if (cp) { mf->replylen[mf->nreply] = strlen(cp); mf->reply[mf->nreply++] = cp; rv++; @@ -1255,12 +1202,12 @@ lookup(Mfile *mf) /* * not a known network, don't translate host or service */ - if(mf->serv) - snprintf(reply, sizeof(reply), "%s/%s/clone %s!%s", - mntpt, mf->net, mf->host, mf->serv); + if (mf->serv) + snprintf(reply, sizeof(reply), "%s/%s/clone %s!%s", mntpt, mf->net, + mf->host, mf->serv); else - snprintf(reply, sizeof(reply), "%s/%s/clone %s", - mntpt, mf->net, mf->host); + snprintf(reply, sizeof(reply), "%s/%s/clone %s", mntpt, mf->net, + mf->host); mf->reply[0] = strdup(reply); mf->replylen[0] = strlen(reply); mf->nreply = 1; @@ -1274,8 +1221,7 @@ lookup(Mfile *mf) * * the service '*' needs no translation. */ -char* -ipserv(Network *np, char *name, char *buf, int blen) +char *ipserv(Network *np, char *name, char *buf, int blen) { char *p; int alpha = 0; @@ -1285,40 +1231,39 @@ ipserv(Network *np, char *name, char *buf, int blen) struct ndbs s; /* '*' means any service */ - if(strcmp(name, "*")==0){ + if (strcmp(name, "*") == 0) { strcpy(buf, name); return buf; } /* see if it's numeric or symbolic */ port[0] = 0; - for(p = name; *p; p++){ - if(isdigit(*p)) - {} - else if(isalpha(*p) || *p == '-' || *p == '$') + for (p = name; *p; p++) { + if (isdigit(*p)) { + } else if (isalpha(*p) || *p == '-' || *p == '$') alpha = 1; else return 0; } t = NULL; p = NULL; - if(alpha){ + if (alpha) { p = ndbgetvalue(db, &s, np->net, name, "port", &t); - if(p == NULL) + if (p == NULL) return 0; } else { /* look up only for tcp ports < 1024 to get the restricted * attribute */ - if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0) + if (atoi(name) < 1024 && strcmp(np->net, "tcp") == 0) p = ndbgetvalue(db, &s, "port", name, "port", &t); - if(p == NULL) + if (p == NULL) p = strdup(name); } - if(t){ - for(nt = t; nt; nt = nt->entry) - if(strcmp(nt->attr, "restricted") == 0) + if (t) { + for (nt = t; nt; nt = nt->entry) + if (strcmp(nt->attr, "restricted") == 0) restr = 1; ndbfree(t); } @@ -1330,8 +1275,7 @@ ipserv(Network *np, char *name, char *buf, int blen) /* * lookup an ip attribute */ -int -ipattrlookup(struct ndb *db, char *ipa, char *attr, char *val, int vlen) +int ipattrlookup(struct ndb *db, char *ipa, char *attr, char *val, int vlen) { struct ndbtuple *t, *nt; @@ -1339,10 +1283,10 @@ ipattrlookup(struct ndb *db, char *ipa, char *attr, char *val, int vlen) alist[0] = attr; t = ndbipinfo(db, "ip", ipa, alist, 1); - if(t == NULL) + if (t == NULL) return 0; - for(nt = t; nt != NULL; nt = nt->entry){ - if(strcmp(nt->attr, attr) == 0){ + for (nt = t; nt != NULL; nt = nt->entry) { + if (strcmp(nt->attr, attr) == 0) { nstrcpy(val, nt->val, vlen); ndbfree(t); return 1; @@ -1357,8 +1301,7 @@ ipattrlookup(struct ndb *db, char *ipa, char *attr, char *val, int vlen) /* * lookup (and translate) an ip destination */ -struct ndbtuple* -iplookup(Network *np, char *host, char *serv, int nolookup) +struct ndbtuple *iplookup(Network *np, char *host, char *serv, int nolookup) { char *attr, *dnsname; struct ndbtuple *t, *nt; @@ -1376,43 +1319,43 @@ iplookup(Network *np, char *host, char *serv, int nolookup) * and costs the least */ werrstr("can't translate address"); - if(serv==0 || ipserv(np, serv, ts, sizeof ts) == 0){ + if (serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0) { werrstr("can't translate service"); return 0; } /* for dial strings with no host */ - if(strcmp(host, "*") == 0) + if (strcmp(host, "*") == 0) return ndbnew("ip", "*"); /* * hack till we go v6 :: = 0.0.0.0 */ - if(strcmp("::", host) == 0) + if (strcmp("::", host) == 0) return ndbnew("ip", "*"); /* * '$' means the rest of the name is an attribute that we * need to search for */ - if(*host == '$'){ - if(ipattrlookup(db, ipaddr, host+1, dollar, sizeof dollar)) + if (*host == '$') { + if (ipattrlookup(db, ipaddr, host + 1, dollar, sizeof dollar)) host = dollar; } /* * turn '[ip address]' into just 'ip address' */ - if(*host == '[' && host[strlen(host)-1] == ']'){ + if (*host == '[' && host[strlen(host) - 1] == ']') { host++; - host[strlen(host)-1] = 0; + host[strlen(host) - 1] = 0; } /* * just accept addresses */ attr = ipattr(host); - if(strcmp(attr, "ip") == 0) + if (strcmp(attr, "ip") == 0) return ndbnew("ip", host); /* @@ -1421,20 +1364,20 @@ iplookup(Network *np, char *host, char *serv, int nolookup) */ t = 0; werrstr("can't translate address"); - if(strcmp(attr, "dom") == 0) + if (strcmp(attr, "dom") == 0) t = dnsiplookup(host, &s); - if(t == 0) + if (t == 0) free(ndbgetvalue(db, &s, attr, host, "ip", &t)); - if(t == 0){ + if (t == 0) { dnsname = ndbgetvalue(db, &s, attr, host, "dom", NULL); - if(dnsname){ + if (dnsname) { t = dnsiplookup(dnsname, &s); free(dnsname); } } - if(t == 0) + if (t == 0) t = dnsiplookup(host, &s); - if(t == 0) + if (t == 0) return 0; /* @@ -1447,15 +1390,15 @@ iplookup(Network *np, char *host, char *serv, int nolookup) * reorder according to our interfaces */ spinlock_lock(&ipifclock); - for(ifc = ipifcs; ifc != NULL; ifc = ifc->next){ - for(lifc = ifc->lifc; lifc != NULL; lifc = lifc->next){ + for (ifc = ipifcs; ifc != NULL; ifc = ifc->next) { + for (lifc = ifc->lifc; lifc != NULL; lifc = lifc->next) { maskip(lifc->ip, lifc->mask, net); - for(nt = t; nt; nt = nt->entry){ - if(strcmp(nt->attr, "ip") != 0) + for (nt = t; nt; nt = nt->entry) { + if (strcmp(nt->attr, "ip") != 0) continue; parseip(ip, nt->val); maskip(ip, lifc->mask, tnet); - if(memcmp(net, tnet, IPaddrlen) == 0){ + if (memcmp(net, tnet, IPaddrlen) == 0) { t = reorder(t, nt); spinlock_unlock(&ipifclock); return t; @@ -1471,31 +1414,30 @@ iplookup(Network *np, char *host, char *serv, int nolookup) /* * translate an ip address */ -char* -iptrans(struct ndbtuple *t, Network *np, char *serv, char *rem, int hack) +char *iptrans(struct ndbtuple *t, Network *np, char *serv, char *rem, int hack) { char ts[Maxservice]; char reply[Maxreply]; char x[Maxservice]; - if(strcmp(t->attr, "ip") != 0) + if (strcmp(t->attr, "ip") != 0) return 0; - if(serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0){ + if (serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0) { werrstr("can't translate service"); return 0; } - if(rem != NULL) + if (rem != NULL) snprintf(x, sizeof(x), "!%s", rem); else *x = 0; - if(*t->val == '*') - snprintf(reply, sizeof(reply), "%s/%s/clone %s%s", - mntpt, np->net, ts, x); + if (*t->val == '*') + snprintf(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net, ts, + x); else - snprintf(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s", - mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": ""); + snprintf(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s", mntpt, np->net, + t->val, ts, x, hack ? "!fasttimeout" : ""); return strdup(reply); } @@ -1503,15 +1445,14 @@ iptrans(struct ndbtuple *t, Network *np, char *serv, char *rem, int hack) /* * lookup a telephone number */ -struct ndbtuple* -telcolookup(Network *np, char *host, char *serv, int nolookup) +struct ndbtuple *telcolookup(Network *np, char *host, char *serv, int nolookup) { struct ndbtuple *t; struct ndbs s; werrstr("can't translate address"); free(ndbgetvalue(db, &s, "sys", host, "telco", &t)); - if(t == 0) + if (t == 0) return ndbnew("telco", host); return reorder(t, s.t); @@ -1520,62 +1461,60 @@ telcolookup(Network *np, char *host, char *serv, int nolookup) /* * translate a telephone address */ -char* -telcotrans(struct ndbtuple *t, Network *np, char *serv, char *rem, int unused) +char *telcotrans(struct ndbtuple *t, Network *np, char *serv, char *rem, + int unused) { char reply[Maxreply]; char x[Maxservice]; - if(strcmp(t->attr, "telco") != 0) + if (strcmp(t->attr, "telco") != 0) return 0; - if(rem != NULL) + if (rem != NULL) snprintf(x, sizeof(x), "!%s", rem); else *x = 0; - if(serv) + if (serv) snprintf(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net, - t->val, serv, x); + t->val, serv, x); else snprintf(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net, - t->val, x); + t->val, x); return strdup(reply); } /* * reorder the tuple to put x's line first in the entry */ -struct ndbtuple* -reorder(struct ndbtuple *t, struct ndbtuple *x) +struct ndbtuple *reorder(struct ndbtuple *t, struct ndbtuple *x) { struct ndbtuple *nt; struct ndbtuple *line; /* find start of this entry's line */ - for(line = x; line->entry == line->line; line = line->line) + for (line = x; line->entry == line->line; line = line->line) ; line = line->line; - if(line == t) - return t; /* already the first line */ + if (line == t) + return t; /* already the first line */ /* remove this line and everything after it from the entry */ - for(nt = t; nt->entry != line; nt = nt->entry) + for (nt = t; nt->entry != line; nt = nt->entry) ; nt->entry = 0; /* make that the start of the entry */ - for(nt = line; nt->entry; nt = nt->entry) + for (nt = line; nt->entry; nt = nt->entry) ; nt->entry = t; return line; } -static struct ndbtuple* -dnsip6lookup(char *mntpt, char *buf, struct ndbtuple *t) +static struct ndbtuple *dnsip6lookup(char *mntpt, char *buf, struct ndbtuple *t) { struct ndbtuple *t6, *tt; - t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */ + t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */ if (t6 == NULL) return t; @@ -1597,8 +1536,7 @@ dnsip6lookup(char *mntpt, char *buf, struct ndbtuple *t) /* * call the dns process and have it try to translate a name */ -struct ndbtuple* -dnsiplookup(char *host, struct ndbs *s) +struct ndbtuple *dnsiplookup(char *host, struct ndbs *s) { char buf[Maxreply]; struct ndbtuple *t; @@ -1608,7 +1546,7 @@ dnsiplookup(char *host, struct ndbs *s) /* save the name */ snprintf(buf, sizeof(buf), "%s", host); - if(strcmp(ipattr(buf), "ip") == 0) + if (strcmp(ipattr(buf), "ip") == 0) t = dnsquery(mntpt, buf, "ptr"); else { t = dnsquery(mntpt, buf, "ip"); @@ -1618,11 +1556,11 @@ dnsiplookup(char *host, struct ndbs *s) } s->t = t; - if(t == NULL){ + if (t == NULL) { snprintf(buf, sizeof buf, "%r"); - if(strstr(buf, "exist")) + if (strstr(buf, "exist")) werrstr("can't translate address: %s", buf); - else if(strstr(buf, "dns failure")) + else if (strstr(buf, "dns failure")) werrstr("temporary problem: %s", buf); } @@ -1630,22 +1568,20 @@ dnsiplookup(char *host, struct ndbs *s) return t; } -int -qmatch(struct ndbtuple *t, char **attr, char **val, int n) +int qmatch(struct ndbtuple *t, char **attr, char **val, int n) { int i, found; struct ndbtuple *nt; - for(i = 1; i < n; i++){ + for (i = 1; i < n; i++) { found = 0; - for(nt = t; nt; nt = nt->entry) - if(strcmp(attr[i], nt->attr) == 0) - if(strcmp(val[i], "*") == 0 - || strcmp(val[i], nt->val) == 0){ + for (nt = t; nt; nt = nt->entry) + if (strcmp(attr[i], nt->attr) == 0) + if (strcmp(val[i], "*") == 0 || strcmp(val[i], nt->val) == 0) { found = 1; break; } - if(found == 0) + if (found == 0) break; } return i == n; @@ -1654,8 +1590,7 @@ qmatch(struct ndbtuple *t, char **attr, char **val, int n) /* this is awful but I don't want to bring in libstring just for this. * you want real strings don't use C */ -void -qreply(Mfile *mf, struct ndbtuple *t) +void qreply(Mfile *mf, struct ndbtuple *t) { struct ndbtuple *nt; char *s, *cur; @@ -1665,7 +1600,7 @@ qreply(Mfile *mf, struct ndbtuple *t) cur = s; len = 4096; - for(nt = t; mf->nreply < Nreply && nt; nt = nt->entry){ + for (nt = t; mf->nreply < Nreply && nt; nt = nt->entry) { amt = snprintf(cur, len, "%s=%s", nt->attr, nt->val); if (amt < 0) @@ -1675,7 +1610,7 @@ qreply(Mfile *mf, struct ndbtuple *t) cur += amt; } - if(nt->line != nt->entry){ + if (nt->line != nt->entry) { mf->replylen[mf->nreply] = strlen(s); mf->reply[mf->nreply++] = strdup(s); cur = s; @@ -1689,14 +1624,12 @@ qreply(Mfile *mf, struct ndbtuple *t) cur += amt; } } - } free(s); } -enum -{ - Maxattr= 32, +enum { + Maxattr = 32, }; /* @@ -1712,8 +1645,7 @@ enum * is like ipinfo and returns the attr{1-n} * associated with the ip address. */ -char* -genquery(Mfile *mf, char *query) +char *genquery(Mfile *mf, char *query) { int i, n; char *p; @@ -1723,26 +1655,26 @@ genquery(Mfile *mf, char *query) struct ndbs s; n = getfields(query, attr, COUNT_OF(attr), 1, " "); - if(n == 0) + if (n == 0) return "bad query"; - if(strcmp(attr[0], "ipinfo") == 0) + if (strcmp(attr[0], "ipinfo") == 0) return ipinfoquery(mf, attr, n); /* parse pairs */ - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) { p = strchr(attr[i], '='); - if(p == 0) + if (p == 0) return "bad query"; *p++ = 0; val[i] = p; } /* give dns a chance */ - if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){ + if ((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]) { t = dnsiplookup(val[0], &s); - if(t){ - if(qmatch(t, attr, val, n)){ + if (t) { + if (qmatch(t, attr, val, n)) { qreply(mf, t); ndbfree(t); return 0; @@ -1755,8 +1687,8 @@ genquery(Mfile *mf, char *query) t = ndbsearch(db, &s, attr[0], val[0]); /* search is the and of all the pairs */ - while(t){ - if(qmatch(t, attr, val, n)){ + while (t) { + if (qmatch(t, attr, val, n)) { qreply(mf, t); ndbfree(t); return 0; @@ -1772,15 +1704,14 @@ genquery(Mfile *mf, char *query) /* * resolve an ip address */ -static struct ndbtuple* -ipresolve(char *attr, char *host) +static struct ndbtuple *ipresolve(char *attr, char *host) { struct ndbtuple *t, *nt, **l; t = iplookup(&network[Ntcp], host, "*", 0); - for(l = &t; *l != NULL; ){ + for (l = &t; *l != NULL;) { nt = *l; - if(strcmp(nt->attr, "ip") != 0){ + if (strcmp(nt->attr, "ip") != 0) { *l = nt->entry; nt->entry = NULL; ndbfree(nt); @@ -1792,8 +1723,7 @@ ipresolve(char *attr, char *host) return t; } -char* -ipinfoquery(Mfile *mf, char **list, int n) +char *ipinfoquery(Mfile *mf, char **list, int n) { int i, nresolve; int resolve[Maxattr]; @@ -1801,23 +1731,24 @@ ipinfoquery(Mfile *mf, char **list, int n) char *attr, *val; /* skip 'ipinfo' */ - list++; n--; + list++; + n--; - if(n < 1) + if (n < 1) return "bad query"; /* get search attribute=value, or assume ip=myipaddr */ attr = *list; - if((val = strchr(attr, '=')) != NULL){ + if ((val = strchr(attr, '=')) != NULL) { *val++ = 0; list++; n--; - }else{ + } else { attr = "ip"; val = ipaddr; } - if(n < 1) + if (n < 1) return "bad query"; /* @@ -1825,40 +1756,40 @@ ipinfoquery(Mfile *mf, char **list, int n) * better at it. */ nresolve = 0; - for(i = 0; i < n; i++) - if(*list[i] == '@'){ /* @attr=val ? */ + for (i = 0; i < n; i++) + if (*list[i] == '@') { /* @attr=val ? */ list[i]++; - resolve[i] = 1; /* we'll resolve it */ + resolve[i] = 1; /* we'll resolve it */ nresolve++; } else resolve[i] = 0; t = ndbipinfo(db, attr, val, list, n); - if(t == NULL) + if (t == NULL) return "no match"; - if(nresolve != 0){ - for(l = &t; *l != NULL;){ + if (nresolve != 0) { + for (l = &t; *l != NULL;) { nt = *l; /* already an address? */ - if(strcmp(ipattr(nt->val), "ip") == 0){ + if (strcmp(ipattr(nt->val), "ip") == 0) { l = &(*l)->entry; continue; } /* user wants it resolved? */ - for(i = 0; i < n; i++) - if(strcmp(list[i], nt->attr) == 0) + for (i = 0; i < n; i++) + if (strcmp(list[i], nt->attr) == 0) break; - if(i >= n || resolve[i] == 0){ + if (i >= n || resolve[i] == 0) { l = &(*l)->entry; continue; } /* resolve address and replace entry */ *l = ipresolve(nt->attr, nt->val); - while(*l != NULL) + while (*l != NULL) l = &(*l)->entry; *l = nt->entry; @@ -1868,8 +1799,8 @@ ipinfoquery(Mfile *mf, char **list, int n) } /* make it all one line */ - for(nt = t; nt != NULL; nt = nt->entry){ - if(nt->entry == NULL) + for (nt = t; nt != NULL; nt = nt->entry) { + if (nt->entry == NULL) nt->line = t; else nt->line = nt->entry; @@ -1880,27 +1811,25 @@ ipinfoquery(Mfile *mf, char **list, int n) return NULL; } -void* -emalloc(int size) +void *emalloc(int size) { void *x; x = calloc(size, 1); - if(x == NULL) + if (x == NULL) abort(); memset(x, 0, size); return x; } -char* -estrdup(char *s) +char *estrdup(char *s) { int size; char *p; - size = strlen(s)+1; + size = strlen(s) + 1; p = calloc(size, 1); - if(p == NULL) + if (p == NULL) abort(); memmove(p, s, size); return p; -- 2.8.0.rc3.226.g39d4020 -- You received this message because you are subscribed to the Google Groups "Akaros" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
