X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=daemon%2Fnet.c;h=7a68dae5c5dca11856a4824edda73fa7fd78ca6c;hb=89d650f1e7be2401bcbd7c6a1782d29eaa33c413;hp=242f10e70a9a3460b2dbf028ae3704514a5e7560;hpb=5afb0d147fcc162c25b18cc5729d4ceedb9badd3;p=doldaconnect.git diff --git a/daemon/net.c b/daemon/net.c index 242f10e..7a68dae 100644 --- a/daemon/net.c +++ b/daemon/net.c @@ -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); @@ -707,14 +704,16 @@ static void sockrecv(struct ufd *ufd) } } -static void sockflush(struct ufd *ufd) +static int sockflush(struct ufd *ufd) { int ret; struct dgrambuf *dbuf; int dgram; - if((dgram = ufddgram(ufd)) < 0) - return; + if((dgram = ufddgram(ufd)) < 0) { + errno = EBADFD; + return(-1); + } if(dgram) { dbuf = sockgetdgbuf(ufd->sk); sendto(ufd->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen); @@ -724,34 +723,22 @@ static void sockflush(struct ufd *ufd) ret = send(ufd->fd, ufd->sk->buf.s.buf, ufd->sk->buf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL); else ret = write(ufd->fd, ufd->sk->buf.s.buf, ufd->sk->buf.s.datasize); - if(ret < 0) { - /* For now, assume transient error, since - * the socket is polled for errors */ - return; - } + if(ret < 0) + return(-1); if(ret > 0) { memmove(ufd->sk->buf.s.buf, ((char *)ufd->sk->buf.s.buf) + ret, ufd->sk->buf.s.datasize -= ret); sockread(ufd->sk); } } + return(0); } 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); } @@ -980,6 +975,7 @@ static void runbatches(void) nsc = sc->n; if(sc->s->conncb != NULL) sc->s->conncb(sc->s, 0, sc->s->data); + putsock(sc->s); free(sc); } for(sc = rbatch, rbatch = NULL; sc; sc = nsc) { @@ -991,12 +987,14 @@ static void runbatches(void) sc->s->errcb(sc->s, 0, sc->s->data); sc->s->eos = 2; } + putsock(sc->s); free(sc); } for(sc = wbatch, wbatch = NULL; sc; sc = nsc) { nsc = sc->n; if(sc->s->writecb != NULL) sc->s->writecb(sc->s, sc->s->data); + putsock(sc->s); free(sc); } } @@ -1007,7 +1005,7 @@ static void cleansocks(void) for(ufd = ufds; ufd != NULL; ufd = next) { next = ufd->next; - if(ufd->sk && (sockgetdatalen(ufd->sk) == 0)) { + if(ufd->sk && ((ufd->fd < 0) || (sockgetdatalen(ufd->sk) == 0))) { if(ufd->sk->eos == 1) { ufd->sk->eos = 2; closeufd(ufd); @@ -1121,8 +1119,13 @@ int pollsocks(int timeout) sockrecv(ufd); if(ufd->fd == -1) continue; - if(FD_ISSET(ufd->fd, &wfds)) - sockflush(ufd); + if(FD_ISSET(ufd->fd, &wfds)) { + if(sockflush(ufd)) { + sockerror(ufd->sk, errno); + closeufd(ufd); + continue; + } + } } } } @@ -1490,9 +1493,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))