source: mainline/uspace/app/netecho/netecho.c@ 4dd8529

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4dd8529 was c51fa73, checked in by Martin Decky <martin@…>, 16 years ago

down print the banner

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