receivedata.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* $Id: receivedata.c,v 1.7 2015/11/09 21:51:41 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Website : http://miniupnp.free.fr/
  4. * Author : Thomas Bernard
  5. * Copyright (c) 2011-2014 Thomas Bernard
  6. * This software is subject to the conditions detailed in the
  7. * LICENCE file provided in this distribution. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #ifdef _WIN32
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #else /* _WIN32 */
  14. #include <unistd.h>
  15. #if defined(__amigaos__) && !defined(__amigaos4__)
  16. #define socklen_t int
  17. #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */
  18. #include <sys/select.h>
  19. #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #if !defined(__amigaos__) && !defined(__amigaos4__)
  23. #include <poll.h>
  24. #endif /* !defined(__amigaos__) && !defined(__amigaos4__) */
  25. #include <errno.h>
  26. #define MINIUPNPC_IGNORE_EINTR
  27. #endif /* _WIN32 */
  28. #include "receivedata.h"
  29. int
  30. receivedata(SOCKET socket,
  31. char * data, int length,
  32. int timeout, unsigned int * scope_id)
  33. {
  34. #ifdef MINIUPNPC_GET_SRC_ADDR
  35. struct sockaddr_storage src_addr;
  36. socklen_t src_addr_len = sizeof(src_addr);
  37. #endif /* MINIUPNPC_GET_SRC_ADDR */
  38. int n;
  39. #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
  40. /* using poll */
  41. struct pollfd fds[1]; /* for the poll */
  42. #ifdef MINIUPNPC_IGNORE_EINTR
  43. do {
  44. #endif /* MINIUPNPC_IGNORE_EINTR */
  45. fds[0].fd = socket;
  46. fds[0].events = POLLIN;
  47. n = poll(fds, 1, timeout);
  48. #ifdef MINIUPNPC_IGNORE_EINTR
  49. } while(n < 0 && errno == EINTR);
  50. #endif /* MINIUPNPC_IGNORE_EINTR */
  51. if(n < 0) {
  52. PRINT_SOCKET_ERROR("poll");
  53. return -1;
  54. } else if(n == 0) {
  55. /* timeout */
  56. return 0;
  57. }
  58. #else /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  59. /* using select under _WIN32 and amigaos */
  60. fd_set socketSet;
  61. TIMEVAL timeval;
  62. FD_ZERO(&socketSet);
  63. FD_SET(socket, &socketSet);
  64. timeval.tv_sec = timeout / 1000;
  65. timeval.tv_usec = (timeout % 1000) * 1000;
  66. n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
  67. if(n < 0) {
  68. PRINT_SOCKET_ERROR("select");
  69. return -1;
  70. } else if(n == 0) {
  71. return 0;
  72. }
  73. #endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  74. #ifdef MINIUPNPC_GET_SRC_ADDR
  75. memset(&src_addr, 0, sizeof(src_addr));
  76. n = recvfrom(socket, data, length, 0,
  77. (struct sockaddr *)&src_addr, &src_addr_len);
  78. #else /* MINIUPNPC_GET_SRC_ADDR */
  79. n = recv(socket, data, length, 0);
  80. #endif /* MINIUPNPC_GET_SRC_ADDR */
  81. if(n<0) {
  82. PRINT_SOCKET_ERROR("recv");
  83. }
  84. #ifdef MINIUPNPC_GET_SRC_ADDR
  85. if (src_addr.ss_family == AF_INET6) {
  86. const struct sockaddr_in6 * src_addr6 = (struct sockaddr_in6 *)&src_addr;
  87. #ifdef DEBUG
  88. printf("scope_id=%u\n", src_addr6->sin6_scope_id);
  89. #endif /* DEBUG */
  90. if(scope_id)
  91. *scope_id = src_addr6->sin6_scope_id;
  92. }
  93. #endif /* MINIUPNPC_GET_SRC_ADDR */
  94. return n;
  95. }