Changes in uspace/srv/net/app/echo/echo.c [aadf01e:1b053ca2] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/echo/echo.c
raadf01e r1b053ca2 61 61 * @returns EOK on success. 62 62 */ 63 int main(int argc, char * argv[]);63 int main( int argc, char * argv[] ); 64 64 65 65 /** Prints the application help. 66 66 */ 67 void echo_print_help(void);67 void echo_print_help( void ); 68 68 69 69 /** Translates the character string to the protocol family number. … … 72 72 * @returns EPFNOSUPPORTED if the protocol family is not supported. 73 73 */ 74 int echo_parse_protocol_family(const char * name);74 int echo_parse_protocol_family( const char * name ); 75 75 76 76 /** Translates the character string to the socket type number. … … 79 79 * @returns ESOCKNOSUPPORTED if the socket type is not supported. 80 80 */ 81 int echo_parse_socket_type(const char * name);82 83 void echo_print_help( void){81 int echo_parse_socket_type( const char * name ); 82 83 void echo_print_help( void ){ 84 84 printf( 85 85 "Network Echo aplication\n" \ … … 115 115 } 116 116 117 int echo_parse_protocol_family( const char * name){118 if( str_lcmp(name, "PF_INET", 7) == 0){117 int echo_parse_protocol_family( const char * name ){ 118 if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ 119 119 return PF_INET; 120 }else if( str_lcmp(name, "PF_INET6", 8) == 0){120 }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){ 121 121 return PF_INET6; 122 122 } … … 124 124 } 125 125 126 int echo_parse_socket_type( const char * name){127 if( str_lcmp(name, "SOCK_DGRAM", 11) == 0){126 int echo_parse_socket_type( const char * name ){ 127 if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){ 128 128 return SOCK_DGRAM; 129 }else if( str_lcmp(name, "SOCK_STREAM", 12) == 0){129 }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){ 130 130 return SOCK_STREAM; 131 131 } … … 133 133 } 134 134 135 int main( int argc, char * argv[]){135 int main( int argc, char * argv[] ){ 136 136 ERROR_DECLARE; 137 137 138 size_t 139 int 140 char * 141 sock_type_t 142 int 143 int 144 uint16_t 145 int 146 147 socklen_t max_length = sizeof(struct sockaddr_in6);148 uint8_t address_data[max_length];149 struct sockaddr * address = (struct sockaddr *) address_data;150 struct sockaddr_in * address_in = (struct sockaddr_in *) address;151 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address;152 socklen_t 153 char address_string[INET6_ADDRSTRLEN];154 uint8_t * 155 int 156 int 138 size_t size = 1024; 139 int verbose = 0; 140 char * reply = NULL; 141 sock_type_t type = SOCK_DGRAM; 142 int count = -1; 143 int family = PF_INET; 144 uint16_t port = 7; 145 int backlog = 3; 146 147 socklen_t max_length = sizeof( struct sockaddr_in6 ); 148 uint8_t address_data[ max_length ]; 149 struct sockaddr * address = ( struct sockaddr * ) address_data; 150 struct sockaddr_in * address_in = ( struct sockaddr_in * ) address; 151 struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; 152 socklen_t addrlen; 153 char address_string[ INET6_ADDRSTRLEN ]; 154 uint8_t * address_start; 155 int socket_id; 156 int listening_id; 157 157 char * data; 158 size_t length; 159 int index; 160 size_t reply_length; 161 int value; 162 163 printf("Task %d - ", task_get_id()); 164 printf("%s\n", NAME); 165 166 for(index = 1; index < argc; ++ index){ 167 if(argv[index][0] == '-'){ 168 switch(argv[index][1]){ 169 case 'b': 170 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 0)); 171 break; 172 case 'c': 173 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 0)); 174 break; 175 case 'f': 176 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, echo_parse_protocol_family)); 177 break; 178 case 'h': 179 echo_print_help(); 180 return EOK; 181 break; 182 case 'p': 183 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 0)); 184 port = (uint16_t) value; 185 break; 186 case 'r': 187 ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 0)); 188 break; 189 case 's': 190 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 0)); 191 size = (value >= 0) ? (size_t) value : 0; 192 break; 193 case 't': 194 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, echo_parse_socket_type)); 195 type = (sock_type_t) value; 196 break; 197 case 'v': 198 verbose = 1; 199 break; 200 case '-': 201 if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){ 202 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 8)); 203 }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){ 204 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 8)); 205 }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 206 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, echo_parse_protocol_family)); 207 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 208 echo_print_help(); 209 return EOK; 210 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 211 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 7)); 212 port = (uint16_t) value; 213 }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){ 214 ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 8)); 215 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){ 216 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 7)); 217 size = (value >= 0) ? (size_t) value : 0; 218 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 219 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, echo_parse_socket_type)); 220 type = (sock_type_t) value; 221 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 222 verbose = 1; 223 }else{ 224 print_unrecognized(index, argv[index] + 2); 225 echo_print_help(); 226 return EINVAL; 227 } 228 break; 158 size_t length; 159 int index; 160 size_t reply_length; 161 int value; 162 163 printf( "Task %d - ", task_get_id()); 164 printf( "%s\n", NAME ); 165 166 for( index = 1; index < argc; ++ index ){ 167 if( argv[ index ][ 0 ] == '-' ){ 168 switch( argv[ index ][ 1 ] ){ 169 case 'b': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 0 )); 170 break; 171 case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 )); 172 break; 173 case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, echo_parse_protocol_family )); 174 break; 175 case 'h': echo_print_help(); 176 return EOK; 177 break; 178 case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); 179 port = ( uint16_t ) value; 180 break; 181 case 'r': ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 )); 182 break; 183 case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 )); 184 size = (value >= 0 ) ? ( size_t ) value : 0; 185 break; 186 case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, echo_parse_socket_type )); 187 type = ( sock_type_t ) value; 188 break; 189 case 'v': verbose = 1; 190 break; 191 case '-': if( str_lcmp( argv[ index ] + 2, "backlog=", 6 ) == 0 ){ 192 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 8 )); 193 }else if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ 194 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 )); 195 }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ 196 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, echo_parse_protocol_family )); 197 }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ 198 echo_print_help(); 199 return EOK; 200 }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ 201 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); 202 port = ( uint16_t ) value; 203 }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){ 204 ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 )); 205 }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ 206 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 )); 207 size = (value >= 0 ) ? ( size_t ) value : 0; 208 }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ 209 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, echo_parse_socket_type )); 210 type = ( sock_type_t ) value; 211 }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ 212 verbose = 1; 213 }else{ 214 print_unrecognized( index, argv[ index ] + 2 ); 215 echo_print_help(); 216 return EINVAL; 217 } 218 break; 229 219 default: 230 print_unrecognized( index, argv[index] + 1);220 print_unrecognized( index, argv[ index ] + 1 ); 231 221 echo_print_help(); 232 222 return EINVAL; 233 223 } 234 224 }else{ 235 print_unrecognized( index, argv[index]);225 print_unrecognized( index, argv[ index ] ); 236 226 echo_print_help(); 237 227 return EINVAL; … … 239 229 } 240 230 241 if( size <= 0){242 fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);231 if( size <= 0 ){ 232 fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size ); 243 233 size = 1024; 244 234 } 245 235 // size plus terminating null (\0) 246 data = ( char *) malloc(size + 1);247 if( ! data){248 fprintf( stderr, "Failed to allocate receive buffer.\n");236 data = ( char * ) malloc( size + 1 ); 237 if( ! data ){ 238 fprintf( stderr, "Failed to allocate receive buffer.\n" ); 249 239 return ENOMEM; 250 240 } 251 241 252 reply_length = reply ? str_length( reply) : 0;253 254 listening_id = socket( family, type, 0);255 if( listening_id < 0){256 socket_print_error( stderr, listening_id, "Socket create: ", "\n");242 reply_length = reply ? str_length( reply ) : 0; 243 244 listening_id = socket( family, type, 0 ); 245 if( listening_id < 0 ){ 246 socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); 257 247 return listening_id; 258 248 } 259 249 260 bzero( address_data, max_length);261 switch( family){250 bzero( address_data, max_length ); 251 switch( family ){ 262 252 case PF_INET: 263 253 address_in->sin_family = AF_INET; 264 address_in->sin_port = htons( port);265 addrlen = sizeof( struct sockaddr_in);254 address_in->sin_port = htons( port ); 255 addrlen = sizeof( struct sockaddr_in ); 266 256 break; 267 257 case PF_INET6: 268 258 address_in6->sin6_family = AF_INET6; 269 address_in6->sin6_port = htons( port);270 addrlen = sizeof( struct sockaddr_in6);259 address_in6->sin6_port = htons( port ); 260 addrlen = sizeof( struct sockaddr_in6 ); 271 261 break; 272 262 default: 273 fprintf( stderr, "Protocol family is not supported\n");263 fprintf( stderr, "Protocol family is not supported\n" ); 274 264 return EAFNOSUPPORT; 275 265 } 276 266 277 listening_id = socket( family, type, 0);278 if( listening_id < 0){279 socket_print_error( stderr, listening_id, "Socket create: ", "\n");267 listening_id = socket( family, type, 0 ); 268 if( listening_id < 0 ){ 269 socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); 280 270 return listening_id; 281 271 } 282 272 283 if( type == SOCK_STREAM){284 if( backlog <= 0){285 fprintf( stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);273 if( type == SOCK_STREAM ){ 274 if( backlog <= 0 ){ 275 fprintf( stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size ); 286 276 backlog = 3; 287 277 } 288 if( ERROR_OCCURRED(listen(listening_id, backlog))){289 socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n");278 if( ERROR_OCCURRED( listen( listening_id, backlog ))){ 279 socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" ); 290 280 return ERROR_CODE; 291 281 } 292 282 } 293 283 294 if( ERROR_OCCURRED(bind(listening_id, address, addrlen))){295 socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n");284 if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){ 285 socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); 296 286 return ERROR_CODE; 297 287 } 298 288 299 if(verbose){ 300 printf("Socket %d listenning at %d\n", listening_id, port); 301 } 289 if( verbose ) printf( "Socket %d listenning at %d\n", listening_id, port ); 302 290 303 291 socket_id = listening_id; 304 292 305 while( count){293 while( count ){ 306 294 addrlen = max_length; 307 if( type == SOCK_STREAM){308 socket_id = accept( listening_id, address, &addrlen);309 if( socket_id <= 0){310 socket_print_error( stderr, socket_id, "Socket accept: ", "\n");295 if( type == SOCK_STREAM ){ 296 socket_id = accept( listening_id, address, & addrlen ); 297 if( socket_id <= 0 ){ 298 socket_print_error( stderr, socket_id, "Socket accept: ", "\n" ); 311 299 }else{ 312 if(verbose){ 313 printf("Socket %d accepted\n", socket_id); 314 } 300 if( verbose ) printf( "Socket %d accepted\n", socket_id ); 315 301 } 316 302 } 317 if( socket_id > 0){318 value = recvfrom( socket_id, data, size, 0, address, &addrlen);319 if( value < 0){320 socket_print_error( stderr, value, "Socket receive: ", "\n");303 if( socket_id > 0 ){ 304 value = recvfrom( socket_id, data, size, 0, address, & addrlen ); 305 if( value < 0 ){ 306 socket_print_error( stderr, value, "Socket receive: ", "\n" ); 321 307 }else{ 322 length = ( size_t) value;323 if( verbose){308 length = ( size_t ) value; 309 if( verbose ){ 324 310 address_start = NULL; 325 switch( address->sa_family){311 switch( address->sa_family ){ 326 312 case AF_INET: 327 port = ntohs( address_in->sin_port);328 address_start = ( uint8_t *) &address_in->sin_addr.s_addr;313 port = ntohs( address_in->sin_port ); 314 address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; 329 315 break; 330 316 case AF_INET6: 331 port = ntohs( address_in6->sin6_port);332 address_start = ( uint8_t *) &address_in6->sin6_addr.s6_addr;317 port = ntohs( address_in6->sin6_port ); 318 address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; 333 319 break; 334 320 default: 335 fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);321 fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family ); 336 322 } 337 if( address_start){338 if( ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){339 fprintf( stderr, "Received address error %d\n", ERROR_CODE);323 if( address_start ){ 324 if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){ 325 fprintf( stderr, "Received address error %d\n", ERROR_CODE ); 340 326 }else{ 341 data[ length] = '\0';342 printf( "Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);327 data[ length ] = '\0'; 328 printf( "Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data ); 343 329 } 344 330 } 345 331 } 346 if( ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){347 socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n");332 if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){ 333 socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); 348 334 } 349 335 } 350 if( type == SOCK_STREAM){351 if( ERROR_OCCURRED(closesocket(socket_id))){352 socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n");336 if( type == SOCK_STREAM ){ 337 if( ERROR_OCCURRED( closesocket( socket_id ))){ 338 socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); 353 339 } 354 340 } 355 341 } 356 if( count > 0){342 if( count > 0 ){ 357 343 -- count; 358 if(verbose){ 359 printf("Waiting for next %d packet(s)\n", count); 360 } 361 } 362 } 363 364 if(verbose){ 365 printf("Closing the socket\n"); 366 } 367 368 if(ERROR_OCCURRED(closesocket(listening_id))){ 369 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 344 if( verbose ) printf( "Waiting for next %d packet(s)\n", count ); 345 } 346 } 347 348 if( verbose ) printf( "Closing the socket\n" ); 349 350 if( ERROR_OCCURRED( closesocket( listening_id ))){ 351 socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); 370 352 return ERROR_CODE; 371 353 } 372 354 373 if(verbose){ 374 printf("Exiting\n"); 375 } 355 if( verbose ) printf( "Exiting\n" ); 376 356 377 357 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.