From 0d3d364b48292a4bd349b2dd096f060b1db21212 Mon Sep 17 00:00:00 2001 From: mdw Date: Fri, 22 Jun 2001 19:37:00 +0000 Subject: [PATCH] New @conn_init@ interface. --- blast.c | 7 ++++--- socket.c | 61 ++++++++++++------------------------------------------------- 2 files changed, 16 insertions(+), 52 deletions(-) diff --git a/blast.c b/blast.c index fdad3dd..c273a41 100644 --- a/blast.c +++ b/blast.c @@ -75,8 +75,8 @@ static void connected(int fd, void *p) else { count++; close(fd); - newconn(b); } + newconn(b); } static void timeout(struct timeval *tv, void *p) @@ -110,9 +110,10 @@ static void newconn(blast *b) goto fail; gettimeofday(&tv, 0); TV_ADD(&tv, &tv, &ctv); + if (conn_init(&b->c, &sel, fd, (struct sockaddr *)&sin, sizeof(sin), + connected, b)) + goto fail; sel_addtimer(&sel, &b->t, &tv, timeout, b); - conn_init(&b->c, &sel, fd, (struct sockaddr *)&sin, sizeof(sin), - connected, b); return; fail: diff --git a/socket.c b/socket.c index 0a80b48..1b7e214 100644 --- a/socket.c +++ b/socket.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: socket.c,v 1.6 2001/02/03 20:30:03 mdw Exp $ + * $Id: socket.c,v 1.7 2001/06/22 19:37:00 mdw Exp $ * * Socket source and target definitions * @@ -29,6 +29,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: socket.c,v $ + * Revision 1.7 2001/06/22 19:37:00 mdw + * New @conn_init@ interface. + * * Revision 1.6 2001/02/03 20:30:03 mdw * Support re-reading config files on SIGHUP. * @@ -136,9 +139,6 @@ typedef struct ssept { ssource *s; } ssept; -#define SKF_CONN 16u -#define SKF_BROKEN 32u - /*----- Protocol table ----------------------------------------------------*/ static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 }; @@ -249,10 +249,9 @@ static void stept_close(endpt *e) { stept *ee = (stept *)e; - if (ee->e.f & EPF_PENDING) { - if (ee->e.f & SKF_CONN) - conn_kill(&ee->c); - } else { + if (ee->e.f & EPF_PENDING) + conn_kill(&ee->c); + else { REFFD_DEC(ee->e.in); REFFD_DEC(ee->e.out); } @@ -276,35 +275,14 @@ static void stept_go(int fd, void *p) { stept *e = p; - /* --- Complicated and subtle --- * - * - * This code interacts quite closely with @starget_create@, mainly through - * flags in the endpoint block. - * - * If the connection failed, I log a message (that's easy enough). The - * behaviour then depends on whether the endpoints have been joined yet. - * If not, I set @SKF_BROKEN@ and return, so that @starget_create@ can - * clean up the mess and return an immediate failure. If they have, I kill - * the connection and everything ought to work. - * - * If the connection worked, I clear @EPF_PENDING@ (as expected, because - * my endpoint is now ready), and @SKF_CONN@ (to let @starget_create@ know - * that the connection is already going). Then, only if this isn't the - * first attempt, I rejoin this endpoint to its partner. - */ - if (fd == -1) { fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno)); - e->e.f &= ~SKF_CONN; - if (e->e.f & EPF_PENDING) - endpt_kill(&e->e); - else - e->e.f |= SKF_BROKEN; + endpt_kill(&e->e); } else { reffd *r = reffd_init(fd); REFFD_INC(r); e->e.in = e->e.out = r; - e->e.f &= ~(EPF_PENDING | SKF_CONN); + e->e.f &= ~EPF_PENDING; if (e->e.other) endpt_join(&e->e, e->e.other); } @@ -699,30 +677,15 @@ static endpt *starget_create(target *t, const char *desc) fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); e->e.ops = &stept_ops; e->e.other = 0; - e->e.f = EPF_FILE | SKF_CONN; + e->e.f = EPF_FILE | EPF_PENDING; e->e.t = 0; e->desc = xstrdup(desc); - /* --- Pay attention --- * - * - * This bit is quite subtle. The connect can succeed or fail later: that's - * fine. The problem comes if it makes its mind up right now. The flag - * @SKF_CONN@ signifies that I'm trying to connect. I set it up to begin - * with and @stept_go@ turns it off when it's done: @stept_close@ uses it - * to decide whether to kill the connection. The flag @EPF_PENDING@ is - * only set after @conn_init@ returns and @SKF_CONN@ is still set (meaning - * that the connection is still in progress). That's used to let - * @stept_go@ know whether to kill the other endpoint. The flag - * @SKF_BROKEN@ is used to signify an immediate failure. - */ - - conn_init(&e->c, sel, fd, &ga->sa, ga->a.sz, stept_go, e); - if (e->e.f & SKF_BROKEN) { + if (conn_init(&e->c, sel, fd, &ga->sa, ga->a.sz, stept_go, e)) { + fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno)); DESTROY(e); return (0); } - if (e->e.f & SKF_CONN) - e->e.f |= EPF_PENDING; fw_inc(); return (&e->e); } -- 2.11.0