version 1.1.1.1, 2012/02/21 23:16:22
|
version 1.1.1.2, 2013/07/22 00:36:10
|
Line 1
|
Line 1
|
/* $Id$ */ |
/* $Id$ */ |
/* Project : miniupnp |
/* Project : miniupnp |
* Author : Thomas Bernard |
* Author : Thomas Bernard |
* Copyright (c) 2010-2011 Thomas Bernard | * Copyright (c) 2010-2012 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. */ |
|
|
Line 13
|
Line 13
|
|
|
#include <string.h> |
#include <string.h> |
#include <stdio.h> |
#include <stdio.h> |
#ifdef WIN32 | #ifdef _WIN32 |
#include <winsock2.h> |
#include <winsock2.h> |
#include <ws2tcpip.h> |
#include <ws2tcpip.h> |
#include <io.h> |
#include <io.h> |
Line 21
|
Line 21
|
#define snprintf _snprintf |
#define snprintf _snprintf |
#define herror |
#define herror |
#define socklen_t int |
#define socklen_t int |
#else /* #ifdef WIN32 */ | #else /* #ifdef _WIN32 */ |
#include <unistd.h> |
#include <unistd.h> |
#include <sys/param.h> |
#include <sys/param.h> |
#include <errno.h> |
#include <errno.h> |
#define closesocket close |
#define closesocket close |
#include <netdb.h> |
#include <netdb.h> |
|
#include <netinet/in.h> |
/* defining MINIUPNPC_IGNORE_EINTR enable the ignore of interruptions |
/* defining MINIUPNPC_IGNORE_EINTR enable the ignore of interruptions |
* during the connect() call */ |
* during the connect() call */ |
#define MINIUPNPC_IGNORE_EINTR |
#define MINIUPNPC_IGNORE_EINTR |
Line 34
|
Line 35
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
#endif /* #ifndef USE_GETHOSTBYNAME */ |
#endif /* #ifndef USE_GETHOSTBYNAME */ |
#endif /* #else WIN32 */ | #endif /* #else _WIN32 */ |
|
|
/* definition of PRINT_SOCKET_ERROR */ |
/* definition of PRINT_SOCKET_ERROR */ |
#ifdef WIN32 | #ifdef _WIN32 |
#define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError()); |
#define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError()); |
#else |
#else |
#define PRINT_SOCKET_ERROR(x) perror(x) |
#define PRINT_SOCKET_ERROR(x) perror(x) |
Line 52
|
Line 53
|
/* connecthostport() |
/* connecthostport() |
* return a socket connected (TCP) to the host and port |
* return a socket connected (TCP) to the host and port |
* or -1 in case of error */ |
* or -1 in case of error */ |
int connecthostport(const char * host, unsigned short port) | int connecthostport(const char * host, unsigned short port, |
| unsigned int scope_id) |
{ |
{ |
int s, n; |
int s, n; |
#ifdef USE_GETHOSTBYNAME |
#ifdef USE_GETHOSTBYNAME |
Line 67 int connecthostport(const char * host, unsigned short
|
Line 69 int connecthostport(const char * host, unsigned short
|
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT |
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT |
struct timeval timeout; |
struct timeval timeout; |
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */ |
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */ |
| |
#ifdef USE_GETHOSTBYNAME |
#ifdef USE_GETHOSTBYNAME |
hp = gethostbyname(host); |
hp = gethostbyname(host); |
if(hp == NULL) |
if(hp == NULL) |
Line 145 int connecthostport(const char * host, unsigned short
|
Line 147 int connecthostport(const char * host, unsigned short
|
if(host[0] == '[') |
if(host[0] == '[') |
{ |
{ |
/* literal ip v6 address */ |
/* literal ip v6 address */ |
int i; | int i, j; |
for(i = 0; host[i+1] && (host[i+1] != ']') && i < MAXHOSTNAMELEN; i++) | for(i = 0, j = 1; host[j] && (host[j] != ']') && i < MAXHOSTNAMELEN; i++, j++) |
{ |
{ |
tmp_host[i] = host[i+1]; | tmp_host[i] = host[j]; |
| if(0 == memcmp(host+j, "%25", 3)) /* %25 is just url encoding for '%' */ |
| j+=2; /* skip "25" */ |
} |
} |
tmp_host[i] = '\0'; |
tmp_host[i] = '\0'; |
} |
} |
Line 160 int connecthostport(const char * host, unsigned short
|
Line 164 int connecthostport(const char * host, unsigned short
|
n = getaddrinfo(tmp_host, port_str, &hints, &ai); |
n = getaddrinfo(tmp_host, port_str, &hints, &ai); |
if(n != 0) |
if(n != 0) |
{ |
{ |
#ifdef WIN32 | #ifdef _WIN32 |
fprintf(stderr, "getaddrinfo() error : %d\n", n); |
fprintf(stderr, "getaddrinfo() error : %d\n", n); |
#else |
#else |
fprintf(stderr, "getaddrinfo() error : %s\n", gai_strerror(n)); |
fprintf(stderr, "getaddrinfo() error : %s\n", gai_strerror(n)); |
Line 173 int connecthostport(const char * host, unsigned short
|
Line 177 int connecthostport(const char * host, unsigned short
|
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); |
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); |
if(s < 0) |
if(s < 0) |
continue; |
continue; |
|
if(p->ai_addr->sa_family == AF_INET6 && scope_id > 0) { |
|
struct sockaddr_in6 * addr6 = (struct sockaddr_in6 *)p->ai_addr; |
|
addr6->sin6_scope_id = scope_id; |
|
} |
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT |
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT |
/* setting a 3 seconds timeout for the connect() call */ |
/* setting a 3 seconds timeout for the connect() call */ |
timeout.tv_sec = 3; |
timeout.tv_sec = 3; |