1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup nettest
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Networking test support functions implementation.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <net/socket.h>
|
---|
39 |
|
---|
40 | #include "nettest.h"
|
---|
41 | #include "print_error.h"
|
---|
42 |
|
---|
43 | /** Creates new sockets.
|
---|
44 | *
|
---|
45 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
46 | * @param[out] socket_ids A field to store the socket identifiers.
|
---|
47 | * @param[in] sockets The number of sockets to create. Should be at most the size of the field.
|
---|
48 | * @param[in] family The socket address family.
|
---|
49 | * @param[in] type The socket type.
|
---|
50 | * @return EOK on success.
|
---|
51 | * @return Other error codes as defined for the socket() function.
|
---|
52 | */
|
---|
53 | int sockets_create(int verbose, int *socket_ids, unsigned int sockets,
|
---|
54 | uint16_t family, sock_type_t type)
|
---|
55 | {
|
---|
56 | if (verbose)
|
---|
57 | printf("Create\t");
|
---|
58 |
|
---|
59 | fflush(stdout);
|
---|
60 |
|
---|
61 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
62 | socket_ids[index] = socket(family, type, 0);
|
---|
63 | if (socket_ids[index] < 0) {
|
---|
64 | printf("Socket %u (%d) error:\n", index, socket_ids[index]);
|
---|
65 | socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
|
---|
66 | return socket_ids[index];
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (verbose)
|
---|
70 | print_mark(index);
|
---|
71 | }
|
---|
72 |
|
---|
73 | return EOK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Closes sockets.
|
---|
77 | *
|
---|
78 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
79 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
80 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
81 | * @return EOK on success.
|
---|
82 | * @return Other error codes as defined for the closesocket() function.
|
---|
83 | */
|
---|
84 | int sockets_close(int verbose, int *socket_ids, unsigned int sockets)
|
---|
85 | {
|
---|
86 | if (verbose)
|
---|
87 | printf("\tClose\t");
|
---|
88 |
|
---|
89 | fflush(stdout);
|
---|
90 |
|
---|
91 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
92 | int rc = closesocket(socket_ids[index]);
|
---|
93 | if (rc != EOK) {
|
---|
94 | printf("Socket %u (%d) error:\n", index, socket_ids[index]);
|
---|
95 | socket_print_error(stderr, rc, "Socket close: ", "\n");
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (verbose)
|
---|
100 | print_mark(index);
|
---|
101 | }
|
---|
102 |
|
---|
103 | return EOK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Connects sockets.
|
---|
107 | *
|
---|
108 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
109 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
110 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
111 | * @param[in] address The destination host address to connect to.
|
---|
112 | * @param[in] addrlen The length of the destination address in bytes.
|
---|
113 | * @return EOK on success.
|
---|
114 | * @return Other error codes as defined for the connect() function.
|
---|
115 | */
|
---|
116 | int sockets_connect(int verbose, int *socket_ids, unsigned int sockets,
|
---|
117 | struct sockaddr *address, socklen_t addrlen)
|
---|
118 | {
|
---|
119 | if (verbose)
|
---|
120 | printf("\tConnect\t");
|
---|
121 |
|
---|
122 | fflush(stdout);
|
---|
123 |
|
---|
124 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
125 | int rc = connect(socket_ids[index], address, addrlen);
|
---|
126 | if (rc != EOK) {
|
---|
127 | socket_print_error(stderr, rc, "Socket connect: ", "\n");
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (verbose)
|
---|
132 | print_mark(index);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return EOK;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Send data via sockets.
|
---|
139 | *
|
---|
140 | * @param[in] verbose Print out verbose information.
|
---|
141 | * @param[in] socket_ids Stored socket identifiers.
|
---|
142 | * @param[in] sockets Number of sockets.
|
---|
143 | * @param[in] address Destination host address to send data to.
|
---|
144 | * @param[in] addrlen Length of the destination address in bytes.
|
---|
145 | * @param[in] data Data to be sent.
|
---|
146 | * @param[in] size Size of the data in bytes.
|
---|
147 | * @param[in] messages Number of datagrams per socket to be sent.
|
---|
148 | * @param[in] type Socket type.
|
---|
149 | *
|
---|
150 | * @return EOK on success.
|
---|
151 | * @return Other error codes as defined for the sendto() function.
|
---|
152 | *
|
---|
153 | */
|
---|
154 | int sockets_sendto(int verbose, int *socket_ids, unsigned int sockets,
|
---|
155 | struct sockaddr *address, socklen_t addrlen, char *data, size_t size,
|
---|
156 | unsigned int messages, sock_type_t type)
|
---|
157 | {
|
---|
158 | if (verbose)
|
---|
159 | printf("\tSendto\t");
|
---|
160 |
|
---|
161 | fflush(stdout);
|
---|
162 |
|
---|
163 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
164 | for (unsigned int message = 0; message < messages; message++) {
|
---|
165 | int rc;
|
---|
166 |
|
---|
167 | switch (type) {
|
---|
168 | case SOCK_STREAM:
|
---|
169 | rc = send(socket_ids[index], data, size, 0);
|
---|
170 | break;
|
---|
171 | case SOCK_DGRAM:
|
---|
172 | rc = sendto(socket_ids[index], data, size, 0, address, addrlen);
|
---|
173 | break;
|
---|
174 | default:
|
---|
175 | rc = EINVAL;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (rc != EOK) {
|
---|
179 | printf("Socket %u (%d), message %u error:\n",
|
---|
180 | index, socket_ids[index], message);
|
---|
181 | socket_print_error(stderr, rc, "Socket send: ", "\n");
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (verbose)
|
---|
187 | print_mark(index);
|
---|
188 | }
|
---|
189 |
|
---|
190 | return EOK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Receive data via sockets.
|
---|
194 | *
|
---|
195 | * @param[in] verbose Print out verbose information.
|
---|
196 | * @param[in] socket_ids Stored socket identifiers.
|
---|
197 | * @param[in] sockets Number of sockets.
|
---|
198 | * @param[in] address Source host address of received datagrams.
|
---|
199 | * @param[in,out] addrlen Maximum length of the source address in bytes.
|
---|
200 | * The actual size of the source address is set.
|
---|
201 | * @param[out] data Received data.
|
---|
202 | * @param[in] size Maximum data size in bytes.
|
---|
203 | * @param[in] messages Number of datagrams per socket to be received.
|
---|
204 | *
|
---|
205 | * @return EOK on success.
|
---|
206 | * @return Other error codes as defined for the recvfrom() function.
|
---|
207 | *
|
---|
208 | */
|
---|
209 | int sockets_recvfrom(int verbose, int *socket_ids, unsigned int sockets,
|
---|
210 | struct sockaddr *address, socklen_t *addrlen, char *data, size_t size,
|
---|
211 | unsigned int messages)
|
---|
212 | {
|
---|
213 | if (verbose)
|
---|
214 | printf("\tRecvfrom\t");
|
---|
215 |
|
---|
216 | fflush(stdout);
|
---|
217 |
|
---|
218 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
219 | for (unsigned int message = 0; message < messages; message++) {
|
---|
220 | int rc = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
---|
221 | if (rc < 0) {
|
---|
222 | printf("Socket %u (%d), message %u error:\n",
|
---|
223 | index, socket_ids[index], message);
|
---|
224 | socket_print_error(stderr, rc, "Socket receive: ", "\n");
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (verbose)
|
---|
230 | print_mark(index);
|
---|
231 | }
|
---|
232 |
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** Send and receive data via sockets.
|
---|
237 | *
|
---|
238 | * Each datagram is sent and a reply read consequently.
|
---|
239 | * The next datagram is sent after the reply is received.
|
---|
240 | *
|
---|
241 | * @param[in] verbose Print out verbose information.
|
---|
242 | * @param[in] socket_ids Stored socket identifiers.
|
---|
243 | * @param[in] sockets Number of sockets.
|
---|
244 | * @param[in,out] address Destination host address to send data to.
|
---|
245 | * The source host address of received datagrams is set.
|
---|
246 | * @param[in] addrlen Length of the destination address in bytes.
|
---|
247 | * @param[in,out] data Data to be sent. The received data are set.
|
---|
248 | * @param[in] size Size of the data in bytes.
|
---|
249 | * @param[in] messages Number of datagrams per socket to be received.
|
---|
250 | * @param[in] type Socket type.
|
---|
251 | *
|
---|
252 | * @return EOK on success.
|
---|
253 | * @return Other error codes as defined for the recvfrom() function.
|
---|
254 | *
|
---|
255 | */
|
---|
256 | int sockets_sendto_recvfrom(int verbose, int *socket_ids, unsigned int sockets,
|
---|
257 | struct sockaddr *address, socklen_t *addrlen, char *data,
|
---|
258 | size_t size, unsigned int messages, sock_type_t type)
|
---|
259 | {
|
---|
260 | if (verbose)
|
---|
261 | printf("\tSendto and recvfrom\t");
|
---|
262 |
|
---|
263 | fflush(stdout);
|
---|
264 |
|
---|
265 | for (unsigned int index = 0; index < sockets; index++) {
|
---|
266 | for (unsigned int message = 0; message < messages; message++) {
|
---|
267 | int rc;
|
---|
268 |
|
---|
269 | switch (type) {
|
---|
270 | case SOCK_STREAM:
|
---|
271 | rc = send(socket_ids[index], data, size, 0);
|
---|
272 | break;
|
---|
273 | case SOCK_DGRAM:
|
---|
274 | rc = sendto(socket_ids[index], data, size, 0, address, *addrlen);
|
---|
275 | break;
|
---|
276 | default:
|
---|
277 | rc = EINVAL;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (rc != EOK) {
|
---|
281 | printf("Socket %u (%d), message %u error:\n",
|
---|
282 | index, socket_ids[index], message);
|
---|
283 | socket_print_error(stderr, rc, "Socket send: ", "\n");
|
---|
284 | return rc;
|
---|
285 | }
|
---|
286 |
|
---|
287 | rc = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
---|
288 | if (rc < 0) {
|
---|
289 | printf("Socket %u (%d), message %u error:\n",
|
---|
290 | index, socket_ids[index], message);
|
---|
291 | socket_print_error(stderr, rc, "Socket receive: ", "\n");
|
---|
292 | return rc;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (verbose)
|
---|
297 | print_mark(index);
|
---|
298 | }
|
---|
299 |
|
---|
300 | return EOK;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** Prints a mark.
|
---|
304 | *
|
---|
305 | * If the index is a multiple of ten, a different mark is printed.
|
---|
306 | *
|
---|
307 | * @param[in] index The index of the mark to be printed.
|
---|
308 | *
|
---|
309 | */
|
---|
310 | void print_mark(unsigned int index)
|
---|
311 | {
|
---|
312 | if ((index + 1) % 10)
|
---|
313 | printf("*");
|
---|
314 | else
|
---|
315 | printf("|");
|
---|
316 |
|
---|
317 | fflush(stdout);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** @}
|
---|
321 | */
|
---|