receivedata.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* $Id: receivedata.c,v 1.6 2014/11/13 13:51:52 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. #ifdef _WIN32
  29. #define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
  30. #else
  31. #define PRINT_SOCKET_ERROR(x) perror(x)
  32. #endif
  33. #include "receivedata.h"
  34. int
  35. receivedata(int socket,
  36. char * data, int length,
  37. int timeout, unsigned int * scope_id)
  38. {
  39. #if MINIUPNPC_GET_SRC_ADDR
  40. struct sockaddr_storage src_addr;
  41. socklen_t src_addr_len = sizeof(src_addr);
  42. #endif /* MINIUPNPC_GET_SRC_ADDR */
  43. int n;
  44. #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
  45. /* using poll */
  46. struct pollfd fds[1]; /* for the poll */
  47. #ifdef MINIUPNPC_IGNORE_EINTR
  48. do {
  49. #endif /* MINIUPNPC_IGNORE_EINTR */
  50. fds[0].fd = socket;
  51. fds[0].events = POLLIN;
  52. n = poll(fds, 1, timeout);
  53. #ifdef MINIUPNPC_IGNORE_EINTR
  54. } while(n < 0 && errno == EINTR);
  55. #endif /* MINIUPNPC_IGNORE_EINTR */
  56. if(n < 0) {
  57. PRINT_SOCKET_ERROR("poll");
  58. return -1;
  59. } else if(n == 0) {
  60. /* timeout */
  61. return 0;
  62. }
  63. #else /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  64. /* using select under _WIN32 and amigaos */
  65. fd_set socketSet;
  66. TIMEVAL timeval;
  67. FD_ZERO(&socketSet);
  68. FD_SET(socket, &socketSet);
  69. timeval.tv_sec = timeout / 1000;
  70. timeval.tv_usec = (timeout % 1000) * 1000;
  71. n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
  72. if(n < 0) {
  73. PRINT_SOCKET_ERROR("select");
  74. return -1;
  75. } else if(n == 0) {
  76. return 0;
  77. }
  78. #endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  79. #if MINIUPNPC_GET_SRC_ADDR
  80. memset(&src_addr, 0, sizeof(src_addr));
  81. n = recvfrom(socket, data, length, 0,
  82. (struct sockaddr *)&src_addr, &src_addr_len);
  83. #else /* MINIUPNPC_GET_SRC_ADDR */
  84. n = recv(socket, data, length, 0);
  85. #endif /* MINIUPNPC_GET_SRC_ADDR */
  86. if(n<0) {
  87. PRINT_SOCKET_ERROR("recv");
  88. }
  89. #if MINIUPNPC_GET_SRC_ADDR
  90. if (src_addr.ss_family == AF_INET6) {
  91. const struct sockaddr_in6 * src_addr6 = (struct sockaddr_in6 *)&src_addr;
  92. #ifdef DEBUG
  93. printf("scope_id=%u\n", src_addr6->sin6_scope_id);
  94. #endif /* DEBUG */
  95. if(scope_id)
  96. *scope_id = src_addr6->sin6_scope_id;
  97. }
  98. #endif /* MINIUPNPC_GET_SRC_ADDR */
  99. return n;
  100. }