Rev 4741 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4741 | Rev 4743 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup echo |
29 | /** @addtogroup echo |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * Echo application. |
34 | * Echo application. |
35 | * Answers received packets. |
35 | * Answers received packets. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <malloc.h> |
38 | #include <malloc.h> |
39 | #include <stdio.h> |
39 | #include <stdio.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | #include <task.h> |
41 | #include <task.h> |
42 | 42 | ||
43 | #include "../../include/in.h" |
43 | #include "../../include/in.h" |
44 | #include "../../include/in6.h" |
44 | #include "../../include/in6.h" |
45 | #include "../../include/inet.h" |
45 | #include "../../include/inet.h" |
46 | #include "../../include/socket.h" |
46 | #include "../../include/socket.h" |
47 | 47 | ||
48 | #include "../../err.h" |
48 | #include "../../err.h" |
49 | 49 | ||
50 | #include "../parse.h" |
50 | #include "../parse.h" |
51 | #include "../print_error.h" |
51 | #include "../print_error.h" |
52 | 52 | ||
53 | /** Echo module name. |
53 | /** Echo module name. |
54 | */ |
54 | */ |
55 | #define NAME "Echo" |
55 | #define NAME "Echo" |
56 | 56 | ||
57 | /** Module entry point. |
57 | /** Module entry point. |
58 | * Reads command line parameters and starts listenning. |
58 | * Reads command line parameters and starts listenning. |
59 | * @param argc The number of command line parameters. Input parameter. |
59 | * @param argc The number of command line parameters. Input parameter. |
60 | * @param argv The command line parameters. Input parameter. |
60 | * @param argv The command line parameters. Input parameter. |
61 | * @returns EOK on success. |
61 | * @returns EOK on success. |
62 | */ |
62 | */ |
63 | int main( int argc, char * argv[] ); |
63 | int main( int argc, char * argv[] ); |
64 | 64 | ||
65 | /** Prints the application help. |
65 | /** Prints the application help. |
66 | */ |
66 | */ |
67 | void print_help( void ); |
67 | void print_help( void ); |
68 | 68 | ||
69 | /** Translates the character string to the protocol family number. |
69 | /** Translates the character string to the protocol family number. |
70 | * @param name The protocol family name. Input parameter. |
70 | * @param name The protocol family name. Input parameter. |
71 | * @returns The corresponding protocol family number. |
71 | * @returns The corresponding protocol family number. |
72 | * @returns EPFNOSUPPORTED if the protocol family is not supported. |
72 | * @returns EPFNOSUPPORTED if the protocol family is not supported. |
73 | */ |
73 | */ |
74 | int parse_protocol_family( const char * name ); |
74 | int parse_protocol_family( const char * name ); |
75 | 75 | ||
76 | /** Translates the character string to the socket type number. |
76 | /** Translates the character string to the socket type number. |
77 | * @param name The socket type name. Input parameter. |
77 | * @param name The socket type name. Input parameter. |
78 | * @returns The corresponding socket type number. |
78 | * @returns The corresponding socket type number. |
79 | * @returns ESOCKNOSUPPORTED if the socket type is not supported. |
79 | * @returns ESOCKNOSUPPORTED if the socket type is not supported. |
80 | */ |
80 | */ |
81 | int parse_socket_type( const char * name ); |
81 | int parse_socket_type( const char * name ); |
82 | 82 | ||
83 | void print_help( void ){ |
83 | void print_help( void ){ |
84 | printf( |
84 | printf( |
85 | "Network Echo aplication\n" \ |
85 | "Network Echo aplication\n" \ |
86 | "Usage: echo [options]\n" \ |
86 | "Usage: echo [options]\n" \ |
87 | "Where options are:\n" \ |
87 | "Where options are:\n" \ |
88 | "-b backlog | --backlog=size\n" \ |
88 | "-b backlog | --backlog=size\n" \ |
89 | "\tThe size of the accepted sockets queue. Only for SOCK_STREAM. The default is 3.\n" \ |
89 | "\tThe size of the accepted sockets queue. Only for SOCK_STREAM. The default is 3.\n" \ |
90 | "\n" \ |
90 | "\n" \ |
91 | "-c count | --count=count\n" \ |
91 | "-c count | --count=count\n" \ |
92 | "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \ |
92 | "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \ |
93 | "\n" \ |
93 | "\n" \ |
94 | "-f protocol_family | --family=protocol_family\n" \ |
94 | "-f protocol_family | --family=protocol_family\n" \ |
95 | "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" |
95 | "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" |
96 | "\n" \ |
96 | "\n" \ |
97 | "-h | --help\n" \ |
97 | "-h | --help\n" \ |
98 | "\tShow this application help.\n" |
98 | "\tShow this application help.\n" |
99 | "\n" \ |
99 | "\n" \ |
100 | "-p port_number | --port=port_number\n" \ |
100 | "-p port_number | --port=port_number\n" \ |
101 | "\tThe port number the application should listen at. The default is 7.\n" \ |
101 | "\tThe port number the application should listen at. The default is 7.\n" \ |
102 | "\n" \ |
102 | "\n" \ |
103 | "-r reply_string | --reply=reply_string\n" \ |
103 | "-r reply_string | --reply=reply_string\n" \ |
104 | "\tThe constant reply string. The default is the original data received.\n" \ |
104 | "\tThe constant reply string. The default is the original data received.\n" \ |
105 | "\n" \ |
105 | "\n" \ |
106 | "-s receive_size | --size=receive_size\n" \ |
106 | "-s receive_size | --size=receive_size\n" \ |
107 | "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \ |
107 | "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \ |
108 | "\n" \ |
108 | "\n" \ |
109 | "-t socket_type | --type=socket_type\n" \ |
109 | "-t socket_type | --type=socket_type\n" \ |
110 | "\tThe listenning socket type. Only the SOCK_DGRAM and the SOCK_STREAM are supported.\n" \ |
110 | "\tThe listenning socket type. Only the SOCK_DGRAM and the SOCK_STREAM are supported.\n" \ |
111 | "\n" \ |
111 | "\n" \ |
112 | "-v | --verbose\n" \ |
112 | "-v | --verbose\n" \ |
113 | "\tShow all output messages.\n" |
113 | "\tShow all output messages.\n" |
114 | ); |
114 | ); |
115 | } |
115 | } |
116 | 116 | ||
117 | int parse_protocol_family( const char * name ){ |
117 | int parse_protocol_family( const char * name ){ |
118 | if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ |
118 | if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ |
119 | return PF_INET; |
119 | return PF_INET; |
120 | }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){ |
120 | }else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){ |
121 | return PF_INET6; |
121 | return PF_INET6; |
122 | } |
122 | } |
123 | return EPFNOSUPPORT; |
123 | return EPFNOSUPPORT; |
124 | } |
124 | } |
125 | 125 | ||
126 | int parse_socket_type( const char * name ){ |
126 | int parse_socket_type( const char * name ){ |
127 | if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){ |
127 | if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){ |
128 | return SOCK_DGRAM; |
128 | return SOCK_DGRAM; |
129 | }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){ |
129 | }else if( str_lcmp( name, "SOCK_STREAM", 12 ) == 0 ){ |
130 | return SOCK_STREAM; |
130 | return SOCK_STREAM; |
131 | } |
131 | } |
132 | return ESOCKTNOSUPPORT; |
132 | return ESOCKTNOSUPPORT; |
133 | } |
133 | } |
134 | 134 | ||
135 | int main( int argc, char * argv[] ){ |
135 | int main( int argc, char * argv[] ){ |
136 | ERROR_DECLARE; |
136 | ERROR_DECLARE; |
137 | 137 | ||
138 | size_t size = 1024; |
138 | size_t size = 1024; |
139 | int verbose = 0; |
139 | int verbose = 0; |
140 | char * reply = NULL; |
140 | char * reply = NULL; |
141 | sock_type_t type = SOCK_DGRAM; |
141 | sock_type_t type = SOCK_DGRAM; |
142 | int count = -1; |
142 | int count = -1; |
143 | int family = PF_INET; |
143 | int family = PF_INET; |
144 | uint16_t port = 7; |
144 | uint16_t port = 7; |
145 | int backlog = 3; |
145 | int backlog = 3; |
146 | 146 | ||
147 | socklen_t max_length = sizeof( struct sockaddr_in6 ); |
147 | socklen_t max_length = sizeof( struct sockaddr_in6 ); |
148 | uint8_t address_data[ max_length ]; |
148 | uint8_t address_data[ max_length ]; |
149 | struct sockaddr * address = ( struct sockaddr * ) address_data; |
149 | struct sockaddr * address = ( struct sockaddr * ) address_data; |
150 | struct sockaddr_in * address_in = ( struct sockaddr_in * ) address; |
150 | struct sockaddr_in * address_in = ( struct sockaddr_in * ) address; |
151 | struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; |
151 | struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address; |
152 | socklen_t addrlen; |
152 | socklen_t addrlen; |
153 | char address_string[ INET6_ADDRSTRLEN ]; |
153 | char address_string[ INET6_ADDRSTRLEN ]; |
154 | uint8_t * address_start; |
154 | uint8_t * address_start; |
155 | int socket_id; |
155 | int socket_id; |
156 | int listening_id; |
156 | int listening_id; |
157 | char * data; |
157 | char * data; |
158 | size_t length; |
158 | size_t length; |
159 | int index; |
159 | int index; |
160 | size_t reply_length; |
160 | size_t reply_length; |
161 | int value; |
161 | int value; |
162 | 162 | ||
163 | printf( "Task %d - ", task_get_id()); |
163 | printf( "Task %d - ", task_get_id()); |
164 | printf( "%s\n", NAME ); |
164 | printf( "%s\n", NAME ); |
165 | 165 | ||
166 | for( index = 1; index < argc; ++ index ){ |
166 | for( index = 1; index < argc; ++ index ){ |
167 | if( argv[ index ][ 0 ] == '-' ){ |
167 | if( argv[ index ][ 0 ] == '-' ){ |
168 | switch( argv[ index ][ 1 ] ){ |
168 | switch( argv[ index ][ 1 ] ){ |
169 | case 'b': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 0 )); |
169 | case 'b': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 0 )); |
170 | break; |
170 | break; |
171 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 )); |
171 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 0 )); |
172 | break; |
172 | break; |
173 | case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family )); |
173 | case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family )); |
174 | break; |
174 | break; |
175 | case 'h': print_help(); |
175 | case 'h': print_help(); |
176 | return EOK; |
176 | return EOK; |
177 | break; |
177 | break; |
178 | case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); |
178 | case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); |
179 | port = ( uint16_t ) value; |
179 | port = ( uint16_t ) value; |
180 | break; |
180 | break; |
181 | case 'r': ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 )); |
181 | case 'r': ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 )); |
182 | break; |
182 | break; |
183 | case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 )); |
183 | case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 )); |
184 | size = (value >= 0 ) ? ( size_t ) value : 0; |
184 | size = (value >= 0 ) ? ( size_t ) value : 0; |
185 | break; |
185 | break; |
186 | case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type )); |
186 | case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 0, parse_socket_type )); |
187 | type = ( sock_type_t ) value; |
187 | type = ( sock_type_t ) value; |
188 | break; |
188 | break; |
189 | case 'v': verbose = 1; |
189 | case 'v': verbose = 1; |
190 | break; |
190 | break; |
191 | case '-': if( str_lcmp( argv[ index ] + 2, "backlog=", 6 ) == 0 ){ |
191 | case '-': if( str_lcmp( argv[ index ] + 2, "backlog=", 6 ) == 0 ){ |
192 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 8 )); |
192 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & backlog, "accepted sockets queue size", 8 )); |
193 | }else if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
193 | }else if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
194 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 )); |
194 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "message count", 8 )); |
195 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
195 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
196 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family )); |
196 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family )); |
197 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
197 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
198 | print_help(); |
198 | print_help(); |
199 | return EOK; |
199 | return EOK; |
200 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
200 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
201 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
201 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
202 | port = ( uint16_t ) value; |
202 | port = ( uint16_t ) value; |
203 | }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){ |
203 | }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){ |
204 | ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 )); |
204 | ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 )); |
205 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
205 | }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){ |
206 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 )); |
206 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 )); |
207 | size = (value >= 0 ) ? ( size_t ) value : 0; |
207 | size = (value >= 0 ) ? ( size_t ) value : 0; |
208 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
208 | }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){ |
209 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type )); |
209 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket type", 7, parse_socket_type )); |
210 | type = ( sock_type_t ) value; |
210 | type = ( sock_type_t ) value; |
211 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
211 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
212 | verbose = 1; |
212 | verbose = 1; |
213 | }else{ |
213 | }else{ |
214 | print_unrecognized( index, argv[ index ] + 2 ); |
214 | print_unrecognized( index, argv[ index ] + 2 ); |
215 | print_help(); |
215 | print_help(); |
216 | return EINVAL; |
216 | return EINVAL; |
217 | } |
217 | } |
218 | break; |
218 | break; |
219 | default: |
219 | default: |
220 | print_unrecognized( index, argv[ index ] + 1 ); |
220 | print_unrecognized( index, argv[ index ] + 1 ); |
221 | print_help(); |
221 | print_help(); |
222 | return EINVAL; |
222 | return EINVAL; |
223 | } |
223 | } |
224 | }else{ |
224 | }else{ |
225 | print_unrecognized( index, argv[ index ] ); |
225 | print_unrecognized( index, argv[ index ] ); |
226 | print_help(); |
226 | print_help(); |
227 | return EINVAL; |
227 | return EINVAL; |
228 | } |
228 | } |
229 | } |
229 | } |
230 | 230 | ||
231 | if( size <= 0 ){ |
231 | if( size <= 0 ){ |
232 | fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size ); |
232 | fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size ); |
233 | size = 1024; |
233 | size = 1024; |
234 | } |
234 | } |
235 | // size plus terminating null (\0) |
235 | // size plus terminating null (\0) |
236 | data = ( char * ) malloc( size + 1 ); |
236 | data = ( char * ) malloc( size + 1 ); |
237 | if( ! data ){ |
237 | if( ! data ){ |
238 | fprintf( stderr, "Failed to allocate receive buffer.\n" ); |
238 | fprintf( stderr, "Failed to allocate receive buffer.\n" ); |
239 | return ENOMEM; |
239 | return ENOMEM; |
240 | } |
240 | } |
241 | 241 | ||
242 | reply_length = reply ? str_length( reply ) : 0; |
242 | reply_length = reply ? str_length( reply ) : 0; |
243 | 243 | ||
244 | listening_id = socket( family, type, 0 ); |
244 | listening_id = socket( family, type, 0 ); |
245 | if( listening_id < 0 ){ |
245 | if( listening_id < 0 ){ |
246 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
246 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
247 | return listening_id; |
247 | return listening_id; |
248 | } |
248 | } |
249 | 249 | ||
250 | bzero( address_data, max_length ); |
250 | bzero( address_data, max_length ); |
251 | switch( family ){ |
251 | switch( family ){ |
252 | case PF_INET: |
252 | case PF_INET: |
253 | address_in->sin_family = AF_INET; |
253 | address_in->sin_family = AF_INET; |
254 | address_in->sin_port = htons( port ); |
254 | address_in->sin_port = htons( port ); |
255 | addrlen = sizeof( struct sockaddr_in ); |
255 | addrlen = sizeof( struct sockaddr_in ); |
256 | break; |
256 | break; |
257 | case PF_INET6: |
257 | case PF_INET6: |
258 | address_in6->sin6_family = AF_INET6; |
258 | address_in6->sin6_family = AF_INET6; |
259 | address_in6->sin6_port = htons( port ); |
259 | address_in6->sin6_port = htons( port ); |
260 | addrlen = sizeof( struct sockaddr_in6 ); |
260 | addrlen = sizeof( struct sockaddr_in6 ); |
261 | break; |
261 | break; |
262 | default: |
262 | default: |
263 | fprintf( stderr, "Protocol family is not supported\n" ); |
263 | fprintf( stderr, "Protocol family is not supported\n" ); |
264 | return EAFNOSUPPORT; |
264 | return EAFNOSUPPORT; |
265 | } |
265 | } |
266 | 266 | ||
267 | listening_id = socket( family, type, 0 ); |
267 | listening_id = socket( family, type, 0 ); |
268 | if( listening_id < 0 ){ |
268 | if( listening_id < 0 ){ |
269 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
269 | socket_print_error( stderr, listening_id, "Socket create: ", "\n" ); |
270 | return listening_id; |
270 | return listening_id; |
271 | } |
271 | } |
272 | 272 | ||
273 | if( type == SOCK_STREAM ){ |
273 | if( type == SOCK_STREAM ){ |
274 | if( backlog <= 0 ){ |
274 | if( backlog <= 0 ){ |
275 | fprintf( stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size ); |
275 | fprintf( stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size ); |
276 | backlog = 3; |
276 | backlog = 3; |
277 | } |
277 | } |
278 | if( ERROR_OCCURRED( listen( listening_id, backlog ))){ |
278 | if( ERROR_OCCURRED( listen( listening_id, backlog ))){ |
279 | socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" ); |
279 | socket_print_error( stderr, ERROR_CODE, "Socket listen: ", "\n" ); |
280 | return ERROR_CODE; |
280 | return ERROR_CODE; |
281 | } |
281 | } |
282 | } |
282 | } |
283 | 283 | ||
284 | socket_id = listening_id; |
284 | socket_id = listening_id; |
285 | 285 | ||
286 | if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){ |
286 | if( ERROR_OCCURRED( bind( listening_id, address, addrlen ))){ |
287 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
287 | socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" ); |
288 | return ERROR_CODE; |
288 | return ERROR_CODE; |
289 | } |
289 | } |
290 | 290 | ||
291 | if( verbose ) printf( "Listenning at %d\n", port ); |
291 | if( verbose ) printf( "Listenning at %d\n", port ); |
292 | 292 | ||
293 | while( count ){ |
293 | while( count ){ |
294 | addrlen = max_length; |
294 | addrlen = max_length; |
295 | if( type == SOCK_STREAM ){ |
295 | if( type == SOCK_STREAM ){ |
296 | socket_id = accept( listening_id, address, & addrlen ); |
296 | socket_id = accept( listening_id, address, & addrlen ); |
297 | if( socket_id <= 0 ){ |
297 | if( socket_id <= 0 ){ |
298 | socket_print_error( stderr, socket_id, "Socket accept: ", "\n" ); |
298 | socket_print_error( stderr, socket_id, "Socket accept: ", "\n" ); |
299 | } |
299 | } |
- | 300 | if( verbose ) printf( "Socket %d accepted\n", socket_id ); |
|
300 | } |
301 | } |
301 | if( socket_id > 0 ){ |
302 | if( socket_id > 0 ){ |
302 | value = recvfrom( socket_id, data, size, 0, address, & addrlen ); |
303 | value = recvfrom( socket_id, data, size, 0, address, & addrlen ); |
303 | if( value < 0 ){ |
304 | if( value < 0 ){ |
304 | socket_print_error( stderr, value, "Socket receive: ", "\n" ); |
305 | socket_print_error( stderr, value, "Socket receive: ", "\n" ); |
305 | }else{ |
306 | }else{ |
306 | length = ( size_t ) value; |
307 | length = ( size_t ) value; |
307 | if( verbose ){ |
308 | if( verbose ){ |
308 | address_start = NULL; |
309 | address_start = NULL; |
309 | switch( address->sa_family ){ |
310 | switch( address->sa_family ){ |
310 | case AF_INET: |
311 | case AF_INET: |
311 | port = ntohs( address_in->sin_port ); |
312 | port = ntohs( address_in->sin_port ); |
312 | address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; |
313 | address_start = ( uint8_t * ) & address_in->sin_addr.s_addr; |
313 | break; |
314 | break; |
314 | case AF_INET6: |
315 | case AF_INET6: |
315 | port = ntohs( address_in6->sin6_port ); |
316 | port = ntohs( address_in6->sin6_port ); |
316 | address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; |
317 | address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr; |
317 | break; |
318 | break; |
318 | default: |
319 | default: |
319 | fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family ); |
320 | fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family ); |
320 | } |
321 | } |
321 | if( address_start ){ |
322 | if( address_start ){ |
322 | if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){ |
323 | 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 | fprintf( stderr, "Received address error %d\n", ERROR_CODE ); |
324 | }else{ |
325 | }else{ |
325 | data[ length ] = '\0'; |
326 | data[ length ] = '\0'; |
326 | printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data ); |
327 | printf( "Received %d bytes from %s:%d\n%s\n", length, address_string, port, data ); |
327 | } |
328 | } |
328 | } |
329 | } |
329 | } |
330 | } |
330 | if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){ |
331 | if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){ |
331 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); |
332 | socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" ); |
332 | } |
333 | } |
333 | } |
334 | } |
334 | if( type == SOCK_STREAM ){ |
335 | if( type == SOCK_STREAM ){ |
335 | if( ERROR_OCCURRED( closesocket( socket_id ))){ |
336 | if( ERROR_OCCURRED( closesocket( socket_id ))){ |
336 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
337 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
337 | } |
338 | } |
338 | } |
339 | } |
339 | } |
340 | } |
340 | if( count > 0 ){ |
341 | if( count > 0 ){ |
341 | -- count; |
342 | -- count; |
342 | if( verbose ) printf( "Waiting for next %d packet(s)\n", count ); |
343 | if( verbose ) printf( "Waiting for next %d packet(s)\n", count ); |
343 | } |
344 | } |
344 | } |
345 | } |
345 | 346 | ||
346 | if( verbose ) printf( "Closing the socket\n" ); |
347 | if( verbose ) printf( "Closing the socket\n" ); |
347 | 348 | ||
348 | if( ERROR_OCCURRED( closesocket( listening_id ))){ |
349 | if( ERROR_OCCURRED( closesocket( listening_id ))){ |
349 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
350 | socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" ); |
350 | return ERROR_CODE; |
351 | return ERROR_CODE; |
351 | } |
352 | } |
352 | 353 | ||
353 | if( verbose ) printf( "Exiting\n" ); |
354 | if( verbose ) printf( "Exiting\n" ); |
354 | 355 | ||
355 | return EOK; |
356 | return EOK; |
356 | } |
357 | } |
357 | 358 | ||
358 | /** @} |
359 | /** @} |
359 | */ |
360 | */ |
360 | 361 |