This source file includes following definitions.
- open_server_socket
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include <sys/socket.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (getsockname, int, (int, struct sockaddr *, socklen_t *));
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "sockets.h"
32 #include "macros.h"
33
34 static int
35 open_server_socket (void)
36 {
37 int s, x;
38 struct sockaddr_in ia;
39
40 s = socket (AF_INET, SOCK_STREAM, 0);
41
42 x = 1;
43 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
44
45 memset (&ia, 0, sizeof (ia));
46 ia.sin_family = AF_INET;
47 inet_pton (AF_INET, "0.0.0.0", &ia.sin_addr);
48
49 ia.sin_port = htons (0);
50 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
51 {
52 perror ("bind");
53 exit (77);
54 }
55
56 if (listen (s, 1) < 0)
57 {
58 perror ("listen");
59 exit (77);
60 }
61
62 return s;
63 }
64
65 int
66 main (void)
67 {
68 (void) gl_sockets_startup (SOCKETS_1_1);
69
70
71 {
72 struct sockaddr_in addr;
73 socklen_t addrlen = sizeof (addr);
74
75 errno = 0;
76 ASSERT (getsockname (-1, (struct sockaddr *) &addr, &addrlen) == -1);
77 ASSERT (errno == EBADF);
78 }
79 {
80 struct sockaddr_in addr;
81 socklen_t addrlen = sizeof (addr);
82
83 close (99);
84 errno = 0;
85 ASSERT (getsockname (99, (struct sockaddr *) &addr, &addrlen) == -1);
86 ASSERT (errno == EBADF);
87 }
88
89
90 {
91 int s = open_server_socket ();
92 struct sockaddr_in addr;
93 socklen_t addrlen = sizeof (addr);
94
95 memset (&addr, 0, sizeof (addr));
96 ASSERT (getsockname (s, (struct sockaddr *) &addr, &addrlen) == 0);
97 ASSERT (addr.sin_family == AF_INET);
98 ASSERT (ntohs (addr.sin_port) != 0);
99 }
100
101 return 0;
102 }