From: Eli Zaretskii <eliz@gnu.org>
To: Ken Brown <kbrown@cornell.edu>
Cc: 23615@debbugs.gnu.org
Subject: bug#23615: 25.1.50; Which platforms can safely use getsockopt(,,SO_ERROR,,)?
Date: Sat, 28 May 2016 15:57:00 +0300 [thread overview]
Message-ID: <83twhixrvn.fsf@gnu.org> (raw)
In-Reply-To: <8080c018-3cc8-c97e-0654-dc98be95a7ef@cornell.edu> (message from Ken Brown on Wed, 25 May 2016 15:21:56 -0400)
[-- Attachment #1: Type: text/plain, Size: 1100 bytes --]
> Cc: 23615@debbugs.gnu.org
> From: Ken Brown <kbrown@cornell.edu>
> Date: Wed, 25 May 2016 15:21:56 -0400
>
> On 5/25/2016 12:24 PM, Eli Zaretskii wrote:
> > Perhaps you or someone could write a small test program, and then
> > people here could run it various platforms and provide feedback.
>
> Test program attached. It simulates the situation of bug 23606 (before
> the bug was fixed). Here's what happens on Cygwin:
>
> $ gcc -o socket_test socket_test.c
>
> $ ./socket_test.exe
> Server listening on port 50176.
> Attempting client connection...failure: Connection refused.
With MinGW, I get this instead:
D:\usr\eli\data>socket_test
Server listening on port 2213.
Attempting client connection...success.
Do we have to have a failure in this case? Or is the above a valid
outcome?
Of course, I needed to hack the code quite a lot to get it compile on
MS-Windows; the result is attached below. Maybe I broke the code
while doing that?
(I don't think calling 'connect' after 'listen' is supposed to work;
on Windows it predictably fails with EINVAL, as documented on MSDN.)
[-- Attachment #2: socket_test.c --]
[-- Type: application/octet-stream, Size: 4705 bytes --]
#ifdef __MINGW32__
#define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
#define sleep(s) Sleep(s*1000)
#define SOCK_DESC SOCKET
#include <errno.h>
#ifndef EISCONN
# define EISCONN WSAEISCONN
#endif
#ifndef EINPROGRESS
# define EINPROGRESS WSAEINPROGRESS
#endif
#ifndef EWOULDBLOCK
# define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#else
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SOCK_DESC int
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
/* Return file descriptor or -1. */
SOCK_DESC make_connection (int server, int *pservice);
int
set_nblock (SOCK_DESC s)
{
#ifdef __MINGW32__
unsigned long nblock = 1;
return ioctlsocket (s, FIONBIO, &nblock);
#else
return fcntl (s, F_SETFL, O_NONBLOCK)
#endif
}
void
close_socket (SOCK_DESC s)
{
#ifdef __MINGW32__
closesocket (s);
#else
close (s);
#endif
}
#ifdef __MINGW32__
char *
w32_strerror (int error_no)
{
static char buf[500];
DWORD ret;
if (error_no == 0)
error_no = GetLastError ();
ret = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_no,
0, /* choose most suitable language */
buf, sizeof (buf), NULL);
while (ret > 0 && (buf[ret - 1] == '\n' ||
buf[ret - 1] == '\r' ))
--ret;
buf[ret] = '\0';
if (!ret)
sprintf (buf, "w32 error %u", error_no);
return buf;
}
#endif
int
main(void)
{
SOCK_DESC sfd, cfd;
int xerrno, service;
socklen_t xlen = sizeof (xerrno);
#ifdef __MINGW32__
WSADATA winsockData;
WSAStartup (0x101, &winsockData);
#endif
sfd = make_connection (1, &service);
if (sfd == (SOCK_DESC)-1)
{
fprintf (stderr, "Can't start server.\n");
exit(1);
}
printf ("Server listening on port %d.\n", service);
cfd = make_connection (0, &service);
if (cfd == (SOCK_DESC)-1)
{
fprintf (stderr, "Can't start client.\n");
exit (1);
}
fprintf (stderr, "Attempting client connection...");
/* Here we should use 'select' to wait for cfd to be ready for
writing. To simplify the test, I’m just calling 'sleep'. */
sleep (2);
if (getsockopt (cfd, SOL_SOCKET, SO_ERROR, (char *)&xerrno, &xlen))
{
perror ("getsockopt");
exit (1);
}
if (xerrno)
{
#ifdef __MINGW32__
printf ("failure: %s.\n", w32_strerror (xerrno));
#else
printf ("failure: %s.\n", sterror (xerrno));
#endif
}
else
printf ("success.\n");
exit (0);
}
SOCK_DESC
make_connection (int server, int *pservice)
{
struct addrinfo hints, *result, *rp;
int family, optval, ret;
SOCK_DESC s = (SOCK_DESC)-1;
const char *host;
const char *portstring;
char portbuf[128];
struct sockaddr_in sa;
socklen_t len = sizeof (sa);
int xerrno;
if (server)
/* Assigned port will be returned in pservice. */
{
family = AF_INET;
portstring = "0";
host = "127.0.0.1";
}
else
/* Client; desired port should be specified in pservice. */
{
family = AF_UNSPEC;
portstring = portbuf;
sprintf (portbuf, "%d", *pservice);
host = "localhost";
}
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = family;
if (getaddrinfo (host, portstring, &hints, &result))
return -1;
optval = 1;
for (rp = result; rp; rp = rp->ai_next)
{
s = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (s == (SOCK_DESC)-1)
continue;
if (server)
{
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval,
sizeof(optval))
== -1)
{
perror ("setsockopt");
close_socket (s);
return -1;
}
if (bind (s, rp->ai_addr, rp->ai_addrlen))
{
perror ("bind");
close_socket (s);
return -1;
}
if (listen (s, 5))
{
perror ("listen");
close_socket (s);
return -1;
}
if (getsockname (s, (struct sockaddr *) &sa, &len))
{
perror ("getsockname");
close_socket (s);
return -1;
}
*pservice = ntohs (sa.sin_port);
ret = 0;
}
else
/* Nonblocking client. */
{
if (set_nblock (s) == -1)
{
close_socket (s);
s = -1;
continue;
}
ret = connect (s, rp->ai_addr, rp->ai_addrlen);
}
#ifdef __MINGW32__
xerrno = WSAGetLastError ();
#else
xerrno = errno;
#endif
if (ret == 0 || xerrno == EISCONN)
break;
if (!server && (xerrno == EINPROGRESS || xerrno == EWOULDBLOCK))
break;
close_socket (s);
s = -1;
}
freeaddrinfo(result);
return s;
}
next prev parent reply other threads:[~2016-05-28 12:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-25 0:26 bug#23615: 25.1.50; Which platforms can safely use getsockopt(,,SO_ERROR,,)? Ken Brown
2016-05-25 16:24 ` Eli Zaretskii
2016-05-25 19:21 ` Ken Brown
2016-05-28 12:57 ` Eli Zaretskii [this message]
2016-05-28 17:18 ` Ken Brown
2016-05-28 17:48 ` Eli Zaretskii
2016-06-02 16:00 ` Paul Eggert
2016-06-02 17:17 ` Ken Brown
2016-06-02 17:55 ` Ken Brown
2016-06-02 18:55 ` Ken Brown
2016-06-10 12:38 ` Ken Brown
2016-06-10 17:48 ` Paul Eggert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=83twhixrvn.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=23615@debbugs.gnu.org \
--cc=kbrown@cornell.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).