Fixed a bug in lstgetremotename2.
[doldaconnect.git] / daemon / net.c
index 242f10e..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;
@@ -365,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);
     }
 }
 
@@ -519,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)
@@ -638,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;
@@ -699,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);
@@ -738,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)
@@ -799,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);
 }
 
@@ -1490,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))