2011-03-30 12:46:40 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <auth.h>
|
|
|
|
#include <fcall.h>
|
|
|
|
#include <thread.h>
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
#define NS(x) ((vlong)x)
|
|
|
|
#define US(x) (NS(x) * 1000LL)
|
|
|
|
#define MS(x) (US(x) * 1000LL)
|
|
|
|
#define S(x) (MS(x) * 1000LL)
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
Synctime = S(8),
|
|
|
|
Nbuf = 10,
|
|
|
|
K = 1024,
|
|
|
|
Bufsize = 8 * K,
|
|
|
|
Stacksize = 8 * K,
|
2012-06-27 10:36:15 +00:00
|
|
|
Timer = 0, // Alt channels.
|
2011-03-30 12:46:40 +00:00
|
|
|
Unsent = 1,
|
|
|
|
Maxto = 24 * 3600, // A full day to reconnect.
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct Endpoints Endpoints;
|
|
|
|
struct Endpoints {
|
|
|
|
char *lsys;
|
|
|
|
char *lserv;
|
|
|
|
char *rsys;
|
|
|
|
char *rserv;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2012-06-27 10:36:15 +00:00
|
|
|
uchar nb[4]; // Number of data bytes in this message
|
|
|
|
uchar msg[4]; // Message number
|
|
|
|
uchar acked[4]; // Number of messages acked
|
2011-03-30 12:46:40 +00:00
|
|
|
} Hdr;
|
|
|
|
|
|
|
|
typedef struct t_Buf {
|
2012-06-27 10:36:15 +00:00
|
|
|
Hdr hdr;
|
|
|
|
uchar buf[Bufsize];
|
2011-03-30 12:46:40 +00:00
|
|
|
} Buf;
|
|
|
|
|
|
|
|
static Channel *unsent;
|
|
|
|
static Channel *unacked;
|
|
|
|
static Channel *empty;
|
2012-06-27 10:36:15 +00:00
|
|
|
static int netfd;
|
|
|
|
static int inmsg;
|
2011-03-30 12:46:40 +00:00
|
|
|
static char *devdir;
|
2012-06-27 10:36:15 +00:00
|
|
|
static int debug;
|
|
|
|
static int done;
|
2011-03-30 12:46:40 +00:00
|
|
|
static char *dialstring;
|
2012-06-27 10:36:15 +00:00
|
|
|
static int maxto = Maxto;
|
|
|
|
static char *Logname = "aan";
|
|
|
|
static int client;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
static Alt a[] = {
|
|
|
|
/* c v op */
|
|
|
|
{ nil, nil, CHANRCV }, // timer
|
|
|
|
{ nil, nil, CHANRCV }, // unsent
|
|
|
|
{ nil, nil, CHANEND },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void fromnet(void*);
|
|
|
|
static void fromclient(void*);
|
2012-06-27 10:36:15 +00:00
|
|
|
static void reconnect(int);
|
2011-03-30 12:46:40 +00:00
|
|
|
static void synchronize(void);
|
|
|
|
static int sendcommand(ulong, ulong);
|
|
|
|
static void showmsg(int, char *, Buf *);
|
|
|
|
static int writen(int, uchar *, int);
|
|
|
|
static int getport(char *);
|
|
|
|
static void dmessage(int, char *, ...);
|
|
|
|
static void timerproc(void *);
|
2012-06-27 10:36:15 +00:00
|
|
|
static Endpoints* getendpoints(char *);
|
2011-03-30 12:46:40 +00:00
|
|
|
static void freeendpoints(Endpoints *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2012-06-27 10:36:15 +00:00
|
|
|
fprint(2, "Usage: %s [-cd] [-m maxto] dialstring|netdir\n", argv0);
|
2011-03-30 12:46:40 +00:00
|
|
|
exits("usage");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
catch(void *, char *s)
|
|
|
|
{
|
|
|
|
if (!strcmp(s, "alarm")) {
|
2012-06-27 10:36:15 +00:00
|
|
|
syslog(0, Logname, "Timed out while waiting for reconnect, exiting...");
|
2011-03-30 12:46:40 +00:00
|
|
|
threadexitsall(nil);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
static void*
|
|
|
|
emalloc(int n)
|
|
|
|
{
|
|
|
|
ulong pc;
|
|
|
|
void *v;
|
|
|
|
|
|
|
|
pc = getcallerpc(&n);
|
|
|
|
v = malloc(n);
|
|
|
|
if(v == nil)
|
|
|
|
sysfatal("Cannot allocate memory; pc=%lux", pc);
|
|
|
|
setmalloctag(v, pc);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
estrdup(char *s)
|
|
|
|
{
|
|
|
|
char *v;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = strlen(s)+1;
|
|
|
|
v = emalloc(n);
|
|
|
|
memmove(v, s, n);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
void
|
|
|
|
threadmain(int argc, char **argv)
|
|
|
|
{
|
|
|
|
vlong synctime;
|
2012-06-27 10:36:15 +00:00
|
|
|
int i, n, failed;
|
|
|
|
Channel *timer;
|
|
|
|
Buf *b;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
client++;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug++;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2012-06-27 10:36:15 +00:00
|
|
|
maxto = (int)strtol(EARGF(usage()), nil, 0);
|
2011-03-30 12:46:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
devdir = argv[0];
|
|
|
|
if ((p = strstr(devdir, "/local")) != nil)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dialstring = argv[0];
|
|
|
|
|
|
|
|
if (debug > 0) {
|
|
|
|
int fd = open("#c/cons", OWRITE|OCEXEC);
|
|
|
|
dup(fd, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmtinstall('F', fcallfmt);
|
|
|
|
|
|
|
|
atnotify(catch, 1);
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
/*
|
|
|
|
* Set up initial connection. use short timeout
|
|
|
|
* of 60 seconds so we wont hang arround for too
|
|
|
|
* long if there is some general connection problem
|
|
|
|
* (like NAT).
|
|
|
|
*/
|
|
|
|
netfd = -1;
|
|
|
|
reconnect(60);
|
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
unsent = chancreate(sizeof(Buf *), Nbuf);
|
|
|
|
unacked = chancreate(sizeof(Buf *), Nbuf);
|
|
|
|
empty = chancreate(sizeof(Buf *), Nbuf);
|
|
|
|
timer = chancreate(sizeof(uchar *), 1);
|
2012-06-27 10:36:15 +00:00
|
|
|
if(unsent == nil || unacked == nil || empty == nil || timer == nil)
|
|
|
|
sysfatal("Cannot allocate channels");
|
2011-03-30 12:46:40 +00:00
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
for (i = 0; i < Nbuf; i++)
|
|
|
|
sendp(empty, emalloc(sizeof(Buf)));
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
if (proccreate(fromnet, nil, Stacksize) < 0)
|
2012-06-27 10:36:15 +00:00
|
|
|
sysfatal("Cannot start fromnet; %r");
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
if (proccreate(fromclient, nil, Stacksize) < 0)
|
2012-06-27 10:36:15 +00:00
|
|
|
sysfatal("Cannot start fromclient; %r");
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
if (proccreate(timerproc, timer, Stacksize) < 0)
|
2012-06-27 10:36:15 +00:00
|
|
|
sysfatal("Cannot start timerproc; %r");
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
a[Timer].c = timer;
|
|
|
|
a[Unsent].c = unsent;
|
|
|
|
a[Unsent].v = &b;
|
|
|
|
|
|
|
|
synctime = nsec() + Synctime;
|
|
|
|
failed = 0;
|
|
|
|
while (!done) {
|
|
|
|
if (failed) {
|
|
|
|
// Wait for the netreader to die.
|
|
|
|
while (netfd >= 0) {
|
|
|
|
dmessage(1, "main; waiting for netreader to die\n");
|
|
|
|
sleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the reader died; reestablish the world.
|
2012-06-27 10:36:15 +00:00
|
|
|
reconnect(maxto);
|
2011-03-30 12:46:40 +00:00
|
|
|
synchronize();
|
|
|
|
failed = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
if (nsec() >= synctime) {
|
2011-03-30 12:46:40 +00:00
|
|
|
Hdr hdr;
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
PBIT32(hdr.nb, 0);
|
|
|
|
PBIT32(hdr.acked, inmsg);
|
|
|
|
PBIT32(hdr.msg, -1);
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
if (writen(netfd, (uchar *)&hdr, sizeof(Hdr)) < 0) {
|
|
|
|
dmessage(2, "main; writen failed; %r\n");
|
|
|
|
failed = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
synctime = nsec() + Synctime;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (alt(a)) {
|
|
|
|
case Timer:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Unsent:
|
|
|
|
sendp(unacked, b);
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
PBIT32(b->hdr.acked, inmsg);
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
if (writen(netfd, (uchar *)&b->hdr, sizeof(Hdr)) < 0) {
|
|
|
|
dmessage(2, "main; writen failed; %r\n");
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
n = GBIT32(b->hdr.nb);
|
|
|
|
if (writen(netfd, b->buf, n) < 0) {
|
2011-03-30 12:46:40 +00:00
|
|
|
dmessage(2, "main; writen failed; %r\n");
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
if (n == 0)
|
2011-03-30 12:46:40 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syslog(0, Logname, "exiting...");
|
|
|
|
threadexitsall(nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
fromclient(void*)
|
|
|
|
{
|
|
|
|
static int outmsg;
|
2012-06-27 10:36:15 +00:00
|
|
|
int n;
|
|
|
|
Buf *b;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
do {
|
|
|
|
b = recvp(empty);
|
|
|
|
n = read(0, b->buf, Bufsize);
|
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0)
|
2011-03-30 12:46:40 +00:00
|
|
|
dmessage(2, "fromclient; Cannot read 9P message; %r\n");
|
|
|
|
else
|
|
|
|
dmessage(2, "fromclient; Client terminated\n");
|
2012-06-27 10:36:15 +00:00
|
|
|
n = 0;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
PBIT32(b->hdr.nb, n);
|
|
|
|
PBIT32(b->hdr.msg, outmsg);
|
2011-03-30 12:46:40 +00:00
|
|
|
showmsg(1, "fromclient", b);
|
|
|
|
sendp(unsent, b);
|
2012-06-27 10:36:15 +00:00
|
|
|
outmsg++;
|
|
|
|
} while(n > 0);
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fromnet(void*)
|
|
|
|
{
|
|
|
|
static int lastacked;
|
2012-06-27 10:36:15 +00:00
|
|
|
int n, m, len, acked;
|
2011-03-30 12:46:40 +00:00
|
|
|
Buf *b;
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
b = emalloc(sizeof(Buf));
|
2011-03-30 12:46:40 +00:00
|
|
|
while (!done) {
|
|
|
|
while (netfd < 0) {
|
2012-06-27 10:36:15 +00:00
|
|
|
if(done)
|
|
|
|
return;
|
|
|
|
dmessage(1, "fromnet; waiting for connection... (inmsg %d)\n", inmsg);
|
2011-03-30 12:46:40 +00:00
|
|
|
sleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the header.
|
2012-06-27 10:36:15 +00:00
|
|
|
len = readn(netfd, (uchar *)&b->hdr, sizeof(Hdr));
|
|
|
|
if (len <= 0) {
|
2011-03-30 12:46:40 +00:00
|
|
|
if (len < 0)
|
|
|
|
dmessage(1, "fromnet; (hdr) network failure; %r\n");
|
|
|
|
else
|
|
|
|
dmessage(1, "fromnet; (hdr) network closed\n");
|
|
|
|
close(netfd);
|
|
|
|
netfd = -1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
n = GBIT32(b->hdr.nb);
|
|
|
|
m = GBIT32(b->hdr.msg);
|
|
|
|
acked = GBIT32(b->hdr.acked);
|
|
|
|
dmessage(2, "fromnet: Got message, size %d, nb %d, msg %d, acked %d, lastacked %d\n",
|
|
|
|
len, n, m, acked, lastacked);
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
if (m >= 0) {
|
2011-03-30 12:46:40 +00:00
|
|
|
dmessage(1, "fromnet; network closed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
|
|
|
|
if (n > Bufsize) {
|
|
|
|
dmessage(1, "fromnet; message too big %d > %d\n", n, Bufsize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = readn(netfd, b->buf, n);
|
|
|
|
if (len <= 0 || len != n) {
|
2011-03-30 12:46:40 +00:00
|
|
|
if (len == 0)
|
|
|
|
dmessage(1, "fromnet; network closed\n");
|
|
|
|
else
|
|
|
|
dmessage(1, "fromnet; network failure; %r\n");
|
|
|
|
close(netfd);
|
|
|
|
netfd = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
if (m < inmsg) {
|
|
|
|
dmessage(1, "fromnet; skipping message %d, currently at %d\n", m, inmsg);
|
2011-03-30 12:46:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the acked list.
|
2012-06-27 10:36:15 +00:00
|
|
|
while(lastacked != acked) {
|
2011-03-30 12:46:40 +00:00
|
|
|
Buf *rb;
|
|
|
|
|
|
|
|
rb = recvp(unacked);
|
2012-06-27 10:36:15 +00:00
|
|
|
m = GBIT32(rb->hdr.msg);
|
|
|
|
if (m != lastacked) {
|
|
|
|
dmessage(1, "fromnet; rb %p, msg %d, lastacked %d\n", rb, m, lastacked);
|
|
|
|
sysfatal("fromnet; bug");
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
PBIT32(rb->hdr.msg, -1);
|
2011-03-30 12:46:40 +00:00
|
|
|
sendp(empty, rb);
|
2012-06-27 10:36:15 +00:00
|
|
|
lastacked++;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
inmsg++;
|
|
|
|
|
|
|
|
showmsg(1, "fromnet", b);
|
|
|
|
|
|
|
|
if (writen(1, b->buf, len) < 0)
|
|
|
|
sysfatal("fromnet; cannot write to client; %r");
|
|
|
|
}
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-27 10:36:15 +00:00
|
|
|
reconnect(int secs)
|
2011-03-30 12:46:40 +00:00
|
|
|
{
|
|
|
|
char ldir[40];
|
|
|
|
int lcfd, fd;
|
|
|
|
|
|
|
|
if (dialstring) {
|
|
|
|
syslog(0, Logname, "dialing %s", dialstring);
|
2012-06-27 10:36:15 +00:00
|
|
|
alarm(secs*1000);
|
2011-03-30 12:46:40 +00:00
|
|
|
while ((fd = dial(dialstring, nil, nil, nil)) < 0) {
|
|
|
|
char err[32];
|
|
|
|
|
|
|
|
err[0] = '\0';
|
|
|
|
errstr(err, sizeof err);
|
|
|
|
if (strstr(err, "connection refused")) {
|
|
|
|
dmessage(1, "reconnect; server died...\n");
|
|
|
|
threadexitsall("server died...");
|
|
|
|
}
|
|
|
|
dmessage(1, "reconnect: dialed %s; %s\n", dialstring, err);
|
|
|
|
sleep(1000);
|
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
alarm(0);
|
2011-03-30 12:46:40 +00:00
|
|
|
syslog(0, Logname, "reconnected to %s", dialstring);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Endpoints *ep;
|
|
|
|
|
|
|
|
syslog(0, Logname, "waiting for connection on %s", devdir);
|
2012-06-27 10:36:15 +00:00
|
|
|
alarm(secs*1000);
|
2011-03-30 12:46:40 +00:00
|
|
|
if ((lcfd = listen(devdir, ldir)) < 0)
|
|
|
|
sysfatal("reconnect; cannot listen; %r");
|
|
|
|
if ((fd = accept(lcfd, ldir)) < 0)
|
|
|
|
sysfatal("reconnect; cannot accept; %r");
|
|
|
|
alarm(0);
|
|
|
|
close(lcfd);
|
|
|
|
|
|
|
|
ep = getendpoints(ldir);
|
|
|
|
dmessage(1, "rsys '%s'\n", ep->rsys);
|
|
|
|
syslog(0, Logname, "connected from %s", ep->rsys);
|
|
|
|
freeendpoints(ep);
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
// Wakes up the netreader.
|
|
|
|
netfd = fd;
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
synchronize(void)
|
|
|
|
{
|
|
|
|
Channel *tmp;
|
|
|
|
Buf *b;
|
2012-06-27 10:36:15 +00:00
|
|
|
int n;
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
// Ignore network errors here. If we fail during
|
|
|
|
// synchronization, the next alarm will pick up
|
|
|
|
// the error.
|
|
|
|
|
|
|
|
tmp = chancreate(sizeof(Buf *), Nbuf);
|
|
|
|
while ((b = nbrecvp(unacked)) != nil) {
|
2012-06-27 10:36:15 +00:00
|
|
|
n = GBIT32(b->hdr.nb);
|
|
|
|
writen(netfd, (uchar *)&b->hdr, sizeof(Hdr));
|
|
|
|
writen(netfd, b->buf, n);
|
2011-03-30 12:46:40 +00:00
|
|
|
sendp(tmp, b);
|
|
|
|
}
|
|
|
|
chanfree(unacked);
|
|
|
|
unacked = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
showmsg(int level, char *s, Buf *b)
|
|
|
|
{
|
2012-06-27 10:36:15 +00:00
|
|
|
int n;
|
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
if (b == nil) {
|
|
|
|
dmessage(level, "%s; b == nil\n", s);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
n = GBIT32(b->hdr.nb);
|
|
|
|
dmessage(level, "%s; (len %d) %X %X %X %X %X %X %X %X %X (%p)\n", s, n,
|
2011-03-30 12:46:40 +00:00
|
|
|
b->buf[0], b->buf[1], b->buf[2],
|
|
|
|
b->buf[3], b->buf[4], b->buf[5],
|
|
|
|
b->buf[6], b->buf[7], b->buf[8], b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
writen(int fd, uchar *buf, int nb)
|
|
|
|
{
|
|
|
|
int len = nb;
|
|
|
|
|
|
|
|
while (nb > 0) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((n = write(fd, buf, nb)) < 0) {
|
|
|
|
dmessage(1, "writen; Write failed; %r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dmessage(2, "writen: wrote %d bytes\n", n);
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
nb -= n;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timerproc(void *x)
|
|
|
|
{
|
|
|
|
Channel *timer = x;
|
|
|
|
while (!done) {
|
|
|
|
sleep((Synctime / MS(1)) >> 1);
|
|
|
|
sendp(timer, "timer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dmessage(int level, char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
if (level > debug)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
vfprint(2, fmt, arg);
|
|
|
|
va_end(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
getendpoint(char *dir, char *file, char **sysp, char **servp)
|
|
|
|
{
|
|
|
|
int fd, n;
|
|
|
|
char buf[128];
|
|
|
|
char *sys, *serv;
|
|
|
|
|
|
|
|
sys = serv = 0;
|
|
|
|
|
|
|
|
snprint(buf, sizeof buf, "%s/%s", dir, file);
|
|
|
|
fd = open(buf, OREAD);
|
|
|
|
if(fd >= 0){
|
|
|
|
n = read(fd, buf, sizeof(buf)-1);
|
|
|
|
if(n>0){
|
|
|
|
buf[n-1] = 0;
|
|
|
|
serv = strchr(buf, '!');
|
|
|
|
if(serv){
|
|
|
|
*serv++ = 0;
|
2012-06-27 10:36:15 +00:00
|
|
|
serv = estrdup(serv);
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
2012-06-27 10:36:15 +00:00
|
|
|
sys = estrdup(buf);
|
2011-03-30 12:46:40 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if(serv == 0)
|
2012-06-27 10:36:15 +00:00
|
|
|
serv = estrdup("unknown");
|
2011-03-30 12:46:40 +00:00
|
|
|
if(sys == 0)
|
2012-06-27 10:36:15 +00:00
|
|
|
sys = estrdup("unknown");
|
2011-03-30 12:46:40 +00:00
|
|
|
*servp = serv;
|
|
|
|
*sysp = sys;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Endpoints *
|
|
|
|
getendpoints(char *dir)
|
|
|
|
{
|
|
|
|
Endpoints *ep;
|
|
|
|
|
2012-06-27 10:36:15 +00:00
|
|
|
ep = emalloc(sizeof(*ep));
|
2011-03-30 12:46:40 +00:00
|
|
|
getendpoint(dir, "local", &ep->lsys, &ep->lserv);
|
|
|
|
getendpoint(dir, "remote", &ep->rsys, &ep->rserv);
|
|
|
|
return ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
freeendpoints(Endpoints *ep)
|
|
|
|
{
|
|
|
|
free(ep->lsys);
|
|
|
|
free(ep->rsys);
|
|
|
|
free(ep->lserv);
|
|
|
|
free(ep->rserv);
|
|
|
|
free(ep);
|
|
|
|
}
|