This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Dup'd sockets lose error information


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]