Changeset 10578 for branches/1.9x/third-party/miniupnp/miniwget.c
- Timestamp:
- May 1, 2010, 4:21:41 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.9x/third-party/miniupnp/miniwget.c
r10285 r10578 1 /* $Id: miniwget.c,v 1.3 1 2009/12/04 11:29:19nanard Exp $ */1 /* $Id: miniwget.c,v 1.37 2010/04/12 20:39:42 nanard Exp $ */ 2 2 /* Project : miniupnp 3 3 * Author : Thomas Bernard 4 * Copyright (c) 2005-20 09Thomas Bernard4 * Copyright (c) 2005-2010 Thomas Bernard 5 5 * This software is subject to the conditions detailed in the 6 * LICENCE file provided in this distribution. 7 * */6 * LICENCE file provided in this distribution. */ 7 8 8 #include <stdio.h> 9 9 #include <stdlib.h> … … 12 12 #ifdef WIN32 13 13 #include <winsock2.h> 14 #include <ws2tcpip.h> 14 15 #include <io.h> 15 16 #define MAXHOSTNAMELEN 64 16 17 #define MIN(x,y) (((x)<(y))?(x):(y)) 17 18 #define snprintf _snprintf 18 #define herror19 19 #define socklen_t int 20 #else 20 #else /* #ifdef WIN32 */ 21 21 #include <unistd.h> 22 22 #include <sys/param.h> 23 23 #if defined(__amigaos__) && !defined(__amigaos4__) 24 24 #define socklen_t int 25 #else 25 #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */ 26 26 #include <sys/select.h> 27 #endif 27 #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */ 28 28 #include <sys/socket.h> 29 #include <arpa/inet.h> 29 30 #include <netdb.h> 30 #include <netinet/in.h>31 #include <arpa/inet.h>32 #include <errno.h>33 #include <time.h>34 31 #define closesocket close 32 /* defining MINIUPNPC_IGNORE_EINTR enable the ignore of interruptions 33 * during the connect() call */ 35 34 #define MINIUPNPC_IGNORE_EINTR 36 #endif 35 #endif /* #else WIN32 */ 37 36 #if defined(__sun) || defined(sun) 38 37 #define MIN(x,y) (((x)<(y))?(x):(y)) 39 38 #endif 40 #if defined(__amigaos__) || defined(__amigaos4__)41 #define herror(A) printf("%s\n", A)42 #endif43 39 44 40 #include "miniupnpcstrings.h" 45 41 #include "miniwget.h" 46 47 /* miniwget2() : 48 * */ 42 #include "connecthostport.h" 43 44 /* miniwget3() : 45 * do all the work. 46 * Return NULL if something failed. */ 49 47 static void * 50 miniwget 2(const char * url, const char * host,48 miniwget3(const char * url, const char * host, 51 49 unsigned short port, const char * path, 52 int * size, char * addr_str, int addr_str_len )50 int * size, char * addr_str, int addr_str_len, const char * httpversion) 53 51 { 54 52 char buf[2048]; 55 53 int s; 56 struct sockaddr_in dest;57 struct hostent *hp;58 54 int n; 59 55 int len; 60 56 int sent; 61 #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT 62 struct timeval timeout; 63 #endif 57 64 58 *size = 0; 65 hp = gethostbyname(host); 66 if(hp==NULL) 67 { 68 herror(host); 59 s = connecthostport(host, port); 60 if(s < 0) 69 61 return NULL; 70 }71 /* memcpy((char *)&dest.sin_addr, hp->h_addr, hp->h_length); */72 memcpy(&dest.sin_addr, hp->h_addr, sizeof(dest.sin_addr));73 memset(dest.sin_zero, 0, sizeof(dest.sin_zero));74 s = socket(PF_INET, SOCK_STREAM, 0);75 if(s < 0)76 {77 perror("socket");78 return NULL;79 }80 #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT81 /* setting a 3 seconds timeout for the connect() call */82 timeout.tv_sec = 3;83 timeout.tv_usec = 0;84 if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)85 {86 perror("setsockopt");87 }88 timeout.tv_sec = 3;89 timeout.tv_usec = 0;90 if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)91 {92 perror("setsockopt");93 }94 #endif95 dest.sin_family = AF_INET;96 dest.sin_port = htons(port);97 n = connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in));98 #ifdef MINIUPNPC_IGNORE_EINTR99 while(n < 0 && errno == EINTR)100 {101 socklen_t len;102 fd_set wset;103 int err;104 FD_ZERO(&wset);105 FD_SET(s, &wset);106 if((n = select(s + 1, NULL, &wset, NULL, NULL)) == -1 && errno == EINTR)107 continue;108 /*len = 0;*/109 /*n = getpeername(s, NULL, &len);*/110 len = sizeof(err);111 if(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {112 perror("getsockopt");113 closesocket(s);114 return NULL;115 }116 if(err != 0) {117 errno = err;118 n = -1;119 }120 }121 #endif122 if(n<0)123 {124 perror("connect");125 closesocket(s);126 return NULL;127 }128 62 129 63 /* get address for caller ! */ 130 64 if(addr_str) 131 65 { 132 struct sockaddr _insaddr;66 struct sockaddr saddr; 133 67 socklen_t saddrlen; 134 68 135 69 saddrlen = sizeof(saddr); 136 if(getsockname(s, (struct sockaddr *)&saddr, &saddrlen) < 0)70 if(getsockname(s, &saddr, &saddrlen) < 0) 137 71 { 138 72 perror("getsockname"); … … 140 74 else 141 75 { 142 #if defined( WIN32) || (defined(__amigaos__) && !defined(__amigaos4__))76 #if defined(__amigaos__) && !defined(__amigaos4__) 143 77 /* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD); 144 78 * But his function make a string with the port : nn.nn.nn.nn:port */ … … 148 82 printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError()); 149 83 }*/ 150 strncpy(addr_str, inet_ntoa( saddr.sin_addr), addr_str_len);84 strncpy(addr_str, inet_ntoa(((struct sockaddr_in *)&saddr)->sin_addr), addr_str_len); 151 85 #else 152 inet_ntop(AF_INET, &saddr.sin_addr, addr_str, addr_str_len); 86 /*inet_ntop(AF_INET, &saddr.sin_addr, addr_str, addr_str_len);*/ 87 n = getnameinfo(&saddr, saddrlen, 88 addr_str, addr_str_len, 89 NULL, 0, 90 NI_NUMERICHOST | NI_NUMERICSERV); 91 if(n != 0) { 92 #ifdef WIN32 93 fprintf(stderr, "getnameinfo() failed : %d\n", n); 94 #else 95 fprintf(stderr, "getnameinfo() failed : %s\n", gai_strerror(n)); 96 #endif 97 } 153 98 #endif 154 99 } … … 159 104 160 105 len = snprintf(buf, sizeof(buf), 161 "GET %s HTTP/ 1.0\r\n"106 "GET %s HTTP/%s\r\n" 162 107 "Host: %s:%d\r\n" 163 108 "Connection: Close\r\n" … … 165 110 166 111 "\r\n", 167 path, host, port);112 path, httpversion, host, port); 168 113 sent = 0; 169 114 /* sending the HTTP request */ … … 232 177 } 233 178 179 /* miniwget2() : 180 * Call miniwget3(); retry with HTTP/1.1 if 1.0 fails. */ 181 static void * 182 miniwget2(const char * url, const char * host, 183 unsigned short port, const char * path, 184 int * size, char * addr_str, int addr_str_len) 185 { 186 char * respbuffer; 187 188 respbuffer = miniwget3(url, host, port, path, size, addr_str, addr_str_len, "1.0"); 189 if (*size == 0) 190 { 191 #ifdef DEBUG 192 printf("Retrying with HTTP/1.1\n"); 193 #endif 194 free(respbuffer); 195 respbuffer = miniwget3(url, host, port, path, size, addr_str, addr_str_len, "1.1"); 196 } 197 return respbuffer; 198 } 199 200 201 202 234 203 /* parseURL() 235 204 * arguments :
Note: See TracChangeset
for help on using the changeset viewer.