Annotation of libwww/Library/src/HTTCP.c, revision 2.40
2.31 frystyk 1: /* HTTCP.c
2: ** GENERIC COMMUNICATION CODE
3: **
4: ** (c) COPYRIGHT CERN 1994.
5: ** Please first read the full copyright statement in the file COPYRIGH.
1.1 timbl 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.
2.7 duns 11: ** 13 Sep 93 MD Added correct return of vmserrorno for HTInetStatus.
12: ** Added decoding of vms error message for MULTINET.
2.13 frystyk 13: ** 31 May 94 HF Added cache on host id's; now use inet_ntoa() to
14: ** HTInetString and some other fixes. Added HTDoConnect
15: ** and HTDoAccept
1.1 timbl 16: */
17:
2.36 frystyk 18: /* Library include files */
19: #include "tcp.h"
2.13 frystyk 20: #include "HTUtils.h"
2.36 frystyk 21: #include "HTString.h"
2.13 frystyk 22: #include "HTAtom.h"
23: #include "HTList.h"
24: #include "HTParse.h"
25: #include "HTAccess.h"
26: #include "HTError.h"
2.27 frystyk 27: #include "HTThread.h"
2.36 frystyk 28: #include "HTTCP.h" /* Implemented here */
2.29 frystyk 29:
2.36 frystyk 30: #ifdef VMS
31: #include "HTVMSUtils.h"
32: #endif /* VMS */
1.1 timbl 33:
34: #ifdef SHORT_NAMES
35: #define HTInetStatus HTInStat
2.12 luotonen 36: #define HTErrnoString HTErrnoS
1.1 timbl 37: #define HTInetString HTInStri
38: #define HTParseInet HTPaInet
39: #endif
40:
2.36 frystyk 41:
42: /* VMS stuff */
43: #ifdef VMS
44: #ifndef MULTINET
45: #define FD_SETSIZE 32
46: #else /* Multinet */
47: #define FD_SETSIZE 256
48: #endif /* Multinet */
49: #endif /* VMS */
50:
2.13 frystyk 51: /* Macros and other defines */
2.24 frystyk 52: /* x seconds penalty on a multi-homed host if IP-address is down */
53: #define TCP_PENALTY 1200
54:
55: /* x seconds penalty on a multi-homed host if IP-address is timed out */
56: #define TCP_DELAY 600
57:
58: /* Max number of non-blocking accepts */
2.13 frystyk 59: #define MAX_ACCEPT_POLL 30
60: #define FCNTL(r, s, t) fcntl(r, s, t)
61:
2.36 frystyk 62: #ifndef RESOLV_CONF
63: #define RESOLV_CONF "/etc/resolv.conf"
64: #endif
65:
2.13 frystyk 66: /* Globals */
67: PUBLIC unsigned int HTConCacheSize = 512; /* Number of cached servers */
68:
69: /* Type definitions and global variables etc. local to this module */
70:
71: /* This structure is a cache of hosts to whom we have connected over time.
72: The structure contains the necessary parts from hostent. For Internet host
73: hostent->h_addr_list is not an array of char pointers but an array of
74: pointers of type in_addr. */
75: typedef struct _host_info {
76: HTAtom * hostname; /* Official name of host */
77: int hits; /* Total number of hits on this host */
78: int addrlength; /* Length of address in bytes */
2.24 frystyk 79: int homes; /* Number of IP addresses on the host */
2.13 frystyk 80: int offset; /* Offset value of active IP address */
81: char ** addrlist; /* List of addresses from name server */
2.37 frystyk 82: double * weight; /* Weight on each address */
2.13 frystyk 83: } host_info;
84:
85: PRIVATE char *hostname = NULL; /* The name of this host */
2.19 frystyk 86: PRIVATE char *mailaddress = NULL; /* Current mail address */
2.13 frystyk 87: PRIVATE HTList *hostcache = NULL; /* List of servers that we have talked to */
88: PRIVATE unsigned int HTCacheSize = 0; /* Current size of cache */
2.11 duns 89:
2.13 frystyk 90: /* ------------------------------------------------------------------------- */
1.1 timbl 91:
2.36 frystyk 92: /*
93: ** Returns the string equivalent to the errno passed in the argument.
2.37 frystyk 94: ** We can't use errno directly as we have both errno and socerrno. The
2.36 frystyk 95: ** result is a static buffer.
1.1 timbl 96: */
2.36 frystyk 97: PUBLIC CONST char * HTErrnoString ARGS1(int, errornumber)
1.1 timbl 98: {
2.40 ! frystyk 99: #ifdef HAVE_STRERROR
2.36 frystyk 100: return strerror(errornumber);
2.34 roeber 101: #else
2.36 frystyk 102: #ifdef VMS
2.12 luotonen 103: static char buf[60];
2.36 frystyk 104: sprintf(buf,"Unix errno = %ld dec, VMS error = %lx hex", errornumber,
105: vaxc$errno);
2.12 luotonen 106: return buf;
2.36 frystyk 107: #else
2.40 ! frystyk 108: #ifdef _WINDOWS
! 109: static char buf[60];
! 110: sprintf(buf, "Unix errno = %ld dec, WinSock erro = %ld", errornumber, WSAGetLastError());
! 111: return buf;
! 112: #else
2.36 frystyk 113: return (errornumber < sys_nerr ? sys_errlist[errornumber]:"Unknown error");
2.40 ! frystyk 114: #endif /* WINDOWS */
! 115: #endif /* VMS */
! 116: #endif /* Next of THINK_C */
2.12 luotonen 117: }
118:
2.36 frystyk 119:
120: /* Debug error message
2.12 luotonen 121: */
2.39 frystyk 122: PUBLIC int HTInetStatus ARGS2(int, errnum, char *, where)
2.12 luotonen 123: {
2.40 ! frystyk 124: #if ! (defined(VMS) || defined(WINDOWS))
2.12 luotonen 125:
2.27 frystyk 126: if (PROT_TRACE)
2.39 frystyk 127: fprintf(TDEST, "TCP errno... %d after call to %s() failed.\n............ %s\n", errnum, where, HTErrnoString(errnum));
1.1 timbl 128:
2.36 frystyk 129: #else /* VMS */
2.40 ! frystyk 130: #ifdef VMS
2.36 frystyk 131: if (PROT_TRACE) fprintf(TDEST, " Unix error number = %ld dec\n", errno);
132: if (PROT_TRACE) fprintf(TDEST, " VMS error = %lx hex\n", vaxc$errno);
2.40 ! frystyk 133: #endif
! 134: #ifdef WINDOWS
! 135: if (PROT_TRACE) fprintf(TDEST, " Unix error number = %ld dec\n", errno);
! 136: if (PROT_TRACE) fprintf(TDEST, " NT error = %lx hex\n", WSAGetLastError());
! 137: #endif
2.12 luotonen 138:
2.36 frystyk 139: #ifdef MULTINET
140: if (PROT_TRACE) fprintf(TDEST, " Multinet error = %lx hex\n", socket_errno);
141: if (PROT_TRACE) fprintf(TDEST, " Error String = %s\n", vms_errno_string());
142: #endif /* MULTINET */
2.12 luotonen 143:
2.36 frystyk 144: #endif /* VMS */
2.7 duns 145:
2.36 frystyk 146: #ifdef VMS
2.11 duns 147: /* errno happen to be zero if vaxc$errno <> 0 */
148: return -vaxc$errno;
2.7 duns 149: #else
1.1 timbl 150: return -errno;
2.7 duns 151: #endif
1.1 timbl 152: }
153:
154:
155: /* Parse a cardinal value parse_cardinal()
156: ** ----------------------
157: **
158: ** On entry,
159: ** *pp points to first character to be interpreted, terminated by
160: ** non 0:9 character.
161: ** *pstatus points to status already valid
162: ** maxvalue gives the largest allowable value.
163: **
164: ** On exit,
165: ** *pp points to first unread character
166: ** *pstatus points to status updated iff bad
167: */
168:
169: PUBLIC unsigned int HTCardinal ARGS3
170: (int *, pstatus,
171: char **, pp,
172: unsigned int, max_value)
173: {
2.36 frystyk 174: unsigned int n=0;
1.1 timbl 175: if ( (**pp<'0') || (**pp>'9')) { /* Null string is error */
176: *pstatus = -3; /* No number where one expeceted */
177: return 0;
178: }
179: while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
180:
181: if (n>max_value) {
182: *pstatus = -4; /* Cardinal outside range */
183: return 0;
184: }
185:
186: return n;
187: }
188:
2.19 frystyk 189: /* ------------------------------------------------------------------------- */
2.27 frystyk 190: /* SIGNAL HANDLING */
191: /* ------------------------------------------------------------------------- */
192:
193: #ifdef WWWLIB_SIG
194: /* HTSetSignal
195: ** This function sets up signal handlers. This might not be necessary to
196: ** call if the application has its own handlers.
197: */
198: #include <signal.h>
199: PUBLIC void HTSetSignal NOARGS
200: {
201: /* On some systems (SYSV) it is necessary to catch the SIGPIPE signal
202: ** when attemting to connect to a remote host where you normally should
203: ** get `connection refused' back
204: */
205: if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
2.36 frystyk 206: if (PROT_TRACE) fprintf(TDEST, "HTSignal.... Can't catch SIGPIPE\n");
2.27 frystyk 207: } else {
2.36 frystyk 208: if (PROT_TRACE) fprintf(TDEST, "HTSignal.... Ignoring SIGPIPE\n");
2.27 frystyk 209: }
210: }
211: #endif /* WWWLIB_SIG */
212:
213: /* ------------------------------------------------------------------------- */
2.19 frystyk 214: /* HOST CACHE MANAGEMENT */
215: /* ------------------------------------------------------------------------- */
1.1 timbl 216:
2.27 frystyk 217: /* HTTCPCacheRemoveElement
2.13 frystyk 218: **
219: ** Remove the element specified from the cache
220: */
221: PRIVATE void HTTCPCacheRemoveElement ARGS1(host_info *, element)
222: {
223: if (!hostcache) {
2.27 frystyk 224: if (PROT_TRACE)
2.36 frystyk 225: fprintf(TDEST, "HostCache... Remove not done, no cache\n");
2.13 frystyk 226: return;
227: }
2.36 frystyk 228: if (PROT_TRACE) fprintf(TDEST, "HostCache... Remove `%s' from cache\n",
2.27 frystyk 229: HTAtom_name(element->hostname));
230: HTList_removeObject(hostcache, (void *) element);
2.22 frystyk 231: if (*element->addrlist)
232: free(*element->addrlist);
2.13 frystyk 233: if (element->addrlist)
234: free(element->addrlist);
235: if (element->weight)
236: free(element->weight);
237: free(element);
238: }
239:
240:
2.32 frystyk 241: /* HTTCPCacheRemoveAll
242: **
243: ** Cleans up the memory. Called by HTLibTerminate
244: */
245: PUBLIC void HTTCPCacheRemoveAll NOARGS
246: {
247: if (hostcache) {
248: HTList *cur = hostcache;
249: host_info *pres;
250: while ((pres = (host_info *) HTList_nextObject(cur))) {
251: if (*pres->addrlist)
252: free(*pres->addrlist);
253: if (pres->addrlist)
254: free(pres->addrlist);
255: if (pres->weight)
256: free(pres->weight);
257: free(pres);
258: }
259: HTList_delete(hostcache);
260: hostcache = NULL;
261: }
262: }
263:
264:
2.13 frystyk 265: /* HTTCPCacheRemoveHost
266: **
267: ** Removes the corresponding entrance in the cache
268: */
269: PRIVATE void HTTCPCacheRemoveHost ARGS1(char *, host)
270: {
271: HTAtom *hostatom = HTAtom_for(host);
272: HTList *cur = hostcache;
273: host_info *pres = NULL;
274: if (!hostcache) {
2.36 frystyk 275: if (PROT_TRACE)
276: fprintf(TDEST, "HostCache... Remove host not done, no cache\n");
2.13 frystyk 277: return;
278: }
279: while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
280: if (pres->hostname == hostatom) {
281: break;
282: }
283: }
284: if (pres)
285: HTTCPCacheRemoveElement(pres);
286: }
287:
288:
289: /* HTTCPCacheGarbage
290: **
291: ** Remove the element with the lowest hit rate
292: */
293: PRIVATE void HTTCPCacheGarbage NOARGS
294: {
295: HTList *cur = hostcache;
296: host_info *pres, *worst_match = NULL;
297: unsigned int worst_hits = 30000; /* Should use UINT_MAX :-( */
298: if (!hostcache) {
2.27 frystyk 299: if (PROT_TRACE)
2.36 frystyk 300: fprintf(TDEST, "HostCache... Garbage collection not done, no cache\n");
2.13 frystyk 301: return;
302: }
303:
304: /* Seek for worst element */
305: while ((pres = (host_info *) HTList_nextObject(cur))) {
306: if (!worst_match || pres->hits <= worst_hits) {
307: worst_match = pres;
308: worst_hits = pres->hits;
309: }
310: }
311: if (worst_match)
312: HTTCPCacheRemoveElement(worst_match);
313: }
314:
315:
316: /* HTTCPCacheAddElement
317: **
318: ** Add an element to the cache of visited hosts. Note that this function
319: ** requires the system implemented structure hostent and not our own
2.24 frystyk 320: ** host_info. The homes variable indicates the number of
321: ** IP addresses found.
2.13 frystyk 322: **
323: ** Returns new element if OK NULL if error
324: */
325: PRIVATE host_info *HTTCPCacheAddElement ARGS2(HTAtom *, host,
326: struct hostent *, element)
327: {
2.25 frystyk 328: host_info *newhost;
2.22 frystyk 329: char *addr;
330: char **index = element->h_addr_list;
331: int cnt = 1;
2.13 frystyk 332: if (!host || !element) {
2.27 frystyk 333: if (PROT_TRACE)
2.36 frystyk 334: fprintf(TDEST, "HostCache... Bad argument to add to cache\n");
2.13 frystyk 335: return NULL;
336: }
2.22 frystyk 337: while(*index++)
338: cnt++;
2.25 frystyk 339: if ((newhost = (host_info *) calloc(1, sizeof(host_info))) == NULL ||
340: (newhost->addrlist = (char **) calloc(1, cnt*sizeof(char*))) == NULL ||
2.22 frystyk 341: (addr = (char *) calloc(1, cnt*element->h_length)) == NULL)
2.18 frystyk 342: outofmem(__FILE__, "HTTCPCacheAddElement");
2.25 frystyk 343: newhost->hostname = host;
2.22 frystyk 344: index = element->h_addr_list;
345: cnt = 0;
346: while (*index) {
2.25 frystyk 347: *(newhost->addrlist+cnt) = addr+cnt*element->h_length;
348: memcpy((void *) *(newhost->addrlist+cnt++), *index++,
349: element->h_length);
2.22 frystyk 350: }
2.25 frystyk 351: newhost->homes = cnt;
2.37 frystyk 352: if ((newhost->weight = (double *) calloc(newhost->homes,
353: sizeof(double))) == NULL)
2.24 frystyk 354: outofmem(__FILE__, "HTTCPCacheAddElement");
355:
2.25 frystyk 356: newhost->addrlength = element->h_length;
2.13 frystyk 357: if (!hostcache)
358: hostcache = HTList_new();
359:
2.27 frystyk 360: if (PROT_TRACE) {
2.25 frystyk 361: if (newhost->homes == 1)
2.36 frystyk 362: fprintf(TDEST, "HostCache... Adding single-homed host `%s'\n",
2.24 frystyk 363: HTAtom_name(host));
2.13 frystyk 364: else
2.36 frystyk 365: fprintf(TDEST, "HostCache... Adding host `%s' with %d homes\n",
2.25 frystyk 366: HTAtom_name(host), newhost->homes);
2.13 frystyk 367: }
2.25 frystyk 368: HTList_addObject(hostcache, (void *) newhost);
2.13 frystyk 369: HTCacheSize++; /* Update number of elements */
2.25 frystyk 370: return newhost;
2.13 frystyk 371: }
372:
373:
374: /* HTTCPAddrWeights
375: **
376: ** This function calculates the weights of the different IP addresses
377: ** on a multi homed host. Each weight is calculated as
378: **
379: ** w(n+1) = w(n)*a + (1-a) * deltatime
380: ** a = exp(-1/Neff)
381: ** Neff is the effective number of samples used
382: ** deltatime is time spend on making a connection
383: **
384: ** A short window (low Neff) gives a high sensibility, but this is
385: ** required as we can't expect a lot of data to test on.
386: **
387: */
388: PUBLIC void HTTCPAddrWeights ARGS2(char *, host, time_t, deltatime)
389: {
390: HTAtom *hostatom = HTAtom_for(host);
391: HTList *cur = hostcache;
392: host_info *pres = NULL;
393: if (!hostcache) {
2.36 frystyk 394: fprintf(TDEST, "HostCache... Weights not calculated, no cache\n");
2.13 frystyk 395: return;
396: }
2.24 frystyk 397: /* Skip any port number from host name */
398: if (strchr(host, ':')) {
399: char *newhost = NULL;
400: char *strptr;
401: StrAllocCopy(newhost, host);
402: strptr = strchr(newhost, ':');
403: *strptr = '\0';
404: hostatom = HTAtom_for(newhost);
405: free(newhost);
406: } else
407: hostatom = HTAtom_for(host);
408:
2.13 frystyk 409: while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
410: if (pres->hostname == hostatom) {
411: break;
412: }
413: }
2.24 frystyk 414: if (pres) {
2.13 frystyk 415: int cnt;
2.37 frystyk 416: CONST double passive = 0.9; /* Factor for all passive IP_addrs */
2.13 frystyk 417: #if 0
2.14 frystyk 418: CONST int Neff = 3;
2.37 frystyk 419: CONST double alpha = exp(-1.0/Neff);
2.13 frystyk 420: #else
2.37 frystyk 421: CONST double alpha = 0.716531310574; /* Doesn't need the math lib */
2.13 frystyk 422: #endif
2.24 frystyk 423: for (cnt=0; cnt<pres->homes; cnt++) {
2.13 frystyk 424: if (cnt == pres->offset) {
425: *(pres->weight+pres->offset) = *(pres->weight+pres->offset)*alpha + (1.0-alpha)*deltatime;
426: } else {
427: *(pres->weight+cnt) = *(pres->weight+cnt) * passive;
428: }
2.24 frystyk 429: if (PROT_TRACE)
2.36 frystyk 430: fprintf(TDEST, "AddrWeights. Home %d has weight %4.2f\n", cnt,
2.24 frystyk 431: *(pres->weight+cnt));
2.13 frystyk 432: }
2.27 frystyk 433: } else if (PROT_TRACE) {
2.36 frystyk 434: fprintf(TDEST, "HostCache... Weights not calculated, host not found in cache: `%s\'\n", host);
2.13 frystyk 435: }
436: }
437:
2.19 frystyk 438: /* ------------------------------------------------------------------------- */
439: /* HOST NAME FUNCTIONS */
440: /* ------------------------------------------------------------------------- */
441:
442: #ifndef DECNET /* Function only used below for a trace message */
443:
444: /* Produce a string for an Internet address
445: ** ----------------------------------------
446: **
447: ** On exit,
448: ** returns a pointer to a static string which must be copied if
2.40 ! frystyk 449: ** it is to be kept. inet_ntoa dumps core on some Sun systems :-(
! 450: ** The current implementation only works for IP-addresses and not in
! 451: ** other address spaces.
2.19 frystyk 452: */
453: PUBLIC CONST char * HTInetString ARGS1(SockA *, sin)
454: {
455: static char string[16];
456: sprintf(string, "%d.%d.%d.%d",
457: (int)*((unsigned char *)(&sin->sin_addr)+0),
458: (int)*((unsigned char *)(&sin->sin_addr)+1),
459: (int)*((unsigned char *)(&sin->sin_addr)+2),
460: (int)*((unsigned char *)(&sin->sin_addr)+3));
461: return string;
462: }
463: #endif /* Decnet */
464:
2.13 frystyk 465:
466: /* HTGetHostByName
467: **
468: ** Searched first the local cache then asks the DNS for an address of
469: ** the host specified.
470: **
2.24 frystyk 471: ** Returns: >0 if OK the number of homes are returned
472: ** -1 if error
2.13 frystyk 473: */
2.24 frystyk 474: PUBLIC int HTGetHostByName ARGS3(char *, host, SockA *, sin,
475: BOOL, use_cur)
2.13 frystyk 476: {
477: HTAtom *hostatom = HTAtom_for(host);
478: host_info *pres = NULL;
479: if (!hostcache)
480: hostcache = HTList_new(); /* First time through */
481: else {
482: HTList *cur = hostcache; /* Search cache */
483: while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
484: if (pres->hostname == hostatom) {
2.27 frystyk 485: if (PROT_TRACE)
2.36 frystyk 486: fprintf(TDEST, "HostByName.. Host `%s\' found in cache.\n", host);
2.13 frystyk 487: break;
488: }
489: }
490: }
491:
492: /* If the host was not found in the cache, then do gethostbyname.
2.24 frystyk 493: If we are talking to a multi homed host then take the IP address with
494: the lowest weight. If `use_cur'=YES then use current IP-address */
2.13 frystyk 495: if (pres) {
2.24 frystyk 496: if (pres->homes > 1 && !use_cur) {
2.13 frystyk 497: int cnt;
2.37 frystyk 498: double best_weight = 1e30; /* Should be FLT_MAX :-( */
2.24 frystyk 499: for (cnt=0; cnt<pres->homes; cnt++) {
2.13 frystyk 500: if (*(pres->weight+cnt) < best_weight) {
501: best_weight = *(pres->weight+cnt);
502: pres->offset = cnt;
503: }
504: }
2.24 frystyk 505: }
506: pres->hits++; /* Update total number of hits on this host */
2.13 frystyk 507: } else { /* Go and ask for it */
508: struct hostent *hostelement; /* see netdb.h */
509: #ifdef MVS /* Outstanding problem with crash in MVS gethostbyname */
2.27 frystyk 510: if (PROT_TRACE)
2.36 frystyk 511: fprintf(TDEST, "HTTCP on MVS gethostbyname(%s)\n", host);
2.13 frystyk 512: #endif
513: if ((hostelement = gethostbyname(host)) == NULL) {
2.27 frystyk 514: if (PROT_TRACE)
2.36 frystyk 515: fprintf(TDEST, "HostByName.. Can't find internet node name `%s'.\n", host);
2.13 frystyk 516: return -1;
517: }
518:
519: /* Add element to the cache and maybe do garbage collection */
520: if (HTCacheSize >= HTConCacheSize)
521: HTTCPCacheGarbage();
522: if ((pres = HTTCPCacheAddElement(hostatom, hostelement)) == NULL) {
523: return -1;
524: }
525: }
526:
527: /* Update socket structure using the element with the lowest weight. On
528: single homed hosts it means the first value */
529: memcpy(&sin->sin_addr, *(pres->addrlist+pres->offset), pres->addrlength);
2.24 frystyk 530: return pres->homes;
2.13 frystyk 531: }
532:
533:
2.19 frystyk 534: /*
535: ** Get host name of the machine on the other end of a socket.
536: **
537: */
538: PUBLIC char * HTGetHostBySock ARGS1(int, soc)
539: {
540: struct sockaddr addr;
541: int len = sizeof(struct sockaddr);
542: struct in_addr *iaddr;
543: struct hostent * phost; /* Pointer to host -- See netdb.h */
544: char *name = NULL;
545:
546: #ifdef DECNET /* Decnet ain't got no damn name server 8#OO */
547: return NULL;
548: #else
549: if (getpeername(soc, &addr, &len) < 0)
550: return NULL;
551:
552: iaddr = &(((struct sockaddr_in *)&addr)->sin_addr);
553: phost=gethostbyaddr((char*)iaddr,
554: sizeof(struct in_addr),
555: AF_INET);
556: if (!phost) {
2.27 frystyk 557: if (PROT_TRACE)
2.36 frystyk 558: fprintf(TDEST, "TCP......... Can't find internet node name for peer!!\n");
2.19 frystyk 559: return NULL;
560: }
561: StrAllocCopy(name, phost->h_name);
2.36 frystyk 562: if (PROT_TRACE) fprintf(TDEST, "TCP......... Peer name is `%s'\n", name);
2.19 frystyk 563:
564: return name;
565:
566: #endif /* not DECNET */
567: }
568:
569:
1.1 timbl 570: /* Parse a network node address and port
571: ** -------------------------------------
572: **
573: ** On entry,
574: ** str points to a string with a node name or number,
575: ** with optional trailing colon and port number.
576: ** sin points to the binary internet or decnet address field.
577: **
2.24 frystyk 578: ** On exit, -1 If error
579: ** >0 If OK the number of homes on the host
1.1 timbl 580: ** *sin is filled in. If no port is specified in str, that
581: ** field is left unchanged in *sin.
2.13 frystyk 582: **
583: ** NOTE: It is assumed that any portnumber and numeric host address
584: ** is given in decimal notation. Separation character is '.'
1.1 timbl 585: */
2.24 frystyk 586: PUBLIC int HTParseInet ARGS3(SockA *, sin, CONST char *, str,
587: BOOL, use_cur)
1.1 timbl 588: {
2.13 frystyk 589: char *host = NULL;
2.24 frystyk 590: int status = 0;
2.13 frystyk 591: StrAllocCopy(host, str); /* Take a copy we can mutilate */
1.1 timbl 592:
2.13 frystyk 593: /* Parse port number if present. */
594: {
595: char *port;
596: if ((port=strchr(host, ':'))) {
597: *port++ = 0; /* Chop off port */
598: if (isdigit(*port)) {
2.27 frystyk 599:
1.1 timbl 600: #ifdef DECNET
2.13 frystyk 601: sin->sdn_objnum = (unsigned char)(strtol(port, (char**)0, 10));
602: #else /* Internet */
603: sin->sin_port = htons(atol(port));
1.1 timbl 604: #endif
2.13 frystyk 605: } else {
2.24 frystyk 606: if (PROT_TRACE)
2.36 frystyk 607: fprintf(TDEST, "ParseInet... No port indicated\n");
2.24 frystyk 608: free(host);
609: return -1;
2.13 frystyk 610: }
1.1 timbl 611: }
2.13 frystyk 612: }
1.1 timbl 613:
2.13 frystyk 614: /* Parse Internet host */
1.1 timbl 615: #ifdef DECNET
616: /* read Decnet node name. @@ Should know about DECnet addresses, but it's
617: probably worth waiting until the Phase transition from IV to V. */
618:
619: sin->sdn_nam.n_len = min(DN_MAXNAML, strlen(host)); /* <=6 in phase 4 */
620: strncpy (sin->sdn_nam.n_name, host, sin->sdn_nam.n_len + 1);
621:
2.36 frystyk 622: if (PROT_TRACE) fprintf(TDEST,
1.1 timbl 623: "DECnet: Parsed address as object number %d on host %.6s...\n",
624: sin->sdn_objnum, host);
625:
2.13 frystyk 626: #else /* Internet */
1.1 timbl 627:
2.13 frystyk 628: /* Parse host number if present */
629: {
630: BOOL numeric = YES;
631: char *strptr = host;
632: while (*strptr) {
633: if (!isdigit(*strptr) && *strptr != '.') {
634: numeric = NO;
635: break;
636: }
637: ++strptr;
638: }
639: if (numeric) {
640: sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
641: } else {
2.24 frystyk 642: if ((status = HTGetHostByName(host, sin, use_cur)) < 0) {
2.13 frystyk 643: free(host);
644: return -1;
645: }
646: }
2.27 frystyk 647: if (PROT_TRACE) {
2.36 frystyk 648: fprintf(TDEST, "ParseInet... Parsed address as port %d on %s\n",
2.13 frystyk 649: (int) ntohs(sin->sin_port),
650: HTInetString(sin));
1.1 timbl 651: }
652: }
653: #endif /* Internet vs. Decnet */
2.13 frystyk 654: free(host);
2.24 frystyk 655: return status;
1.1 timbl 656: }
657:
658:
2.16 frystyk 659: #ifdef OLD_CODE
1.1 timbl 660: /* Derive the name of the host on which we are
661: ** -------------------------------------------
662: **
663: */
2.8 luotonen 664: PRIVATE void get_host_details NOARGS
1.1 timbl 665:
2.36 frystyk 666: #ifndef MAXHOSTNAMELEN
667: #define MAXHOSTNAMELEN 64 /* Arbitrary limit */
668: #endif
669:
1.1 timbl 670: {
671: char name[MAXHOSTNAMELEN+1]; /* The name of this host */
672: struct hostent * phost; /* Pointer to host -- See netdb.h */
673: int namelength = sizeof(name);
674:
675: if (hostname) return; /* Already done */
676: gethostname(name, namelength); /* Without domain */
2.36 frystyk 677: if (PROT_TRACE) fprintf(TDEST, "TCP......... Local host name is %s\n", name);
1.1 timbl 678: StrAllocCopy(hostname, name);
679:
680: #ifndef DECNET /* Decnet ain't got no damn name server 8#OO */
681: phost=gethostbyname(name); /* See netdb.h */
682: if (!phost) {
2.36 frystyk 683: if (PROT_TRACE) fprintf(TDEST,
2.9 luotonen 684: "TCP......... Can't find my own internet node address for `%s'!!\n",
1.1 timbl 685: name);
686: return; /* Fail! */
687: }
688: StrAllocCopy(hostname, phost->h_name);
2.27 frystyk 689: if (PROT_TRACE)
2.36 frystyk 690: fprintf(TDEST, "TCP......... Full local host name is %s\n", hostname);
2.8 luotonen 691:
692: #ifdef NEED_HOST_ADDRESS /* no -- needs name server! */
1.1 timbl 693: memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
2.36 frystyk 694: if (PROT_TRACE) fprintf(TDEST, " Name server says that I am `%s' = %s\n",
1.1 timbl 695: hostname, HTInetString(&HTHostAddress));
2.8 luotonen 696: #endif /* NEED_HOST_ADDRESS */
1.1 timbl 697:
698: #endif /* not Decnet */
699: }
2.16 frystyk 700: #endif /* OLD_CODE */
1.1 timbl 701:
2.19 frystyk 702:
2.24 frystyk 703: /* HTGetDomainName
704: ** Returns the current domain name without the local host name.
705: ** The response is pointing to a static area that might be changed
2.36 frystyk 706: ** using HTSetHostName().
707: **
708: ** Returns NULL on error, "" if domain name is not found
2.24 frystyk 709: */
710: PUBLIC CONST char *HTGetDomainName NOARGS
711: {
712: CONST char *host = HTGetHostName();
713: char *domain;
714: if (host && *host) {
715: if ((domain = strchr(host, '.')) != NULL)
716: return ++domain;
717: else
2.36 frystyk 718: return "";
2.24 frystyk 719: } else
720: return NULL;
721: }
722:
723:
724:
2.19 frystyk 725: /* HTSetHostName
726: ** Sets the current hostname inclusive domain name.
727: ** If this is not set then the default approach is used using
728: ** HTGetHostname().
729: */
730: PUBLIC void HTSetHostName ARGS1(char *, host)
731: {
2.24 frystyk 732: if (host && *host) {
733: char *strptr;
2.19 frystyk 734: StrAllocCopy(hostname, host);
2.24 frystyk 735: strptr = hostname;
736: while (*strptr) {
737: *strptr = TOLOWER(*strptr);
738: strptr++;
739: }
740: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
741: *(hostname+strlen(hostname)-1) = '\0';
742: } else {
2.36 frystyk 743: if (PROT_TRACE) fprintf(TDEST, "SetHostName. Bad argument ignored\n");
2.19 frystyk 744: }
745: }
746:
747:
748: /* HTGetHostName
2.18 frystyk 749: ** Returns the name of this host. It uses the following algoritm:
750: **
751: ** 1) gethostname()
752: ** 2) if the hostname doesn't contain any '.' try to read
753: ** /etc/resolv.conf. If there is no domain line in this file then
754: ** 3) Try getdomainname and do as the man pages say for resolv.conf (sun)
755: ** If there is no domain line in this file, then it is derived
756: ** from the domain name set by the domainname(1) command, usually
757: ** by removing the first component. For example, if the domain-
758: ** name is set to ``foo.podunk.edu'' then the default domain name
759: ** used will be ``pudunk.edu''.
760: **
761: ** This is the same procedure as used by res_init() and sendmail.
2.16 frystyk 762: **
763: ** Return: hostname on success else NULL
764: */
2.19 frystyk 765: PUBLIC CONST char * HTGetHostName NOARGS
1.1 timbl 766: {
2.18 frystyk 767: BOOL got_it = NO;
768: FILE *fp;
2.16 frystyk 769: char name[MAXHOSTNAMELEN+1];
2.18 frystyk 770: if (hostname) { /* If already done */
771: if (*hostname)
772: return hostname;
773: else
774: return NULL; /* We couldn't get the last time */
775: }
2.16 frystyk 776: *(name+MAXHOSTNAMELEN) = '\0';
777: if (gethostname(name, MAXHOSTNAMELEN)) { /* Maybe without domain */
2.27 frystyk 778: if (PROT_TRACE)
2.36 frystyk 779: fprintf(TDEST, "HostName.... Can't get host name\n");
2.16 frystyk 780: return NULL;
781: }
2.27 frystyk 782: if (PROT_TRACE)
2.36 frystyk 783: fprintf(TDEST, "HostName.... Local host name is `%s\'\n", name);
2.16 frystyk 784: StrAllocCopy(hostname, name);
2.24 frystyk 785: {
786: char *strptr = strchr(hostname, '.');
787: if (strptr != NULL) /* We have it all */
788: got_it = YES;
789: }
2.16 frystyk 790:
2.40 ! frystyk 791: #if !(defined(VMS) || defined(WINDOWS))
2.18 frystyk 792: /* Now try the resolver config file */
2.24 frystyk 793: if (!got_it && (fp = fopen(RESOLV_CONF, "r")) != NULL) {
2.18 frystyk 794: char buffer[80];
795: *(buffer+79) = '\0';
796: while (fgets(buffer, 79, fp)) {
797: if (!strncasecomp(buffer, "domain", 6)) {
798: char *domainstr = buffer+6;
799: char *end;
800: while (*domainstr == ' ' || *domainstr == '\t')
801: domainstr++;
802: end = domainstr;
803: while (*end && !isspace(*end))
804: end++;
805: *end = '\0';
806: if (*domainstr) {
807: StrAllocCat(hostname, ".");
808: StrAllocCat(hostname, domainstr);
809: got_it = YES;
810: break;
811: }
812: }
813: }
814: fclose(fp);
2.16 frystyk 815: }
816:
2.18 frystyk 817: /* If everything else has failed then try getdomainname */
2.36 frystyk 818: #ifndef NO_GETDOMAINNAME
2.18 frystyk 819: if (!got_it) {
820: if (getdomainname(name, MAXHOSTNAMELEN)) {
2.27 frystyk 821: if (PROT_TRACE)
2.36 frystyk 822: fprintf(TDEST, "HostName.... Can't get domain name\n");
2.24 frystyk 823: StrAllocCopy(hostname, "");
2.18 frystyk 824: return NULL;
825: }
826:
827: /* If the host name and the first part of the domain name are different
828: then use the former as it is more exact (I guess) */
829: if (strncmp(name, hostname, (int) strlen(hostname))) {
830: char *domain = strchr(name, '.');
2.36 frystyk 831: if (domain)
832: StrAllocCat(hostname, domain);
2.18 frystyk 833: }
2.16 frystyk 834: }
2.36 frystyk 835: #endif /* NO_GETDOMAINNAME */
836: #endif /* not VMS */
2.23 duns 837:
2.24 frystyk 838: {
839: char *strptr = hostname;
840: while (*strptr) {
841: *strptr = TOLOWER(*strptr);
842: strptr++;
843: }
844: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
845: *(hostname+strlen(hostname)-1) = '\0';
846: }
2.27 frystyk 847: if (PROT_TRACE)
2.36 frystyk 848: fprintf(TDEST, "HostName.... Full host name is `%s\'\n", hostname);
2.18 frystyk 849: return hostname;
850:
851: #ifndef DECNET /* Decnet ain't got no damn name server 8#OO */
852: #ifdef OLD_CODE
853: /* Now we try to get information on the domain */
2.16 frystyk 854: {
855: struct hostent *hostelement;
856: if ((hostelement = gethostbyname(hostname)) == NULL) {
2.27 frystyk 857: if (PROT_TRACE)
2.36 frystyk 858: fprintf(TDEST, "HostName.... Can't find host name on DNS\n");
2.16 frystyk 859: FREE(hostname);
860: return NULL;
861: }
2.17 frystyk 862: StrAllocCopy(hostname, (char *) hostelement->h_name);
2.16 frystyk 863: }
2.18 frystyk 864: #endif /* OLD_CODE */
2.16 frystyk 865: #endif /* not Decnet */
2.13 frystyk 866: }
867:
2.19 frystyk 868:
2.32 frystyk 869: /*
870: ** Free the host name. Called from HTLibTerminate
871: */
872: PUBLIC void HTFreeHostName NOARGS
873: {
874: FREE(hostname);
875: }
876:
877:
2.19 frystyk 878: /* HTSetMailAddress
879: ** Sets the current mail address plus host name and domain name.
880: ** If this is not set then the default approach is used using
2.27 frystyk 881: ** HTGetMailAddress(). If the argument is NULL or "" then HTGetMailAddress
882: ** returns NULL on a succeding request.
2.19 frystyk 883: */
884: PUBLIC void HTSetMailAddress ARGS1(char *, address)
885: {
2.27 frystyk 886: if (!address || !*address)
887: StrAllocCopy(mailaddress, "");
888: else
2.19 frystyk 889: StrAllocCopy(mailaddress, address);
2.27 frystyk 890: if (TRACE)
2.36 frystyk 891: fprintf(TDEST, "SetMailAdr.. Set mail address to `%s\'\n",
2.27 frystyk 892: mailaddress);
2.19 frystyk 893: }
894:
895:
896: /* HTGetMailAddress
897: **
898: ** Get the mail address of the current user on the current host. The
899: ** domain name used is the one initialized in HTSetHostName or
900: ** HTGetHostName. The login name is determined using (ordered):
901: **
902: ** getlogin
903: ** getpwuid(getuid())
904: **
905: ** The weakness about the last attempt is if the user has multiple
906: ** login names each with the same user ID. If this fails as well then:
907: **
908: ** LOGNAME environment variable
909: ** USER environment variable
910: **
911: ** Returns NULL if error else pointer to static string
912: */
913: PUBLIC CONST char * HTGetMailAddress NOARGS
914: {
915: char *login;
916: CONST char *domain;
917: struct passwd *pw_info;
2.21 frystyk 918: if (mailaddress) {
919: if (*mailaddress)
920: return mailaddress;
921: else
922: return NULL; /* No luck the last time so we wont try again */
923: }
2.23 duns 924:
2.36 frystyk 925: #ifdef VMS
2.23 duns 926: if ((login = (char *) cuserid(NULL)) == NULL) {
2.36 frystyk 927: if (PROT_TRACE) fprintf(TDEST, "MailAddress. cuserid returns NULL\n");
928: }
929:
930: #else
931: #ifdef _WINDOWS
2.40 ! frystyk 932: #ifdef WIN32
! 933: login = getenv("USERNAME");
! 934: #else
2.36 frystyk 935: login = "PCUSER"; /* @@@ COULD BE BETTER @@@ */
2.40 ! frystyk 936: #endif /* _WINDOWS */
2.36 frystyk 937: #else /* Unix like... */
2.34 roeber 938: if ((login = (char *) getlogin()) == NULL) {
2.36 frystyk 939: if (PROT_TRACE)
940: fprintf(TDEST, "MailAddress. getlogin returns NULL\n");
941: if ((pw_info = getpwuid(getuid())) == NULL) {
942: if (PROT_TRACE)
943: fprintf(TDEST, "MailAddress. getpwid returns NULL\n");
944: if ((login = getenv("LOGNAME")) == NULL) {
945: if (PROT_TRACE)
946: fprintf(TDEST, "MailAddress. LOGNAME not found\n");
947: if ((login = getenv("USER")) == NULL) {
948: if (PROT_TRACE)
949: fprintf(TDEST,"MailAddress. USER not found\n");
950: return NULL; /* I GIVE UP */
951: }
952: }
953: } else
954: login = pw_info->pw_name;
955: }
2.40 ! frystyk 956: #endif /* Unix like */
! 957: #endif /* VMS */
2.34 roeber 958:
2.19 frystyk 959: if (login) {
960: StrAllocCopy(mailaddress, login);
961: StrAllocCat(mailaddress, "@");
962: if ((domain = HTGetHostName()) != NULL)
963: StrAllocCat(mailaddress, domain);
2.21 frystyk 964: else {
965: *mailaddress = '\0';
966: return NULL; /* Domain name not available */
967: }
2.19 frystyk 968: return mailaddress;
969: }
970: return NULL;
971: }
2.32 frystyk 972:
973:
974: /*
975: ** Free the mail address. Called from HTLibTerminate
976: */
977: PUBLIC void HTFreeMailAddress NOARGS
978: {
979: FREE(mailaddress);
980: }
981:
2.19 frystyk 982:
983: /* ------------------------------------------------------------------------- */
984: /* CONNECTION ESTABLISHMENT MANAGEMENT */
985: /* ------------------------------------------------------------------------- */
2.13 frystyk 986:
987: /* HTDoConnect()
988: **
989: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
990: ** the default_port value.
991: **
2.40 ! frystyk 992: ** returns HT_ERROR Error has occured or interrupted
! 993: ** HT_OK if connected
! 994: ** HT_WOULD_BLOCK if operation would have blocked
! 995: ** HT_INTERRUPTED if interrupted
2.13 frystyk 996: */
2.24 frystyk 997: PUBLIC int HTDoConnect ARGS5(HTNetInfo *, net, char *, url,
998: u_short, default_port, u_long *, addr,
999: BOOL, use_cur)
2.13 frystyk 1000: {
1001: int status;
1002: char *p1 = HTParse(url, "", PARSE_HOST);
1003: char *at_sign;
1004: char *host;
1005:
1006: /* if theres an @ then use the stuff after it as a hostname */
2.27 frystyk 1007: if((at_sign = strchr(p1, '@')) != NULL)
2.13 frystyk 1008: host = at_sign+1;
1009: else
1010: host = p1;
2.24 frystyk 1011: if (!*host) {
1012: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_HOST,
1013: NULL, 0, "HTDoConnect");
1014: free(p1);
2.40 ! frystyk 1015: return HT_ERROR;
2.27 frystyk 1016: } else {
1017: if (PROT_TRACE)
2.36 frystyk 1018: fprintf(TDEST, "HTDoConnect. Looking up `%s\'\n", host);
2.27 frystyk 1019: }
2.13 frystyk 1020:
1021: /* Set up defaults */
2.36 frystyk 1022: if (net->sockfd==INVSOC) {
2.27 frystyk 1023: memset((void *) &net->sock_addr, '\0', sizeof(net->sock_addr));
2.13 frystyk 1024: #ifdef DECNET
2.27 frystyk 1025: net->sock_addr.sdn_family = AF_DECnet;/* Family = DECnet, host order */
1026: net->sock_addr.sdn_objnum = DNP_OBJ; /* Default: http object number */
2.13 frystyk 1027: #else /* Internet */
2.27 frystyk 1028: net->sock_addr.sin_family = AF_INET;
1029: net->sock_addr.sin_port = htons(default_port);
2.13 frystyk 1030: #endif
2.27 frystyk 1031: }
2.13 frystyk 1032:
2.24 frystyk 1033: /* If we are trying to connect to a multi-homed host then loop here until
1034: success or we have tried all IP-addresses */
1035: do {
2.36 frystyk 1036: if (net->sockfd==INVSOC) {
2.27 frystyk 1037: int hosts;
1038: if ((hosts = HTParseInet(&net->sock_addr, host, use_cur)) < 0) {
1039: if (PROT_TRACE)
2.36 frystyk 1040: fprintf(TDEST, "HTDoConnect. Can't locate remote host `%s\'\n", host);
2.27 frystyk 1041: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_REMOTE_HOST,
1042: (void *) host, strlen(host), "HTDoConnect");
1043: break;
2.40 ! frystyk 1044: }
2.27 frystyk 1045: if (!net->addressCount && hosts > 1)
1046: net->addressCount = hosts;
1047: #ifdef DECNET
2.36 frystyk 1048: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
2.27 frystyk 1049: #else
2.36 frystyk 1050: if ((net->sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVSOC)
2.27 frystyk 1051: #endif
1052: {
2.36 frystyk 1053: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO, "socket");
2.27 frystyk 1054: break;
1055: }
1056: if (addr)
1057: *addr = ntohl(net->sock_addr.sin_addr.s_addr);
1058: if (PROT_TRACE)
2.36 frystyk 1059: fprintf(TDEST, "HTDoConnect. Created socket number %d\n",
2.27 frystyk 1060: net->sockfd);
1061:
2.28 frystyk 1062: /* If non-blocking protocol then change socket status
1063: ** I use FCNTL so that I can ask the status before I set it.
1064: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
1065: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
1066: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
1067: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
1068: */
2.27 frystyk 1069: if (!HTProtocolBlocking(net->request)) {
2.36 frystyk 1070: #if defined(_WINDOWS) || defined(VMS)
1071: {
1072: int enable = 1;
1073: status = IOCTL(net->sockfd, FIONBIO, &enable);
1074: }
1075: #else
2.27 frystyk 1076: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.28 frystyk 1077: status |= O_NONBLOCK; /* POSIX */
2.27 frystyk 1078: status = FCNTL(net->sockfd, F_SETFL, status);
1079: }
2.34 roeber 1080: #endif
2.27 frystyk 1081: if (status == -1) {
1082: if (PROT_TRACE)
2.36 frystyk 1083: fprintf(TDEST, "HTDoConnect. Can NOT make socket non-blocking\n");
2.33 frystyk 1084: } else if (PROT_TRACE)
2.36 frystyk 1085: fprintf(TDEST, "HTDoConnect. Using NON_BLOCKING I/O\n");
2.27 frystyk 1086: }
1087:
1088: /* If multi-homed host then start timer on connection */
1089: if (net->addressCount >= 1)
1090: net->connecttime = time(NULL);
2.24 frystyk 1091: }
2.13 frystyk 1092:
2.27 frystyk 1093: /* Check for interrupt */
2.40 ! frystyk 1094: if (HTThreadIntr(net->sockfd)) { /* connect call was interrupted */
2.27 frystyk 1095: if (NETCLOSE(net->sockfd) < 0)
2.36 frystyk 1096: HTErrorSysAdd(net->request, ERR_FATAL, socerrno,NO,"NETCLOSE");
2.27 frystyk 1097: HTThreadState(net->sockfd, THD_CLOSE);
2.36 frystyk 1098: net->sockfd = INVSOC;
2.27 frystyk 1099: free(p1);
1100: return HT_INTERRUPTED;
1101: }
1102:
1103: /* Do a connect */
1104: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
2.40 ! frystyk 1105:
2.27 frystyk 1106: sizeof(net->sock_addr));
1107: /*
1108: * According to the Sun man page for connect:
1109: * EINPROGRESS The socket is non-blocking and the con-
1110: * nection cannot be completed immediately.
1111: * It is possible to select(2) for comple-
1112: * tion by selecting the socket for writ-
1113: * ing.
1114: * According to the Motorola SVR4 man page for connect:
1115: * EAGAIN The socket is non-blocking and the con-
1116: * nection cannot be completed immediately.
1117: * It is possible to select for completion
1118: * by selecting the socket for writing.
1119: * However, this is only possible if the
1120: * socket STREAMS module is the topmost
1121: * module on the protocol stack with a
1122: * write service procedure. This will be
1123: * the normal case.
1124: */
1125: #ifdef EAGAIN
2.40 ! frystyk 1126:
2.36 frystyk 1127: if ((status < 0) && ((socerrno==EINPROGRESS) || (socerrno==EAGAIN)))
2.13 frystyk 1128: #else
2.36 frystyk 1129: if ((status < 0) && (socerrno == EINPROGRESS))
2.40 ! frystyk 1130: #ifdef WSAEWOULDBLOCK /* WinSock API */
! 1131: if ((status == SOCKET_ERROR) && (socerrno == WSAEWOULDBLOCK))
! 1132: #else
2.27 frystyk 1133: #endif /* EAGAIN */
2.40 ! frystyk 1134: #endif /* WSAEWOULDBLOCK */
2.24 frystyk 1135: {
2.27 frystyk 1136: if (PROT_TRACE)
2.36 frystyk 1137: fprintf(TDEST, "HTDoConnect. WOULD BLOCK `%s'\n", host);
2.27 frystyk 1138: HTThreadState(net->sockfd, THD_SET_WRITE);
1139: free(p1);
1140: return HT_WOULD_BLOCK;
2.24 frystyk 1141: }
2.27 frystyk 1142: if (net->addressCount >= 1) {
2.40 ! frystyk 1143: net->connecttime = time((long *)0) - net->connecttime;
2.24 frystyk 1144: if (status < 0) {
2.36 frystyk 1145: if (socerrno==EISCONN) { /* connect multi after would block */
2.27 frystyk 1146: HTThreadState(net->sockfd, THD_CLR_WRITE);
1147: HTTCPAddrWeights(host, net->connecttime);
1148: free(p1);
1149: net->addressCount = 0;
2.40 ! frystyk 1150: return HT_OK;
! 1151: if (PROT_TRACE)
! 1152: fprintf(TDEST, "HTDoConnect. Socket %ld already connected\n", net->sockfd);
2.27 frystyk 1153: }
2.36 frystyk 1154: HTErrorSysAdd(net->request, ERR_NON_FATAL, socerrno, NO,
2.40 ! frystyk 1155:
2.36 frystyk 1156: "connect");
2.27 frystyk 1157:
1158: /* I have added EINVAL `invalid argument' as this is what I
1159: get back from a non-blocking connect where I should
2.36 frystyk 1160: get `connection refused' on SVR4 */
1161: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
2.40 ! frystyk 1162:
2.36 frystyk 1163: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
1164: #ifdef __srv4__
1165: socerrno==EHOSTDOWN || socerrno==EINVAL)
1166: #else
1167: socerrno==EHOSTDOWN)
2.35 roeber 1168: #endif
2.27 frystyk 1169: net->connecttime += TCP_DELAY;
2.24 frystyk 1170: else
2.27 frystyk 1171: net->connecttime += TCP_PENALTY;
2.24 frystyk 1172: if (NETCLOSE(net->sockfd) < 0)
2.40 ! frystyk 1173:
2.36 frystyk 1174: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
1175: "NETCLOSE");
2.27 frystyk 1176: HTThreadState(net->sockfd, THD_CLOSE);
2.36 frystyk 1177: net->sockfd = INVSOC;
2.27 frystyk 1178: HTTCPAddrWeights(host, net->connecttime);
1179: } else { /* Connect on multi-homed */
1180: HTTCPAddrWeights(host, net->connecttime);
1181: free(p1);
1182: net->addressCount = 0;
2.40 ! frystyk 1183: return HT_OK;
2.27 frystyk 1184: }
1185: } else if (status < 0) {
2.36 frystyk 1186: if (socerrno==EISCONN) { /* Connect single after would block */
2.27 frystyk 1187: HTThreadState(net->sockfd, THD_CLR_WRITE);
1188: net->addressCount = 0;
1189: free(p1);
2.40 ! frystyk 1190: return HT_OK;
2.24 frystyk 1191: } else {
2.36 frystyk 1192: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
1193: "connect");
2.27 frystyk 1194: HTTCPCacheRemoveHost(host);
1195: if (NETCLOSE(net->sockfd) < 0)
2.36 frystyk 1196: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
1197: "NETCLOSE");
2.27 frystyk 1198: HTThreadState(net->sockfd, THD_CLOSE);
2.24 frystyk 1199: break;
1200: }
2.27 frystyk 1201: } else { /* Connect on single homed */
1202: free(p1);
1203: net->addressCount = 0;
2.40 ! frystyk 1204: return HT_OK;
2.24 frystyk 1205: }
2.27 frystyk 1206: } while (--net->addressCount);
2.13 frystyk 1207:
2.27 frystyk 1208: if (PROT_TRACE)
2.36 frystyk 1209: fprintf(TDEST, "HTDoConnect. Connect failed\n");
2.24 frystyk 1210: free (p1);
1211: net->addressCount = 0;
2.36 frystyk 1212: net->sockfd = INVSOC;
2.40 ! frystyk 1213: return HT_ERROR;
2.13 frystyk 1214: }
1215:
1216:
1217: /* HTDoAccept()
1218: **
1219: ** This function makes a non-blocking accept on a port and polls every
1220: ** second until MAX_ACCEPT_POLL or interrupted by user.
1221: **
1222: ** BUGS Interrupted is not yet implemented!!!
1223: **
2.27 frystyk 1224: ** Returns HT_WOULD_BLOCK if waiting
1225: ** 0 if OK,
1226: ** -1 on error
2.13 frystyk 1227: */
2.15 frystyk 1228: PUBLIC int HTDoAccept ARGS1(HTNetInfo *, net)
2.13 frystyk 1229: {
1230: SockA soc_address; /* SockA is defined in tcp.h */
1231: int status;
1232: int cnt;
1233: int soc_addrlen = sizeof(soc_address);
2.36 frystyk 1234: if (net->sockfd==INVSOC) {
1235: if (PROT_TRACE) fprintf(TDEST, "HTDoAccept.. Bad socket number\n");
2.13 frystyk 1236: return -1;
1237: }
2.27 frystyk 1238:
2.13 frystyk 1239: /* First make the socket non-blocking */
2.36 frystyk 1240: #if defined(_WINDOWS) || defined(VMS)
2.23 duns 1241: {
2.36 frystyk 1242: int enable = 1; /* Need the variable! */
1243: status = IOCTL(net->sockfd, FIONBIO, enable);
2.23 duns 1244: }
2.36 frystyk 1245: #else
2.15 frystyk 1246: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.36 frystyk 1247: status |= O_NONBLOCK; /* POSIX */
2.15 frystyk 1248: status = FCNTL(net->sockfd, F_SETFL, status);
2.13 frystyk 1249: }
2.36 frystyk 1250: #endif
2.13 frystyk 1251: if (status == -1) {
2.36 frystyk 1252: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO, "IOCTL");
2.13 frystyk 1253: return -1;
1254: }
1255:
1256: /* Now poll every sekund */
1257: for(cnt=0; cnt<MAX_ACCEPT_POLL; cnt++) {
2.15 frystyk 1258: if ((status = accept(net->sockfd, (struct sockaddr*) &soc_address,
2.36 frystyk 1259: &soc_addrlen)) != INVSOC) {
1260: if (PROT_TRACE) fprintf(TDEST,
2.13 frystyk 1261: "HTDoAccept.. Accepted new socket %d\n",
1262: status);
1263: return status;
1264: } else
2.36 frystyk 1265: HTErrorSysAdd(net->request, ERR_WARN, socerrno, YES, "accept");
2.38 frystyk 1266: SLEEP(1);
1267: }
2.13 frystyk 1268:
1269: /* If nothing has happened */
2.27 frystyk 1270: if (PROT_TRACE)
2.36 frystyk 1271: fprintf(TDEST, "HTDoAccept.. Timed out, no connection!\n");
2.15 frystyk 1272: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_TIME_OUT, NULL, 0,
1273: "HTDoAccept");
2.13 frystyk 1274: return -1;
1.1 timbl 1275: }
2.27 frystyk 1276:
1.1 timbl 1277:
Webmaster