| 1 |
/* mswindows.c -- Windows-specific support |
| 2 |
Copyright (C) 1996-2011, 2014-2015, 2018-2024 Free Software |
| 3 |
Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU Wget. |
| 6 |
|
| 7 |
GNU Wget is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
GNU Wget is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Wget. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Additional permission under GNU GPL version 3 section 7 |
| 21 |
|
| 22 |
If you modify this program, or any covered work, by linking or |
| 23 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 24 |
modified version of that library), containing parts covered by the |
| 25 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 26 |
grants you additional permission to convey the resulting work. |
| 27 |
Corresponding Source for a non-source form of such a combination |
| 28 |
shall include the source code for the parts of OpenSSL used as well |
| 29 |
as that of the covered work. */ |
| 30 |
|
| 31 |
#define INHIBIT_WRAP /* avoid wrapping of socket, bind, ... */ |
| 32 |
|
| 33 |
#include "wget.h" |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
|
| 42 |
#include "utils.h" |
| 43 |
#include "url.h" |
| 44 |
#include "exits.h" |
| 45 |
|
| 46 |
#ifndef ES_SYSTEM_REQUIRED |
| 47 |
#define ES_SYSTEM_REQUIRED 0x00000001 |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifndef ES_CONTINUOUS |
| 51 |
#define ES_CONTINUOUS 0x80000000 |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
/* Windows version of xsleep in utils.c. */ |
| 56 |
|
| 57 |
void |
| 58 |
xsleep (double seconds) |
| 59 |
{ |
| 60 |
#if defined(HAVE_USLEEP) && defined(HAVE_SLEEP) |
| 61 |
if (seconds >= 1) |
| 62 |
{ |
| 63 |
/* Explained in utils.c. */ |
| 64 |
sleep (seconds); |
| 65 |
seconds -= (long) seconds; |
| 66 |
} |
| 67 |
usleep (seconds * 1000000); |
| 68 |
#else /* not HAVE_USLEEP */ |
| 69 |
SleepEx ((DWORD) (seconds * 1000 + .5), FALSE); |
| 70 |
#endif /* not HAVE_USLEEP */ |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
windows_main (char **exec_name) |
| 75 |
{ |
| 76 |
char *p; |
| 77 |
|
| 78 |
/* Remove .EXE from filename if it has one. */ |
| 79 |
*exec_name = xstrdup (*exec_name); |
| 80 |
p = strrchr (*exec_name, '.'); |
| 81 |
if (p) |
| 82 |
*p = '\0'; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
ws_cleanup (void) |
| 87 |
{ |
| 88 |
xfree (exec_name); |
| 89 |
WSACleanup (); |
| 90 |
} |
| 91 |
|
| 92 |
#if defined(CTRLBREAK_BACKGND) || defined(CTRLC_BACKGND) |
| 93 |
static void |
| 94 |
ws_hangup (const char *reason) |
| 95 |
{ |
| 96 |
fprintf (stderr, _("Continuing in background.\n")); |
| 97 |
redirect_output (true, reason); |
| 98 |
|
| 99 |
/* Detach process from the current console. Under Windows 9x, if we |
| 100 |
were launched from a 16-bit process (which is usually the case; |
| 101 |
command.com is 16-bit) the parent process should resume right away. |
| 102 |
Under NT or if launched from a 32-process under 9x, this is a futile |
| 103 |
gesture as the parent will wait for us to terminate before resuming. */ |
| 104 |
FreeConsole (); |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
/* Construct the name for a named section (a.k.a. `file mapping') object. |
| 109 |
The returned string is dynamically allocated and needs to be xfree()'d. */ |
| 110 |
static char * |
| 111 |
make_section_name (DWORD pid) |
| 112 |
{ |
| 113 |
return aprintf ("gnu_wget_fake_fork_%lu", pid); |
| 114 |
} |
| 115 |
|
| 116 |
/* This structure is used to hold all the data that is exchanged between |
| 117 |
parent and child. */ |
| 118 |
struct fake_fork_info |
| 119 |
{ |
| 120 |
HANDLE event; |
| 121 |
bool logfile_changed; |
| 122 |
char lfilename[MAX_PATH + 1]; |
| 123 |
}; |
| 124 |
|
| 125 |
/* Determines if we are the child and if so performs the child logic. |
| 126 |
Return values: |
| 127 |
< 0 error |
| 128 |
0 parent |
| 129 |
> 0 child |
| 130 |
*/ |
| 131 |
static int |
| 132 |
fake_fork_child (void) |
| 133 |
{ |
| 134 |
HANDLE section, event; |
| 135 |
struct fake_fork_info *info; |
| 136 |
char *name; |
| 137 |
|
| 138 |
name = make_section_name (GetCurrentProcessId ()); |
| 139 |
section = OpenFileMapping (FILE_MAP_WRITE, FALSE, name); |
| 140 |
xfree (name); |
| 141 |
/* It seems that Windows 9x and NT set last-error inconsistently when |
| 142 |
OpenFileMapping() fails; so we assume it failed because the section |
| 143 |
object does not exist. */ |
| 144 |
if (!section) |
| 145 |
return 0; /* We are the parent. */ |
| 146 |
|
| 147 |
info = MapViewOfFile (section, FILE_MAP_WRITE, 0, 0, 0); |
| 148 |
if (!info) |
| 149 |
{ |
| 150 |
CloseHandle (section); |
| 151 |
return -1; |
| 152 |
} |
| 153 |
|
| 154 |
event = info->event; |
| 155 |
|
| 156 |
info->logfile_changed = false; |
| 157 |
if (!opt.lfilename && (!opt.quiet || opt.server_response)) |
| 158 |
{ |
| 159 |
/* See utils:fork_to_background for explanation. */ |
| 160 |
FILE *new_log_fp = unique_create (DEFAULT_LOGFILE, false, &opt.lfilename); |
| 161 |
if (new_log_fp) |
| 162 |
{ |
| 163 |
info->logfile_changed = true; |
| 164 |
snprintf (info->lfilename, sizeof (info->lfilename), "%s", |
| 165 |
opt.lfilename); |
| 166 |
fclose (new_log_fp); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
UnmapViewOfFile (info); |
| 171 |
CloseHandle (section); |
| 172 |
|
| 173 |
/* Inform the parent that we've done our part. */ |
| 174 |
if (!SetEvent (event)) |
| 175 |
return -1; |
| 176 |
|
| 177 |
CloseHandle (event); |
| 178 |
return 1; /* We are the child. */ |
| 179 |
} |
| 180 |
|
| 181 |
/* Windows doesn't support the fork() call; so we fake it by invoking |
| 182 |
another copy of Wget with the same arguments with which we were |
| 183 |
invoked. The child copy of Wget should perform the same initialization |
| 184 |
sequence as the parent; so we should have two processes that are |
| 185 |
essentially identical. We create a specially named section object that |
| 186 |
allows the child to distinguish itself from the parent and is used to |
| 187 |
exchange information between the two processes. We use an event object |
| 188 |
for synchronization. */ |
| 189 |
static void |
| 190 |
fake_fork (void) |
| 191 |
{ |
| 192 |
char exe[MAX_PATH + 1]; |
| 193 |
DWORD exe_len, le; |
| 194 |
SECURITY_ATTRIBUTES sa; |
| 195 |
HANDLE section, event, h[2]; |
| 196 |
STARTUPINFO si; |
| 197 |
PROCESS_INFORMATION pi; |
| 198 |
struct fake_fork_info *info; |
| 199 |
char *name; |
| 200 |
BOOL rv; |
| 201 |
|
| 202 |
section = pi.hProcess = pi.hThread = NULL; |
| 203 |
|
| 204 |
/* Get the fully qualified name of our executable. This is more reliable |
| 205 |
than using argv[0]. */ |
| 206 |
exe_len = GetModuleFileName (GetModuleHandle (NULL), exe, sizeof (exe)); |
| 207 |
if (!exe_len || (exe_len >= sizeof (exe))) |
| 208 |
return; |
| 209 |
|
| 210 |
sa.nLength = sizeof (sa); |
| 211 |
sa.lpSecurityDescriptor = NULL; |
| 212 |
sa.bInheritHandle = TRUE; |
| 213 |
|
| 214 |
/* Create an anonymous inheritable event object that starts out |
| 215 |
non-signaled. */ |
| 216 |
event = CreateEvent (&sa, FALSE, FALSE, NULL); |
| 217 |
if (!event) |
| 218 |
return; |
| 219 |
|
| 220 |
/* Create the child process detached form the current console and in a |
| 221 |
suspended state. */ |
| 222 |
xzero (si); |
| 223 |
si.cb = sizeof (si); |
| 224 |
rv = CreateProcess (exe, GetCommandLine (), NULL, NULL, TRUE, |
| 225 |
CREATE_SUSPENDED | DETACHED_PROCESS, |
| 226 |
NULL, NULL, &si, &pi); |
| 227 |
if (!rv) |
| 228 |
goto cleanup; |
| 229 |
|
| 230 |
/* Create a named section object with a name based on the process id of |
| 231 |
the child. */ |
| 232 |
name = make_section_name (pi.dwProcessId); |
| 233 |
section = |
| 234 |
CreateFileMapping (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, |
| 235 |
sizeof (struct fake_fork_info), name); |
| 236 |
le = GetLastError(); |
| 237 |
xfree (name); |
| 238 |
/* Fail if the section object already exists (should not happen). */ |
| 239 |
if (!section || (le == ERROR_ALREADY_EXISTS)) |
| 240 |
{ |
| 241 |
rv = FALSE; |
| 242 |
goto cleanup; |
| 243 |
} |
| 244 |
|
| 245 |
/* Copy the event handle into the section object. */ |
| 246 |
info = MapViewOfFile (section, FILE_MAP_WRITE, 0, 0, 0); |
| 247 |
if (!info) |
| 248 |
{ |
| 249 |
rv = FALSE; |
| 250 |
goto cleanup; |
| 251 |
} |
| 252 |
|
| 253 |
info->event = event; |
| 254 |
|
| 255 |
UnmapViewOfFile (info); |
| 256 |
|
| 257 |
/* Start the child process. */ |
| 258 |
rv = ResumeThread (pi.hThread); |
| 259 |
if (!rv) |
| 260 |
{ |
| 261 |
TerminateProcess (pi.hProcess, (DWORD) -1); |
| 262 |
goto cleanup; |
| 263 |
} |
| 264 |
|
| 265 |
/* Wait for the child to signal to us that it has done its part. If it |
| 266 |
terminates before signaling us it's an error. */ |
| 267 |
|
| 268 |
h[0] = event; |
| 269 |
h[1] = pi.hProcess; |
| 270 |
rv = WAIT_OBJECT_0 == WaitForMultipleObjects (2, h, FALSE, 5 * 60 * 1000); |
| 271 |
if (!rv) |
| 272 |
goto cleanup; |
| 273 |
|
| 274 |
info = MapViewOfFile (section, FILE_MAP_READ, 0, 0, 0); |
| 275 |
if (!info) |
| 276 |
{ |
| 277 |
rv = FALSE; |
| 278 |
goto cleanup; |
| 279 |
} |
| 280 |
|
| 281 |
/* Ensure string is properly terminated. */ |
| 282 |
if (info->logfile_changed && |
| 283 |
!memchr (info->lfilename, '\0', sizeof (info->lfilename))) |
| 284 |
{ |
| 285 |
rv = FALSE; |
| 286 |
goto cleanup; |
| 287 |
} |
| 288 |
|
| 289 |
printf (_("Continuing in background, pid %lu.\n"), pi.dwProcessId); |
| 290 |
if (info->logfile_changed) |
| 291 |
printf (_("Output will be written to %s.\n"), quote (info->lfilename)); |
| 292 |
|
| 293 |
UnmapViewOfFile (info); |
| 294 |
|
| 295 |
cleanup: |
| 296 |
|
| 297 |
if (event) |
| 298 |
CloseHandle (event); |
| 299 |
if (section) |
| 300 |
CloseHandle (section); |
| 301 |
if (pi.hThread) |
| 302 |
CloseHandle (pi.hThread); |
| 303 |
if (pi.hProcess) |
| 304 |
CloseHandle (pi.hProcess); |
| 305 |
|
| 306 |
/* We're the parent. If all is well, terminate. */ |
| 307 |
if (rv) |
| 308 |
exit (WGET_EXIT_SUCCESS); |
| 309 |
|
| 310 |
/* We failed, return. */ |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
/* |
| 315 |
* This is the corresponding Windows implementation of |
| 316 |
* set_fd_nonblocking() function in utils.c |
| 317 |
* Currently it is an empty no-op. Maybe, we will get to |
| 318 |
* implementing a nice solution one day |
| 319 |
*/ |
| 320 |
void |
| 321 |
set_fd_nonblocking(const int fd) |
| 322 |
{} |
| 323 |
|
| 324 |
/* This is the corresponding Windows implementation of the |
| 325 |
fork_to_background() function in utils.c. */ |
| 326 |
bool |
| 327 |
fork_to_background (void) |
| 328 |
{ |
| 329 |
int rv; |
| 330 |
|
| 331 |
rv = fake_fork_child (); |
| 332 |
if (rv < 0) |
| 333 |
{ |
| 334 |
fprintf (stderr, _("fake_fork_child() failed\n")); |
| 335 |
abort (); |
| 336 |
} |
| 337 |
else if (rv == 0) |
| 338 |
{ |
| 339 |
/* We're the parent. */ |
| 340 |
fake_fork (); |
| 341 |
/* If fake_fork() returns, it failed. */ |
| 342 |
fprintf (stderr, _("fake_fork() failed\n")); |
| 343 |
abort (); |
| 344 |
} |
| 345 |
/* If we get here, we're the child. */ |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
static BOOL WINAPI |
| 350 |
ws_handler (DWORD dwEvent) |
| 351 |
{ |
| 352 |
switch (dwEvent) |
| 353 |
{ |
| 354 |
#ifdef CTRLC_BACKGND |
| 355 |
case CTRL_C_EVENT: |
| 356 |
ws_hangup ("CTRL+C"); |
| 357 |
return TRUE; |
| 358 |
#endif |
| 359 |
#ifdef CTRLBREAK_BACKGND |
| 360 |
case CTRL_BREAK_EVENT: |
| 361 |
ws_hangup ("CTRL+Break"); |
| 362 |
return TRUE; |
| 363 |
#endif |
| 364 |
default: |
| 365 |
return FALSE; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static char *title_buf = NULL; |
| 370 |
static char *curr_url = NULL; |
| 371 |
static int old_percentage = -1; |
| 372 |
|
| 373 |
/* Updates the console title with the URL of the current file being |
| 374 |
transferred. */ |
| 375 |
void |
| 376 |
ws_changetitle (const char *url) |
| 377 |
{ |
| 378 |
xfree (title_buf); |
| 379 |
xfree (curr_url); |
| 380 |
title_buf = xmalloc (strlen (url) + 20); |
| 381 |
curr_url = xstrdup (url); |
| 382 |
old_percentage = -1; |
| 383 |
sprintf (title_buf, "Wget %s", curr_url); |
| 384 |
SetConsoleTitle (title_buf); |
| 385 |
} |
| 386 |
|
| 387 |
/* Updates the console title with the percentage of the current file |
| 388 |
transferred. */ |
| 389 |
void |
| 390 |
ws_percenttitle (double percentage_float) |
| 391 |
{ |
| 392 |
int percentage; |
| 393 |
|
| 394 |
if (!title_buf || !curr_url) |
| 395 |
return; |
| 396 |
|
| 397 |
percentage = (int) percentage_float; |
| 398 |
|
| 399 |
/* Clamp percentage value. */ |
| 400 |
if (percentage < 0) |
| 401 |
percentage = 0; |
| 402 |
if (percentage > 100) |
| 403 |
percentage = 100; |
| 404 |
|
| 405 |
/* Only update the title when the percentage has changed. */ |
| 406 |
if (percentage == old_percentage) |
| 407 |
return; |
| 408 |
|
| 409 |
old_percentage = percentage; |
| 410 |
|
| 411 |
sprintf (title_buf, "Wget [%d%%] %s", percentage, curr_url); |
| 412 |
SetConsoleTitle (title_buf); |
| 413 |
} |
| 414 |
|
| 415 |
/* Returns a pointer to the fully qualified name of the directory that |
| 416 |
contains the Wget binary (wget.exe). The returned path does not have a |
| 417 |
trailing path separator. Returns NULL on failure. */ |
| 418 |
char * |
| 419 |
ws_mypath (void) |
| 420 |
{ |
| 421 |
static char *wspathsave = NULL; |
| 422 |
|
| 423 |
if (!wspathsave) |
| 424 |
{ |
| 425 |
char buf[MAX_PATH + 1]; |
| 426 |
char *p; |
| 427 |
DWORD len; |
| 428 |
|
| 429 |
len = GetModuleFileName (GetModuleHandle (NULL), buf, sizeof (buf)); |
| 430 |
if (!len || (len >= sizeof (buf))) |
| 431 |
return NULL; |
| 432 |
|
| 433 |
p = strrchr (buf, PATH_SEPARATOR); |
| 434 |
if (!p) |
| 435 |
return NULL; |
| 436 |
|
| 437 |
*p = '\0'; |
| 438 |
wspathsave = xstrdup (buf); |
| 439 |
} |
| 440 |
|
| 441 |
return wspathsave; |
| 442 |
} |
| 443 |
|
| 444 |
/* Prevent Windows entering sleep/hibernation-mode while Wget is doing |
| 445 |
a lengthy transfer. Windows does not, by default, consider network |
| 446 |
activity in console-programs as activity! Works on Win-98/ME/2K |
| 447 |
and up. */ |
| 448 |
static void |
| 449 |
set_sleep_mode (void) |
| 450 |
{ |
| 451 |
typedef DWORD (WINAPI *func_t) (DWORD); |
| 452 |
func_t set_exec_state; |
| 453 |
|
| 454 |
set_exec_state = |
| 455 |
(func_t) GetProcAddress (GetModuleHandle ("KERNEL32.DLL"), |
| 456 |
"SetThreadExecutionState"); |
| 457 |
|
| 458 |
if (set_exec_state) |
| 459 |
set_exec_state (ES_SYSTEM_REQUIRED | ES_CONTINUOUS); |
| 460 |
} |
| 461 |
|
| 462 |
/* Perform Windows specific initialization. */ |
| 463 |
void |
| 464 |
ws_startup (void) |
| 465 |
{ |
| 466 |
WSADATA data; |
| 467 |
WORD requested = MAKEWORD (1, 1); |
| 468 |
int err = WSAStartup (requested, &data); |
| 469 |
if (err != 0) |
| 470 |
{ |
| 471 |
fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"), |
| 472 |
exec_name); |
| 473 |
exit (WGET_EXIT_GENERIC_ERROR); |
| 474 |
} |
| 475 |
|
| 476 |
if (data.wVersion < requested) |
| 477 |
{ |
| 478 |
fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"), |
| 479 |
exec_name); |
| 480 |
WSACleanup (); |
| 481 |
exit (WGET_EXIT_GENERIC_ERROR); |
| 482 |
} |
| 483 |
|
| 484 |
atexit (ws_cleanup); |
| 485 |
set_sleep_mode (); |
| 486 |
SetConsoleCtrlHandler (ws_handler, TRUE); |
| 487 |
} |
| 488 |
|
| 489 |
/* run_with_timeout Windows implementation. */ |
| 490 |
|
| 491 |
/* Stack size 0 uses default thread stack-size (reserve+commit). |
| 492 |
Determined by what's in the PE header. */ |
| 493 |
#define THREAD_STACK_SIZE 0 |
| 494 |
|
| 495 |
struct thread_data |
| 496 |
{ |
| 497 |
void (*fun) (void *); |
| 498 |
void *arg; |
| 499 |
DWORD ws_error; |
| 500 |
}; |
| 501 |
|
| 502 |
/* The callback that runs FUN(ARG) in a separate thread. This |
| 503 |
function exists for two reasons: a) to not require FUN to be |
| 504 |
declared WINAPI/__stdcall[1], and b) to retrieve Winsock errors, |
| 505 |
which are per-thread. The latter is useful when FUN calls Winsock |
| 506 |
functions, which is how run_with_timeout is used in Wget. |
| 507 |
|
| 508 |
[1] MSVC can use __fastcall globally (cl /Gr) and on Watcom this is |
| 509 |
the default (wcc386 -3r). */ |
| 510 |
|
| 511 |
static DWORD WINAPI |
| 512 |
thread_helper (void *arg) |
| 513 |
{ |
| 514 |
struct thread_data *td = (struct thread_data *) arg; |
| 515 |
|
| 516 |
/* Initialize Winsock error to what it was in the parent. That way |
| 517 |
the subsequent call to WSAGetLastError will return the same value |
| 518 |
if td->fun doesn't change Winsock error state. */ |
| 519 |
WSASetLastError (td->ws_error); |
| 520 |
|
| 521 |
td->fun (td->arg); |
| 522 |
|
| 523 |
/* Return Winsock error to the caller, in case FUN ran Winsock |
| 524 |
code. */ |
| 525 |
td->ws_error = WSAGetLastError (); |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
/* Call FUN(ARG), but don't allow it to run for more than TIMEOUT |
| 530 |
seconds. Returns true if the function was interrupted with a |
| 531 |
timeout, false otherwise. |
| 532 |
|
| 533 |
This works by running FUN in a separate thread and terminating the |
| 534 |
thread if it doesn't finish in the specified time. */ |
| 535 |
|
| 536 |
bool |
| 537 |
run_with_timeout (double seconds, void (*fun) (void *), void *arg) |
| 538 |
{ |
| 539 |
HANDLE thread_hnd; |
| 540 |
struct thread_data thread_arg; |
| 541 |
DWORD thread_id; |
| 542 |
bool rc; |
| 543 |
|
| 544 |
DEBUGP (("seconds %.2f, ", seconds)); |
| 545 |
|
| 546 |
if (seconds == 0) |
| 547 |
{ |
| 548 |
blocking_fallback: |
| 549 |
fun (arg); |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
thread_arg.fun = fun; |
| 554 |
thread_arg.arg = arg; |
| 555 |
thread_arg.ws_error = WSAGetLastError (); |
| 556 |
thread_hnd = CreateThread (NULL, THREAD_STACK_SIZE, thread_helper, |
| 557 |
&thread_arg, 0, &thread_id); |
| 558 |
if (!thread_hnd) |
| 559 |
{ |
| 560 |
DEBUGP (("CreateThread() failed; [%#lx]\n", |
| 561 |
(unsigned long) GetLastError ())); |
| 562 |
goto blocking_fallback; |
| 563 |
} |
| 564 |
|
| 565 |
if (WaitForSingleObject (thread_hnd, (DWORD)(1000 * seconds)) |
| 566 |
== WAIT_OBJECT_0) |
| 567 |
{ |
| 568 |
/* Propagate error state (which is per-thread) to this thread, |
| 569 |
so the caller can inspect it. */ |
| 570 |
WSASetLastError (thread_arg.ws_error); |
| 571 |
DEBUGP (("Winsock error: %d\n", WSAGetLastError ())); |
| 572 |
rc = false; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
TerminateThread (thread_hnd, 1); |
| 577 |
rc = true; |
| 578 |
} |
| 579 |
|
| 580 |
CloseHandle (thread_hnd); /* Clear-up after TerminateThread(). */ |
| 581 |
thread_hnd = NULL; |
| 582 |
return rc; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
#ifdef ENABLE_IPV6 |
| 587 |
/* An inet_ntop implementation that uses WSAAddressToString. |
| 588 |
Prototype complies with POSIX 1003.1-2004. This is only used under |
| 589 |
IPv6 because Wget prints IPv4 addresses using inet_ntoa. */ |
| 590 |
|
| 591 |
const char * |
| 592 |
inet_ntop (int af, const void *src, char *dst, socklen_t cnt) |
| 593 |
{ |
| 594 |
/* struct sockaddr can't accommodate struct sockaddr_in6. */ |
| 595 |
union { |
| 596 |
struct sockaddr_in6 sin6; |
| 597 |
struct sockaddr_in sin; |
| 598 |
} sa; |
| 599 |
DWORD dstlen = cnt; |
| 600 |
size_t srcsize; |
| 601 |
|
| 602 |
xzero (sa); |
| 603 |
switch (af) |
| 604 |
{ |
| 605 |
case AF_INET: |
| 606 |
sa.sin.sin_family = AF_INET; |
| 607 |
sa.sin.sin_addr = *(struct in_addr *) src; |
| 608 |
srcsize = sizeof (sa.sin); |
| 609 |
break; |
| 610 |
case AF_INET6: |
| 611 |
sa.sin6.sin6_family = AF_INET6; |
| 612 |
sa.sin6.sin6_addr = *(struct in6_addr *) src; |
| 613 |
srcsize = sizeof (sa.sin6); |
| 614 |
break; |
| 615 |
default: |
| 616 |
abort (); |
| 617 |
} |
| 618 |
|
| 619 |
if (WSAAddressToString ((struct sockaddr *) &sa, srcsize, NULL, dst, &dstlen) != 0) |
| 620 |
{ |
| 621 |
errno = WSAGetLastError(); |
| 622 |
return NULL; |
| 623 |
} |
| 624 |
return (const char *) dst; |
| 625 |
} |
| 626 |
#endif |
| 627 |
|
| 628 |
|
| 629 |
void |
| 630 |
set_windows_fd_as_blocking_socket (int fd) |
| 631 |
{ |
| 632 |
/* 04/2011 |
| 633 |
gnulib select() converts blocking sockets to nonblocking in windows |
| 634 |
discussed here: |
| 635 |
http://old.nabble.com/blocking-socket-is-nonblocking-after-calling-gnulib- |
| 636 |
select%28%29-in-windows-td31432857.html |
| 637 |
|
| 638 |
wget uses blocking sockets so we must convert them back to blocking. |
| 639 |
*/ |
| 640 |
int ret = 0; |
| 641 |
int wsagle = 0; |
| 642 |
const int zero = 0; |
| 643 |
|
| 644 |
do |
| 645 |
{ |
| 646 |
if(wsagle == WSAEINPROGRESS) |
| 647 |
Sleep(1); /* use windows sleep */ |
| 648 |
|
| 649 |
WSASetLastError (0); |
| 650 |
ret = ioctl (fd, FIONBIO, &zero); |
| 651 |
wsagle = WSAGetLastError (); |
| 652 |
} |
| 653 |
while (ret && (wsagle == WSAEINPROGRESS)); |
| 654 |
|
| 655 |
if(ret) |
| 656 |
{ |
| 657 |
fprintf (stderr, |
| 658 |
_("ioctl() failed. The socket could not be set as blocking.\n") ); |
| 659 |
DEBUGP (("Winsock error: %d\n", WSAGetLastError ())); |
| 660 |
abort (); |
| 661 |
} |
| 662 |
return; |
| 663 |
} |