Rev 4731 | Rev 4737 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4731 | Rev 4734 | ||
---|---|---|---|
Line 102... | Line 102... | ||
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 is 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 | } |
Line 121... | Line 121... | ||
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 ){ |
|
- | 127 | return SOCK_STREAM; |
|
126 | } |
128 | } |
127 | return ESOCKTNOSUPPORT; |
129 | return ESOCKTNOSUPPORT; |
128 | } |
130 | } |
129 | 131 | ||
130 | int main( int argc, char * argv[] ){ |
132 | int main( int argc, char * argv[] ){ |
Line 145... | Line 147... | ||
145 | struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; |
147 | struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; |
146 | socklen_t addrlen; |
148 | socklen_t addrlen; |
147 | char address_string[ INET6_ADDRSTRLEN ]; |
149 | char address_string[ INET6_ADDRSTRLEN ]; |
148 | uint8_t * address_start; |
150 | uint8_t * address_start; |
149 | int socket_id; |
151 | int socket_id; |
- | 152 | int listening_id; |
|
150 | char * data; |
153 | char * data; |
151 | size_t length; |
154 | size_t length; |
152 | int index; |
155 | int index; |
153 | size_t reply_length; |
156 | size_t reply_length; |
154 | int value; |
157 | int value; |
Line 157... | Line 160... | ||
157 | printf( "%s\n", NAME ); |
160 | printf( "%s\n", NAME ); |
158 | 161 | ||
159 | for( index = 1; index < argc; ++ index ){ |
162 | for( index = 1; index < argc; ++ index ){ |
160 | if( argv[ index ][ 0 ] == '-' ){ |
163 | if( argv[ index ][ 0 ] == '-' ){ |
161 | switch( argv[ index ][ 1 ] ){ |
164 | switch( argv[ index ][ 1 ] ){ |
162 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 )); |
165 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 )); |
163 | break; |
166 | break; |
164 | 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 )); |
165 | break; |
168 | break; |
166 | case 'h': print_help(); |
169 | case 'h': print_help(); |
167 | return EOK; |
170 | return EOK; |
Line 172... | Line 175... | ||
172 | 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 )); |
173 | break; |
176 | break; |
174 | 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 )); |
175 | size = (value >= 0 ) ? ( size_t ) value : 0; |
178 | size = (value >= 0 ) ? ( size_t ) value : 0; |
176 | break; |
179 | break; |
177 | 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 )); |
178 | type = ( sock_type_t ) value; |
181 | type = ( sock_type_t ) value; |
179 | break; |
182 | break; |
180 | case 'v': verbose = 1; |
183 | case 'v': verbose = 1; |
181 | break; |
184 | break; |
182 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
185 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
183 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 )) |
186 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 )); |
184 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
187 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
185 | 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 )); |
186 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
189 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
187 | print_help(); |
190 | print_help(); |
188 | return EOK; |
191 | return EOK; |
Line 193... | Line 196... | ||
193 | 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 )); |
194 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
197 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
195 | 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 )); |
196 | size = (value >= 0 ) ? ( size_t ) value : 0; |
199 | size = (value >= 0 ) ? ( size_t ) value : 0; |
197 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
200 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
198 | 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 )); |
199 | type = ( sock_type_t ) value; |
202 | type = ( sock_type_t ) value; |
200 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
203 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
201 | verbose = 1; |
204 | verbose = 1; |
202 | }else{ |
205 | }else{ |
203 | print_unrecognized( index, argv[ index ] + 2 ); |
206 | print_unrecognized( index, argv[ index ] + 2 ); |
Line 228... | Line 231... | ||
228 | return ENOMEM; |
231 | return ENOMEM; |
229 | } |
232 | } |
230 | 233 | ||
231 | reply_length = reply ? str_length( reply ) : 0; |
234 | reply_length = reply ? str_length( reply ) : 0; |
232 | 235 | ||
233 | socket_id = socket( family, type, 0 ); |
236 | listening_id = socket( family, type, 0 ); |
234 | if( socket_id < 0 ){ |
237 | if( listening_id < 0 ){ |
235 | fprintf( stderr, "Socket create error %d\n", socket_id ); |
238 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
236 | return socket_id; |
239 | return listening_id; |
237 | } |
240 | } |
238 | 241 | ||
239 | bzero( address_data, max_length ); |
242 | bzero( address_data, max_length ); |
240 | switch( family ){ |
243 | switch( family ){ |
241 | case PF_INET: |
244 | case PF_INET: |
242 | address_in->sin_family = AF_INET; |
245 | address_in->sin_family = AF_INET; |
243 | address_in->sin_port = port; |
246 | address_in->sin_port = htons( port ); |
244 | addrlen = sizeof( struct sockaddr_in ); |
247 | addrlen = sizeof( struct sockaddr_in ); |
245 | break; |
248 | break; |
246 | case PF_INET6: |
249 | case PF_INET6: |
247 | address_in6->sin6_family = AF_INET6; |
250 | address_in6->sin6_family = AF_INET6; |
248 | address_in6->sin6_port = port; |
251 | address_in6->sin6_port = htons( port ); |
249 | addrlen = sizeof( struct sockaddr_in6 ); |
252 | addrlen = sizeof( struct sockaddr_in6 ); |
250 | break; |
253 | break; |
251 | default: |
254 | default: |
252 | fprintf( stderr, "Protocol family is not supported\n" ); |
255 | fprintf( stderr, "Protocol family is not supported\n" ); |
253 | return EAFNOSUPPORT; |
256 | return EAFNOSUPPORT; |
254 | } |
257 | } |
- | 258 | ||
- | 259 | listening_id = socket( family, type, 0 ); |
|
- | 260 | if( listening_id < 0 ){ |
|
- | 261 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
|
- | 262 | return listening_id; |
|
- | 263 | } |
|
- | 264 | ||
- | 265 | if( type == SOCK_STREAM ){ |
|
- | 266 | if( ERROR_OCCURRED( listen( listening_id, 3 ))){ |
|
- | 267 | socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" ); |
|
- | 268 | return ERROR_CODE; |
|
- | 269 | } |
|
- | 270 | }else{ |
|
- | 271 | socket_id = listening_id; |
|
- | 272 | } |
|
- | 273 | ||
255 | if( ERROR_OCCURRED( bind( socket_id, address, addrlen ))){ |
274 | if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){ |
256 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
275 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
257 | return ERROR_CODE; |
276 | return ERROR_CODE; |
258 | } |
277 | } |
259 | 278 | ||
260 | if( verbose ) printf( "Listenning at %d\n", port ); |
279 | if( verbose ) printf( "Listenning at %d\n", port ); |
261 | 280 | ||
262 | while( count ){ |
281 | while( count ){ |
263 | addrlen = max_length; |
282 | addrlen = max_length; |
- | 283 | if( type == SOCK_STREAM ){ |
|
- | 284 | socket_id = accept( listening_id, address, & addrlen ); |
|
- | 285 | if( socket_id <= 0 ){ |
|
- | 286 | socket_print_error( stderr, socket_id, "Socket accept: ", "\n" ); |
|
- | 287 | } |
|
- | 288 | } |
|
- | 289 | if( socket_id > 0 ){ |
|
264 | value = recvfrom( socket_id, data, size, 0, address, & addrlen ); |
290 | value = recvfrom( socket_id, data, size, 0, address, & addrlen ); |
265 | if( value < 0 ){ |
291 | if( value < 0 ){ |
266 | socket_print_error( stderr, value, "Socket receive: ", "\n" ); |
292 | socket_print_error( stderr, value, "Socket receive: ", "\n" ); |
267 | }else{ |
293 | }else{ |
268 | length = ( size_t ) value; |
294 | length = ( size_t ) value; |
269 | if( verbose ){ |
295 | if( verbose ){ |
270 | address_start = NULL; |
296 | address_start = NULL; |
271 | switch( address->sa_family ){ |
297 | switch( address->sa_family ){ |
272 | case AF_INET: |
298 | case AF_INET: |
273 | port = address_in->sin_port; |
299 | port = ntohs( address_in->sin_port ); |
274 | address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; |
300 | address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; |
275 | break; |
301 | break; |
276 | case AF_INET6: |
302 | case AF_INET6: |
277 | port = address_in6->sin6_port; |
303 | port = ntohs( address_in6->sin6_port ); |
278 | address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; |
304 | address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; |
279 | break; |
305 | break; |
280 | default: |
306 | default: |
281 | 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 ); |
282 | } |
- | |
283 | if( address_start ){ |
- | |
284 | if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){ |
- | |
285 | fprintf( stderr, "Received address error %d\n", ERROR_CODE ); |
- | |
286 | }else{ |
- | |
287 | data[ length ] = '\0'; |
- | |
288 | printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data ); |
- | |
289 | } |
308 | } |
- | 309 | if( address_start ){ |
|
- | 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 ); |
|
- | 312 | }else{ |
|
- | 313 | data[ length ] = '\0'; |
|
- | 314 | printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data ); |
|
- | 315 | } |
|
- | 316 | } |
|
- | 317 | } |
|
- | 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" ); |
|
290 | } |
320 | } |
291 | } |
321 | } |
- | 322 | if( type == SOCK_STREAM ){ |
|
292 | if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){ |
323 | if( ERROR_OCCURRED( closesocket( socket_id ))){ |
293 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); |
324 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
- | 325 | } |
|
294 | } |
326 | } |
295 | } |
327 | } |
296 | if( count > 0 ){ |
328 | if( count > 0 ){ |
297 | -- count; |
329 | -- count; |
298 | if( verbose ) printf( "Waiting for next %d packet(s)\n", count ); |
330 | if( verbose ) printf( "Waiting for next %d packet(s)\n", count ); |
299 | } |
331 | } |
300 | } |
332 | } |
301 | 333 | ||
302 | if( verbose ) printf( "Closing the socket\n" ); |
334 | if( verbose ) printf( "Closing the socket\n" ); |
303 | 335 | ||
304 | if( ERROR_OCCURRED( closesocket( socket_id ))){ |
336 | if( ERROR_OCCURRED( closesocket( listening_id ))){ |
305 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
337 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
306 | return ERROR_CODE; |
338 | return ERROR_CODE; |
307 | } |
339 | } |
308 | 340 | ||
309 | if( verbose ) printf( "Exiting\n" ); |
341 | if( verbose ) printf( "Exiting\n" ); |