Rev 4708 | Rev 4731 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4708 | Rev 4709 | ||
---|---|---|---|
Line 44... | Line 44... | ||
44 | #include "../../include/inet.h" |
44 | #include "../../include/inet.h" |
45 | #include "../../include/socket.h" |
45 | #include "../../include/socket.h" |
46 | 46 | ||
47 | #include "../../err.h" |
47 | #include "../../err.h" |
48 | 48 | ||
- | 49 | #include "../parse.h" |
|
- | 50 | ||
49 | /** Echo module name. |
51 | /** Echo module name. |
50 | */ |
52 | */ |
51 | #define NAME "Echo" |
53 | #define NAME "Echo" |
52 | 54 | ||
53 | /** Module entry point. |
55 | /** Module entry point. |
Line 56... | Line 58... | ||
56 | * @param argv The command line parameters. Input parameter. |
58 | * @param argv The command line parameters. Input parameter. |
57 | * @returns EOK on success. |
59 | * @returns EOK on success. |
58 | */ |
60 | */ |
59 | int main( int argc, char * argv[] ); |
61 | int main( int argc, char * argv[] ); |
60 | 62 | ||
61 | /** @name Output printing functions |
- | |
62 | */ |
- | |
63 | /*@{*/ |
- | |
64 | - | ||
65 | /** Prints the application help. |
63 | /** Prints the application help. |
66 | */ |
64 | */ |
67 | void print_help( void ); |
65 | void print_help( void ); |
68 | 66 | ||
69 | /** Prints the parameter unrecognized message and the application help. |
- | |
70 | * @param index The index of the parameter. Input parameter. |
- | |
71 | * @param parameter The parameter name. Input parameter. |
- | |
72 | */ |
- | |
73 | void print_unrecognized( int index, const char * parameter ); |
- | |
74 | - | ||
75 | /*@}*/ |
- | |
76 | - | ||
77 | /** @name Command line argumets parsing functions |
- | |
78 | */ |
- | |
79 | /*@{*/ |
- | |
80 | - | ||
81 | /** Parses the next parameter as an integral number. |
- | |
82 | * Uses the offseted actual parameter if the offset is set or the next one if not. |
- | |
83 | * @param argc The total number of the parameters. Input parameter. |
- | |
84 | * @param argv The parameters. Input parameter. |
- | |
85 | * @param index The actual parameter index. Input/output parameter. |
- | |
86 | * @param value The parsed parameter value. Output parameter. |
- | |
87 | * @param name The parameter name to be printed on errors. Input parameter. |
- | |
88 | * @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter. |
- | |
89 | * @returns EOK on success. |
- | |
90 | * @returns EINVAL if the parameter is missing. |
- | |
91 | * @returns EINVAL if the parameter is in wrong format. |
- | |
92 | */ |
- | |
93 | int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset ); |
- | |
94 | - | ||
95 | /** Parses the next parameter as a character string. |
- | |
96 | * Uses the offseted actual parameter if the offset is set or the next one if not. |
- | |
97 | * @param argc The total number of the parameters. Input parameter. |
- | |
98 | * @param argv The parameters. Input parameter. |
- | |
99 | * @param index The actual parameter index. Input/output parameter. |
- | |
100 | * @param value The parsed parameter value. Output parameter. |
- | |
101 | * @param name The parameter name to be printed on errors. Input parameter. |
- | |
102 | * @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter. |
- | |
103 | * @returns EOK on success. |
- | |
104 | * @returns EINVAL if the parameter is missing. |
- | |
105 | */ |
- | |
106 | int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset ); |
- | |
107 | - | ||
108 | /** Parses the next named parameter as an integral number. |
- | |
109 | * Uses the offseted actual parameter if the offset is set or the next one if not. |
- | |
110 | * Translates the parameter using the parse_value function. |
- | |
111 | * @param argc The total number of the parameters. Input parameter. |
- | |
112 | * @param argv The parameters. Input parameter. |
- | |
113 | * @param index The actual parameter index. Input/output parameter. |
- | |
114 | * @param value The parsed parameter value. Output parameter. |
- | |
115 | * @param name The parameter name to be printed on errors. Input parameter. |
- | |
116 | * @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter. |
- | |
117 | * @param parse_value The translation function to parse the named value. |
- | |
118 | * @returns EOK on success. |
- | |
119 | * @returns EINVAL if the parameter is missing. |
- | |
120 | * @returns ENOENT if the parameter name has not been found. |
- | |
121 | */ |
- | |
122 | int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value )); |
- | |
123 | - | ||
124 | /** Translates the character string to the protocol family number. |
67 | /** Translates the character string to the protocol family number. |
125 | * @param name The protocol family name. Input parameter. |
68 | * @param name The protocol family name. Input parameter. |
126 | * @returns The corresponding protocol family number. |
69 | * @returns The corresponding protocol family number. |
127 | */ |
70 | */ |
128 | int parse_protocol_family( const char * name ); |
71 | int parse_protocol_family( const char * name ); |
Line 131... | Line 74... | ||
131 | * @param name The socket type name. Input parameter. |
74 | * @param name The socket type name. Input parameter. |
132 | * @returns The corresponding socket type number. |
75 | * @returns The corresponding socket type number. |
133 | */ |
76 | */ |
134 | int parse_socket_type( const char * name ); |
77 | int parse_socket_type( const char * name ); |
135 | 78 | ||
136 | /*@}*/ |
- | |
137 | - | ||
138 | void print_help( void ){ |
79 | void print_help( void ){ |
139 | printf( |
80 | printf( |
140 | "Network Echo aplication\n" \ |
81 | "Network Echo aplication\n" \ |
141 | "Usage: echo [options]\n" \ |
82 | "Usage: echo [options]\n" \ |
142 | "Where options are:\n" \ |
83 | "Where options are:\n" \ |
143 | "-p port_number | --port=port_number\n" \ |
- | |
144 | "\tThe port number the application should listen at. The default is 7.\n" \ |
- | |
145 | "\n" \ |
- | |
146 | "-s receive_size | --size=receive_size\n" \ |
- | |
147 | "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \ |
- | |
148 | "\n" \ |
- | |
149 | "-c count | --count\n" \ |
84 | "-c count | --count=count\n" \ |
150 | "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \ |
85 | "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \ |
151 | "\n" \ |
86 | "\n" \ |
152 | "-r reply_string | --reply=reply_string\n" \ |
- | |
153 | "\tThe constant reply string. The default is the original data received.\n" \ |
- | |
154 | "\n" \ |
- | |
155 | "-f protocol_family | --family=protocol_family\n" \ |
87 | "-f protocol_family | --family=protocol_family\n" \ |
156 | "\tThe listenning socket protocol family. Only the PF_INET is supported.\n" |
88 | "\tThe listenning socket protocol family. Only the PF_INET is supported.\n" |
157 | "\n" \ |
89 | "\n" \ |
158 | "-h | --help\n" \ |
90 | "-h | --help\n" \ |
159 | "\tShow this application help.\n" |
91 | "\tShow this application help.\n" |
160 | "\n" \ |
92 | "\n" \ |
- | 93 | "-p port_number | --port=port_number\n" \ |
|
- | 94 | "\tThe port number the application should listen at. The default is 7.\n" \ |
|
- | 95 | "\n" \ |
|
- | 96 | "-r reply_string | --reply=reply_string\n" \ |
|
- | 97 | "\tThe constant reply string. The default is the original data received.\n" \ |
|
- | 98 | "\n" \ |
|
- | 99 | "-s receive_size | --size=receive_size\n" \ |
|
- | 100 | "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \ |
|
- | 101 | "\n" \ |
|
161 | "-t socket_type | --type=socket_type\n" \ |
102 | "-t socket_type | --type=socket_type\n" \ |
162 | "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \ |
103 | "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \ |
163 | "\n" \ |
104 | "\n" \ |
164 | "-v | --verbose\n" \ |
105 | "-v | --verbose\n" \ |
165 | "\tShow all output messages.\n" |
106 | "\tShow all output messages.\n" |
166 | ); |
107 | ); |
167 | } |
108 | } |
168 | 109 | ||
169 | int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset ){ |
- | |
170 | char * rest; |
- | |
171 | - | ||
172 | if( offset ){ |
- | |
173 | * value = strtol( argv[ * index ] + offset, & rest, 10 ); |
- | |
174 | }else if(( * index ) + 1 < argc ){ |
- | |
175 | ++ ( * index ); |
- | |
176 | * value = strtol( argv[ * index ], & rest, 10 ); |
- | |
177 | }else{ |
- | |
178 | fprintf( stderr, "Command line error: missing %s\n", name ); |
- | |
179 | return EINVAL; |
- | |
180 | } |
- | |
181 | if( rest && ( * rest )){ |
- | |
182 | fprintf( stderr, "Command line error: %s unrecognized (%d: %s)\n", name, * index, argv[ * index ] ); |
- | |
183 | return EINVAL; |
- | |
184 | } |
- | |
185 | return EOK; |
- | |
186 | } |
- | |
187 | - | ||
188 | int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset ){ |
- | |
189 | if( offset ){ |
- | |
190 | * value = argv[ * index ] + offset; |
- | |
191 | }else if(( * index ) + 1 < argc ){ |
- | |
192 | ++ ( * index ); |
- | |
193 | * value = argv[ * index ]; |
- | |
194 | }else{ |
- | |
195 | fprintf( stderr, "Command line error: missing %s\n", name ); |
- | |
196 | return EINVAL; |
- | |
197 | } |
- | |
198 | return EOK; |
- | |
199 | } |
- | |
200 | - | ||
201 | int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value )){ |
- | |
202 | ERROR_DECLARE; |
- | |
203 | - | ||
204 | char * parameter; |
- | |
205 | - | ||
206 | ERROR_PROPAGATE( parse_parameter_string( argc, argv, index, & parameter, name, offset )); |
- | |
207 | * value = ( * parse_value )( parameter ); |
- | |
208 | if(( * value ) == ENOENT ){ |
- | |
209 | fprintf( stderr, "Command line error: unrecognized %s value (%d: %s)\n", name, * index, parameter ); |
- | |
210 | return ENOENT; |
- | |
211 | } |
- | |
212 | return EOK; |
- | |
213 | } |
- | |
214 | - | ||
215 | int parse_protocol_family( const char * name ){ |
110 | int parse_protocol_family( const char * name ){ |
216 | if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ |
111 | if( str_lcmp( name, "PF_INET", 7 ) == 0 ){ |
217 | return PF_INET; |
112 | return PF_INET; |
218 | } |
113 | } |
219 | return ENOENT; |
114 | return ENOENT; |
Line 224... | Line 119... | ||
224 | return SOCK_DGRAM; |
119 | return SOCK_DGRAM; |
225 | } |
120 | } |
226 | return ENOENT; |
121 | return ENOENT; |
227 | } |
122 | } |
228 | 123 | ||
229 | void print_unrecognized( int index, const char * parameter ){ |
- | |
230 | fprintf( stderr, "Command line error - unrecognized parameter (%d: %s)\n", index, parameter ); |
- | |
231 | print_help(); |
- | |
232 | } |
- | |
233 | - | ||
234 | int main( int argc, char * argv[] ){ |
124 | int main( int argc, char * argv[] ){ |
235 | ERROR_DECLARE; |
125 | ERROR_DECLARE; |
236 | 126 | ||
237 | size_t size = 1024; |
127 | size_t size = 1024; |
238 | int verbose = 0; |
128 | int verbose = 0; |
239 | char * reply = NULL; |
129 | char * reply = NULL; |
240 | sock_type_t type = SOCK_DGRAM; |
130 | sock_type_t type = SOCK_DGRAM; |
241 | int count = -1; |
131 | int count = -1; |
242 | struct sockaddr_in address = { .sin_family = PF_INET, .sin_port = 7 }; |
132 | struct sockaddr_in address = { .sin_family = AF_INET, .sin_port = 7 }; |
- | 133 | int family = PF_INET; |
|
243 | 134 | ||
244 | int socket_id; |
135 | int socket_id; |
245 | int address_length; |
136 | int address_length; |
246 | char address_string[ INET_ADDRSTRLEN ]; |
137 | char address_string[ INET_ADDRSTRLEN ]; |
247 | char * data; |
138 | char * data; |
Line 257... | Line 148... | ||
257 | if( argv[ index ][ 0 ] == '-' ){ |
148 | if( argv[ index ][ 0 ] == '-' ){ |
258 | switch( argv[ index ][ 1 ] ){ |
149 | switch( argv[ index ][ 1 ] ){ |
259 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 )); |
150 | case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 )); |
260 | break; |
151 | break; |
261 | case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family )); |
152 | case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family )); |
- | 153 | family = value; |
|
- | 154 | switch( family ){ |
|
- | 155 | case PF_INET: |
|
262 | address.sin_family = ( uint16_t ) value; |
156 | address.sin_family = AF_INET; |
- | 157 | break; |
|
- | 158 | default: |
|
- | 159 | return ENOENT; |
|
- | 160 | } |
|
263 | break; |
161 | break; |
264 | case 'h': print_help(); |
162 | case 'h': print_help(); |
265 | return EOK; |
163 | return EOK; |
266 | break; |
164 | break; |
267 | case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); |
165 | case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 )); |
Line 279... | Line 177... | ||
279 | break; |
177 | break; |
280 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
178 | case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){ |
281 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 )) |
179 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 )) |
282 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
180 | }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){ |
283 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family )); |
181 | ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family )); |
- | 182 | family = value; |
|
- | 183 | switch( family ){ |
|
- | 184 | case PF_INET: |
|
284 | address.sin_family = ( uint16_t ) value; |
185 | address.sin_family = AF_INET; |
- | 186 | break; |
|
- | 187 | default: |
|
- | 188 | return ENOENT; |
|
- | 189 | } |
|
- | 190 | break; |
|
285 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
191 | }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){ |
286 | print_help(); |
192 | print_help(); |
287 | return EOK; |
193 | return EOK; |
288 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
194 | }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){ |
289 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
195 | ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 )); |
Line 298... | Line 204... | ||
298 | type = ( sock_type_t ) value; |
204 | type = ( sock_type_t ) value; |
299 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
205 | }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){ |
300 | verbose = 1; |
206 | verbose = 1; |
301 | }else{ |
207 | }else{ |
302 | print_unrecognized( index, argv[ index ] + 2 ); |
208 | print_unrecognized( index, argv[ index ] + 2 ); |
- | 209 | print_help(); |
|
303 | return EINVAL; |
210 | return EINVAL; |
304 | } |
211 | } |
305 | break; |
212 | break; |
306 | default: |
213 | default: |
307 | print_unrecognized( index, argv[ index ] + 1 ); |
214 | print_unrecognized( index, argv[ index ] + 1 ); |
- | 215 | print_help(); |
|
308 | return EINVAL; |
216 | return EINVAL; |
309 | } |
217 | } |
310 | }else{ |
218 | }else{ |
311 | print_unrecognized( index, argv[ index ] ); |
219 | print_unrecognized( index, argv[ index ] ); |
- | 220 | print_help(); |
|
312 | return EINVAL; |
221 | return EINVAL; |
313 | } |
222 | } |
314 | } |
223 | } |
315 | 224 | ||
316 | if( size <= 0 ){ |
225 | if( size <= 0 ){ |
Line 323... | Line 232... | ||
323 | return ENOMEM; |
232 | return ENOMEM; |
324 | } |
233 | } |
325 | 234 | ||
326 | reply_length = reply ? str_length( reply ) : 0; |
235 | reply_length = reply ? str_length( reply ) : 0; |
327 | 236 | ||
328 | socket_id = socket( address.sin_family, type, 0 ); |
237 | socket_id = socket( family, type, 0 ); |
329 | if( socket_id < 0 ){ |
238 | if( socket_id < 0 ){ |
330 | fprintf( stderr, "Socket create error %d\n", socket_id ); |
239 | fprintf( stderr, "Socket create error %d\n", socket_id ); |
331 | return socket_id; |
240 | return socket_id; |
332 | } |
241 | } |
333 | if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){ |
242 | if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){ |