-
-#ifndef SO_REUSEPORT
-#define SO_REUSEPORT SO_REUSEADDR
-#endif
-
-#define TEST_PORT 12345
-
-
-/* Minimal testing infrastructure. */
-
-static int failures;
-
-static void
-failed (const char *reason)
-{
- if (++failures > 1)
- printf (" ");
- printf ("failed (%s)\n", reason);
-}
-
-static int
-test (void (*fn) (void), const char *msg)
-{
- failures = 0;
- printf ("%s... ", msg);
- fflush (stdout);
- fn ();
-
- if (!failures)
- printf ("passed\n");
-
- return failures;
-}
-
-
-/* Funny socket code. */
-
-static int
-open_server_socket ()
-{
- int s, x;
- struct sockaddr_in ia;
-
- s = socket (AF_INET, SOCK_STREAM, 0);
-
- memset (&ia, 0, sizeof (ia));
- ia.sin_family = AF_INET;
- inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
- ia.sin_port = htons (TEST_PORT);
- if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
- {
- perror ("bind");
- exit (77);
- }
-
- x = 1;
- setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
-
- if (listen (s, 1) < 0)
- {
- perror ("listen");
- exit (77);
- }
-
- return s;
-}
-
-static int
-connect_to_socket (int blocking)
-{
- int s;
- struct sockaddr_in ia;
-
- s = socket (AF_INET, SOCK_STREAM, 0);
-
- memset (&ia, 0, sizeof (ia));
- ia.sin_family = AF_INET;
- inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
- ia.sin_port = htons (TEST_PORT);
-
- if (!blocking)
- {
-#ifdef WIN32_NATIVE
- unsigned long iMode = 1;
- ioctl (s, FIONBIO, (char *) &iMode);
-
-#elif defined F_GETFL
- int oldflags = fcntl (s, F_GETFL, NULL);
-
- if (!(oldflags & O_NONBLOCK))
- fcntl (s, F_SETFL, oldflags | O_NONBLOCK);