diff options
Diffstat (limited to 'src/nat/miniupnp/miniwget.c')
-rw-r--r-- | src/nat/miniupnp/miniwget.c | 224 |
1 files changed, 0 insertions, 224 deletions
diff --git a/src/nat/miniupnp/miniwget.c b/src/nat/miniupnp/miniwget.c deleted file mode 100644 index 38d5610d8..000000000 --- a/src/nat/miniupnp/miniwget.c +++ /dev/null | |||
@@ -1,224 +0,0 @@ | |||
1 | /* $Id: miniwget.c,v 1.22 2009/02/28 10:36:35 nanard Exp $ */ | ||
2 | /* Project : miniupnp | ||
3 | * Author : Thomas Bernard | ||
4 | * Copyright (c) 2005 Thomas Bernard | ||
5 | * This software is subject to the conditions detailed in the | ||
6 | * LICENCE file provided in this distribution. | ||
7 | * */ | ||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | ||
11 | #include "miniupnpc.h" | ||
12 | #ifdef WIN32 | ||
13 | #include <winsock2.h> | ||
14 | #include <io.h> | ||
15 | #define MAXHOSTNAMELEN 64 | ||
16 | #define MIN(x,y) (((x)<(y))?(x):(y)) | ||
17 | #define snprintf _snprintf | ||
18 | #define herror | ||
19 | #define socklen_t int | ||
20 | #else | ||
21 | #include <unistd.h> | ||
22 | #include <sys/param.h> | ||
23 | #include <sys/socket.h> | ||
24 | #include <netdb.h> | ||
25 | #include <netinet/in.h> | ||
26 | #include <arpa/inet.h> | ||
27 | #define closesocket close | ||
28 | #endif | ||
29 | #if defined(__sun) || defined(sun) | ||
30 | #define MIN(x,y) (((x)<(y))?(x):(y)) | ||
31 | #endif | ||
32 | |||
33 | #include "miniupnpcstrings.h" | ||
34 | |||
35 | /* miniwget2() : | ||
36 | * */ | ||
37 | static void * | ||
38 | miniwget2 (const char *url, const char *host, | ||
39 | unsigned short port, const char *path, | ||
40 | int *size, char *addr_str, int addr_str_len) | ||
41 | { | ||
42 | char buf[2048]; | ||
43 | int s; | ||
44 | struct sockaddr_in dest; | ||
45 | struct hostent *hp; | ||
46 | *size = 0; | ||
47 | hp = gethostbyname (host); | ||
48 | if (hp == NULL) | ||
49 | { | ||
50 | herror (host); | ||
51 | return NULL; | ||
52 | } | ||
53 | /* memcpy((char *)&dest.sin_addr, hp->h_addr, hp->h_length); */ | ||
54 | memcpy (&dest.sin_addr, hp->h_addr, sizeof (dest.sin_addr)); | ||
55 | memset (dest.sin_zero, 0, sizeof (dest.sin_zero)); | ||
56 | s = socket (PF_INET, SOCK_STREAM, 0); | ||
57 | if (s < 0) | ||
58 | { | ||
59 | perror ("socket"); | ||
60 | return NULL; | ||
61 | } | ||
62 | dest.sin_family = AF_INET; | ||
63 | dest.sin_port = htons (port); | ||
64 | if (connect (s, (struct sockaddr *) &dest, sizeof (struct sockaddr_in)) < 0) | ||
65 | { | ||
66 | perror ("connect"); | ||
67 | closesocket (s); | ||
68 | return NULL; | ||
69 | } | ||
70 | |||
71 | /* get address for caller ! */ | ||
72 | if (addr_str) | ||
73 | { | ||
74 | struct sockaddr_in saddr; | ||
75 | socklen_t len; | ||
76 | |||
77 | len = sizeof (saddr); | ||
78 | getsockname (s, (struct sockaddr *) &saddr, &len); | ||
79 | #ifndef WIN32 | ||
80 | inet_ntop (AF_INET, &saddr.sin_addr, addr_str, addr_str_len); | ||
81 | #else | ||
82 | /* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD); | ||
83 | * But his function make a string with the port : nn.nn.nn.nn:port */ | ||
84 | /* if(WSAAddressToStringA((SOCKADDR *)&saddr, sizeof(saddr), | ||
85 | NULL, addr_str, (DWORD *)&addr_str_len)) | ||
86 | { | ||
87 | printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError()); | ||
88 | }*/ | ||
89 | strncpy (addr_str, inet_ntoa (saddr.sin_addr), addr_str_len); | ||
90 | #endif | ||
91 | #ifdef DEBUG | ||
92 | printf ("address miniwget : %s\n", addr_str); | ||
93 | #endif | ||
94 | } | ||
95 | |||
96 | snprintf (buf, sizeof (buf), | ||
97 | "GET %s HTTP/1.1\r\n" | ||
98 | "Host: %s:%d\r\n" | ||
99 | "Connection: Close\r\n" | ||
100 | "User-Agent: " OS_STRING ", UPnP/1.0, MiniUPnPc/" | ||
101 | MINIUPNPC_VERSION_STRING "\r\n" "\r\n", path, host, port); | ||
102 | /*write(s, buf, strlen(buf)); */ | ||
103 | send (s, buf, strlen (buf), 0); | ||
104 | { | ||
105 | int n, headers = 1; | ||
106 | char *respbuffer = NULL; | ||
107 | int allreadyread = 0; | ||
108 | /*while((n = recv(s, buf, 2048, 0)) > 0) */ | ||
109 | while ((n = ReceiveData (s, buf, 2048, 5000)) > 0) | ||
110 | { | ||
111 | if (headers) | ||
112 | { | ||
113 | int i = 0; | ||
114 | while (i < n - 3) | ||
115 | { | ||
116 | if (buf[i] == '\r' && buf[i + 1] == '\n' | ||
117 | && buf[i + 2] == '\r' && buf[i + 3] == '\n') | ||
118 | { | ||
119 | headers = 0; /* end */ | ||
120 | if (i < n - 4) | ||
121 | { | ||
122 | respbuffer = (char *) realloc ((void *) respbuffer, | ||
123 | allreadyread + (n - i - | ||
124 | 4)); | ||
125 | memcpy (respbuffer + allreadyread, buf + i + 4, | ||
126 | n - i - 4); | ||
127 | allreadyread += (n - i - 4); | ||
128 | } | ||
129 | break; | ||
130 | } | ||
131 | i++; | ||
132 | } | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | respbuffer = (char *) realloc ((void *) respbuffer, | ||
137 | allreadyread + n); | ||
138 | memcpy (respbuffer + allreadyread, buf, n); | ||
139 | allreadyread += n; | ||
140 | } | ||
141 | } | ||
142 | *size = allreadyread; | ||
143 | #ifdef DEBUG | ||
144 | printf ("%d bytes read\n", *size); | ||
145 | #endif | ||
146 | closesocket (s); | ||
147 | return respbuffer; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | /* parseURL() | ||
152 | * arguments : | ||
153 | * url : source string not modified | ||
154 | * hostname : hostname destination string (size of MAXHOSTNAMELEN+1) | ||
155 | * port : port (destination) | ||
156 | * path : pointer to the path part of the URL | ||
157 | * | ||
158 | * Return values : | ||
159 | * 0 - Failure | ||
160 | * 1 - Success */ | ||
161 | int | ||
162 | parseURL (const char *url, char *hostname, unsigned short *port, char **path) | ||
163 | { | ||
164 | char *p1, *p2, *p3; | ||
165 | p1 = strstr (url, "://"); | ||
166 | if (!p1) | ||
167 | return 0; | ||
168 | p1 += 3; | ||
169 | if ((url[0] != 'h') || (url[1] != 't') | ||
170 | || (url[2] != 't') || (url[3] != 'p')) | ||
171 | return 0; | ||
172 | p2 = strchr (p1, ':'); | ||
173 | p3 = strchr (p1, '/'); | ||
174 | if (!p3) | ||
175 | return 0; | ||
176 | memset (hostname, 0, MAXHOSTNAMELEN + 1); | ||
177 | if (!p2 || (p2 > p3)) | ||
178 | { | ||
179 | strncpy (hostname, p1, MIN (MAXHOSTNAMELEN, (int) (p3 - p1))); | ||
180 | *port = 80; | ||
181 | } | ||
182 | else | ||
183 | { | ||
184 | strncpy (hostname, p1, MIN (MAXHOSTNAMELEN, (int) (p2 - p1))); | ||
185 | *port = 0; | ||
186 | p2++; | ||
187 | while ((*p2 >= '0') && (*p2 <= '9')) | ||
188 | { | ||
189 | *port *= 10; | ||
190 | *port += (unsigned short) (*p2 - '0'); | ||
191 | p2++; | ||
192 | } | ||
193 | } | ||
194 | *path = p3; | ||
195 | return 1; | ||
196 | } | ||
197 | |||
198 | void * | ||
199 | miniwget (const char *url, int *size) | ||
200 | { | ||
201 | unsigned short port; | ||
202 | char *path; | ||
203 | /* protocol://host:port/chemin */ | ||
204 | char hostname[MAXHOSTNAMELEN + 1]; | ||
205 | *size = 0; | ||
206 | if (!parseURL (url, hostname, &port, &path)) | ||
207 | return NULL; | ||
208 | return miniwget2 (url, hostname, port, path, size, 0, 0); | ||
209 | } | ||
210 | |||
211 | void * | ||
212 | miniwget_getaddr (const char *url, int *size, char *addr, int addrlen) | ||
213 | { | ||
214 | unsigned short port; | ||
215 | char *path; | ||
216 | /* protocol://host:port/chemin */ | ||
217 | char hostname[MAXHOSTNAMELEN + 1]; | ||
218 | *size = 0; | ||
219 | if (addr) | ||
220 | addr[0] = '\0'; | ||
221 | if (!parseURL (url, hostname, &port, &path)) | ||
222 | return NULL; | ||
223 | return miniwget2 (url, hostname, port, path, size, addr, addrlen); | ||
224 | } | ||