This source file includes following definitions.
- create_server
- create_server_socket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <stdio.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23
24
25
26
27
28 static int
29 create_server (int port, unsigned int max_backlog, int *pport)
30 {
31 int server;
32
33
34 server = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
35 if (server < 0)
36 {
37 fputs ("Skipping test: cannot create server socket: socket() failed\n",
38 stderr);
39 exit (77);
40 }
41
42 if (port != 0)
43 {
44
45
46
47
48 unsigned int flag = 1;
49 if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR, &flag,
50 sizeof (flag))
51 < 0)
52 {
53 fputs ("Skipping test: cannot create server socket: setsockopt() failed\n",
54 stderr);
55 exit (77);
56 }
57 }
58 {
59 struct sockaddr_in addr;
60
61 memset (&addr, 0, sizeof (addr));
62 addr.sin_family = AF_INET;
63 #if 0
64 inet_pton (AF_INET, "127.0.0.1", &addr.sin_addr);
65 #elif 0
66 addr.sin_addr.s_addr = htonl (0x7F000001);
67 #else
68 {
69 unsigned char dotted[4] = { 127, 0, 0, 1 };
70 memcpy (&addr.sin_addr.s_addr, dotted, 4);
71 }
72 #endif
73 addr.sin_port = htons (port);
74
75 if (bind (server, (const struct sockaddr *) &addr, sizeof (addr)) < 0)
76 {
77 fputs ("Skipping test: cannot create server socket: bind() failed\n",
78 stderr);
79 exit (77);
80 }
81 }
82 if (port == 0)
83 {
84
85 struct sockaddr_in addr;
86 socklen_t addrlen = sizeof (addr);
87
88 if (getsockname (server, (struct sockaddr *) &addr, &addrlen) < 0)
89 {
90 fputs ("Skipping test: cannot create server socket: getsockname() failed\n",
91 stderr);
92 exit (77);
93 }
94 port = ntohs (addr.sin_port);
95 }
96
97 if (listen (server, max_backlog) < 0)
98 {
99 fputs ("Skipping test: cannot create server socket: listen() failed\n",
100 stderr);
101 exit (77);
102 }
103
104 *pport = port;
105 return server;
106 }
107
108
109 static int
110 create_server_socket (int server)
111 {
112 struct sockaddr_storage addr;
113 socklen_t addrlen = sizeof (addr);
114 int connected_socket = accept (server, (struct sockaddr *) &addr, &addrlen);
115 ASSERT (connected_socket >= 0);
116 return connected_socket;
117 }