transmission 2.51 update
[tomato.git] / release / src / router / transmission / third-party / miniupnp / receivedata.c
blobd1bc87028122944a5232267f0b15f600e3a791f3
1 /* $Id: receivedata.c,v 1.3 2012/03/05 19:42:47 nanard Exp $ */
2 /* Project : miniupnp
3 * Website : http://miniupnp.free.fr/
4 * Author : Thomas Bernard
5 * Copyright (c) 2011-2012 Thomas Bernard
6 * This software is subject to the conditions detailed in the
7 * LICENCE file provided in this distribution. */
9 #include <stdio.h>
10 #ifdef _WIN32
11 #include <winsock2.h>
12 #include <ws2tcpip.h>
13 #else
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 #if !defined(__amigaos__) && !defined(__amigaos4__)
22 #include <poll.h>
23 #endif
24 #include <errno.h>
25 #define MINIUPNPC_IGNORE_EINTR
26 #endif
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
34 #include "receivedata.h"
36 int
37 receivedata(int socket, char * data, int length, int timeout)
39 int n;
40 #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
41 /* using poll */
42 struct pollfd fds[1]; /* for the poll */
43 #ifdef MINIUPNPC_IGNORE_EINTR
44 do {
45 #endif
46 fds[0].fd = socket;
47 fds[0].events = POLLIN;
48 n = poll(fds, 1, timeout);
49 #ifdef MINIUPNPC_IGNORE_EINTR
50 } while(n < 0 && errno == EINTR);
51 #endif
52 if(n < 0) {
53 PRINT_SOCKET_ERROR("poll");
54 return -1;
55 } else if(n == 0) {
56 /* timeout */
57 return 0;
59 #else /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
60 /* using select under _WIN32 and amigaos */
61 fd_set socketSet;
62 TIMEVAL timeval;
63 FD_ZERO(&socketSet);
64 FD_SET(socket, &socketSet);
65 timeval.tv_sec = timeout / 1000;
66 timeval.tv_usec = (timeout % 1000) * 1000;
67 n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
68 if(n < 0) {
69 PRINT_SOCKET_ERROR("select");
70 return -1;
71 } else if(n == 0) {
72 return 0;
74 #endif
75 n = recv(socket, data, length, 0);
76 if(n<0) {
77 PRINT_SOCKET_ERROR("recv");
79 return n;