Changes in uspace/app/netecho/netecho.c [d9e2e0e:a8e5051] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netecho/netecho.c
rd9e2e0e ra8e5051 28 28 29 29 /** @addtogroup netecho 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 35 * 34 * Network echo application. 35 * Answers received packets. 36 36 */ 37 37 … … 51 51 #include "print_error.h" 52 52 53 /** Network echo module name. 54 */ 53 /** Network echo module name. */ 55 54 #define NAME "Network Echo" 56 55 57 /** Prints the application help. 58 */ 59 void echo_print_help(void); 60 61 /** Module entry point. 62 * Reads command line parameters and starts listenning. 63 * @param[in] argc The number of command line parameters. 64 * @param[in] argv The command line parameters. 65 * @returns EOK on success. 66 */ 67 int main(int argc, char * argv[]); 68 69 void echo_print_help(void){ 56 static void echo_print_help(void) 57 { 70 58 printf( 71 59 "Network Echo aplication\n" \ … … 101 89 } 102 90 103 int main(int argc, char * argv[]){ 91 int main(int argc, char *argv[]) 92 { 104 93 ERROR_DECLARE; 105 94 106 size_t size 107 int verbose 108 char * reply= NULL;109 sock_type_t type 110 int count 111 int family 112 uint16_t port 113 int backlog 114 115 socklen_t max_length 95 size_t size = 1024; 96 int verbose = 0; 97 char *reply = NULL; 98 sock_type_t type = SOCK_DGRAM; 99 int count = -1; 100 int family = PF_INET; 101 uint16_t port = 7; 102 int backlog = 3; 103 104 socklen_t max_length = sizeof(struct sockaddr_in6); 116 105 uint8_t address_data[max_length]; 117 struct sockaddr * address= (struct sockaddr *) address_data;118 struct sockaddr_in * address_in= (struct sockaddr_in *) address;119 struct sockaddr_in6 * address_in6= (struct sockaddr_in6 *) address;106 struct sockaddr *address = (struct sockaddr *) address_data; 107 struct sockaddr_in *address_in = (struct sockaddr_in *) address; 108 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address; 120 109 socklen_t addrlen; 121 110 char address_string[INET6_ADDRSTRLEN]; 122 uint8_t * 111 uint8_t *address_start; 123 112 int socket_id; 124 113 int listening_id; 125 char * 114 char *data; 126 115 size_t length; 127 116 int index; … … 130 119 131 120 // parse the command line arguments 132 for(index = 1; index < argc; ++ index){ 133 if(argv[index][0] == '-'){ 134 switch(argv[index][1]){ 135 case 'b': 136 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0)); 137 break; 138 case 'c': 139 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0)); 140 break; 141 case 'f': 142 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 143 break; 144 case 'h': 121 for (index = 1; index < argc; ++ index) { 122 if (argv[index][0] == '-') { 123 switch (argv[index][1]) { 124 case 'b': 125 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0)); 126 break; 127 case 'c': 128 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0)); 129 break; 130 case 'f': 131 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 132 break; 133 case 'h': 134 echo_print_help(); 135 return EOK; 136 break; 137 case 'p': 138 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 139 port = (uint16_t) value; 140 break; 141 case 'r': 142 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0)); 143 break; 144 case 's': 145 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 146 size = (value >= 0) ? (size_t) value : 0; 147 break; 148 case 't': 149 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 150 type = (sock_type_t) value; 151 break; 152 case 'v': 153 verbose = 1; 154 break; 155 // long options with the double minus sign ('-') 156 case '-': 157 if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) { 158 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8)); 159 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) { 160 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8)); 161 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 162 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 163 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 145 164 echo_print_help(); 146 165 return EOK; 147 break; 148 case 'p': 149 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 166 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 167 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 150 168 port = (uint16_t) value; 151 break; 152 case 'r': 153 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0)); 154 break; 155 case 's': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 169 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 170 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8)); 171 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 172 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 157 173 size = (value >= 0) ? (size_t) value : 0; 158 break; 159 case 't': 160 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 174 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 175 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 161 176 type = (sock_type_t) value; 162 break; 163 case 'v': 177 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 164 178 verbose = 1; 165 break; 166 // long options with the double minus sign ('-') 167 case '-': 168 if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){ 169 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8)); 170 }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){ 171 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8)); 172 }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 173 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 174 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 175 echo_print_help(); 176 return EOK; 177 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 178 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 179 port = (uint16_t) value; 180 }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){ 181 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8)); 182 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){ 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 184 size = (value >= 0) ? (size_t) value : 0; 185 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 186 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 187 type = (sock_type_t) value; 188 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 189 verbose = 1; 190 }else{ 191 echo_print_help(); 192 return EINVAL; 193 } 194 break; 195 default: 179 } else { 196 180 echo_print_help(); 197 181 return EINVAL; 182 } 183 break; 184 default: 185 echo_print_help(); 186 return EINVAL; 198 187 } 199 } else{188 } else { 200 189 echo_print_help(); 201 190 return EINVAL; … … 204 193 205 194 // check the buffer size 206 if (size <= 0){195 if (size <= 0) { 207 196 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size); 208 197 size = 1024; … … 210 199 // size plus the terminating null (\0) 211 200 data = (char *) malloc(size + 1); 212 if (! data){201 if (!data) { 213 202 fprintf(stderr, "Failed to allocate receive buffer.\n"); 214 203 return ENOMEM; … … 220 209 // prepare the address buffer 221 210 bzero(address_data, max_length); 222 switch (family){223 224 225 226 227 228 229 230 231 232 233 234 235 211 switch (family) { 212 case PF_INET: 213 address_in->sin_family = AF_INET; 214 address_in->sin_port = htons(port); 215 addrlen = sizeof(struct sockaddr_in); 216 break; 217 case PF_INET6: 218 address_in6->sin6_family = AF_INET6; 219 address_in6->sin6_port = htons(port); 220 addrlen = sizeof(struct sockaddr_in6); 221 break; 222 default: 223 fprintf(stderr, "Protocol family is not supported\n"); 224 return EAFNOSUPPORT; 236 225 } 237 226 238 227 // get a listening socket 239 228 listening_id = socket(family, type, 0); 240 if (listening_id < 0){229 if (listening_id < 0) { 241 230 socket_print_error(stderr, listening_id, "Socket create: ", "\n"); 242 231 return listening_id; … … 244 233 245 234 // if the stream socket is used 246 if (type == SOCK_STREAM){235 if (type == SOCK_STREAM) { 247 236 // check the backlog 248 if (backlog <= 0){237 if (backlog <= 0) { 249 238 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size); 250 239 backlog = 3; 251 240 } 252 241 // set the backlog 253 if (ERROR_OCCURRED(listen(listening_id, backlog))){242 if (ERROR_OCCURRED(listen(listening_id, backlog))) { 254 243 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); 255 244 return ERROR_CODE; … … 258 247 259 248 // bind the listenning socket 260 if (ERROR_OCCURRED(bind(listening_id, address, addrlen))){249 if (ERROR_OCCURRED(bind(listening_id, address, addrlen))) { 261 250 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); 262 251 return ERROR_CODE; 263 252 } 264 253 265 if (verbose){254 if (verbose) 266 255 printf("Socket %d listenning at %d\n", listening_id, port); 267 }268 256 269 257 socket_id = listening_id; … … 271 259 // do count times 272 260 // or indefinitely if set to a negative value 273 while (count){261 while (count) { 274 262 275 263 addrlen = max_length; 276 if (type == SOCK_STREAM){264 if (type == SOCK_STREAM) { 277 265 // acceept a socket if the stream socket is used 278 266 socket_id = accept(listening_id, address, &addrlen); 279 if (socket_id <= 0){267 if (socket_id <= 0) { 280 268 socket_print_error(stderr, socket_id, "Socket accept: ", "\n"); 281 } else{282 if (verbose){269 } else { 270 if (verbose) 283 271 printf("Socket %d accepted\n", socket_id); 284 }285 272 } 286 273 } 287 274 288 275 // if the datagram socket is used or the stream socked was accepted 289 if (socket_id > 0){276 if (socket_id > 0) { 290 277 291 278 // receive an echo request 292 279 value = recvfrom(socket_id, data, size, 0, address, &addrlen); 293 if (value < 0){280 if (value < 0) { 294 281 socket_print_error(stderr, value, "Socket receive: ", "\n"); 295 } else{282 } else { 296 283 length = (size_t) value; 297 if (verbose){284 if (verbose) { 298 285 // print the header 299 286 300 287 // get the source port and prepare the address buffer 301 288 address_start = NULL; 302 switch (address->sa_family){303 304 305 306 307 308 309 310 311 312 289 switch (address->sa_family) { 290 case AF_INET: 291 port = ntohs(address_in->sin_port); 292 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 293 break; 294 case AF_INET6: 295 port = ntohs(address_in6->sin6_port); 296 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 297 break; 298 default: 299 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family); 313 300 } 314 301 // parse the source address 315 if (address_start){316 if (ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){302 if (address_start) { 303 if (ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))) { 317 304 fprintf(stderr, "Received address error %d\n", ERROR_CODE); 318 } else{305 } else { 319 306 data[length] = '\0'; 320 307 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data); … … 324 311 325 312 // answer the request either with the static reply or the original data 326 if (ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){313 if (ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))) 327 314 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 328 }329 330 315 } 331 316 332 317 // close the accepted stream socket 333 if (type == SOCK_STREAM){334 if (ERROR_OCCURRED(closesocket(socket_id))){318 if (type == SOCK_STREAM) { 319 if (ERROR_OCCURRED(closesocket(socket_id))) 335 320 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 336 }337 321 } 338 322 … … 340 324 341 325 // decrease the count if positive 342 if (count > 0){343 -- count;344 if (verbose){326 if (count > 0) { 327 count--; 328 if (verbose) 345 329 printf("Waiting for next %d packet(s)\n", count); 346 } 347 } 348 } 349 350 if(verbose){ 330 } 331 } 332 333 if (verbose) 351 334 printf("Closing the socket\n"); 352 }353 335 354 336 // close the listenning socket 355 if (ERROR_OCCURRED(closesocket(listening_id))){337 if (ERROR_OCCURRED(closesocket(listening_id))) { 356 338 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 357 339 return ERROR_CODE; 358 340 } 359 341 360 if (verbose){342 if (verbose) 361 343 printf("Exiting\n"); 362 }363 344 364 345 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.