source: mainline/uspace/app/netecho/netecho.c@ d0d1f4f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d0d1f4f was d9e2e0e, checked in by Jakub Jermar <jakub@…>, 15 years ago

Move the client socket code to the standard library.

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