Changeset c5ad226 in mainline
- Timestamp:
- 2010-12-25T22:05:34Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bfe6366
- Parents:
- af4f86f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netecho/netecho.c
raf4f86f rc5ad226 40 40 #include <malloc.h> 41 41 #include <stdio.h> 42 #include <stdlib.h> 42 43 #include <str.h> 43 44 #include <task.h> … … 53 54 54 55 #define NAME "netecho" 56 57 static int count = -1; 58 static int family = PF_INET; 59 static sock_type_t type = SOCK_DGRAM; 60 static uint16_t port = 7; 61 static int backlog = 3; 62 static size_t size = 1024; 63 static int verbose = 0; 64 65 static char *reply = NULL; 55 66 56 67 static void echo_print_help(void) … … 96 107 } 97 108 109 static int netecho_parse_option(int argc, char *argv[], int *index) 110 { 111 int value; 112 int rc; 113 114 switch (argv[*index][1]) { 115 case 'b': 116 rc = arg_parse_int(argc, argv, index, &backlog, 0); 117 if (rc != EOK) 118 return rc; 119 break; 120 case 'c': 121 rc = arg_parse_int(argc, argv, index, &count, 0); 122 if (rc != EOK) 123 return rc; 124 break; 125 case 'f': 126 rc = arg_parse_name_int(argc, argv, index, &family, 0, 127 socket_parse_protocol_family); 128 if (rc != EOK) 129 return rc; 130 break; 131 case 'h': 132 echo_print_help(); 133 exit(0); 134 break; 135 case 'p': 136 rc = arg_parse_int(argc, argv, index, &value, 0); 137 if (rc != EOK) 138 return rc; 139 port = (uint16_t) value; 140 break; 141 case 'r': 142 rc = arg_parse_string(argc, argv, index, &reply, 0); 143 if (rc != EOK) 144 return rc; 145 break; 146 case 's': 147 rc = arg_parse_int(argc, argv, index, &value, 0); 148 if (rc != EOK) 149 return rc; 150 size = (value >= 0) ? (size_t) value : 0; 151 break; 152 case 't': 153 rc = arg_parse_name_int(argc, argv, index, &value, 0, 154 socket_parse_socket_type); 155 if (rc != EOK) 156 return rc; 157 type = (sock_type_t) value; 158 break; 159 case 'v': 160 verbose = 1; 161 break; 162 /* Long options with double dash */ 163 case '-': 164 if (str_lcmp(argv[*index] + 2, "backlog=", 6) == 0) { 165 rc = arg_parse_int(argc, argv, index, &backlog, 8); 166 if (rc != EOK) 167 return rc; 168 } else if (str_lcmp(argv[*index] + 2, "count=", 6) == 0) { 169 rc = arg_parse_int(argc, argv, index, &count, 8); 170 if (rc != EOK) 171 return rc; 172 } else if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 173 rc = arg_parse_name_int(argc, argv, index, &family, 9, 174 socket_parse_protocol_family); 175 if (rc != EOK) 176 return rc; 177 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 178 echo_print_help(); 179 return EOK; 180 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { 181 rc = arg_parse_int(argc, argv, index, &value, 7); 182 if (rc != EOK) 183 return rc; 184 port = (uint16_t) value; 185 } else if (str_lcmp(argv[*index] + 2, "reply=", 6) == 0) { 186 rc = arg_parse_string(argc, argv, index, &reply, 8); 187 if (rc != EOK) 188 return rc; 189 } else if (str_lcmp(argv[*index] + 2, "size=", 5) == 0) { 190 rc = arg_parse_int(argc, argv, index, &value, 7); 191 if (rc != EOK) 192 return rc; 193 size = (value >= 0) ? (size_t) value : 0; 194 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { 195 rc = arg_parse_name_int(argc, argv, index, &value, 7, 196 socket_parse_socket_type); 197 if (rc != EOK) 198 return rc; 199 type = (sock_type_t) value; 200 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { 201 verbose = 1; 202 } else { 203 echo_print_help(); 204 return EINVAL; 205 } 206 break; 207 default: 208 echo_print_help(); 209 return EINVAL; 210 } 211 212 return EOK; 213 } 214 98 215 int main(int argc, char *argv[]) 99 216 { 100 size_t size = 1024;101 int verbose = 0;102 char *reply = NULL;103 sock_type_t type = SOCK_DGRAM;104 int count = -1;105 int family = PF_INET;106 uint16_t port = 7;107 int backlog = 3;108 109 217 socklen_t max_length = sizeof(struct sockaddr_in6); 110 218 uint8_t address_data[max_length]; … … 121 229 int index; 122 230 size_t reply_length; 123 int value;231 ssize_t rcv_size; 124 232 int rc; 125 233 … … 127 235 for (index = 1; index < argc; ++index) { 128 236 if (argv[index][0] == '-') { 129 switch (argv[index][1]) { 130 case 'b': 131 rc = arg_parse_int(argc, argv, &index, &backlog, 0); 132 if (rc != EOK) 133 return rc; 134 break; 135 case 'c': 136 rc = arg_parse_int(argc, argv, &index, &count, 0); 137 if (rc != EOK) 138 return rc; 139 break; 140 case 'f': 141 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 142 if (rc != EOK) 143 return rc; 144 break; 145 case 'h': 146 echo_print_help(); 147 return EOK; 148 break; 149 case 'p': 150 rc = arg_parse_int(argc, argv, &index, &value, 0); 151 if (rc != EOK) 152 return rc; 153 port = (uint16_t) value; 154 break; 155 case 'r': 156 rc = arg_parse_string(argc, argv, &index, &reply, 0); 157 if (rc != EOK) 158 return rc; 159 break; 160 case 's': 161 rc = arg_parse_int(argc, argv, &index, &value, 0); 162 if (rc != EOK) 163 return rc; 164 size = (value >= 0) ? (size_t) value : 0; 165 break; 166 case 't': 167 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 168 if (rc != EOK) 169 return rc; 170 type = (sock_type_t) value; 171 break; 172 case 'v': 173 verbose = 1; 174 break; 175 /* Long options with double dash */ 176 case '-': 177 if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) { 178 rc = arg_parse_int(argc, argv, &index, &backlog, 8); 179 if (rc != EOK) 180 return rc; 181 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) { 182 rc = arg_parse_int(argc, argv, &index, &count, 8); 183 if (rc != EOK) 184 return rc; 185 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 186 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 187 if (rc != EOK) 188 return rc; 189 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 190 echo_print_help(); 191 return EOK; 192 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 193 rc = arg_parse_int(argc, argv, &index, &value, 7); 194 if (rc != EOK) 195 return rc; 196 port = (uint16_t) value; 197 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 198 rc = arg_parse_string(argc, argv, &index, &reply, 8); 199 if (rc != EOK) 200 return rc; 201 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 202 rc = arg_parse_int(argc, argv, &index, &value, 7); 203 if (rc != EOK) 204 return rc; 205 size = (value >= 0) ? (size_t) value : 0; 206 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 207 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 208 if (rc != EOK) 209 return rc; 210 type = (sock_type_t) value; 211 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 212 verbose = 1; 213 } else { 214 echo_print_help(); 215 return EINVAL; 216 } 217 break; 218 default: 219 echo_print_help(); 220 return EINVAL; 221 } 237 rc = netecho_parse_option(argc, argv, &index); 238 if (rc != EOK) 239 return rc; 222 240 } else { 223 241 echo_print_help(); … … 317 335 318 336 /* Receive a message to echo */ 319 value = recvfrom(socket_id, data, size, 0, address, &addrlen);320 if ( value < 0) {321 socket_print_error(stderr, value, "Socket receive: ", "\n");337 rcv_size = recvfrom(socket_id, data, size, 0, address, &addrlen); 338 if (rcv_size < 0) { 339 socket_print_error(stderr, rcv_size, "Socket receive: ", "\n"); 322 340 } else { 323 length = (size_t) value;341 length = (size_t) rcv_size; 324 342 if (verbose) { 325 343 /* Print the header */
Note:
See TracChangeset
for help on using the changeset viewer.