Rev 4743 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4743 | Rev 4756 | ||
|---|---|---|---|
| Line 54... | Line 54... | ||
| 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[in] argc The number of command line parameters. |
| 60 | * @param argv The command line parameters. Input parameter. |
60 | * @param[in] argv The command line parameters. |
| 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 echo_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[in] name The protocol family name. |
| 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 echo_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[in] name The socket type name. |
| 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 echo_parse_socket_type( const char * name ); |
| 82 | 82 | ||
| 83 | void print_help( void ){ |
83 | void echo_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" \ |
| Line 112... | Line 112... | ||
| 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 echo_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 echo_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 | } |
| Line 168... | Line 168... | ||
| 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, echo_parse_protocol_family )); |
| 174 | break; |
174 | break; |
| 175 | case 'h': print_help(); |
175 | case 'h': echo_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, echo_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, echo_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 | echo_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, echo_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 | echo_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 | echo_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 | echo_print_help(); |
| 227 | return EINVAL; |
227 | return EINVAL; |
| 228 | } |
228 | } |
| 229 | } |
229 | } |
| 230 | 230 | ||
| 231 | if( size <= 0 ){ |
231 | if( size <= 0 ){ |