/thirdparty/libportfwd/third-party/miniupnpc-1.6/receivedata.c

http://github.com/tomahawk-player/tomahawk · C · 81 lines · 66 code · 6 blank · 9 comment · 16 complexity · 2ef587faa05f988c674b2a5708030ea9 MD5 · raw file

  1. /* $Id: receivedata.c,v 1.1 2011/04/11 08:21:47 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2011 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution. */
  7. #include <stdio.h>
  8. #ifdef WIN32
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #else
  12. #include <unistd.h>
  13. #if defined(__amigaos__) && !defined(__amigaos4__)
  14. #define socklen_t int
  15. #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */
  16. #include <sys/select.h>
  17. #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */
  18. #include <sys/socket.h>
  19. #if !defined(__amigaos__) && !defined(__amigaos4__)
  20. #include <poll.h>
  21. #endif
  22. #include <errno.h>
  23. #define MINIUPNPC_IGNORE_EINTR
  24. #endif
  25. #ifdef WIN32
  26. #define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
  27. #else
  28. #define PRINT_SOCKET_ERROR(x) perror(x)
  29. #endif
  30. #include "receivedata.h"
  31. int
  32. receivedata(int socket, char * data, int length, int timeout)
  33. {
  34. int n;
  35. #if !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
  36. /* using poll */
  37. struct pollfd fds[1]; /* for the poll */
  38. #ifdef MINIUPNPC_IGNORE_EINTR
  39. do {
  40. #endif
  41. fds[0].fd = socket;
  42. fds[0].events = POLLIN;
  43. n = poll(fds, 1, timeout);
  44. #ifdef MINIUPNPC_IGNORE_EINTR
  45. } while(n < 0 && errno == EINTR);
  46. #endif
  47. if(n < 0) {
  48. PRINT_SOCKET_ERROR("poll");
  49. return -1;
  50. } else if(n == 0) {
  51. /* timeout */
  52. return 0;
  53. }
  54. #else /* !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  55. /* using select under WIN32 and amigaos */
  56. fd_set socketSet;
  57. TIMEVAL timeval;
  58. FD_ZERO(&socketSet);
  59. FD_SET(socket, &socketSet);
  60. timeval.tv_sec = timeout / 1000;
  61. timeval.tv_usec = (timeout % 1000) * 1000;
  62. n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
  63. if(n < 0) {
  64. PRINT_SOCKET_ERROR("select");
  65. return -1;
  66. } else if(n == 0) {
  67. return 0;
  68. }
  69. #endif
  70. n = recv(socket, data, length, 0);
  71. if(n<0) {
  72. PRINT_SOCKET_ERROR("recv");
  73. }
  74. return n;
  75. }