1 /*
2 * This file is part of net-yy-inet strace test.
3 *
4 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2018 The strace developers.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "tests.h"
32 #include <assert.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <arpa/inet.h>
41
42 #include "accept_compat.h"
43
44 int
main(void)45 main(void)
46 {
47 skip_if_unavailable("/proc/self/fd/");
48
49 const struct sockaddr_in addr = {
50 .sin_family = AF_INET,
51 .sin_addr.s_addr = htonl(INADDR_LOOPBACK)
52 };
53 struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr));
54 TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
55 *len = sizeof(addr);
56
57 const int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
58 if (listen_fd < 0)
59 perror_msg_and_skip("socket");
60 const unsigned long listen_inode = inode_of_sockfd(listen_fd);
61 printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d<TCP:[%lu]>\n",
62 listen_fd, listen_inode);
63
64 if (bind(listen_fd, listen_sa, *len))
65 perror_msg_and_skip("bind");
66 printf("bind(%d<TCP:[%lu]>, {sa_family=AF_INET, sin_port=htons(0)"
67 ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n",
68 listen_fd, listen_inode, (unsigned) *len);
69
70 if (listen(listen_fd, 1))
71 perror_msg_and_skip("listen");
72 printf("listen(%d<TCP:[%lu]>, 1) = 0\n", listen_fd, listen_inode);
73
74 memset(listen_sa, 0, sizeof(addr));
75 *len = sizeof(addr);
76 if (getsockname(listen_fd, listen_sa, len))
77 perror_msg_and_fail("getsockname");
78 const unsigned int listen_port =
79 ntohs(((struct sockaddr_in *) listen_sa)->sin_port);
80 printf("getsockname(%d<TCP:[127.0.0.1:%u]>, {sa_family=AF_INET"
81 ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}"
82 ", [%u]) = 0\n",
83 listen_fd, listen_port, listen_port, (unsigned) *len);
84
85 TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, optval);
86 *len = sizeof(*optval);
87 if (getsockopt(listen_fd, SOL_TCP, TCP_MAXSEG, optval, len))
88 perror_msg_and_fail("getsockopt");
89 printf("getsockopt(%d<TCP:[127.0.0.1:%u]>, SOL_TCP, TCP_MAXSEG"
90 ", [%u], [%u]) = 0\n",
91 listen_fd, listen_port, *optval, (unsigned) *len);
92
93 const int connect_fd = socket(AF_INET, SOCK_STREAM, 0);
94 if (connect_fd < 0)
95 perror_msg_and_fail("socket");
96 const unsigned long connect_inode = inode_of_sockfd(connect_fd);
97 printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d<TCP:[%lu]>\n",
98 connect_fd, connect_inode);
99
100 *len = sizeof(addr);
101 if (connect(connect_fd, listen_sa, *len))
102 perror_msg_and_fail("connect");
103 printf("connect(%d<TCP:[%lu]>, {sa_family=AF_INET, sin_port=htons(%u)"
104 ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n",
105 connect_fd, connect_inode, listen_port, (unsigned) *len);
106
107 struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
108 memset(accept_sa, 0, sizeof(addr));
109 *len = sizeof(addr);
110 const int accept_fd = do_accept(listen_fd, accept_sa, len);
111 if (accept_fd < 0)
112 perror_msg_and_fail("accept");
113 const unsigned int connect_port =
114 ntohs(((struct sockaddr_in *) accept_sa)->sin_port);
115 printf("accept(%d<TCP:[127.0.0.1:%u]>, {sa_family=AF_INET"
116 ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}"
117 ", [%u]) = %d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>\n",
118 listen_fd, listen_port, connect_port, (unsigned) *len,
119 accept_fd, listen_port, connect_port);
120
121 memset(accept_sa, 0, sizeof(addr));
122 *len = sizeof(addr);
123 if (getpeername(accept_fd, accept_sa, len))
124 perror_msg_and_fail("getpeername");
125 printf("getpeername(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
126 ", {sa_family=AF_INET, sin_port=htons(%u)"
127 ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n",
128 accept_fd, listen_port, connect_port, connect_port,
129 (unsigned) *len);
130
131 memset(listen_sa, 0, sizeof(addr));
132 *len = sizeof(addr);
133 if (getpeername(connect_fd, listen_sa, len))
134 perror_msg_and_fail("getpeername");
135 printf("getpeername(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
136 ", {sa_family=AF_INET, sin_port=htons(%u)"
137 ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n",
138 connect_fd, connect_port, listen_port, listen_port,
139 (unsigned) *len);
140
141 *len = sizeof(*optval);
142 if (setsockopt(connect_fd, SOL_TCP, TCP_MAXSEG, optval, *len))
143 perror_msg_and_fail("setsockopt");
144 printf("setsockopt(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
145 ", SOL_TCP, TCP_MAXSEG, [%u], %u) = 0\n",
146 connect_fd, connect_port, listen_port, *optval,
147 (unsigned) *len);
148
149 char text[] = "text";
150 assert(sendto(connect_fd, text, sizeof(text) - 1,
151 MSG_DONTROUTE | MSG_DONTWAIT, NULL, 0) == sizeof(text) - 1);
152 printf("sendto(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>, \"%s\", %u"
153 ", MSG_DONTROUTE|MSG_DONTWAIT, NULL, 0) = %u\n",
154 connect_fd, connect_port, listen_port, text,
155 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
156
157 assert(close(connect_fd) == 0);
158 printf("close(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>) = 0\n",
159 connect_fd, connect_port, listen_port);
160
161 assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_WAITALL,
162 NULL, NULL) == sizeof(text) - 1);
163 printf("recvfrom(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>, \"%s\", %u"
164 ", MSG_WAITALL, NULL, NULL) = %u\n",
165 accept_fd, listen_port, connect_port, text,
166 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
167
168 assert(close(accept_fd) == 0);
169 printf("close(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>) = 0\n",
170 accept_fd, listen_port, connect_port);
171
172 assert(close(listen_fd) == 0);
173 printf("close(%d<TCP:[127.0.0.1:%u]>) = 0\n",
174 listen_fd, listen_port);
175
176 puts("+++ exited with 0 +++");
177 return 0;
178 }
179