7 #define NS(x) ((vlong)x)
8 #define US(x) (NS(x) * 1000LL)
9 #define MS(x) (US(x) * 1000LL)
10 #define S(x) (MS(x) * 1000LL)
18 Timer = 0, // Alt channels.
20 Maxto = 24 * 3600, // A full day to reconnect.
24 typedef struct Endpoints Endpoints;
33 uchar nb[4]; // Number of data bytes in this message
34 uchar msg[4]; // Message number
35 uchar acked[4]; // Number of messages acked
38 typedef struct t_Buf {
43 static Channel *unsent;
44 static Channel *unacked;
45 static Channel *empty;
51 static char *dialstring;
52 static int maxto = Maxto;
53 static char *Logname = "aan";
58 { nil, nil, CHANRCV }, // timer
59 { nil, nil, CHANRCV }, // unsent
60 { nil, nil, CHANEND },
63 static void fromnet(void*);
64 static void fromclient(void*);
65 static void reconnect(int);
66 static void synchronize(void);
67 static int sendcommand(ulong, ulong);
68 static void showmsg(int, char *, Buf *);
69 static int writen(int, uchar *, int);
70 static void dmessage(int, char *, ...);
71 static void timerproc(void *);
76 fprint(2, "Usage: %s [-cd] [-m maxto] dialstring|netdir\n", argv0);
81 catch(void *, char *s)
83 if (!strcmp(s, "alarm")) {
84 syslog(0, Logname, "Timed out while waiting for reconnect, exiting...");
99 sysfatal("Cannot allocate memory; pc=%lux", pc);
105 threadmain(int argc, char **argv)
120 maxto = (int)strtol(EARGF(usage()), nil, 0);
133 if ((p = strstr(devdir, "/local")) != nil)
137 dialstring = argv[0];
140 int fd = open("#c/cons", OWRITE|OCEXEC);
144 fmtinstall('F', fcallfmt);
149 * Set up initial connection. use short timeout
150 * of 60 seconds so we wont hang arround for too
151 * long if there is some general connection problem
157 unsent = chancreate(sizeof(Buf *), Nbuf);
158 unacked = chancreate(sizeof(Buf *), Nbuf);
159 empty = chancreate(sizeof(Buf *), Nbuf);
160 timer = chancreate(sizeof(uchar *), 1);
161 if(unsent == nil || unacked == nil || empty == nil || timer == nil)
162 sysfatal("Cannot allocate channels");
164 for (i = 0; i < Nbuf; i++)
165 sendp(empty, emalloc(sizeof(Buf)));
167 if (proccreate(fromnet, nil, Stacksize) < 0)
168 sysfatal("Cannot start fromnet; %r");
170 if (proccreate(fromclient, nil, Stacksize) < 0)
171 sysfatal("Cannot start fromclient; %r");
173 if (proccreate(timerproc, timer, Stacksize) < 0)
174 sysfatal("Cannot start timerproc; %r");
177 a[Unsent].c = unsent;
180 synctime = nsec() + Synctime;
184 // Wait for the netreader to die.
186 dmessage(1, "main; waiting for netreader to die\n");
190 // the reader died; reestablish the world.
196 if (nsec() >= synctime) {
200 PBIT32(hdr.acked, inmsg);
203 if (writen(netfd, (uchar *)&hdr, Hdrsz) < 0) {
204 dmessage(2, "main; writen failed; %r\n");
208 synctime = nsec() + Synctime;
218 PBIT32(b->hdr.acked, inmsg);
220 if (writen(netfd, (uchar *)&b->hdr, Hdrsz) < 0) {
221 dmessage(2, "main; writen failed; %r\n");
225 n = GBIT32(b->hdr.nb);
226 if (writen(netfd, b->buf, n) < 0) {
227 dmessage(2, "main; writen failed; %r\n");
236 syslog(0, Logname, "exiting...");
248 threadsetname("fromclient");
252 n = read(0, b->buf, Bufsize);
255 dmessage(2, "fromclient; Cannot read 9P message; %r\n");
257 dmessage(2, "fromclient; Client terminated\n");
260 PBIT32(b->hdr.nb, n);
261 PBIT32(b->hdr.msg, outmsg);
262 showmsg(1, "fromclient", b);
271 static int lastacked;
272 int n, m, len, acked;
275 threadsetname("fromnet");
277 b = emalloc(sizeof(Buf));
282 dmessage(1, "fromnet; waiting for connection... (inmsg %d)\n", inmsg);
287 len = readn(netfd, (uchar *)&b->hdr, Hdrsz);
290 dmessage(1, "fromnet; (hdr) network failure; %r\n");
292 dmessage(1, "fromnet; (hdr) network closed\n");
297 n = GBIT32(b->hdr.nb);
298 m = GBIT32(b->hdr.msg);
299 acked = GBIT32(b->hdr.acked);
300 dmessage(2, "fromnet: Got message, size %d, nb %d, msg %d, acked %d, lastacked %d\n",
301 len, n, m, acked, lastacked);
305 dmessage(1, "fromnet; network closed\n");
312 dmessage(1, "fromnet; message too big %d > %d\n", n, Bufsize);
316 len = readn(netfd, b->buf, n);
317 if (len <= 0 || len != n) {
319 dmessage(1, "fromnet; network closed\n");
321 dmessage(1, "fromnet; network failure; %r\n");
328 dmessage(1, "fromnet; skipping message %d, currently at %d\n", m, inmsg);
332 // Process the acked list.
333 while(lastacked != acked) {
337 m = GBIT32(rb->hdr.msg);
338 if (m != lastacked) {
339 dmessage(1, "fromnet; rb %p, msg %d, lastacked %d\n", rb, m, lastacked);
340 sysfatal("fromnet; bug");
342 PBIT32(rb->hdr.msg, -1);
348 showmsg(1, "fromnet", b);
350 if (writen(1, b->buf, len) < 0)
351 sysfatal("fromnet; cannot write to client; %r");
364 syslog(0, Logname, "dialing %s", dialstring);
366 while ((fd = dial(dialstring, nil, ldir, nil)) < 0) {
370 errstr(err, sizeof err);
371 if (strstr(err, "connection refused")) {
372 dmessage(1, "reconnect; server died...\n");
373 threadexitsall("server died...");
375 dmessage(1, "reconnect: dialed %s; %s\n", dialstring, err);
379 syslog(0, Logname, "reconnected to %s", dialstring);
382 syslog(0, Logname, "waiting for connection on %s", devdir);
384 if ((lcfd = listen(devdir, ldir)) < 0)
385 sysfatal("reconnect; cannot listen; %r");
386 if ((fd = accept(lcfd, ldir)) < 0)
387 sysfatal("reconnect; cannot accept; %r");
392 if(nci = getnetconninfo(ldir, fd)){
393 syslog(0, Logname, "connected from %s", nci->rsys);
394 threadsetname(client? "client %s %s" : "server %s %s", ldir, nci->rsys);
395 freenetconninfo(nci);
397 syslog(0, Logname, "connected");
399 // Wakes up the netreader.
410 // Ignore network errors here. If we fail during
411 // synchronization, the next alarm will pick up
414 tmp = chancreate(sizeof(Buf *), Nbuf);
415 while ((b = nbrecvp(unacked)) != nil) {
416 n = GBIT32(b->hdr.nb);
417 writen(netfd, (uchar *)&b->hdr, Hdrsz);
418 writen(netfd, b->buf, n);
426 showmsg(int level, char *s, Buf *b)
431 dmessage(level, "%s; b == nil\n", s);
434 n = GBIT32(b->hdr.nb);
435 dmessage(level, "%s; (len %d) %X %X %X %X %X %X %X %X %X (%p)\n", s, n,
436 b->buf[0], b->buf[1], b->buf[2],
437 b->buf[3], b->buf[4], b->buf[5],
438 b->buf[6], b->buf[7], b->buf[8], b);
442 writen(int fd, uchar *buf, int nb)
452 if ((n = write(fd, buf, nb)) < 0) {
453 dmessage(1, "writen; Write failed; %r\n");
456 dmessage(2, "writen: wrote %d bytes\n", n);
469 threadsetname("timer");
472 sleep((Synctime / MS(1)) >> 1);
473 sendp(timer, "timer");
478 dmessage(int level, char *fmt, ...)
486 vfprint(2, fmt, arg);