1
/* $Id: miniupnpc.c,v 1.66 2009/10/10 19:15:34 nanard Exp $ */
1
/* $Id: miniupnpc.c,v 1.74 2010/01/09 23:54:40 nanard Exp $ */
2
2
/* Project : miniupnp
3
3
* Author : Thomas BERNARD
4
* copyright (c) 2005-2009 Thomas Bernard
4
* copyright (c) 2005-2010 Thomas Bernard
5
5
* This software is subjet to the conditions detailed in the
6
* provided LICENCE file. */
6
* provided LICENSE file. */
7
7
#define __EXTENSIONS__ 1
8
8
#if !defined(MACOSX) && !defined(__sun)
9
9
#if !defined(_XOPEN_SOURCE) && !defined(__OpenBSD__) && !defined(__NetBSD__)
24
28
#include <winsock2.h>
25
29
#include <ws2tcpip.h>
31
/*#include <IPHlpApi.h>*/
27
32
#define snprintf _snprintf
28
33
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
29
34
#define strncasecmp _memicmp
35
#else /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
31
36
#define strncasecmp memicmp
37
#endif /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
33
38
#define MAXHOSTNAMELEN 64
39
#else /* #ifdef WIN32 */
35
40
/* Standard POSIX includes */
36
41
#include <unistd.h>
42
#if defined(__amigaos__) && !defined(__amigaos4__)
43
/* Amiga OS 3 specific stuff */
37
46
#include <sys/select.h>
38
48
#include <sys/socket.h>
39
49
#include <sys/types.h>
40
50
#include <sys/param.h>
41
51
#include <netinet/in.h>
42
52
#include <arpa/inet.h>
53
#if !defined(__amigaos__) && !defined(__amigaos4__)
45
57
#include <strings.h>
47
59
#define closesocket close
48
60
#define MINIUPNPC_IGNORE_EINTR
61
#endif /* #else WIN32 */
50
62
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
51
63
#include <sys/time.h>
65
#if defined(__amigaos__) || defined(__amigaos4__)
66
/* Amiga OS specific stuff */
67
#define TIMEVAL struct timeval
53
70
#include "miniupnpc.h"
54
71
#include "minissdpc.h"
55
72
#include "miniwget.h"
88
/* Content-length: nnn */
105
/* getcontentlenfromline() : parse the Content-Length HTTP header line.
106
* Content-length: nnn */
89
107
static int getcontentlenfromline(const char * p, int n)
91
109
static const char contlenstr[] = "content-length";
141
/* getContentLengthAndHeaderLength()
142
* retrieve header length and content length from an HTTP response
143
* TODO : retrieve Transfer-Encoding: header value, in order to support
144
* HTTP/1.1, chunked transfer encoding must be supported. */
124
146
getContentLengthAndHeaderLength(char * p, int n,
125
147
int * contentlen, int * headerlen)
431
453
struct sockaddr_in sockudp_r, sockudp_w;
456
/*MIB_IPFORWARDROW ip_forward;*/
459
#if !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
435
460
/* first try to get infos from minissdpd ! */
436
461
if(!minissdpdsock)
437
462
minissdpdsock = "/var/run/minissdpd.sock";
467
492
sockudp_w.sin_family = AF_INET;
468
493
sockudp_w.sin_port = htons(PORT);
469
494
sockudp_w.sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR);
496
/* This code could help us to use the right Network interface for
497
* SSDP multicast traffic */
498
/* TODO : Get IP associated with the index given in the ip_forward struct
499
* in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */
501
if(GetBestRoute(inet_addr("223.255.255.255"), 0, &ip_forward) == NO_ERROR) {
503
PMIB_IPADDRTABLE pIPAddrTable;
507
printf("ifIndex=%lu nextHop=%lx \n", ip_forward.dwForwardIfIndex, ip_forward.dwForwardNextHop);
508
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE));
509
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
511
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
514
dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 );
515
printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries);
516
for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) {
517
printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex);
518
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr;
519
printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
520
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask;
521
printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
522
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr;
523
printf("\tBroadCast[%d]: \t%s (%ld)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);
524
printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize);
525
printf("\tType and State[%d]:", i);
472
536
if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof (opt)) < 0)
652
716
int ReceiveData(int socket, char * data, int length, int timeout)
719
#if !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
656
720
struct pollfd fds[1]; /* for the poll */
657
721
#ifdef MINIUPNPC_IGNORE_EINTR