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