aan: use netconninfo from dial(2), threadsetname
This commit is contained in:
parent
623901ed1f
commit
0b22e8b455
1 changed files with 16 additions and 76 deletions
|
@ -66,11 +66,8 @@ static void synchronize(void);
|
||||||
static int sendcommand(ulong, ulong);
|
static int sendcommand(ulong, ulong);
|
||||||
static void showmsg(int, char *, Buf *);
|
static void showmsg(int, char *, Buf *);
|
||||||
static int writen(int, uchar *, int);
|
static int writen(int, uchar *, int);
|
||||||
static int getport(char *);
|
|
||||||
static void dmessage(int, char *, ...);
|
static void dmessage(int, char *, ...);
|
||||||
static void timerproc(void *);
|
static void timerproc(void *);
|
||||||
static Endpoints* getendpoints(char *);
|
|
||||||
static void freeendpoints(Endpoints *);
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
|
@ -103,18 +100,6 @@ emalloc(int n)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char*
|
|
||||||
estrdup(char *s)
|
|
||||||
{
|
|
||||||
char *v;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
n = strlen(s)+1;
|
|
||||||
v = emalloc(n);
|
|
||||||
memmove(v, s, n);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
threadmain(int argc, char **argv)
|
threadmain(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
@ -259,6 +244,8 @@ fromclient(void*)
|
||||||
int n;
|
int n;
|
||||||
Buf *b;
|
Buf *b;
|
||||||
|
|
||||||
|
threadsetname("fromclient");
|
||||||
|
|
||||||
do {
|
do {
|
||||||
b = recvp(empty);
|
b = recvp(empty);
|
||||||
n = read(0, b->buf, Bufsize);
|
n = read(0, b->buf, Bufsize);
|
||||||
|
@ -284,6 +271,8 @@ fromnet(void*)
|
||||||
int n, m, len, acked;
|
int n, m, len, acked;
|
||||||
Buf *b;
|
Buf *b;
|
||||||
|
|
||||||
|
threadsetname("fromnet");
|
||||||
|
|
||||||
b = emalloc(sizeof(Buf));
|
b = emalloc(sizeof(Buf));
|
||||||
while (!done) {
|
while (!done) {
|
||||||
while (netfd < 0) {
|
while (netfd < 0) {
|
||||||
|
@ -366,13 +355,14 @@ fromnet(void*)
|
||||||
static void
|
static void
|
||||||
reconnect(int secs)
|
reconnect(int secs)
|
||||||
{
|
{
|
||||||
|
NetConnInfo *nci;
|
||||||
char ldir[40];
|
char ldir[40];
|
||||||
int lcfd, fd;
|
int lcfd, fd;
|
||||||
|
|
||||||
if (dialstring) {
|
if (dialstring) {
|
||||||
syslog(0, Logname, "dialing %s", dialstring);
|
syslog(0, Logname, "dialing %s", dialstring);
|
||||||
alarm(secs*1000);
|
alarm(secs*1000);
|
||||||
while ((fd = dial(dialstring, nil, nil, nil)) < 0) {
|
while ((fd = dial(dialstring, nil, ldir, nil)) < 0) {
|
||||||
char err[32];
|
char err[32];
|
||||||
|
|
||||||
err[0] = '\0';
|
err[0] = '\0';
|
||||||
|
@ -388,8 +378,6 @@ reconnect(int secs)
|
||||||
syslog(0, Logname, "reconnected to %s", dialstring);
|
syslog(0, Logname, "reconnected to %s", dialstring);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Endpoints *ep;
|
|
||||||
|
|
||||||
syslog(0, Logname, "waiting for connection on %s", devdir);
|
syslog(0, Logname, "waiting for connection on %s", devdir);
|
||||||
alarm(secs*1000);
|
alarm(secs*1000);
|
||||||
if ((lcfd = listen(devdir, ldir)) < 0)
|
if ((lcfd = listen(devdir, ldir)) < 0)
|
||||||
|
@ -398,13 +386,15 @@ reconnect(int secs)
|
||||||
sysfatal("reconnect; cannot accept; %r");
|
sysfatal("reconnect; cannot accept; %r");
|
||||||
alarm(0);
|
alarm(0);
|
||||||
close(lcfd);
|
close(lcfd);
|
||||||
|
|
||||||
ep = getendpoints(ldir);
|
|
||||||
dmessage(1, "rsys '%s'\n", ep->rsys);
|
|
||||||
syslog(0, Logname, "connected from %s", ep->rsys);
|
|
||||||
freeendpoints(ep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(nci = getnetconninfo(ldir, fd)){
|
||||||
|
syslog(0, Logname, "connected from %s", nci->rsys);
|
||||||
|
threadsetname(client? "client %s %s" : "server %s %s", ldir, nci->rsys);
|
||||||
|
freenetconninfo(nci);
|
||||||
|
} else
|
||||||
|
syslog(0, Logname, "connected");
|
||||||
|
|
||||||
// Wakes up the netreader.
|
// Wakes up the netreader.
|
||||||
netfd = fd;
|
netfd = fd;
|
||||||
}
|
}
|
||||||
|
@ -474,6 +464,9 @@ static void
|
||||||
timerproc(void *x)
|
timerproc(void *x)
|
||||||
{
|
{
|
||||||
Channel *timer = x;
|
Channel *timer = x;
|
||||||
|
|
||||||
|
threadsetname("timer");
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
sleep((Synctime / MS(1)) >> 1);
|
sleep((Synctime / MS(1)) >> 1);
|
||||||
sendp(timer, "timer");
|
sendp(timer, "timer");
|
||||||
|
@ -492,56 +485,3 @@ dmessage(int level, char *fmt, ...)
|
||||||
vfprint(2, fmt, arg);
|
vfprint(2, fmt, arg);
|
||||||
va_end(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;
|
|
||||||
serv = estrdup(serv);
|
|
||||||
}
|
|
||||||
sys = estrdup(buf);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
if(serv == 0)
|
|
||||||
serv = estrdup("unknown");
|
|
||||||
if(sys == 0)
|
|
||||||
sys = estrdup("unknown");
|
|
||||||
*servp = serv;
|
|
||||||
*sysp = sys;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Endpoints *
|
|
||||||
getendpoints(char *dir)
|
|
||||||
{
|
|
||||||
Endpoints *ep;
|
|
||||||
|
|
||||||
ep = emalloc(sizeof(*ep));
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue