From: Robert W. <wrw...@us...> - 2007-03-15 22:28:34
|
Update of /cvsroot/linuxisns/isnsNT/isnsclient/src In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv16670/isnsclient/src Modified Files: comm.c Log Message: add timeout to socket connection Index: comm.c =================================================================== RCS file: /cvsroot/linuxisns/isnsNT/isnsclient/src/comm.c,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** comm.c 13 Mar 2007 19:34:43 -0000 1.7 --- comm.c 15 Mar 2007 22:28:30 -0000 1.8 *************** *** 46,49 **** --- 46,52 ---- #include "comm.h" #include "parse.h" + #include <fcntl.h> + #include <errno.h> + #include <poll.h> MSG_CB msg_q[MSG_Q_SIZE]; /* TCP msg Q */ *************** *** 80,83 **** --- 83,91 ---- { tcpFlag=t_flag; + int flags = 0; + int rc = 0; + int timeout = 5; // # seconds + struct pollfd fds; + #ifndef SNS_LINUX /* Start up the winsock proprietary Stuff */ *************** *** 104,111 **** if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { ! perror ("Fatal Error while calling socket"); exit(-1); } /* Setup Variables, Addresses, Etc. */ their_addr.sin_family = AF_INET; /* host byte order */ --- 112,123 ---- if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { ! perror ("TCP Fatal Error while calling socket"); exit(-1); } + /* setup socket to nonblocking so we can do a timeout on the connect call */ + flags = fcntl(fd, F_GETFL,0); + fcntl (fd, F_SETFL, flags | O_NONBLOCK); + /* Setup Variables, Addresses, Etc. */ their_addr.sin_family = AF_INET; /* host byte order */ *************** *** 113,125 **** their_addr.sin_addr.s_addr = inet_addr (p_ip); - /* Do a Connect */ printf ("Connecting to %s...\n", p_ip); ! if (connect (fd, (struct sockaddr *) &their_addr, sizeof (their_addr)) ! < 0) { ! printf ("Fatal Error: Connect.\n"); ! exit(-1); } /* Spawn TCP Recv Thread*/ { --- 125,165 ---- their_addr.sin_addr.s_addr = inet_addr (p_ip); printf ("Connecting to %s...\n", p_ip); ! rc = connect (fd, (struct sockaddr *) &their_addr, sizeof (their_addr)); ! if (rc < 0) { ! if (!(errno == EINTR || errno == EINPROGRESS || errno == EWOULDBLOCK)) ! { ! printf ("TCP Fatal Error connecting: %s\n",strerror(errno)); ! exit(-1); ! } ! ! /* because we have nonblocking connection, we have to poll to find out when */ ! /* the connection actually occurs. If it can't connect, then a timeout occurs */ ! fds.fd = fd; ! fds.events = POLLOUT; ! for (;;) { ! fds.revents = 0; ! rc = poll(&fds, 1, timeout * 1000); ! if (rc < 0) ! { ! if (!(errno == EINTR || errno == EAGAIN)) ! break; ! } ! else if (rc == 0) ! { ! printf ("TCP Fatal Error Timeout occured trying to Connect\n"); ! exit(-1); ! } ! else ! /* success we have a connection */ ! if (fds.revents & POLLOUT) ! break; ! } } + /* set socket back to blocking */ + fcntl (fd, F_SETFL, flags ); + /* Spawn TCP Recv Thread*/ { *************** *** 141,145 **** if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { ! perror ("Fatal Error while calling socket"); exit(-1); } --- 181,185 ---- if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { ! perror ("UDP Fatal Error while calling socket"); exit(-1); } *************** *** 151,158 **** /* Do a Bind */ ! if (bind (fd, (struct sockaddr *) &my_addr, sizeof (my_addr)) < 0) { ! printf ("Fatal Error while Binding.\n"); ! exit(-1); } --- 191,199 ---- /* Do a Bind */ ! rc = bind (fd, (struct sockaddr *) &my_addr, sizeof (my_addr)); ! if (rc < 0) { ! printf ("UDP Fatal Error: Bind: %s\n",strerror(errno)); ! exit(-1); } *************** *** 161,164 **** --- 202,209 ---- their_addr.sin_port = htons ((short) isns_port); /* short, network byte order */ their_addr.sin_addr.s_addr = inet_addr (p_ip); + + /* setup socket to nonblocking so we can do a timeout on the connect call */ + flags = fcntl(fd, F_GETFL,0); + fcntl (fd, F_SETFL, flags | O_NONBLOCK); } *************** *** 260,263 **** --- 305,313 ---- { int e; + fd_set read_set; + struct timeval timeout; + int nb; + + if (tcpFlag) { *************** *** 266,270 **** if (e < 0) { ! printf ("Error Sending.\n"); return (-1); } --- 316,320 ---- if (e < 0) { ! printf ("Error Sending TCP request.\n"); return (-1); } *************** *** 273,281 **** else { - /* Setup Variables, Addresses, Etc. */ - their_addr.sin_family = AF_INET; /* host byte order */ - their_addr.sin_port = htons ((short) isns_port); /* short, network byte order */ - their_addr.sin_addr.s_addr = inet_addr (p_ip); - e = sendto (fd, (char *) cmd, len, 0, (struct sockaddr *) &their_addr, sizeof (their_addr)); --- 323,326 ---- *************** *** 283,289 **** if (e < 0) { ! printf ("Error Sending.\n"); return (e); } } --- 328,351 ---- if (e < 0) { ! printf ("Error Sending UDP request.\n"); return (e); } + + /* setup a timer to check for results */ + FD_ZERO(&read_set); + FD_SET(fd, &read_set); + timeout.tv_sec = 5; // seconds + timeout.tv_usec = 0; // microseconds + + nb = select(fd+1, &read_set, NULL, NULL, &timeout); + if (nb < 0 ) + printf ("Error setting up socket\n"); + + if (nb == 0) + { + printf ("Unable to connect - timedout\n"); + exit (-1); + } + } |