2021-03-02 23:41:49 +00:00
|
|
|
/* $Id: receivedata.c,v 1.10 2021/03/02 23:33:07 nanard Exp $ */
|
2011-09-27 20:25:35 +00:00
|
|
|
/* Project : miniupnp
|
2012-01-21 13:38:38 +00:00
|
|
|
* Website : http://miniupnp.free.fr/
|
2011-09-27 20:25:35 +00:00
|
|
|
* Author : Thomas Bernard
|
2021-03-02 23:41:49 +00:00
|
|
|
* Copyright (c) 2011-2021 Thomas Bernard
|
2011-09-27 20:25:35 +00:00
|
|
|
* This software is subject to the conditions detailed in the
|
|
|
|
* LICENCE file provided in this distribution. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-11-13 13:52:58 +00:00
|
|
|
#include <string.h>
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2014-11-13 13:52:58 +00:00
|
|
|
#else /* _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#if defined(__amigaos__) && !defined(__amigaos4__)
|
|
|
|
#define socklen_t int
|
|
|
|
#else /* #if defined(__amigaos__) && !defined(__amigaos4__) */
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */
|
|
|
|
#include <sys/socket.h>
|
2012-06-23 22:52:51 +00:00
|
|
|
#include <netinet/in.h>
|
2011-09-27 20:25:35 +00:00
|
|
|
#if !defined(__amigaos__) && !defined(__amigaos4__)
|
|
|
|
#include <poll.h>
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* !defined(__amigaos__) && !defined(__amigaos4__) */
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#define MINIUPNPC_IGNORE_EINTR
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
|
|
|
|
#include "receivedata.h"
|
|
|
|
|
|
|
|
int
|
2018-04-06 10:21:36 +00:00
|
|
|
receivedata(SOCKET socket,
|
2012-06-23 22:52:51 +00:00
|
|
|
char * data, int length,
|
|
|
|
int timeout, unsigned int * scope_id)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2015-11-17 09:08:34 +00:00
|
|
|
#ifdef MINIUPNPC_GET_SRC_ADDR
|
2012-06-23 22:52:51 +00:00
|
|
|
struct sockaddr_storage src_addr;
|
|
|
|
socklen_t src_addr_len = sizeof(src_addr);
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* MINIUPNPC_GET_SRC_ADDR */
|
2011-09-27 20:25:35 +00:00
|
|
|
int n;
|
2012-01-21 13:38:38 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
|
2011-09-27 20:25:35 +00:00
|
|
|
/* using poll */
|
|
|
|
struct pollfd fds[1]; /* for the poll */
|
|
|
|
#ifdef MINIUPNPC_IGNORE_EINTR
|
|
|
|
do {
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* MINIUPNPC_IGNORE_EINTR */
|
2011-09-27 20:25:35 +00:00
|
|
|
fds[0].fd = socket;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
n = poll(fds, 1, timeout);
|
|
|
|
#ifdef MINIUPNPC_IGNORE_EINTR
|
|
|
|
} while(n < 0 && errno == EINTR);
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* MINIUPNPC_IGNORE_EINTR */
|
2011-09-27 20:25:35 +00:00
|
|
|
if(n < 0) {
|
|
|
|
PRINT_SOCKET_ERROR("poll");
|
|
|
|
return -1;
|
|
|
|
} else if(n == 0) {
|
|
|
|
/* timeout */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-13 13:52:58 +00:00
|
|
|
#else /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
|
2012-01-21 13:38:38 +00:00
|
|
|
/* using select under _WIN32 and amigaos */
|
2011-09-27 20:25:35 +00:00
|
|
|
fd_set socketSet;
|
|
|
|
TIMEVAL timeval;
|
|
|
|
FD_ZERO(&socketSet);
|
|
|
|
FD_SET(socket, &socketSet);
|
|
|
|
timeval.tv_sec = timeout / 1000;
|
|
|
|
timeval.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
|
|
|
|
if(n < 0) {
|
|
|
|
PRINT_SOCKET_ERROR("select");
|
|
|
|
return -1;
|
|
|
|
} else if(n == 0) {
|
|
|
|
return 0;
|
2012-03-01 01:51:24 +00:00
|
|
|
}
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
|
2015-11-17 09:08:34 +00:00
|
|
|
#ifdef MINIUPNPC_GET_SRC_ADDR
|
2014-11-13 13:53:50 +00:00
|
|
|
memset(&src_addr, 0, sizeof(src_addr));
|
2012-06-23 22:52:51 +00:00
|
|
|
n = recvfrom(socket, data, length, 0,
|
|
|
|
(struct sockaddr *)&src_addr, &src_addr_len);
|
2014-11-13 13:52:58 +00:00
|
|
|
#else /* MINIUPNPC_GET_SRC_ADDR */
|
2011-09-27 20:25:35 +00:00
|
|
|
n = recv(socket, data, length, 0);
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* MINIUPNPC_GET_SRC_ADDR */
|
2011-09-27 20:25:35 +00:00
|
|
|
if(n<0) {
|
|
|
|
PRINT_SOCKET_ERROR("recv");
|
|
|
|
}
|
2015-11-17 09:08:34 +00:00
|
|
|
#ifdef MINIUPNPC_GET_SRC_ADDR
|
2012-06-23 22:52:51 +00:00
|
|
|
if (src_addr.ss_family == AF_INET6) {
|
|
|
|
const struct sockaddr_in6 * src_addr6 = (struct sockaddr_in6 *)&src_addr;
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("scope_id=%u\n", src_addr6->sin6_scope_id);
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* DEBUG */
|
2012-06-23 22:52:51 +00:00
|
|
|
if(scope_id)
|
|
|
|
*scope_id = src_addr6->sin6_scope_id;
|
2021-01-27 12:10:49 +00:00
|
|
|
} else {
|
|
|
|
if(scope_id)
|
|
|
|
*scope_id = 0;
|
2012-06-23 22:52:51 +00:00
|
|
|
}
|
2021-01-27 12:10:49 +00:00
|
|
|
#else /* MINIUPNPC_GET_SRC_ADDR */
|
|
|
|
if(scope_id)
|
|
|
|
*scope_id = 0;
|
2014-11-13 13:52:58 +00:00
|
|
|
#endif /* MINIUPNPC_GET_SRC_ADDR */
|
2011-09-27 20:25:35 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|