📄 devip.c
字号:
p = setraddrport(c, fields[1]); if(p != 0) error(p); break; case 3: p = setraddrport(c, fields[1]); if(p != 0) error(p); c->lport = atoi(fields[2]); setlport(c); break; } so_connect(c->sfd, c->raddr, c->rport); setladdr(c); c->state = "Established"; return n; } if(strcmp(fields[0], "announce") == 0) { switch(nf){ default: error("bad args to announce"); case 2: setladdrport(c, fields[1]); break; } so_listen(c->sfd); c->state = "Announced"; return n; } if(strcmp(fields[0], "bind") == 0){ switch(nf){ default: error("bad args to bind"); case 2: c->lport = atoi(fields[1]); break; } setlport(c); return n; } error("bad control message"); case Qdata: x = &proto[PROTO(ch->qid)]; c = x->conv[CONV(ch->qid)]; r = so_send(c->sfd, a, n, 0); if(r < 0){ oserrstr(); nexterror(); } return r; } return n;}static Conv*protoclone(Proto *p, char *user, int nfd){ Conv *c, **pp, **ep; c = 0; lock(&p->l); if(waserror()) { unlock(&p->l); nexterror(); } ep = &p->conv[p->maxconv]; for(pp = p->conv; pp < ep; pp++) { c = *pp; if(c == 0) { c = mallocz(sizeof(Conv), 1); if(c == 0) error(Enomem); lock(&c->r.lk); c->r.ref = 1; c->p = p; c->x = pp - p->conv; p->nc++; *pp = c; break; } lock(&c->r.lk); if(c->r.ref == 0) { c->r.ref++; break; } unlock(&c->r.lk); } if(pp >= ep) { unlock(&p->l); poperror(); return 0; } strcpy(c->owner, user); c->perm = 0660; c->state = "Closed"; c->restricted = 0; c->laddr = 0; c->raddr = 0; c->lport = 0; c->rport = 0; c->sfd = nfd; if(nfd == -1) c->sfd = so_socket(p->stype); unlock(&c->r.lk); unlock(&p->l); poperror(); return c;}enum{ Isprefix= 16,};uchar prefixvals[256] ={/*0x00*/ 0 | Isprefix, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x20*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x80*/ 1 | Isprefix, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0x90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xA0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xB0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xC0*/ 2 | Isprefix, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xD0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xE0*/ 3 | Isprefix, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/*0xF0*/ 4 | Isprefix, 0, 0, 0, 0, 0, 0, 0, /*0xF8*/ 5 | Isprefix, 0, 0, 0, /*0xFC*/ 6 | Isprefix, 0,/*0xFE*/ 7 | Isprefix,/*0xFF*/ 8 | Isprefix,};inteipfmt(Fmt *f){ char buf[5*8]; static char *efmt = "%.2lux%.2lux%.2lux%.2lux%.2lux%.2lux"; static char *ifmt = "%d.%d.%d.%d"; uchar *p, ip[16]; ulong ul; switch(f->r) { case 'E': /* Ethernet address */ p = va_arg(f->args, uchar*); snprint(buf, sizeof buf, efmt, p[0], p[1], p[2], p[3], p[4], p[5]); return fmtstrcpy(f, buf); case 'I': ul = va_arg(f->args, ulong); hnputl(ip, ul); snprint(buf, sizeof buf, ifmt, ip[0], ip[1], ip[2], ip[3]); return fmtstrcpy(f, buf); } return fmtstrcpy(f, "(eipfmt)");}voidhnputl(void *p, unsigned long v){ unsigned char *a; a = p; a[0] = v>>24; a[1] = v>>16; a[2] = v>>8; a[3] = v;}voidhnputs(void *p, unsigned short v){ unsigned char *a; a = p; a[0] = v>>8; a[1] = v;}unsigned longnhgetl(void *p){ unsigned char *a; a = p; return (a[0]<<24)|(a[1]<<16)|(a[2]<<8)|(a[3]<<0);}unsigned shortnhgets(void *p){ unsigned char *a; a = p; return (a[0]<<8)|(a[1]<<0);}#define CLASS(p) ((*(unsigned char*)(p))>>6)unsigned longparseip(char *to, char *from){ int i; char *p; p = from; memset(to, 0, 4); for(i = 0; i < 4 && *p; i++){ to[i] = strtoul(p, &p, 10); if(*p != '.' && *p != 0){ memset(to, 0, 4); return 0; } if(*p == '.') p++; } switch(CLASS(to)){ case 0: /* class A - 1 byte net */ case 1: if(i == 3){ to[3] = to[2]; to[2] = to[1]; to[1] = 0; } else if (i == 2){ to[3] = to[1]; to[1] = 0; } break; case 2: /* class B - 2 byte net */ if(i == 3){ to[3] = to[2]; to[2] = 0; } break; } return nhgetl(to);}voidcsclose(Chan *c){ free(c->aux);}longcsread(Chan *c, void *a, long n, vlong offset){ if(c->aux == nil) return 0; return readstr(offset, a, n, c->aux);}static struct{ char *name; uint num;} tab[] = { "cs", 1, "echo", 7, "discard", 9, "systat", 11, "daytime", 13, "netstat", 15, "chargen", 19, "ftp-data", 20, "ftp", 21, "ssh", 22, "telnet", 23, "smtp", 25, "time", 37, "whois", 43, "dns", 53, "domain", 53, "uucp", 64, "gopher", 70, "rje", 77, "finger", 79, "http", 80, "link", 87, "supdup", 95, "hostnames", 101, "iso-tsap", 102, "x400", 103, "x400-snd", 104, "csnet-ns", 105, "pop-2", 109, "pop3", 110, "portmap", 111, "uucp-path", 117, "nntp", 119, "netbios", 139, "imap4", 143, "NeWS", 144, "print-srv", 170, "z39.50", 210, "fsb", 400, "sysmon", 401, "proxy", 402, "proxyd", 404, "https", 443, "cifs", 445, "ssmtp", 465, "rexec", 512, "login", 513, "shell", 514, "printer", 515, "courier", 530, "cscan", 531, "uucp", 540, "snntp", 563, "9fs", 564, "whoami", 565, "guard", 566, "ticket", 567, "dlsftp", 666, "fmclient", 729, "imaps", 993, "pop3s", 995, "ingreslock", 1524, "pptp", 1723, "nfs", 2049, "webster", 2627, "weather", 3000, "secstore", 5356, "Xdisplay", 6000, "styx", 6666, "mpeg", 6667, "rstyx", 6668, "infdb", 6669, "infsigner", 6671, "infcsigner", 6672, "inflogin", 6673, "bandt", 7330, "face", 32000, "dhashgate", 11978, "exportfs", 17007, "rexexec", 17009, "ncpu", 17010, "cpu", 17013, "glenglenda1", 17020, "glenglenda2", 17021, "glenglenda3", 17022, "glenglenda4", 17023, "glenglenda5", 17024, "glenglenda6", 17025, "glenglenda7", 17026, "glenglenda8", 17027, "glenglenda9", 17028, "glenglenda10", 17029, "flyboy", 17032, "dlsftp", 17033, "venti", 17034, "wiki", 17035, "vica", 17036, 0};static intlookupport(char *s){ int i; char buf[10], *p; i = strtol(s, &p, 0); if(*s && *p == 0) return i; i = so_getservbyname(s, "tcp", buf); if(i != -1) return atoi(buf); for(i=0; tab[i].name; i++) if(strcmp(s, tab[i].name) == 0) return tab[i].num; return 0;}static ulonglookuphost(char *s){ char to[4]; ulong ip; memset(to, 0, sizeof to); parseip(to, s); ip = nhgetl(to); if(ip != 0) return ip; if((s = hostlookup(s)) == nil) return 0; parseip(to, s); ip = nhgetl(to); free(s); return ip;}longcswrite(Chan *c, void *a, long n, vlong offset){ char *f[4]; char *s, *ns; ulong ip; int nf, port; s = malloc(n+1); if(s == nil) error(Enomem); if(waserror()){ free(s); nexterror(); } memmove(s, a, n); s[n] = 0; nf = getfields(s, f, nelem(f), 0, "!"); if(nf != 3) error("can't translate"); port = lookupport(f[2]); if(port <= 0) error("no translation for port found"); ip = lookuphost(f[1]); if(ip == 0) error("no translation for host found"); ns = smprint("/net/%s/clone %I!%d", f[0], ip, port); if(ns == nil) error(Enomem); free(c->aux); c->aux = ns; poperror(); free(s); return n;}Dev ipdevtab = { 'I', "ip", devreset, ipinit, devshutdown, ipattach, ipwalk, ipstat, ipopen, devcreate, ipclose, ipread, devbread, ipwrite, devbwrite, devremove, devwstat,};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -