--------------------- PatchSet 1654 Date: 2001/02/25 12:22:05 Author: hno Branch: eventio Tag: (none) Log: ncomm_connect implemented addrsize added to COMMNEWCB Members: doc/Programming-Guide/prog-guide.sgml:1.9.8.8->1.9.8.9 src/ncomm.c:1.1.2.7->1.1.2.8 src/ncomm_internals.h:1.1.2.2->1.1.2.3 src/ncomm_test.c:1.1.2.2->1.1.2.3 src/typedefs.h:1.15.8.6->1.15.8.7 Index: squid/doc/Programming-Guide/prog-guide.sgml =================================================================== RCS file: /cvsroot/squid-sf//squid/doc/Programming-Guide/prog-guide.sgml,v retrieving revision 1.9.8.8 retrieving revision 1.9.8.9 diff -u -r1.9.8.8 -r1.9.8.9 --- squid/doc/Programming-Guide/prog-guide.sgml 25 Feb 2001 07:20:32 -0000 1.9.8.8 +++ squid/doc/Programming-Guide/prog-guide.sgml 25 Feb 2001 12:22:05 -0000 1.9.8.9 @@ -2,7 +2,7 @@
Squid Programmers Guide Duane Wessels, Squid Developers -$Id: prog-guide.sgml,v 1.9.8.8 2001/02/25 07:20:32 hno Exp $ +$Id: prog-guide.sgml,v 1.9.8.9 2001/02/25 12:22:05 hno Exp $ Squid is a WWW Cache application developed by the National Laboratory @@ -3175,7 +3175,7 @@

typedef void COMMNEWCB(filehandle *fh, int error, struct sockaddr *local, - struct sockaddr *peer, void *cbdata); + struct sockaddr *peer, int addrsize, void *cbdata);

Index: squid/src/ncomm.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm.c,v retrieving revision 1.1.2.7 retrieving revision 1.1.2.8 diff -u -r1.1.2.7 -r1.1.2.8 --- squid/src/ncomm.c 25 Feb 2001 01:15:32 -0000 1.1.2.7 +++ squid/src/ncomm.c 25 Feb 2001 12:22:06 -0000 1.1.2.8 @@ -153,7 +153,7 @@ break; case COMMCB_new: if (valid) - cb->cb.new.callback(fh, cb->error, &fh->local, &fh->peer, cb->cbdata); + cb->cb.new.callback(fh, cb->error, &fh->local, &fh->peer, fh->addrsize, cb->cbdata); /* NOT YET DONE */ break; case COMMCB_close: @@ -361,8 +361,8 @@ filehandle *nfh; int nfd; struct sockaddr saddr; - socklen_t addrlen = sizeof(saddr); - nfd = accept(fd, &saddr, &addrlen); + socklen_t addrsize = sizeof(saddr); + nfd = accept(fd, &saddr, &addrsize); statCounter.syscalls.sock.accepts++; if (nfd < 0) break; @@ -374,9 +374,13 @@ fh_open(nfh, "Incoming connection"); /* Update the socket information */ - addrlen = sizeof(struct sockaddr); - getsockname(nfd, (struct sockaddr *) &nfh->local, &addrlen); - nfh->peer = saddr; + memcpy(&nfh->peer, &saddr, addrsize); + nfh->addrsize = addrsize; + + addrsize = sizeof(nfh->local); + getsockname(nfd, (struct sockaddr *) &nfh->local, &addrsize); + if (addrsize < nfh->addrsize) + nfh->addrsize = addrsize; /* Set the flags */ ncommSetCloseOnExec(nfd); @@ -446,54 +450,55 @@ { } +int +ncomm_close(filehandle *fh) +{ + /* NOT YET IMPLEMENTED */ + return 1; +} -/* - * Open a new socket to a given service - */ -#if NOT_YET_READY int -ncomm_connect(int sock_type, int proto, const struct sockaddr *local, const struct sockaddr *remote, int addrsize, COMMCB *callback, void *data) +ncomm_abort(filehandle *fh) +{ + /* NOT YET IMPLEMENTED */ + return 1; +} + +filehandle * +ncomm_accept(int sock_type, int proto, struct sockaddr *where, + struct sockaddr *from, int addrsize, + COMMNEWCB callback, void *cbdata) +{ + /* NOT YET IMPLEMENTED */ + return NULL; +} + +static int +comm_do_connect(filehandle *fh) { - int status = COMM_OK; - int sock; int x; - int err = 0; - socklen_t errlen; - assert(remote); - assert(ntohs(remote->sin_port) != 0); - statCounter.syscalls.sock.socket++; - sock = socket(remote.sin_family, sock_type, proto); - if (sock == -1) [ - debug(5, 1) ("comm_connet: Cannot create new socket: %s\n", xstrerror()); - return 1; - } - /* Bind to local endpoint */ - if (local) { - statCounter.syscalls.sock.bind++; - x = bind(sock, local, addrsize); - if (x != 0) - debug(5, 1) ("comm_connect: Failed to bind local endpoint: %s\n", xstrerror()); - } - /* Establish connection. */ + int done = 0; + int err; + int errlen = sizeof(err); + x = connect(fh->fd, &fh->peer, sizeof(fh->peer)); statCounter.syscalls.sock.connects++; - x = connect(sock, (struct sockaddr *) remote, addrsize); if (x == 0) { errno = 0; - } else if (x < 0) - debug(5, 9) ("connect FD %d: %s\n", sock, xstrerror()); + } else if (x < 0) { + debug(5, 9) ("connect FD %d: %s\n", fh->fd, xstrerror()); } else { #if defined(_SQUID_NEWSOS6_) /* Makoto MATSUSHITA */ - connect(sock, (struct sockaddr *) address, sizeof(*address)); + x = connect(fh->fd, &fh->peer, sizeof(fh->peer)); if (errno == EINVAL) { errlen = sizeof(err); - x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); + x = getsockopt(fh->fd, SOL_SOCKET, SO_ERROR, &err, &errlen); if (x >= 0) errno = x; } #else errlen = sizeof(err); - x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); + x = getsockopt(fh->fd, SOL_SOCKET, SO_ERROR, &err, &errlen); if (x == 0) errno = err; #if defined(_SQUID_SOLARIS_) @@ -508,21 +513,68 @@ #endif #endif } - if (errno == 0 || errno == EISCONN) - status = COMM_OK; - else if (ncommIgnoreErrno(errno)) - status = COMM_INPROGRESS; - else - return COMM_ERROR; - xstrncpy(F->ipaddr, inet_ntoa(address->sin_addr), 16); - F->remote_port = ntohs(address->sin_port); - if (status == COMM_OK) { - debug(5, 10) ("comm_connect_addr: FD %d connected to %s:%d\n", - sock, F->ipaddr, F->remote_port); - } else if (status == COMM_INPROGRESS) { - debug(5, 10) ("comm_connect_addr: FD %d connection pending\n", sock); + if (errno == 0 || errno == EISCONN) { + errno = 0; + done = 1; + } else if (!ncommIgnoreErrno(errno)) { + done = 1; + } + if (done) { + socklen_t addrsize = sizeof(fh->local); + getsockname(fh->fd, &fh->local, &addrsize); + if (addrsize < fh->addrsize) + fh->addrsize = addrsize; } - return status; + return done; +} + +/* + * Open a new socket to a given service + */ +filehandle * +ncomm_connect(int sock_type, int proto, const struct sockaddr *local, const struct sockaddr *remote, int addrsize, COMMNEWCB *callback, void *cbdata) +{ + filehandle *fh; + int sock; + int x; + int done; + assert(remote); + sock = socket(remote->sa_family, sock_type, proto); + statCounter.syscalls.sock.sockets++; + if (sock == -1) { + debug(5, 1) ("comm_connet: Cannot create new socket: %s\n", xstrerror()); + return NULL; + } + /* Bind to local endpoint */ + if (local) { + x = bind(sock, local, addrsize); + statCounter.syscalls.sock.binds++; + if (x != 0) + debug(5, 1) ("comm_connect: WARNING: Failed to bind local endpoint: %s\n", xstrerror()); + } + /* set up filehandle */ + fh = cbdataAlloc(filehandle); + fh->fd = sock; + if (local) + memcpy(&fh->local, local, addrsize); + memcpy(&fh->peer, remote, addrsize); + fh->addrsize = addrsize; + fh->peer = *remote; + fh->connect.callback = callback; + fh->connect.cbdata = cbdataReference(cbdata); + + /* Establish connection. */ + done = comm_do_connect(fh); + + if (!done) + comm_register_for_write_event(fh, comm_do_connect); + return fh; +} + +int +ncomm_add_close_handler(filehandle *fh, COMMCLOSECB *handler, void *cbdata) +{ + /* NOT YET IMPLEMENTED */ + return 1; } -#endif Index: squid/src/ncomm_internals.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm_internals.h,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- squid/src/ncomm_internals.h 24 Feb 2001 23:48:43 -0000 1.1.2.2 +++ squid/src/ncomm_internals.h 25 Feb 2001 12:22:06 -0000 1.1.2.3 @@ -41,6 +41,7 @@ fh_write_t *write; fh_connect_t connect; void *eventdata; + socklen_t addrsize; struct sockaddr peer; struct sockaddr local; COMMEVENTREAD *read_handler; Index: squid/src/ncomm_test.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm_test.c,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- squid/src/ncomm_test.c 25 Feb 2001 01:15:32 -0000 1.1.2.2 +++ squid/src/ncomm_test.c 25 Feb 2001 12:22:06 -0000 1.1.2.3 @@ -11,13 +11,14 @@ abort(); } -void +static void got_data(filehandle *fh, IOBuf *buf, int offset, int len, int error, void *data) { printf("Got %d bytes\n", len); ncomm_read(fh, 65536, got_data, NULL); } -void + +static void new_connection(filehandle *fh, int error, struct sockaddr *local, struct sockaddr *remote, void *cbdata) { printf("New connection!\n"); Index: squid/src/typedefs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/typedefs.h,v retrieving revision 1.15.8.6 retrieving revision 1.15.8.7 diff -u -r1.15.8.6 -r1.15.8.7 --- squid/src/typedefs.h 25 Feb 2001 01:15:32 -0000 1.15.8.6 +++ squid/src/typedefs.h 25 Feb 2001 12:22:06 -0000 1.15.8.7 @@ -1,6 +1,6 @@ /* - * $Id: typedefs.h,v 1.15.8.6 2001/02/25 01:15:32 hno Exp $ + * $Id: typedefs.h,v 1.15.8.7 2001/02/25 12:22:06 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -357,7 +357,7 @@ * ncomm.c */ typedef MemBuf IOBuf; -typedef void COMMNEWCB(filehandle *fh, int error, struct sockaddr *local, struct sockaddr *peer, void *cbdata); +typedef void COMMNEWCB(filehandle *fh, int error, struct sockaddr *local, struct sockaddr *peer, int addrsize, void *cbdata); typedef void COMMIOCB(filehandle *fh, IOBuf *buf, int offset, int size, int error, void *cbdata); typedef void COMMCLOSECB(filehandle *fh, void *cbdata);