[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 |
|
---|
| 29 | /** @addtogroup echo
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Echo application.
|
---|
| 35 | * Answers received packets.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <malloc.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <string.h>
|
---|
| 41 | #include <task.h>
|
---|
| 42 |
|
---|
| 43 | #include "../../include/in.h"
|
---|
| 44 | #include "../../include/in6.h"
|
---|
| 45 | #include "../../include/inet.h"
|
---|
| 46 | #include "../../include/socket.h"
|
---|
| 47 |
|
---|
| 48 | #include "../../err.h"
|
---|
| 49 |
|
---|
| 50 | #include "../parse.h"
|
---|
| 51 | #include "../print_error.h"
|
---|
| 52 |
|
---|
| 53 | /** Echo module name.
|
---|
| 54 | */
|
---|
| 55 | #define NAME "Echo"
|
---|
| 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 | // print the program label
|
---|
[aadf01e] | 132 | printf("Task %d - ", task_get_id());
|
---|
| 133 | printf("%s\n", NAME);
|
---|
| 134 |
|
---|
[3be62bc] | 135 | // parse the command line arguments
|
---|
[aadf01e] | 136 | for(index = 1; index < argc; ++ index){
|
---|
| 137 | if(argv[index][0] == '-'){
|
---|
| 138 | switch(argv[index][1]){
|
---|
| 139 | case 'b':
|
---|
| 140 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 0));
|
---|
| 141 | break;
|
---|
| 142 | case 'c':
|
---|
| 143 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 0));
|
---|
| 144 | break;
|
---|
| 145 | case 'f':
|
---|
[3be62bc] | 146 | ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family));
|
---|
[aadf01e] | 147 | break;
|
---|
| 148 | case 'h':
|
---|
| 149 | echo_print_help();
|
---|
| 150 | return EOK;
|
---|
| 151 | break;
|
---|
| 152 | case 'p':
|
---|
| 153 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 0));
|
---|
| 154 | port = (uint16_t) value;
|
---|
| 155 | break;
|
---|
| 156 | case 'r':
|
---|
| 157 | ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 0));
|
---|
| 158 | break;
|
---|
| 159 | case 's':
|
---|
| 160 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 0));
|
---|
| 161 | size = (value >= 0) ? (size_t) value : 0;
|
---|
| 162 | break;
|
---|
| 163 | case 't':
|
---|
[3be62bc] | 164 | ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type));
|
---|
[aadf01e] | 165 | type = (sock_type_t) value;
|
---|
| 166 | break;
|
---|
| 167 | case 'v':
|
---|
| 168 | verbose = 1;
|
---|
| 169 | break;
|
---|
[3be62bc] | 170 | // long options with the double minus sign ('-')
|
---|
[aadf01e] | 171 | case '-':
|
---|
| 172 | if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){
|
---|
| 173 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 8));
|
---|
| 174 | }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
|
---|
| 175 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 8));
|
---|
| 176 | }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
|
---|
[3be62bc] | 177 | ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family));
|
---|
[aadf01e] | 178 | }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
|
---|
| 179 | echo_print_help();
|
---|
| 180 | return EOK;
|
---|
| 181 | }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){
|
---|
| 182 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 7));
|
---|
| 183 | port = (uint16_t) value;
|
---|
| 184 | }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){
|
---|
| 185 | ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 8));
|
---|
| 186 | }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
|
---|
| 187 | ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 7));
|
---|
| 188 | size = (value >= 0) ? (size_t) value : 0;
|
---|
| 189 | }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
|
---|
[3be62bc] | 190 | ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type));
|
---|
[aadf01e] | 191 | type = (sock_type_t) value;
|
---|
| 192 | }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){
|
---|
| 193 | verbose = 1;
|
---|
| 194 | }else{
|
---|
| 195 | print_unrecognized(index, argv[index] + 2);
|
---|
| 196 | echo_print_help();
|
---|
| 197 | return EINVAL;
|
---|
| 198 | }
|
---|
| 199 | break;
|
---|
[21580dd] | 200 | default:
|
---|
[aadf01e] | 201 | print_unrecognized(index, argv[index] + 1);
|
---|
[21580dd] | 202 | echo_print_help();
|
---|
| 203 | return EINVAL;
|
---|
| 204 | }
|
---|
| 205 | }else{
|
---|
[aadf01e] | 206 | print_unrecognized(index, argv[index]);
|
---|
[21580dd] | 207 | echo_print_help();
|
---|
| 208 | return EINVAL;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[3be62bc] | 212 | // check the buffer size
|
---|
[aadf01e] | 213 | if(size <= 0){
|
---|
| 214 | fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
|
---|
[21580dd] | 215 | size = 1024;
|
---|
| 216 | }
|
---|
[3be62bc] | 217 | // size plus the terminating null (\0)
|
---|
[aadf01e] | 218 | data = (char *) malloc(size + 1);
|
---|
| 219 | if(! data){
|
---|
| 220 | fprintf(stderr, "Failed to allocate receive buffer.\n");
|
---|
[21580dd] | 221 | return ENOMEM;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[3be62bc] | 224 | // set the reply size if set
|
---|
[aadf01e] | 225 | reply_length = reply ? str_length(reply) : 0;
|
---|
[21580dd] | 226 |
|
---|
[3be62bc] | 227 | // prepare the address buffer
|
---|
[aadf01e] | 228 | bzero(address_data, max_length);
|
---|
| 229 | switch(family){
|
---|
[21580dd] | 230 | case PF_INET:
|
---|
| 231 | address_in->sin_family = AF_INET;
|
---|
[aadf01e] | 232 | address_in->sin_port = htons(port);
|
---|
| 233 | addrlen = sizeof(struct sockaddr_in);
|
---|
[21580dd] | 234 | break;
|
---|
| 235 | case PF_INET6:
|
---|
| 236 | address_in6->sin6_family = AF_INET6;
|
---|
[aadf01e] | 237 | address_in6->sin6_port = htons(port);
|
---|
| 238 | addrlen = sizeof(struct sockaddr_in6);
|
---|
[21580dd] | 239 | break;
|
---|
| 240 | default:
|
---|
[aadf01e] | 241 | fprintf(stderr, "Protocol family is not supported\n");
|
---|
[21580dd] | 242 | return EAFNOSUPPORT;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[3be62bc] | 245 | // get a listening socket
|
---|
[aadf01e] | 246 | listening_id = socket(family, type, 0);
|
---|
| 247 | if(listening_id < 0){
|
---|
| 248 | socket_print_error(stderr, listening_id, "Socket create: ", "\n");
|
---|
[21580dd] | 249 | return listening_id;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[3be62bc] | 252 | // if the stream socket is used
|
---|
[aadf01e] | 253 | if(type == SOCK_STREAM){
|
---|
[3be62bc] | 254 | // check the backlog
|
---|
[aadf01e] | 255 | if(backlog <= 0){
|
---|
| 256 | fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
|
---|
[21580dd] | 257 | backlog = 3;
|
---|
| 258 | }
|
---|
[3be62bc] | 259 | // set the backlog
|
---|
[aadf01e] | 260 | if(ERROR_OCCURRED(listen(listening_id, backlog))){
|
---|
| 261 | socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
|
---|
[21580dd] | 262 | return ERROR_CODE;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[3be62bc] | 266 | // bind the listenning socket
|
---|
[aadf01e] | 267 | if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
|
---|
| 268 | socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
|
---|
[21580dd] | 269 | return ERROR_CODE;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[aadf01e] | 272 | if(verbose){
|
---|
| 273 | printf("Socket %d listenning at %d\n", listening_id, port);
|
---|
| 274 | }
|
---|
[2d68c72] | 275 |
|
---|
| 276 | socket_id = listening_id;
|
---|
[21580dd] | 277 |
|
---|
[3be62bc] | 278 | // do count times
|
---|
| 279 | // or indefinitely if set to a negative value
|
---|
[aadf01e] | 280 | while(count){
|
---|
[3be62bc] | 281 |
|
---|
[21580dd] | 282 | addrlen = max_length;
|
---|
[aadf01e] | 283 | if(type == SOCK_STREAM){
|
---|
[3be62bc] | 284 | // acceept a socket if the stream socket is used
|
---|
[aadf01e] | 285 | socket_id = accept(listening_id, address, &addrlen);
|
---|
| 286 | if(socket_id <= 0){
|
---|
| 287 | socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
|
---|
[1b053ca2] | 288 | }else{
|
---|
[aadf01e] | 289 | if(verbose){
|
---|
| 290 | printf("Socket %d accepted\n", socket_id);
|
---|
| 291 | }
|
---|
[21580dd] | 292 | }
|
---|
| 293 | }
|
---|
[3be62bc] | 294 |
|
---|
| 295 | // if the datagram socket is used or the stream socked was accepted
|
---|
[aadf01e] | 296 | if(socket_id > 0){
|
---|
[3be62bc] | 297 |
|
---|
| 298 | // receive an echo request
|
---|
[aadf01e] | 299 | value = recvfrom(socket_id, data, size, 0, address, &addrlen);
|
---|
| 300 | if(value < 0){
|
---|
| 301 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
---|
[21580dd] | 302 | }else{
|
---|
[aadf01e] | 303 | length = (size_t) value;
|
---|
| 304 | if(verbose){
|
---|
[3be62bc] | 305 | // print the header
|
---|
| 306 |
|
---|
| 307 | // get the source port and prepare the address buffer
|
---|
[21580dd] | 308 | address_start = NULL;
|
---|
[aadf01e] | 309 | switch(address->sa_family){
|
---|
[21580dd] | 310 | case AF_INET:
|
---|
[aadf01e] | 311 | port = ntohs(address_in->sin_port);
|
---|
| 312 | address_start = (uint8_t *) &address_in->sin_addr.s_addr;
|
---|
[21580dd] | 313 | break;
|
---|
| 314 | case AF_INET6:
|
---|
[aadf01e] | 315 | port = ntohs(address_in6->sin6_port);
|
---|
| 316 | address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
|
---|
[21580dd] | 317 | break;
|
---|
| 318 | default:
|
---|
[aadf01e] | 319 | fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
|
---|
[21580dd] | 320 | }
|
---|
[3be62bc] | 321 | // parse the source address
|
---|
[aadf01e] | 322 | if(address_start){
|
---|
| 323 | if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
|
---|
| 324 | fprintf(stderr, "Received address error %d\n", ERROR_CODE);
|
---|
[21580dd] | 325 | }else{
|
---|
[aadf01e] | 326 | data[length] = '\0';
|
---|
| 327 | printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
|
---|
[21580dd] | 328 | }
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
[3be62bc] | 331 |
|
---|
| 332 | // answer the request either with the static reply or the original data
|
---|
[aadf01e] | 333 | if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){
|
---|
| 334 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
---|
[21580dd] | 335 | }
|
---|
[3be62bc] | 336 |
|
---|
[21580dd] | 337 | }
|
---|
[3be62bc] | 338 |
|
---|
| 339 | // close the accepted stream socket
|
---|
[aadf01e] | 340 | if(type == SOCK_STREAM){
|
---|
| 341 | if(ERROR_OCCURRED(closesocket(socket_id))){
|
---|
| 342 | socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
|
---|
[21580dd] | 343 | }
|
---|
| 344 | }
|
---|
[3be62bc] | 345 |
|
---|
[21580dd] | 346 | }
|
---|
[3be62bc] | 347 |
|
---|
| 348 | // decrease the count if positive
|
---|
[aadf01e] | 349 | if(count > 0){
|
---|
[21580dd] | 350 | -- count;
|
---|
[aadf01e] | 351 | if(verbose){
|
---|
| 352 | printf("Waiting for next %d packet(s)\n", count);
|
---|
| 353 | }
|
---|
[21580dd] | 354 | }
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[aadf01e] | 357 | if(verbose){
|
---|
| 358 | printf("Closing the socket\n");
|
---|
| 359 | }
|
---|
[21580dd] | 360 |
|
---|
[3be62bc] | 361 | // close the listenning socket
|
---|
[aadf01e] | 362 | if(ERROR_OCCURRED(closesocket(listening_id))){
|
---|
| 363 | socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
|
---|
[21580dd] | 364 | return ERROR_CODE;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[aadf01e] | 367 | if(verbose){
|
---|
| 368 | printf("Exiting\n");
|
---|
| 369 | }
|
---|
[21580dd] | 370 |
|
---|
| 371 | return EOK;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /** @}
|
---|
| 375 | */
|
---|