Subversion Repositories HelenOS

Rev

Rev 4740 | Rev 4749 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4740 Rev 4743
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup echo
29
/** @addtogroup echo
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  Networking test 1 application - UDP sockets.
34
 *  Networking test 1 application - UDP sockets.
35
 */
35
 */
36
 
36
 
37
#include <malloc.h>
37
#include <malloc.h>
38
#include <stdio.h>
38
#include <stdio.h>
39
#include <string.h>
39
#include <string.h>
40
#include <task.h>
40
#include <task.h>
-
 
41
#include <time.h>
41
 
42
 
42
#include "../../include/in.h"
43
#include "../../include/in.h"
43
#include "../../include/in6.h"
44
#include "../../include/in6.h"
44
#include "../../include/inet.h"
45
#include "../../include/inet.h"
45
#include "../../include/socket.h"
46
#include "../../include/socket.h"
46
 
47
 
47
#include "../../err.h"
48
#include "../../err.h"
48
 
49
 
49
#include "../parse.h"
50
#include "../parse.h"
50
#include "../print_error.h"
51
#include "../print_error.h"
51
 
52
 
52
/** Echo module name.
53
/** Echo module name.
53
 */
54
 */
54
#define NAME    "Nettest1"
55
#define NAME    "Nettest1"
55
 
56
 
56
/** Packet data pattern.
57
/** Packet data pattern.
57
 */
58
 */
58
#define NETTEST1_TEXT   "Networking test 1 - UDP sockets"
59
#define NETTEST1_TEXT   "Networking test 1 - UDP sockets"
59
 
60
 
60
/** Module entry point.
61
/** Module entry point.
61
 *  Starts testing.
62
 *  Starts testing.
62
 *  @param argc The number of command line parameters. Input parameter.
63
 *  @param argc The number of command line parameters. Input parameter.
63
 *  @param argv The command line parameters. Input parameter.
64
 *  @param argv The command line parameters. Input parameter.
64
 *  @returns EOK on success.
65
 *  @returns EOK on success.
65
 */
66
 */
66
int     main( int argc, char * argv[] );
67
int     main( int argc, char * argv[] );
67
 
68
 
68
/** Prints the application help.
69
/** Prints the application help.
69
 */
70
 */
70
void    print_help( void );
71
void    print_help( void );
71
 
72
 
72
/** Translates the character string to the protocol family number.
73
/** Translates the character string to the protocol family number.
73
 *  @param name The protocol family name. Input parameter.
74
 *  @param name The protocol family name. Input parameter.
74
 *  @returns The corresponding protocol family number.
75
 *  @returns The corresponding protocol family number.
75
 *  @returns EPFNOSUPPORTED if the protocol family is not supported.
76
 *  @returns EPFNOSUPPORTED if the protocol family is not supported.
76
 */
77
 */
77
int     parse_protocol_family( const char * name );
78
int     parse_protocol_family( const char * name );
78
 
79
 
79
/** Translates the character string to the socket type number.
80
/** Translates the character string to the socket type number.
80
 *  @param name The socket type name. Input parameter.
81
 *  @param name The socket type name. Input parameter.
81
 *  @returns The corresponding socket type number.
82
 *  @returns The corresponding socket type number.
82
 *  @returns ESOCKNOSUPPORTED if the socket type is not supported.
83
 *  @returns ESOCKNOSUPPORTED if the socket type is not supported.
83
 */
84
 */
84
int     parse_socket_type( const char * name );
85
int     parse_socket_type( const char * name );
85
 
86
 
86
void    refresh_data( char * data, int size );
87
void    refresh_data( char * data, size_t size );
87
int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type );
88
int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type );
88
int sockets_close( int verbose, int * socket_ids, int sockets );
89
int sockets_close( int verbose, int * socket_ids, int sockets );
89
int sockets_bind( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen );
90
int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen );
90
int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages );
91
int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages );
91
int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
92
int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
92
int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
93
int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages );
93
void    print_mark( int index );
94
void    print_mark( int index );
94
 
95
 
95
void print_help( void ){
96
void print_help( void ){
96
    printf(
97
    printf(
97
        "Network Networking test 1 aplication - UDP sockets\n" \
98
        "Network Networking test 1 aplication - UDP sockets\n" \
98
        "Usage: echo [options] numeric_address\n" \
99
        "Usage: echo [options] numeric_address\n" \
99
        "Where options are:\n" \
100
        "Where options are:\n" \
100
        "-f protocol_family | --family=protocol_family\n" \
101
        "-f protocol_family | --family=protocol_family\n" \
101
        "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
102
        "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
102
        "\n" \
103
        "\n" \
103
        "-h | --help\n" \
104
        "-h | --help\n" \
104
        "\tShow this application help.\n"
105
        "\tShow this application help.\n"
105
        "\n" \
106
        "\n" \
106
        "-m count | --messages=count\n" \
107
        "-m count | --messages=count\n" \
107
        "\tThe number of messages to send and receive per socket. The default is 100.\n" \
108
        "\tThe number of messages to send and receive per socket. The default is 100.\n" \
108
        "\n" \
109
        "\n" \
109
        "-n sockets | --sockets=count\n" \
110
        "-n sockets | --sockets=count\n" \
110
        "\tThe number of sockets to use. The default is 100.\n" \
111
        "\tThe number of sockets to use. The default is 100.\n" \
111
        "\n" \
112
        "\n" \
112
        "-p port_number | --port=port_number\n" \
113
        "-p port_number | --port=port_number\n" \
113
        "\tThe port number the application should send messages to. The default is 7.\n" \
114
        "\tThe port number the application should send messages to. The default is 7.\n" \
114
        "\n" \
115
        "\n" \
115
        "-s packet_size | --size=packet_size\n" \
116
        "-s packet_size | --size=packet_size\n" \
116
        "\tThe packet data size the application sends. The default is 38 bytes.\n" \
117
        "\tThe packet data size the application sends. The default is 38 bytes.\n" \
117
        "\n" \
118
        "\n" \
118
        "-v | --verbose\n" \
119
        "-v | --verbose\n" \
119
        "\tShow all output messages.\n"
120
        "\tShow all output messages.\n"
120
    );
121
    );
121
}
122
}
122
 
123
 
123
int parse_protocol_family( const char * name ){
124
int parse_protocol_family( const char * name ){
124
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
125
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
125
        return PF_INET;
126
        return PF_INET;
126
    }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
127
    }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
127
        return PF_INET6;
128
        return PF_INET6;
128
    }
129
    }
129
    return EPFNOSUPPORT;
130
    return EPFNOSUPPORT;
130
}
131
}
131
 
132
 
132
int parse_socket_type( const char * name ){
133
int parse_socket_type( const char * name ){
133
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
134
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
134
        return SOCK_DGRAM;
135
        return SOCK_DGRAM;
135
    }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){
136
    }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){
136
        return SOCK_STREAM;
137
        return SOCK_STREAM;
137
    }
138
    }
138
    return ESOCKTNOSUPPORT;
139
    return ESOCKTNOSUPPORT;
139
}
140
}
140
 
141
 
141
void refresh_data( char * data, int size ){
142
void refresh_data( char * data, size_t size ){
142
    int length;
143
    size_t  length;
143
 
144
 
144
    // fill the data
145
    // fill the data
145
    length = 0;
146
    length = 0;
146
    while( size > length + sizeof( NETTEST1_TEXT )){
147
    while( size > length + sizeof( NETTEST1_TEXT )){
147
        memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT ));
148
        memcpy( data + length, NETTEST1_TEXT, sizeof( NETTEST1_TEXT ));
148
        length += sizeof( NETTEST1_TEXT );
149
        length += sizeof( NETTEST1_TEXT );
149
    }
150
    }
150
    memcpy( data + length, NETTEST1_TEXT, size - length );
151
    memcpy( data + length, NETTEST1_TEXT, size - length );
151
}
152
}
152
 
153
 
153
int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ){
154
int sockets_create( int verbose, int * socket_ids, int sockets, int family, sock_type_t type ){
154
    int index;
155
    int index;
155
 
156
 
156
    if( verbose ) printf( "Create\t" );
157
    if( verbose ) printf( "Create\t" );
157
    fflush( stdout );
158
    fflush( stdout );
158
    for( index = 0; index < sockets; ++ index ){
159
    for( index = 0; index < sockets; ++ index ){
159
        socket_ids[ index ] = socket( family, type, 0 );
160
        socket_ids[ index ] = socket( family, type, 0 );
160
        if( socket_ids[ index ] < 0 ){
161
        if( socket_ids[ index ] < 0 ){
161
            printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
162
            printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
162
            socket_print_error( stderr, socket_ids[ index ], "Socket create: ", "\n" );
163
            socket_print_error( stderr, socket_ids[ index ], "Socket create: ", "\n" );
163
            return socket_ids[ index ];
164
            return socket_ids[ index ];
164
        }
165
        }
165
        if( verbose ) print_mark( index );
166
        if( verbose ) print_mark( index );
166
    }
167
    }
167
    return EOK;
168
    return EOK;
168
}
169
}
169
 
170
 
170
int sockets_close( int verbose, int * socket_ids, int sockets ){
171
int sockets_close( int verbose, int * socket_ids, int sockets ){
171
    ERROR_DECLARE;
172
    ERROR_DECLARE;
172
 
173
 
173
    int index;
174
    int index;
174
 
175
 
175
    if( verbose ) printf( "\tClose\t" );
176
    if( verbose ) printf( "\tClose\t" );
176
    fflush( stdout );
177
    fflush( stdout );
177
    for( index = 0; index < sockets; ++ index ){
178
    for( index = 0; index < sockets; ++ index ){
178
        if( ERROR_OCCURRED( closesocket( socket_ids[ index ] ))){
179
        if( ERROR_OCCURRED( closesocket( socket_ids[ index ] ))){
179
            printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
180
            printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
180
            socket_print_error( stderr, ERROR_CODE, "Socket close: ", "\n" );
181
            socket_print_error( stderr, ERROR_CODE, "Socket close: ", "\n" );
181
            return ERROR_CODE;
182
            return ERROR_CODE;
182
        }
183
        }
183
        if( verbose ) print_mark( index );
184
        if( verbose ) print_mark( index );
184
    }
185
    }
185
    return EOK;
186
    return EOK;
186
}
187
}
187
 
188
 
188
int sockets_bind( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){
189
int sockets_connect( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen ){
189
    ERROR_DECLARE;
190
    ERROR_DECLARE;
190
 
191
 
191
    int index;
192
    int index;
192
 
193
 
193
    if( verbose ) printf( "\tBind\t" );
194
    if( verbose ) printf( "\tConnect\t" );
194
    fflush( stdout );
195
    fflush( stdout );
195
    for( index = 0; index < sockets; ++ index ){
196
    for( index = 0; index < sockets; ++ index ){
196
        if( ERROR_OCCURRED( bind( socket_ids[ index ], address, addrlen ))){
197
        if( ERROR_OCCURRED( connect( socket_ids[ index ], address, addrlen ))){
197
            printf( "Socket %d (%d) error:\n", index, socket_ids[ index ] );
-
 
198
            socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
198
            socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" );
199
            return ERROR_CODE;
199
            return ERROR_CODE;
200
        }
200
        }
201
        if( verbose ) print_mark( index );
201
        if( verbose ) print_mark( index );
202
    }
202
    }
203
    return EOK;
203
    return EOK;
204
}
204
}
205
 
205
 
206
int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ){
206
int sockets_sendto( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages ){
207
    ERROR_DECLARE;
207
    ERROR_DECLARE;
208
 
208
 
209
    int index;
209
    int index;
210
    int message;
210
    int message;
211
 
211
 
212
    if( verbose ) printf( "\tSendto\t" );
212
    if( verbose ) printf( "\tSendto\t" );
213
    fflush( stdout );
213
    fflush( stdout );
214
    for( index = 0; index < sockets; ++ index ){
214
    for( index = 0; index < sockets; ++ index ){
215
        for( message = 0; message < messages; ++ message ){
215
        for( message = 0; message < messages; ++ message ){
216
            if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, addrlen ))){
216
            if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, addrlen ))){
217
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
217
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
218
                socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
218
                socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
219
                return ERROR_CODE;
219
                return ERROR_CODE;
220
            }
220
            }
221
        }
221
        }
222
        if( verbose ) print_mark( index );
222
        if( verbose ) print_mark( index );
223
    }
223
    }
224
    return EOK;
224
    return EOK;
225
}
225
}
226
 
226
 
227
int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
227
int sockets_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
228
    int value;
228
    int value;
229
    int index;
229
    int index;
230
    int message;
230
    int message;
231
 
231
 
232
    if( verbose ) printf( "\tRecvfrom\t" );
232
    if( verbose ) printf( "\tRecvfrom\t" );
233
    fflush( stdout );
233
    fflush( stdout );
234
    for( index = 0; index < sockets; ++ index ){
234
    for( index = 0; index < sockets; ++ index ){
235
        for( message = 0; message < messages; ++ message ){
235
        for( message = 0; message < messages; ++ message ){
236
            value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
236
            value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
237
            if( value < 0 ){
237
            if( value < 0 ){
238
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
238
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
239
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
239
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
240
                return value;
240
                return value;
241
            }
241
            }
242
        }
242
        }
243
        if( verbose ) print_mark( index );
243
        if( verbose ) print_mark( index );
244
    }
244
    }
245
    return EOK;
245
    return EOK;
246
}
246
}
247
 
247
 
248
int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
248
int sockets_sendto_recvfrom( int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages ){
249
    ERROR_DECLARE;
249
    ERROR_DECLARE;
250
 
250
 
251
    int value;
251
    int value;
252
    int index;
252
    int index;
253
    int message;
253
    int message;
254
 
254
 
255
    if( verbose ) printf( "\tSendto and recvfrom\t" );
255
    if( verbose ) printf( "\tSendto and recvfrom\t" );
256
    fflush( stdout );
256
    fflush( stdout );
257
    for( index = 0; index < sockets; ++ index ){
257
    for( index = 0; index < sockets; ++ index ){
258
        for( message = 0; message < messages; ++ message ){
258
        for( message = 0; message < messages; ++ message ){
259
            if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, * addrlen ))){
259
            if( ERROR_OCCURRED( sendto( socket_ids[ index ], data, size, 0, address, * addrlen ))){
260
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
260
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
261
                socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
261
                socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
262
                return ERROR_CODE;
262
                return ERROR_CODE;
263
            }
263
            }
264
            value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
264
            value = recvfrom( socket_ids[ index ], data, size, 0, address, addrlen );
265
            if( value < 0 ){
265
            if( value < 0 ){
266
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
266
                printf( "Socket %d (%d), message %d error:\n", index, socket_ids[ index ], message );
267
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
267
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
268
                return value;
268
                return value;
269
            }
269
            }
270
        }
270
        }
271
        if( verbose ) print_mark( index );
271
        if( verbose ) print_mark( index );
272
    }
272
    }
273
    return EOK;
273
    return EOK;
274
}
274
}
275
 
275
 
276
void print_mark( int index ){
276
void print_mark( int index ){
277
    if(( index + 1 ) % 10 ){
277
    if(( index + 1 ) % 10 ){
278
        printf( "*" );
278
        printf( "*" );
279
    }else{
279
    }else{
280
        printf( "|" );
280
        printf( "|" );
281
    }
281
    }
282
    fflush( stdout );
282
    fflush( stdout );
283
}
283
}
284
 
284
 
285
int main( int argc, char * argv[] ){
285
int main( int argc, char * argv[] ){
286
    ERROR_DECLARE;
286
    ERROR_DECLARE;
287
 
287
 
288
    size_t              size            = 38;
288
    size_t              size            = 38;
289
    int                 verbose         = 0;
289
    int                 verbose         = 0;
290
    sock_type_t         type            = SOCK_DGRAM;
290
    sock_type_t         type            = SOCK_DGRAM;
291
    int                 sockets         = 10;
291
    int                 sockets         = 10;
292
    int                 messages        = 10;
292
    int                 messages        = 10;
293
    int                 family          = PF_INET;
293
    int                 family          = PF_INET;
294
    uint16_t            port            = 7;
294
    uint16_t            port            = 7;
295
 
295
 
296
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
296
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
297
    uint8_t             address_data[ max_length ];
297
    uint8_t             address_data[ max_length ];
298
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
298
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
299
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
299
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
300
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
300
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
301
    socklen_t           addrlen;
301
    socklen_t           addrlen;
302
//  char                address_string[ INET6_ADDRSTRLEN ];
302
//  char                address_string[ INET6_ADDRSTRLEN ];
303
    uint8_t *           address_start;
303
    uint8_t *           address_start;
304
 
304
 
305
    int *               socket_ids;
305
    int *               socket_ids;
306
    char *              data;
306
    char *              data;
307
    int                 value;
307
    int                 value;
308
    int                 index;
308
    int                 index;
-
 
309
    struct timeval      time_before;
-
 
310
    struct timeval      time_after;
309
 
311
 
310
    printf( "Task %d - ", task_get_id());
312
    printf( "Task %d - ", task_get_id());
311
    printf( "%s\n", NAME );
313
    printf( "%s\n", NAME );
312
 
314
 
313
    if( argc <= 1 ){
315
    if( argc <= 1 ){
314
        print_help();
316
        print_help();
315
        return EINVAL;
317
        return EINVAL;
316
    }
318
    }
317
 
319
 
318
    for( index = 1; ( index < argc - 1 ) || (( index == argc ) && ( argv[ index ][ 0 ] == '-' )); ++ index ){
320
    for( index = 1; ( index < argc - 1 ) || (( index == argc ) && ( argv[ index ][ 0 ] == '-' )); ++ index ){
319
        if( argv[ index ][ 0 ] == '-' ){
321
        if( argv[ index ][ 0 ] == '-' ){
320
            switch( argv[ index ][ 1 ] ){
322
            switch( argv[ index ][ 1 ] ){
321
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
323
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
322
                            break;
324
                            break;
323
                case 'h':   print_help();
325
                case 'h':   print_help();
324
                            return EOK;
326
                            return EOK;
325
                            break;
327
                            break;
326
                case 'm':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 0 ));
328
                case 'm':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 0 ));
327
                            break;
329
                            break;
328
                case 'n':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 0 ));
330
                case 'n':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 0 ));
329
                            break;
331
                            break;
330
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
332
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
331
                            port = ( uint16_t ) value;
333
                            port = ( uint16_t ) value;
332
                            break;
334
                            break;
333
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 ));
335
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "packet size", 0 ));
334
                            size = (value >= 0 ) ? ( size_t ) value : 0;
336
                            size = (value >= 0 ) ? ( size_t ) value : 0;
335
                            break;
337
                            break;
336
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type ));
338
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type ));
337
                            type = ( sock_type_t ) value;
339
                            type = ( sock_type_t ) value;
338
                            break;
340
                            break;
339
                case 'v':   verbose = 1;
341
                case 'v':   verbose = 1;
340
                            break;
342
                            break;
341
                case '-':   if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
343
                case '-':   if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
342
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
344
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
343
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
345
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
344
                                print_help();
346
                                print_help();
345
                                return EOK;
347
                                return EOK;
346
                            }else if( str_lcmp( argv[ index ] + 2, "messages=", 6 ) == 0 ){
348
                            }else if( str_lcmp( argv[ index ] + 2, "messages=", 6 ) == 0 ){
347
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 8 ));
349
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & messages, "message count", 8 ));
348
                            }else if( str_lcmp( argv[ index ] + 2, "sockets=", 6 ) == 0 ){
350
                            }else if( str_lcmp( argv[ index ] + 2, "sockets=", 6 ) == 0 ){
349
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 8 ));
351
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & sockets, "socket count", 8 ));
350
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
352
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
351
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
353
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
352
                                port = ( uint16_t ) value;
354
                                port = ( uint16_t ) value;
353
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
355
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
354
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type ));
356
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type ));
355
                                type = ( sock_type_t ) value;
357
                                type = ( sock_type_t ) value;
356
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
358
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
357
                                verbose = 1;
359
                                verbose = 1;
358
                            }else{
360
                            }else{
359
                                print_unrecognized( index, argv[ index ] + 2 );
361
                                print_unrecognized( index, argv[ index ] + 2 );
360
                                print_help();
362
                                print_help();
361
                                return EINVAL;
363
                                return EINVAL;
362
                            }
364
                            }
363
                            break;
365
                            break;
364
                default:
366
                default:
365
                    print_unrecognized( index, argv[ index ] + 1 );
367
                    print_unrecognized( index, argv[ index ] + 1 );
366
                    print_help();
368
                    print_help();
367
                    return EINVAL;
369
                    return EINVAL;
368
            }
370
            }
369
        }else{
371
        }else{
370
            print_unrecognized( index, argv[ index ] );
372
            print_unrecognized( index, argv[ index ] );
371
            print_help();
373
            print_help();
372
            return EINVAL;
374
            return EINVAL;
373
        }
375
        }
374
    }
376
    }
375
 
377
 
376
    bzero( address_data, max_length );
378
    bzero( address_data, max_length );
377
    switch( family ){
379
    switch( family ){
378
        case PF_INET:
380
        case PF_INET:
379
            address_in->sin_family = AF_INET;
381
            address_in->sin_family = AF_INET;
380
            address_in->sin_port = htons( port );
382
            address_in->sin_port = htons( port );
381
            address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
383
            address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
382
            addrlen = sizeof( struct sockaddr_in );
384
            addrlen = sizeof( struct sockaddr_in );
383
            break;
385
            break;
384
        case PF_INET6:
386
        case PF_INET6:
385
            address_in6->sin6_family = AF_INET6;
387
            address_in6->sin6_family = AF_INET6;
386
            address_in6->sin6_port = htons( port );
388
            address_in6->sin6_port = htons( port );
387
            address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
389
            address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
388
            addrlen = sizeof( struct sockaddr_in6 );
390
            addrlen = sizeof( struct sockaddr_in6 );
389
            break;
391
            break;
390
        default:
392
        default:
391
            fprintf( stderr, "Address family is not supported\n" );
393
            fprintf( stderr, "Address family is not supported\n" );
392
            return EAFNOSUPPORT;
394
            return EAFNOSUPPORT;
393
    }
395
    }
394
 
396
 
395
    if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){
397
    if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){
396
        fprintf( stderr, "Address parse error %d\n", ERROR_CODE );
398
        fprintf( stderr, "Address parse error %d\n", ERROR_CODE );
397
        return ERROR_CODE;
399
        return ERROR_CODE;
398
    }
400
    }
399
 
401
 
400
    if( size <= 0 ){
402
    if( size <= 0 ){
401
        fprintf( stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size );
403
        fprintf( stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size );
402
        size = 1024;
404
        size = 1024;
403
    }
405
    }
404
    // size plus terminating null (\0)
406
    // size plus terminating null (\0)
405
    data = ( char * ) malloc( size + 1 );
407
    data = ( char * ) malloc( size + 1 );
406
    if( ! data ){
408
    if( ! data ){
407
        fprintf( stderr, "Failed to allocate data buffer.\n" );
409
        fprintf( stderr, "Failed to allocate data buffer.\n" );
408
        return ENOMEM;
410
        return ENOMEM;
409
    }
411
    }
410
    refresh_data( data, size );
412
    refresh_data( data, size );
411
 
413
 
412
    if( sockets <= 0 ){
414
    if( sockets <= 0 ){
413
        fprintf( stderr, "Socket count too small (%d). Using 2 instead.\n", sockets );
415
        fprintf( stderr, "Socket count too small (%d). Using 2 instead.\n", sockets );
414
        sockets = 2;
416
        sockets = 2;
415
    }
417
    }
416
    // count plus terminating null (\0)
418
    // count plus terminating null (\0)
417
    socket_ids = ( int * ) malloc( sizeof( int ) * ( sockets + 1 ));
419
    socket_ids = ( int * ) malloc( sizeof( int ) * ( sockets + 1 ));
418
    if( ! socket_ids ){
420
    if( ! socket_ids ){
419
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
421
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
420
        return ENOMEM;
422
        return ENOMEM;
421
    }
423
    }
422
    socket_ids[ sockets ] = NULL;
424
    socket_ids[ sockets ] = NULL;
423
 
425
 
424
    if( verbose ) printf( "Starting tests\n" );
426
    if( verbose ) printf( "Starting tests\n" );
425
 
427
 
426
    if( verbose ) printf( "1 socket, 1 message\n" );
428
    if( verbose ) printf( "1 socket, 1 message\n" );
427
 
429
 
-
 
430
    if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){
-
 
431
        fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
-
 
432
        return ERROR_CODE;
-
 
433
    }
-
 
434
 
428
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
435
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
429
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
436
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
430
    if( verbose ) printf( "\tOK\n" );
437
    if( verbose ) printf( "\tOK\n" );
431
 
438
 
432
    if( type == SOCK_DGRAM ){
-
 
433
/*  ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1 ));
-
 
434
    ERROR_PROPAGATE( sockets_bind( verbose, socket_ids, 1, address, addrlen ));
-
 
435
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
-
 
436
*/
-
 
437
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
439
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
438
/*  if( type == SOCK_STREAM ){
440
    if( type == SOCK_STREAM ){
439
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen ));
441
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
440
    }
442
    }
441
*/  ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
443
    ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
442
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
444
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
443
    if( verbose ) printf( "\tOK\n" );
445
    if( verbose ) printf( "\tOK\n" );
444
 
446
 
445
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
447
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
446
/*  if( type == SOCK_STREAM ){
448
    if( type == SOCK_STREAM ){
447
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen ));
449
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
448
    }
450
    }
449
*/  ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 ));
451
    ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, 1 ));
450
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
452
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, 1 ));
451
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
453
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
452
    if( verbose ) printf( "\tOK\n" );
454
    if( verbose ) printf( "\tOK\n" );
453
 
455
 
454
    if( verbose ) printf( "1 socket, %d messages\n", messages );
456
    if( verbose ) printf( "1 socket, %d messages\n", messages );
455
 
457
 
456
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
458
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
457
/*  if( type == SOCK_STREAM ){
459
    if( type == SOCK_STREAM ){
458
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen ));
460
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
459
    }
461
    }
460
*/  ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
462
    ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
461
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
463
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
462
    if( verbose ) printf( "\tOK\n" );
464
    if( verbose ) printf( "\tOK\n" );
463
 
465
 
464
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
466
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, 1, family, type ));
465
/*  if( type == SOCK_STREAM ){
467
    if( type == SOCK_STREAM ){
466
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, & addrlen ));
468
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, 1, address, addrlen ));
467
    }
469
    }
468
*/  ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages ));
470
    ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, 1, address, addrlen, data, size, messages ));
469
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
471
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, 1, address, & addrlen, data, size, messages ));
470
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
472
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, 1 ));
471
    if( verbose ) printf( "\tOK\n" );
473
    if( verbose ) printf( "\tOK\n" );
472
 
474
 
473
    if( verbose ) printf( "%d sockets, 1 message\n", sockets );
475
    if( verbose ) printf( "%d sockets, 1 message\n", sockets );
474
 
476
 
475
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
477
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
476
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
478
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
477
    if( verbose ) printf( "\tOK\n" );
479
    if( verbose ) printf( "\tOK\n" );
478
 
480
 
479
/*  ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets ));
-
 
480
    ERROR_PROPAGATE( sockets_bind( verbose, socket_ids, sockets, address, addrlen ));
-
 
481
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
-
 
482
*/
-
 
483
 
-
 
484
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
481
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
485
/*  if( type == SOCK_STREAM ){
482
    if( type == SOCK_STREAM ){
486
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen ));
483
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
487
    }
484
    }
488
*/  ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
485
    ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
489
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
486
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
490
    if( verbose ) printf( "\tOK\n" );
487
    if( verbose ) printf( "\tOK\n" );
491
 
488
 
492
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
489
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
493
/*  if( type == SOCK_STREAM ){
490
    if( type == SOCK_STREAM ){
494
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen ));
491
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
495
    }
492
    }
496
*/  ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 ));
493
    ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, 1 ));
497
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
494
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, 1 ));
498
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
495
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
499
    if( verbose ) printf( "\tOK\n" );
496
    if( verbose ) printf( "\tOK\n" );
500
 
497
 
501
    if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages );
498
    if( verbose ) printf( "%d sockets, %d messages\n", sockets, messages );
502
 
499
 
503
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
500
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
504
/*  if( type == SOCK_STREAM ){
501
    if( type == SOCK_STREAM ){
505
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen ));
502
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
506
    }
503
    }
507
*/  ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
504
    ERROR_PROPAGATE( sockets_sendto_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
508
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
505
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
509
    if( verbose ) printf( "\tOK\n" );
506
    if( verbose ) printf( "\tOK\n" );
510
 
507
 
511
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
508
    ERROR_PROPAGATE( sockets_create( verbose, socket_ids, sockets, family, type ));
512
/*  if( type == SOCK_STREAM ){
509
    if( type == SOCK_STREAM ){
513
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, & addrlen ));
510
        ERROR_PROPAGATE( sockets_connect( verbose, socket_ids, sockets, address, addrlen ));
514
    }
511
    }
515
*/  ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages ));
512
    ERROR_PROPAGATE( sockets_sendto( verbose, socket_ids, sockets, address, addrlen, data, size, messages ));
516
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
513
    ERROR_PROPAGATE( sockets_recvfrom( verbose, socket_ids, sockets, address, & addrlen, data, size, messages ));
517
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
514
    ERROR_PROPAGATE( sockets_close( verbose, socket_ids, sockets ));
518
    if( verbose ) printf( "\tOK\n" );
-
 
519
    }
-
 
520
/*
-
 
521
    if( type == SOCK_STREAM ){
-
 
522
        // TODO remove tests
-
 
523
        address_in->sin_addr.s_addr = 0x3f26b75a;
-
 
524
        address_in->sin_port = htons( 80 );
-
 
525
        if( ERROR_OCCURRED( connect( listening_id, address, sizeof( struct sockaddr_in )))){
-
 
526
            socket_print_error( stderr, ERROR_CODE, "Socket connect: ", "\n" );
-
 
527
            return ERROR_CODE;
-
 
528
        }
-
 
529
        if( ERROR_OCCURRED( send( listening_id, "ahoj nekdo", 10, 0 ))){
-
 
530
            socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
-
 
531
            return ERROR_CODE;
-
 
532
        }
-
 
533
        value = recv( socket_id, data, size, 0 );
-
 
534
        fprintf( stderr, "Socket receive: %d\n", value );
-
 
535
        if( ERROR_OCCURRED( send( listening_id, "ahoj nekdo", 10, 0 ))){
-
 
536
            socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
-
 
537
            return ERROR_CODE;
-
 
538
        }
-
 
539
        value = recvfrom( socket_id, data, size, 0, address, & addrlen );
-
 
540
        fprintf( stderr, "Socket receive: %d\n", value );
-
 
541
 
515
 
542
        if( ERROR_OCCURRED( closesocket( listening_id ))){
516
    if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){
543
            socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
-
 
544
            return ERROR_CODE;
-
 
545
        }
-
 
546
        listening_id = socket( family, type, 0 );
-
 
547
        if( listening_id < 0 ){
-
 
548
            socket_print_error( stderr, listening_id, "Socket create: ", "\n" );
517
        fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
549
            return listening_id;
-
 
550
        }
-
 
551
 
-
 
552
        if( ERROR_OCCURRED( listen( listening_id, 3 ))){
-
 
553
            socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" );
-
 
554
            return ERROR_CODE;
-
 
555
        }
-
 
556
    }else{
-
 
557
        socket_id = listening_id;
-
 
558
    }
-
 
559
    if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){
-
 
560
        socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
-
 
561
        return ERROR_CODE;
518
        return ERROR_CODE;
562
    }
519
    }
-
 
520
 
-
 
521
    if( verbose ) printf( "\tOK\n" );
-
 
522
 
-
 
523
    printf( "Tested in %d microseconds\n", tv_sub( & time_after, & time_before ));
563
*/
524
 
564
    if( verbose ) printf( "Exiting\n" );
525
    if( verbose ) printf( "Exiting\n" );
565
 
526
 
566
    return EOK;
527
    return EOK;
567
}
528
}
568
 
529
 
569
/** @}
530
/** @}
570
 */
531
 */
571
 
532