1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-10 14:13:23 +00:00

(trunk third-party) update miniupnpc to the Aug 7 2009 snapshot

This commit is contained in:
Charles Kerr 2009-08-18 00:56:12 +00:00
parent 0730986c1c
commit 77ec5c12f2
4 changed files with 135 additions and 25 deletions

View file

@ -1,6 +1,15 @@
$Id: Changelog.txt,v 1.86 2009/07/29 08:44:29 nanard Exp $ $Id: Changelog.txt,v 1.90 2009/08/07 08:42:18 nanard Exp $
miniUPnP client Changelog. miniUPnP client Changelog.
2009/08/07:
Set socket timeout for connect()
Some cleanup in miniwget.c
2009/08/04:
remove multiple redirections with -d in upnpc.c
Print textual error code in upnpc.c
Ignore EINTR during the connect() and poll() calls.
2009/07/29: 2009/07/29:
fix in updateminiupnpcstrings.sh if OS name contains "/" fix in updateminiupnpcstrings.sh if OS name contains "/"
Sending a correct value for MX: field in SSDP request Sending a correct value for MX: field in SSDP request

View file

@ -1,11 +1,23 @@
/* $Id: miniupnpc.c,v 1.59 2009/07/29 08:44:29 nanard Exp $ */ /* $Id: miniupnpc.c,v 1.63 2009/08/07 14:44:50 nanard Exp $ */
/* Project : miniupnp /* Project : miniupnp
* Author : Thomas BERNARD * Author : Thomas BERNARD
* copyright (c) 2005-2007 Thomas Bernard * copyright (c) 2005-2009 Thomas Bernard
* This software is subjet to the conditions detailed in the * This software is subjet to the conditions detailed in the
* provided LICENCE file. */ * provided LICENCE file. */
#include <stdio.h> #define __EXTENSIONS__ 1
#ifndef MACOSX
#if !defined(_XOPEN_SOURCE) && !defined(__OpenBSD__) && !defined(__NetBSD__)
#ifndef __cplusplus
#define _XOPEN_SOURCE 600
#endif
#endif
#ifndef __BSD_VISIBLE
#define __BSD_VISIBLE 1
#endif
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#ifdef WIN32 #ifdef WIN32
/* Win32 Specific includes and defines */ /* Win32 Specific includes and defines */
@ -29,7 +41,13 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <poll.h> #include <poll.h>
#include <netdb.h> #include <netdb.h>
#include <strings.h>
#include <errno.h>
#define closesocket close #define closesocket close
#define MINIUPNPC_IGNORE_EINTR
#endif
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
#include <sys/time.h>
#endif #endif
#include "miniupnpc.h" #include "miniupnpc.h"
#include "minissdpc.h" #include "minissdpc.h"
@ -149,6 +167,9 @@ int simpleUPnPcommand(int s, const char * url, const char * service,
int buffree; int buffree;
int n; int n;
int contentlen, headerlen; /* for the response */ int contentlen, headerlen; /* for the response */
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
struct timeval timeout;
#endif
snprintf(soapact, sizeof(soapact), "%s#%s", service, action); snprintf(soapact, sizeof(soapact), "%s#%s", service, action);
if(args==NULL) if(args==NULL)
{ {
@ -224,11 +245,33 @@ int simpleUPnPcommand(int s, const char * url, const char * service,
*bufsize = 0; *bufsize = 0;
return -1; return -1;
} }
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
/* setting a 3 seconds timeout for the connect() call */
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
{
PRINT_SOCKET_ERROR("setsockopt");
}
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
{
PRINT_SOCKET_ERROR("setsockopt");
}
#endif
dest.sin_family = AF_INET; dest.sin_family = AF_INET;
dest.sin_port = htons(port); dest.sin_port = htons(port);
dest.sin_addr.s_addr = inet_addr(hostname); dest.sin_addr.s_addr = inet_addr(hostname);
if(connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr))<0) #ifdef MINIUPNPC_IGNORE_EINTR
{ do {
#endif
n = connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr));
#ifdef MINIUPNPC_IGNORE_EINTR
} while(n < 0 && errno == EINTR);
#endif
if(n < 0)
{
PRINT_SOCKET_ERROR("connect"); PRINT_SOCKET_ERROR("connect");
closesocket(s); closesocket(s);
*bufsize = 0; *bufsize = 0;
@ -590,9 +633,15 @@ int ReceiveData(int socket, char * data, int length, int timeout)
int n; int n;
#ifndef WIN32 #ifndef WIN32
struct pollfd fds[1]; /* for the poll */ struct pollfd fds[1]; /* for the poll */
fds[0].fd = socket; #ifdef MINIUPNPC_IGNORE_EINTR
fds[0].events = POLLIN; do {
n = poll(fds, 1, timeout); #endif
fds[0].fd = socket;
fds[0].events = POLLIN;
n = poll(fds, 1, timeout);
#ifdef MINIUPNPC_IGNORE_EINTR
} while(n < 0 && errno == EINTR);
#endif
if(n < 0) if(n < 0)
{ {
PRINT_SOCKET_ERROR("poll"); PRINT_SOCKET_ERROR("poll");
@ -609,7 +658,6 @@ int ReceiveData(int socket, char * data, int length, int timeout)
FD_SET(socket, &socketSet); FD_SET(socket, &socketSet);
timeval.tv_sec = timeout / 1000; timeval.tv_sec = timeout / 1000;
timeval.tv_usec = (timeout % 1000) * 1000; timeval.tv_usec = (timeout % 1000) * 1000;
/*n = select(0, &socketSet, NULL, NULL, &timeval);*/
n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval); n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
if(n < 0) if(n < 0)
{ {
@ -629,7 +677,7 @@ int ReceiveData(int socket, char * data, int length, int timeout)
return n; return n;
} }
int static int
UPNPIGD_IsConnected(struct UPNPUrls * urls, struct IGDdatas * data) UPNPIGD_IsConnected(struct UPNPUrls * urls, struct IGDdatas * data)
{ {
char status[64]; char status[64];

View file

@ -1,7 +1,7 @@
/* $Id: miniwget.c,v 1.22 2009/02/28 10:36:35 nanard Exp $ */ /* $Id: miniwget.c,v 1.25 2009/08/07 14:44:51 nanard Exp $ */
/* Project : miniupnp /* Project : miniupnp
* Author : Thomas Bernard * Author : Thomas Bernard
* Copyright (c) 2005 Thomas Bernard * Copyright (c) 2005-2009 Thomas Bernard
* This software is subject to the conditions detailed in the * This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution. * LICENCE file provided in this distribution.
* */ * */
@ -24,13 +24,16 @@
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h>
#define closesocket close #define closesocket close
#define MINIUPNPC_IGNORE_EINTR
#endif #endif
#if defined(__sun) || defined(sun) #if defined(__sun) || defined(sun)
#define MIN(x,y) (((x)<(y))?(x):(y)) #define MIN(x,y) (((x)<(y))?(x):(y))
#endif #endif
#include "miniupnpcstrings.h" #include "miniupnpcstrings.h"
#include "miniwget.h"
/* miniwget2() : /* miniwget2() :
* */ * */
@ -43,6 +46,12 @@ miniwget2(const char * url, const char * host,
int s; int s;
struct sockaddr_in dest; struct sockaddr_in dest;
struct hostent *hp; struct hostent *hp;
int n;
int len;
int sent;
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
struct timeval timeout;
#endif
*size = 0; *size = 0;
hp = gethostbyname(host); hp = gethostbyname(host);
if(hp==NULL) if(hp==NULL)
@ -59,9 +68,31 @@ miniwget2(const char * url, const char * host,
perror("socket"); perror("socket");
return NULL; return NULL;
} }
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
/* setting a 3 seconds timeout for the connect() call */
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
{
perror("setsockopt");
}
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
{
perror("setsockopt");
}
#endif
dest.sin_family = AF_INET; dest.sin_family = AF_INET;
dest.sin_port = htons(port); dest.sin_port = htons(port);
if(connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in))<0) #ifdef MINIUPNPC_IGNORE_EINTR
do {
#endif
n = connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in));
#ifdef MINIUPNPC_IGNORE_EINTR
} while(n < 0 && errno == EINTR);
#endif
if(n<0)
{ {
perror("connect"); perror("connect");
closesocket(s); closesocket(s);
@ -72,12 +103,17 @@ miniwget2(const char * url, const char * host,
if(addr_str) if(addr_str)
{ {
struct sockaddr_in saddr; struct sockaddr_in saddr;
socklen_t len; socklen_t saddrlen;
len = sizeof(saddr); saddrlen = sizeof(saddr);
getsockname(s, (struct sockaddr *)&saddr, &len); if(getsockname(s, (struct sockaddr *)&saddr, &saddrlen) < 0)
{
perror("getsockname");
}
else
{
#ifndef WIN32 #ifndef WIN32
inet_ntop(AF_INET, &saddr.sin_addr, addr_str, addr_str_len); inet_ntop(AF_INET, &saddr.sin_addr, addr_str, addr_str_len);
#else #else
/* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD); /* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD);
* But his function make a string with the port : nn.nn.nn.nn:port */ * But his function make a string with the port : nn.nn.nn.nn:port */
@ -86,14 +122,15 @@ miniwget2(const char * url, const char * host,
{ {
printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError()); printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError());
}*/ }*/
strncpy(addr_str, inet_ntoa(saddr.sin_addr), addr_str_len); strncpy(addr_str, inet_ntoa(saddr.sin_addr), addr_str_len);
#endif #endif
}
#ifdef DEBUG #ifdef DEBUG
printf("address miniwget : %s\n", addr_str); printf("address miniwget : %s\n", addr_str);
#endif #endif
} }
snprintf(buf, sizeof(buf), len = snprintf(buf, sizeof(buf),
"GET %s HTTP/1.1\r\n" "GET %s HTTP/1.1\r\n"
"Host: %s:%d\r\n" "Host: %s:%d\r\n"
"Connection: Close\r\n" "Connection: Close\r\n"
@ -101,10 +138,24 @@ miniwget2(const char * url, const char * host,
"\r\n", "\r\n",
path, host, port); path, host, port);
/*write(s, buf, strlen(buf));*/ sent = 0;
send(s, buf, strlen(buf), 0); /* sending the HTTP request */
while(sent < len)
{ {
int n, headers=1; n = send(s, buf+sent, len-sent, 0);
if(n < 0)
{
perror("send");
closesocket(s);
return NULL;
}
else
{
sent += n;
}
}
{
int headers=1;
char * respbuffer = NULL; char * respbuffer = NULL;
int allreadyread = 0; int allreadyread = 0;
/*while((n = recv(s, buf, 2048, 0)) > 0)*/ /*while((n = recv(s, buf, 2048, 0)) > 0)*/
@ -115,12 +166,14 @@ miniwget2(const char * url, const char * host,
int i=0; int i=0;
while(i<n-3) while(i<n-3)
{ {
/* searching for the end of the HTTP headers */
if(buf[i]=='\r' && buf[i+1]=='\n' if(buf[i]=='\r' && buf[i+1]=='\n'
&& buf[i+2]=='\r' && buf[i+3]=='\n') && buf[i+2]=='\r' && buf[i+3]=='\n')
{ {
headers = 0; /* end */ headers = 0; /* end */
if(i<n-4) if(i<n-4)
{ {
/* Copy the content into respbuffet */
respbuffer = (char *)realloc((void *)respbuffer, respbuffer = (char *)realloc((void *)respbuffer,
allreadyread+(n-i-4)); allreadyread+(n-i-4));
memcpy(respbuffer+allreadyread, buf + i + 4, n-i-4); memcpy(respbuffer+allreadyread, buf + i + 4, n-i-4);

View file

@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
/* parseatt : used to parse the argument list /* parseatt : used to parse the argument list
* return 0 (false) in case of success and -1 (true) if the end * return 0 (false) in case of success and -1 (true) if the end
* of the xmlbuffer is reached. */ * of the xmlbuffer is reached. */
int parseatt(struct xmlparser * p) static int parseatt(struct xmlparser * p)
{ {
const char * attname; const char * attname;
int attnamelen; int attnamelen;
@ -106,7 +106,7 @@ int parseatt(struct xmlparser * p)
/* parseelt parse the xml stream and /* parseelt parse the xml stream and
* call the callback functions when needed... */ * call the callback functions when needed... */
void parseelt(struct xmlparser * p) static void parseelt(struct xmlparser * p)
{ {
int i; int i;
const char * elementname; const char * elementname;