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