diff options
Diffstat (limited to 'fw/src')
| -rw-r--r-- | fw/src/fw.c | 155 |
1 files changed, 97 insertions, 58 deletions
diff --git a/fw/src/fw.c b/fw/src/fw.c index 1fb88bd..8901c2a 100644 --- a/fw/src/fw.c +++ b/fw/src/fw.c @@ -1126,6 +1126,36 @@ etherout(void *a) } /* + * The first n lines of a Biobuf, copied out, since Brdline's buffer is + * only good until the next one. Returns how many there were. + */ +static int +rdline(Biobuf *b, char **lines, int n) +{ + char *p; + int i; + + for(i = 0; i < n; i++){ + if((p = Brdline(b, '\n')) == nil) + break; + p[Blinelen(b)-1] = '\0'; + lines[i] = estrdup(p); + } + return i; +} + +static void +freelines(char **lines, int n) +{ + int i; + + for(i = 0; i < n; i++){ + free(lines[i]); + lines[i] = nil; + } +} + +/* * The address, mask and gateway the stack is already using. Asking the * administrator to repeat what ipconfig(8) was told is a way of getting * the two out of step, so read them instead: the interface status names @@ -1134,52 +1164,53 @@ etherout(void *a) static int learnaddr(char *net, char *dev, char *addr, int naddr, char *gw, int ngw) { - char path[128], buf[1024], *lines[8], *f[8], *p; - int i, fd, n, nl, nf, found; + char path[128], *lines[2], *f[8], *p; + Biobuf *b; + int i, nf, found; found = 0; for(i = 0; i < 16 && !found; i++){ snprint(path, sizeof path, "%s/ipifc/%d/status", net, i); - if((fd = open(path, OREAD)) < 0) - continue; - n = read(fd, buf, sizeof buf - 1); - close(fd); - if(n <= 0) - continue; - buf[n] = '\0'; - if((p = strstr(buf, dev)) == nil) - continue; - USED(p); - nl = getfields(buf, lines, nelem(lines), 0, "\n"); - if(nl < 2) + if((b = Bopen(path, OREAD)) == nil) continue; - /* the address line: address, then the mask as a prefix */ - nf = tokenize(lines[1], f, nelem(f)); - if(nf < 2) - continue; - snprint(addr, naddr, "%s%s", f[0], f[1]); - found = 1; + /* + * The device line, then the first address line. Read, not + * searched: the whole status text used to be scanned for the + * device name, which an address could satisfy, and it was + * read into a fixed buffer that a few addresses would fill. + */ + if(rdline(b, lines, 2) == 2 && strstr(lines[0], dev) != nil){ + nf = tokenize(lines[1], f, nelem(f)); + if(nf >= 2){ + snprint(addr, naddr, "%s%s", f[0], f[1]); + found = 1; + } + } + freelines(lines, 2); + Bterm(b); } if(!found) return -1; + /* + * The default route names the gateway. A line at a time: a + * routing table is as long as it is, and reading 1024 bytes of it + * meant the default route could be off the end and the gateway + * silently unknown. + */ *gw = '\0'; snprint(path, sizeof path, "%s/iproute", net); - if((fd = open(path, OREAD)) >= 0){ - n = read(fd, buf, sizeof buf - 1); - close(fd); - if(n > 0){ - buf[n] = '\0'; - nl = getfields(buf, lines, nelem(lines), 0, "\n"); - for(i = 0; i < nl; i++){ - nf = tokenize(lines[i], f, nelem(f)); - if(nf >= 3 && strcmp(f[0], "0.0.0.0") == 0 - && strcmp(f[1], "/96") == 0){ - snprint(gw, ngw, "%s", f[2]); - break; - } + if((b = Bopen(path, OREAD)) != nil){ + while((p = Brdline(b, '\n')) != nil){ + p[Blinelen(b)-1] = '\0'; + nf = tokenize(p, f, nelem(f)); + if(nf >= 3 && strcmp(f[0], "0.0.0.0") == 0 + && strcmp(f[1], "/96") == 0){ + snprint(gw, ngw, "%s", f[2]); + break; } } + Bterm(b); } return 0; } @@ -1207,27 +1238,31 @@ learnaddr(char *net, char *dev, char *addr, int naddr, char *gw, int ngw) static void reclaim(char *net, char *addr) { - char path[128], buf[1024], *lines[8], *f[8]; - int i, fd, n, nl, nf; + char path[128], *lines[2], *f[8]; + Biobuf *b; + int i, fd, nf; for(i = 0; i < 16; i++){ snprint(path, sizeof path, "%s/ipifc/%d/status", net, i); - if((fd = open(path, OREAD)) < 0) - continue; - n = read(fd, buf, sizeof buf - 1); - close(fd); - if(n <= 0) + if((b = Bopen(path, OREAD)) == nil) continue; - buf[n] = '\0'; - nl = getfields(buf, lines, nelem(lines), 0, "\n"); - if(nl < 2) + nf = rdline(b, lines, 2); + Bterm(b); + if(nf != 2){ + freelines(lines, 2); continue; + } nf = tokenize(lines[0], f, nelem(f)); - if(nf < 2 || strncmp(f[1], "pkt", 3) != 0) + if(nf < 2 || strncmp(f[1], "pkt", 3) != 0){ + freelines(lines, 2); continue; + } nf = tokenize(lines[1], f, nelem(f)); - if(nf < 1 || strcmp(f[0], addr) != 0) + if(nf < 1 || strcmp(f[0], addr) != 0){ + freelines(lines, 2); continue; + } + freelines(lines, 2); snprint(path, sizeof path, "%s/ipifc/%d/ctl", net, i); if((fd = open(path, OWRITE)) < 0) @@ -1249,26 +1284,30 @@ reclaim(char *net, char *addr) static void takecard(char *net, char *dev) { - char path[128], buf[512], *p; - int i, fd, n, found; + char path[128], *lines[1], *f[8]; + Biobuf *b; + int i, fd, nf, found; found = 0; for(i = 0; i < 16; i++){ snprint(path, sizeof path, "%s/ipifc/%d/status", net, i); - if((fd = open(path, OREAD)) < 0) - continue; - n = read(fd, buf, sizeof buf - 1); - close(fd); - if(n <= 0) + if((b = Bopen(path, OREAD)) == nil) continue; - buf[n] = '\0'; - if((p = strchr(buf, ' ')) == nil) + nf = rdline(b, lines, 1); + Bterm(b); + if(nf != 1) continue; - *p = '\0'; - if(strcmp(buf, "device") != 0 && strstr(buf, dev) == nil) - continue; - if(strstr(p+1, dev) == nil && strstr(buf, dev) == nil) + /* + * "device <name> maxtu ...". The name, not the rest of the + * status: an address that happened to contain the device's + * name used to match it. + */ + nf = tokenize(lines[0], f, nelem(f)); + if(nf < 2 || strstr(f[1], dev) == nil){ + freelines(lines, 1); continue; + } + freelines(lines, 1); snprint(path, sizeof path, "%s/ipifc/%d/ctl", net, i); if((fd = open(path, OWRITE)) < 0) continue; |
