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
RevLine 
[21580dd]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
[849ed54]29/** @addtogroup netecho
[21580dd]30 * @{
31 */
32
33/** @file
[849ed54]34 * Network echo application.
[21580dd]35 * Answers received packets.
36 */
37
38#include <malloc.h>
39#include <stdio.h>
[19f857a]40#include <str.h>
[21580dd]41#include <task.h>
42
[849ed54]43#include <in.h>
44#include <in6.h>
45#include <inet.h>
46#include <socket.h>
47#include <net_err.h>
[21580dd]48
[849ed54]49#include "parse.h"
50#include "print_error.h"
[21580dd]51
[849ed54]52/** Network echo module name.
[21580dd]53 */
[849ed54]54#define NAME "Network Echo"
[21580dd]55
[3be62bc]56/** Prints the application help.
57 */
58void echo_print_help(void);
59
[21580dd]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 */
[aadf01e]66int main(int argc, char * argv[]);
[21580dd]67
[aadf01e]68void echo_print_help(void){
[21580dd]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
[aadf01e]102int main(int argc, char * argv[]){
[21580dd]103 ERROR_DECLARE;
104
[aadf01e]105 size_t size = 1024;
106 int verbose = 0;
[3be62bc]107 char * reply = NULL;
108 sock_type_t type = SOCK_DGRAM;
[aadf01e]109 int count = -1;
110 int family = PF_INET;
[3be62bc]111 uint16_t port = 7;
[aadf01e]112 int backlog = 3;
113
[3be62bc]114 socklen_t max_length = sizeof(struct sockaddr_in6);
[aadf01e]115 uint8_t address_data[max_length];
[3be62bc]116 struct sockaddr * address = (struct sockaddr *) address_data;
[aadf01e]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;
[3be62bc]124 char * data;
[aadf01e]125 size_t length;
126 int index;
127 size_t reply_length;
128 int value;
129
[3be62bc]130 // print the program label
[aadf01e]131 printf("Task %d - ", task_get_id());
132 printf("%s\n", NAME);
133
[3be62bc]134 // parse the command line arguments
[aadf01e]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':
[3be62bc]145 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 0, parse_protocol_family));
[aadf01e]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':
[3be62bc]163 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 0, parse_socket_type));
[aadf01e]164 type = (sock_type_t) value;
165 break;
166 case 'v':
167 verbose = 1;
168 break;
[3be62bc]169 // long options with the double minus sign ('-')
[aadf01e]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){
[3be62bc]176 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "protocol family", 9, parse_protocol_family));
[aadf01e]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){
[3be62bc]189 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &value, "socket type", 7, parse_socket_type));
[aadf01e]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;
[21580dd]199 default:
[aadf01e]200 print_unrecognized(index, argv[index] + 1);
[21580dd]201 echo_print_help();
202 return EINVAL;
203 }
204 }else{
[aadf01e]205 print_unrecognized(index, argv[index]);
[21580dd]206 echo_print_help();
207 return EINVAL;
208 }
209 }
210
[3be62bc]211 // check the buffer size
[aadf01e]212 if(size <= 0){
213 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
[21580dd]214 size = 1024;
215 }
[3be62bc]216 // size plus the terminating null (\0)
[aadf01e]217 data = (char *) malloc(size + 1);
218 if(! data){
219 fprintf(stderr, "Failed to allocate receive buffer.\n");
[21580dd]220 return ENOMEM;
221 }
222
[3be62bc]223 // set the reply size if set
[aadf01e]224 reply_length = reply ? str_length(reply) : 0;
[21580dd]225
[3be62bc]226 // prepare the address buffer
[aadf01e]227 bzero(address_data, max_length);
228 switch(family){
[21580dd]229 case PF_INET:
230 address_in->sin_family = AF_INET;
[aadf01e]231 address_in->sin_port = htons(port);
232 addrlen = sizeof(struct sockaddr_in);
[21580dd]233 break;
234 case PF_INET6:
235 address_in6->sin6_family = AF_INET6;
[aadf01e]236 address_in6->sin6_port = htons(port);
237 addrlen = sizeof(struct sockaddr_in6);
[21580dd]238 break;
239 default:
[aadf01e]240 fprintf(stderr, "Protocol family is not supported\n");
[21580dd]241 return EAFNOSUPPORT;
242 }
243
[3be62bc]244 // get a listening socket
[aadf01e]245 listening_id = socket(family, type, 0);
246 if(listening_id < 0){
247 socket_print_error(stderr, listening_id, "Socket create: ", "\n");
[21580dd]248 return listening_id;
249 }
250
[3be62bc]251 // if the stream socket is used
[aadf01e]252 if(type == SOCK_STREAM){
[3be62bc]253 // check the backlog
[aadf01e]254 if(backlog <= 0){
255 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
[21580dd]256 backlog = 3;
257 }
[3be62bc]258 // set the backlog
[aadf01e]259 if(ERROR_OCCURRED(listen(listening_id, backlog))){
260 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
[21580dd]261 return ERROR_CODE;
262 }
263 }
264
[3be62bc]265 // bind the listenning socket
[aadf01e]266 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
267 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
[21580dd]268 return ERROR_CODE;
269 }
270
[aadf01e]271 if(verbose){
272 printf("Socket %d listenning at %d\n", listening_id, port);
273 }
[2d68c72]274
275 socket_id = listening_id;
[21580dd]276
[3be62bc]277 // do count times
278 // or indefinitely if set to a negative value
[aadf01e]279 while(count){
[3be62bc]280
[21580dd]281 addrlen = max_length;
[aadf01e]282 if(type == SOCK_STREAM){
[3be62bc]283 // acceept a socket if the stream socket is used
[aadf01e]284 socket_id = accept(listening_id, address, &addrlen);
285 if(socket_id <= 0){
286 socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
[1b053ca2]287 }else{
[aadf01e]288 if(verbose){
289 printf("Socket %d accepted\n", socket_id);
290 }
[21580dd]291 }
292 }
[3be62bc]293
294 // if the datagram socket is used or the stream socked was accepted
[aadf01e]295 if(socket_id > 0){
[3be62bc]296
297 // receive an echo request
[aadf01e]298 value = recvfrom(socket_id, data, size, 0, address, &addrlen);
299 if(value < 0){
300 socket_print_error(stderr, value, "Socket receive: ", "\n");
[21580dd]301 }else{
[aadf01e]302 length = (size_t) value;
303 if(verbose){
[3be62bc]304 // print the header
305
306 // get the source port and prepare the address buffer
[21580dd]307 address_start = NULL;
[aadf01e]308 switch(address->sa_family){
[21580dd]309 case AF_INET:
[aadf01e]310 port = ntohs(address_in->sin_port);
311 address_start = (uint8_t *) &address_in->sin_addr.s_addr;
[21580dd]312 break;
313 case AF_INET6:
[aadf01e]314 port = ntohs(address_in6->sin6_port);
315 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
[21580dd]316 break;
317 default:
[aadf01e]318 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
[21580dd]319 }
[3be62bc]320 // parse the source address
[aadf01e]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);
[21580dd]324 }else{
[aadf01e]325 data[length] = '\0';
326 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
[21580dd]327 }
328 }
329 }
[3be62bc]330
331 // answer the request either with the static reply or the original data
[aadf01e]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");
[21580dd]334 }
[3be62bc]335
[21580dd]336 }
[3be62bc]337
338 // close the accepted stream socket
[aadf01e]339 if(type == SOCK_STREAM){
340 if(ERROR_OCCURRED(closesocket(socket_id))){
341 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
[21580dd]342 }
343 }
[3be62bc]344
[21580dd]345 }
[3be62bc]346
347 // decrease the count if positive
[aadf01e]348 if(count > 0){
[21580dd]349 -- count;
[aadf01e]350 if(verbose){
351 printf("Waiting for next %d packet(s)\n", count);
352 }
[21580dd]353 }
354 }
355
[aadf01e]356 if(verbose){
357 printf("Closing the socket\n");
358 }
[21580dd]359
[3be62bc]360 // close the listenning socket
[aadf01e]361 if(ERROR_OCCURRED(closesocket(listening_id))){
362 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
[21580dd]363 return ERROR_CODE;
364 }
365
[aadf01e]366 if(verbose){
367 printf("Exiting\n");
368 }
[21580dd]369
370 return EOK;
371}
372
373/** @}
374 */
Note: See TracBrowser for help on using the repository browser.