12 typedef struct Endpoints Endpoints;
23 void xfer9p(int, int);
24 Endpoints* getendpoints(char*);
25 void freeendpoints(Endpoints*);
26 char* iptomac(char*, char*);
32 fprint(2, "usage: trampoline [-9] [-a addr] [-m netdir] addr\n");
37 main(int argc, char **argv)
39 char *altaddr, *checkmac, *mac;
52 altaddr = EARGF(usage());
55 checkmac = EARGF(usage());
65 ep = getendpoints(checkmac);
66 mac = iptomac(ep->rsys, ep->net);
68 syslog(0, "trampoline", "badmac %s from %s!%s for %s!%s on %s",
69 mac, ep->rsys, ep->rserv, ep->lsys, ep->lserv, ep->net);
77 fd0 = dial(altaddr, 0, 0, 0);
79 sysfatal("dial %s: %r", altaddr);
82 fd = dial(argv[0], 0, 0, 0);
84 sysfatal("dial %s: %r", argv[0]);
89 fprint(2, "%s: fork: %r\n", argv0);
98 postnote(PNGROUP, getpid(), "die yankee pig dog");
103 xfer(int from, int to)
108 while((n = read(from, buf, sizeof buf)) > 0)
109 if(write(to, buf, n) < 0)
114 xfer9p(int from, int to)
123 sysfatal("xfer: malloc %ud: %r", nbuf);
126 if(readn(from, buf, 4) != 4)
131 buf = realloc(buf, nbuf);
133 sysfatal("xfer: realloc %ud: %r", nbuf);
135 if(readn(from, buf+4, n-4) != n-4)
137 if(write(to, buf, n) != n){
138 sysfatal("oops: %r");
145 getendpoint(char *dir, char *file, char **sysp, char **servp)
153 snprint(buf, sizeof buf, "%s/%s", dir, file);
154 fd = open(buf, OREAD);
156 n = read(fd, buf, sizeof(buf)-1);
159 serv = strchr(buf, '!');
169 serv = strdup("unknown");
171 sys = strdup("unknown");
177 getendpoints(char *dir)
182 ep = malloc(sizeof(*ep));
183 ep->net = strdup(dir);
184 p = strchr(ep->net+1, '/');
190 getendpoint(dir, "local", &ep->lsys, &ep->lserv);
191 getendpoint(dir, "remote", &ep->rsys, &ep->rserv);
196 freeendpoints(Endpoints *ep)
206 iptomac(char *ip, char *net)
213 snprint(file, sizeof(file), "%s/arp", net);
214 b = Bopen(file, OREAD);
217 while((p = Brdline(b, '\n')) != nil){
218 p[Blinelen(b)-1] = 0;
219 if(tokenize(p, f, nelem(f)) < 4)
221 if(strcmp(f[1], "OK") == 0
222 && strcmp(f[2], ip) == 0){
239 free(p = csgetvalue("/net", "ether", mac, "trampok", nil));