Changeset bfe6366 in mainline
- Timestamp:
- 2010-12-25T22:24:12Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f57aebc
- Parents:
- c5ad226
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netecho/netecho.c
rc5ad226 rbfe6366 64 64 65 65 static char *reply = NULL; 66 static size_t reply_length; 67 68 static char *data; 66 69 67 70 static void echo_print_help(void) … … 213 216 } 214 217 218 /** Echo one message (accept one connection and echo message). 219 * 220 * @param listening_id Listening socket. 221 * @return EOK on success or negative error code. 222 */ 223 static int netecho_socket_echo_message(int listening_id) 224 { 225 socklen_t max_length = sizeof(struct sockaddr_in6); 226 uint8_t address_data[max_length]; 227 struct sockaddr *address = (struct sockaddr *) address_data; 228 socklen_t addrlen; 229 int socket_id; 230 ssize_t rcv_size; 231 size_t length; 232 uint8_t *address_start; 233 struct sockaddr_in *address_in = (struct sockaddr_in *) address; 234 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address; 235 char address_string[INET6_ADDRSTRLEN]; 236 int rc; 237 238 addrlen = (socklen_t) max_length; 239 if (type == SOCK_STREAM) { 240 /* Accept a socket if the stream socket is used */ 241 socket_id = accept(listening_id, address, &addrlen); 242 if (socket_id <= 0) { 243 socket_print_error(stderr, socket_id, "Socket accept: ", "\n"); 244 } else { 245 if (verbose) 246 printf("Socket %d accepted\n", socket_id); 247 } 248 } else { 249 socket_id = listening_id; 250 } 251 252 /* if the datagram socket is used or the stream socked was accepted */ 253 if (socket_id > 0) { 254 255 /* Receive a message to echo */ 256 rcv_size = recvfrom(socket_id, data, size, 0, address, &addrlen); 257 if (rcv_size < 0) { 258 socket_print_error(stderr, rcv_size, "Socket receive: ", "\n"); 259 } else { 260 length = (size_t) rcv_size; 261 if (verbose) { 262 /* Print the header */ 263 264 /* Get the source port and prepare the address buffer */ 265 address_start = NULL; 266 switch (address->sa_family) { 267 case AF_INET: 268 port = ntohs(address_in->sin_port); 269 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 270 break; 271 case AF_INET6: 272 port = ntohs(address_in6->sin6_port); 273 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 274 break; 275 default: 276 fprintf(stderr, "Address family %u (%#x) is not supported.\n", 277 address->sa_family, address->sa_family); 278 } 279 280 /* Parse source address */ 281 if (address_start) { 282 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)); 283 if (rc != EOK) { 284 fprintf(stderr, "Received address error %d\n", rc); 285 } else { 286 data[length] = '\0'; 287 printf("Socket %d received %zu bytes from %s:%d\n%s\n", 288 socket_id, length, address_string, port, data); 289 } 290 } 291 } 292 293 /* Answer the request either with the static reply or the original data */ 294 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen); 295 if (rc != EOK) 296 socket_print_error(stderr, rc, "Socket send: ", "\n"); 297 } 298 299 /* Close accepted stream socket */ 300 if (type == SOCK_STREAM) { 301 rc = closesocket(socket_id); 302 if (rc != EOK) 303 socket_print_error(stderr, rc, "Close socket: ", "\n"); 304 } 305 306 } 307 308 return EOK; 309 } 310 311 215 312 int main(int argc, char *argv[]) 216 313 { … … 221 318 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address; 222 319 socklen_t addrlen; 223 char address_string[INET6_ADDRSTRLEN];224 uint8_t *address_start;225 int socket_id;226 320 int listening_id; 227 char *data;228 size_t length;229 321 int index; 230 size_t reply_length;231 ssize_t rcv_size;232 322 int rc; 233 323 … … 311 401 printf("Socket %d listenning at %d\n", listening_id, port); 312 402 313 socket_id = listening_id;314 315 403 /* 316 404 * do count times … … 318 406 */ 319 407 while (count) { 320 321 addrlen = max_length; 322 if (type == SOCK_STREAM) { 323 /* Accept a socket if the stream socket is used */ 324 socket_id = accept(listening_id, address, &addrlen); 325 if (socket_id <= 0) { 326 socket_print_error(stderr, socket_id, "Socket accept: ", "\n"); 327 } else { 328 if (verbose) 329 printf("Socket %d accepted\n", socket_id); 330 } 331 } 332 333 /* if the datagram socket is used or the stream socked was accepted */ 334 if (socket_id > 0) { 335 336 /* Receive a message to echo */ 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"); 340 } else { 341 length = (size_t) rcv_size; 342 if (verbose) { 343 /* Print the header */ 344 345 /* Get the source port and prepare the address buffer */ 346 address_start = NULL; 347 switch (address->sa_family) { 348 case AF_INET: 349 port = ntohs(address_in->sin_port); 350 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 351 break; 352 case AF_INET6: 353 port = ntohs(address_in6->sin6_port); 354 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 355 break; 356 default: 357 fprintf(stderr, "Address family %u (%#x) is not supported.\n", 358 address->sa_family, address->sa_family); 359 } 360 361 /* Parse source address */ 362 if (address_start) { 363 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)); 364 if (rc != EOK) { 365 fprintf(stderr, "Received address error %d\n", rc); 366 } else { 367 data[length] = '\0'; 368 printf("Socket %d received %zu bytes from %s:%d\n%s\n", 369 socket_id, length, address_string, port, data); 370 } 371 } 372 } 373 374 /* Answer the request either with the static reply or the original data */ 375 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen); 376 if (rc != EOK) 377 socket_print_error(stderr, rc, "Socket send: ", "\n"); 378 } 379 380 /* Close accepted stream socket */ 381 if (type == SOCK_STREAM) { 382 rc = closesocket(socket_id); 383 if (rc != EOK) 384 socket_print_error(stderr, rc, "Close socket: ", "\n"); 385 } 386 387 } 408 rc = netecho_socket_echo_message(listening_id); 409 if (rc != EOK) 410 break; 388 411 389 412 /* Decrease count if positive */
Note:
See TracChangeset
for help on using the changeset viewer.