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