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 1 application - sockets.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <malloc.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <task.h>
|
---|
41 | #include <time.h>
|
---|
42 |
|
---|
43 | #include "../../include/in.h"
|
---|
44 | #include "../../include/in6.h"
|
---|
45 | #include "../../include/inet.h"
|
---|
46 | #include "../../include/socket.h"
|
---|
47 |
|
---|
48 | #include "../../err.h"
|
---|
49 |
|
---|
50 | #include "../parse.h"
|
---|
51 | #include "../print_error.h"
|
---|
52 |
|
---|
53 | /** Echo module name.
|
---|
54 | */
|
---|
55 | #define NAME "Nettest1"
|
---|
56 |
|
---|
57 | /** Packet data pattern.
|
---|
58 | */
|
---|
59 | #define NETTEST1_TEXT "Networking test 1 - sockets"
|
---|
60 |
|
---|
61 | /** Module entry point.
|
---|
62 | * Starts testing.
|
---|
63 | * @param[in] argc The number of command line parameters.
|
---|
64 | * @param[in] argv The command line parameters.
|
---|
65 | * @returns EOK on success.
|
---|
66 | */
|
---|
67 | int main( int argc, char * argv[] );
|
---|
68 |
|
---|
69 | /** Prints the application help.
|
---|
70 | */
|
---|
71 | void print_help( void );
|
---|
72 |
|
---|
73 | /** Translates the character string to the protocol family number.
|
---|
74 | * @param[in] name The protocol family name.
|
---|
75 | * @returns The corresponding protocol family number.
|
---|
76 | * @returns EPFNOSUPPORTED if the protocol family is not supported.
|
---|
77 | */
|
---|
78 | int parse_protocol_family( const char * name );
|
---|
79 |
|
---|
80 | /** Translates the character string to the socket type number.
|
---|
81 | * @param[in] name The socket type name.
|
---|
82 | * @returns The corresponding socket type number.
|
---|
83 | * @returns ESOCKNOSUPPORTED if the socket type is not supported.
|
---|
84 | */
|
---|
85 | int parse_socket_type( const char * name );
|
---|
86 |
|
---|
87 | /** Refreshes the data.
|
---|
88 | * Fills the data block with the NETTEST1_TEXT pattern.
|
---|
89 | * @param[out] data The data block.
|
---|
90 | * @param[in] size The data block size in bytes.
|
---|
91 | */
|
---|
92 | void refresh_data( char * data, size_t size );
|
---|
93 |
|
---|
94 | /** Creates new sockets.
|
---|
95 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
96 | * @param[out] socket_ids A field to store the socket identifiers.
|
---|
97 | * @param[in] sockets The number of sockets to create. Should be at most the size of the field.
|
---|
98 | * @param[in] family The socket address family.
|
---|
99 | * @param[in] type The socket type.
|
---|
100 | * @returns EOK on success.
|
---|
101 | * @returns Other error codes as defined for the socket() function.
|
---|
102 | */
|
---|
103 | int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type );
|
---|
104 |
|
---|
105 | /** Closes sockets.
|
---|
106 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
107 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
108 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
109 | * @returns EOK on success.
|
---|
110 | * @returns Other error codes as defined for the closesocket() function.
|
---|
111 | */
|
---|
112 | int sockets_close( int verbose, int * socket_ids, int sockets );
|
---|
113 |
|
---|
114 | /** Connects sockets.
|
---|
115 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
116 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
117 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
118 | * @param[in] address The destination host address to connect to.
|
---|
119 | * @param[in] addrlen The length of the destination address in bytes.
|
---|
120 | * @returns EOK on success.
|
---|
121 | * @returns Other error codes as defined for the connect() function.
|
---|
122 | */
|
---|
123 | int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen );
|
---|
124 |
|
---|
125 | /** Sends data via sockets.
|
---|
126 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
127 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
128 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
129 | * @param[in] address The destination host address to send data to.
|
---|
130 | * @param[in] addrlen The length of the destination address in bytes.
|
---|
131 | * @param[in] data The data to be sent.
|
---|
132 | * @param[in] size The data size in bytes.
|
---|
133 | * @param[in] messages The number of datagrams per socket to be sent.
|
---|
134 | * @returns EOK on success.
|
---|
135 | * @returns Other error codes as defined for the sendto() function.
|
---|
136 | */
|
---|
137 | int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages );
|
---|
138 |
|
---|
139 | /** Receives data via sockets.
|
---|
140 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
141 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
142 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
143 | * @param[in] address The source host address of received datagrams.
|
---|
144 | * @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead.
|
---|
145 | * @param[out] data The received data.
|
---|
146 | * @param[in] size The maximum data size in bytes.
|
---|
147 | * @param[in] messages The number of datagrams per socket to be received.
|
---|
148 | * @returns EOK on success.
|
---|
149 | * @returns Other error codes as defined for the recvfrom() function.
|
---|
150 | */
|
---|
151 | int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
|
---|
152 |
|
---|
153 | /** Sends and receives data via sockets.
|
---|
154 | * Each datagram is sent and a reply read consequently.
|
---|
155 | * The next datagram is sent after the reply is received.
|
---|
156 | * @param[in] verbose A value indicating whether to print out verbose information.
|
---|
157 | * @param[in] socket_ids A field of stored socket identifiers.
|
---|
158 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
---|
159 | * @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead.
|
---|
160 | * @param[in] addrlen The length of the destination address in bytes.
|
---|
161 | * @param[in,out] data The data to be sent. The received data are set instead.
|
---|
162 | * @param[in] size The data size in bytes.
|
---|
163 | * @param[in] messages The number of datagrams per socket to be received.
|
---|
164 | * @returns EOK on success.
|
---|
165 | * @returns Other error codes as defined for the recvfrom() function.
|
---|
166 | */
|
---|
167 | int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
|
---|
168 |
|
---|
169 | /** Prints a mark.
|
---|
170 | * If the index is a multiple of ten, a different mark is printed.
|
---|
171 | * @param[in] index The index of the mark to be printed.
|
---|
172 | */
|
---|
173 | void print_mark( int index );
|
---|
174 |
|
---|
175 | void print_help( void ){
|
---|
176 | printf(
|
---|
177 | "Network Networking test 1 aplication - sockets\n" \
|
---|
178 | "Usage: echo [options] numeric_address\n" \
|
---|
179 | "Where options are:\n" \
|
---|
180 | "-f protocol_family | --family=protocol_family\n" \
|
---|
181 | "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
|
---|
182 | "\n" \
|
---|
183 | "-h | --help\n" \
|
---|
184 | "\tShow this application help.\n"
|
---|
185 | "\n" \
|
---|
186 | "-m count | --messages=count\n" \
|
---|
187 | "\tThe number of messages to send and receive per socket. The default is 10.\n" \
|
---|
188 | "\n" \
|
---|
189 | "-n sockets | --sockets=count\n" \
|
---|
190 | "\tThe number of sockets to use. The default is 10.\n" \
|
---|
191 | "\n" \
|
---|
192 | "-p port_number | --port=port_number\n" \
|
---|
193 | "\tThe port number the application should send messages to. The default is 7.\n" \
|
---|
194 | "\n" \
|
---|
195 | "-s packet_size | --size=packet_size\n" \
|
---|
196 | "\tThe packet data size the application sends. The default is 28 bytes.\n" \
|
---|
197 | "\n" \
|
---|
198 | "-v | --verbose\n" \
|
---|
199 | "\tShow all output messages.\n"
|
---|
200 | );
|
---|
201 | }
|
---|
202 |
|
---|
203 | int parse_protocol_family( const char * name ){
|
---|
204 | if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
|
---|
205 | return PF_INET;
|
---|
206 | }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
|
---|
207 | return PF_INET6;
|
---|
208 | }
|
---|
209 | return EPFNOSUPPORT;
|
---|
210 | }
|
---|
211 |
|
---|
212 | int parse_socket_type( const char * name ){
|
---|
213 | if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
|
---|
214 | return SOCK_DGRAM;
|
---|
215 | }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){
|
---|
216 | return SOCK_STREAM;
|
---|
217 | }
|
---|
218 | return ESOCKTNOSUPPORT;
|
---|
219 | }
|
---|
220 |
|
---|
221 | void refresh_data( char * data, size_t size ){
|
---|
222 | size_t length;
|
---|
223 |
|
---|
224 | // fill the data
|
---|
225 | length = 0;
|
---|
226 | while( size > length + sizeof( NETTEST1_TEXT ) - 1 ){
|
---|
227 | memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT ) - 1 );
|
---|
228 | length += sizeof( NETTEST1_TEXT ) - 1;
|
---|
229 | }
|
---|
230 | memcpy( data + length, NETTEST1_TEXT, size - length );
|
---|
231 | data[ size ] = '\0';
|
---|
232 | }
|
---|
233 |
|
---|
234 | int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ){
|
---|
235 | int index;
|
---|
236 |
|
---|
237 | if( verbose ) printf( "Create\t" );
|
---|
238 | fflush( stdout );
|
---|
239 | for( index = 0; index < sockets; ++ index ){
|
---|
240 | socket_ids[ index ] = socket( family, type, 0 );
|
---|
241 | if( socket_ids[ index ] < 0 ){
|
---|
242 | printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
|
---|
243 | socket_print_error( stderr, socket_ids[ index ], "Socket create: ", "\n" );
|
---|
244 | return socket_ids[ index ];
|
---|
245 | }
|
---|
246 | if( verbose ){
|
---|
247 | print_mark( index );
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return EOK;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int sockets_close( int verbose, int * socket_ids, int sockets ){
|
---|
254 | ERROR_DECLARE;
|
---|
255 |
|
---|
256 | int index;
|
---|
257 |
|
---|
258 | if( verbose ) printf( "\tClose\t" );
|
---|
259 | fflush( stdout );
|
---|
260 | for( index = 0; index < sockets; ++ index ){
|
---|
261 | if( ERROR_OCCURRED( closesocket( socket_ids[ index ] ))){
|
---|
262 | printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
|
---|
263 | socket_print_error( stderr, ERROR_CODE, "Socket close: ", "\n" );
|
---|
264 | return ERROR_CODE;
|
---|
265 | }
|
---|
266 | if( verbose ) print_mark( index );
|
---|
267 | }
|
---|
268 | return EOK;
|
---|
269 | }
|
---|
270 |
|
---|
271 | int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){
|
---|
272 | ERROR_DECLARE;
|
---|
273 |
|
---|
274 | int index;
|
---|
275 |
|
---|
276 | if( verbose ) printf( "\tConnect\t" );
|
---|
277 | fflush( stdout );
|
---|
278 | for( index = 0; index < sockets; ++ index ){
|
---|
279 | if( ERROR_OCCURRED( connect( socket_ids[ index ], address, addrlen ))){
|
---|
280 | socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" );
|
---|
281 | return ERROR_CODE;
|
---|
282 | }
|
---|
283 | if( verbose ) print_mark( index );
|
---|
284 | }
|
---|
285 | return EOK;
|
---|
286 | }
|
---|
287 |
|
---|
288 | int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ){
|
---|
289 | ERROR_DECLARE;
|
---|
290 |
|
---|
291 | int index;
|
---|
292 | int message;
|
---|
293 |
|
---|
294 | if( verbose ) printf( "\tSendto\t" );
|
---|
295 | fflush( stdout );
|
---|
296 | for( index = 0; index < sockets; ++ index ){
|
---|
297 | for( message = 0; message < messages; ++ message ){
|
---|
298 | if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, addrlen ))){
|
---|
299 | printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
|
---|
300 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
|
---|
301 | return ERROR_CODE;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | if( verbose ) print_mark( index );
|
---|
305 | }
|
---|
306 | return EOK;
|
---|
307 | }
|
---|
308 |
|
---|
309 | int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
|
---|
310 | int value;
|
---|
311 | int index;
|
---|
312 | int message;
|
---|
313 |
|
---|
314 | if( verbose ) printf( "\tRecvfrom\t" );
|
---|
315 | fflush( stdout );
|
---|
316 | for( index = 0; index < sockets; ++ index ){
|
---|
317 | for( message = 0; message < messages; ++ message ){
|
---|
318 | value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
|
---|
319 | if( value < 0 ){
|
---|
320 | printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
|
---|
321 | socket_print_error( stderr, value, "Socket receive: ", "\n" );
|
---|
322 | return value;
|
---|
323 | }
|
---|
324 | }
|
---|
325 | if( verbose ) print_mark( index );
|
---|
326 | }
|
---|
327 | return EOK;
|
---|
328 | }
|
---|
329 |
|
---|
330 | int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
|
---|
331 | ERROR_DECLARE;
|
---|
332 |
|
---|
333 | int value;
|
---|
334 | int index;
|
---|
335 | int message;
|
---|
336 |
|
---|
337 | if( verbose ) printf( "\tSendto and recvfrom\t" );
|
---|
338 | fflush( stdout );
|
---|
339 | for( index = 0; index < sockets; ++ index ){
|
---|
340 | for( message = 0; message < messages; ++ message ){
|
---|
341 | if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, * addrlen ))){
|
---|
342 | printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
|
---|
343 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
|
---|
344 | return ERROR_CODE;
|
---|
345 | }
|
---|
346 | value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
|
---|
347 | if( value < 0 ){
|
---|
348 | printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
|
---|
349 | socket_print_error( stderr, value, "Socket receive: ", "\n" );
|
---|
350 | return value;
|
---|
351 | }
|
---|
352 | }
|
---|
353 | if( verbose ) print_mark( index );
|
---|
354 | }
|
---|
355 | return EOK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | void print_mark( int index ){
|
---|
359 | if(( index + 1 ) % 10 ){
|
---|
360 | printf( "*" );
|
---|
361 | }else{
|
---|
362 | printf( "|" );
|
---|
363 | }
|
---|
364 | fflush( stdout );
|
---|
365 | }
|
---|
366 |
|
---|
367 | int main( int argc, char * argv[] ){
|
---|
368 | ERROR_DECLARE;
|
---|
369 |
|
---|
370 | size_t size = 27;
|
---|
371 | int verbose = 0;
|
---|
372 | sock_type_t type = SOCK_DGRAM;
|
---|
373 | int sockets = 10;
|
---|
374 | int messages = 10;
|
---|
375 | int family = PF_INET;
|
---|
376 | uint16_t port = 7;
|
---|
377 |
|
---|
378 | socklen_t max_length = sizeof( struct sockaddr_in6 );
|
---|
379 | uint8_t address_data[ max_length ];
|
---|
380 | struct sockaddr * address = ( struct sockaddr * ) address_data;
|
---|
381 | struct sockaddr_in * address_in = ( struct sockaddr_in * ) address;
|
---|
382 | struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address;
|
---|
383 | socklen_t addrlen;
|
---|
384 | // char address_string[ INET6_ADDRSTRLEN ];
|
---|
385 | uint8_t * address_start;
|
---|
386 |
|
---|
387 | int * socket_ids;
|
---|
388 | char * data;
|
---|
389 | int value;
|
---|
390 | int index;
|
---|
391 | struct timeval time_before;
|
---|
392 | struct timeval time_after;
|
---|
393 |
|
---|
394 | printf( "Task %d - ", task_get_id());
|
---|
395 | printf( "%s\n", NAME );
|
---|
396 |
|
---|
397 | if( argc <= 1 ){
|
---|
398 | print_help();
|
---|
399 | return EINVAL;
|
---|
400 | }
|
---|
401 |
|
---|
402 | for( index = 1; ( index < argc - 1 ) || (( index == argc ) && ( argv[ index ][ 0 ] == '-' )); ++ index ){
|
---|
403 | if( argv[ index ][ 0 ] == '-' ){
|
---|
404 | switch( argv[ index ][ 1 ] ){
|
---|
405 | case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
|
---|
406 | break;
|
---|
407 | case 'h': print_help();
|
---|
408 | return EOK;
|
---|
409 | break;
|
---|
410 | case 'm': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 0 ));
|
---|
411 | break;
|
---|
412 | case 'n': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 0 ));
|
---|
413 | break;
|
---|
414 | case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
|
---|
415 | port = ( uint16_t ) value;
|
---|
416 | break;
|
---|
417 | case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 ));
|
---|
418 | size = (value >= 0 ) ? ( size_t ) value : 0;
|
---|
419 | break;
|
---|
420 | case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type ));
|
---|
421 | type = ( sock_type_t ) value;
|
---|
422 | break;
|
---|
423 | case 'v': verbose = 1;
|
---|
424 | break;
|
---|
425 | case '-': if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
|
---|
426 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
|
---|
427 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
|
---|
428 | print_help();
|
---|
429 | return EOK;
|
---|
430 | }else if( str_lcmp( argv[ index ] + 2, "messages=", 6 ) == 0 ){
|
---|
431 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 8 ));
|
---|
432 | }else if( str_lcmp( argv[ index ] + 2, "sockets=", 6 ) == 0 ){
|
---|
433 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 8 ));
|
---|
434 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
|
---|
435 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
|
---|
436 | port = ( uint16_t ) value;
|
---|
437 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
|
---|
438 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type ));
|
---|
439 | type = ( sock_type_t ) value;
|
---|
440 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
|
---|
441 | verbose = 1;
|
---|
442 | }else{
|
---|
443 | print_unrecognized( index, argv[ index ] + 2 );
|
---|
444 | print_help();
|
---|
445 | return EINVAL;
|
---|
446 | }
|
---|
447 | break;
|
---|
448 | default:
|
---|
449 | print_unrecognized( index, argv[ index ] + 1 );
|
---|
450 | print_help();
|
---|
451 | return EINVAL;
|
---|
452 | }
|
---|
453 | }else{
|
---|
454 | print_unrecognized( index, argv[ index ] );
|
---|
455 | print_help();
|
---|
456 | return EINVAL;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | bzero( address_data, max_length );
|
---|
461 | switch( family ){
|
---|
462 | case PF_INET:
|
---|
463 | address_in->sin_family = AF_INET;
|
---|
464 | address_in->sin_port = htons( port );
|
---|
465 | address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
|
---|
466 | addrlen = sizeof( struct sockaddr_in );
|
---|
467 | break;
|
---|
468 | case PF_INET6:
|
---|
469 | address_in6->sin6_family = AF_INET6;
|
---|
470 | address_in6->sin6_port = htons( port );
|
---|
471 | address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
|
---|
472 | addrlen = sizeof( struct sockaddr_in6 );
|
---|
473 | break;
|
---|
474 | default:
|
---|
475 | fprintf( stderr, "Address family is not supported\n" );
|
---|
476 | return EAFNOSUPPORT;
|
---|
477 | }
|
---|
478 |
|
---|
479 | if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){
|
---|
480 | fprintf( stderr, "Address parse error %d\n", ERROR_CODE );
|
---|
481 | return ERROR_CODE;
|
---|
482 | }
|
---|
483 |
|
---|
484 | if( size <= 0 ){
|
---|
485 | fprintf( stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size );
|
---|
486 | size = 1024;
|
---|
487 | }
|
---|
488 | // size plus terminating null (\0)
|
---|
489 | data = ( char * ) malloc( size + 1 );
|
---|
490 | if( ! data ){
|
---|
491 | fprintf( stderr, "Failed to allocate data buffer.\n" );
|
---|
492 | return ENOMEM;
|
---|
493 | }
|
---|
494 | refresh_data( data, size );
|
---|
495 |
|
---|
496 | if( sockets <= 0 ){
|
---|
497 | fprintf( stderr, "Socket count too small (%d). Using 2 instead.\n", sockets );
|
---|
498 | sockets = 2;
|
---|
499 | }
|
---|
500 | // count plus terminating null (\0)
|
---|
501 | socket_ids = ( int * ) malloc( sizeof( int ) * ( sockets + 1 ));
|
---|
502 | if( ! socket_ids ){
|
---|
503 | fprintf( stderr, "Failed to allocate receive buffer.\n" );
|
---|
504 | return ENOMEM;
|
---|
505 | }
|
---|
506 | socket_ids[ sockets ] = NULL;
|
---|
507 |
|
---|
508 | if( verbose ) printf( "Starting tests\n" );
|
---|
509 |
|
---|
510 | if( verbose ) printf( "1 socket, 1 message\n" );
|
---|
511 |
|
---|
512 | if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){
|
---|
513 | fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
|
---|
514 | return ERROR_CODE;
|
---|
515 | }
|
---|
516 |
|
---|
517 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
|
---|
518 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
|
---|
519 | if( verbose ) printf( "\tOK\n" );
|
---|
520 |
|
---|
521 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
|
---|
522 | if( type == SOCK_STREAM ){
|
---|
523 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
|
---|
524 | }
|
---|
525 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
|
---|
526 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
|
---|
527 | if( verbose ) printf( "\tOK\n" );
|
---|
528 |
|
---|
529 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
|
---|
530 | if( type == SOCK_STREAM ){
|
---|
531 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
|
---|
532 | }
|
---|
533 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 ));
|
---|
534 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
|
---|
535 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
|
---|
536 | if( verbose ) printf( "\tOK\n" );
|
---|
537 |
|
---|
538 | if( verbose ) printf( "1 socket, %d messages\n", messages );
|
---|
539 |
|
---|
540 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
|
---|
541 | if( type == SOCK_STREAM ){
|
---|
542 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
|
---|
543 | }
|
---|
544 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
|
---|
545 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
|
---|
546 | if( verbose ) printf( "\tOK\n" );
|
---|
547 |
|
---|
548 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
|
---|
549 | if( type == SOCK_STREAM ){
|
---|
550 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
|
---|
551 | }
|
---|
552 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages ));
|
---|
553 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
|
---|
554 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
|
---|
555 | if( verbose ) printf( "\tOK\n" );
|
---|
556 |
|
---|
557 | if( verbose ) printf( "%d sockets, 1 message\n", sockets );
|
---|
558 |
|
---|
559 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
|
---|
560 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
|
---|
561 | if( verbose ) printf( "\tOK\n" );
|
---|
562 |
|
---|
563 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
|
---|
564 | if( type == SOCK_STREAM ){
|
---|
565 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
|
---|
566 | }
|
---|
567 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
|
---|
568 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
|
---|
569 | if( verbose ) printf( "\tOK\n" );
|
---|
570 |
|
---|
571 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
|
---|
572 | if( type == SOCK_STREAM ){
|
---|
573 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
|
---|
574 | }
|
---|
575 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 ));
|
---|
576 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
|
---|
577 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
|
---|
578 | if( verbose ) printf( "\tOK\n" );
|
---|
579 |
|
---|
580 | if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages );
|
---|
581 |
|
---|
582 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
|
---|
583 | if( type == SOCK_STREAM ){
|
---|
584 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
|
---|
585 | }
|
---|
586 | ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
|
---|
587 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
|
---|
588 | if( verbose ) printf( "\tOK\n" );
|
---|
589 |
|
---|
590 | ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
|
---|
591 | if( type == SOCK_STREAM ){
|
---|
592 | ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
|
---|
593 | }
|
---|
594 | ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages ));
|
---|
595 | ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
|
---|
596 | ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
|
---|
597 |
|
---|
598 | if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){
|
---|
599 | fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
|
---|
600 | return ERROR_CODE;
|
---|
601 | }
|
---|
602 |
|
---|
603 | if( verbose ) printf( "\tOK\n" );
|
---|
604 |
|
---|
605 | printf( "Tested in %d microseconds\n", tv_sub( & time_after, & time_before ));
|
---|
606 |
|
---|
607 | if( verbose ) printf( "Exiting\n" );
|
---|
608 |
|
---|
609 | return EOK;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /** @}
|
---|
613 | */
|
---|