From: Ken Brown <kbrown@cornell.edu>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 23615@debbugs.gnu.org
Subject: bug#23615: 25.1.50; Which platforms can safely use getsockopt(,,SO_ERROR,,)?
Date: Wed, 25 May 2016 15:21:56 -0400 [thread overview]
Message-ID: <8080c018-3cc8-c97e-0654-dc98be95a7ef@cornell.edu> (raw)
In-Reply-To: <8337p63y2a.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 459 bytes --]
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.
Ken
[-- Attachment #2: socket_test.c --]
[-- Type: text/plain, Size: 3032 bytes --]
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
/* Return file descriptor or -1. */
int make_connection (int server, int *pservice);
int
main(void)
{
int sfd, cfd, xerrno, service;
socklen_t xlen = sizeof (xerrno);
sfd = make_connection (1, &service);
if (sfd == -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 == -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, &xerrno, &xlen))
{
perror ("getsockopt");
exit (1);
}
if (xerrno)
printf ("failure: %s.\n", strerror (xerrno));
else
printf ("success.\n");
exit (0);
}
int
make_connection (int server, int *pservice)
{
struct addrinfo hints, *result, *rp;
int family, optval, ret, s = -1;
const char *host;
const char *portstring;
char portbuf[128];
struct sockaddr_in sa;
socklen_t len = sizeof (sa);
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 == -1)
continue;
if (server)
{
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))
== -1)
{
perror ("setsockopt");
close (s);
return -1;
}
if (bind (s, rp->ai_addr, rp->ai_addrlen))
{
perror ("bind");
close (s);
return -1;
}
if (listen (s, 5))
{
perror ("listen");
close (s);
return -1;
}
if (getsockname (s, (struct sockaddr *) &sa, &len))
{
perror ("getsockname");
close (s);
return -1;
}
*pservice = ntohs (sa.sin_port);
}
else
/* Nonblocking client. */
{
if (fcntl (s, F_SETFL, O_NONBLOCK) == -1)
{
close (s);
s = -1;
continue;
}
}
ret = connect (s, rp->ai_addr, rp->ai_addrlen);
if (ret == 0 || errno == EISCONN)
break;
if (!server && errno == EINPROGRESS)
break;
close (s);
s = -1;
}
freeaddrinfo(result);
return s;
}
next prev parent reply other threads:[~2016-05-25 19:21 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 [this message]
2016-05-28 12:57 ` Eli Zaretskii
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=8080c018-3cc8-c97e-0654-dc98be95a7ef@cornell.edu \
--to=kbrown@cornell.edu \
--cc=23615@debbugs.gnu.org \
--cc=eliz@gnu.org \
/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).