X-Git-Url: http://dolda2000.com/gitweb/?p=doldaconnect.git;a=blobdiff_plain;f=daemon%2Fnet.c;h=240855ac52f59683cb8782d1230ca5314835d8c5;hp=594379117d39c2e2f0596753971ca9d611e3b30a;hb=51da262d8d796aa8a31ee1513783735a40130931;hpb=f96d0914e73385d5938650636b584c9be9d35ab4 diff --git a/daemon/net.c b/daemon/net.c index 5943791..240855a 100644 --- a/daemon/net.c +++ b/daemon/net.c @@ -66,8 +66,6 @@ static struct configvar myvars[] = * and net.visibleipv4 are unspecified the address of the hub * connection is used. */ {CONF_VAR_STRING, "publicif", {.str = L""}}, - /* Diffserv should be supported on IPv4, too, but I don't know the - * API to do that. */ /** The Diffserv value to use on IPv6 connections when the * minimize cost TOS value is used (see the TOS VALUES * section). */ @@ -84,6 +82,12 @@ static struct configvar myvars[] = * minimize delay TOS value is used (see the TOS VALUES * section). */ {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}}, + /** If enabled, the IP TOS interface will be used to set Diffserv + * codepoints on IPv4 sockets, by shifting the DSCP value two bits + * to the left (remember, the DSCP field in the IPv4 header is + * defined as the 6 uppermost bits of the TOS field, the lower two + * being left for ECN). This may only work on Linux. */ + {CONF_VAR_BOOL, "dscp-tos", {.num = 0}}, {CONF_VAR_END} }; @@ -120,6 +124,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; @@ -209,6 +215,7 @@ static struct socket *newsock1(int dgram) new->refcount = 1; new->state = -1; new->dgram = dgram; + new->maxbuf = 65536; numsocks++; return(new); } @@ -247,7 +254,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 +269,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 +303,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 +372,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 +420,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 +529,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 +640,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 +701,7 @@ static void sockrecv(struct ufd *ufd) { free(buf); closeufd(ufd); - sockeos(ufd->sk); + closesock(ufd->sk); return; } sockqueue(ufd->sk, buf, ret); @@ -702,14 +709,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); @@ -719,34 +728,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) @@ -764,9 +761,14 @@ size_t sockgetdatalen(struct socket *sk) return(ret); } -size_t sockqueuesize(struct socket *sk) +/* size_t sockqueuesize(struct socket *sk) */ +/* { */ +/* return(sockgetdatalen(sk->back)); */ +/* } */ + +ssize_t sockqueueleft(struct socket *sk) { - return(sockgetdatalen(sk->back)); + return(sk->back->maxbuf - sockgetdatalen(sk->back)); } /* @@ -794,6 +796,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 +922,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,40 +983,72 @@ 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); + putsock(sc->s); 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; + } + putsock(sc->s); 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); + putsock(sc->s); free(sc); } } +static void cleansocks(void) +{ + struct ufd *ufd, *next; + + for(ufd = ufds; ufd != NULL; ufd = next) { + next = ufd->next; + if(ufd->sk && ((ufd->fd < 0) || (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); for(maxfd = 0, ufd = ufds; ufd != NULL; ufd = ufd->next) { if(ufd->fd < 0) continue; - if(!ufd->ignread) + if(!ufd->ignread && ((ufd->sk == NULL) || (sockqueueleft(ufd->sk) > 0))) FD_SET(ufd->fd, &rfds); if(ufd->sk != NULL) { if(sockgetdatalen(ufd->sk) > 0) @@ -1018,6 +1060,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 +1074,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 +1093,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 +1127,20 @@ int pollsocks(int timeout) } if(FD_ISSET(ufd->fd, &rfds)) sockrecv(ufd); - 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; + if(ufd->fd == -1) + continue; + if(FD_ISSET(ufd->fd, &wfds)) { + if(sockflush(ufd)) { + sockerror(ufd->sk, errno); + closeufd(ufd); + continue; + } + } } } } runbatches(); + cleansocks(); return(1); } @@ -1121,6 +1159,7 @@ int socksettos(struct socket *sk, int tos) { int buf; struct ufd *ufd; + int dscp2tos; ufd = getskufd(sk); if(ufd->type != UFD_SOCK) { @@ -1131,22 +1170,35 @@ int socksettos(struct socket *sk, int tos) return(0); /* Unix sockets are always perfect. :) */ if(ufd->d.s.family == AF_INET) { + dscp2tos = confgetint("net", "dscp-tos"); switch(tos) { case 0: buf = 0; break; case SOCK_TOS_MINCOST: - buf = 0x02; + if(dscp2tos) + buf = confgetint("net", "diffserv-mincost") << 2; + else + buf = 0x02; break; case SOCK_TOS_MAXREL: - buf = 0x04; + if(dscp2tos) + buf = confgetint("net", "diffserv-maxrel") << 2; + else + buf = 0x04; break; case SOCK_TOS_MAXTP: - buf = 0x08; + if(dscp2tos) + buf = confgetint("net", "diffserv-maxtp") << 2; + else + buf = 0x08; break; case SOCK_TOS_MINDELAY: - buf = 0x10; + if(dscp2tos) + buf = confgetint("net", "diffserv-mindelay") << 2; + else + buf = 0x10; break; default: flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos); @@ -1465,9 +1517,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)) @@ -1501,13 +1553,13 @@ int getucred(struct socket *sk, uid_t *uid, gid_t *gid) return(0); } -void sockblock(struct socket *sk, int block) -{ - struct ufd *ufd; +/* void sockblock(struct socket *sk, int block) */ +/* { */ +/* struct ufd *ufd; */ - ufd = getskufd(sk); - ufd->ignread = block; -} +/* ufd = getskufd(sk); */ +/* ufd->ignread = block; */ +/* } */ int sockfamily(struct socket *sk) {