source: mainline/uspace/app/netecho/netecho.c@ 849ed54

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

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • 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 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 // print the program label
131 printf("Task %d - ", task_get_id());
132 printf("%s\n", NAME);
133
134 // parse the command line arguments
135 for(index = 1; index < argc; ++ index){
136 if(argv[index][0] == '-'){
137 switch(argv[index][1]){
138 case 'b':
139 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 0));
140 break;
141 case 'c':
142 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 0));
143 break;
144 case 'f':
145 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family));
146 break;
147 case 'h':
148 echo_print_help();
149 return EOK;
150 break;
151 case 'p':
152 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 0));
153 port = (uint16_t) value;
154 break;
155 case 'r':
156 ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 0));
157 break;
158 case 's':
159 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 0));
160 size = (value >= 0) ? (size_t) value : 0;
161 break;
162 case 't':
163 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type));
164 type = (sock_type_t) value;
165 break;
166 case 'v':
167 verbose = 1;
168 break;
169 // long options with the double minus sign ('-')
170 case '-':
171 if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){
172 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &backlog, "accepted sockets queue size", 8));
173 }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
174 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "message count", 8));
175 }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
176 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family));
177 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
178 echo_print_help();
179 return EOK;
180 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){
181 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "port number", 7));
182 port = (uint16_t) value;
183 }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){
184 ERROR_PROPAGATE(parse_parameter_string(argc, argv, &index, &reply, "reply string", 8));
185 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
186 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &value, "receive size", 7));
187 size = (value >= 0) ? (size_t) value : 0;
188 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
189 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type));
190 type = (sock_type_t) value;
191 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){
192 verbose = 1;
193 }else{
194 print_unrecognized(index, argv[index] + 2);
195 echo_print_help();
196 return EINVAL;
197 }
198 break;
199 default:
200 print_unrecognized(index, argv[index] + 1);
201 echo_print_help();
202 return EINVAL;
203 }
204 }else{
205 print_unrecognized(index, argv[index]);
206 echo_print_help();
207 return EINVAL;
208 }
209 }
210
211 // check the buffer size
212 if(size <= 0){
213 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
214 size = 1024;
215 }
216 // size plus the terminating null (\0)
217 data = (char *) malloc(size + 1);
218 if(! data){
219 fprintf(stderr, "Failed to allocate receive buffer.\n");
220 return ENOMEM;
221 }
222
223 // set the reply size if set
224 reply_length = reply ? str_length(reply) : 0;
225
226 // prepare the address buffer
227 bzero(address_data, max_length);
228 switch(family){
229 case PF_INET:
230 address_in->sin_family = AF_INET;
231 address_in->sin_port = htons(port);
232 addrlen = sizeof(struct sockaddr_in);
233 break;
234 case PF_INET6:
235 address_in6->sin6_family = AF_INET6;
236 address_in6->sin6_port = htons(port);
237 addrlen = sizeof(struct sockaddr_in6);
238 break;
239 default:
240 fprintf(stderr, "Protocol family is not supported\n");
241 return EAFNOSUPPORT;
242 }
243
244 // get a listening socket
245 listening_id = socket(family, type, 0);
246 if(listening_id < 0){
247 socket_print_error(stderr, listening_id, "Socket create: ", "\n");
248 return listening_id;
249 }
250
251 // if the stream socket is used
252 if(type == SOCK_STREAM){
253 // check the backlog
254 if(backlog <= 0){
255 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
256 backlog = 3;
257 }
258 // set the backlog
259 if(ERROR_OCCURRED(listen(listening_id, backlog))){
260 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
261 return ERROR_CODE;
262 }
263 }
264
265 // bind the listenning socket
266 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
267 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
268 return ERROR_CODE;
269 }
270
271 if(verbose){
272 printf("Socket %d listenning at %d\n", listening_id, port);
273 }
274
275 socket_id = listening_id;
276
277 // do count times
278 // or indefinitely if set to a negative value
279 while(count){
280
281 addrlen = max_length;
282 if(type == SOCK_STREAM){
283 // acceept a socket if the stream socket is used
284 socket_id = accept(listening_id, address, &addrlen);
285 if(socket_id <= 0){
286 socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
287 }else{
288 if(verbose){
289 printf("Socket %d accepted\n", socket_id);
290 }
291 }
292 }
293
294 // if the datagram socket is used or the stream socked was accepted
295 if(socket_id > 0){
296
297 // receive an echo request
298 value = recvfrom(socket_id, data, size, 0, address, &addrlen);
299 if(value < 0){
300 socket_print_error(stderr, value, "Socket receive: ", "\n");
301 }else{
302 length = (size_t) value;
303 if(verbose){
304 // print the header
305
306 // get the source port and prepare the address buffer
307 address_start = NULL;
308 switch(address->sa_family){
309 case AF_INET:
310 port = ntohs(address_in->sin_port);
311 address_start = (uint8_t *) &address_in->sin_addr.s_addr;
312 break;
313 case AF_INET6:
314 port = ntohs(address_in6->sin6_port);
315 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
316 break;
317 default:
318 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
319 }
320 // parse the source address
321 if(address_start){
322 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
323 fprintf(stderr, "Received address error %d\n", ERROR_CODE);
324 }else{
325 data[length] = '\0';
326 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
327 }
328 }
329 }
330
331 // answer the request either with the static reply or the original data
332 if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){
333 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
334 }
335
336 }
337
338 // close the accepted stream socket
339 if(type == SOCK_STREAM){
340 if(ERROR_OCCURRED(closesocket(socket_id))){
341 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
342 }
343 }
344
345 }
346
347 // decrease the count if positive
348 if(count > 0){
349 -- count;
350 if(verbose){
351 printf("Waiting for next %d packet(s)\n", count);
352 }
353 }
354 }
355
356 if(verbose){
357 printf("Closing the socket\n");
358 }
359
360 // close the listenning socket
361 if(ERROR_OCCURRED(closesocket(listening_id))){
362 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
363 return ERROR_CODE;
364 }
365
366 if(verbose){
367 printf("Exiting\n");
368 }
369
370 return EOK;
371}
372
373/** @}
374 */
Note: See TracBrowser for help on using the repository browser.