Fixed a bug in lstgetremotename2.
[doldaconnect.git] / daemon / net.c
index 5943791..751c493 100644 (file)
@@ -120,6 +120,8 @@ struct ufd {
     } d;
 };
 
+static int getlocalname(int fd, struct sockaddr **namebuf, socklen_t *lenbuf);
+
 static struct ufd *ufds = NULL;
 static struct scons *rbatch, *wbatch, *cbatch;
 int numsocks = 0;
@@ -247,7 +249,8 @@ static void freeufd(struct ufd *ufd)
     if(ufd == ufds)
        ufds = ufd->next;
     closeufd(ufd);
-    putsock(ufd->sk);
+    if(ufd->sk != NULL)
+       putsock(ufd->sk);
     if(ufd->type == UFD_SOCK) {
        if(ufd->d.s.remote != NULL)
            free(ufd->d.s.remote);
@@ -261,6 +264,7 @@ static struct ufd *mkufd(int fd, int type, struct socket *sk)
     
     ufd = memset(smalloc(sizeof(*ufd)), 0, sizeof(*ufd));
     ufd->fd = fd;
+    ufd->type = type;
     if(sk != NULL) {
        getsock(ufd->sk = sk);
        sk->ufd = ufd;
@@ -294,6 +298,7 @@ static struct ufd *dupufd(struct ufd *ufd)
        freeufd(nufd);
        return(NULL);
     }
+    sksetstate(nsk, SOCK_EST);
     if(ufd->type == UFD_SOCK) {
        nufd->d.s.family = ufd->d.s.family;
        nufd->d.s.type = ufd->d.s.type;
@@ -362,13 +367,16 @@ static void freesock(struct socket *sk)
 
 void putsock(struct socket *sk)
 {
+    struct socket *back;
+    
     if(--(sk->refcount) < 0) {
        flog(LOG_CRIT, "BUG: socket refcount < 0");
        abort();
     }
     if((sk->refcount == 0) && (sk->back->refcount == 0)) {
+       back = sk->back;
        freesock(sk);
-       freesock(sk->back);
+       freesock(back);
     }
 }
 
@@ -407,6 +415,8 @@ void sockpushdata(struct socket *sk, void *buf, size_t size)
 /* Read as the preterite of `read' */
 void sockread(struct socket *sk)
 {
+    if((sockgetdatalen(sk) == 0) && (sk->eos == 1))
+       linksock(&rbatch, sk);
     linksock(&wbatch, sk->back);
 }
 
@@ -514,15 +524,7 @@ void sockqueuedg(struct socket *sk, struct dgrambuf *dg)
     linksock(&rbatch, sk->back);
 }
 
-void sockeos(struct socket *sk)
-{
-    sksetstate(sk, SOCK_STL);
-    if(sk->back->eos == 0)
-       sk->back->eos = 1;
-    linksock(&rbatch, sk->back);
-}
-
-static void sockerror(struct socket *sk, int en)
+void sockerror(struct socket *sk, int en)
 {
     sksetstate(sk, SOCK_STL);
     if(sk->back->errcb != NULL)
@@ -633,7 +635,7 @@ static void sockrecv(struct ufd *ufd)
            freedgbuf(dbuf);
            if((ufd->type != UFD_SOCK) || !((ufd->d.s.family == AF_INET) || (ufd->d.s.family == AF_INET6)))
            {
-               sockeos(ufd->sk);
+               closesock(ufd->sk);
                closeufd(ufd);
            }
            return;
@@ -694,7 +696,7 @@ static void sockrecv(struct ufd *ufd)
        {
            free(buf);
            closeufd(ufd);
-           sockeos(ufd->sk);
+           closesock(ufd->sk);
            return;
        }
        sockqueue(ufd->sk, buf, ret);
@@ -733,20 +735,10 @@ static void sockflush(struct ufd *ufd)
 
 void closesock(struct socket *sk)
 {
-/*
-    struct sockaddr_un *un;
-    
-    if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)(void *)&un, NULL) && (un->sun_family == PF_UNIX))
-    {
-       if((sk->state == SOCK_LST) && strchr(un->sun_path, '/'))
-       {
-           if(unlink(un->sun_path))
-               flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
-       }
-    }
-*/
     sksetstate(sk, SOCK_STL);
-    sockeos(sk);
+    if(sk->back->eos == 0)
+       sk->back->eos = 1;
+    linksock(&rbatch, sk->back);
 }
 
 size_t sockgetdatalen(struct socket *sk)
@@ -794,6 +786,14 @@ static int rebindunix(struct ufd *ufd, struct sockaddr *name, socklen_t namelen)
 
 void closelport(struct lport *lp)
 {
+    struct ufd *ufd;
+    struct sockaddr_un *un;
+    
+    ufd = lp->ufd;
+    if((ufd->d.l.family == AF_UNIX) && !getlocalname(ufd->fd, (struct sockaddr **)(void *)&un, NULL) && (un->sun_family == PF_UNIX) && strchr(un->sun_path, '/')) {
+       if(unlink(un->sun_path))
+           flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
+    }
     freeufd(lp->ufd);
 }
 
@@ -912,7 +912,7 @@ struct socket *netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t
     struct ufd *nufd;
     
     nufd = dupufd(sk->back->ufd);
-    sk = nufd->sk->back;
+    getsock(sk = nufd->sk->back);
     memcpy(nufd->d.s.remote = smalloc(addrlen), addr, nufd->d.s.remotelen = addrlen);
     nufd->ignread = 1;
     return(sk);
@@ -973,33 +973,62 @@ static void runbatches(void)
 
     for(sc = cbatch, cbatch = NULL; sc; sc = nsc) {
        nsc = sc->n;
-       sc->s->conncb(sc->s, 0, sc->s->data);
+       if(sc->s->conncb != NULL)
+           sc->s->conncb(sc->s, 0, sc->s->data);
        free(sc);
     }
     for(sc = rbatch, rbatch = NULL; sc; sc = nsc) {
        nsc = sc->n;
-       sc->s->readcb(sc->s, sc->s->data);
+       if(sc->s->readcb != NULL)
+           sc->s->readcb(sc->s, sc->s->data);
+       if((sockgetdatalen(sc->s) == 0) && (sc->s->eos == 1)) {
+           if(sc->s->errcb != NULL)
+               sc->s->errcb(sc->s, 0, sc->s->data);
+           sc->s->eos = 2;
+       }
        free(sc);
     }
     for(sc = wbatch, wbatch = NULL; sc; sc = nsc) {
        nsc = sc->n;
-       sc->s->writecb(sc->s, sc->s->data);
+       if(sc->s->writecb != NULL)
+           sc->s->writecb(sc->s, sc->s->data);
        free(sc);
     }
 }
 
+static void cleansocks(void)
+{
+    struct ufd *ufd, *next;
+    
+    for(ufd = ufds; ufd != NULL; ufd = next) {
+       next = ufd->next;
+       if(ufd->sk && (sockgetdatalen(ufd->sk) == 0)) {
+           if(ufd->sk->eos == 1) {
+               ufd->sk->eos = 2;
+               closeufd(ufd);
+               closesock(ufd->sk);
+           }
+           if((ufd->sk->refcount == 1) && (ufd->sk->back->refcount == 0)) {
+               freeufd(ufd);
+               continue;
+           }
+       }
+    }
+}
+
 int pollsocks(int timeout)
 {
     int ret;
     socklen_t retlen;
     int newfd, maxfd;
     fd_set rfds, wfds, efds;
-    struct ufd *ufd, *nufd, *next;
+    struct ufd *ufd, *nufd;
     struct socket *nsk;
     struct sockaddr_storage ss;
     socklen_t sslen;
     struct timeval tv;
     
+    cleansocks();
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&efds);
@@ -1018,6 +1047,8 @@ int pollsocks(int timeout)
        if(ufd->fd > maxfd)
            maxfd = ufd->fd;
     }
+    if(rbatch || wbatch || cbatch)
+       timeout = 0;
     tv.tv_sec = timeout / 1000;
     tv.tv_usec = (timeout % 1000) * 1000;
     ret = select(maxfd + 1, &rfds, &wfds, &efds, (timeout < 0)?NULL:&tv);
@@ -1030,8 +1061,7 @@ int pollsocks(int timeout)
        }
        return(1);
     }
-    for(ufd = ufds; ufd != NULL; ufd = ufd->next)
-    {
+    for(ufd = ufds; ufd != NULL; ufd = ufd->next) {
        if(ufd->sk < 0)
            continue;
        if(ufd->type == UFD_LISTEN) {
@@ -1050,7 +1080,7 @@ int pollsocks(int timeout)
                if(ss.ss_family == PF_UNIX)
                    acceptunix(nufd);
                if(ufd->d.l.lp->acceptcb != NULL)
-                   ufd->d.l.lp->acceptcb(ufd->d.l.lp, nsk, ufd->d.l.lp->data);
+                   ufd->d.l.lp->acceptcb(ufd->d.l.lp, nsk->back, ufd->d.l.lp->data);
                putsock(nsk);
            }
            if(FD_ISSET(ufd->fd, &efds)) {
@@ -1084,25 +1114,15 @@ int pollsocks(int timeout)
                }
                if(FD_ISSET(ufd->fd, &rfds))
                    sockrecv(ufd);
+               if(ufd->fd == -1)
+                   continue;
                if(FD_ISSET(ufd->fd, &wfds))
                    sockflush(ufd);
            }
        }
     }
-    for(ufd = ufds; ufd != NULL; ufd = next) {
-       next = ufd->next;
-       if(sockgetdatalen(ufd->sk) == 0) {
-           if(ufd->sk->eos) {
-               closeufd(ufd);
-               closesock(ufd->sk);
-           }
-           if((ufd->sk->refcount == 1) && (ufd->sk->back->refcount == 0)) {
-               freeufd(ufd);
-               continue;
-           }
-       }
-    }
     runbatches();
+    cleansocks();
     return(1);
 }
 
@@ -1465,9 +1485,9 @@ int lstgetremotename2(struct lport *lp, struct socket *sk2, struct sockaddr **na
        errno = EOPNOTSUPP;
        return(-1);
     }
-    if(ufd1->d.s.family != ufd2->d.s.family)
+    if(ufd1->d.l.family != ufd2->d.s.family)
     {
-       flog(LOG_ERR, "using lstgetremotename2 with sockets of differing family: %i %i", ufd1->d.s.family, ufd2->d.s.family);
+       flog(LOG_ERR, "using lstgetremotename2 with sockets of differing family: %i %i", ufd1->d.l.family, ufd2->d.s.family);
        return(-1);
     }
     if(getremotename(ufd1->fd, &name1, &len1))