Skip to content

Instantly share code, notes, and snippets.

@rudimeier
Created February 5, 2012 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudimeier/1746651 to your computer and use it in GitHub Desktop.
Save rudimeier/1746651 to your computer and use it in GitHub Desktop.
wine bug 9425 test
#include <winsock2.h>
#include <stdio.h>
int main(int argc, char** argv)
{
SOCKET fd;
WSADATA data;
if( WSAStartup( MAKEWORD(2, 2), &data) != 0 ) {
return -1;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if( fd == INVALID_SOCKET ) {
return -1;
}
/* set non block */
unsigned long mode = 1;
if( ioctlsocket(fd, FIONBIO, &mode) == SOCKET_ERROR ) {
return -1;
}
{
int rval;
struct timeval select_timeout;
struct sockaddr_in sa;
select_timeout.tv_sec=5;
select_timeout.tv_usec=0;
/* assuming this ip:port rejects any tcp connection */
memset( &sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons(54321);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
/* try to connect */
rval = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
if( rval == SOCKET_ERROR ) {
if( WSAGetLastError() == WSAEWOULDBLOCK ) {
/* this is the expected case - now wait for "connection refused" */
fd_set writefds, exceptfds;
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(fd, &writefds);
FD_SET(fd, &exceptfds);
rval = select((int) fd+1, NULL, &writefds, &exceptfds, &select_timeout);
if( rval == SOCKET_ERROR ) {
return -1;
} else if( rval == 0 ) {
printf("wrong, select says 'timeout' but we know we were rejected\n");
} {
int w = FD_ISSET(fd, &writefds);
int e = FD_ISSET(fd, &exceptfds);
if( !w && e ) {
printf("correct (%d, %d), now getsockopt whould tell us "
"about 'connection refused(10061)'\n", w, e);
} else {
printf("wrong, want (0, 1) have (%d, %d), probably even getsockopt "
" doesn't know what happend here\n", w, e);
}
}
return 0;
}
}
/* should not happen - test *ucked up */
return -1;
}
}
@rudimeier
Copy link
Author

Here a test which should describe what is expected with non blocking sockets and refusing tcp connection in real life on Windows.
See wine bug here:
http://bugs.winehq.org/show_bug.cgi?id=9425

NOTE it seems to be a race. If you use a remote IP then wine does it correctly sometimes. But getsockopt will fail anyway which is probably another bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment