This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Dup'd sockets lose error information
- From: "qq qq" <miska0000 at gmx dot com>
- To: cygwin at cygwin dot com
- Date: Wed, 23 Apr 2014 16:25:40 +0200
- Subject: Dup'd sockets lose error information
- Authentication-results: sourceware.org; auth=none
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 <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#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