Changes in uspace/srv/net/app/echo/echo.c [1b053ca2:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/echo/echo.c
r1b053ca2 raadf01e 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': 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; 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; 219 229 default: 220 print_unrecognized( index, argv[ index ] + 1);230 print_unrecognized(index, argv[index] + 1); 221 231 echo_print_help(); 222 232 return EINVAL; 223 233 } 224 234 }else{ 225 print_unrecognized( index, argv[ index ]);235 print_unrecognized(index, argv[index]); 226 236 echo_print_help(); 227 237 return EINVAL; … … 229 239 } 230 240 231 if( size <= 0){232 fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);241 if(size <= 0){ 242 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size); 233 243 size = 1024; 234 244 } 235 245 // size plus terminating null (\0) 236 data = ( char * ) malloc( size + 1);237 if( ! data){238 fprintf( stderr, "Failed to allocate receive buffer.\n");246 data = (char *) malloc(size + 1); 247 if(! data){ 248 fprintf(stderr, "Failed to allocate receive buffer.\n"); 239 249 return ENOMEM; 240 250 } 241 251 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");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"); 247 257 return listening_id; 248 258 } 249 259 250 bzero( address_data, max_length);251 switch( family){260 bzero(address_data, max_length); 261 switch(family){ 252 262 case PF_INET: 253 263 address_in->sin_family = AF_INET; 254 address_in->sin_port = htons( port);255 addrlen = sizeof( struct sockaddr_in);264 address_in->sin_port = htons(port); 265 addrlen = sizeof(struct sockaddr_in); 256 266 break; 257 267 case PF_INET6: 258 268 address_in6->sin6_family = AF_INET6; 259 address_in6->sin6_port = htons( port);260 addrlen = sizeof( struct sockaddr_in6);269 address_in6->sin6_port = htons(port); 270 addrlen = sizeof(struct sockaddr_in6); 261 271 break; 262 272 default: 263 fprintf( stderr, "Protocol family is not supported\n");273 fprintf(stderr, "Protocol family is not supported\n"); 264 274 return EAFNOSUPPORT; 265 275 } 266 276 267 listening_id = socket( family, type, 0);268 if( listening_id < 0){269 socket_print_error( stderr, listening_id, "Socket create: ", "\n");277 listening_id = socket(family, type, 0); 278 if(listening_id < 0){ 279 socket_print_error(stderr, listening_id, "Socket create: ", "\n"); 270 280 return listening_id; 271 281 } 272 282 273 if( type == SOCK_STREAM){274 if( backlog <= 0){275 fprintf( stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);283 if(type == SOCK_STREAM){ 284 if(backlog <= 0){ 285 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size); 276 286 backlog = 3; 277 287 } 278 if( ERROR_OCCURRED( listen( listening_id, backlog))){279 socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n");288 if(ERROR_OCCURRED(listen(listening_id, backlog))){ 289 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); 280 290 return ERROR_CODE; 281 291 } 282 292 } 283 293 284 if( ERROR_OCCURRED( bind( listening_id, address, addrlen))){285 socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n");294 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){ 295 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); 286 296 return ERROR_CODE; 287 297 } 288 298 289 if( verbose ) printf( "Socket %d listenning at %d\n", listening_id, port ); 299 if(verbose){ 300 printf("Socket %d listenning at %d\n", listening_id, port); 301 } 290 302 291 303 socket_id = listening_id; 292 304 293 while( count){305 while(count){ 294 306 addrlen = max_length; 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");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"); 299 311 }else{ 300 if( verbose ) printf( "Socket %d accepted\n", socket_id ); 301 } 302 } 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" ); 312 if(verbose){ 313 printf("Socket %d accepted\n", socket_id); 314 } 315 } 316 } 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"); 307 321 }else{ 308 length = ( size_t) value;309 if( verbose){322 length = (size_t) value; 323 if(verbose){ 310 324 address_start = NULL; 311 switch( address->sa_family){325 switch(address->sa_family){ 312 326 case AF_INET: 313 port = ntohs( address_in->sin_port);314 address_start = ( uint8_t * ) &address_in->sin_addr.s_addr;327 port = ntohs(address_in->sin_port); 328 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 315 329 break; 316 330 case AF_INET6: 317 port = ntohs( address_in6->sin6_port);318 address_start = ( uint8_t * ) &address_in6->sin6_addr.s6_addr;331 port = ntohs(address_in6->sin6_port); 332 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 319 333 break; 320 334 default: 321 fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);335 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family); 322 336 } 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);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); 326 340 }else{ 327 data[ length] = '\0';328 printf( "Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);341 data[length] = '\0'; 342 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data); 329 343 } 330 344 } 331 345 } 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");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"); 334 348 } 335 349 } 336 if( type == SOCK_STREAM){337 if( ERROR_OCCURRED( closesocket( socket_id))){338 socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n");350 if(type == SOCK_STREAM){ 351 if(ERROR_OCCURRED(closesocket(socket_id))){ 352 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 339 353 } 340 354 } 341 355 } 342 if( count > 0){356 if(count > 0){ 343 357 -- count; 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" ); 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"); 352 370 return ERROR_CODE; 353 371 } 354 372 355 if( verbose ) printf( "Exiting\n" ); 373 if(verbose){ 374 printf("Exiting\n"); 375 } 356 376 357 377 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.