--------------------- PatchSet 1594 Date: 2001/02/18 00:00:44 Author: hno Branch: eventio Tag: (none) Log: Started coding on ncomm_connect. Creates a new socket to a given service. Members: src/ncomm.c:1.1.2.1->1.1.2.2 Index: squid/src/ncomm.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm.c,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid/src/ncomm.c 17 Feb 2001 23:24:10 -0000 1.1.2.1 +++ squid/src/ncomm.c 18 Feb 2001 00:00:44 -0000 1.1.2.2 @@ -21,12 +21,13 @@ static unsigned int pollfirstfree = 1; /* 0 is skipped to simplify logics */ dlink_list callbacklist; +CBDATA_TYPE(comm_callback_entry_t); static void comm_callback(int fd, void *buf, ssize_t len, int error, int errno_nr, COMMCB *callback, void *data, FREE *freefunc) { - comm_callback_entry_t *cbent = xcalloc(1, sizeof(comm_callback_entry_t)); + comm_callback_entry_t *cbent = CBDATA_ALLOC(comm_callback_entry_t, NULL); cbent->callback = callback; cbent->fd = fd; cbent->buf = buf; @@ -96,7 +97,7 @@ cb->callback(cb->fd, cb->buf, cb->len, cb->error, cb->errno_nr, cb->data); dlinkDelete(&cb->node, &callbacklist); - xfree(cb); + cbdataFree(cb); } } @@ -343,7 +344,7 @@ void ncomm_init(void) { - /* nothing for now */ + CBDATA_INIT_TYPE(comm_callback_entry_t); } @@ -399,3 +400,83 @@ /* Done! */ } + +/* + * 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) +{ + 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. */ + 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 defined(_SQUID_NEWSOS6_) + /* Makoto MATSUSHITA */ + connect(sock, (struct sockaddr *) address, sizeof(*address)); + if (errno == EINVAL) { + errlen = sizeof(err); + x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); + if (x >= 0) + errno = x; + } +#else + errlen = sizeof(err); + x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); + if (x == 0) + errno = err; +#if defined(_SQUID_SOLARIS_) + /* + * Solaris 2.4's socket emulation doesn't allow you + * to determine the error from a failed non-blocking + * connect and just returns EPIPE. Create a fake + * error message for connect. -- fenner@parc.xerox.com + */ + if (x < 0 && errno == EPIPE) + errno = ENOTCONN; +#endif +#endif + } + if (errno == 0 || errno == EISCONN) + status = COMM_OK; + else if (ignoreErrno(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); + } + return status; +} +#endif +