source: mainline/uspace/srv/net/app/echo/echo.c@ 3be62bc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3be62bc was 3be62bc, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago
  • net app command line argument checks, * code reorganization
  • Property mode set to 100644
File size: 11.4 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 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
57/** Prints the application help.
58 */
59void echo_print_help(void);
60
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 */
67int main(int argc, char * argv[]);
68
69void echo_print_help(void){
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
103int main(int argc, char * argv[]){
104 ERROR_DECLARE;
105
106 size_t size = 1024;
107 int verbose = 0;
108 char * reply = NULL;
109 sock_type_t type = SOCK_DGRAM;
110 int count = -1;
111 int family = PF_INET;
112 uint16_t port = 7;
113 int backlog = 3;
114
115 socklen_t max_length = sizeof(struct sockaddr_in6);
116 uint8_t address_data[max_length];
117 struct sockaddr * address = (struct sockaddr *) address_data;
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;
125 char * data;
126 size_t length;
127 int index;
128 size_t reply_length;
129 int value;
130
131 // print the program label
132 printf("Task %d - ", task_get_id());
133 printf("%s\n", NAME);
134
135 // parse the command line arguments
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':
146 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family));
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':
164 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type));
165 type = (sock_type_t) value;
166 break;
167 case 'v':
168 verbose = 1;
169 break;
170 // long options with the double minus sign ('-')
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){
177 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family));
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){
190 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type));
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;
200 default:
201 print_unrecognized(index, argv[index] + 1);
202 echo_print_help();
203 return EINVAL;
204 }
205 }else{
206 print_unrecognized(index, argv[index]);
207 echo_print_help();
208 return EINVAL;
209 }
210 }
211
212 // check the buffer size
213 if(size <= 0){
214 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
215 size = 1024;
216 }
217 // size plus the terminating null (\0)
218 data = (char *) malloc(size + 1);
219 if(! data){
220 fprintf(stderr, "Failed to allocate receive buffer.\n");
221 return ENOMEM;
222 }
223
224 // set the reply size if set
225 reply_length = reply ? str_length(reply) : 0;
226
227 // prepare the address buffer
228 bzero(address_data, max_length);
229 switch(family){
230 case PF_INET:
231 address_in->sin_family = AF_INET;
232 address_in->sin_port = htons(port);
233 addrlen = sizeof(struct sockaddr_in);
234 break;
235 case PF_INET6:
236 address_in6->sin6_family = AF_INET6;
237 address_in6->sin6_port = htons(port);
238 addrlen = sizeof(struct sockaddr_in6);
239 break;
240 default:
241 fprintf(stderr, "Protocol family is not supported\n");
242 return EAFNOSUPPORT;
243 }
244
245 // get a listening socket
246 listening_id = socket(family, type, 0);
247 if(listening_id < 0){
248 socket_print_error(stderr, listening_id, "Socket create: ", "\n");
249 return listening_id;
250 }
251
252 // if the stream socket is used
253 if(type == SOCK_STREAM){
254 // check the backlog
255 if(backlog <= 0){
256 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
257 backlog = 3;
258 }
259 // set the backlog
260 if(ERROR_OCCURRED(listen(listening_id, backlog))){
261 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
262 return ERROR_CODE;
263 }
264 }
265
266 // bind the listenning socket
267 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
268 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
269 return ERROR_CODE;
270 }
271
272 if(verbose){
273 printf("Socket %d listenning at %d\n", listening_id, port);
274 }
275
276 socket_id = listening_id;
277
278 // do count times
279 // or indefinitely if set to a negative value
280 while(count){
281
282 addrlen = max_length;
283 if(type == SOCK_STREAM){
284 // acceept a socket if the stream socket is used
285 socket_id = accept(listening_id, address, &addrlen);
286 if(socket_id <= 0){
287 socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
288 }else{
289 if(verbose){
290 printf("Socket %d accepted\n", socket_id);
291 }
292 }
293 }
294
295 // if the datagram socket is used or the stream socked was accepted
296 if(socket_id > 0){
297
298 // receive an echo request
299 value = recvfrom(socket_id, data, size, 0, address, &addrlen);
300 if(value < 0){
301 socket_print_error(stderr, value, "Socket receive: ", "\n");
302 }else{
303 length = (size_t) value;
304 if(verbose){
305 // print the header
306
307 // get the source port and prepare the address buffer
308 address_start = NULL;
309 switch(address->sa_family){
310 case AF_INET:
311 port = ntohs(address_in->sin_port);
312 address_start = (uint8_t *) &address_in->sin_addr.s_addr;
313 break;
314 case AF_INET6:
315 port = ntohs(address_in6->sin6_port);
316 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
317 break;
318 default:
319 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
320 }
321 // parse the source address
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);
325 }else{
326 data[length] = '\0';
327 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
328 }
329 }
330 }
331
332 // answer the request either with the static reply or the original data
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");
335 }
336
337 }
338
339 // close the accepted stream socket
340 if(type == SOCK_STREAM){
341 if(ERROR_OCCURRED(closesocket(socket_id))){
342 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
343 }
344 }
345
346 }
347
348 // decrease the count if positive
349 if(count > 0){
350 -- count;
351 if(verbose){
352 printf("Waiting for next %d packet(s)\n", count);
353 }
354 }
355 }
356
357 if(verbose){
358 printf("Closing the socket\n");
359 }
360
361 // close the listenning socket
362 if(ERROR_OCCURRED(closesocket(listening_id))){
363 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
364 return ERROR_CODE;
365 }
366
367 if(verbose){
368 printf("Exiting\n");
369 }
370
371 return EOK;
372}
373
374/** @}
375 */
Note: See TracBrowser for help on using the repository browser.