]> granicus.if.org Git - strace/blob - tests/net-yy-inet.c
Fix a few spacing style issues
[strace] / tests / net-yy-inet.c
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-2017 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 int
43 main(void)
44 {
45         skip_if_unavailable("/proc/self/fd/");
46
47         const struct sockaddr_in addr = {
48                 .sin_family = AF_INET,
49                 .sin_addr.s_addr = htonl(INADDR_LOOPBACK)
50         };
51         struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr));
52         TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
53         *len = sizeof(addr);
54
55         const int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
56         if (listen_fd < 0)
57                 perror_msg_and_skip("socket");
58         const unsigned long listen_inode = inode_of_sockfd(listen_fd);
59         printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d<TCP:[%lu]>\n",
60                listen_fd, listen_inode);
61
62         if (bind(listen_fd, listen_sa, *len))
63                 perror_msg_and_skip("bind");
64         printf("bind(%d<TCP:[%lu]>, {sa_family=AF_INET, sin_port=htons(0)"
65                ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n",
66                listen_fd, listen_inode, (unsigned) *len);
67
68         if (listen(listen_fd, 1))
69                 perror_msg_and_skip("listen");
70         printf("listen(%d<TCP:[%lu]>, 1) = 0\n", listen_fd, listen_inode);
71
72         memset(listen_sa, 0, sizeof(addr));
73         *len = sizeof(addr);
74         if (getsockname(listen_fd, listen_sa, len))
75                 perror_msg_and_fail("getsockname");
76         const unsigned int listen_port =
77                 ntohs(((struct sockaddr_in *) listen_sa)->sin_port);
78         printf("getsockname(%d<TCP:[127.0.0.1:%u]>, {sa_family=AF_INET"
79                ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}"
80                ", [%u]) = 0\n",
81                listen_fd, listen_port, listen_port, (unsigned) *len);
82
83         TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, optval);
84         *len = sizeof(*optval);
85         if (getsockopt(listen_fd, SOL_TCP, TCP_MAXSEG, optval, len))
86                 perror_msg_and_fail("getsockopt");
87         printf("getsockopt(%d<TCP:[127.0.0.1:%u]>, SOL_TCP, TCP_MAXSEG"
88                ", [%u], [%u]) = 0\n",
89                listen_fd, listen_port, *optval, (unsigned) *len);
90
91         const int connect_fd = socket(AF_INET, SOCK_STREAM, 0);
92         if (connect_fd < 0)
93                 perror_msg_and_fail("socket");
94         const unsigned long connect_inode = inode_of_sockfd(connect_fd);
95         printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d<TCP:[%lu]>\n",
96                connect_fd, connect_inode);
97
98         *len = sizeof(addr);
99         if (connect(connect_fd, listen_sa, *len))
100                 perror_msg_and_fail("connect");
101         printf("connect(%d<TCP:[%lu]>, {sa_family=AF_INET, sin_port=htons(%u)"
102                ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n",
103                connect_fd, connect_inode, listen_port, (unsigned) *len);
104
105         struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
106         memset(accept_sa, 0, sizeof(addr));
107         *len = sizeof(addr);
108         const int accept_fd = accept(listen_fd, accept_sa, len);
109         if (accept_fd < 0)
110                 perror_msg_and_fail("accept");
111         const unsigned int connect_port =
112                 ntohs(((struct sockaddr_in *) accept_sa)->sin_port);
113         printf("accept(%d<TCP:[127.0.0.1:%u]>, {sa_family=AF_INET"
114                ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}"
115                ", [%u]) = %d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>\n",
116                listen_fd, listen_port, connect_port, (unsigned) *len,
117                accept_fd, listen_port, connect_port);
118
119         memset(accept_sa, 0, sizeof(addr));
120         *len = sizeof(addr);
121         if (getpeername(accept_fd, accept_sa, len))
122                 perror_msg_and_fail("getpeername");
123         printf("getpeername(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
124                ", {sa_family=AF_INET, sin_port=htons(%u)"
125                ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n",
126                accept_fd, listen_port, connect_port, connect_port,
127                (unsigned) *len);
128
129         memset(listen_sa, 0, sizeof(addr));
130         *len = sizeof(addr);
131         if (getpeername(connect_fd, listen_sa, len))
132                 perror_msg_and_fail("getpeername");
133         printf("getpeername(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
134                ", {sa_family=AF_INET, sin_port=htons(%u)"
135                ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n",
136                connect_fd, connect_port, listen_port, listen_port,
137                (unsigned) *len);
138
139         *len = sizeof(*optval);
140         if (setsockopt(connect_fd, SOL_TCP, TCP_MAXSEG, optval, *len))
141                 perror_msg_and_fail("setsockopt");
142         printf("setsockopt(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>"
143                ", SOL_TCP, TCP_MAXSEG, [%u], %u) = 0\n",
144                connect_fd, connect_port, listen_port, *optval,
145                (unsigned) *len);
146
147         char text[] = "text";
148         assert(sendto(connect_fd, text, sizeof(text) - 1,
149                MSG_DONTROUTE | MSG_DONTWAIT, NULL, 0) == sizeof(text) - 1);
150         printf("sendto(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>, \"%s\", %u"
151                ", MSG_DONTROUTE|MSG_DONTWAIT, NULL, 0) = %u\n",
152                connect_fd, connect_port, listen_port, text,
153                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
154
155         assert(close(connect_fd) == 0);
156         printf("close(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>) = 0\n",
157                connect_fd, connect_port, listen_port);
158
159         assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_WAITALL,
160                         NULL, NULL) == sizeof(text) - 1);
161         printf("recvfrom(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>, \"%s\", %u"
162                ", MSG_WAITALL, NULL, NULL) = %u\n",
163                accept_fd, listen_port, connect_port, text,
164                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
165
166         assert(close(accept_fd) == 0);
167         printf("close(%d<TCP:[127.0.0.1:%u->127.0.0.1:%u]>) = 0\n",
168                accept_fd, listen_port, connect_port);
169
170         assert(close(listen_fd) == 0);
171         printf("close(%d<TCP:[127.0.0.1:%u]>) = 0\n",
172                listen_fd, listen_port);
173
174         puts("+++ exited with 0 +++");
175         return 0;
176 }