source: mainline/uspace/app/netecho/netecho.c@ 5d0f1bc

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

Remove use of ERROR_ macros from netecho.

  • Property mode set to 100644
File size: 10.8 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 application.
35 * Answers received packets.
36 */
37
38#include <malloc.h>
39#include <stdio.h>
40#include <str.h>
41#include <task.h>
42#include <arg_parse.h>
43
44#include <net/in.h>
45#include <net/in6.h>
46#include <net/inet.h>
47#include <net/socket.h>
48#include <net/socket_parse.h>
49
50#include "print_error.h"
51
52/** Network echo module name. */
53#define NAME "Network Echo"
54
55static void echo_print_help(void)
56{
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
90int 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;
100
101 socklen_t max_length = sizeof(struct sockaddr_in6);
102 uint8_t address_data[max_length];
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;
106 socklen_t addrlen;
107 char address_string[INET6_ADDRSTRLEN];
108 uint8_t *address_start;
109 int socket_id;
110 int listening_id;
111 char *data;
112 size_t length;
113 int index;
114 size_t reply_length;
115 int value;
116 int rc;
117
118 // parse the command line arguments
119 for (index = 1; index < argc; ++ index) {
120 if (argv[index][0] == '-') {
121 switch (argv[index][1]) {
122 case 'b':
123 rc = arg_parse_int(argc, argv, &index, &backlog, 0);
124 if (rc != EOK)
125 return rc;
126 break;
127 case 'c':
128 rc = arg_parse_int(argc, argv, &index, &count, 0);
129 if (rc != EOK)
130 return rc;
131 break;
132 case 'f':
133 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family);
134 if (rc != EOK)
135 return rc;
136 break;
137 case 'h':
138 echo_print_help();
139 return EOK;
140 break;
141 case 'p':
142 rc = arg_parse_int(argc, argv, &index, &value, 0);
143 if (rc != EOK)
144 return rc;
145 port = (uint16_t) value;
146 break;
147 case 'r':
148 rc = arg_parse_string(argc, argv, &index, &reply, 0);
149 if (rc != EOK)
150 return rc;
151 break;
152 case 's':
153 rc = arg_parse_int(argc, argv, &index, &value, 0);
154 if (rc != EOK)
155 return rc;
156 size = (value >= 0) ? (size_t) value : 0;
157 break;
158 case 't':
159 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type);
160 if (rc != EOK)
161 return rc;
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) {
170 rc = arg_parse_int(argc, argv, &index, &backlog, 8);
171 if (rc != EOK)
172 return rc;
173 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) {
174 rc = arg_parse_int(argc, argv, &index, &count, 8);
175 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) {
176 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family);
177 if (rc != EOK)
178 return rc;
179 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) {
180 echo_print_help();
181 return EOK;
182 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) {
183 rc = arg_parse_int(argc, argv, &index, &value, 7);
184 if (rc != EOK)
185 return rc;
186 port = (uint16_t) value;
187 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) {
188 rc = arg_parse_string(argc, argv, &index, &reply, 8);
189 if (rc != EOK)
190 return rc;
191 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) {
192 rc = arg_parse_int(argc, argv, &index, &value, 7);
193 if (rc != EOK)
194 return rc;
195 size = (value >= 0) ? (size_t) value : 0;
196 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) {
197 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type);
198 if (rc != EOK)
199 return rc;
200 type = (sock_type_t) value;
201 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) {
202 verbose = 1;
203 } else {
204 echo_print_help();
205 return EINVAL;
206 }
207 break;
208 default:
209 echo_print_help();
210 return EINVAL;
211 }
212 } else {
213 echo_print_help();
214 return EINVAL;
215 }
216 }
217
218 // check the buffer size
219 if (size <= 0) {
220 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
221 size = 1024;
222 }
223 // size plus the terminating null (\0)
224 data = (char *) malloc(size + 1);
225 if (!data) {
226 fprintf(stderr, "Failed to allocate receive buffer.\n");
227 return ENOMEM;
228 }
229
230 // set the reply size if set
231 reply_length = reply ? str_length(reply) : 0;
232
233 // prepare the address buffer
234 bzero(address_data, max_length);
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;
249 }
250
251 // get a listening socket
252 listening_id = socket(family, type, 0);
253 if (listening_id < 0) {
254 socket_print_error(stderr, listening_id, "Socket create: ", "\n");
255 return listening_id;
256 }
257
258 // if the stream socket is used
259 if (type == SOCK_STREAM) {
260 // check the backlog
261 if (backlog <= 0) {
262 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
263 backlog = 3;
264 }
265 // set the backlog
266 rc = listen(listening_id, backlog);
267 if (rc != EOK) {
268 socket_print_error(stderr, rc, "Socket listen: ", "\n");
269 return rc;
270 }
271 }
272
273 // bind the listenning socket
274 rc = bind(listening_id, address, addrlen);
275 if (rc != EOK) {
276 socket_print_error(stderr, rc, "Socket bind: ", "\n");
277 return rc;
278 }
279
280 if (verbose)
281 printf("Socket %d listenning at %d\n", listening_id, port);
282
283 socket_id = listening_id;
284
285 // do count times
286 // or indefinitely if set to a negative value
287 while (count) {
288
289 addrlen = max_length;
290 if (type == SOCK_STREAM) {
291 // acceept a socket if the stream socket is used
292 socket_id = accept(listening_id, address, &addrlen);
293 if (socket_id <= 0) {
294 socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
295 } else {
296 if (verbose)
297 printf("Socket %d accepted\n", socket_id);
298 }
299 }
300
301 // if the datagram socket is used or the stream socked was accepted
302 if (socket_id > 0) {
303
304 // receive an echo request
305 value = recvfrom(socket_id, data, size, 0, address, &addrlen);
306 if (value < 0) {
307 socket_print_error(stderr, value, "Socket receive: ", "\n");
308 } else {
309 length = (size_t) value;
310 if (verbose) {
311 // print the header
312
313 // get the source port and prepare the address buffer
314 address_start = NULL;
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);
326 }
327 // parse the source address
328 if (address_start) {
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);
332 } else {
333 data[length] = '\0';
334 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
335 }
336 }
337 }
338
339 // answer the request either with the static reply or the original data
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");
343 }
344
345 // close the accepted stream socket
346 if (type == SOCK_STREAM) {
347 rc = closesocket(socket_id);
348 if (rc != EOK)
349 socket_print_error(stderr, rc, "Close socket: ", "\n");
350 }
351
352 }
353
354 // decrease the count if positive
355 if (count > 0) {
356 count--;
357 if (verbose)
358 printf("Waiting for next %d packet(s)\n", count);
359 }
360 }
361
362 if (verbose)
363 printf("Closing the socket\n");
364
365 // close the listenning socket
366 rc = closesocket(listening_id);
367 if (rc != EOK) {
368 socket_print_error(stderr, rc, "Close socket: ", "\n");
369 return rc;
370 }
371
372 if (verbose)
373 printf("Exiting\n");
374
375 return EOK;
376}
377
378/** @}
379 */
Note: See TracBrowser for help on using the repository browser.