Debian Codegraph @

src/net123_wininet.c

raw
1 #include "config.h"
2 #include "version.h"
3 #include "net123.h"
4 #include "compat/compat.h"
5 #include "common/debug.h"
6 #include <ws2tcpip.h>
7 #include <wininet.h>
8
9 // The network implementation defines the struct for private use.
10 // The purpose is just to keep enough context to be able to
11 // call net123_read() and net123_close() afterwards.
12 #define URL_COMPONENTS_LENGTH 255
13 typedef struct {
14 HINTERNET session;
15 HINTERNET connect;
16 HINTERNET request;
17 URL_COMPONENTSW comps;
18 wchar_t lpszHostName[URL_COMPONENTS_LENGTH];
19 wchar_t lpszUserName[URL_COMPONENTS_LENGTH];
20 wchar_t lpszPassword[URL_COMPONENTS_LENGTH];
21 wchar_t lpszUrlPath[URL_COMPONENTS_LENGTH];
22 wchar_t lpszExtraInfo[URL_COMPONENTS_LENGTH];
23 wchar_t lpszScheme[URL_COMPONENTS_LENGTH];
24 DWORD supportedAuth, firstAuth, authTarget, authTried;
25 char *headers;
26 size_t headers_pos, headers_len;
27 DWORD HttpQueryInfoIndex;
28 DWORD internetStatus, internetStatusLength;
29 LPVOID additionalInfo;
30 } wininet_handle;
31
32 #define MPG123CONCAT_(x,y) x ## y
33 #define MPG123CONCAT(x,y) MPG123CONCAT_(x,y)
34 #define MPG123STRINGIFY_(x) #x
35 #define MPG123STRINGIFY(x) MPG123STRINGIFY_(x)
36 #define MPG123WSTR(x) MPG123CONCAT(L,MPG123STRINGIFY(x))
37
38 #if DEBUG
39 static void debug_crack(URL_COMPONENTSW *comps){
40 wprintf(L"dwStructSize: %lu\n", comps->dwStructSize);
41 wprintf(L"lpszScheme: %s\n", comps->lpszScheme ? comps->lpszScheme : L"null");
42 wprintf(L"dwSchemeLength: %lu\n", comps->dwSchemeLength);
43 wprintf(L"nScheme: %u %s\n", comps->nScheme, comps->nScheme == 1 ? L"INTERNET_SCHEME_HTTP": comps->nScheme == 2 ? L"INTERNET_SCHEME_HTTPS" : L"UNKNOWN");
44 wprintf(L"lpszHostName: %s\n", comps->lpszHostName ? comps->lpszHostName : L"null");
45 wprintf(L"dwHostNameLength: %u\n", comps->dwHostNameLength);
46 wprintf(L"nPort: %u\n", comps->nPort);
47 wprintf(L"lpszUserName: %s\n", comps->lpszUserName ? comps->lpszUserName : L"null");
48 wprintf(L"dwUserNameLength: %lu\n", comps->dwUserNameLength);
49 wprintf(L"lpszPassword: %s\n", comps->lpszPassword ? comps->lpszPassword : L"null");
50 wprintf(L"dwPasswordLength: %lu\n", comps->dwPasswordLength);
51 wprintf(L"lpszUrlPath: %s\n", comps->lpszUrlPath ? comps->lpszUrlPath : L"null");
52 wprintf(L"dwUrlPathLength: %lu\n", comps->dwUrlPathLength);
53 wprintf(L"lpszExtraInfo: %s\n", comps->lpszExtraInfo? comps->lpszExtraInfo : L"null");
54 wprintf(L"dwExtraInfoLength: %lu\n", comps->dwExtraInfoLength);
55 }
56 #else
57 static void debug_crack(URL_COMPONENTSW *comps){}
58 #endif
59
60 static
61 void WINAPI net123_ssl_errors(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength){
62 debug("In net123_ssl_errors");
63 wininet_handle *nh = (wininet_handle *)(dwContext);
64 nh->internetStatus = dwInternetStatus;
65 nh->additionalInfo = lpvStatusInformation;
66 nh->internetStatusLength = dwStatusInformationLength;
67 }
68
69 static size_t net123_read(net123_handle *nh, void *buf, size_t bufsize);
70 static void net123_close(net123_handle *nh);
71
72 net123_handle *net123_open_wininet(const char *url, const char * const *client_head){
73 LPWSTR urlW = NULL, headers = NULL;
74 size_t ii;
75 WINBOOL res;
76 DWORD headerlen;
77 net123_handle *ret = NULL;
78 const LPCWSTR useragent = MPG123WSTR(PACKAGE_NAME) L"/" MPG123WSTR(MPG123_VERSION);
79 INTERNET_STATUS_CALLBACK cb;
80
81 INT123_win32_utf8_wide(url, &urlW, NULL);
82 if(urlW == NULL) goto cleanup;
83
84 ret = calloc(1, sizeof(net123_handle));
85 wininet_handle *wh = calloc(1, sizeof(wininet_handle));
86 if(!ret || !wh)
87 {
88 if(ret)
89 free(ret);
90 if(wh)
91 free(wh);
92 return NULL;
93 }
94
95 ret->parts = wh;
96 ret->read = net123_read;
97 ret->close = net123_close;
98
99 wh->comps.dwStructSize = sizeof(wh->comps);
100 wh->comps.dwSchemeLength = URL_COMPONENTS_LENGTH - 1;
101 wh->comps.dwUserNameLength = URL_COMPONENTS_LENGTH - 1;
102 wh->comps.dwPasswordLength = URL_COMPONENTS_LENGTH - 1;
103 wh->comps.dwHostNameLength = URL_COMPONENTS_LENGTH - 1;
104 wh->comps.dwUrlPathLength = URL_COMPONENTS_LENGTH - 1;
105 wh->comps.dwExtraInfoLength = URL_COMPONENTS_LENGTH - 1;
106 wh->comps.lpszHostName = wh->lpszHostName;
107 wh->comps.lpszUserName = wh->lpszUserName;
108 wh->comps.lpszPassword = wh->lpszPassword;
109 wh->comps.lpszUrlPath = wh->lpszUrlPath;
110 wh->comps.lpszExtraInfo = wh->lpszExtraInfo;
111 wh->comps.lpszScheme = wh->lpszScheme;
112
113 debug1("net123_open start crack %S", urlW);
114
115 if(!(res = InternetCrackUrlW(urlW, 0, 0, &wh->comps))) {
116 debug1("net123_open crack fail %lu", GetLastError());
117 goto cleanup;
118 }
119
120 debug("net123_open crack OK");
121 debug_crack(&wh->comps);
122
123 wh->session = InternetOpenW(useragent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
124 free(urlW);
125 urlW = NULL;
126 debug("net123_open InternetOpenW OK");
127 if(!wh->session) goto cleanup;
128
129 debug2("net123_open InternetConnectW %S %u", wh->comps.lpszHostName, wh->comps.nPort);
130 wh->connect = InternetConnectW(wh->session, wh->comps.lpszHostName, wh->comps.nPort,
131 wh->comps.dwUserNameLength ? wh->comps.lpszUserName : NULL, wh->comps.dwPasswordLength ? wh->comps.lpszPassword : NULL,
132 INTERNET_SERVICE_HTTP, 0, 0);
133 if(!wh->connect) goto cleanup;
134 debug("net123_open InternetConnectW OK");
135
136 debug1("HttpOpenRequestW GET %S", wh->comps.lpszUrlPath);
137 wh->request = HttpOpenRequestW(wh->connect, L"GET", wh->comps.lpszUrlPath, NULL, NULL, NULL, wh->comps.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)wh);
138 if(!wh->request) goto cleanup;
139 debug("HttpOpenRequestW GET OK");
140
141 cb = InternetSetStatusCallback(wh->request, (INTERNET_STATUS_CALLBACK)net123_ssl_errors);
142 if(cb != NULL){
143 error1("InternetSetStatusCallback failed to install callback, errors might not be reported properly! (%lu)", GetLastError());
144 }
145
146 for(ii = 0; client_head[ii]; ii++){
147 INT123_win32_utf8_wide(client_head[ii], &headers, NULL);
148 if(!headers)
149 goto cleanup;
150 debug1("HttpAddRequestHeadersW add %S", headers);
151 res = HttpAddRequestHeadersW(wh->request, headers, (DWORD) -1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
152 debug2("HttpAddRequestHeadersW returns %u %lu", res, res ? 0 : GetLastError());
153 free(headers);
154 headers = NULL;
155 }
156
157 debug("net123_open ADD HEADERS OK");
158
159 res = HttpSendRequestW(wh->request, NULL, 0, NULL, 0);
160
161 if (!res) {
162 res = GetLastError();
163 error1("HttpSendRequestW failed with %d", res);
164 goto cleanup;
165 }
166 debug("HttpSendRequestW OK");
167
168 // dummy, cannot be null
169 headers = calloc(1,1);
170 headerlen = 1;
171 if(headers == NULL) {
172 error("Cannot allocate dummy buffer for HttpQueryInfoW");
173 goto cleanup;
174 }
175 debug("Try HttpQueryInfoW pass 1");
176 res = HttpQueryInfoW(wh->request, HTTP_QUERY_RAW_HEADERS_CRLF, headers, &headerlen, &wh->HttpQueryInfoIndex);
177 free(headers);
178 debug("HttpQueryInfoW pass 1 OK");
179
180 if(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER && headerlen > 0) {
181 /* buffer size is in bytes, not including terminator */
182 headers = calloc(1, headerlen + sizeof(*headers));
183 if (!headers) goto cleanup;
184 res = HttpQueryInfoW(wh->request, HTTP_QUERY_RAW_HEADERS_CRLF, headers, &headerlen, &wh->HttpQueryInfoIndex);
185 debug3("HttpQueryInfoW returned %u, err %u : %S", res, GetLastError(), headers ? headers : L"null");
186 INT123_win32_wide_utf7(headers, &wh->headers, &wh->headers_len);
187 /* bytes written, skip the terminating null, we want to stop at the \r\n\r\n */
188 wh->headers_len --;
189 free(headers);
190 headers = NULL;
191 } else {
192 error("HttpQueryInfoW did not execute as expected");
193 goto cleanup;
194 }
195 debug("net123_open OK");
196
197 return ret;
198 cleanup:
199 debug("net123_open error");
200 if (urlW) free(urlW);
201 if(ret) {
202 net123_close(ret);
203 ret = NULL;
204 }
205 return ret;
206 }
207
208 static size_t net123_read(net123_handle *nh, void *buf, size_t bufsize){
209 if(!nh || !nh->parts)
210 return 0;
211 wininet_handle *wh = nh->parts;
212 size_t ret;
213 size_t to_copy = wh->headers_len - wh->headers_pos;
214 DWORD bytesread = 0;
215
216 if(to_copy){
217 ret = to_copy <= bufsize ? to_copy : bufsize;
218 memcpy(buf, wh->headers + wh->headers_pos, ret);
219 wh->headers_pos += ret;
220 return ret;
221 }
222
223 /* is this needed? */
224 to_copy = bufsize > ULONG_MAX ? ULONG_MAX : bufsize;
225 if(!InternetReadFile(wh->request, buf, to_copy, &bytesread)){
226 error1("InternetReadFile exited with %ld", GetLastError());
227 return EOF;
228 }
229 return bytesread;
230 }
231
232 // Call that to free up resources, end processes.
233 static void net123_close(net123_handle *nh){
234 if(!nh)
235 return;
236 wininet_handle *wh = nh->parts;
237 if(!wh) /*???*/
238 return;
239 if(wh->headers) {
240 free(wh->headers);
241 wh->headers = NULL;
242 }
243 if(wh->request) {
244 InternetCloseHandle(wh->request);
245 wh->request = NULL;
246 }
247 if(wh->connect) {
248 InternetCloseHandle(wh->connect);
249 wh->connect = NULL;
250 }
251 if(wh->session) {
252 InternetCloseHandle(wh->session);
253 wh->session = NULL;
254 }
255 free(nh);
256 free(wh);
257 }