[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[849ed54] | 29 | /** @addtogroup netecho
|
---|
[21580dd] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[849ed54] | 34 | * Network echo application.
|
---|
[21580dd] | 35 | * Answers received packets.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <malloc.h>
|
---|
| 39 | #include <stdio.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[21580dd] | 41 | #include <task.h>
|
---|
[2721a75] | 42 | #include <arg_parse.h>
|
---|
[c5b59ce] | 43 | #include <err.h>
|
---|
[21580dd] | 44 |
|
---|
[e4554d4] | 45 | #include <net/in.h>
|
---|
| 46 | #include <net/in6.h>
|
---|
| 47 | #include <net/inet.h>
|
---|
[d9e2e0e] | 48 | #include <net/socket.h>
|
---|
| 49 | #include <net/socket_parse.h>
|
---|
[21580dd] | 50 |
|
---|
[849ed54] | 51 | #include "print_error.h"
|
---|
[21580dd] | 52 |
|
---|
[849ed54] | 53 | /** Network echo module name.
|
---|
[21580dd] | 54 | */
|
---|
[849ed54] | 55 | #define NAME "Network Echo"
|
---|
[21580dd] | 56 |
|
---|
[3be62bc] | 57 | /** Prints the application help.
|
---|
| 58 | */
|
---|
| 59 | void echo_print_help(void);
|
---|
| 60 |
|
---|
[21580dd] | 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 | */
|
---|
[aadf01e] | 67 | int main(int argc, char * argv[]);
|
---|
[21580dd] | 68 |
|
---|
[aadf01e] | 69 | void echo_print_help(void){
|
---|
[21580dd] | 70 | printf(
|
---|
| 71 | "Network Echo aplication\n" \
|
---|
| 72 | "Usage: echo [options]\n" \
|
---|
| 73 | "Where options are:\n" \
|
---|
| 74 | "-b backlog | --backlog=size\n" \
|
---|
| 75 | "\tThe size of the accepted sockets queue. Only for SOCK_STREAM. The default is 3.\n" \
|
---|
| 76 | "\n" \
|
---|
| 77 | "-c count | --count=count\n" \
|
---|
| 78 | "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
|
---|
| 79 | "\n" \
|
---|
| 80 | "-f protocol_family | --family=protocol_family\n" \
|
---|
| 81 | "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
|
---|
| 82 | "\n" \
|
---|
| 83 | "-h | --help\n" \
|
---|
| 84 | "\tShow this application help.\n"
|
---|
| 85 | "\n" \
|
---|
| 86 | "-p port_number | --port=port_number\n" \
|
---|
| 87 | "\tThe port number the application should listen at. The default is 7.\n" \
|
---|
| 88 | "\n" \
|
---|
| 89 | "-r reply_string | --reply=reply_string\n" \
|
---|
| 90 | "\tThe constant reply string. The default is the original data received.\n" \
|
---|
| 91 | "\n" \
|
---|
| 92 | "-s receive_size | --size=receive_size\n" \
|
---|
| 93 | "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
|
---|
| 94 | "\n" \
|
---|
| 95 | "-t socket_type | --type=socket_type\n" \
|
---|
| 96 | "\tThe listenning socket type. Only the SOCK_DGRAM and the SOCK_STREAM are supported.\n" \
|
---|
| 97 | "\n" \
|
---|
| 98 | "-v | --verbose\n" \
|
---|
| 99 | "\tShow all output messages.\n"
|
---|
| 100 | );
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[aadf01e] | 103 | int main(int argc, char * argv[]){
|
---|
[21580dd] | 104 | ERROR_DECLARE;
|
---|
| 105 |
|
---|
[aadf01e] | 106 | size_t size = 1024;
|
---|
| 107 | int verbose = 0;
|
---|
[3be62bc] | 108 | char * reply = NULL;
|
---|
| 109 | sock_type_t type = SOCK_DGRAM;
|
---|
[aadf01e] | 110 | int count = -1;
|
---|
| 111 | int family = PF_INET;
|
---|
[3be62bc] | 112 | uint16_t port = 7;
|
---|
[aadf01e] | 113 | int backlog = 3;
|
---|
| 114 |
|
---|
[3be62bc] | 115 | socklen_t max_length = sizeof(struct sockaddr_in6);
|
---|
[aadf01e] | 116 | uint8_t address_data[max_length];
|
---|
[3be62bc] | 117 | struct sockaddr * address = (struct sockaddr *) address_data;
|
---|
[aadf01e] | 118 | struct sockaddr_in * address_in = (struct sockaddr_in *) address;
|
---|
| 119 | struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address;
|
---|
| 120 | socklen_t addrlen;
|
---|
| 121 | char address_string[INET6_ADDRSTRLEN];
|
---|
| 122 | uint8_t * address_start;
|
---|
| 123 | int socket_id;
|
---|
| 124 | int listening_id;
|
---|
[3be62bc] | 125 | char * data;
|
---|
[aadf01e] | 126 | size_t length;
|
---|
| 127 | int index;
|
---|
| 128 | size_t reply_length;
|
---|
| 129 | int value;
|
---|
| 130 |
|
---|
[3be62bc] | 131 | // parse the command line arguments
|
---|
[aadf01e] | 132 | for(index = 1; index < argc; ++ index){
|
---|
| 133 | if(argv[index][0] == '-'){
|
---|
| 134 | switch(argv[index][1]){
|
---|
| 135 | case 'b':
|
---|
[2721a75] | 136 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0));
|
---|
[aadf01e] | 137 | break;
|
---|
| 138 | case 'c':
|
---|
[2721a75] | 139 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0));
|
---|
[aadf01e] | 140 | break;
|
---|
| 141 | case 'f':
|
---|
[2721a75] | 142 | ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family));
|
---|
[aadf01e] | 143 | break;
|
---|
| 144 | case 'h':
|
---|
| 145 | echo_print_help();
|
---|
| 146 | return EOK;
|
---|
| 147 | break;
|
---|
| 148 | case 'p':
|
---|
[2721a75] | 149 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
|
---|
[aadf01e] | 150 | port = (uint16_t) value;
|
---|
| 151 | break;
|
---|
| 152 | case 'r':
|
---|
[2721a75] | 153 | ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0));
|
---|
[aadf01e] | 154 | break;
|
---|
| 155 | case 's':
|
---|
[2721a75] | 156 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
|
---|
[aadf01e] | 157 | size = (value >= 0) ? (size_t) value : 0;
|
---|
| 158 | break;
|
---|
| 159 | case 't':
|
---|
[2721a75] | 160 | ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type));
|
---|
[aadf01e] | 161 | type = (sock_type_t) value;
|
---|
| 162 | break;
|
---|
| 163 | case 'v':
|
---|
| 164 | verbose = 1;
|
---|
| 165 | break;
|
---|
[3be62bc] | 166 | // long options with the double minus sign ('-')
|
---|
[aadf01e] | 167 | case '-':
|
---|
| 168 | if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){
|
---|
[2721a75] | 169 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8));
|
---|
[aadf01e] | 170 | }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
|
---|
[2721a75] | 171 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8));
|
---|
[aadf01e] | 172 | }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
|
---|
[2721a75] | 173 | ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family));
|
---|
[aadf01e] | 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){
|
---|
[2721a75] | 178 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
|
---|
[aadf01e] | 179 | port = (uint16_t) value;
|
---|
| 180 | }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){
|
---|
[2721a75] | 181 | ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8));
|
---|
[aadf01e] | 182 | }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
|
---|
[2721a75] | 183 | ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
|
---|
[aadf01e] | 184 | size = (value >= 0) ? (size_t) value : 0;
|
---|
| 185 | }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
|
---|
[2721a75] | 186 | ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type));
|
---|
[aadf01e] | 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;
|
---|
[21580dd] | 195 | default:
|
---|
| 196 | echo_print_help();
|
---|
| 197 | return EINVAL;
|
---|
| 198 | }
|
---|
| 199 | }else{
|
---|
| 200 | echo_print_help();
|
---|
| 201 | return EINVAL;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[3be62bc] | 205 | // check the buffer size
|
---|
[aadf01e] | 206 | if(size <= 0){
|
---|
| 207 | fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
|
---|
[21580dd] | 208 | size = 1024;
|
---|
| 209 | }
|
---|
[3be62bc] | 210 | // size plus the terminating null (\0)
|
---|
[aadf01e] | 211 | data = (char *) malloc(size + 1);
|
---|
| 212 | if(! data){
|
---|
| 213 | fprintf(stderr, "Failed to allocate receive buffer.\n");
|
---|
[21580dd] | 214 | return ENOMEM;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[3be62bc] | 217 | // set the reply size if set
|
---|
[aadf01e] | 218 | reply_length = reply ? str_length(reply) : 0;
|
---|
[21580dd] | 219 |
|
---|
[3be62bc] | 220 | // prepare the address buffer
|
---|
[aadf01e] | 221 | bzero(address_data, max_length);
|
---|
| 222 | switch(family){
|
---|
[21580dd] | 223 | case PF_INET:
|
---|
| 224 | address_in->sin_family = AF_INET;
|
---|
[aadf01e] | 225 | address_in->sin_port = htons(port);
|
---|
| 226 | addrlen = sizeof(struct sockaddr_in);
|
---|
[21580dd] | 227 | break;
|
---|
| 228 | case PF_INET6:
|
---|
| 229 | address_in6->sin6_family = AF_INET6;
|
---|
[aadf01e] | 230 | address_in6->sin6_port = htons(port);
|
---|
| 231 | addrlen = sizeof(struct sockaddr_in6);
|
---|
[21580dd] | 232 | break;
|
---|
| 233 | default:
|
---|
[aadf01e] | 234 | fprintf(stderr, "Protocol family is not supported\n");
|
---|
[21580dd] | 235 | return EAFNOSUPPORT;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[3be62bc] | 238 | // get a listening socket
|
---|
[aadf01e] | 239 | listening_id = socket(family, type, 0);
|
---|
| 240 | if(listening_id < 0){
|
---|
| 241 | socket_print_error(stderr, listening_id, "Socket create: ", "\n");
|
---|
[21580dd] | 242 | return listening_id;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[3be62bc] | 245 | // if the stream socket is used
|
---|
[aadf01e] | 246 | if(type == SOCK_STREAM){
|
---|
[3be62bc] | 247 | // check the backlog
|
---|
[aadf01e] | 248 | if(backlog <= 0){
|
---|
| 249 | fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
|
---|
[21580dd] | 250 | backlog = 3;
|
---|
| 251 | }
|
---|
[3be62bc] | 252 | // set the backlog
|
---|
[aadf01e] | 253 | if(ERROR_OCCURRED(listen(listening_id, backlog))){
|
---|
| 254 | socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
|
---|
[21580dd] | 255 | return ERROR_CODE;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[3be62bc] | 259 | // bind the listenning socket
|
---|
[aadf01e] | 260 | if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
|
---|
| 261 | socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
|
---|
[21580dd] | 262 | return ERROR_CODE;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[aadf01e] | 265 | if(verbose){
|
---|
| 266 | printf("Socket %d listenning at %d\n", listening_id, port);
|
---|
| 267 | }
|
---|
[2d68c72] | 268 |
|
---|
| 269 | socket_id = listening_id;
|
---|
[21580dd] | 270 |
|
---|
[3be62bc] | 271 | // do count times
|
---|
| 272 | // or indefinitely if set to a negative value
|
---|
[aadf01e] | 273 | while(count){
|
---|
[3be62bc] | 274 |
|
---|
[21580dd] | 275 | addrlen = max_length;
|
---|
[aadf01e] | 276 | if(type == SOCK_STREAM){
|
---|
[3be62bc] | 277 | // acceept a socket if the stream socket is used
|
---|
[aadf01e] | 278 | socket_id = accept(listening_id, address, &addrlen);
|
---|
| 279 | if(socket_id <= 0){
|
---|
| 280 | socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
|
---|
[1b053ca2] | 281 | }else{
|
---|
[aadf01e] | 282 | if(verbose){
|
---|
| 283 | printf("Socket %d accepted\n", socket_id);
|
---|
| 284 | }
|
---|
[21580dd] | 285 | }
|
---|
| 286 | }
|
---|
[3be62bc] | 287 |
|
---|
| 288 | // if the datagram socket is used or the stream socked was accepted
|
---|
[aadf01e] | 289 | if(socket_id > 0){
|
---|
[3be62bc] | 290 |
|
---|
| 291 | // receive an echo request
|
---|
[aadf01e] | 292 | value = recvfrom(socket_id, data, size, 0, address, &addrlen);
|
---|
| 293 | if(value < 0){
|
---|
| 294 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
---|
[21580dd] | 295 | }else{
|
---|
[aadf01e] | 296 | length = (size_t) value;
|
---|
| 297 | if(verbose){
|
---|
[3be62bc] | 298 | // print the header
|
---|
| 299 |
|
---|
| 300 | // get the source port and prepare the address buffer
|
---|
[21580dd] | 301 | address_start = NULL;
|
---|
[aadf01e] | 302 | switch(address->sa_family){
|
---|
[21580dd] | 303 | case AF_INET:
|
---|
[aadf01e] | 304 | port = ntohs(address_in->sin_port);
|
---|
| 305 | address_start = (uint8_t *) &address_in->sin_addr.s_addr;
|
---|
[21580dd] | 306 | break;
|
---|
| 307 | case AF_INET6:
|
---|
[aadf01e] | 308 | port = ntohs(address_in6->sin6_port);
|
---|
| 309 | address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
|
---|
[21580dd] | 310 | break;
|
---|
| 311 | default:
|
---|
[aadf01e] | 312 | fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
|
---|
[21580dd] | 313 | }
|
---|
[3be62bc] | 314 | // parse the source address
|
---|
[aadf01e] | 315 | if(address_start){
|
---|
| 316 | if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
|
---|
| 317 | fprintf(stderr, "Received address error %d\n", ERROR_CODE);
|
---|
[21580dd] | 318 | }else{
|
---|
[aadf01e] | 319 | data[length] = '\0';
|
---|
| 320 | printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
|
---|
[21580dd] | 321 | }
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
[3be62bc] | 324 |
|
---|
| 325 | // answer the request either with the static reply or the original data
|
---|
[aadf01e] | 326 | if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){
|
---|
| 327 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
---|
[21580dd] | 328 | }
|
---|
[3be62bc] | 329 |
|
---|
[21580dd] | 330 | }
|
---|
[3be62bc] | 331 |
|
---|
| 332 | // close the accepted stream socket
|
---|
[aadf01e] | 333 | if(type == SOCK_STREAM){
|
---|
| 334 | if(ERROR_OCCURRED(closesocket(socket_id))){
|
---|
| 335 | socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
|
---|
[21580dd] | 336 | }
|
---|
| 337 | }
|
---|
[3be62bc] | 338 |
|
---|
[21580dd] | 339 | }
|
---|
[3be62bc] | 340 |
|
---|
| 341 | // decrease the count if positive
|
---|
[aadf01e] | 342 | if(count > 0){
|
---|
[21580dd] | 343 | -- count;
|
---|
[aadf01e] | 344 | if(verbose){
|
---|
| 345 | printf("Waiting for next %d packet(s)\n", count);
|
---|
| 346 | }
|
---|
[21580dd] | 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[aadf01e] | 350 | if(verbose){
|
---|
| 351 | printf("Closing the socket\n");
|
---|
| 352 | }
|
---|
[21580dd] | 353 |
|
---|
[3be62bc] | 354 | // close the listenning socket
|
---|
[aadf01e] | 355 | if(ERROR_OCCURRED(closesocket(listening_id))){
|
---|
| 356 | socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
|
---|
[21580dd] | 357 | return ERROR_CODE;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[aadf01e] | 360 | if(verbose){
|
---|
| 361 | printf("Exiting\n");
|
---|
| 362 | }
|
---|
[21580dd] | 363 |
|
---|
| 364 | return EOK;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /** @}
|
---|
| 368 | */
|
---|