source: mainline/uspace/app/netecho/netecho.c@ bfe6366

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bfe6366 was bfe6366, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Factor out an echo iteration.

  • Property mode set to 100644
File size: 11.7 KB
Line 
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 netecho
30 * @{
31 */
32
33/** @file
34 * Network echo server.
35 *
36 * Sockets-based server that echoes incomming messages. If stream mode
37 * is selected, accepts incoming connections.
38 */
39
40#include <malloc.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str.h>
44#include <task.h>
45#include <arg_parse.h>
46
47#include <net/in.h>
48#include <net/in6.h>
49#include <net/inet.h>
50#include <net/socket.h>
51#include <net/socket_parse.h>
52
53#include "print_error.h"
54
55#define NAME "netecho"
56
57static int count = -1;
58static int family = PF_INET;
59static sock_type_t type = SOCK_DGRAM;
60static uint16_t port = 7;
61static int backlog = 3;
62static size_t size = 1024;
63static int verbose = 0;
64
65static char *reply = NULL;
66static size_t reply_length;
67
68static char *data;
69
70static void echo_print_help(void)
71{
72 printf(
73 "Network echo server\n"
74 "Usage: " NAME " [options]\n"
75 "Where options are:\n"
76 "-b backlog | --backlog=size\n"
77 "\tThe size of the accepted sockets queue. Only for SOCK_STREAM. "
78 "The default is 3.\n"
79 "\n"
80 "-c count | --count=count\n"
81 "\tThe number of received messages to handle. A negative number "
82 "means infinity. The default is infinity.\n"
83 "\n"
84 "-f protocol_family | --family=protocol_family\n"
85 "\tThe listenning socket protocol family. Only the PF_INET and "
86 "PF_INET6 are supported.\n"
87 "\n"
88 "-h | --help\n"
89 "\tShow this application help.\n"
90 "\n"
91 "-p port_number | --port=port_number\n"
92 "\tThe port number the application should listen at. The default "
93 "is 7.\n"
94 "\n"
95 "-r reply_string | --reply=reply_string\n"
96 "\tThe constant reply string. The default is the original data "
97 "received.\n"
98 "\n"
99 "-s receive_size | --size=receive_size\n"
100 "\tThe maximum receive data size the application should accept. "
101 "The default is 1024 bytes.\n"
102 "\n"
103 "-t socket_type | --type=socket_type\n"
104 "\tThe listenning socket type. Only the SOCK_DGRAM and the "
105 "SOCK_STREAM are supported.\n"
106 "\n"
107 "-v | --verbose\n"
108 "\tShow all output messages.\n"
109 );
110}
111
112static int netecho_parse_option(int argc, char *argv[], int *index)
113{
114 int value;
115 int rc;
116
117 switch (argv[*index][1]) {
118 case 'b':
119 rc = arg_parse_int(argc, argv, index, &backlog, 0);
120 if (rc != EOK)
121 return rc;
122 break;
123 case 'c':
124 rc = arg_parse_int(argc, argv, index, &count, 0);
125 if (rc != EOK)
126 return rc;
127 break;
128 case 'f':
129 rc = arg_parse_name_int(argc, argv, index, &family, 0,
130 socket_parse_protocol_family);
131 if (rc != EOK)
132 return rc;
133 break;
134 case 'h':
135 echo_print_help();
136 exit(0);
137 break;
138 case 'p':
139 rc = arg_parse_int(argc, argv, index, &value, 0);
140 if (rc != EOK)
141 return rc;
142 port = (uint16_t) value;
143 break;
144 case 'r':
145 rc = arg_parse_string(argc, argv, index, &reply, 0);
146 if (rc != EOK)
147 return rc;
148 break;
149 case 's':
150 rc = arg_parse_int(argc, argv, index, &value, 0);
151 if (rc != EOK)
152 return rc;
153 size = (value >= 0) ? (size_t) value : 0;
154 break;
155 case 't':
156 rc = arg_parse_name_int(argc, argv, index, &value, 0,
157 socket_parse_socket_type);
158 if (rc != EOK)
159 return rc;
160 type = (sock_type_t) value;
161 break;
162 case 'v':
163 verbose = 1;
164 break;
165 /* Long options with double dash */
166 case '-':
167 if (str_lcmp(argv[*index] + 2, "backlog=", 6) == 0) {
168 rc = arg_parse_int(argc, argv, index, &backlog, 8);
169 if (rc != EOK)
170 return rc;
171 } else if (str_lcmp(argv[*index] + 2, "count=", 6) == 0) {
172 rc = arg_parse_int(argc, argv, index, &count, 8);
173 if (rc != EOK)
174 return rc;
175 } else if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
176 rc = arg_parse_name_int(argc, argv, index, &family, 9,
177 socket_parse_protocol_family);
178 if (rc != EOK)
179 return rc;
180 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
181 echo_print_help();
182 return EOK;
183 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
184 rc = arg_parse_int(argc, argv, index, &value, 7);
185 if (rc != EOK)
186 return rc;
187 port = (uint16_t) value;
188 } else if (str_lcmp(argv[*index] + 2, "reply=", 6) == 0) {
189 rc = arg_parse_string(argc, argv, index, &reply, 8);
190 if (rc != EOK)
191 return rc;
192 } else if (str_lcmp(argv[*index] + 2, "size=", 5) == 0) {
193 rc = arg_parse_int(argc, argv, index, &value, 7);
194 if (rc != EOK)
195 return rc;
196 size = (value >= 0) ? (size_t) value : 0;
197 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
198 rc = arg_parse_name_int(argc, argv, index, &value, 7,
199 socket_parse_socket_type);
200 if (rc != EOK)
201 return rc;
202 type = (sock_type_t) value;
203 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
204 verbose = 1;
205 } else {
206 echo_print_help();
207 return EINVAL;
208 }
209 break;
210 default:
211 echo_print_help();
212 return EINVAL;
213 }
214
215 return EOK;
216}
217
218/** Echo one message (accept one connection and echo message).
219 *
220 * @param listening_id Listening socket.
221 * @return EOK on success or negative error code.
222 */
223static int netecho_socket_echo_message(int listening_id)
224{
225 socklen_t max_length = sizeof(struct sockaddr_in6);
226 uint8_t address_data[max_length];
227 struct sockaddr *address = (struct sockaddr *) address_data;
228 socklen_t addrlen;
229 int socket_id;
230 ssize_t rcv_size;
231 size_t length;
232 uint8_t *address_start;
233 struct sockaddr_in *address_in = (struct sockaddr_in *) address;
234 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
235 char address_string[INET6_ADDRSTRLEN];
236 int rc;
237
238 addrlen = (socklen_t) max_length;
239 if (type == SOCK_STREAM) {
240 /* Accept a socket if the stream socket is used */
241 socket_id = accept(listening_id, address, &addrlen);
242 if (socket_id <= 0) {
243 socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
244 } else {
245 if (verbose)
246 printf("Socket %d accepted\n", socket_id);
247 }
248 } else {
249 socket_id = listening_id;
250 }
251
252 /* if the datagram socket is used or the stream socked was accepted */
253 if (socket_id > 0) {
254
255 /* Receive a message to echo */
256 rcv_size = recvfrom(socket_id, data, size, 0, address, &addrlen);
257 if (rcv_size < 0) {
258 socket_print_error(stderr, rcv_size, "Socket receive: ", "\n");
259 } else {
260 length = (size_t) rcv_size;
261 if (verbose) {
262 /* Print the header */
263
264 /* Get the source port and prepare the address buffer */
265 address_start = NULL;
266 switch (address->sa_family) {
267 case AF_INET:
268 port = ntohs(address_in->sin_port);
269 address_start = (uint8_t *) &address_in->sin_addr.s_addr;
270 break;
271 case AF_INET6:
272 port = ntohs(address_in6->sin6_port);
273 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
274 break;
275 default:
276 fprintf(stderr, "Address family %u (%#x) is not supported.\n",
277 address->sa_family, address->sa_family);
278 }
279
280 /* Parse source address */
281 if (address_start) {
282 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string));
283 if (rc != EOK) {
284 fprintf(stderr, "Received address error %d\n", rc);
285 } else {
286 data[length] = '\0';
287 printf("Socket %d received %zu bytes from %s:%d\n%s\n",
288 socket_id, length, address_string, port, data);
289 }
290 }
291 }
292
293 /* Answer the request either with the static reply or the original data */
294 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen);
295 if (rc != EOK)
296 socket_print_error(stderr, rc, "Socket send: ", "\n");
297 }
298
299 /* Close accepted stream socket */
300 if (type == SOCK_STREAM) {
301 rc = closesocket(socket_id);
302 if (rc != EOK)
303 socket_print_error(stderr, rc, "Close socket: ", "\n");
304 }
305
306 }
307
308 return EOK;
309}
310
311
312int main(int argc, char *argv[])
313{
314 socklen_t max_length = sizeof(struct sockaddr_in6);
315 uint8_t address_data[max_length];
316 struct sockaddr *address = (struct sockaddr *) address_data;
317 struct sockaddr_in *address_in = (struct sockaddr_in *) address;
318 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
319 socklen_t addrlen;
320 int listening_id;
321 int index;
322 int rc;
323
324 /* Parse command line arguments */
325 for (index = 1; index < argc; ++index) {
326 if (argv[index][0] == '-') {
327 rc = netecho_parse_option(argc, argv, &index);
328 if (rc != EOK)
329 return rc;
330 } else {
331 echo_print_help();
332 return EINVAL;
333 }
334 }
335
336 /* Check buffer size */
337 if (size <= 0) {
338 fprintf(stderr, "Receive size too small (%zu). Using 1024 bytes instead.\n", size);
339 size = 1024;
340 }
341
342 /* size plus the terminating null character. */
343 data = (char *) malloc(size + 1);
344 if (!data) {
345 fprintf(stderr, "Failed to allocate receive buffer.\n");
346 return ENOMEM;
347 }
348
349 /* Set the reply size if set */
350 reply_length = reply ? str_length(reply) : 0;
351
352 /* Prepare the address buffer */
353 bzero(address_data, max_length);
354 switch (family) {
355 case PF_INET:
356 address_in->sin_family = AF_INET;
357 address_in->sin_port = htons(port);
358 addrlen = sizeof(struct sockaddr_in);
359 break;
360 case PF_INET6:
361 address_in6->sin6_family = AF_INET6;
362 address_in6->sin6_port = htons(port);
363 addrlen = sizeof(struct sockaddr_in6);
364 break;
365 default:
366 fprintf(stderr, "Protocol family is not supported\n");
367 return EAFNOSUPPORT;
368 }
369
370 /* Get a listening socket */
371 listening_id = socket(family, type, 0);
372 if (listening_id < 0) {
373 socket_print_error(stderr, listening_id, "Socket create: ", "\n");
374 return listening_id;
375 }
376
377 /* if the stream socket is used */
378 if (type == SOCK_STREAM) {
379 /* Check backlog size */
380 if (backlog <= 0) {
381 fprintf(stderr, "Accepted sockets queue size too small (%zu). Using 3 instead.\n", size);
382 backlog = 3;
383 }
384
385 /* Set the backlog */
386 rc = listen(listening_id, backlog);
387 if (rc != EOK) {
388 socket_print_error(stderr, rc, "Socket listen: ", "\n");
389 return rc;
390 }
391 }
392
393 /* Bind the listening socket */
394 rc = bind(listening_id, address, addrlen);
395 if (rc != EOK) {
396 socket_print_error(stderr, rc, "Socket bind: ", "\n");
397 return rc;
398 }
399
400 if (verbose)
401 printf("Socket %d listenning at %d\n", listening_id, port);
402
403 /*
404 * do count times
405 * or indefinitely if set to a negative value
406 */
407 while (count) {
408 rc = netecho_socket_echo_message(listening_id);
409 if (rc != EOK)
410 break;
411
412 /* Decrease count if positive */
413 if (count > 0) {
414 count--;
415 if (verbose)
416 printf("Waiting for next %d packet(s)\n", count);
417 }
418 }
419
420 if (verbose)
421 printf("Closing the socket\n");
422
423 /* Close listenning socket */
424 rc = closesocket(listening_id);
425 if (rc != EOK) {
426 socket_print_error(stderr, rc, "Close socket: ", "\n");
427 return rc;
428 }
429
430 if (verbose)
431 printf("Exiting\n");
432
433 return EOK;
434}
435
436/** @}
437 */
Note: See TracBrowser for help on using the repository browser.