void main(int argc, char **argv) { int fd, cfd, n; char buf[4096], data[128], devdir[40], net[32]; setnetmtpt(net, sizeof net, nil); ARGBEGIN{ case 'x': setnetmtpt(net, sizeof net, EARGF(usage())); break; }ARGEND; sprint(data, "%s/udp!*!echo", net); cfd = announce(data, devdir); if(cfd < 0) sysfatal("can't announce %s: %r", data); if(fprint(cfd, "headers") < 0) sysfatal("can't set header mode: %r"); sprint(data, "%s/data", devdir); fd = open(data, ORDWR); if(fd < 0) sysfatal("open %s: %r", data); while ((n = read(fd, buf, sizeof buf)) > 0) write(fd, buf, n); if (n < 0) sysfatal("error reading: %r"); exits(0); }
int main(int argc, char *argv[]) { int justsetname, ch; char ext[Maxpath], servefile[Maxpath]; /* Make us an SCP with a 2LS */ parlib_wants_to_be_mcp = FALSE; register_printf_specifier('F', printf_fcall, printf_fcall_info); argv0 = argv[0]; justsetname = 0; setnetmtpt(mntpt, sizeof(mntpt), NULL); ext[0] = 0; while ((ch = getopt(argc, argv, "4df:nSx:")) != -1) { switch (ch) { case '4': ipv6lookups = 0; break; case 'd': debug = 1; break; case 'f': dbfile = optarg; break; case 'n': justsetname = 1; break; case 'S': server = 1; break; case 'x': setnetmtpt(mntpt, sizeof(mntpt), optarg); setext(ext, sizeof(ext), mntpt); break; default: usage(); break; } } snprintf(servefile, sizeof(servefile), "#srv/cs%s", ext); snprintf(netndb, sizeof(netndb), "%s/ndb", mntpt); syscall(SYS_nunmount, (unsigned long)servefile, strlen(servefile), (unsigned long)mntpt, strlen(mntpt)); remove(servefile); ndbinit(); netinit(0); if (!justsetname) { mountinit(servefile, mntpt); if (server) evnotify(0); io(); } evexit(0); }
void main(int argc, char *argv[]) { char *p; setnetmtpt(net, sizeof(net), nil); ARGBEGIN{ case 'x': p = ARGF(); if(p == nil) usage(); setnetmtpt(net, sizeof(net), p); }ARGEND; fmtinstall('E', eipfmt); fmtinstall('I', eipfmt); fmtinstall('V', eipfmt); dhcpinit(); rfork(RFNOTEG|RFREND); thread(timerthread, 0); thread(stdinthread, 0); qlock(&dhcp.lk); dhcp.starttime = time(0); dhcp.fd = openlisten(net); dhcpsend(Discover); dhcp.state = Sselecting; dhcp.resend = 0; dhcp.timeout = 4; while(dhcp.state != Sbound) dhcprecv(); /* allows other clients on this machine */ close(dhcp.fd); dhcp.fd = -1; print("ip=%I\n", dhcp.client); print("mask=%I\n", dhcp.mask); print("end\n"); /* keep lease alive */ for(;;) { //fprint(2, "got lease for %d\n", dhcp.lease); qunlock(&dhcp.lk); sleep(dhcp.lease*500); /* wait half of lease time */ qlock(&dhcp.lk); //fprint(2, "try renue\n", dhcp.lease); dhcp.starttime = time(0); dhcp.fd = openlisten(net); dhcp.xid = time(0)*getpid(); dhcpsend(Request); dhcp.state = Srenewing; dhcp.resend = 0; dhcp.timeout = 1; while(dhcp.state != Sbound) dhcprecv(); /* allows other clients on this machine */ close(dhcp.fd); dhcp.fd = -1; } }
void main(int argc, char **argv) { int afd, dfd, lcfd, forceSTA = 0; char aserve[128], net[128], adir[40], ldir[40]; char *remote, *serve = "tcp!*!5356", *S = "secstore"; Ndb *db2; setnetmtpt(net, sizeof(net), nil); ARGBEGIN{ case 'R': forceSTA = 1; break; case 's': serve = EARGF(usage()); break; case 'S': S = EARGF(usage()); break; case 'x': setnetmtpt(net, sizeof(net), EARGF(usage())); break; case 'v': verbose++; break; default: usage(); }ARGEND; if(!verbose) switch(rfork(RFNOTEG|RFPROC|RFFDG)) { case -1: sysfatal("fork: %r"); case 0: break; default: exits(0); } snprint(aserve, sizeof aserve, "%s/%s", net, serve); afd = announce(aserve, adir); if(afd < 0) sysfatal("%s: %r", aserve); syslog(0, LOG, "ANNOUNCE %s", aserve); for(;;){ if((lcfd = listen(adir, ldir)) < 0) exits("can't listen"); switch(fork()){ case -1: fprint(2, "secstore forking: %r\n"); close(lcfd); break; case 0: /* * "/lib/ndb/common.radius does not exist" * if db set before fork. */ db = ndbopen("/lib/ndb/auth"); if(db == 0) syslog(0, LOG, "no /lib/ndb/auth"); db2 = ndbopen(0); if(db2 == 0) syslog(0, LOG, "no /lib/ndb/local"); db = ndbcat(db, db2); if((dfd = accept(lcfd, ldir)) < 0) exits("can't accept"); alarm(30*60*1000); /* 30 min */ remote = remoteIP(ldir); syslog(0, LOG, "secstore from %s", remote); free(remote); dologin(dfd, S, forceSTA); exits(nil); default: close(lcfd); break; } } }
void main(int argc, char *argv[]) { uchar buf[8*1024], *p; char addr[128], dir[40], ldir[40], *s; int cmd, fd, cfd, n; NetConnInfo *nc; fmtinstall('I', eipfmt); setnetmtpt(inside, sizeof(inside), 0); setnetmtpt(outside, sizeof(outside), 0); ARGBEGIN { case 'x': setnetmtpt(inside, sizeof(inside), ARGF()); break; case 'o': setnetmtpt(outside, sizeof(outside), ARGF()); break; } ARGEND; /* ver+cmd or ver+nmethod */ if(readn(0, buf, 2) != 2) return; socksver = buf[0]; if(socksver < 4) return; if(socksver > 5) socksver = 5; if(socksver == 4){ /* port+ip4 */ if(readn(0, buf+2, 2+4) != 2+4) return; /* +user\0 */ for(p = buf+2+2+4;; p++){ if(p >= buf+sizeof(buf)) return; if(read(0, p, 1) != 1) return; if(*p == 0) break; } /* socks 4a dom hack */ if((buf[4] | buf[5] | buf[6]) == 0 && buf[7]){ /* +dom\0 */ for(++p;; p++){ if(p >= buf+sizeof(buf)) return; if(read(0, p, 1) != 1) return; if(*p == 0) break; } } } else { /* nmethod */ if((n = buf[1]) > 0) if(readn(0, buf+2, n) != n) return; /* ver+method */ buf[0] = socksver; buf[1] = 0x00; /* no authentication required */ if(write(1, buf, 2) != 2) return; /* ver+cmd+res+atyp */ if(readn(0, buf, 4) != 4) return; switch(buf[3]){ default: return; case 0x01: /* +ipv4 */ if(readn(0, buf+4, 4+2) != 4+2) return; break; case 0x03: /* +len+dom[len] */ if(readn(0, buf+4, 1) != 1) return; if((n = buf[4]) == 0) return; if(readn(0, buf+5, n+2) != n+2) return; break; case 0x04: /* +ipv6 */ if(readn(0, buf+4, 16+2) != 16+2) return; break; } } dir[0] = 0; fd = cfd = -1; cmd = buf[1]; switch(cmd){ case 0x01: /* CONNECT */ snprint(addr, sizeof(addr), "%s/tcp", outside); if((s = addr2str(addr, buf)) == nil) break; alarm(30000); fd = dial(s, 0, dir, &cfd); alarm(0); break; case 0x02: /* BIND */ if(myipaddr(buf, outside) < 0) break; snprint(addr, sizeof(addr), "%s/tcp!%I!0", outside, buf); fd = announce(addr, dir); break; case 0x03: /* UDP */ if(myipaddr(buf, inside) < 0) break; snprint(addr, sizeof(addr), "%s/udp!%I!0", inside, buf); fd = announce(addr, dir); break; } Reply: /* reply */ buf[1] = sockerr(fd < 0); /* status */ if(socksver == 4){ buf[0] = 0x00; /* vc */ if(fd < 0){ memset(buf+2, 0, 2+4); write(1, buf, 2+2+4); return; } } else { buf[0] = socksver; /* ver */ buf[2] = 0x00; /* res */ if(fd < 0){ buf[3] = 0x01; /* atyp */ memset(buf+4, 0, 4+2); write(1, buf, 4+4+2); return; } } if((nc = getnetconninfo(dir, cfd)) == nil) return; if((n = str2addr((cmd & 0x100) ? nc->raddr : nc->laddr, buf)) <= 0) return; if(write(1, buf, n) != n) return; switch(cmd){ default: return; case 0x01: /* CONNECT */ break; case 0x02: /* BIND */ cfd = listen(dir, ldir); close(fd); fd = -1; if(cfd >= 0){ strcpy(dir, ldir); fd = accept(cfd, dir); } cmd |= 0x100; goto Reply; case 0x102: break; case 0x03: /* UDP */ if(udprelay(fd, dir) == 0) while(read(0, buf, sizeof(buf)) > 0) ; goto Hangup; } /* relay data */ switch(rfork(RFMEM|RFPROC|RFFDG|RFNOWAIT)){ case -1: return; case 0: dup(fd, 0); break; default: dup(fd, 1); } while((n = read(0, buf, sizeof(buf))) > 0) if(write(1, buf, n) != n) break; Hangup: if(cfd >= 0) hangup(cfd); postnote(PNGROUP, getpid(), "kill"); }
void main(int argc, char *argv[]) { int dobroadcast, i, n; long diff; char *p; char buf[2*1024]; uint8_t raddr[Pasize]; Bnet *bn, **l; Udphdr *up; Rip *r; Ripmsg *m; Route route; static long btime; setnetmtpt(netdir, sizeof(netdir), nil); dobroadcast = 0; ARGBEGIN{ case 'b': dobroadcast++; break; case 'd': debug++; break; case 'n': readonly++; break; case 'x': p = ARGF(); if(p == nil) usage(); setnetmtpt(netdir, sizeof(netdir), p); break; default: usage(); }ARGEND /* specific broadcast nets */ l = &bnets; while(argc > 0){ bn = (Bnet*)malloc(sizeof(Bnet)); if(bn == 0) fatal(1, "out of mem"); v4parseip(bn->addr, *argv); *l = bn; l = &bn->next; argc--; argv++; dobroadcast++; } /* command returns */ if(!debug) switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT)) { case -1: fatal(1, "fork"); case 0: break; default: exits(0); } fmtinstall('E', eipfmt); fmtinstall('V', eipfmt); snprint(routefile, sizeof(routefile), "%s/iproute", netdir); snprint(buf, sizeof(buf), "%s/iproute", netdir); now = time(0); readifcs(); readroutes(); notify(ding); ripfd = openport(); for(;;) { diff = btime - time(0); if(diff <= 0){ if(dobroadcast) broadcast(); timeoutroutes(); btime = time(0) + 2*60; diff = 2*60; } alarm(diff*1000); n = read(ripfd, buf, sizeof(buf)); alarm(0); if(n <= 0) continue; n = (n - Udphdrsize - 4) / sizeof(Rip); if(n <= 0) continue; up = (Udphdr*)buf; m = (Ripmsg*)(buf+Udphdrsize); if(m->type != Response || m->vers != Version) continue; v6tov4(raddr, up->raddr); /* ignore our own messages */ for(i = 0; i < ialloc.nifc; i++) if(equivip(ialloc.ifc[i].addr, raddr)) continue; now = time(0); for(r = m->rip; r < &m->rip[n]; r++){ memmove(route.gate, raddr, Pasize); memmove(route.mask, getmask(r->addr), Pasize); v4maskip(r->addr, route.mask, route.dest); route.metric = nhgetl(r->metric) + 1; if(route.metric < 1) continue; considerroute(&route); } } /* not reached */ }
void main(int argc, char *argv[]) { int kid, pid; char servefile[Maxpath], ext[Maxpath]; Dir *dir; setnetmtpt(mntpt, sizeof mntpt, nil); ext[0] = 0; ARGBEGIN{ case 'a': maxage = atol(EARGF(usage())); if (maxage <= 0) maxage = Defmaxage; break; case 'd': debug = 1; traceactivity = 1; break; case 'f': dbfile = EARGF(usage()); break; case 'F': cfg.justforw = cfg.resolver = 1; break; case 'n': sendnotifies = 1; break; case 'N': target = atol(EARGF(usage())); if (target < 1000) target = 1000; break; case 'o': cfg.straddle = 1; /* straddle inside & outside networks */ break; case 'r': cfg.resolver = 1; break; case 'R': norecursion = 1; break; case 's': cfg.serve = 1; /* serve network */ cfg.cachedb = 1; break; case 't': testing = 1; break; case 'T': addforwtarg(EARGF(usage())); break; case 'x': setnetmtpt(mntpt, sizeof mntpt, EARGF(usage())); setext(ext, sizeof ext, mntpt); break; case 'z': zonerefreshprogram = EARGF(usage()); break; default: usage(); break; }ARGEND if(argc != 0) usage(); if(testing) mainmem->flags |= POOL_NOREUSE | POOL_ANTAGONISM; mainmem->flags |= POOL_ANTAGONISM; rfork(RFREND|RFNOTEG); cfg.inside = (*mntpt == '\0' || strcmp(mntpt, "/net") == 0); /* start syslog before we fork */ fmtinstall('F', fcallfmt); dninit(); /* this really shouldn't be fatal */ if(myipaddr(ipaddr, mntpt) < 0) sysfatal("can't read my ip address"); dnslog("starting %s%sdns %s%s%son %I's %s", (cfg.straddle? "straddling ": ""), (cfg.cachedb? "caching ": ""), (cfg.serve? "udp server ": ""), (cfg.justforw? "forwarding-only ": ""), (cfg.resolver? "resolver ": ""), ipaddr, mntpt); opendatabase(); now = time(nil); /* open time files before we fork */ nowns = nsec(); snprint(servefile, sizeof servefile, "#s/dns%s", ext); dir = dirstat(servefile); if (dir) sysfatal("%s exists; another dns instance is running", servefile); free(dir); /* don't unmount here; could deadlock */ // while (unmount(servefile, mntpt) >= 0) // ; mountinit(servefile, mntpt); /* forks, parent exits */ srand(now*getpid()); db2cache(1); // dnageallnever(); if (cfg.straddle && !seerootns()) dnslog("straddle server misconfigured; can't resolve root name servers"); /* * fork without sharing heap. * parent waits around for child to die, then forks & restarts. * child may spawn udp server, notify procs, etc.; when it gets too * big or too old, it kills itself and any children. * * /srv/dns remains open and valid, but /net/dns was only mounted in * a child's separate namespace from 9p service, to avoid a deadlock * from serving our own namespace, so we must remount it upon restart, * in a separate process and namespace. */ for (;;) { start = time(nil); /* don't unmount here; could deadlock */ // unmount(servefile, mntpt); kid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG); switch (kid) { case -1: sysfatal("fork failed: %r"); case 0: if(cfg.serve) dnudpserver(mntpt); if(sendnotifies) notifyproc(); io(); /* serve 9p; return implies restart */ _exits("restart"); } sleep(1000); /* wait for 9p service to start */ justremount(servefile, mntpt); while ((pid = waitpid()) != kid && pid != -1) continue; dnslog("restarting"); } }