From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18064 invoked by alias); 23 Apr 2014 14:25:46 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 18053 invoked by uid 89); 23 Apr 2014 14:25:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.7 required=5.0 tests=AWL,BAYES_40,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=no version=3.3.2 X-HELO: mout.gmx.net Received: from mout.gmx.net (HELO mout.gmx.net) (212.227.15.19) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 23 Apr 2014 14:25:45 +0000 Received: from mailout-eu.gmx.com ([10.1.101.210]) by mrigmx.server.lan (mrigmx002) with ESMTP (Nemesis) id 0MIBYu-1WdVYB1mow-003sEx for ; Wed, 23 Apr 2014 16:25:41 +0200 Received: (qmail 31819 invoked by uid 0); 23 Apr 2014 14:25:41 -0000 Received: from 108.51.190.55 by rms-eu018.v300.gmx.net with HTTP Content-Type: text/plain; charset="utf-8" Date: Wed, 23 Apr 2014 14:25:00 -0000 From: "qq qq" Message-ID: <20140423142541.156360@gmx.com> MIME-Version: 1.0 Subject: Dup'd sockets lose error information To: cygwin@cygwin.com x-registered: 0 Content-Transfer-Encoding: 8bit X-SW-Source: 2014-04/txt/msg00510.txt.bz2 The following code is a simplified app that was used to test-connect to local ports 55000+ (none of which were actually listening) and received false-positive "connected" results because Cygwin's dup() for socket causes SO_ERROR to be lost.  Since FD_SETSIZE is only 64 on Cygwin, the app uses dup()'s to lower the descriptors as it checks them for completion.  There is no such problem on Linux. Also, strangely that Cygwin does not accept sin_addr as 0 to connect locally (and either localhost or local host IP must be stuffed in there, otherwise resulting in the "Cannot assign requested address" error). $ cat connect.c #include #include #include #include #include #include #include #include #ifndef MAX_SOCK #define MAX_SOCK 100 #endif #ifdef LINUX #undef  FD_SETSIZE #define FD_SETSIZE 16 #endif int main() {   static struct {       struct sockaddr_in sin;       int                sock;   } s[MAX_SOCK];   int i;     for (i = 0;  i < MAX_SOCK;  ++i) {       s[i].sock = socket(AF_INET, SOCK_STREAM, 0);       fcntl(s[i].sock, F_SETFL, O_NONBLOCK);       memset(&s[i].sin, 0, sizeof(s[i].sin));       s[i].sin.sin_family = AF_INET; #ifdef BUG2       s[i].sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); #endif       s[i].sin.sin_port = htons(55000 + i);       connect(s[i].sock, (struct sockaddr*) &s[i].sin, sizeof(s[i].sin));       printf("connecting #%d(%d) to :%hu\n", i, s[i].sock, ntohs(s[i].sin.sin_port));   }   for (;;) {      fd_set wfds, efds;      int m = 0, v = 0;      for (i = 0;  i < MAX_SOCK;  ++i) {          if (s[i].sock < 0)              continue;          if (s[i].sock >= FD_SETSIZE) {              int fd = dup(s[i].sock);              if (fd < 0)                  continue;              if (fd >= FD_SETSIZE) {                  close(fd);                  continue;              }              close(s[i].sock);              printf("%d -> %d\n", s[i].sock, fd);              s[i].sock = fd;          }          if (!m) {              FD_ZERO(&wfds);              FD_ZERO(&efds);          }                    FD_SET(s[i].sock, &wfds);          FD_SET(s[i].sock, &efds);          ++m;          if (v < s[i].sock)              v = s[i].sock;       }       if (!m)          break;       if (select(v + 1, 0, &wfds, &efds, 0) < 0) {          perror("select");          return 1;       }       for (i = 0;  i < MAX_SOCK;  ++i) {          int err;          socklen_t len;                    if (s[i].sock < 0  ||  FD_SETSIZE <= s[i].sock)              continue;                        if (FD_ISSET(s[i].sock, &efds))              v = 1;          else if (FD_ISSET(s[i].sock, &wfds))              v = 0;          else              continue;                        len = sizeof(err);          if (getsockopt(s[i].sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {              perror("getsockopt");              return 1;          }          if (!err)              printf("#%d connected to :%hu%s\n", i, ntohs(s[i].sin.sin_port), v ? " w/exception" : "!");          else              printf("#%d to :%hu: %s\n", i, ntohs(s[i].sin.sin_port), strerror(err));          close(s[i].sock);          s[i].sock = -1;       }   }   return 0; } Linux: $ gcc -Wall -DLINUX connect.c $ ./a.out (shows all connection refused with or without -DBUG2 given to the compiler) Cygwin: $ gcc -Wall -DBUG2 connect.c $ ./a.exe (only shows "connection refused" for fds less than FD_SETSIZE==64, others will be "successfully connected") $ gcc -Wall connect.c (shows "cannot assign requested address" errors for fds < 64, all others "successfully connected") Keep in mind the socket fds are offset by 3 open descriptors used by stdio. Setting MAX_SOCK to 2 and FD_SETSIZE to 1 on Cygwin clearly shows that it's the dup() call that messes up the socket error state: the first socket will get the proper "connection refused", and the other -- will be erroneously reported back as "connected" (no error returned). -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple