Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/socket-server.h @ 17460:d11431703671
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 09 Aug 2013 08:03:30 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
14590
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Create sockets for use in tests (server side). |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2011-2013 Free Software Foundation, Inc. |
14590
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 (at your option) any later version. |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 #include <stdio.h> |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 #include <sys/socket.h> |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include <netinet/in.h> |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 #include <arpa/inet.h> |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 /* Creates a server that can be used to listen on incoming |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 connections. It uses the IPv4 protocol. |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 If PORT is 0, a port is assigned by the kernel. |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 Returns the server. Returns the chosen port in *PPORT. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 static int |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 create_server (int port, unsigned int max_backlog, int *pport) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 int server; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 /* Create a server socket. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 server = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 if (server < 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 fputs ("Skipping test: cannot create server socket: socket() failed\n", |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 stderr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 exit (77); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 /* Bind it to a local IPv4 address. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 if (port != 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 /* Set an option for the next bind() call: Avoid an EADDRINUSE error |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 in case there are TIME_WAIT or CLOSE_WAIT sockets hanging around on |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 the port. (Sockets in LISTEN or ESTABLISHED state on the same port |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 will still yield an error.) */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 unsigned int flag = 1; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR, &flag, |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 sizeof (flag)) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 < 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 fputs ("Skipping test: cannot create server socket: setsockopt() failed\n", |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 stderr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 exit (77); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 struct sockaddr_in addr; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 memset (&addr, 0, sizeof (addr)); /* needed on AIX and OSF/1 */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 addr.sin_family = AF_INET; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 #if 0 /* Unoptimized */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 inet_pton (AF_INET, "127.0.0.1", &addr.sin_addr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 #elif 0 /* Nearly optimized */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 addr.sin_addr.s_addr = htonl (0x7F000001); /* 127.0.0.1 */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 #else /* Fully optimized */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 unsigned char dotted[4] = { 127, 0, 0, 1 }; /* 127.0.0.1 */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 memcpy (&addr.sin_addr.s_addr, dotted, 4); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 #endif |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 addr.sin_port = htons (port); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 if (bind (server, (const struct sockaddr *) &addr, sizeof (addr)) < 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 fputs ("Skipping test: cannot create server socket: bind() failed\n", |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 stderr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 exit (77); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
81 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
82 if (port == 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
84 /* Get the port that was assigned by bind(). */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 struct sockaddr_in addr; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 socklen_t addrlen = sizeof (addr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
87 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
88 if (getsockname (server, (struct sockaddr *) &addr, &addrlen) < 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 fputs ("Skipping test: cannot create server socket: getsockname() failed\n", |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 stderr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 exit (77); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 port = ntohs (addr.sin_port); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
95 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 /* Start listening for a connection from the child process. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 if (listen (server, max_backlog) < 0) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 fputs ("Skipping test: cannot create server socket: listen() failed\n", |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 stderr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 exit (77); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 *pport = port; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 return server; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
106 } |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
108 /* Creates a server socket, by accepting a connection to a server. */ |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
109 static int |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
110 create_server_socket (int server) |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
111 { |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
112 struct sockaddr_storage addr; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
113 socklen_t addrlen = sizeof (addr); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
114 int connected_socket = accept (server, (struct sockaddr *) &addr, &addrlen); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
115 ASSERT (connected_socket >= 0); |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
116 return connected_socket; |
e0d0a4052520
nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
117 } |