Subversion Repositories HelenOS

Rev

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

Rev 4734 Rev 4737
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
 *  Echo application.
34
 *  Echo application.
35
 *  Answers received packets.
35
 *  Answers received packets.
36
 */
36
 */
37
 
37
 
38
#include <malloc.h>
38
#include <malloc.h>
39
#include <stdio.h>
39
#include <stdio.h>
40
#include <string.h>
40
#include <string.h>
41
#include <task.h>
41
#include <task.h>
42
 
42
 
43
#include "../../include/in.h"
43
#include "../../include/in.h"
44
#include "../../include/in6.h"
44
#include "../../include/in6.h"
45
#include "../../include/inet.h"
45
#include "../../include/inet.h"
46
#include "../../include/socket.h"
46
#include "../../include/socket.h"
47
 
47
 
48
#include "../../err.h"
48
#include "../../err.h"
49
 
49
 
50
#include "../parse.h"
50
#include "../parse.h"
51
#include "../print_error.h"
51
#include "../print_error.h"
52
 
52
 
53
/** Echo module name.
53
/** Echo module name.
54
 */
54
 */
55
#define NAME    "Echo"
55
#define NAME    "Echo"
56
 
56
 
57
/** Module entry point.
57
/** Module entry point.
58
 *  Reads command line parameters and starts listenning.
58
 *  Reads command line parameters and starts listenning.
59
 *  @param argc The number of command line parameters. Input parameter.
59
 *  @param argc The number of command line parameters. Input parameter.
60
 *  @param argv The command line parameters. Input parameter.
60
 *  @param argv The command line parameters. Input parameter.
61
 *  @returns EOK on success.
61
 *  @returns EOK on success.
62
 */
62
 */
63
int     main( int argc, char * argv[] );
63
int     main( int argc, char * argv[] );
64
 
64
 
65
/** Prints the application help.
65
/** Prints the application help.
66
 */
66
 */
67
void    print_help( void );
67
void    print_help( void );
68
 
68
 
69
/** Translates the character string to the protocol family number.
69
/** Translates the character string to the protocol family number.
70
 *  @param name The protocol family name. Input parameter.
70
 *  @param name The protocol family name. Input parameter.
71
 *  @returns The corresponding protocol family number.
71
 *  @returns The corresponding protocol family number.
72
 *  @returns EPFNOSUPPORTED if the protocol family is not supported.
72
 *  @returns EPFNOSUPPORTED if the protocol family is not supported.
73
 */
73
 */
74
int     parse_protocol_family( const char * name );
74
int     parse_protocol_family( const char * name );
75
 
75
 
76
/** Translates the character string to the socket type number.
76
/** Translates the character string to the socket type number.
77
 *  @param name The socket type name. Input parameter.
77
 *  @param name The socket type name. Input parameter.
78
 *  @returns The corresponding socket type number.
78
 *  @returns The corresponding socket type number.
79
 *  @returns ESOCKNOSUPPORTED if the socket type is not supported.
79
 *  @returns ESOCKNOSUPPORTED if the socket type is not supported.
80
 */
80
 */
81
int     parse_socket_type( const char * name );
81
int     parse_socket_type( const char * name );
82
 
82
 
83
void print_help( void ){
83
void print_help( void ){
84
    printf(
84
    printf(
85
        "Network Echo aplication\n" \
85
        "Network Echo aplication\n" \
86
        "Usage: echo [options]\n" \
86
        "Usage: echo [options]\n" \
87
        "Where options are:\n" \
87
        "Where options are:\n" \
88
        "-c count | --count=count\n" \
88
        "-c count | --count=count\n" \
89
        "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
89
        "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
90
        "\n" \
90
        "\n" \
91
        "-f protocol_family | --family=protocol_family\n" \
91
        "-f protocol_family | --family=protocol_family\n" \
92
        "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
92
        "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
93
        "\n" \
93
        "\n" \
94
        "-h | --help\n" \
94
        "-h | --help\n" \
95
        "\tShow this application help.\n"
95
        "\tShow this application help.\n"
96
        "\n" \
96
        "\n" \
97
        "-p port_number | --port=port_number\n" \
97
        "-p port_number | --port=port_number\n" \
98
        "\tThe port number the application should listen at. The default is 7.\n" \
98
        "\tThe port number the application should listen at. The default is 7.\n" \
99
        "\n" \
99
        "\n" \
100
        "-r reply_string | --reply=reply_string\n" \
100
        "-r reply_string | --reply=reply_string\n" \
101
        "\tThe constant reply string. The default is the original data received.\n" \
101
        "\tThe constant reply string. The default is the original data received.\n" \
102
        "\n" \
102
        "\n" \
103
        "-s receive_size | --size=receive_size\n" \
103
        "-s receive_size | --size=receive_size\n" \
104
        "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
104
        "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
105
        "\n" \
105
        "\n" \
106
        "-t socket_type | --type=socket_type\n" \
106
        "-t socket_type | --type=socket_type\n" \
107
        "\tThe listenning socket type. Only the SOCK_DGRAM and the SOCK_STREAM are supported.\n" \
107
        "\tThe listenning socket type. Only the SOCK_DGRAM and the SOCK_STREAM are supported.\n" \
108
        "\n" \
108
        "\n" \
109
        "-v | --verbose\n" \
109
        "-v | --verbose\n" \
110
        "\tShow all output messages.\n"
110
        "\tShow all output messages.\n"
111
    );
111
    );
112
}
112
}
113
 
113
 
114
int parse_protocol_family( const char * name ){
114
int parse_protocol_family( const char * name ){
115
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
115
    if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
116
        return PF_INET;
116
        return PF_INET;
117
    }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
117
    }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
118
        return PF_INET6;
118
        return PF_INET6;
119
    }
119
    }
120
    return EPFNOSUPPORT;
120
    return EPFNOSUPPORT;
121
}
121
}
122
 
122
 
123
int parse_socket_type( const char * name ){
123
int parse_socket_type( const char * name ){
124
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
124
    if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
125
        return SOCK_DGRAM;
125
        return SOCK_DGRAM;
126
    }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){
126
    }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){
127
        return SOCK_STREAM;
127
        return SOCK_STREAM;
128
    }
128
    }
129
    return ESOCKTNOSUPPORT;
129
    return ESOCKTNOSUPPORT;
130
}
130
}
131
 
131
 
132
int main( int argc, char * argv[] ){
132
int main( int argc, char * argv[] ){
133
    ERROR_DECLARE;
133
    ERROR_DECLARE;
134
 
134
 
135
    size_t              size            = 1024;
135
    size_t              size            = 1024;
136
    int                 verbose         = 0;
136
    int                 verbose         = 0;
137
    char *              reply           = NULL;
137
    char *              reply           = NULL;
138
    sock_type_t         type            = SOCK_DGRAM;
138
    sock_type_t         type            = SOCK_DGRAM;
139
    int                 count           = -1;
139
    int                 count           = -1;
140
    int                 family          = PF_INET;
140
    int                 family          = PF_INET;
141
    uint16_t            port            = 7;
141
    uint16_t            port            = 7;
142
 
142
 
143
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
143
    socklen_t           max_length      = sizeof( struct sockaddr_in6 );
144
    uint8_t             address_data[ max_length ];
144
    uint8_t             address_data[ max_length ];
145
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
145
    struct sockaddr *       address     = ( struct sockaddr * ) address_data;
146
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
146
    struct sockaddr_in *    address_in      = ( struct sockaddr_in * ) address;
147
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
147
    struct sockaddr_in6 *   address_in6 = ( struct sockaddr_in6 * ) address;
148
    socklen_t           addrlen;
148
    socklen_t           addrlen;
149
    char                address_string[ INET6_ADDRSTRLEN ];
149
    char                address_string[ INET6_ADDRSTRLEN ];
150
    uint8_t *           address_start;
150
    uint8_t *           address_start;
151
    int                 socket_id;
151
    int                 socket_id;
152
    int                 listening_id;
152
    int                 listening_id;
153
    char *              data;
153
    char *              data;
154
    size_t              length;
154
    size_t              length;
155
    int                 index;
155
    int                 index;
156
    size_t              reply_length;
156
    size_t              reply_length;
157
    int                 value;
157
    int                 value;
158
 
158
 
159
    printf( "Task %d - ", task_get_id());
159
    printf( "Task %d - ", task_get_id());
160
    printf( "%s\n", NAME );
160
    printf( "%s\n", NAME );
161
 
161
 
162
    for( index = 1; index < argc; ++ index ){
162
    for( index = 1; index < argc; ++ index ){
163
        if( argv[ index ][ 0 ] == '-' ){
163
        if( argv[ index ][ 0 ] == '-' ){
164
            switch( argv[ index ][ 1 ] ){
164
            switch( argv[ index ][ 1 ] ){
165
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 ));
165
                case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 ));
166
                            break;
166
                            break;
167
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
167
                case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
168
                            break;
168
                            break;
169
                case 'h':   print_help();
169
                case 'h':   print_help();
170
                            return EOK;
170
                            return EOK;
171
                            break;
171
                            break;
172
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
172
                case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
173
                            port = ( uint16_t ) value;
173
                            port = ( uint16_t ) value;
174
                            break;
174
                            break;
175
                case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
175
                case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
176
                            break;
176
                            break;
177
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 ));
177
                case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 ));
178
                            size = (value >= 0 ) ? ( size_t ) value : 0;
178
                            size = (value >= 0 ) ? ( size_t ) value : 0;
179
                            break;
179
                            break;
180
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type ));
180
                case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type ));
181
                            type = ( sock_type_t ) value;
181
                            type = ( sock_type_t ) value;
182
                            break;
182
                            break;
183
                case 'v':   verbose = 1;
183
                case 'v':   verbose = 1;
184
                            break;
184
                            break;
185
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
185
                case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
186
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 ));
186
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 ));
187
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
187
                            }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
188
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
188
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
189
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
189
                            }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
190
                                print_help();
190
                                print_help();
191
                                return EOK;
191
                                return EOK;
192
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
192
                            }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
193
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
193
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
194
                                port = ( uint16_t ) value;
194
                                port = ( uint16_t ) value;
195
                            }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
195
                            }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
196
                                ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
196
                                ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
197
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
197
                            }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
198
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 ));
198
                                ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 ));
199
                                size = (value >= 0 ) ? ( size_t ) value : 0;
199
                                size = (value >= 0 ) ? ( size_t ) value : 0;
200
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
200
                            }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
201
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type ));
201
                                ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type ));
202
                                type = ( sock_type_t ) value;
202
                                type = ( sock_type_t ) value;
203
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
203
                            }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
204
                                verbose = 1;
204
                                verbose = 1;
205
                            }else{
205
                            }else{
206
                                print_unrecognized( index, argv[ index ] + 2 );
206
                                print_unrecognized( index, argv[ index ] + 2 );
207
                                print_help();
207
                                print_help();
208
                                return EINVAL;
208
                                return EINVAL;
209
                            }
209
                            }
210
                            break;
210
                            break;
211
                default:
211
                default:
212
                    print_unrecognized( index, argv[ index ] + 1 );
212
                    print_unrecognized( index, argv[ index ] + 1 );
213
                    print_help();
213
                    print_help();
214
                    return EINVAL;
214
                    return EINVAL;
215
            }
215
            }
216
        }else{
216
        }else{
217
            print_unrecognized( index, argv[ index ] );
217
            print_unrecognized( index, argv[ index ] );
218
            print_help();
218
            print_help();
219
            return EINVAL;
219
            return EINVAL;
220
        }
220
        }
221
    }
221
    }
222
 
222
 
223
    if( size <= 0 ){
223
    if( size <= 0 ){
224
        fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
224
        fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
225
        size = 1024;
225
        size = 1024;
226
    }
226
    }
227
    // size plus terminating null (\0)
227
    // size plus terminating null (\0)
228
    data = ( char * ) malloc( size + 1 );
228
    data = ( char * ) malloc( size + 1 );
229
    if( ! data ){
229
    if( ! data ){
230
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
230
        fprintf( stderr, "Failed to allocate receive buffer.\n" );
231
        return ENOMEM;
231
        return ENOMEM;
232
    }
232
    }
233
 
233
 
234
    reply_length = reply ? str_length( reply ) : 0;
234
    reply_length = reply ? str_length( reply ) : 0;
235
 
235
 
236
    listening_id = socket( family, type, 0 );
236
    listening_id = socket( family, type, 0 );
237
    if( listening_id < 0 ){
237
    if( listening_id < 0 ){
238
        socket_print_error( stderr, listening_id, "Socket create: ", "\n" );
238
        socket_print_error( stderr, listening_id, "Socket create: ", "\n" );
239
        return listening_id;
239
        return listening_id;
240
    }
240
    }
241
 
241
 
242
    bzero( address_data, max_length );
242
    bzero( address_data, max_length );
243
    switch( family ){
243
    switch( family ){
244
        case PF_INET:
244
        case PF_INET:
245
            address_in->sin_family = AF_INET;
245
            address_in->sin_family = AF_INET;
246
            address_in->sin_port = htons( port );
246
            address_in->sin_port = htons( port );
247
            addrlen = sizeof( struct sockaddr_in );
247
            addrlen = sizeof( struct sockaddr_in );
248
            break;
248
            break;
249
        case PF_INET6:
249
        case PF_INET6:
250
            address_in6->sin6_family = AF_INET6;
250
            address_in6->sin6_family = AF_INET6;
251
            address_in6->sin6_port = htons( port );
251
            address_in6->sin6_port = htons( port );
252
            addrlen = sizeof( struct sockaddr_in6 );
252
            addrlen = sizeof( struct sockaddr_in6 );
253
            break;
253
            break;
254
        default:
254
        default:
255
            fprintf( stderr, "Protocol family is not supported\n" );
255
            fprintf( stderr, "Protocol family is not supported\n" );
256
            return EAFNOSUPPORT;
256
            return EAFNOSUPPORT;
257
    }
257
    }
258
 
258
 
259
    listening_id = socket( family, type, 0 );
259
    listening_id = socket( family, type, 0 );
260
    if( listening_id < 0 ){
260
    if( listening_id < 0 ){
261
        socket_print_error( stderr, listening_id, "Socket create: ", "\n" );
261
        socket_print_error( stderr, listening_id, "Socket create: ", "\n" );
262
        return listening_id;
262
        return listening_id;
263
    }
263
    }
264
 
264
 
265
    if( type == SOCK_STREAM ){
265
    if( type == SOCK_STREAM ){
266
        if( ERROR_OCCURRED( listen( listening_id, 3 ))){
266
        if( ERROR_OCCURRED( listen( listening_id, 3 ))){
267
            socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" );
267
            socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" );
268
            return ERROR_CODE;
268
            return ERROR_CODE;
269
        }
269
        }
270
    }else{
-
 
271
        socket_id = listening_id;
-
 
272
    }
270
    }
273
 
271
 
-
 
272
    socket_id = listening_id;
-
 
273
 
274
    if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){
274
    if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){
275
        socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
275
        socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
276
        return ERROR_CODE;
276
        return ERROR_CODE;
277
    }
277
    }
278
 
278
 
279
    if( verbose ) printf( "Listenning at %d\n", port );
279
    if( verbose ) printf( "Listenning at %d\n", port );
280
 
280
 
281
    while( count ){
281
    while( count ){
282
        addrlen = max_length;
282
        addrlen = max_length;
283
        if( type == SOCK_STREAM ){
283
        if( type == SOCK_STREAM ){
284
            socket_id = accept( listening_id, address, & addrlen );
284
            socket_id = accept( listening_id, address, & addrlen );
285
            if( socket_id <= 0 ){
285
            if( socket_id <= 0 ){
286
                socket_print_error( stderr, socket_id, "Socket accept: ", "\n" );
286
                socket_print_error( stderr, socket_id, "Socket accept: ", "\n" );
287
            }
287
            }
288
        }
288
        }
289
        if( socket_id > 0 ){
289
        if( socket_id > 0 ){
290
            value = recvfrom( socket_id, data, size, 0, address, & addrlen );
290
            value = recvfrom( socket_id, data, size, 0, address, & addrlen );
291
            if( value < 0 ){
291
            if( value < 0 ){
292
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
292
                socket_print_error( stderr, value, "Socket receive: ", "\n" );
293
            }else{
293
            }else{
294
                length = ( size_t ) value;
294
                length = ( size_t ) value;
295
                if( verbose ){
295
                if( verbose ){
296
                    address_start = NULL;
296
                    address_start = NULL;
297
                    switch( address->sa_family ){
297
                    switch( address->sa_family ){
298
                        case AF_INET:
298
                        case AF_INET:
299
                            port = ntohs( address_in->sin_port );
299
                            port = ntohs( address_in->sin_port );
300
                            address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
300
                            address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
301
                            break;
301
                            break;
302
                        case AF_INET6:
302
                        case AF_INET6:
303
                            port = ntohs( address_in6->sin6_port );
303
                            port = ntohs( address_in6->sin6_port );
304
                            address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
304
                            address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
305
                            break;
305
                            break;
306
                        default:
306
                        default:
307
                            fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family );
307
                            fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family );
308
                    }
308
                    }
309
                    if( address_start ){
309
                    if( address_start ){
310
                        if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
310
                        if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
311
                            fprintf( stderr, "Received address error %d\n", ERROR_CODE );
311
                            fprintf( stderr, "Received address error %d\n", ERROR_CODE );
312
                        }else{
312
                        }else{
313
                            data[ length ] = '\0';
313
                            data[ length ] = '\0';
314
                            printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data );
314
                            printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data );
315
                        }
315
                        }
316
                    }
316
                    }
317
                }
317
                }
318
                if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){
318
                if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){
319
                    socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
319
                    socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
320
                }
320
                }
321
            }
321
            }
322
            if( type == SOCK_STREAM ){
322
            if( type == SOCK_STREAM ){
323
                if( ERROR_OCCURRED( closesocket( socket_id ))){
323
                if( ERROR_OCCURRED( closesocket( socket_id ))){
324
                    socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
324
                    socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
325
                }
325
                }
326
            }
326
            }
327
        }
327
        }
328
        if( count > 0 ){
328
        if( count > 0 ){
329
            -- count;
329
            -- count;
330
            if( verbose ) printf( "Waiting for next %d packet(s)\n", count );
330
            if( verbose ) printf( "Waiting for next %d packet(s)\n", count );
331
        }
331
        }
332
    }
332
    }
333
 
333
 
334
    if( verbose ) printf( "Closing the socket\n" );
334
    if( verbose ) printf( "Closing the socket\n" );
335
 
335
 
336
    if( ERROR_OCCURRED( closesocket( listening_id ))){
336
    if( ERROR_OCCURRED( closesocket( listening_id ))){
337
        socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
337
        socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
338
        return ERROR_CODE;
338
        return ERROR_CODE;
339
    }
339
    }
340
 
340
 
341
    if( verbose ) printf( "Exiting\n" );
341
    if( verbose ) printf( "Exiting\n" );
342
 
342
 
343
    return EOK;
343
    return EOK;
344
}
344
}
345
 
345
 
346
/** @}
346
/** @}
347
 */
347
 */
348
 
348