From: Art Cancro Date: Thu, 3 Jun 2010 15:55:24 +0000 (+0000) Subject: * Simplified tcp_connectsock() X-Git-Tag: v7.86~174 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=71a70498e364a7643e2021513badc0aee432cfe7 * Simplified tcp_connectsock() --- diff --git a/webcit/serv_func.c b/webcit/serv_func.c index 32778e275..50bf1f771 100644 --- a/webcit/serv_func.c +++ b/webcit/serv_func.c @@ -168,7 +168,7 @@ int GetConnected (void) if (is_uds) /* unix domain socket */ WCC->serv_sock = uds_connectsock(serv_sock_name); else /* tcp socket */ - WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport); + WCC->serv_sock = tcp_connectsock(ctdlhost, atoi(ctdlport)); if (WCC->serv_sock < 0) { do_logout(); diff --git a/webcit/tcp_sockets.c b/webcit/tcp_sockets.c index b4197e5d9..ba4cac556 100644 --- a/webcit/tcp_sockets.c +++ b/webcit/tcp_sockets.c @@ -41,8 +41,7 @@ RETSIGTYPE timeout(int signum) /* - * Connect a unix domain socket - * sockpath where to open a unix domain socket + * Client side - connect to a unix domain socket */ int uds_connectsock(char *sockpath) { @@ -55,16 +54,12 @@ int uds_connectsock(char *sockpath) s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { - lprintf(1, "Can't create socket[%s]: %s\n", - sockpath, - strerror(errno)); + lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno)); return(-1); } if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - lprintf(1, "Can't connect [%s]: %s\n", - sockpath, - strerror(errno)); + lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno)); close(s); return(-1); } @@ -74,80 +69,37 @@ int uds_connectsock(char *sockpath) /* - * Connect a TCP/IP socket - * host the host to connect to - * service the service on the host to call + * TCP client - connect to a host/port */ -int tcp_connectsock(char *host, char *service) +int tcp_connectsock(char *host, int port) { - int fdflags; - struct hostent *phe; - struct servent *pse; - struct protoent *ppe; - struct sockaddr_in sin; - int s; - - memset(&sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; + struct sockaddr_in stSockAddr; + int rv; + int sock; - pse = getservbyname(service, "tcp"); - if (pse) { - sin.sin_port = pse->s_port; - } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) { - lprintf(1, "Can't get %s service entry\n", service); - return (-1); - } - phe = gethostbyname(host); - if (phe) { - memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); - } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) { - lprintf(1, "Can't get %s host entry: %s\n", - host, strerror(errno)); - return (-1); - } - if ((ppe = getprotobyname("tcp")) == 0) { - lprintf(1, "Can't get TCP protocol entry: %s\n", - strerror(errno)); - return (-1); - } - - s = socket(PF_INET, SOCK_STREAM, ppe->p_proto); - if (s < 0) { + sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock < 0) { lprintf(1, "Can't create socket: %s\n", strerror(errno)); return (-1); } - fdflags = fcntl(s, F_GETFL); - if (fdflags < 0) - lprintf(1, "unable to get socket flags! %s.%s: %s \n", - host, service, strerror(errno)); - fdflags = fdflags | O_NONBLOCK; - if (fcntl(s, F_SETFD, fdflags) < 0) - lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n", - host, service, strerror(errno)); - - signal(SIGALRM, timeout); - alarm(30); + memset(&stSockAddr, 0, sizeof(struct sockaddr_in)); + stSockAddr.sin_family = AF_INET; + stSockAddr.sin_port = htons(port); + rv = inet_pton(AF_INET, host, &stSockAddr.sin_addr); - if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { - lprintf(1, "Can't connect to %s.%s: %s\n", - host, service, strerror(errno)); - close(s); + if (rv <= 0) { + lprintf(1, "Can't grok %s: %s\n", host, strerror(errno)); return (-1); } - alarm(0); - signal(SIGALRM, SIG_IGN); - if (!is_https) { - fdflags = fcntl(s, F_GETFL); - if (fdflags < 0) - lprintf(1, "unable to get socket flags! %s.%s: %s \n", - host, service, strerror(errno)); - fdflags = fdflags | O_NONBLOCK; - if (fcntl(s, F_SETFD, fdflags) < 0) - lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n", - host, service, strerror(errno)); + + if (connect(sock, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)) != 0) { + lprintf(1, "Can't connect to %s.%d: %s\n", host, port, strerror(errno)); + close(sock); + return (-1); } - return (s); + + return (sock); } diff --git a/webcit/webcit.h b/webcit/webcit.h index 4e9442f44..2e53e3098 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -652,7 +652,7 @@ void CreateMimeStr(void); int GetConnected(void); void DeleteServInfo(ServInfo **FreeMe); int uds_connectsock(char *); -int tcp_connectsock(char *, char *); +int tcp_connectsock(char *, int); int serv_getln(char *strbuf, int bufsize); int StrBuf_ServGetln(StrBuf *buf); int GetServerStatus(StrBuf *Line, long* FullState);