summaryrefslogtreecommitdiff
path: root/www/lynx/patches.v6/patch-ao
blob: bfaa2988152c590eac7bce7bcf2b281cb830ed59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
$NetBSD: patch-ao,v 1.1 2000/01/15 17:44:24 hubertf Exp $

Place(s) where this was (most probably...) in before:

diff -x *.orig -urN ./WWW/Library/Implementation/HTTCP.c /usr/pkgsrc/www/lynx/work.unpatched/lynx2-8-2/WWW/Library/Implementation/HTTCP.c
--- ./WWW/Library/Implementation/HTTCP.c	Mon May 24 21:00:53 1999
+++ /usr/pkgsrc/www/lynx/work.unpatched/lynx2-8-2/WWW/Library/Implementation/HTTCP.c	Sat Jan 15 07:57:18 2000
@@ -297,13 +297,15 @@
 PUBLIC CONST char * HTInetString ARGS1(
 	SockA*, 	soc_in)
 {
-    static char string[16];
-    sprintf(string, "%d.%d.%d.%d",
-	    (int)*((unsigned char *)(&soc_in->sin_addr)+0),
-	    (int)*((unsigned char *)(&soc_in->sin_addr)+1),
-	    (int)*((unsigned char *)(&soc_in->sin_addr)+2),
-	    (int)*((unsigned char *)(&soc_in->sin_addr)+3));
-    return string;
+    static char hostbuf[MAXHOSTNAMELEN];
+    getnameinfo((struct sockaddr *)soc_in,
+#ifdef SIN6_LEN
+	    ((struct sockaddr *)soc_in)->sa_len,
+#else
+	    SA_LEN((struct sockaddr *)soc_in),
+#endif
+	    hostbuf, sizeof(hostbuf), NULL, 0, NI_NUMERICHOST);
+    return hostbuf;
 }
 #endif /* !DECNET */
 
@@ -1051,11 +1053,13 @@
 **	*soc_in is filled in.  If no port is specified in str, that
 **		field is left unchanged in *soc_in.
 */
-PUBLIC int HTParseInet ARGS2(
+PUBLIC int HTParseInet ARGS3(
 	SockA *,	soc_in,
-	CONST char *,	str)
+	CONST char *,	str,
+	int, default_port)
 {
     char *port;
+    char portstr[NI_MAXSERV];
     int dotcount_ip = 0;	/* for dotted decimal IP addr */
 #ifndef _WINDOWS_NSL
     char *host = NULL;
@@ -1078,28 +1082,18 @@
     /*
     **	Parse port number if present.
     */
-    if ((port = strchr(host, ':')) != NULL) {
-	*port++ = 0;		/* Chop off port */
-	if (port[0] >= '0' && port[0] <= '9') {
-#ifdef unix
-	    soc_in->sin_port = htons(atol(port));
-#else /* VMS: */
-#ifdef DECNET
-	    soc_in->sdn_objnum = (unsigned char)(strtol(port, (char**)0, 10));
-#else
-	    soc_in->sin_port = htons((unsigned short)strtol(port,(char**)0,10));
-#endif /* Decnet */
-#endif /* Unix vs. VMS */
-#ifdef SUPPRESS 	/* 1. crashes!?!.  2. Not recommended */
-	} else {
-	    struct servent * serv = getservbyname(port, (char*)0);
-	    if (serv) {
-		soc_in->sin_port = serv->s_port;
-	    } else {
-		CTRACE(tfp, "TCP: Unknown service %s\n", port);
-	    }
-#endif /* SUPPRESS */
-	}
+
+    if (!strrchr(host, ']'))
+	port = strrchr(host, ':');
+    else
+	port = strrchr(strrchr(host, ']'), ':');
+
+    if (port) {
+      *port++ = 0;            /* Chop off port */
+    }
+    else {
+      sprintf(portstr,"%d", default_port);
+      port = portstr;
     }
 
 #ifdef DECNET
@@ -1113,6 +1107,13 @@
 		soc_in->sdn_objnum, host);
 #else  /* parse Internet host: */
 
+    /* [host] case */
+    if (host[0] == '[' && host[strlen(host) - 1] == ']') {
+	host[strlen(host) - 1] = '\0';
+	host++;
+    }
+
+#ifndef INET6
     if (*host >= '0' && *host <= '9') {   /* Test for numeric node address: */
 	char *strptr = host;
 	while (*strptr) {
@@ -1127,11 +1128,14 @@
 	    dotcount_ip = 0;
 	}
     }
+#endif
 
     /*
     **	Parse host number if present.
     */
-    if (dotcount_ip == 3) {   /* Numeric node address: */
+#ifndef INET6
+    if (dotcount_ip == 3)   /* Numeric node address: */
+    {
 
 #ifdef DJGPP
 	soc_in->sin_addr.s_addr = htonl(aton(host));
@@ -1159,7 +1163,9 @@
 #ifndef _WINDOWS_NSL
 	FREE(host);
 #endif /* _WINDOWS_NSL */
-    } else {		    /* Alphanumeric node name: */
+    } else
+#endif
+    {		    /* Alphanumeric node name: */
 
 #ifdef MVS	/* Outstanding problem with crash in MVS gethostbyname */
 	CTRACE(tfp, "HTParseInet: Calling LYGetHostByName(%s)\n", host);
@@ -1181,10 +1187,18 @@
 	memcpy((void *)&soc_in->sin_addr, phost->h_addr, phost->h_length);
 #else /* !DJGPP, !_WINDOWS_NSL: */
 	{
-	    struct hostent  *phost;
-	    phost = LYGetHostByName(host);	/* See above */
+	    struct addrinfo hints, *res;
+	    int error;
 
-	    if (!phost) goto failed;
+	    memset(&hints, 0, sizeof(hints));
+	    hints.ai_family = PF_UNSPEC;
+	    error = getaddrinfo(host, port, &hints, &res);
+
+	    if (error || !res) {
+		CTRACE(tfp, "HTParseInet: getaddrinfo(%s): %s\n", host,
+			gai_strerror(error));
+		goto failed;
+	    }
 #if defined(VMS) && defined(CMU_TCP)
 	    /*
 	    **  In LIBCMU, phost->h_length contains not the length of one address
@@ -1194,14 +1208,18 @@
 	    **  longer supported, and CMU users are encouraged to obtain and use
 	    **  SOCKETSHR/NETLIB instead. - S. Bjorndahl
 	    */
-	    memcpy((void *)&soc_in->sin_addr, phost->h_addr, 4);
-#else
-	    if (!phost) goto failed;
-	    if (phost->h_length != sizeof soc_in->sin_addr) {
-		HTAlwaysAlert(host, gettext("Address length looks invalid"));
+	    if (res->ai_family == AF_INET) {
+		memcpy((void *)&soc_in->sin_addr,
+		    &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4);
+	    } else {
+		CTRACE(tfp, "HTParseInet: unsupported address family %d\n",
+			res->ai_family);
+		goto failed;
 	    }
-	    memcpy((void *)&soc_in->sin_addr, phost->h_addr, phost->h_length);
+#else
+	    memcpy((void *)soc_in, res->ai_addr, res->ai_addrlen);
 #endif /* VMS && CMU_TCP */
+	    freeaddrinfo(res);
 	}
 #endif /* !DJGPP, !_WINDOWS_NSL */
 #endif /* !DJGPP */
@@ -1211,12 +1229,14 @@
 
     }	/* Alphanumeric node name */
 
+#ifndef INET6
     CTRACE(tfp, "HTParseInet: Parsed address as port %d, IP address %d.%d.%d.%d\n",
 		(int)ntohs(soc_in->sin_port),
 		(int)*((unsigned char *)(&soc_in->sin_addr)+0),
 		(int)*((unsigned char *)(&soc_in->sin_addr)+1),
 		(int)*((unsigned char *)(&soc_in->sin_addr)+2),
 		(int)*((unsigned char *)(&soc_in->sin_addr)+3));
+#endif
 #endif	/* Internet vs. Decnet */
 
     return 0;	/* OK */
@@ -1232,8 +1252,50 @@
     case HT_INTERRUPTED:
 	return lynx_nsl_status;
     default:
-    return -1;
+        return -1;
+    }
 }
+
+PRIVATE struct addrinfo *
+HTGetAddrInfo ARGS2(
+    CONST char *, str,
+    CONST int, defport)
+{
+    struct addrinfo hints, *res;
+    int error;
+    char *p;
+    char *s;
+    char *host, *port;
+    char pbuf[10];
+
+    s = strdup(str);
+
+    if (s[0] == '[' && (p = strchr(s, ']')) != NULL) {
+	*p++ = '\0';
+	host = s + 1;
+    } else {
+	p = s;
+	host = &s[0];
+    }
+    port = strrchr(p, ':');
+    if (port) {
+	*port++ = '\0';
+    } else {
+	snprintf(pbuf, sizeof(pbuf), "%d", defport);
+	port = pbuf;
+    }
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    error = getaddrinfo(host, port, &hints, &res);
+    if (error || !res) {
+	CTRACE(tfp, "HTGetAddrInfo: getaddrinfo(%s, %s): %s\n", host, port,
+		gai_strerror(error));
+	return NULL;
+    }
+
+    return res;
 }
 
 #ifdef LY_FIND_LEAKS
@@ -1262,7 +1324,8 @@
     char *domain_name;			/* The name of this host domain */
 #endif /* UCX */
 #ifdef NEED_HOST_ADDRESS		/* no -- needs name server! */
-    struct hostent * phost;		/* Pointer to host -- See netdb.h */
+    struct addrinfo hints, *res;
+    int error;
 #endif /* NEED_HOST_ADDRESS */
     int namelength = sizeof(name);
 
@@ -1290,14 +1353,20 @@
 
 #ifndef DECNET	/* Decnet ain't got no damn name server 8#OO */
 #ifdef NEED_HOST_ADDRESS		/* no -- needs name server! */
-    phost = gethostbyname(name);	/* See netdb.h */
-    if (!OK_HOST(phost)) {
-	CTRACE(tfp, "TCP: Can't find my own internet node address for `%s'!!\n",
-		    name);
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_flags = AI_CANONNAME;
+    error = getaddrinfo(name, NULL, &hints, &res);
+    if (error || !res || !res->ai_canonname) {
+	CTRACE(tfp, "TCP: %s: `%s'\n", gai_strerror(error), name);
+	if (res)
+	    freeaddrinfo(res);
 	return;  /* Fail! */
     }
-    StrAllocCopy(hostname, phost->h_name);
-    memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
+    StrAllocCopy(hostname, res->ai_canonname);
+    memcpy(&HTHostAddress, res->ai_addr, res->ai_addrlen);
+    freeaddrinfo(res);
     CTRACE(tfp, "     Name server says that I am `%s' = %s\n",
 		hostname, HTInetString(&HTHostAddress));
 #endif /* NEED_HOST_ADDRESS */
@@ -1322,20 +1391,14 @@
 	int,		default_port,
 	int *,		s)
 {
-    struct sockaddr_in soc_address;
-    struct sockaddr_in *soc_in = &soc_address;
     int status;
     char *line = NULL;
     char *p1 = NULL;
     char *at_sign = NULL;
     char *host = NULL;
-
-    /*
-    **	Set up defaults.
-    */
-    memset(soc_in, 0, sizeof(*soc_in));
-    soc_in->sin_family = AF_INET;
-    soc_in->sin_port = htons(default_port);
+    int error;
+    struct sockaddr *sa;
+    struct addrinfo *res, *res0;
 
     /*
     **	Get node name and optional port number.
@@ -1353,25 +1416,15 @@
 
     HTSprintf0 (&line, gettext("Looking up %s."), host);
     _HTProgress (line);
-    status = HTParseInet(soc_in, host);
-    if (status) {
-	if (status != HT_INTERRUPTED) {
-	    if (status == HT_NOT_ACCEPTABLE) {
-		/*  Not HTProgress, so warning won't be overwritten
-		 *  immediately; but not HTAlert, because typically
-		 *  there will be other alerts from the callers. - kw
-		 */
-		HTUserMsg2(gettext("Invalid hostname %s"), host);
-	    } else {
-		HTSprintf0 (&line,
-			 gettext("Unable to locate remote host %s."), host);
-		_HTProgress(line);
-	    }
-	    status = HT_NO_DATA;
-	}
+    /* HTParseInet() is useless! */
+    _HTProgress(host);
+    res0 = HTGetAddrInfo(host, default_port);
+    if (res0 == NULL) {
+	sprintf (line, "Unable to locate remote host %s.", host);
+	_HTProgress(line);
 	FREE(host);
 	FREE(line);
-	return status;
+	return HT_NO_DATA;
     }
 
     HTSprintf0 (&line, gettext("Making %s connection to %s."), protocol, host);
@@ -1379,199 +1432,214 @@
     FREE(host);
     FREE(line);
 
-    /*
-    **	Now, let's get a socket set up from the server for the data.
-    */
-    *s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-    if (*s == -1) {
-	HTAlert(gettext("socket failed."));
-	return HT_NO_DATA;
-    }
+    for (res = res0; res; res = res->ai_next) {
+	/*
+	**	Now, let's get a socket set up from the server for the data.
+	*/
+	*s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+	if (*s == -1) {
+	    char hostbuf[1024], portbuf[1024];
+	    getnameinfo(res->ai_addr, res->ai_addrlen,
+		    hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
+		    NI_NUMERICHOST|NI_NUMERICSERV);
+	    HTSprintf0 (&line, "socket failed: family %d addr %s port %s.",
+		    res->ai_family, hostbuf, portbuf);
+	    _HTProgress (line);
+	    FREE(line);
+	    continue;
+	}
 
 #ifndef DOSPATH
 #if !defined(NO_IOCTL) || defined(USE_FCNTL)
-    /*
-    **	Make the socket non-blocking, so the connect can be canceled.
-    **	This means that when we issue the connect we should NOT
-    **	have to wait for the accept on the other end.
-    */
-    {
+	/*
+	**	Make the socket non-blocking, so the connect can be canceled.
+	**	This means that when we issue the connect we should NOT
+	**	have to wait for the accept on the other end.
+	*/
+	{
 #ifdef USE_FCNTL
-	int ret = fcntl(*s, F_SETFL, O_NONBLOCK);
+	    int ret = fcntl(*s, F_SETFL, O_NONBLOCK);
 #else
-	int val = 1;
-	int ret = IOCTL(*s, FIONBIO, &val);
+	    int val = 1;
+	    int ret = IOCTL(*s, FIONBIO, &val);
 #endif /* USE_FCNTL */
-	if (ret == -1)
-	    _HTProgress(gettext("Could not make connection non-blocking."));
-    }
+	    if (ret == -1)
+		_HTProgress("Could not make connection non-blocking.");
+	}
 #endif /* !NO_IOCTL || USE_FCNTL */
 #endif /* !DOSPATH */
 
-    /*
-    **	Issue the connect.  Since the server can't do an instantaneous
-    **	accept and we are non-blocking, this will almost certainly return
-    **	a negative status.
-    */
-#ifdef SOCKS
-    if (socks_flag) {
-	status = Rconnect(*s, (struct sockaddr*)&soc_address,
-			  sizeof(soc_address));
 	/*
-	**  For long Rbind.
+	**	Issue the connect.  Since the server can't do an instantaneous
+	**	accept and we are non-blocking, this will almost certainly return
+	**	a negative status.
 	*/
-	socks_bind_remoteAddr = soc_address.sin_addr.s_addr;
-    } else
+#ifdef SOCKS
+	if (socks_flag) {
+	    status = Rconnect(*s, res->ai_addr, res->ai_addrlen);
+	    /*
+	    **  For long Rbind.
+	    */
+	    socks_bind_remoteAddr = soc_address.sin_addr.s_addr;
+	} else
 #endif /* SOCKS */
-    status = connect(*s, (struct sockaddr*)&soc_address, sizeof(soc_address));
+	status = connect(*s, res->ai_addr, res->ai_addrlen);
 #ifndef DJGPP
-    /*
-    **	According to the Sun man page for connect:
-    **	   EINPROGRESS	       The socket is non-blocking and the  con-
-    **			       nection cannot be completed immediately.
-    **			       It is possible to select(2) for	comple-
-    **			       tion  by  selecting the socket for writ-
-    **			       ing.
-    **	According to the Motorola SVR4 man page for connect:
-    **	   EAGAIN	       The socket is non-blocking and the  con-
-    **			       nection cannot be completed immediately.
-    **			       It is possible to select for  completion
-    **			       by  selecting  the  socket  for writing.
-    **			       However, this is only  possible	if  the
-    **			       socket  STREAMS	module	is  the topmost
-    **			       module on  the  protocol  stack	with  a
-    **			       write  service  procedure.  This will be
-    **			       the normal case.
-    */
-    if ((status < 0) &&
-	(SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EAGAIN)) {
-	struct timeval timeout;
-	int ret;
-	int tries=0;
-
-	ret = 0;
-	while (ret <= 0) {
-	    fd_set writefds;
+	/*
+	**	According to the Sun man page for connect:
+	**	   EINPROGRESS	       The socket is non-blocking and the  con-
+	**			       nection cannot be completed immediately.
+	**			       It is possible to select(2) for	comple-
+	**			       tion  by  selecting the socket for writ-
+	**			       ing.
+	**	According to the Motorola SVR4 man page for connect:
+	**	   EAGAIN	       The socket is non-blocking and the  con-
+	**			       nection cannot be completed immediately.
+	**			       It is possible to select for  completion
+	**			       by  selecting  the  socket  for writing.
+	**			       However, this is only  possible	if  the
+	**			       socket  STREAMS	module	is  the topmost
+	**			       module on  the  protocol  stack	with  a
+	**			       write  service  procedure.  This will be
+	**			       the normal case.
+	*/
+	if ((status < 0) &&
+	    (SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EAGAIN)) {
+	    struct timeval timeout;
+	    int ret;
+	    int tries=0;
+
+	    ret = 0;
+	    while (ret <= 0) {
+		fd_set writefds;
 
-	    /*
-	    **	Protect against an infinite loop.
-	    */
-	    if (tries++ >= 180000) {
-		HTAlert(gettext("Connection failed for 180,000 tries."));
-		return HT_NO_DATA;
-	    }
+		/*
+		**	Protect against an infinite loop.
+		*/
+		if (tries++ >= 180000) {
+		    HTAlert("Connection failed for 180,000 tries.");
+		    FREE(line);
+		    freeaddrinfo(res0);
+		    return HT_NO_DATA;
+		}
 
 #ifdef _WINDOWS_NSL
-	    timeout.tv_sec = 100;
+		timeout.tv_sec = 100;
 #else
-	    timeout.tv_sec = 0;
+		timeout.tv_sec = 0;
 #endif /* _WINDOWS_NSL */
-	    timeout.tv_usec = 100000;
-	    FD_ZERO(&writefds);
-	    FD_SET(*s, &writefds);
+		timeout.tv_usec = 100000;
+		FD_ZERO(&writefds);
+		FD_SET(*s, &writefds);
 #ifdef SOCKS
-	    if (socks_flag)
-		ret = Rselect(FD_SETSIZE, NULL,
-			      (void *)&writefds, NULL, &timeout);
-	    else
+		if (socks_flag)
+		    ret = Rselect(FD_SETSIZE, NULL,
+				  (void *)&writefds, NULL, &timeout);
+		else
 #endif /* SOCKS */
-	    ret = select(FD_SETSIZE, NULL, (void *)&writefds, NULL, &timeout);
+		ret = select(FD_SETSIZE, NULL, (void *)&writefds, NULL, &timeout);
 
-	   /*
-	   **  If we suspend, then it is possible that select will be
-	   **  interrupted.  Allow for this possibility. - JED
-	   */
-	   if ((ret == -1) && (errno == EINTR))
-	     continue;
+	       /*
+	       **  If we suspend, then it is possible that select will be
+	       **  interrupted.  Allow for this possibility. - JED
+	       */
+	       if ((ret == -1) && (errno == EINTR))
+		 continue;
 
-	    /*
-	    **	Again according to the Sun and Motorola man pages for connect:
-	    **	   EALREADY	       The socket is non-blocking and a  previ-
-	    **			       ous  connection attempt has not yet been
-	    **			       completed.
-	    **	Thus if the SOCKET_ERRNO is NOT EALREADY we have a real error,
-	    **	and should break out here and return that error.
-	    **	Otherwise if it is EALREADY keep on trying to complete the
-	    **	connection.
-	    */
-	    if ((ret < 0) && (SOCKET_ERRNO != EALREADY)) {
-		status = ret;
-		break;
-	    } else if (ret > 0) {
 		/*
-		**  Extra check here for connection success, if we try to
-		**  connect again, and get EISCONN, it means we have a
-		**  successful connection.  But don't check with SOCKS.
+		**	Again according to the Sun and Motorola man pages for connect:
+		**	   EALREADY	       The socket is non-blocking and a  previ-
+		**			       ous  connection attempt has not yet been
+		**			       completed.
+		**	Thus if the SOCKET_ERRNO is NOT EALREADY we have a real error,
+		**	and should break out here and return that error.
+		**	Otherwise if it is EALREADY keep on trying to complete the
+		**	connection.
 		*/
+		if ((ret < 0) && (SOCKET_ERRNO != EALREADY)) {
+		    status = ret;
+		    break;
+		} else if (ret > 0) {
+		    /*
+		    **  Extra check here for connection success, if we try to
+		    **  connect again, and get EISCONN, it means we have a
+		    **  successful connection.  But don't check with SOCKS.
+		    */
 #ifdef SOCKS
-		if (socks_flag) {
-		    status = 0;
-		} else {
+		    if (socks_flag) {
+			status = 0;
+		    } else
 #endif /* SOCKS */
-		status = connect(*s, (struct sockaddr*)&soc_address,
-				 sizeof(soc_address));
+		    {
+			status = connect(*s, res->ai_addr, res->ai_addrlen);
 #ifdef UCX
-		/*
-		**  A UCX feature: Instead of returning EISCONN
-		**		 UCX returns EADDRINUSE.
-		**  Test for this status also.
-		*/
-		if ((status < 0) && ((SOCKET_ERRNO == EISCONN) ||
-				     (SOCKET_ERRNO == EADDRINUSE)))
+			/*
+			**  A UCX feature: Instead of returning EISCONN
+			**		 UCX returns EADDRINUSE.
+			**  Test for this status also.
+			*/
+			if ((status < 0) && ((SOCKET_ERRNO == EISCONN) ||
+					     (SOCKET_ERRNO == EADDRINUSE)))
 #else
-		if ((status < 0) && (SOCKET_ERRNO == EISCONN))
+			if ((status < 0) && (SOCKET_ERRNO == EISCONN))
 #endif /* UCX */
-		{
-		    status = 0;
-		}
+			{
+			    status = 0;
+			}
 
-		if (status && (SOCKET_ERRNO == EALREADY)) /* new stuff LJM */
-		    ret = 0; /* keep going */
-		else
-		    break;
-#ifdef SOCKS
+			if (status && (SOCKET_ERRNO == EALREADY)) /* new stuff LJM */
+			    ret = 0; /* keep going */
+			else
+			    break;
+		    }
 		}
-#endif /* SOCKS */
-	    }
 #ifdef SOCKS
-	    else if (!socks_flag)
+		else if (!socks_flag)
 #else
-	    else
+		else
 #endif /* SOCKS */
-	    {
-		/*
-		**  The select says we aren't ready yet.  Try to connect
-		**  again to make sure.  If we don't get EALREADY or EISCONN,
-		**  something has gone wrong.  Break out and report it.
-		**
-		**  For some reason, SVR4 returns EAGAIN here instead of
-		**  EALREADY, even though the man page says it should be
-		**  EALREADY.
-		**
-		**  For some reason, UCX pre 3 apparently returns
-		**  errno = 18242 instead the EALREADY or EISCONN.
-		*/
-		status = connect(*s, (struct sockaddr*)&soc_address,
-				 sizeof(soc_address));
-		if ((status < 0) &&
-		    (SOCKET_ERRNO != EALREADY && SOCKET_ERRNO != EAGAIN) &&
-#ifdef UCX
-		    (SOCKET_ERRNO != 18242) &&
-#endif /* UCX */
-		    (SOCKET_ERRNO != EISCONN)) {
+		{
+		    /*
+		    **  The select says we aren't ready yet.  Try to connect
+		    **  again to make sure.  If we don't get EALREADY or EISCONN,
+		    **  something has gone wrong.  Break out and report it.
+		    **
+		    **  For some reason, SVR4 returns EAGAIN here instead of
+		    **  EALREADY, even though the man page says it should be
+		    **  EALREADY.
+		    **
+		    **  For some reason, UCX pre 3 apparently returns
+		    **  errno = 18242 instead the EALREADY or EISCONN.
+		    */
+		    status = connect(*s, res->ai_addr, res->ai_addrlen);
+		    if ((status < 0) &&
+			(SOCKET_ERRNO != EALREADY && SOCKET_ERRNO != EAGAIN) &&
+    #ifdef UCX
+			(SOCKET_ERRNO != 18242) &&
+    #endif /* UCX */
+			(SOCKET_ERRNO != EISCONN)) {
+			break;
+		    }
+		}
+		if (HTCheckForInterrupt()) {
+		    CTRACE(tfp, "*** INTERRUPTED in middle of connect.\n");
+		    status = HT_INTERRUPTED;
+		    SOCKET_ERRNO = EINTR;
 		    break;
 		}
 	    }
-	    if (HTCheckForInterrupt()) {
-		CTRACE(tfp, "*** INTERRUPTED in middle of connect.\n");
-		status = HT_INTERRUPTED;
-		SOCKET_ERRNO = EINTR;
-		break;
-	    }
 	}
+
+	if (status < 0) {
+	    NETCLOSE(*s);
+	    *s = -1;
+	    continue;
+	}
+
+	break;
     }
 #endif /* !DJGPP */
-    if (status < 0) {
+    if (*s < 0) {
 	/*
 	**  The connect attempt failed or was interrupted,
 	**  so close up the socket.
@@ -1596,6 +1664,8 @@
 #endif /* !NO_IOCTL || USE_FCNTL */
 #endif /* !DOSPATH */
 
+    FREE(line);
+    freeaddrinfo(res0);
     return status;
 }