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