Changeset 3be62bc in mainline for uspace/srv/net/app/echo/echo.c
- Timestamp:
- 2010-03-09T22:23:18Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9f2ea28
- Parents:
- a8a13d0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/echo/echo.c
ra8a13d0 r3be62bc 55 55 #define NAME "Echo" 56 56 57 /** Prints the application help. 58 */ 59 void echo_print_help(void); 60 57 61 /** Module entry point. 58 62 * Reads command line parameters and starts listenning. … … 62 66 */ 63 67 int main(int argc, char * argv[]); 64 65 /** Prints the application help.66 */67 void echo_print_help(void);68 69 /** Translates the character string to the protocol family number.70 * @param[in] name The protocol family name.71 * @returns The corresponding protocol family number.72 * @returns EPFNOSUPPORTED if the protocol family is not supported.73 */74 int echo_parse_protocol_family(const char * name);75 76 /** Translates the character string to the socket type number.77 * @param[in] name The socket type name.78 * @returns The corresponding socket type number.79 * @returns ESOCKNOSUPPORTED if the socket type is not supported.80 */81 int echo_parse_socket_type(const char * name);82 68 83 69 void echo_print_help(void){ … … 115 101 } 116 102 117 int echo_parse_protocol_family(const char * name){118 if(str_lcmp(name, "PF_INET", 7) == 0){119 return PF_INET;120 }else if(str_lcmp(name, "PF_INET6", 8) == 0){121 return PF_INET6;122 }123 return EPFNOSUPPORT;124 }125 126 int echo_parse_socket_type(const char * name){127 if(str_lcmp(name, "SOCK_DGRAM", 11) == 0){128 return SOCK_DGRAM;129 }else if(str_lcmp(name, "SOCK_STREAM", 12) == 0){130 return SOCK_STREAM;131 }132 return ESOCKTNOSUPPORT;133 }134 135 103 int main(int argc, char * argv[]){ 136 104 ERROR_DECLARE; … … 138 106 size_t size = 1024; 139 107 int verbose = 0; 140 char * reply 141 sock_type_t type 108 char * reply = NULL; 109 sock_type_t type = SOCK_DGRAM; 142 110 int count = -1; 143 111 int family = PF_INET; 144 uint16_t port 112 uint16_t port = 7; 145 113 int backlog = 3; 146 114 147 socklen_t max_length = sizeof(struct sockaddr_in6);115 socklen_t max_length = sizeof(struct sockaddr_in6); 148 116 uint8_t address_data[max_length]; 149 struct sockaddr * address = (struct sockaddr *) address_data;117 struct sockaddr * address = (struct sockaddr *) address_data; 150 118 struct sockaddr_in * address_in = (struct sockaddr_in *) address; 151 119 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address; … … 155 123 int socket_id; 156 124 int listening_id; 157 char * 125 char * data; 158 126 size_t length; 159 127 int index; … … 161 129 int value; 162 130 131 // print the program label 163 132 printf("Task %d - ", task_get_id()); 164 133 printf("%s\n", NAME); 165 134 135 // parse the command line arguments 166 136 for(index = 1; index < argc; ++ index){ 167 137 if(argv[index][0] == '-'){ … … 174 144 break; 175 145 case 'f': 176 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, echo_parse_protocol_family));146 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family)); 177 147 break; 178 148 case 'h': … … 192 162 break; 193 163 case 't': 194 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, echo_parse_socket_type));164 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type)); 195 165 type = (sock_type_t) value; 196 166 break; … … 198 168 verbose = 1; 199 169 break; 170 // long options with the double minus sign ('-') 200 171 case '-': 201 172 if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){ … … 204 175 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 8)); 205 176 }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));177 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family)); 207 178 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 208 179 echo_print_help(); … … 217 188 size = (value >= 0) ? (size_t) value : 0; 218 189 }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));190 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type)); 220 191 type = (sock_type_t) value; 221 192 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ … … 239 210 } 240 211 212 // check the buffer size 241 213 if(size <= 0){ 242 214 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size); 243 215 size = 1024; 244 216 } 245 // size plus t erminating null (\0)217 // size plus the terminating null (\0) 246 218 data = (char *) malloc(size + 1); 247 219 if(! data){ … … 250 222 } 251 223 224 // set the reply size if set 252 225 reply_length = reply ? str_length(reply) : 0; 253 226 254 listening_id = socket(family, type, 0); 255 if(listening_id < 0){ 256 socket_print_error(stderr, listening_id, "Socket create: ", "\n"); 257 return listening_id; 258 } 259 227 // prepare the address buffer 260 228 bzero(address_data, max_length); 261 229 switch(family){ … … 275 243 } 276 244 245 // get a listening socket 277 246 listening_id = socket(family, type, 0); 278 247 if(listening_id < 0){ … … 281 250 } 282 251 252 // if the stream socket is used 283 253 if(type == SOCK_STREAM){ 254 // check the backlog 284 255 if(backlog <= 0){ 285 256 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size); 286 257 backlog = 3; 287 258 } 259 // set the backlog 288 260 if(ERROR_OCCURRED(listen(listening_id, backlog))){ 289 261 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); … … 292 264 } 293 265 266 // bind the listenning socket 294 267 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){ 295 268 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); … … 303 276 socket_id = listening_id; 304 277 278 // do count times 279 // or indefinitely if set to a negative value 305 280 while(count){ 281 306 282 addrlen = max_length; 307 283 if(type == SOCK_STREAM){ 284 // acceept a socket if the stream socket is used 308 285 socket_id = accept(listening_id, address, &addrlen); 309 286 if(socket_id <= 0){ … … 315 292 } 316 293 } 294 295 // if the datagram socket is used or the stream socked was accepted 317 296 if(socket_id > 0){ 297 298 // receive an echo request 318 299 value = recvfrom(socket_id, data, size, 0, address, &addrlen); 319 300 if(value < 0){ … … 322 303 length = (size_t) value; 323 304 if(verbose){ 305 // print the header 306 307 // get the source port and prepare the address buffer 324 308 address_start = NULL; 325 309 switch(address->sa_family){ … … 335 319 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family); 336 320 } 321 // parse the source address 337 322 if(address_start){ 338 323 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){ … … 344 329 } 345 330 } 331 332 // answer the request either with the static reply or the original data 346 333 if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){ 347 334 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 348 335 } 349 } 336 337 } 338 339 // close the accepted stream socket 350 340 if(type == SOCK_STREAM){ 351 341 if(ERROR_OCCURRED(closesocket(socket_id))){ … … 353 343 } 354 344 } 355 } 345 346 } 347 348 // decrease the count if positive 356 349 if(count > 0){ 357 350 -- count; … … 366 359 } 367 360 361 // close the listenning socket 368 362 if(ERROR_OCCURRED(closesocket(listening_id))){ 369 363 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
Note:
See TracChangeset
for help on using the changeset viewer.