| 1 |
/* |
| 2 |
* $LynxId: HTTCP.c,v 1.173 2026/04/20 08:14:02 tom Exp $ |
| 3 |
* |
| 4 |
* Generic Communication Code HTTCP.c |
| 5 |
* ========================== |
| 6 |
* |
| 7 |
* This code is in common between client and server sides. |
| 8 |
* |
| 9 |
* 16 Jan 92 TBL Fix strtol() undefined on CMU Mach. |
| 10 |
* 25 Jun 92 JFG Added DECNET option through TCP socket emulation. |
| 11 |
* 13 Sep 93 MD Added correct return of vmserrorno for HTInetStatus. |
| 12 |
* Added decoding of vms error message for MULTINET. |
| 13 |
* 7-DEC-1993 Bjorn S. Nilsson, ALEPH, CERN, VMS UCX ioctl() changes |
| 14 |
* (done of Mosaic) |
| 15 |
* 19 Feb 94 Danny Mayer Added Bjorn Fixes to Lynx version |
| 16 |
* 7 Mar 94 Danny Mayer Added Fix UCX version for full domain name |
| 17 |
* 20 May 94 Andy Harper Added support for CMU TCP/IP transport |
| 18 |
* 17 Nov 94 Andy Harper Added support for SOCKETSHR transport |
| 19 |
* 16 Jul 95 S. Bjorndahl added kluge to deal with LIBCMU bug |
| 20 |
*/ |
| 21 |
|
| 22 |
#define LYNX_ADDRINFO struct addrinfo |
| 23 |
#define LYNX_HOSTENT struct hostent |
| 24 |
|
| 25 |
#include <HTUtils.h> |
| 26 |
#include <HTParse.h> |
| 27 |
#include <HTAlert.h> |
| 28 |
#include <HTTCP.h> |
| 29 |
#include <LYGlobalDefs.h> /* added for no_suspend */ |
| 30 |
#include <LYUtils.h> |
| 31 |
|
| 32 |
#ifdef NSL_FORK |
| 33 |
#include <signal.h> |
| 34 |
#include <www_wait.h> |
| 35 |
#define FREE_NSL_FORK(p) { FREE(p); } |
| 36 |
#elif defined(_WINDOWS_NSL) |
| 37 |
#define FREE_NSL_FORK(p) if ((p) == gbl_phost) { FREE(p); } |
| 38 |
#else |
| 39 |
#define FREE_NSL_FORK(p) /* nothing */ |
| 40 |
#endif /* NSL_FORK */ |
| 41 |
|
| 42 |
#ifdef HAVE_RESOLV_H |
| 43 |
#include <resolv.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef __DJGPP__ |
| 47 |
#include <netdb.h> |
| 48 |
#endif /* __DJGPP__ */ |
| 49 |
|
| 50 |
#define OK_HOST(p) ((p) != NULL && ((p)->h_length) != 0) |
| 51 |
|
| 52 |
#ifdef SVR4_BSDSELECT |
| 53 |
int BSDselect(int nfds, |
| 54 |
fd_set * readfds, |
| 55 |
fd_set * writefds, |
| 56 |
fd_set * exceptfds, |
| 57 |
struct timeval *select_timeout); |
| 58 |
|
| 59 |
#ifdef select |
| 60 |
#undef select |
| 61 |
#endif /* select */ |
| 62 |
#define select BSDselect |
| 63 |
#ifdef SOCKS |
| 64 |
#ifdef Rselect |
| 65 |
#undef Rselect |
| 66 |
#endif /* Rselect */ |
| 67 |
#define Rselect BSDselect |
| 68 |
#endif /* SOCKS */ |
| 69 |
#endif /* SVR4_BSDSELECT */ |
| 70 |
|
| 71 |
#include <LYLeaks.h> |
| 72 |
|
| 73 |
/* |
| 74 |
* Module-Wide variables |
| 75 |
*/ |
| 76 |
static char *hostname = NULL; /* The name of this host */ |
| 77 |
|
| 78 |
/* |
| 79 |
* PUBLIC VARIABLES |
| 80 |
*/ |
| 81 |
#ifdef SOCKS |
| 82 |
unsigned long socks_bind_remoteAddr; /* for long Rbind */ |
| 83 |
#endif /* SOCKS */ |
| 84 |
|
| 85 |
/* Encode INET status (as in sys/errno.h) inet_status() |
| 86 |
* ------------------ |
| 87 |
* |
| 88 |
* On entry, |
| 89 |
* where gives a description of what caused the error |
| 90 |
* global errno gives the error number in the Unix way. |
| 91 |
* |
| 92 |
* On return, |
| 93 |
* returns a negative status in the Unix way. |
| 94 |
*/ |
| 95 |
|
| 96 |
#ifdef DECL_SYS_ERRLIST |
| 97 |
extern char *sys_errlist[]; /* see man perror on cernvax */ |
| 98 |
extern int sys_nerr; |
| 99 |
#endif /* DECL_SYS_ERRLIST */ |
| 100 |
|
| 101 |
#ifdef __DJGPP__ |
| 102 |
static int ResolveYield(void) |
| 103 |
{ |
| 104 |
return HTCheckForInterrupt()? 0 : 1; |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
#if defined(VMS) && defined(UCX) |
| 109 |
/* |
| 110 |
* A routine to mimic the ioctl function for UCX. |
| 111 |
* Bjorn S. Nilsson, 25-Nov-1993. Based on an example in the UCX manual. |
| 112 |
*/ |
| 113 |
#include <HTioctl.h> |
| 114 |
|
| 115 |
int HTioctl(int d, |
| 116 |
int request, |
| 117 |
int *argp) |
| 118 |
{ |
| 119 |
int sdc, status; |
| 120 |
unsigned short fun, iosb[4]; |
| 121 |
char *p5, *p6; |
| 122 |
struct comm { |
| 123 |
int command; |
| 124 |
char *addr; |
| 125 |
} ioctl_comm; |
| 126 |
struct it2 { |
| 127 |
unsigned short len; |
| 128 |
unsigned short opt; |
| 129 |
struct comm *addr; |
| 130 |
} ioctl_desc; |
| 131 |
|
| 132 |
if ((sdc = vaxc$get_sdc(d)) == 0) { |
| 133 |
set_errno(EBADF); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
ioctl_desc.opt = UCX$C_IOCTL; |
| 137 |
ioctl_desc.len = sizeof(struct comm); |
| 138 |
|
| 139 |
ioctl_desc.addr = &ioctl_comm; |
| 140 |
if (request & IOC_OUT) { |
| 141 |
fun = IO$_SENSEMODE; |
| 142 |
p5 = 0; |
| 143 |
p6 = (char *) &ioctl_desc; |
| 144 |
} else { |
| 145 |
fun = IO$_SETMODE; |
| 146 |
p5 = (char *) &ioctl_desc; |
| 147 |
p6 = 0; |
| 148 |
} |
| 149 |
ioctl_comm.command = request; |
| 150 |
ioctl_comm.addr = (char *) argp; |
| 151 |
status = sys$qiow(0, sdc, fun, iosb, 0, 0, 0, 0, 0, 0, p5, p6); |
| 152 |
if (!(status & 01)) { |
| 153 |
set_errno(status); |
| 154 |
return -1; |
| 155 |
} |
| 156 |
if (!(iosb[0] & 01)) { |
| 157 |
set_errno(iosb[0]); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
return 0; |
| 161 |
} |
| 162 |
#endif /* VMS && UCX */ |
| 163 |
|
| 164 |
#define MY_FORMAT "TCP: Error %d in `SOCKET_ERRNO' after call to %s() failed.\n\t%s\n" |
| 165 |
/* third arg is transport/platform specific */ |
| 166 |
|
| 167 |
/* Report Internet Error |
| 168 |
* --------------------- |
| 169 |
*/ |
| 170 |
int HTInetStatus(const char *where) |
| 171 |
{ |
| 172 |
int status; |
| 173 |
int saved_errno = errno; |
| 174 |
|
| 175 |
#ifdef VMS |
| 176 |
#ifdef MULTINET |
| 177 |
SOCKET_ERRNO = vmserrno; |
| 178 |
#endif /* MULTINET */ |
| 179 |
#endif /* VMS */ |
| 180 |
|
| 181 |
#ifdef VM |
| 182 |
CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where, |
| 183 |
"(Error number not translated)")); /* What Is the VM equiv? */ |
| 184 |
#define ER_NO_TRANS_DONE |
| 185 |
#endif /* VM */ |
| 186 |
|
| 187 |
#ifdef VMS |
| 188 |
#ifdef MULTINET |
| 189 |
CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where, |
| 190 |
vms_errno_string())); |
| 191 |
#else |
| 192 |
CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where, |
| 193 |
((SOCKET_ERRNO > 0 && SOCKET_ERRNO <= 65) ? |
| 194 |
strerror(SOCKET_ERRNO) : "(Error number not translated)"))); |
| 195 |
#endif /* MULTINET */ |
| 196 |
#define ER_NO_TRANS_DONE |
| 197 |
#endif /* VMS */ |
| 198 |
|
| 199 |
#ifdef HAVE_STRERROR |
| 200 |
CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where, |
| 201 |
strerror(SOCKET_ERRNO))); |
| 202 |
#define ER_NO_TRANS_DONE |
| 203 |
#endif /* HAVE_STRERROR */ |
| 204 |
|
| 205 |
#ifndef ER_NO_TRANS_DONE |
| 206 |
CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where, |
| 207 |
(SOCKET_ERRNO < sys_nerr ? |
| 208 |
sys_errlist[SOCKET_ERRNO] : "Unknown error"))); |
| 209 |
#endif /* !ER_NO_TRANS_DONE */ |
| 210 |
|
| 211 |
#ifdef VMS |
| 212 |
#ifndef MULTINET |
| 213 |
CTRACE((tfp, |
| 214 |
" Unix error number (SOCKET_ERRNO) = %ld dec\n", |
| 215 |
SOCKET_ERRNO)); |
| 216 |
CTRACE((tfp, |
| 217 |
" VMS error (vaxc$errno) = %lx hex\n", |
| 218 |
vaxc$errno)); |
| 219 |
#endif /* MULTINET */ |
| 220 |
#endif /* VMS */ |
| 221 |
|
| 222 |
set_errno(saved_errno); |
| 223 |
|
| 224 |
#ifdef VMS |
| 225 |
/* |
| 226 |
* uerrno and errno happen to be zero if vmserrno <> 0 |
| 227 |
*/ |
| 228 |
#ifdef MULTINET |
| 229 |
status = -vmserrno; |
| 230 |
#else |
| 231 |
status = -vaxc$errno; |
| 232 |
#endif /* MULTINET */ |
| 233 |
#else |
| 234 |
status = -SOCKET_ERRNO; |
| 235 |
#endif /* VMS */ |
| 236 |
return status; |
| 237 |
} |
| 238 |
|
| 239 |
/* Parse a cardinal value parse_cardinal() |
| 240 |
* ---------------------- |
| 241 |
* |
| 242 |
* On entry, |
| 243 |
* *pp points to first character to be interpreted, terminated by |
| 244 |
* non 0:9 character. |
| 245 |
* *pstatus points to status already valid |
| 246 |
* maxvalue gives the largest allowable value. |
| 247 |
* |
| 248 |
* On exit, |
| 249 |
* *pp points to first unread character |
| 250 |
* *pstatus points to status updated iff bad |
| 251 |
*/ |
| 252 |
unsigned int HTCardinal(int *pstatus, |
| 253 |
char **pp, |
| 254 |
unsigned int max_value) |
| 255 |
{ |
| 256 |
unsigned int n; |
| 257 |
|
| 258 |
if ((**pp < '0') || (**pp > '9')) { /* Null string is error */ |
| 259 |
*pstatus = -3; /* No number where one expected */ |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
n = 0; |
| 264 |
while ((**pp >= '0') && (**pp <= '9')) |
| 265 |
n = n * 10 + (unsigned) (*((*pp)++) - '0'); |
| 266 |
|
| 267 |
if (n > max_value) { |
| 268 |
*pstatus = -4; /* Cardinal outside range */ |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
return n; |
| 273 |
} |
| 274 |
|
| 275 |
#ifndef DECNET /* Function only used below for a trace message */ |
| 276 |
/* Produce a string for an Internet address |
| 277 |
* ---------------------------------------- |
| 278 |
* |
| 279 |
* On exit, |
| 280 |
* returns a pointer to a static string which must be copied if |
| 281 |
* it is to be kept. |
| 282 |
*/ |
| 283 |
const char *HTInetString(LY_SOCKADDR * soc_A) |
| 284 |
{ |
| 285 |
#ifdef INET6 |
| 286 |
static char hostbuf[MAXHOSTNAMELEN]; |
| 287 |
struct sockaddr *soc_addr = &(soc_A->soc_address); |
| 288 |
|
| 289 |
getnameinfo(soc_addr, |
| 290 |
SA_LEN(soc_addr), |
| 291 |
hostbuf, (socklen_t) sizeof(hostbuf), |
| 292 |
NULL, 0, |
| 293 |
NI_NUMERICHOST); |
| 294 |
return hostbuf; |
| 295 |
#else |
| 296 |
struct sockaddr_in *soc_in = &(soc_A->soc_in); |
| 297 |
static char string[20]; |
| 298 |
|
| 299 |
sprintf(string, "%d.%d.%d.%d", |
| 300 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 0), |
| 301 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 1), |
| 302 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 2), |
| 303 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 3)); |
| 304 |
return string; |
| 305 |
#endif /* INET6 */ |
| 306 |
} |
| 307 |
#endif /* !DECNET */ |
| 308 |
|
| 309 |
/* Check whether string is a valid Internet hostname - kw |
| 310 |
* ------------------------------------------------- |
| 311 |
* |
| 312 |
* Checks whether |
| 313 |
* - contains only valid chars for domain names (actually, the |
| 314 |
* restrictions are somewhat relaxed), |
| 315 |
* - no leading dots or empty segments, |
| 316 |
* - no segment starts with '-' or '+' [this protects telnet command], |
| 317 |
* - max. length of dot-separated segment <= 63 (RFC 1034,1035), |
| 318 |
* - total length <= 254 (if it ends with dot) or 253 (otherwise) |
| 319 |
* [an interpretation of RFC 1034,1035, although RFC 1123 |
| 320 |
* suggests 255 as limit - kw]. |
| 321 |
* |
| 322 |
* Note: user (before '@') and port (after ':') components from |
| 323 |
* host part of URL should be already stripped (if appropriate) |
| 324 |
* from the input string. |
| 325 |
* |
| 326 |
* On exit, |
| 327 |
* returns 1 if valid, otherwise 0. |
| 328 |
*/ |
| 329 |
BOOL valid_hostname(char *name) |
| 330 |
{ |
| 331 |
int i = 1, iseg = 0; |
| 332 |
char *cp = name; |
| 333 |
|
| 334 |
if (!(name && *name)) |
| 335 |
return NO; |
| 336 |
for (; (*cp && i <= 253); cp++, i++) { |
| 337 |
if (*cp == '.') { |
| 338 |
if (iseg == 0) { |
| 339 |
return NO; |
| 340 |
} else { |
| 341 |
iseg = 0; |
| 342 |
continue; |
| 343 |
} |
| 344 |
} else if (iseg == 0 && (*cp == '-' || *cp == '+')) { |
| 345 |
return NO; |
| 346 |
} else if (++iseg > 63) { |
| 347 |
return NO; |
| 348 |
} |
| 349 |
if (!isalnum(UCH(*cp)) && |
| 350 |
*cp != '-' && *cp != '_' && |
| 351 |
*cp != '$' && *cp != '+') { |
| 352 |
return NO; |
| 353 |
} |
| 354 |
} |
| 355 |
return (BOOL) (*cp == '\0' || (*cp == '.' && iseg != 0 && cp[1] == '\0')); |
| 356 |
} |
| 357 |
|
| 358 |
/* for transfer of status from child to parent: */ |
| 359 |
typedef struct _statuses { |
| 360 |
size_t rehostentlen; |
| 361 |
int h_length; |
| 362 |
int child_errno; /* sometimes useful to pass this on */ |
| 363 |
int child_h_errno; |
| 364 |
BOOL h_errno_valid; |
| 365 |
} STATUSES; |
| 366 |
|
| 367 |
/* |
| 368 |
* Function to allow us to be killed with a normal signal (not |
| 369 |
* SIGKILL), but don't go through normal libc exit() processing, which |
| 370 |
* would screw up parent's stdio. -BL |
| 371 |
*/ |
| 372 |
#ifdef NSL_FORK |
| 373 |
static void quench(int sig GCC_UNUSED) |
| 374 |
{ |
| 375 |
_exit(2); |
| 376 |
} |
| 377 |
#endif |
| 378 |
|
| 379 |
int lynx_nsl_status = HT_OK; |
| 380 |
|
| 381 |
#define DEBUG_HOSTENT /* disable in case of problems */ |
| 382 |
#define DEBUG_HOSTENT_CHILD /* for NSL_FORK, may screw up trace file */ |
| 383 |
|
| 384 |
/* |
| 385 |
* dump_hostent - dumps the contents of a LYNX_HOSTENT to the |
| 386 |
* trace log or stderr, including all pointer values, strings, and |
| 387 |
* addresses, in a format inspired by gdb's print format. - kw |
| 388 |
*/ |
| 389 |
static void dump_hostent(const char *msgprefix, |
| 390 |
const void *data) |
| 391 |
{ |
| 392 |
if (TRACE) { |
| 393 |
int i; |
| 394 |
char **pcnt; |
| 395 |
const LYNX_HOSTENT *phost = data; |
| 396 |
|
| 397 |
CTRACE((tfp, "%s: %p ", msgprefix, (const void *) phost)); |
| 398 |
if (phost) { |
| 399 |
CTRACE((tfp, "{ h_name = %p", (void *) phost->h_name)); |
| 400 |
if (phost->h_name) { |
| 401 |
CTRACE((tfp, " \"%s\",", phost->h_name)); |
| 402 |
} else { |
| 403 |
CTRACE((tfp, ",")); |
| 404 |
} |
| 405 |
CTRACE((tfp, "\n\t h_aliases = %p", (void *) phost->h_aliases)); |
| 406 |
if (phost->h_aliases) { |
| 407 |
CTRACE((tfp, " {")); |
| 408 |
for (pcnt = phost->h_aliases; *pcnt; pcnt++) { |
| 409 |
CTRACE((tfp, "%s %p \"%s\"", |
| 410 |
(pcnt == phost->h_aliases ? " " : ", "), |
| 411 |
(void *) *pcnt, *pcnt)); |
| 412 |
} |
| 413 |
CTRACE((tfp, "%s0x0 },\n\t", |
| 414 |
(*phost->h_aliases ? ", " : " "))); |
| 415 |
} else { |
| 416 |
CTRACE((tfp, ",\n\t")); |
| 417 |
} |
| 418 |
CTRACE((tfp, " h_addrtype = %d,", phost->h_addrtype)); |
| 419 |
CTRACE((tfp, " h_length = %d,\n\t", phost->h_length)); |
| 420 |
CTRACE((tfp, " h_addr_list = %p", (void *) phost->h_addr_list)); |
| 421 |
if (phost->h_addr_list) { |
| 422 |
CTRACE((tfp, " {")); |
| 423 |
for (pcnt = phost->h_addr_list; *pcnt; pcnt++) { |
| 424 |
CTRACE((tfp, "%s %p", |
| 425 |
(pcnt == phost->h_addr_list ? "" : ","), |
| 426 |
(void *) *pcnt)); |
| 427 |
for (i = 0; i < phost->h_length; i++) { |
| 428 |
CTRACE((tfp, "%s%d%s", (i == 0 ? " \"" : "."), |
| 429 |
(int) *((unsigned char *) (*pcnt) + i), |
| 430 |
(i + 1 == phost->h_length ? "\"" : ""))); |
| 431 |
} |
| 432 |
} |
| 433 |
if (*phost->h_addr_list) { |
| 434 |
CTRACE((tfp, ", 0x0 } }")); |
| 435 |
} else { |
| 436 |
CTRACE((tfp, " 0x0 } }")); |
| 437 |
} |
| 438 |
} else { |
| 439 |
CTRACE((tfp, "}")); |
| 440 |
} |
| 441 |
} |
| 442 |
CTRACE((tfp, "\n")); |
| 443 |
fflush(tfp); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
#ifdef NSL_FORK |
| 448 |
|
| 449 |
/* |
| 450 |
* Even though it is a small amount, we cannot count on reading the whole |
| 451 |
* struct via a pipe in one read -TD |
| 452 |
*/ |
| 453 |
static unsigned read_bytes(int fd, char *buffer, size_t length) |
| 454 |
{ |
| 455 |
unsigned result = 0; |
| 456 |
|
| 457 |
while (length != 0) { |
| 458 |
unsigned got = (unsigned) read(fd, buffer, length); |
| 459 |
|
| 460 |
if ((int) got > 0) { |
| 461 |
result += got; |
| 462 |
buffer += got; |
| 463 |
length -= got; |
| 464 |
} else { |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
return result; |
| 469 |
} |
| 470 |
|
| 471 |
static unsigned read_hostent(int fd, char *buffer, size_t length) |
| 472 |
{ |
| 473 |
unsigned have = read_bytes(fd, buffer, length); |
| 474 |
|
| 475 |
if (have) { |
| 476 |
LYNX_HOSTENT *data = (LYNX_HOSTENT *) (void *) buffer; |
| 477 |
char *next_char = (char *) data + sizeof(*data); |
| 478 |
char **next_ptr = (char **) (void *) next_char; |
| 479 |
long offset = 0; |
| 480 |
int n; |
| 481 |
int num_addrs = 0; |
| 482 |
int num_aliases = 0; |
| 483 |
|
| 484 |
if (data->h_addr_list) { |
| 485 |
data->h_addr_list = next_ptr; |
| 486 |
while (next_ptr[num_addrs] != NULL) { |
| 487 |
++num_addrs; |
| 488 |
} |
| 489 |
next_ptr += (num_addrs + 1); |
| 490 |
next_char += (size_t) (num_addrs + 1) * sizeof(data->h_addr_list[0]); |
| 491 |
} |
| 492 |
|
| 493 |
if (data->h_aliases) { |
| 494 |
data->h_aliases = next_ptr; |
| 495 |
while (next_ptr[num_aliases] != NULL) { |
| 496 |
++num_aliases; |
| 497 |
} |
| 498 |
next_char += (size_t) (num_aliases + 1) * sizeof(data->h_aliases[0]); |
| 499 |
} |
| 500 |
|
| 501 |
if (data->h_name) { |
| 502 |
offset = next_char - data->h_name; |
| 503 |
data->h_name = next_char; |
| 504 |
} else if (data->h_addr_list) { |
| 505 |
offset = next_char - (char *) data->h_addr_list[0]; |
| 506 |
} else if (data->h_aliases) { |
| 507 |
offset = next_char - (char *) data->h_aliases[0]; |
| 508 |
} |
| 509 |
|
| 510 |
if (data->h_addr_list) { |
| 511 |
for (n = 0; n < num_addrs; ++n) { |
| 512 |
data->h_addr_list[n] += offset; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (data->h_aliases) { |
| 517 |
for (n = 0; n < num_aliases; ++n) { |
| 518 |
data->h_aliases[n] += offset; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return have; |
| 524 |
} |
| 525 |
#endif /* NSL_FORK */ |
| 526 |
|
| 527 |
/* |
| 528 |
* fill_rehostent - copies as much as possible relevant content from |
| 529 |
* the LYNX_HOSTENT pointed to by phost to the char buffer given |
| 530 |
* by rehostent, subject to maximum output length rehostentsize, |
| 531 |
* following pointers and building self-contained output which can be |
| 532 |
* cast to a LYNX_HOSTENT. - kw |
| 533 |
* See also description of LYGetHostByName. |
| 534 |
*/ |
| 535 |
#if defined(NSL_FORK) || defined(_WINDOWS_NSL) |
| 536 |
|
| 537 |
#define REHOSTENT_SIZE 128 /* not bigger than pipe buffer! */ |
| 538 |
|
| 539 |
typedef struct { |
| 540 |
LYNX_HOSTENT h; |
| 541 |
char rest[REHOSTENT_SIZE]; |
| 542 |
} AlignedHOSTENT; |
| 543 |
|
| 544 |
static size_t fill_rehostent(void **rehostent, |
| 545 |
const LYNX_HOSTENT *phost) |
| 546 |
{ |
| 547 |
static const char *this_func = "fill_rehostent"; |
| 548 |
|
| 549 |
LYNX_HOSTENT *data = NULL; |
| 550 |
int num_addrs = 0; |
| 551 |
int num_aliases = 0; |
| 552 |
char *result = NULL; |
| 553 |
char *p_next_char; |
| 554 |
char **p_next_charptr; |
| 555 |
size_t name_len = 0; |
| 556 |
size_t need = sizeof(LYNX_HOSTENT); |
| 557 |
int n; |
| 558 |
|
| 559 |
if (!phost) |
| 560 |
return 0; |
| 561 |
|
| 562 |
if (phost->h_name) { |
| 563 |
name_len = strlen(phost->h_name); |
| 564 |
need += name_len + 1; |
| 565 |
} |
| 566 |
if (phost->h_addr_list) { |
| 567 |
while (phost->h_addr_list[num_addrs]) { |
| 568 |
num_addrs++; |
| 569 |
} |
| 570 |
need += ((size_t) num_addrs + 1) * ((size_t) phost->h_length |
| 571 |
+ sizeof(phost->h_addr_list[0])); |
| 572 |
} |
| 573 |
if (phost->h_aliases) { |
| 574 |
while (phost->h_aliases[num_aliases]) { |
| 575 |
need += strlen(phost->h_aliases[num_aliases]) + 1; |
| 576 |
num_aliases++; |
| 577 |
} |
| 578 |
need += ((size_t) num_aliases + 1) * sizeof(phost->h_aliases[0]); |
| 579 |
} |
| 580 |
|
| 581 |
if ((result = calloc(need, sizeof(char))) == NULL) |
| 582 |
outofmem(__FILE__, this_func); |
| 583 |
|
| 584 |
*rehostent = result; |
| 585 |
|
| 586 |
data = (LYNX_HOSTENT *) (void *) result; |
| 587 |
|
| 588 |
data->h_addrtype = phost->h_addrtype; |
| 589 |
data->h_length = phost->h_length; |
| 590 |
|
| 591 |
p_next_char = result + sizeof(LYNX_HOSTENT); |
| 592 |
|
| 593 |
p_next_charptr = (char **) (void *) p_next_char; |
| 594 |
if (phost->h_addr_list) |
| 595 |
p_next_char += (size_t) (num_addrs + 1) * sizeof(phost->h_addr_list[0]); |
| 596 |
if (phost->h_aliases) |
| 597 |
p_next_char += (size_t) (num_aliases + 1) * sizeof(phost->h_aliases[0]); |
| 598 |
|
| 599 |
if (phost->h_name) { |
| 600 |
data->h_name = p_next_char; |
| 601 |
strcpy(p_next_char, phost->h_name); |
| 602 |
p_next_char += name_len + 1; |
| 603 |
} |
| 604 |
|
| 605 |
if (phost->h_addr_list) { |
| 606 |
data->h_addr_list = p_next_charptr; |
| 607 |
for (n = 0; n < num_addrs; ++n) { |
| 608 |
MemCpy(p_next_char, phost->h_addr_list[n], phost->h_length); |
| 609 |
*p_next_charptr++ = p_next_char; |
| 610 |
p_next_char += phost->h_length; |
| 611 |
} |
| 612 |
++p_next_charptr; |
| 613 |
} |
| 614 |
|
| 615 |
if (phost->h_aliases) { |
| 616 |
data->h_aliases = p_next_charptr; |
| 617 |
for (n = 0; n < num_aliases; ++n) { |
| 618 |
strcpy(p_next_char, phost->h_aliases[n]); |
| 619 |
*p_next_charptr++ = p_next_char; |
| 620 |
p_next_char += strlen(phost->h_aliases[n]) + 1;; |
| 621 |
} |
| 622 |
} |
| 623 |
return need; |
| 624 |
} |
| 625 |
#endif /* NSL_FORK */ |
| 626 |
|
| 627 |
/* |
| 628 |
* This chunk of code is used in both win32 and cygwin. |
| 629 |
*/ |
| 630 |
#if defined(_WINDOWS_NSL) |
| 631 |
static LYNX_HOSTENT *gbl_phost; /* Pointer to host - See netdb.h */ |
| 632 |
|
| 633 |
#if !(defined(__CYGWIN__) && defined(NSL_FORK)) |
| 634 |
static int donelookup; |
| 635 |
|
| 636 |
static unsigned long __stdcall _fork_func(void *arg) |
| 637 |
{ |
| 638 |
const char *host = (const char *) arg; |
| 639 |
static AlignedHOSTENT aligned_full_rehostent; |
| 640 |
char *rehostent = (char *) &aligned_full_rehostent; |
| 641 |
size_t rehostentlen = 0; |
| 642 |
|
| 643 |
#ifdef SH_EX |
| 644 |
unsigned long addr; |
| 645 |
|
| 646 |
addr = (unsigned long) inet_addr(host); |
| 647 |
if (addr != INADDR_NONE) |
| 648 |
gbl_phost = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); |
| 649 |
else |
| 650 |
gbl_phost = gethostbyname(host); |
| 651 |
#else |
| 652 |
gbl_phost = gethostbyname(host); |
| 653 |
#endif |
| 654 |
|
| 655 |
if (gbl_phost) { |
| 656 |
rehostentlen = fill_rehostent((void **) &rehostent, gbl_phost); |
| 657 |
if (rehostentlen == 0) { |
| 658 |
gbl_phost = (LYNX_HOSTENT *) NULL; |
| 659 |
} else { |
| 660 |
gbl_phost = (LYNX_HOSTENT *) rehostent; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
donelookup = TRUE; |
| 665 |
return (unsigned long) 1; /* nonzero for successful exit */ |
| 666 |
} |
| 667 |
#endif /* __CYGWIN__ */ |
| 668 |
#endif /* _WINDOWS_NSL */ |
| 669 |
|
| 670 |
#ifdef NSL_FORK |
| 671 |
|
| 672 |
#ifndef HAVE_H_ERRNO |
| 673 |
#undef h_errno |
| 674 |
#define h_errno my_errno |
| 675 |
static int my_errno; |
| 676 |
|
| 677 |
#else /* we do HAVE_H_ERRNO: */ |
| 678 |
#ifndef h_errno /* there may be a macro as well as the extern data */ |
| 679 |
extern int h_errno; |
| 680 |
#endif |
| 681 |
#endif |
| 682 |
|
| 683 |
static BOOL setup_nsl_fork(void (*really) (const char *, |
| 684 |
const char *, |
| 685 |
STATUSES *, |
| 686 |
void **), |
| 687 |
unsigned (*readit) (int, char *, size_t), |
| 688 |
void (*dumpit) (const char *, const void *), |
| 689 |
const char *host, |
| 690 |
const char *port, |
| 691 |
void **rehostent) |
| 692 |
{ |
| 693 |
static const char *this_func = "setup_nsl_fork"; |
| 694 |
|
| 695 |
STATUSES statuses; |
| 696 |
|
| 697 |
/* |
| 698 |
* fork-based gethostbyname() with checks for interrupts. |
| 699 |
* - Tom Zerucha ([email protected]) & FM |
| 700 |
*/ |
| 701 |
int got_rehostent = 0; |
| 702 |
|
| 703 |
#if HAVE_SIGACTION |
| 704 |
sigset_t old_sigset; |
| 705 |
sigset_t new_sigset; |
| 706 |
#endif |
| 707 |
/* |
| 708 |
* Pipe, child pid, status buffers, start time, select() control |
| 709 |
* variables. |
| 710 |
*/ |
| 711 |
int fpid, waitret; |
| 712 |
int pfd[2], selret; |
| 713 |
unsigned readret; |
| 714 |
|
| 715 |
#ifdef HAVE_TYPE_UNIONWAIT |
| 716 |
union wait waitstat; |
| 717 |
|
| 718 |
#else |
| 719 |
int waitstat = 0; |
| 720 |
#endif |
| 721 |
time_t start_time = time((time_t *) 0); |
| 722 |
fd_set readfds; |
| 723 |
struct timeval one_second; |
| 724 |
long dns_patience = 30; /* how many seconds will we wait for DNS? */ |
| 725 |
int child_exited = 0; |
| 726 |
|
| 727 |
pfd[0] = pfd[1] = 0; |
| 728 |
memset(&statuses, 0, sizeof(STATUSES)); |
| 729 |
statuses.h_errno_valid = NO; |
| 730 |
|
| 731 |
/* |
| 732 |
* Reap any children that have terminated since last time through. |
| 733 |
* This might include children that we killed, then waited with WNOHANG |
| 734 |
* before they were actually ready to be reaped. (Should be max of 1 |
| 735 |
* in this state, but the loop is safe if waitpid() is implemented |
| 736 |
* correctly: returns 0 when children exist but none have exited; -1 |
| 737 |
* with errno == ECHILD when no children.) -BL |
| 738 |
*/ |
| 739 |
do { |
| 740 |
waitret = waitpid(-1, NULL, WNOHANG); |
| 741 |
} while (waitret > 0 || (waitret == -1 && errno == EINTR)); |
| 742 |
waitret = 0; |
| 743 |
|
| 744 |
IGNORE_RC(pipe(pfd)); |
| 745 |
|
| 746 |
#if HAVE_SIGACTION |
| 747 |
/* |
| 748 |
* Attempt to prevent a rare situation where the child could execute |
| 749 |
* the Lynx signal handlers because it gets killed before it even has a |
| 750 |
* chance to reset its handlers, resulting in bogus 'Exiting via |
| 751 |
* interrupt' message and screen corruption or worse. |
| 752 |
* Should that continue to be reported, for systems without |
| 753 |
* sigprocmask(), we need to find a different solutions for those. - |
| 754 |
* kw 19990430 |
| 755 |
*/ |
| 756 |
sigemptyset(&new_sigset); |
| 757 |
sigaddset(&new_sigset, SIGTERM); |
| 758 |
sigaddset(&new_sigset, SIGINT); |
| 759 |
#ifndef NOSIGHUP |
| 760 |
sigaddset(&new_sigset, SIGHUP); |
| 761 |
#endif /* NOSIGHUP */ |
| 762 |
#ifdef SIGTSTP |
| 763 |
sigaddset(&new_sigset, SIGTSTP); |
| 764 |
#endif /* SIGTSTP */ |
| 765 |
#ifdef SIGWINCH |
| 766 |
sigaddset(&new_sigset, SIGWINCH); |
| 767 |
#endif /* SIGWINCH */ |
| 768 |
sigprocmask(SIG_BLOCK, &new_sigset, &old_sigset); |
| 769 |
#endif /* HAVE_SIGACTION */ |
| 770 |
|
| 771 |
if ((fpid = fork()) == 0) { |
| 772 |
/* |
| 773 |
* Child - for the long call. |
| 774 |
* |
| 775 |
* Make sure parent can kill us at will. -BL |
| 776 |
*/ |
| 777 |
(void) signal(SIGTERM, quench); |
| 778 |
|
| 779 |
/* |
| 780 |
* Also make sure the child does not run one of the signal handlers |
| 781 |
* that may have been installed by Lynx if one of those signals |
| 782 |
* occurs. For example we don't want the child to remove temp |
| 783 |
* files on ^C, let the parent deal with that. - kw |
| 784 |
*/ |
| 785 |
(void) signal(SIGINT, quench); |
| 786 |
#ifndef NOSIGHUP |
| 787 |
(void) signal(SIGHUP, quench); |
| 788 |
#endif /* NOSIGHUP */ |
| 789 |
#ifdef SIGTSTP |
| 790 |
if (no_suspend) |
| 791 |
(void) signal(SIGTSTP, SIG_IGN); |
| 792 |
else |
| 793 |
(void) signal(SIGTSTP, SIG_DFL); |
| 794 |
#endif /* SIGTSTP */ |
| 795 |
#ifdef SIGWINCH |
| 796 |
(void) signal(SIGWINCH, SIG_IGN); |
| 797 |
#endif /* SIGWINCH */ |
| 798 |
#ifndef __linux__ |
| 799 |
#ifndef DOSPATH |
| 800 |
signal(SIGBUS, SIG_DFL); |
| 801 |
#endif /* DOSPATH */ |
| 802 |
#endif /* !__linux__ */ |
| 803 |
signal(SIGSEGV, SIG_DFL); |
| 804 |
signal(SIGILL, SIG_DFL); |
| 805 |
|
| 806 |
#if HAVE_SIGACTION |
| 807 |
/* Restore signal mask to whatever it was before the fork. -kw */ |
| 808 |
sigprocmask(SIG_SETMASK, &old_sigset, NULL); |
| 809 |
#endif /* HAVE_SIGACTION */ |
| 810 |
|
| 811 |
/* |
| 812 |
* Child won't use read side. -BL |
| 813 |
*/ |
| 814 |
close(pfd[0]); |
| 815 |
#ifdef HAVE_H_ERRNO |
| 816 |
/* to detect cases when it doesn't get set although it should */ |
| 817 |
h_errno = -2; |
| 818 |
#endif |
| 819 |
set_errno(0); |
| 820 |
really(host, port, &statuses, rehostent); |
| 821 |
/* |
| 822 |
* Send variables indicating status of lookup to parent. That |
| 823 |
* includes rehostentlen, which the parent will use as the size for |
| 824 |
* the second read (if > 0). |
| 825 |
*/ |
| 826 |
if (!statuses.child_errno) |
| 827 |
statuses.child_errno = errno; |
| 828 |
IGNORE_RC(write(pfd[1], &statuses, sizeof(statuses))); |
| 829 |
|
| 830 |
if (statuses.rehostentlen) { |
| 831 |
/* |
| 832 |
* Return our resulting rehostent through pipe... |
| 833 |
*/ |
| 834 |
IGNORE_RC(write(pfd[1], *rehostent, statuses.rehostentlen)); |
| 835 |
close(pfd[1]); |
| 836 |
_exit(0); |
| 837 |
} else { |
| 838 |
/* |
| 839 |
* ... or return error as exit code. |
| 840 |
*/ |
| 841 |
_exit(1); |
| 842 |
} |
| 843 |
} |
| 844 |
#if HAVE_SIGACTION |
| 845 |
/* |
| 846 |
* (parent) Restore signal mask to whatever it was before the fork. - |
| 847 |
* kw |
| 848 |
*/ |
| 849 |
sigprocmask(SIG_SETMASK, &old_sigset, NULL); |
| 850 |
#endif /* HAVE_SIGACTION */ |
| 851 |
|
| 852 |
/* |
| 853 |
* (parent) Wait until lookup finishes, or interrupt, or cycled too |
| 854 |
* many times (just in case) -BL |
| 855 |
*/ |
| 856 |
|
| 857 |
close(pfd[1]); /* parent won't use write side -BL */ |
| 858 |
|
| 859 |
if (fpid < 0) { /* fork failed */ |
| 860 |
close(pfd[0]); |
| 861 |
goto failed; |
| 862 |
} |
| 863 |
|
| 864 |
while (child_exited || (long) (time((time_t *) 0) - start_time) < dns_patience) { |
| 865 |
|
| 866 |
FD_ZERO(&readfds); |
| 867 |
/* |
| 868 |
* This allows us to abort immediately, not after 1-second timeout, |
| 869 |
* when user hits abort key. Can't do this when using SLANG (or at |
| 870 |
* least I don't know how), so SLANG users must live with up-to-1s |
| 871 |
* timeout. -BL |
| 872 |
* |
| 873 |
* Whoops -- we need to make sure stdin is actually selectable! |
| 874 |
* /dev/null isn't, on some systems, which makes some useful Lynx |
| 875 |
* invocations fail. -BL |
| 876 |
*/ |
| 877 |
{ |
| 878 |
int kbd_fd = LYConsoleInputFD(TRUE); |
| 879 |
|
| 880 |
if (kbd_fd != INVSOC) { |
| 881 |
FD_SET(kbd_fd, &readfds); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
one_second.tv_sec = 1; |
| 886 |
one_second.tv_usec = 0; |
| 887 |
FD_SET(pfd[0], &readfds); |
| 888 |
|
| 889 |
/* |
| 890 |
* Return when data received, interrupted, or failed. If nothing |
| 891 |
* is waiting, we sleep for 1 second in select(), to be nice to the |
| 892 |
* system. -BL |
| 893 |
*/ |
| 894 |
#ifdef SOCKS |
| 895 |
if (socks_flag) |
| 896 |
selret = Rselect(pfd[0] + 1, &readfds, NULL, NULL, &one_second); |
| 897 |
else |
| 898 |
#endif /* SOCKS */ |
| 899 |
selret = select(pfd[0] + 1, &readfds, NULL, NULL, &one_second); |
| 900 |
|
| 901 |
if ((selret > 0) && FD_ISSET(pfd[0], &readfds)) { |
| 902 |
/* |
| 903 |
* First get status, including length of address. -BL, kw |
| 904 |
*/ |
| 905 |
readret = read_bytes(pfd[0], (char *) &statuses, sizeof(statuses)); |
| 906 |
if (readret == sizeof(statuses)) { |
| 907 |
h_errno = statuses.child_h_errno; |
| 908 |
set_errno(statuses.child_errno); |
| 909 |
#ifdef HAVE_H_ERRNO |
| 910 |
if (statuses.h_errno_valid) { |
| 911 |
lynx_nsl_status = HT_H_ERRNO_VALID; |
| 912 |
/* |
| 913 |
* If something went wrong in the child process other |
| 914 |
* than normal lookup errors, and it appears that we |
| 915 |
* have enough info to know what went wrong, generate |
| 916 |
* diagnostic output. ENOMEM observed on linux in |
| 917 |
* processes constrained with ulimit. It would be too |
| 918 |
* unkind to abort the session, access to local files |
| 919 |
* or through a proxy may still work. - kw |
| 920 |
*/ |
| 921 |
if ( |
| 922 |
#ifdef NETDB_INTERNAL /* linux glibc: defined in netdb.h */ |
| 923 |
(errno && h_errno == NETDB_INTERNAL) || |
| 924 |
#endif |
| 925 |
(errno == ENOMEM && |
| 926 |
statuses.rehostentlen == 0 && |
| 927 |
/* should probably be NETDB_INTERNAL if child |
| 928 |
memory exhausted, but we may find that |
| 929 |
h_errno remains unchanged. - kw */ |
| 930 |
h_errno == -2)) { |
| 931 |
#ifndef MULTINET |
| 932 |
HTInetStatus("CHILD gethostbyname"); |
| 933 |
#endif |
| 934 |
HTAlert(LYStrerror(statuses.child_errno)); |
| 935 |
if (errno == ENOMEM) { |
| 936 |
/* |
| 937 |
* Not much point in continuing, right? Fake a |
| 938 |
* 'z', should shorten pointless guessing |
| 939 |
* cycle. - kw |
| 940 |
*/ |
| 941 |
LYFakeZap(YES); |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
#endif /* HAVE_H_ERRNO */ |
| 946 |
if (statuses.rehostentlen != 0) { |
| 947 |
/* |
| 948 |
* Then get the full reorganized hostent. -BL, kw |
| 949 |
*/ |
| 950 |
if ((*rehostent = malloc(statuses.rehostentlen)) == NULL) |
| 951 |
outofmem(__FILE__, this_func); |
| 952 |
readret = (*readit) (pfd[0], *rehostent, statuses.rehostentlen); |
| 953 |
#ifdef DEBUG_HOSTENT |
| 954 |
dumpit("Read from pipe", *rehostent); |
| 955 |
#endif |
| 956 |
if (readret == statuses.rehostentlen) { |
| 957 |
got_rehostent = 1; |
| 958 |
lynx_nsl_status = HT_OK; |
| 959 |
} else if (!statuses.h_errno_valid) { |
| 960 |
lynx_nsl_status = HT_INTERNAL; |
| 961 |
} |
| 962 |
} |
| 963 |
} else { |
| 964 |
lynx_nsl_status = HT_ERROR; |
| 965 |
} |
| 966 |
/* |
| 967 |
* Make sure child is cleaned up. -BL |
| 968 |
*/ |
| 969 |
if (!child_exited) |
| 970 |
waitret = waitpid(fpid, &waitstat, WNOHANG); |
| 971 |
if (!WIFEXITED(waitstat) && !WIFSIGNALED(waitstat)) { |
| 972 |
kill(fpid, SIGTERM); |
| 973 |
waitret = waitpid(fpid, &waitstat, WNOHANG); |
| 974 |
} |
| 975 |
break; |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* Clean up if child exited before & no data received. -BL |
| 980 |
*/ |
| 981 |
if (child_exited) { |
| 982 |
waitret = waitpid(fpid, &waitstat, WNOHANG); |
| 983 |
break; |
| 984 |
} |
| 985 |
/* |
| 986 |
* If child exited, loop once more looking for data. -BL |
| 987 |
*/ |
| 988 |
if ((waitret = waitpid(fpid, &waitstat, WNOHANG)) > 0) { |
| 989 |
/* |
| 990 |
* Data will be arriving right now, so make sure we don't |
| 991 |
* short-circuit out for too many loops, and skip the interrupt |
| 992 |
* check. -BL |
| 993 |
*/ |
| 994 |
child_exited = 1; |
| 995 |
continue; |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
* Abort if interrupt key pressed. |
| 1000 |
*/ |
| 1001 |
if (HTCheckForInterrupt()) { |
| 1002 |
CTRACE((tfp, "%s: INTERRUPTED gethostbyname.\n", this_func)); |
| 1003 |
kill(fpid, SIGTERM); |
| 1004 |
waitpid(fpid, NULL, WNOHANG); |
| 1005 |
close(pfd[0]); |
| 1006 |
lynx_nsl_status = HT_INTERRUPTED; |
| 1007 |
return FALSE; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
close(pfd[0]); |
| 1011 |
if (waitret <= 0) { |
| 1012 |
kill(fpid, SIGTERM); |
| 1013 |
waitret = waitpid(fpid, &waitstat, WNOHANG); |
| 1014 |
} |
| 1015 |
if (waitret > 0) { |
| 1016 |
if (WIFEXITED(waitstat)) { |
| 1017 |
CTRACE((tfp, |
| 1018 |
"%s: NSL_FORK child %d exited, status 0x%x.\n", |
| 1019 |
this_func, (int) waitret, WEXITSTATUS(waitstat))); |
| 1020 |
} else if (WIFSIGNALED(waitstat)) { |
| 1021 |
CTRACE((tfp, |
| 1022 |
"%s: NSL_FORK child %d got signal, status 0x%x!\n", |
| 1023 |
this_func, (int) waitret, WTERMSIG(waitstat))); |
| 1024 |
#ifdef WCOREDUMP |
| 1025 |
if (WCOREDUMP(waitstat)) { |
| 1026 |
CTRACE((tfp, |
| 1027 |
"%s: NSL_FORK child %d dumped core!\n", |
| 1028 |
this_func, (int) waitret)); |
| 1029 |
} |
| 1030 |
#endif /* WCOREDUMP */ |
| 1031 |
} else if (WIFSTOPPED(waitstat)) { |
| 1032 |
CTRACE((tfp, |
| 1033 |
"%s: NSL_FORK child %d is stopped, status 0x%x!\n", |
| 1034 |
this_func, (int) waitret, WSTOPSIG(waitstat))); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
if (!got_rehostent) { |
| 1038 |
goto failed; |
| 1039 |
} |
| 1040 |
return TRUE; |
| 1041 |
failed: |
| 1042 |
return FALSE; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* This is called via the child-side of the fork. |
| 1047 |
*/ |
| 1048 |
static void really_gethostbyname(const char *host, |
| 1049 |
const char *port GCC_UNUSED, |
| 1050 |
STATUSES * statuses, |
| 1051 |
void **rehostent) |
| 1052 |
{ |
| 1053 |
LYNX_HOSTENT *phost; /* Pointer to host - See netdb.h */ |
| 1054 |
LYNX_HOSTENT *result = NULL; |
| 1055 |
|
| 1056 |
(void) port; |
| 1057 |
|
| 1058 |
phost = gethostbyname(host); |
| 1059 |
statuses->rehostentlen = 0; |
| 1060 |
statuses->child_errno = errno; |
| 1061 |
statuses->child_h_errno = h_errno; |
| 1062 |
#ifdef HAVE_H_ERRNO |
| 1063 |
statuses->h_errno_valid = YES; |
| 1064 |
#endif |
| 1065 |
#ifdef MVS |
| 1066 |
CTRACE((tfp, "really_gethostbyname() returned %d\n", phost)); |
| 1067 |
#endif /* MVS */ |
| 1068 |
|
| 1069 |
#ifdef DEBUG_HOSTENT_CHILD |
| 1070 |
dump_hostent("CHILD gethostbyname", phost); |
| 1071 |
#endif |
| 1072 |
if (OK_HOST(phost)) { |
| 1073 |
statuses->rehostentlen = fill_rehostent(rehostent, phost); |
| 1074 |
result = (LYNX_HOSTENT *) (*rehostent); |
| 1075 |
#ifdef DEBUG_HOSTENT_CHILD |
| 1076 |
dump_hostent("CHILD fill_rehostent", result); |
| 1077 |
#endif |
| 1078 |
} |
| 1079 |
if (statuses->rehostentlen <= sizeof(LYNX_HOSTENT) || !OK_HOST(result)) { |
| 1080 |
statuses->rehostentlen = 0; |
| 1081 |
statuses->h_length = 0; |
| 1082 |
} else { |
| 1083 |
statuses->h_length = result->h_length; |
| 1084 |
#ifdef HAVE_H_ERRNO |
| 1085 |
if (h_errno == -2) /* success, but h_errno unchanged? */ |
| 1086 |
statuses->h_errno_valid = NO; |
| 1087 |
#endif |
| 1088 |
} |
| 1089 |
} |
| 1090 |
#endif /* NSL_FORK */ |
| 1091 |
|
| 1092 |
/* Resolve an internet hostname, like gethostbyname |
| 1093 |
* ------------------------------------------------ |
| 1094 |
* |
| 1095 |
* On entry, |
| 1096 |
* host points to the given host name, not numeric address, |
| 1097 |
* without colon or port number. |
| 1098 |
* |
| 1099 |
* On exit, |
| 1100 |
* returns a pointer to a LYNX_HOSTENT in static storage, |
| 1101 |
* or NULL in case of error or user interruption. |
| 1102 |
* |
| 1103 |
* The interface is intended to be exactly the same as for (Unix) |
| 1104 |
* gethostbyname(), except for the following: |
| 1105 |
* |
| 1106 |
* If NSL_FORK is not used, the result of gethostbyname is returned |
| 1107 |
* directly. Otherwise: |
| 1108 |
* All lists, addresses, and strings referred to by pointers in the |
| 1109 |
* returned struct are located, together with the returned struct |
| 1110 |
* itself, in a buffer of size REHOSTENT_SIZE. If not everything fits, |
| 1111 |
* some info is omitted, but the function is careful to still return |
| 1112 |
* a valid structure, without truncating strings; it tries to return, |
| 1113 |
* in order of decreasing priority, the first address (h_addr_list[0]), the |
| 1114 |
* official name (h_name), the additional addresses, then alias names. |
| 1115 |
* |
| 1116 |
* If NULL is returned, the reason is made available in the global |
| 1117 |
* variable lynx_nsl_status, with one of the following values: |
| 1118 |
* HT_INTERRUPTED Interrupted by user |
| 1119 |
* HT_NOT_ACCEPTABLE Hostname detected as invalid |
| 1120 |
* (also sets h_errno) |
| 1121 |
* HT_H_ERRNO_VALID An error occurred, and h_errno holds |
| 1122 |
* an appropriate value |
| 1123 |
* HT_ERROR Resolver error, reason not known |
| 1124 |
* HT_INTERNAL Internal error |
| 1125 |
*/ |
| 1126 |
static LYNX_HOSTENT *LYGetHostByName(char *host) |
| 1127 |
{ |
| 1128 |
static const char *this_func = "LYGetHostByName"; |
| 1129 |
|
| 1130 |
#ifdef NSL_FORK |
| 1131 |
/* for transfer of result between from child to parent: */ |
| 1132 |
LYNX_HOSTENT *rehostent = NULL; |
| 1133 |
#endif /* NSL_FORK */ |
| 1134 |
|
| 1135 |
LYNX_HOSTENT *result_phost = NULL; |
| 1136 |
|
| 1137 |
#ifdef __DJGPP__ |
| 1138 |
_resolve_hook = ResolveYield; |
| 1139 |
#endif |
| 1140 |
|
| 1141 |
if (!host) { |
| 1142 |
CTRACE((tfp, "%s: Can't parse `NULL'.\n", this_func)); |
| 1143 |
lynx_nsl_status = HT_INTERNAL; |
| 1144 |
return NULL; |
| 1145 |
} |
| 1146 |
CTRACE((tfp, "%s: parsing `%s'.\n", this_func, host)); |
| 1147 |
|
| 1148 |
/* Could disable this if all our callers already check - kw */ |
| 1149 |
if (HTCheckForInterrupt()) { |
| 1150 |
CTRACE((tfp, "%s: INTERRUPTED for '%s'.\n", this_func, host)); |
| 1151 |
lynx_nsl_status = HT_INTERRUPTED; |
| 1152 |
return NULL; |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (!valid_hostname(host)) { |
| 1156 |
lynx_nsl_status = HT_NOT_ACCEPTABLE; |
| 1157 |
#ifdef NO_RECOVERY |
| 1158 |
#ifdef _WINDOWS |
| 1159 |
WSASetLastError(NO_RECOVERY); |
| 1160 |
#else |
| 1161 |
h_errno = NO_RECOVERY; |
| 1162 |
#endif |
| 1163 |
#endif |
| 1164 |
return NULL; |
| 1165 |
} |
| 1166 |
#ifdef MVS /* Outstanding problem with crash in MVS gethostbyname */ |
| 1167 |
CTRACE((tfp, "%s: Calling gethostbyname(%s)\n", this_func, host)); |
| 1168 |
#endif /* MVS */ |
| 1169 |
|
| 1170 |
CTRACE_FLUSH(tfp); /* so child messages will not mess up parent log */ |
| 1171 |
|
| 1172 |
lynx_nsl_status = HT_INTERNAL; /* should be set to something else below */ |
| 1173 |
|
| 1174 |
#ifdef NSL_FORK |
| 1175 |
if (!setup_nsl_fork(really_gethostbyname, |
| 1176 |
read_hostent, |
| 1177 |
dump_hostent, |
| 1178 |
host, NULL, (void **) &rehostent)) { |
| 1179 |
goto failed; |
| 1180 |
} |
| 1181 |
result_phost = rehostent; |
| 1182 |
#else /* Not NSL_FORK: */ |
| 1183 |
|
| 1184 |
#ifdef _WINDOWS_NSL |
| 1185 |
{ |
| 1186 |
HANDLE hThread; |
| 1187 |
DWORD dwThreadID; |
| 1188 |
|
| 1189 |
#ifndef __CYGWIN__ |
| 1190 |
if (!system_is_NT) { /* for Windows9x */ |
| 1191 |
unsigned long t; |
| 1192 |
|
| 1193 |
t = (unsigned long) inet_addr(host); |
| 1194 |
if (t != INADDR_NONE) |
| 1195 |
gbl_phost = gethostbyaddr((char *) &t, sizeof(t), AF_INET); |
| 1196 |
else |
| 1197 |
gbl_phost = gethostbyname(host); |
| 1198 |
} else { /* for Windows NT */ |
| 1199 |
#endif /* !__CYGWIN__ */ |
| 1200 |
gbl_phost = (LYNX_HOSTENT *) NULL; |
| 1201 |
donelookup = FALSE; |
| 1202 |
|
| 1203 |
#if defined(__CYGWIN__) || defined(USE_WINSOCK2_H) |
| 1204 |
SetLastError(WSAHOST_NOT_FOUND); |
| 1205 |
#else |
| 1206 |
WSASetLastError(WSAHOST_NOT_FOUND); |
| 1207 |
#endif |
| 1208 |
|
| 1209 |
hThread = CreateThread(NULL, 4096UL, _fork_func, host, 0UL, |
| 1210 |
&dwThreadID); |
| 1211 |
if (!hThread) |
| 1212 |
MessageBox(NULL, "CreateThread", |
| 1213 |
"CreateThread Failed", 0L); |
| 1214 |
|
| 1215 |
while (!donelookup) { |
| 1216 |
if (HTCheckForInterrupt()) { |
| 1217 |
/* Note that host is a character array and is not freed */ |
| 1218 |
/* to avoid possible subthread problems: */ |
| 1219 |
if (!CloseHandle(hThread)) { |
| 1220 |
MessageBox((void *) NULL, |
| 1221 |
"CloseHandle", "CloseHandle Failed", 0L); |
| 1222 |
} |
| 1223 |
lynx_nsl_status = HT_INTERRUPTED; |
| 1224 |
return NULL; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
#ifndef __CYGWIN__ |
| 1228 |
} |
| 1229 |
#endif /* !__CYGWIN__ */ |
| 1230 |
if (gbl_phost) { |
| 1231 |
lynx_nsl_status = HT_OK; |
| 1232 |
result_phost = gbl_phost; |
| 1233 |
} else { |
| 1234 |
lynx_nsl_status = HT_ERROR; |
| 1235 |
goto failed; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
#else /* !NSL_FORK, !_WINDOWS_NSL: */ |
| 1240 |
{ |
| 1241 |
LYNX_HOSTENT *phost; |
| 1242 |
|
| 1243 |
phost = gethostbyname(host); /* See netdb.h */ |
| 1244 |
#ifdef MVS |
| 1245 |
CTRACE((tfp, "%s: gethostbyname() returned %d\n", this_func, phost)); |
| 1246 |
#endif /* MVS */ |
| 1247 |
if (phost) { |
| 1248 |
lynx_nsl_status = HT_OK; |
| 1249 |
result_phost = phost; |
| 1250 |
} else { |
| 1251 |
lynx_nsl_status = HT_H_ERRNO_VALID; |
| 1252 |
goto failed; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
#endif /* !NSL_FORK, !_WINDOWS_NSL */ |
| 1256 |
#endif /* !NSL_FORK */ |
| 1257 |
|
| 1258 |
#ifdef DEBUG_HOSTENT |
| 1259 |
dump_hostent(this_func, result_phost); |
| 1260 |
CTRACE((tfp, "%s: Resolved name to a hostent.\n", this_func)); |
| 1261 |
#endif |
| 1262 |
|
| 1263 |
return result_phost; /* OK */ |
| 1264 |
|
| 1265 |
failed: |
| 1266 |
CTRACE((tfp, "%s: Can't find internet node name `%s'.\n", this_func, host)); |
| 1267 |
return NULL; |
| 1268 |
} |
| 1269 |
|
| 1270 |
BOOLEAN LYCheckHostByName(char *host) |
| 1271 |
{ |
| 1272 |
LYNX_HOSTENT *data = LYGetHostByName(host); |
| 1273 |
BOOLEAN result = (data != NULL); |
| 1274 |
|
| 1275 |
FREE_NSL_FORK(data); |
| 1276 |
return result; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* Parse a network node address and port |
| 1280 |
* ------------------------------------- |
| 1281 |
* |
| 1282 |
* On entry, |
| 1283 |
* str points to a string with a node name or number, |
| 1284 |
* with optional trailing colon and port number. |
| 1285 |
* soc_in points to the binary internet or decnet address field. |
| 1286 |
* |
| 1287 |
* On exit, |
| 1288 |
* *soc_in is filled in. If no port is specified in str, that |
| 1289 |
* field is left unchanged in *soc_in. |
| 1290 |
*/ |
| 1291 |
#ifndef INET6 |
| 1292 |
static int HTParseInet(SockA *soc_in, const char *str) |
| 1293 |
{ |
| 1294 |
static const char *this_func = "HTParseInet"; |
| 1295 |
|
| 1296 |
char *port; |
| 1297 |
int dotcount_ip = 0; /* for dotted decimal IP addr */ |
| 1298 |
char *strptr; |
| 1299 |
char *host = NULL; |
| 1300 |
|
| 1301 |
if (!str) { |
| 1302 |
CTRACE((tfp, "%s: Can't parse `NULL'.\n", this_func)); |
| 1303 |
return -1; |
| 1304 |
} |
| 1305 |
CTRACE((tfp, "%s: parsing `%s'.\n", this_func, str)); |
| 1306 |
if (HTCheckForInterrupt()) { |
| 1307 |
CTRACE((tfp, "%s: INTERRUPTED for '%s'.\n", this_func, str)); |
| 1308 |
return -1; |
| 1309 |
} |
| 1310 |
StrAllocCopy(host, str); /* Make a copy we can mutilate */ |
| 1311 |
/* |
| 1312 |
* Parse port number if present. |
| 1313 |
*/ |
| 1314 |
if ((port = StrChr(host, ':')) != NULL) { |
| 1315 |
*port++ = 0; /* Chop off port */ |
| 1316 |
strptr = port; |
| 1317 |
if (port[0] >= '0' && port[0] <= '9') { |
| 1318 |
#ifdef UNIX |
| 1319 |
soc_in->sin_port = (PortNumber) htons(strtol(port, &strptr, 10)); |
| 1320 |
#else /* VMS: */ |
| 1321 |
#ifdef DECNET |
| 1322 |
soc_in->sdn_objnum = (unsigned char) (strtol(port, &strptr, 10)); |
| 1323 |
#else |
| 1324 |
soc_in->sin_port = htons((PortNumber) strtol(port, &strptr, 10)); |
| 1325 |
#endif /* Decnet */ |
| 1326 |
#endif /* Unix vs. VMS */ |
| 1327 |
} |
| 1328 |
if (strptr && *strptr != '\0') { |
| 1329 |
FREE(host); |
| 1330 |
HTAlwaysAlert(NULL, gettext("Address has invalid port")); |
| 1331 |
return -1; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
#ifdef DECNET |
| 1335 |
/* |
| 1336 |
* Read Decnet node name. @@ Should know about DECnet addresses, but it's |
| 1337 |
* probably worth waiting until the Phase transition from IV to V. |
| 1338 |
*/ |
| 1339 |
soc_in->sdn_nam.n_len = min(DN_MAXNAML, strlen(host)); /* <=6 in phase 4 */ |
| 1340 |
StrNCpy(soc_in->sdn_nam.n_name, host, soc_in->sdn_nam.n_len + 1); |
| 1341 |
CTRACE((tfp, |
| 1342 |
"DECnet: Parsed address as object number %d on host %.6s...\n", |
| 1343 |
soc_in->sdn_objnum, host)); |
| 1344 |
#else /* parse Internet host: */ |
| 1345 |
|
| 1346 |
if (*host >= '0' && *host <= '9') { /* Test for numeric node address: */ |
| 1347 |
strptr = host; |
| 1348 |
while (*strptr) { |
| 1349 |
if (*strptr == '.') { |
| 1350 |
dotcount_ip++; |
| 1351 |
} else if (!isdigit(UCH(*strptr))) { |
| 1352 |
break; |
| 1353 |
} |
| 1354 |
strptr++; |
| 1355 |
} |
| 1356 |
if (*strptr) { /* found non-numeric, assume domain name */ |
| 1357 |
dotcount_ip = 0; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
/* |
| 1362 |
* Parse host number if present. |
| 1363 |
*/ |
| 1364 |
if (dotcount_ip == 3) /* Numeric node address: */ |
| 1365 |
{ |
| 1366 |
#ifdef GUSI |
| 1367 |
soc_in->sin_addr = inet_addr(host); /* See netinet/in.h */ |
| 1368 |
#else |
| 1369 |
#ifdef HAVE_INET_ATON |
| 1370 |
if (!inet_aton(host, &(soc_in->sin_addr))) { |
| 1371 |
CTRACE((tfp, "inet_aton(%s) returns error\n", host)); |
| 1372 |
FREE(host); |
| 1373 |
return -1; |
| 1374 |
} |
| 1375 |
#else |
| 1376 |
soc_in->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */ |
| 1377 |
#endif /* HAVE_INET_ATON */ |
| 1378 |
#endif /* GUSI */ |
| 1379 |
FREE(host); |
| 1380 |
} else { /* Alphanumeric node name: */ |
| 1381 |
|
| 1382 |
#ifdef MVS /* Outstanding problem with crash in MVS gethostbyname */ |
| 1383 |
CTRACE((tfp, "%s: Calling LYGetHostByName(%s)\n", this_func, host)); |
| 1384 |
#endif /* MVS */ |
| 1385 |
|
| 1386 |
#ifdef _WINDOWS_NSL |
| 1387 |
gbl_phost = LYGetHostByName(host); /* See above */ |
| 1388 |
if (!gbl_phost) |
| 1389 |
goto failed; |
| 1390 |
MemCpy((void *) &soc_in->sin_addr, gbl_phost->h_addr_list[0], gbl_phost->h_length); |
| 1391 |
FREE(gbl_phost); |
| 1392 |
#else /* !_WINDOWS_NSL */ |
| 1393 |
{ |
| 1394 |
LYNX_HOSTENT *phost; |
| 1395 |
|
| 1396 |
phost = LYGetHostByName(host); /* See above */ |
| 1397 |
|
| 1398 |
if (!phost) |
| 1399 |
goto failed; |
| 1400 |
if (phost->h_length != sizeof soc_in->sin_addr) { |
| 1401 |
HTAlwaysAlert(host, gettext("Address length looks invalid")); |
| 1402 |
} |
| 1403 |
MemCpy((void *) &soc_in->sin_addr, phost->h_addr_list[0], phost->h_length); |
| 1404 |
FREE_NSL_FORK(phost); |
| 1405 |
} |
| 1406 |
#endif /* _WINDOWS_NSL */ |
| 1407 |
|
| 1408 |
FREE(host); |
| 1409 |
} /* Alphanumeric node name */ |
| 1410 |
|
| 1411 |
CTRACE((tfp, |
| 1412 |
"%s: Parsed address as port %d, IP address %d.%d.%d.%d\n", |
| 1413 |
this_func, |
| 1414 |
(int) ntohs(soc_in->sin_port), |
| 1415 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 0), |
| 1416 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 1), |
| 1417 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 2), |
| 1418 |
(int) *((unsigned char *) (&soc_in->sin_addr) + 3))); |
| 1419 |
#endif /* Internet vs. Decnet */ |
| 1420 |
|
| 1421 |
return 0; /* OK */ |
| 1422 |
|
| 1423 |
failed: |
| 1424 |
CTRACE((tfp, "%s: Can't find internet node name `%s'.\n", |
| 1425 |
this_func, host)); |
| 1426 |
FREE(host); |
| 1427 |
switch (lynx_nsl_status) { |
| 1428 |
case HT_NOT_ACCEPTABLE: |
| 1429 |
case HT_INTERRUPTED: |
| 1430 |
return lynx_nsl_status; |
| 1431 |
default: |
| 1432 |
return -1; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
#endif /* !INET6 */ |
| 1436 |
|
| 1437 |
#ifdef INET6 |
| 1438 |
|
| 1439 |
static void dump_addrinfo(const char *tag, const void *data) |
| 1440 |
{ |
| 1441 |
const LYNX_ADDRINFO *res; |
| 1442 |
int count = 0; |
| 1443 |
|
| 1444 |
CTRACE((tfp, "dump_addrinfo %s:\n", tag)); |
| 1445 |
for (res = (const LYNX_ADDRINFO *) data; res; res = res->ai_next) { |
| 1446 |
char hostbuf[1024], portbuf[1024]; |
| 1447 |
|
| 1448 |
++count; |
| 1449 |
hostbuf[0] = '\0'; |
| 1450 |
portbuf[0] = '\0'; |
| 1451 |
getnameinfo(res->ai_addr, res->ai_addrlen, |
| 1452 |
hostbuf, (socklen_t) sizeof(hostbuf), |
| 1453 |
portbuf, (socklen_t) sizeof(portbuf), |
| 1454 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 1455 |
|
| 1456 |
CTRACE((tfp, |
| 1457 |
"\t[%d] family %d, socktype %d, protocol %d addr %s port %s\n", |
| 1458 |
count, |
| 1459 |
res->ai_family, |
| 1460 |
res->ai_socktype, |
| 1461 |
res->ai_protocol, |
| 1462 |
hostbuf, |
| 1463 |
portbuf)); |
| 1464 |
} |
| 1465 |
} |
| 1466 |
|
| 1467 |
#if defined(NSL_FORK) |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* Copy the relevant information (on the child-side). |
| 1471 |
*/ |
| 1472 |
static size_t fill_addrinfo(void **buffer, |
| 1473 |
const LYNX_ADDRINFO *phost) |
| 1474 |
{ |
| 1475 |
static const char *this_func = "fill_addinfo"; |
| 1476 |
|
| 1477 |
const LYNX_ADDRINFO *q; |
| 1478 |
LYNX_ADDRINFO *actual; |
| 1479 |
LYNX_ADDRINFO *result; |
| 1480 |
int count = 0; |
| 1481 |
int limit = 0; |
| 1482 |
size_t need = sizeof(LYNX_ADDRINFO); |
| 1483 |
char *heap; |
| 1484 |
|
| 1485 |
CTRACE((tfp, "filladdr_info %p\n", (const void *) phost)); |
| 1486 |
for (q = phost; q != NULL; q = q->ai_next) { |
| 1487 |
++limit; |
| 1488 |
need += q->ai_addrlen; |
| 1489 |
need += sizeof(LYNX_ADDRINFO); |
| 1490 |
} |
| 1491 |
CTRACE((tfp, "...fill_addrinfo %d:%lu\n", limit, (unsigned long) need)); |
| 1492 |
|
| 1493 |
if ((result = (LYNX_ADDRINFO *) calloc(1, need)) == NULL) |
| 1494 |
outofmem(__FILE__, this_func); |
| 1495 |
|
| 1496 |
*buffer = actual = result; |
| 1497 |
heap = ((char *) actual) + ((size_t) limit * sizeof(LYNX_ADDRINFO)); |
| 1498 |
|
| 1499 |
for (count = 0; count < limit; ++count) { |
| 1500 |
|
| 1501 |
/* |
| 1502 |
* copying the whole structure seems simpler but because it is not |
| 1503 |
* packed, uninitialized gaps make it hard to analyse with valgrind. |
| 1504 |
*/ |
| 1505 |
/* *INDENT-EQLS* */ |
| 1506 |
actual->ai_flags = phost->ai_flags; |
| 1507 |
actual->ai_family = phost->ai_family; |
| 1508 |
actual->ai_socktype = phost->ai_socktype; |
| 1509 |
actual->ai_protocol = phost->ai_protocol; |
| 1510 |
actual->ai_addrlen = phost->ai_addrlen; |
| 1511 |
actual->ai_addr = (struct sockaddr *) (void *) heap; |
| 1512 |
|
| 1513 |
MemCpy(heap, phost->ai_addr, phost->ai_addrlen); |
| 1514 |
heap += phost->ai_addrlen; |
| 1515 |
|
| 1516 |
phost = phost->ai_next; |
| 1517 |
|
| 1518 |
actual->ai_next = ((count + 1 < limit) |
| 1519 |
? (actual + 1) |
| 1520 |
: NULL); |
| 1521 |
++actual; |
| 1522 |
} |
| 1523 |
return (size_t) (heap - (char *) result); |
| 1524 |
} |
| 1525 |
|
| 1526 |
/* |
| 1527 |
* Read data, repair pointers as done in fill_addrinfo(). |
| 1528 |
*/ |
| 1529 |
static unsigned read_addrinfo(int fd, char *buffer, size_t length) |
| 1530 |
{ |
| 1531 |
unsigned result = read_bytes(fd, buffer, length); |
| 1532 |
LYNX_ADDRINFO *actual = (LYNX_ADDRINFO *) (void *) buffer; |
| 1533 |
LYNX_ADDRINFO *res; |
| 1534 |
int count = 0; |
| 1535 |
int limit; |
| 1536 |
char *heap; |
| 1537 |
|
| 1538 |
CTRACE((tfp, "read_addrinfo length %lu\n", (unsigned long) length)); |
| 1539 |
for (limit = 0; actual[limit].ai_next; ++limit) { |
| 1540 |
} |
| 1541 |
++limit; |
| 1542 |
heap = (char *) (actual + limit); |
| 1543 |
CTRACE((tfp, "...read_addrinfo %d items\n", limit)); |
| 1544 |
|
| 1545 |
for (res = actual, count = 0; count < limit; ++count) { |
| 1546 |
res->ai_addr = (struct sockaddr *) (void *) heap; |
| 1547 |
heap += res->ai_addrlen; |
| 1548 |
if (count < limit - 1) { |
| 1549 |
res->ai_next = (res + 1); |
| 1550 |
++res; |
| 1551 |
} else { |
| 1552 |
res->ai_next = NULL; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
|
| 1556 |
#ifdef DEBUG_HOSTENT |
| 1557 |
dump_addrinfo("read_addrinfo", buffer); |
| 1558 |
#endif |
| 1559 |
return result; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* |
| 1563 |
* This is called via the child-side of the fork. |
| 1564 |
*/ |
| 1565 |
static void really_getaddrinfo(const char *host, |
| 1566 |
const char *port, |
| 1567 |
STATUSES * statuses, |
| 1568 |
void **result) |
| 1569 |
{ |
| 1570 |
LYNX_ADDRINFO hints, *res = NULL; |
| 1571 |
int error; |
| 1572 |
|
| 1573 |
memset(&hints, 0, sizeof(hints)); |
| 1574 |
hints.ai_family = PF_UNSPEC; |
| 1575 |
hints.ai_socktype = SOCK_STREAM; |
| 1576 |
error = getaddrinfo(host, port, &hints, &res); |
| 1577 |
if (error) { |
| 1578 |
CTRACE((tfp, "HTGetAddrInfo: getaddrinfo(%s, %s): %s\n", host, port, |
| 1579 |
gai_strerror(error))); |
| 1580 |
} else { |
| 1581 |
statuses->child_errno = errno; |
| 1582 |
statuses->child_h_errno = h_errno; |
| 1583 |
#ifdef HAVE_H_ERRNO |
| 1584 |
statuses->h_errno_valid = YES; |
| 1585 |
#endif |
| 1586 |
|
| 1587 |
#ifdef DEBUG_HOSTENT_CHILD |
| 1588 |
dump_addrinfo("CHILD getaddrinfo", res); |
| 1589 |
#endif |
| 1590 |
statuses->rehostentlen = fill_addrinfo(result, res); |
| 1591 |
#ifdef DEBUG_HOSTENT_CHILD |
| 1592 |
dump_addrinfo("CHILD fill_addrinfo", (const LYNX_ADDRINFO *) (*result)); |
| 1593 |
#endif |
| 1594 |
if (statuses->rehostentlen <= sizeof(LYNX_ADDRINFO) || (*result) == NULL) { |
| 1595 |
statuses->rehostentlen = 0; |
| 1596 |
statuses->h_length = 0; |
| 1597 |
} else { |
| 1598 |
statuses->h_length = (int) (((LYNX_ADDRINFO *) (*result))->ai_addrlen); |
| 1599 |
} |
| 1600 |
freeaddrinfo(res); |
| 1601 |
} |
| 1602 |
} |
| 1603 |
#endif /* NSL_FORK */ |
| 1604 |
|
| 1605 |
static LYNX_ADDRINFO *HTGetAddrInfo(const char *str, |
| 1606 |
const int defport) |
| 1607 |
{ |
| 1608 |
#ifdef NSL_FORK |
| 1609 |
/* for transfer of result between from child to parent: */ |
| 1610 |
void *readdrinfo = NULL; |
| 1611 |
|
| 1612 |
#else |
| 1613 |
LYNX_ADDRINFO hints; |
| 1614 |
int error; |
| 1615 |
#endif /* NSL_FORK */ |
| 1616 |
LYNX_ADDRINFO *res; |
| 1617 |
char *p; |
| 1618 |
char *s = NULL; |
| 1619 |
char *host, *port; |
| 1620 |
char pbuf[80]; |
| 1621 |
|
| 1622 |
StrAllocCopy(s, str); |
| 1623 |
|
| 1624 |
if (s[0] == '[' && (p = StrChr(s, ']')) != NULL) { |
| 1625 |
*p++ = '\0'; |
| 1626 |
host = s + 1; |
| 1627 |
} else { |
| 1628 |
p = s; |
| 1629 |
host = &s[0]; |
| 1630 |
} |
| 1631 |
port = strrchr(p, ':'); |
| 1632 |
if (port) { |
| 1633 |
*port++ = '\0'; |
| 1634 |
} else { |
| 1635 |
sprintf(pbuf, "%d", defport); |
| 1636 |
port = pbuf; |
| 1637 |
} |
| 1638 |
|
| 1639 |
#ifdef NSL_FORK |
| 1640 |
if (setup_nsl_fork(really_getaddrinfo, |
| 1641 |
read_addrinfo, |
| 1642 |
dump_addrinfo, |
| 1643 |
host, port, &readdrinfo)) { |
| 1644 |
res = readdrinfo; |
| 1645 |
} else { |
| 1646 |
res = NULL; |
| 1647 |
} |
| 1648 |
#else |
| 1649 |
memset(&hints, 0, sizeof(hints)); |
| 1650 |
hints.ai_family = PF_UNSPEC; |
| 1651 |
hints.ai_socktype = SOCK_STREAM; |
| 1652 |
error = getaddrinfo(host, port, &hints, &res); |
| 1653 |
if (error || !res) { |
| 1654 |
CTRACE((tfp, "HTGetAddrInfo: getaddrinfo(%s, %s): %s\n", host, port, |
| 1655 |
gai_strerror(error))); |
| 1656 |
res = NULL; |
| 1657 |
} |
| 1658 |
#endif |
| 1659 |
|
| 1660 |
free(s); |
| 1661 |
#ifdef DEBUG_HOSTENT |
| 1662 |
dump_addrinfo("HTGetAddrInfo", res); |
| 1663 |
#endif |
| 1664 |
return res; |
| 1665 |
} |
| 1666 |
|
| 1667 |
BOOLEAN HTCheckAddrInfo(const char *str, const int defport) |
| 1668 |
{ |
| 1669 |
LYNX_ADDRINFO *data = HTGetAddrInfo(str, defport); |
| 1670 |
BOOLEAN result = (data != NULL); |
| 1671 |
|
| 1672 |
FREE_NSL_FORK(data); |
| 1673 |
return result; |
| 1674 |
} |
| 1675 |
#endif /* INET6 */ |
| 1676 |
|
| 1677 |
#ifdef LY_FIND_LEAKS |
| 1678 |
/* Free our name for the host on which we are - FM |
| 1679 |
* ------------------------------------------- |
| 1680 |
* |
| 1681 |
*/ |
| 1682 |
static void free_HTTCP_hostname(void) |
| 1683 |
{ |
| 1684 |
FREE(hostname); |
| 1685 |
} |
| 1686 |
#endif /* LY_FIND_LEAKS */ |
| 1687 |
|
| 1688 |
/* Derive the name of the host on which we are |
| 1689 |
* ------------------------------------------- |
| 1690 |
* |
| 1691 |
*/ |
| 1692 |
static void get_host_details(void) |
| 1693 |
{ |
| 1694 |
char name[MAXHOSTNAMELEN + 1]; /* The name of this host */ |
| 1695 |
|
| 1696 |
#ifdef UCX |
| 1697 |
char *domain_name; /* The name of this host domain */ |
| 1698 |
#endif /* UCX */ |
| 1699 |
#ifdef NEED_HOST_ADDRESS /* no -- needs name server! */ |
| 1700 |
#ifdef INET6 |
| 1701 |
LYNX_ADDRINFO hints, *res; |
| 1702 |
int error; |
| 1703 |
|
| 1704 |
#else |
| 1705 |
LYNX_HOSTENT *phost; /* Pointer to host -- See netdb.h */ |
| 1706 |
#endif /* INET6 */ |
| 1707 |
#endif /* NEED_HOST_ADDRESS */ |
| 1708 |
size_t namelength = sizeof(name); |
| 1709 |
|
| 1710 |
if (hostname) |
| 1711 |
return; /* Already done */ |
| 1712 |
gethostname(name, namelength); /* Without domain */ |
| 1713 |
StrAllocCopy(hostname, name); |
| 1714 |
#ifdef LY_FIND_LEAKS |
| 1715 |
atexit(free_HTTCP_hostname); |
| 1716 |
#endif |
| 1717 |
#ifdef UCX |
| 1718 |
/* |
| 1719 |
* UCX doesn't give the complete domain name. Get rest from UCX$BIND_DOM |
| 1720 |
* logical. |
| 1721 |
*/ |
| 1722 |
if (StrChr(hostname, '.') == NULL) { /* Not full address */ |
| 1723 |
domain_name = LYGetEnv("UCX$BIND_DOMAIN"); |
| 1724 |
if (domain_name == NULL) |
| 1725 |
domain_name = LYGetEnv("TCPIP$BIND_DOMAIN"); |
| 1726 |
if (domain_name != NULL) { |
| 1727 |
StrAllocCat(hostname, "."); |
| 1728 |
StrAllocCat(hostname, domain_name); |
| 1729 |
} |
| 1730 |
} |
| 1731 |
#endif /* UCX */ |
| 1732 |
CTRACE((tfp, "TCP: Local host name is %s\n", hostname)); |
| 1733 |
|
| 1734 |
#ifndef DECNET /* Decnet ain't got no damn name server 8#OO */ |
| 1735 |
#ifdef NEED_HOST_ADDRESS /* no -- needs name server! */ |
| 1736 |
#ifdef INET6 |
| 1737 |
memset(&hints, 0, sizeof(hints)); |
| 1738 |
hints.ai_family = PF_UNSPEC; |
| 1739 |
hints.ai_socktype = SOCK_STREAM; |
| 1740 |
hints.ai_flags = AI_CANONNAME; |
| 1741 |
error = getaddrinfo(name, NULL, &hints, &res); |
| 1742 |
if (error || !res || !res->ai_canonname) { |
| 1743 |
CTRACE((tfp, "TCP: %s: `%s'\n", gai_strerror(error), name)); |
| 1744 |
if (res) |
| 1745 |
freeaddrinfo(res); |
| 1746 |
return; /* Fail! */ |
| 1747 |
} |
| 1748 |
StrAllocCopy(hostname, res->ai_canonname); |
| 1749 |
MemCpy(&HTHostAddress, res->ai_addr, res->ai_addrlen); |
| 1750 |
freeaddrinfo(res); |
| 1751 |
#else |
| 1752 |
phost = gethostbyname(name); /* See netdb.h */ |
| 1753 |
if (!OK_HOST(phost)) { |
| 1754 |
CTRACE((tfp, |
| 1755 |
"TCP: Can't find my own internet node address for `%s'!!\n", |
| 1756 |
name)); |
| 1757 |
return; /* Fail! */ |
| 1758 |
} |
| 1759 |
StrAllocCopy(hostname, phost->h_name); |
| 1760 |
MemCpy(&HTHostAddress, &phost->h_addr_list[0], phost->h_length); |
| 1761 |
#endif /* INET6 */ |
| 1762 |
CTRACE((tfp, " Name server says that I am `%s' = %s\n", |
| 1763 |
hostname, HTInetString(&HTHostAddress))); |
| 1764 |
#endif /* NEED_HOST_ADDRESS */ |
| 1765 |
|
| 1766 |
#endif /* !DECNET */ |
| 1767 |
} |
| 1768 |
|
| 1769 |
const char *HTHostName(void) |
| 1770 |
{ |
| 1771 |
get_host_details(); |
| 1772 |
return hostname; |
| 1773 |
} |
| 1774 |
|
| 1775 |
#ifdef _WINDOWS |
| 1776 |
#define SET_EINTR WSASetLastError(EINTR) |
| 1777 |
#else |
| 1778 |
#define SET_EINTR SOCKET_ERRNO = EINTR |
| 1779 |
#endif |
| 1780 |
|
| 1781 |
static BOOL HTWasInterrupted(int *status) |
| 1782 |
{ |
| 1783 |
BOOL result = FALSE; |
| 1784 |
|
| 1785 |
if (HTCheckForInterrupt()) { |
| 1786 |
result = TRUE; |
| 1787 |
*status = HT_INTERRUPTED; |
| 1788 |
SET_EINTR; |
| 1789 |
} |
| 1790 |
return result; |
| 1791 |
} |
| 1792 |
|
| 1793 |
#define TRIES_PER_SECOND 10 |
| 1794 |
|
| 1795 |
/* |
| 1796 |
* Set the select-timeout to 0.1 seconds. |
| 1797 |
*/ |
| 1798 |
static void set_timeout(struct timeval *timeoutp) |
| 1799 |
{ |
| 1800 |
timeoutp->tv_sec = 0; |
| 1801 |
timeoutp->tv_usec = 100000; |
| 1802 |
} |
| 1803 |
|
| 1804 |
#ifndef MULTINET /* SOCKET_ERRNO != errno ? */ |
| 1805 |
#if !defined(UCX) || !defined(VAXC) /* errno not modifiable ? */ |
| 1806 |
#define SOCKET_DEBUG_TRACE /* show errno status after some system calls */ |
| 1807 |
#endif /* UCX && VAXC */ |
| 1808 |
#endif /* MULTINET */ |
| 1809 |
/* |
| 1810 |
* Interruptible connect as implemented for Mosaic by Marc Andreessen |
| 1811 |
* and hacked in for Lynx years ago by Lou Montulli, and further |
| 1812 |
* modified over the years by numerous Lynx lovers. - FM |
| 1813 |
*/ |
| 1814 |
int HTDoConnect(const char *url, |
| 1815 |
const char *protocol, |
| 1816 |
int default_port, |
| 1817 |
int *s) |
| 1818 |
{ |
| 1819 |
char *socks5_host = NULL; |
| 1820 |
unsigned socks5_host_len = 0; |
| 1821 |
int socks5_port; |
| 1822 |
const char *socks5_orig_url; |
| 1823 |
char *socks5_new_url = NULL; |
| 1824 |
char *socks5_protocol = NULL; |
| 1825 |
int status = HT_OK; |
| 1826 |
char *line = NULL; |
| 1827 |
char *p1 = NULL; |
| 1828 |
char *host = NULL; |
| 1829 |
char const *emsg; |
| 1830 |
|
| 1831 |
#ifdef INET6 |
| 1832 |
LYNX_ADDRINFO *res = NULL, *res0 = NULL; |
| 1833 |
|
| 1834 |
#else |
| 1835 |
struct sockaddr_in sock_A; |
| 1836 |
struct sockaddr_in *soc_in = &sock_A; |
| 1837 |
#endif |
| 1838 |
|
| 1839 |
*s = -1; /* nothing is open yet */ |
| 1840 |
|
| 1841 |
/* In case of a present SOCKS5 proxy, marshal */ |
| 1842 |
if (socks5_proxy == NULL) |
| 1843 |
socks5_proxy = LYGetEnv("SOCKS5_PROXY"); |
| 1844 |
if ((socks5_orig_url = socks5_proxy) != NULL) { |
| 1845 |
int xport; |
| 1846 |
|
| 1847 |
xport = default_port; |
| 1848 |
socks5_orig_url = url; |
| 1849 |
StrAllocCopy(socks5_new_url, url); |
| 1850 |
|
| 1851 |
/* Get node name and optional port number of wanted URL */ |
| 1852 |
if ((p1 = HTParse(socks5_new_url, "", PARSE_HOST)) != NULL) { |
| 1853 |
StrAllocCopy(socks5_host, p1); |
| 1854 |
strip_userid(socks5_host, FALSE); |
| 1855 |
FREE(p1); |
| 1856 |
} |
| 1857 |
|
| 1858 |
if (isEmpty(socks5_host)) { |
| 1859 |
emsg = gettext("SOCKS5: no hostname found."); |
| 1860 |
status = HT_ERROR; |
| 1861 |
goto report_error; |
| 1862 |
} |
| 1863 |
|
| 1864 |
if (strlen(socks5_host) > 255) { |
| 1865 |
emsg = gettext("SOCKS5: hostname too long."); |
| 1866 |
status = HT_ERROR; |
| 1867 |
goto report_error; |
| 1868 |
} |
| 1869 |
socks5_host_len = (unsigned) strlen(socks5_host); |
| 1870 |
|
| 1871 |
if (HTParsePort(socks5_new_url, &socks5_port) == NULL) |
| 1872 |
socks5_port = xport; |
| 1873 |
FREE(socks5_new_url); |
| 1874 |
|
| 1875 |
/* And switch over to our SOCKS5 config; in order to embed that into |
| 1876 |
* lynx environment, prepend protocol prefix */ |
| 1877 |
default_port = 1080; /* RFC 1928 */ |
| 1878 |
HTSACat(&socks5_new_url, "socks://"); |
| 1879 |
HTSACat(&socks5_new_url, socks5_proxy); |
| 1880 |
url = socks5_new_url; |
| 1881 |
|
| 1882 |
HTSprintf0(&socks5_protocol, |
| 1883 |
LY_MSG("(for %s at %s) SOCKS5"), |
| 1884 |
protocol, socks5_host); |
| 1885 |
protocol = socks5_protocol; |
| 1886 |
} |
| 1887 |
#ifndef INET6 |
| 1888 |
/* |
| 1889 |
* Set up defaults. |
| 1890 |
*/ |
| 1891 |
memset(soc_in, 0, sizeof(*soc_in)); |
| 1892 |
soc_in->sin_family = AF_INET; |
| 1893 |
soc_in->sin_port = htons((PortNumber) default_port); |
| 1894 |
#endif /* INET6 */ |
| 1895 |
|
| 1896 |
/* |
| 1897 |
* Get node name and optional port number. |
| 1898 |
*/ |
| 1899 |
p1 = HTParse(url, "", PARSE_HOST); |
| 1900 |
StrAllocCopy(host, p1); |
| 1901 |
strip_userid(host, FALSE); |
| 1902 |
FREE(p1); |
| 1903 |
|
| 1904 |
HTSprintf0(&line, "%s%s", WWW_FIND_MESSAGE, host); |
| 1905 |
_HTProgress(line); |
| 1906 |
#ifdef INET6 |
| 1907 |
/* HTParseInet() is useless! */ |
| 1908 |
res0 = HTGetAddrInfo(host, default_port); |
| 1909 |
if (res0 == NULL) { |
| 1910 |
HTSprintf0(&line, LY_MSG("Unable to locate remote host %s."), host); |
| 1911 |
_HTProgress(line); |
| 1912 |
status = HT_NO_DATA; |
| 1913 |
goto cleanup; |
| 1914 |
} |
| 1915 |
#else |
| 1916 |
status = HTParseInet(soc_in, host); |
| 1917 |
if (status) { |
| 1918 |
if (status != HT_INTERRUPTED) { |
| 1919 |
if (status == HT_NOT_ACCEPTABLE) { |
| 1920 |
/* Not HTProgress, so warning won't be overwritten immediately; |
| 1921 |
* but not HTAlert, because typically there will be other |
| 1922 |
* alerts from the callers. - kw |
| 1923 |
*/ |
| 1924 |
HTUserMsg2(LY_MSG("Invalid hostname %s"), host); |
| 1925 |
} else { |
| 1926 |
HTSprintf0(&line, |
| 1927 |
LY_MSG("Unable to locate remote host %s."), host); |
| 1928 |
_HTProgress(line); |
| 1929 |
} |
| 1930 |
status = HT_NO_DATA; |
| 1931 |
} |
| 1932 |
goto cleanup; |
| 1933 |
} |
| 1934 |
#endif /* INET6 */ |
| 1935 |
|
| 1936 |
HTSprintf0(&line, LY_MSG("Making %s connection to %s"), protocol, host); |
| 1937 |
_HTProgress(line); |
| 1938 |
|
| 1939 |
/* |
| 1940 |
* Now, let's get a socket set up from the server for the data. |
| 1941 |
*/ |
| 1942 |
#ifndef INET6 |
| 1943 |
*s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1944 |
if (*s == -1) { |
| 1945 |
status = HT_NO_DATA; |
| 1946 |
emsg = gettext("socket failed."); |
| 1947 |
goto report_error; |
| 1948 |
} |
| 1949 |
#else |
| 1950 |
for (res = res0; res; res = res->ai_next) { |
| 1951 |
*s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 1952 |
if (*s == -1) { |
| 1953 |
char hostbuf[1024], portbuf[1024]; |
| 1954 |
|
| 1955 |
getnameinfo(res->ai_addr, res->ai_addrlen, |
| 1956 |
hostbuf, (socklen_t) sizeof(hostbuf), |
| 1957 |
portbuf, (socklen_t) sizeof(portbuf), |
| 1958 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 1959 |
HTSprintf0(&line, |
| 1960 |
LY_MSG("socket failed: family %d addr %s port %s."), |
| 1961 |
res->ai_family, hostbuf, portbuf); |
| 1962 |
_HTProgress(line); |
| 1963 |
continue; |
| 1964 |
} |
| 1965 |
#endif /* INET6 */ |
| 1966 |
|
| 1967 |
#if !defined(DOSPATH) || defined(__DJGPP__) |
| 1968 |
#if !defined(NO_IOCTL) || defined(USE_FCNTL) |
| 1969 |
/* |
| 1970 |
* Make the socket non-blocking, so the connect can be canceled. This |
| 1971 |
* means that when we issue the connect we should NOT have to wait for |
| 1972 |
* the accept on the other end. |
| 1973 |
*/ |
| 1974 |
{ |
| 1975 |
#ifdef USE_FCNTL |
| 1976 |
int ret = fcntl(*s, F_SETFL, O_NONBLOCK); |
| 1977 |
|
| 1978 |
#else |
| 1979 |
int val = 1; |
| 1980 |
int ret = IOCTL(*s, FIONBIO, &val); |
| 1981 |
#endif /* USE_FCNTL */ |
| 1982 |
if (ret == -1) |
| 1983 |
_HTProgress(gettext("Could not make connection non-blocking.")); |
| 1984 |
} |
| 1985 |
#endif /* !NO_IOCTL || USE_FCNTL */ |
| 1986 |
#endif /* !DOSPATH || __DJGPP__ */ |
| 1987 |
|
| 1988 |
/* |
| 1989 |
* Issue the connect. Since the server can't do an instantaneous |
| 1990 |
* accept and we are non-blocking, this will almost certainly return a |
| 1991 |
* negative status. |
| 1992 |
*/ |
| 1993 |
#ifdef SOCKS |
| 1994 |
if (socks_flag) { |
| 1995 |
#ifdef INET6 |
| 1996 |
status = Rconnect(*s, res->ai_addr, res->ai_addrlen); |
| 1997 |
#else |
| 1998 |
status = Rconnect(*s, SOCKADDR_OF(sock_A), sizeof(sock_A)); |
| 1999 |
#endif /* INET6 */ |
| 2000 |
} else |
| 2001 |
#endif /* SOCKS */ |
| 2002 |
#ifdef INET6 |
| 2003 |
status = connect(*s, res->ai_addr, res->ai_addrlen); |
| 2004 |
#else |
| 2005 |
status = connect(*s, SOCKADDR_OF(sock_A), sizeof(sock_A)); |
| 2006 |
#endif /* INET6 */ |
| 2007 |
|
| 2008 |
/* |
| 2009 |
* According to the Sun man page for connect: |
| 2010 |
* EINPROGRESS The socket is non-blocking and the con- |
| 2011 |
* nection cannot be completed immediately. |
| 2012 |
* It is possible to select(2) for comple- |
| 2013 |
* tion by selecting the socket for writ- |
| 2014 |
* ing. |
| 2015 |
* According to the Motorola SVR4 man page for connect: |
| 2016 |
* EAGAIN The socket is non-blocking and the con- |
| 2017 |
* nection cannot be completed immediately. |
| 2018 |
* It is possible to select for completion |
| 2019 |
* by selecting the socket for writing. |
| 2020 |
* However, this is only possible if the |
| 2021 |
* socket STREAMS module is the topmost |
| 2022 |
* module on the protocol stack with a |
| 2023 |
* write service procedure. This will be |
| 2024 |
* the normal case. |
| 2025 |
*/ |
| 2026 |
CTRACE((tfp, "connect(): status: %d, SOCK_ERRNO: %d\n", status, SOCKET_ERRNO)); |
| 2027 |
|
| 2028 |
if ((status < 0) && |
| 2029 |
(SOCKET_ERRNO == EINPROGRESS |
| 2030 |
#ifdef EAGAIN |
| 2031 |
|| SOCKET_ERRNO == EAGAIN |
| 2032 |
#endif |
| 2033 |
)) { |
| 2034 |
struct timeval select_timeout; |
| 2035 |
int ret; |
| 2036 |
int tries = 0; |
| 2037 |
|
| 2038 |
#ifdef SOCKET_DEBUG_TRACE |
| 2039 |
if (SOCKET_ERRNO != EINPROGRESS) { |
| 2040 |
HTInetStatus("this socket's first connect"); |
| 2041 |
} |
| 2042 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2043 |
ret = 0; |
| 2044 |
while (ret <= 0) { |
| 2045 |
fd_set writefds; |
| 2046 |
|
| 2047 |
/* |
| 2048 |
* Protect against an infinite loop. |
| 2049 |
*/ |
| 2050 |
if ((tries++ / TRIES_PER_SECOND) >= connect_timeout) { |
| 2051 |
HTAlert(gettext("Connection failed (too many retries).")); |
| 2052 |
#ifdef INET6 |
| 2053 |
#ifndef NSL_FORK |
| 2054 |
if (res0) |
| 2055 |
freeaddrinfo(res0); |
| 2056 |
#endif |
| 2057 |
#endif /* INET6 */ |
| 2058 |
status = HT_NO_DATA; |
| 2059 |
goto cleanup; |
| 2060 |
} |
| 2061 |
set_timeout(&select_timeout); |
| 2062 |
FD_ZERO(&writefds); |
| 2063 |
FD_SET((LYNX_FD) *s, &writefds); |
| 2064 |
#ifdef SOCKS |
| 2065 |
if (socks_flag) |
| 2066 |
ret = Rselect(*s + 1, NULL, |
| 2067 |
&writefds, NULL, &select_timeout); |
| 2068 |
else |
| 2069 |
#endif /* SOCKS */ |
| 2070 |
ret = select(*s + 1, |
| 2071 |
NULL, |
| 2072 |
&writefds, |
| 2073 |
NULL, |
| 2074 |
&select_timeout); |
| 2075 |
|
| 2076 |
#ifdef SOCKET_DEBUG_TRACE |
| 2077 |
if (tries == 1) { |
| 2078 |
if (SOCKET_ERRNO != EINPROGRESS) { |
| 2079 |
HTInetStatus("this socket's first select"); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2083 |
/* |
| 2084 |
* If we suspend, then it is possible that select will be |
| 2085 |
* interrupted. Allow for this possibility. - JED |
| 2086 |
*/ |
| 2087 |
if ((ret == -1) && (SOCKET_ERRNO == EINTR)) |
| 2088 |
continue; |
| 2089 |
|
| 2090 |
#ifdef SOCKET_DEBUG_TRACE |
| 2091 |
if (ret < 0) { |
| 2092 |
HTInetStatus("failed select"); |
| 2093 |
} |
| 2094 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2095 |
/* |
| 2096 |
* Again according to the Sun and Motorola man pages for |
| 2097 |
* connect: |
| 2098 |
* EALREADY The socket is non-blocking and a previ- |
| 2099 |
* ous connection attempt has not yet been |
| 2100 |
* completed. |
| 2101 |
* Thus if the SOCKET_ERRNO is NOT EALREADY we have a real |
| 2102 |
* error, and should break out here and return that error. |
| 2103 |
* Otherwise if it is EALREADY keep on trying to complete the |
| 2104 |
* connection. |
| 2105 |
*/ |
| 2106 |
if ((ret < 0) && (SOCKET_ERRNO != EALREADY)) { |
| 2107 |
status = ret; |
| 2108 |
break; |
| 2109 |
} else if (((SOCKET_ERRNO == EALREADY) || |
| 2110 |
(SOCKET_ERRNO == EINPROGRESS)) && |
| 2111 |
HTCheckForInterrupt()) { |
| 2112 |
status = HT_INTERRUPTED; |
| 2113 |
break; |
| 2114 |
} else if (ret > 0) { |
| 2115 |
/* |
| 2116 |
* Extra check here for connection success, if we try to |
| 2117 |
* connect again, and get EISCONN, it means we have a |
| 2118 |
* successful connection. But don't check with SOCKS. |
| 2119 |
*/ |
| 2120 |
#ifdef SOCKS |
| 2121 |
if (socks_flag) { |
| 2122 |
status = 0; |
| 2123 |
} else { |
| 2124 |
#endif /* SOCKS */ |
| 2125 |
#ifdef INET6 |
| 2126 |
status = connect(*s, res->ai_addr, res->ai_addrlen); |
| 2127 |
#else |
| 2128 |
status = connect(*s, SOCKADDR_OF(sock_A), sizeof(sock_A)); |
| 2129 |
#endif /* INET6 */ |
| 2130 |
#ifdef UCX |
| 2131 |
/* |
| 2132 |
* A UCX feature: Instead of returning EISCONN UCX |
| 2133 |
* returns EADDRINUSE. Test for this status also. |
| 2134 |
*/ |
| 2135 |
if ((status < 0) && ((SOCKET_ERRNO == EISCONN) || |
| 2136 |
(SOCKET_ERRNO == EADDRINUSE))) |
| 2137 |
#else |
| 2138 |
if ((status < 0) && (SOCKET_ERRNO == EISCONN)) |
| 2139 |
#endif /* UCX */ |
| 2140 |
{ |
| 2141 |
status = 0; |
| 2142 |
} |
| 2143 |
|
| 2144 |
if (status && (SOCKET_ERRNO == EALREADY)) /* new stuff LJM */ |
| 2145 |
ret = 0; /* keep going */ |
| 2146 |
else { |
| 2147 |
#ifdef SOCKET_DEBUG_TRACE |
| 2148 |
if (status < 0) { |
| 2149 |
HTInetStatus("confirm-ready connect"); |
| 2150 |
} |
| 2151 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2152 |
break; |
| 2153 |
} |
| 2154 |
#ifdef SOCKS |
| 2155 |
} |
| 2156 |
#endif /* SOCKS */ |
| 2157 |
} |
| 2158 |
#ifdef SOCKS |
| 2159 |
else if (!socks_flag) |
| 2160 |
#else |
| 2161 |
else |
| 2162 |
#endif /* SOCKS */ |
| 2163 |
{ |
| 2164 |
/* |
| 2165 |
* The select says we aren't ready yet. Try to connect |
| 2166 |
* again to make sure. If we don't get EALREADY or |
| 2167 |
* EISCONN, something has gone wrong. Break out and report |
| 2168 |
* it. |
| 2169 |
* |
| 2170 |
* For some reason, SVR4 returns EAGAIN here instead of |
| 2171 |
* EALREADY, even though the man page says it should be |
| 2172 |
* EALREADY. |
| 2173 |
* |
| 2174 |
* For some reason, UCX pre 3 apparently returns errno = |
| 2175 |
* 18242 instead of EALREADY or EISCONN. |
| 2176 |
*/ |
| 2177 |
#ifdef INET6 |
| 2178 |
status = connect(*s, res->ai_addr, res->ai_addrlen); |
| 2179 |
#else |
| 2180 |
status = connect(*s, SOCKADDR_OF(sock_A), sizeof(sock_A)); |
| 2181 |
#endif /* INET6 */ |
| 2182 |
if ((status < 0) && |
| 2183 |
(SOCKET_ERRNO != EALREADY |
| 2184 |
#ifdef EAGAIN |
| 2185 |
&& SOCKET_ERRNO != EAGAIN |
| 2186 |
#endif |
| 2187 |
) && |
| 2188 |
#ifdef UCX |
| 2189 |
(SOCKET_ERRNO != 18242) && |
| 2190 |
#endif /* UCX */ |
| 2191 |
(SOCKET_ERRNO != EISCONN)) { |
| 2192 |
#ifdef SOCKET_DEBUG_TRACE |
| 2193 |
HTInetStatus("confirm-not-ready connect"); |
| 2194 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2195 |
break; |
| 2196 |
} |
| 2197 |
} |
| 2198 |
if (HTWasInterrupted(&status)) { |
| 2199 |
CTRACE((tfp, "*** INTERRUPTED in middle of connect.\n")); |
| 2200 |
break; |
| 2201 |
} |
| 2202 |
} |
| 2203 |
} |
| 2204 |
#ifdef SOCKET_DEBUG_TRACE |
| 2205 |
else if (status < 0) { |
| 2206 |
HTInetStatus("this socket's first and only connect"); |
| 2207 |
} |
| 2208 |
#endif /* SOCKET_DEBUG_TRACE */ |
| 2209 |
#ifdef INET6 |
| 2210 |
if (status < 0) { |
| 2211 |
NETCLOSE(*s); |
| 2212 |
*s = -1; |
| 2213 |
if (status != HT_INTERRUPTED) |
| 2214 |
continue; |
| 2215 |
} |
| 2216 |
break; |
| 2217 |
} |
| 2218 |
#endif /* INET6 */ |
| 2219 |
|
| 2220 |
#ifdef INET6 |
| 2221 |
if (*s < 0) |
| 2222 |
#else |
| 2223 |
if (status < 0) |
| 2224 |
#endif /* INET6 */ |
| 2225 |
{ |
| 2226 |
/* |
| 2227 |
* The connect attempt failed or was interrupted, so close up the |
| 2228 |
* socket. |
| 2229 |
*/ |
| 2230 |
NETCLOSE(*s); |
| 2231 |
} |
| 2232 |
#if !defined(DOSPATH) || defined(__DJGPP__) |
| 2233 |
#if !defined(NO_IOCTL) || defined(USE_FCNTL) |
| 2234 |
else { |
| 2235 |
/* |
| 2236 |
* Make the socket blocking again on good connect. |
| 2237 |
*/ |
| 2238 |
#ifdef USE_FCNTL |
| 2239 |
int ret = fcntl(*s, F_SETFL, 0); |
| 2240 |
|
| 2241 |
#else |
| 2242 |
int val = 0; |
| 2243 |
int ret = IOCTL(*s, FIONBIO, &val); |
| 2244 |
#endif /* USE_FCNTL */ |
| 2245 |
if (ret == -1) |
| 2246 |
_HTProgress(gettext("Could not restore socket to blocking.")); |
| 2247 |
} |
| 2248 |
#endif /* !NO_IOCTL || USE_FCNTL */ |
| 2249 |
#endif /* !DOSPATH || __DJGPP__ */ |
| 2250 |
|
| 2251 |
#ifdef INET6 |
| 2252 |
#ifdef NSL_FORK |
| 2253 |
FREE_NSL_FORK(res0); |
| 2254 |
#else |
| 2255 |
if (res0) |
| 2256 |
freeaddrinfo(res0); |
| 2257 |
#endif |
| 2258 |
#endif /* INET6 */ |
| 2259 |
|
| 2260 |
/* Now if this was a SOCKS5 proxy connection, go for the real one */ |
| 2261 |
if (status >= 0 && socks5_orig_url != NULL) { |
| 2262 |
unsigned char pbuf[4 + 1 + 255 + 2]; |
| 2263 |
unsigned i; |
| 2264 |
|
| 2265 |
/* RFC 1928: version identifier/method selection message */ |
| 2266 |
pbuf[0] = 0x05; /* VER: protocol version: X'05' */ |
| 2267 |
pbuf[1] = 0x01; /* NMETHODS: 1 */ |
| 2268 |
pbuf[2] = 0x00; /* METHOD: X'00' NO AUTHENTICATION REQUIRED */ |
| 2269 |
if (NETWRITE(*s, (char *) pbuf, 3) != 3) { |
| 2270 |
goto report_system_err; |
| 2271 |
} else if (HTDoRead(*s, pbuf, 2) != 2) { |
| 2272 |
goto report_system_err; |
| 2273 |
} else if (pbuf[0] != 0x05 || pbuf[1] != 0x00) { |
| 2274 |
goto report_unexpected_reply; |
| 2275 |
} |
| 2276 |
|
| 2277 |
/* RFC 1928: CONNECT request */ |
| 2278 |
HTSprintf0(&line, LY_MSG("SOCKS5: connecting to %s"), socks5_host); |
| 2279 |
_HTProgress(line); |
| 2280 |
pbuf[0] = 0x05; /* VER: protocol version: X'05' */ |
| 2281 |
pbuf[1] = 0x01; /* CMD: CONNECT X'01' */ |
| 2282 |
pbuf[2] = 0x00; /* RESERVED */ |
| 2283 |
pbuf[3] = 0x03; /* ATYP: domain name */ |
| 2284 |
pbuf[4] = (unsigned char) socks5_host_len; |
| 2285 |
memcpy(&pbuf[i = 5], socks5_host, socks5_host_len); |
| 2286 |
i += socks5_host_len; |
| 2287 |
/* C99 */ { |
| 2288 |
unsigned short x; /* XXX 16-bit? */ |
| 2289 |
|
| 2290 |
x = htons((PortNumber) socks5_port); |
| 2291 |
memcpy(&pbuf[i], (unsigned char *) &x, sizeof x); |
| 2292 |
i += (unsigned) sizeof(x); |
| 2293 |
} |
| 2294 |
if ((size_t) NETWRITE(*s, (char *) pbuf, i) != i) { |
| 2295 |
goto report_system_err; |
| 2296 |
} else if ((unsigned) HTDoRead(*s, pbuf, 4) != 4) { |
| 2297 |
goto report_system_err; |
| 2298 |
} |
| 2299 |
/* Version 5, reserved must be 0 */ |
| 2300 |
if (pbuf[0] == 0x05 && pbuf[2] == 0x00) { |
| 2301 |
/* Result */ |
| 2302 |
switch (pbuf[1]) { |
| 2303 |
case 0x00: |
| 2304 |
emsg = NULL; |
| 2305 |
break; |
| 2306 |
case 0x01: |
| 2307 |
emsg = gettext("SOCKS server failure"); |
| 2308 |
break; |
| 2309 |
case 0x02: |
| 2310 |
emsg = gettext("connection not allowed by ruleset"); |
| 2311 |
break; |
| 2312 |
case 0x03: |
| 2313 |
emsg = gettext("network unreachable"); |
| 2314 |
break; |
| 2315 |
case 0x04: |
| 2316 |
emsg = gettext("host unreachable"); |
| 2317 |
break; |
| 2318 |
case 0x05: |
| 2319 |
emsg = gettext("connection refused"); |
| 2320 |
break; |
| 2321 |
case 0x06: |
| 2322 |
emsg = gettext("TTL expired"); |
| 2323 |
break; |
| 2324 |
case 0x07: |
| 2325 |
emsg = gettext("command not supported"); |
| 2326 |
break; |
| 2327 |
case 0x08: |
| 2328 |
emsg = gettext("address type not supported"); |
| 2329 |
break; |
| 2330 |
default: |
| 2331 |
emsg = gettext("unknown SOCKS error code"); |
| 2332 |
break; |
| 2333 |
} |
| 2334 |
if (emsg != NULL) { |
| 2335 |
goto report_no_connection; |
| 2336 |
} |
| 2337 |
} else { |
| 2338 |
goto report_unexpected_reply; |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* Address type variable; read the BND.PORT with it. |
| 2342 |
* This is actually false since RFC 1928 says that the BND.ADDR reply |
| 2343 |
* to CONNECT contains the IP address, so only 0x01 and 0x04 are |
| 2344 |
* allowed */ |
| 2345 |
switch (pbuf[3]) { |
| 2346 |
case 0x01: |
| 2347 |
i = 4; |
| 2348 |
break; |
| 2349 |
case 0x03: |
| 2350 |
i = 1; |
| 2351 |
break; |
| 2352 |
case 0x04: |
| 2353 |
i = 16; |
| 2354 |
break; |
| 2355 |
default: |
| 2356 |
goto report_unexpected_reply; |
| 2357 |
} |
| 2358 |
i += (unsigned) sizeof(unsigned short); |
| 2359 |
|
| 2360 |
if ((size_t) HTDoRead(*s, pbuf, i) != i) { |
| 2361 |
goto report_system_err; |
| 2362 |
} else if (i == 1 + sizeof(unsigned short)) { |
| 2363 |
i = pbuf[0]; |
| 2364 |
if ((size_t) HTDoRead(*s, pbuf, i) != i) { |
| 2365 |
goto report_system_err; |
| 2366 |
} |
| 2367 |
} |
| 2368 |
} |
| 2369 |
goto cleanup; |
| 2370 |
|
| 2371 |
report_system_err: |
| 2372 |
emsg = LYStrerror(errno); |
| 2373 |
goto report_no_connection; |
| 2374 |
|
| 2375 |
report_unexpected_reply: |
| 2376 |
emsg = gettext("unexpected reply\n"); |
| 2377 |
/* FALLTHRU */ |
| 2378 |
|
| 2379 |
report_no_connection: |
| 2380 |
status = HT_NO_CONNECTION; |
| 2381 |
/* FALLTHRU */ |
| 2382 |
|
| 2383 |
report_error: |
| 2384 |
HTAlert(emsg); |
| 2385 |
if (*s != -1) { |
| 2386 |
NETCLOSE(*s); |
| 2387 |
} |
| 2388 |
|
| 2389 |
cleanup: |
| 2390 |
if (socks5_proxy != NULL) { |
| 2391 |
FREE(socks5_new_url); |
| 2392 |
FREE(socks5_protocol); |
| 2393 |
FREE(socks5_host); |
| 2394 |
} |
| 2395 |
FREE(host); |
| 2396 |
FREE(line); |
| 2397 |
return status; |
| 2398 |
} |
| 2399 |
|
| 2400 |
/* |
| 2401 |
* This is interruptible so reads can be implemented cleanly. |
| 2402 |
*/ |
| 2403 |
int HTDoRead(int fildes, |
| 2404 |
void *buf, |
| 2405 |
unsigned nbyte) |
| 2406 |
{ |
| 2407 |
int result; |
| 2408 |
BOOL ready; |
| 2409 |
|
| 2410 |
#if !defined(NO_IOCTL) |
| 2411 |
int ret; |
| 2412 |
fd_set readfds; |
| 2413 |
struct timeval select_timeout; |
| 2414 |
int tries = 0; |
| 2415 |
|
| 2416 |
#ifdef USE_READPROGRESS |
| 2417 |
int otries = 0; |
| 2418 |
time_t otime = time((time_t *) 0); |
| 2419 |
time_t start = otime; |
| 2420 |
#endif |
| 2421 |
#endif /* !NO_IOCTL */ |
| 2422 |
|
| 2423 |
#if defined(UNIX) && !defined(__BEOS__) |
| 2424 |
if (fildes == 0) { |
| 2425 |
/* |
| 2426 |
* 0 can be a valid socket fd, but if it's a tty something must have |
| 2427 |
* gone wrong. - kw |
| 2428 |
*/ |
| 2429 |
if (isatty(fildes)) { |
| 2430 |
CTRACE((tfp, "HTDoRead - refusing to read fd 0 which is a tty!\n")); |
| 2431 |
return -1; |
| 2432 |
} |
| 2433 |
} else |
| 2434 |
#endif |
| 2435 |
if (fildes <= 0) { |
| 2436 |
CTRACE((tfp, "HTDoRead - no file descriptor!\n")); |
| 2437 |
return -1; |
| 2438 |
} |
| 2439 |
|
| 2440 |
if (HTWasInterrupted(&result)) { |
| 2441 |
CTRACE((tfp, "HTDoRead - interrupted before starting!\n")); |
| 2442 |
return (result); |
| 2443 |
} |
| 2444 |
#if defined(NO_IOCTL) |
| 2445 |
ready = TRUE; |
| 2446 |
#else |
| 2447 |
ready = FALSE; |
| 2448 |
while (!ready) { |
| 2449 |
/* |
| 2450 |
* Protect against an infinite loop. |
| 2451 |
*/ |
| 2452 |
if ((tries++ / TRIES_PER_SECOND) >= reading_timeout) { |
| 2453 |
HTAlert(gettext("Socket read failed (too many tries).")); |
| 2454 |
SET_EINTR; |
| 2455 |
result = HT_INTERRUPTED; |
| 2456 |
break; |
| 2457 |
} |
| 2458 |
#ifdef USE_READPROGRESS |
| 2459 |
if (tries - otries > TRIES_PER_SECOND) { |
| 2460 |
time_t t = time((time_t *) 0); |
| 2461 |
|
| 2462 |
otries = tries; |
| 2463 |
if (t - otime >= 5) { |
| 2464 |
otime = t; |
| 2465 |
HTReadProgress((off_t) (-1), (off_t) 0); /* Put "stalled" message */ |
| 2466 |
} |
| 2467 |
} |
| 2468 |
#endif |
| 2469 |
|
| 2470 |
/* |
| 2471 |
* If we suspend, then it is possible that select will be interrupted. |
| 2472 |
* Allow for this possibility. - JED |
| 2473 |
*/ |
| 2474 |
do { |
| 2475 |
set_timeout(&select_timeout); |
| 2476 |
FD_ZERO(&readfds); |
| 2477 |
FD_SET((LYNX_FD) fildes, &readfds); |
| 2478 |
#ifdef SOCKS |
| 2479 |
if (socks_flag) |
| 2480 |
ret = Rselect(fildes + 1, |
| 2481 |
&readfds, NULL, NULL, &select_timeout); |
| 2482 |
else |
| 2483 |
#endif /* SOCKS */ |
| 2484 |
ret = select(fildes + 1, |
| 2485 |
&readfds, NULL, NULL, &select_timeout); |
| 2486 |
} while ((ret == -1) && (errno == EINTR)); |
| 2487 |
|
| 2488 |
if (ret < 0) { |
| 2489 |
result = -1; |
| 2490 |
break; |
| 2491 |
} else if (ret > 0) { |
| 2492 |
ready = TRUE; |
| 2493 |
} else if (HTWasInterrupted(&result)) { |
| 2494 |
#ifdef USE_READPROGRESS |
| 2495 |
if (tries > TRIES_PER_SECOND) |
| 2496 |
result = 0; |
| 2497 |
#endif |
| 2498 |
break; |
| 2499 |
} |
| 2500 |
} |
| 2501 |
#endif /* !NO_IOCTL */ |
| 2502 |
|
| 2503 |
if (ready) { |
| 2504 |
#if defined(UCX) && defined(VAXC) |
| 2505 |
/* |
| 2506 |
* VAXC and UCX problem only. |
| 2507 |
*/ |
| 2508 |
errno = vaxc$errno = 0; |
| 2509 |
result = SOCKET_READ(fildes, buf, nbyte); |
| 2510 |
CTRACE((tfp, |
| 2511 |
"Read - result,errno,vaxc$errno: %d %d %d\n", result, errno, vaxc$errno)); |
| 2512 |
if ((result <= 0) && TRACE) |
| 2513 |
perror("HTTCP.C:HTDoRead:read"); /* RJF */ |
| 2514 |
/* |
| 2515 |
* An errno value of EPIPE and result < 0 indicates end-of-file on VAXC. |
| 2516 |
*/ |
| 2517 |
if ((result <= 0) && (errno == EPIPE)) { |
| 2518 |
result = 0; |
| 2519 |
set_errno(0); |
| 2520 |
} |
| 2521 |
#else |
| 2522 |
#ifdef UNIX |
| 2523 |
while ((result = (int) SOCKET_READ(fildes, buf, nbyte)) == -1) { |
| 2524 |
if (errno == EINTR) |
| 2525 |
continue; |
| 2526 |
#ifdef ERESTARTSYS |
| 2527 |
if (errno == ERESTARTSYS) |
| 2528 |
continue; |
| 2529 |
#endif /* ERESTARTSYS */ |
| 2530 |
HTInetStatus("read"); |
| 2531 |
break; |
| 2532 |
} |
| 2533 |
#else /* UNIX */ |
| 2534 |
result = NETREAD(fildes, (char *) buf, nbyte); |
| 2535 |
#endif /* !UNIX */ |
| 2536 |
#endif /* UCX && VAXC */ |
| 2537 |
} |
| 2538 |
#ifdef USE_READPROGRESS |
| 2539 |
CTRACE2(TRACE_TIMING, (tfp, "...HTDoRead returns %d (%" PRI_time_t |
| 2540 |
" seconds)\n", |
| 2541 |
result, CAST_time_t (time((time_t *)0) - start))); |
| 2542 |
#endif |
| 2543 |
return result; |
| 2544 |
} |
| 2545 |
|
| 2546 |
#ifdef SVR4_BSDSELECT |
| 2547 |
/* |
| 2548 |
* This is a fix for the difference between BSD's select() and |
| 2549 |
* SVR4's select(). SVR4's select() can never return a value larger |
| 2550 |
* than the total number of file descriptors being checked. So, if |
| 2551 |
* you select for read and write on one file descriptor, and both |
| 2552 |
* are true, SVR4 select() will only return 1. BSD select in the |
| 2553 |
* same situation will return 2. |
| 2554 |
* |
| 2555 |
* Additionally, BSD select() on timing out, will zero the masks, |
| 2556 |
* while SVR4 does not. This is fixed here as well. |
| 2557 |
* |
| 2558 |
* Set your tabstops to 4 characters to have this code nicely formatted. |
| 2559 |
* |
| 2560 |
* Jerry Whelan, [email protected], June 12th, 1993 |
| 2561 |
*/ |
| 2562 |
#ifdef select |
| 2563 |
#undef select |
| 2564 |
#endif /* select */ |
| 2565 |
|
| 2566 |
#ifdef SOCKS |
| 2567 |
#ifdef Rselect |
| 2568 |
#undef Rselect |
| 2569 |
#endif /* Rselect */ |
| 2570 |
#endif /* SOCKS */ |
| 2571 |
|
| 2572 |
#include <sys/types.h> |
| 2573 |
#include <sys/time.h> |
| 2574 |
#include <sys/select.h> |
| 2575 |
|
| 2576 |
int BSDselect(int nfds, |
| 2577 |
fd_set * readfds, |
| 2578 |
fd_set * writefds, |
| 2579 |
fd_set * exceptfds, |
| 2580 |
struct timeval *select_timeout) |
| 2581 |
{ |
| 2582 |
int rval, i; |
| 2583 |
|
| 2584 |
#ifdef SOCKS |
| 2585 |
if (socks_flag) |
| 2586 |
rval = Rselect(nfds, readfds, writefds, exceptfds, select_timeout); |
| 2587 |
else |
| 2588 |
#endif /* SOCKS */ |
| 2589 |
rval = select(nfds, readfds, writefds, exceptfds, select_timeout); |
| 2590 |
|
| 2591 |
switch (rval) { |
| 2592 |
case -1: |
| 2593 |
return (rval); |
| 2594 |
|
| 2595 |
case 0: |
| 2596 |
if (readfds != NULL) |
| 2597 |
FD_ZERO(readfds); |
| 2598 |
if (writefds != NULL) |
| 2599 |
FD_ZERO(writefds); |
| 2600 |
if (exceptfds != NULL) |
| 2601 |
FD_ZERO(exceptfds); |
| 2602 |
return (rval); |
| 2603 |
|
| 2604 |
default: |
| 2605 |
for (i = 0, rval = 0; i < nfds; i++) { |
| 2606 |
if ((readfds != NULL) && FD_ISSET(i, readfds)) |
| 2607 |
rval++; |
| 2608 |
if ((writefds != NULL) && FD_ISSET(i, writefds)) |
| 2609 |
rval++; |
| 2610 |
if ((exceptfds != NULL) && FD_ISSET(i, exceptfds)) |
| 2611 |
rval++; |
| 2612 |
|
| 2613 |
} |
| 2614 |
return (rval); |
| 2615 |
} |
| 2616 |
/* Should never get here */ |
| 2617 |
} |
| 2618 |
#endif /* SVR4_BSDSELECT */ |