Annotation of libwww/Library/src/HTTCP.c, revision 2.45
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 */
515: if ((hostelement = gethostbyname(host)) == NULL) {
2.27 frystyk 516: if (PROT_TRACE)
2.36 frystyk 517: fprintf(TDEST, "HostByName.. Can't find internet node name `%s'.\n", host);
2.13 frystyk 518: return -1;
519: }
520:
521: /* Add element to the cache and maybe do garbage collection */
522: if (HTCacheSize >= HTConCacheSize)
523: HTTCPCacheGarbage();
524: if ((pres = HTTCPCacheAddElement(hostatom, hostelement)) == NULL) {
525: return -1;
526: }
527: }
528:
529: /* Update socket structure using the element with the lowest weight. On
530: single homed hosts it means the first value */
531: memcpy(&sin->sin_addr, *(pres->addrlist+pres->offset), pres->addrlength);
2.24 frystyk 532: return pres->homes;
2.13 frystyk 533: }
534:
535:
2.19 frystyk 536: /*
537: ** Get host name of the machine on the other end of a socket.
538: **
539: */
540: PUBLIC char * HTGetHostBySock ARGS1(int, soc)
541: {
542: struct sockaddr addr;
543: int len = sizeof(struct sockaddr);
544: struct in_addr *iaddr;
545: struct hostent * phost; /* Pointer to host -- See netdb.h */
546: char *name = NULL;
547:
548: #ifdef DECNET /* Decnet ain't got no damn name server 8#OO */
549: return NULL;
550: #else
551: if (getpeername(soc, &addr, &len) < 0)
552: return NULL;
553:
554: iaddr = &(((struct sockaddr_in *)&addr)->sin_addr);
555: phost=gethostbyaddr((char*)iaddr,
556: sizeof(struct in_addr),
557: AF_INET);
558: if (!phost) {
2.27 frystyk 559: if (PROT_TRACE)
2.36 frystyk 560: fprintf(TDEST, "TCP......... Can't find internet node name for peer!!\n");
2.19 frystyk 561: return NULL;
562: }
563: StrAllocCopy(name, phost->h_name);
2.36 frystyk 564: if (PROT_TRACE) fprintf(TDEST, "TCP......... Peer name is `%s'\n", name);
2.19 frystyk 565:
566: return name;
567:
568: #endif /* not DECNET */
569: }
570:
571:
1.1 timbl 572: /* Parse a network node address and port
573: ** -------------------------------------
574: **
575: ** On entry,
576: ** str points to a string with a node name or number,
577: ** with optional trailing colon and port number.
578: ** sin points to the binary internet or decnet address field.
579: **
2.24 frystyk 580: ** On exit, -1 If error
581: ** >0 If OK the number of homes on the host
1.1 timbl 582: ** *sin is filled in. If no port is specified in str, that
583: ** field is left unchanged in *sin.
2.13 frystyk 584: **
585: ** NOTE: It is assumed that any portnumber and numeric host address
586: ** is given in decimal notation. Separation character is '.'
1.1 timbl 587: */
2.24 frystyk 588: PUBLIC int HTParseInet ARGS3(SockA *, sin, CONST char *, str,
589: BOOL, use_cur)
1.1 timbl 590: {
2.13 frystyk 591: char *host = NULL;
2.24 frystyk 592: int status = 0;
2.13 frystyk 593: StrAllocCopy(host, str); /* Take a copy we can mutilate */
1.1 timbl 594:
2.13 frystyk 595: /* Parse port number if present. */
596: {
597: char *port;
598: if ((port=strchr(host, ':'))) {
599: *port++ = 0; /* Chop off port */
600: if (isdigit(*port)) {
2.27 frystyk 601:
1.1 timbl 602: #ifdef DECNET
2.13 frystyk 603: sin->sdn_objnum = (unsigned char)(strtol(port, (char**)0, 10));
604: #else /* Internet */
605: sin->sin_port = htons(atol(port));
1.1 timbl 606: #endif
2.13 frystyk 607: } else {
2.24 frystyk 608: if (PROT_TRACE)
2.36 frystyk 609: fprintf(TDEST, "ParseInet... No port indicated\n");
2.24 frystyk 610: free(host);
611: return -1;
2.13 frystyk 612: }
1.1 timbl 613: }
2.13 frystyk 614: }
1.1 timbl 615:
2.13 frystyk 616: /* Parse Internet host */
1.1 timbl 617: #ifdef DECNET
618: /* read Decnet node name. @@ Should know about DECnet addresses, but it's
619: probably worth waiting until the Phase transition from IV to V. */
620:
621: sin->sdn_nam.n_len = min(DN_MAXNAML, strlen(host)); /* <=6 in phase 4 */
622: strncpy (sin->sdn_nam.n_name, host, sin->sdn_nam.n_len + 1);
623:
2.36 frystyk 624: if (PROT_TRACE) fprintf(TDEST,
1.1 timbl 625: "DECnet: Parsed address as object number %d on host %.6s...\n",
626: sin->sdn_objnum, host);
627:
2.13 frystyk 628: #else /* Internet */
1.1 timbl 629:
2.13 frystyk 630: /* Parse host number if present */
631: {
632: BOOL numeric = YES;
633: char *strptr = host;
634: while (*strptr) {
635: if (!isdigit(*strptr) && *strptr != '.') {
636: numeric = NO;
637: break;
638: }
639: ++strptr;
640: }
641: if (numeric) {
642: sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
643: } else {
2.24 frystyk 644: if ((status = HTGetHostByName(host, sin, use_cur)) < 0) {
2.13 frystyk 645: free(host);
646: return -1;
647: }
648: }
2.27 frystyk 649: if (PROT_TRACE) {
2.36 frystyk 650: fprintf(TDEST, "ParseInet... Parsed address as port %d on %s\n",
2.13 frystyk 651: (int) ntohs(sin->sin_port),
652: HTInetString(sin));
1.1 timbl 653: }
654: }
655: #endif /* Internet vs. Decnet */
2.13 frystyk 656: free(host);
2.24 frystyk 657: return status;
1.1 timbl 658: }
659:
660:
2.16 frystyk 661: #ifdef OLD_CODE
1.1 timbl 662: /* Derive the name of the host on which we are
663: ** -------------------------------------------
664: **
665: */
2.8 luotonen 666: PRIVATE void get_host_details NOARGS
1.1 timbl 667:
2.36 frystyk 668: #ifndef MAXHOSTNAMELEN
669: #define MAXHOSTNAMELEN 64 /* Arbitrary limit */
670: #endif
671:
1.1 timbl 672: {
673: char name[MAXHOSTNAMELEN+1]; /* The name of this host */
674: struct hostent * phost; /* Pointer to host -- See netdb.h */
675: int namelength = sizeof(name);
676:
677: if (hostname) return; /* Already done */
678: gethostname(name, namelength); /* Without domain */
2.36 frystyk 679: if (PROT_TRACE) fprintf(TDEST, "TCP......... Local host name is %s\n", name);
1.1 timbl 680: StrAllocCopy(hostname, name);
681:
682: #ifndef DECNET /* Decnet ain't got no damn name server 8#OO */
683: phost=gethostbyname(name); /* See netdb.h */
684: if (!phost) {
2.36 frystyk 685: if (PROT_TRACE) fprintf(TDEST,
2.9 luotonen 686: "TCP......... Can't find my own internet node address for `%s'!!\n",
1.1 timbl 687: name);
688: return; /* Fail! */
689: }
690: StrAllocCopy(hostname, phost->h_name);
2.27 frystyk 691: if (PROT_TRACE)
2.36 frystyk 692: fprintf(TDEST, "TCP......... Full local host name is %s\n", hostname);
2.8 luotonen 693:
694: #ifdef NEED_HOST_ADDRESS /* no -- needs name server! */
1.1 timbl 695: memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
2.36 frystyk 696: if (PROT_TRACE) fprintf(TDEST, " Name server says that I am `%s' = %s\n",
1.1 timbl 697: hostname, HTInetString(&HTHostAddress));
2.8 luotonen 698: #endif /* NEED_HOST_ADDRESS */
1.1 timbl 699:
700: #endif /* not Decnet */
701: }
2.16 frystyk 702: #endif /* OLD_CODE */
1.1 timbl 703:
2.19 frystyk 704:
2.24 frystyk 705: /* HTGetDomainName
706: ** Returns the current domain name without the local host name.
707: ** The response is pointing to a static area that might be changed
2.36 frystyk 708: ** using HTSetHostName().
709: **
710: ** Returns NULL on error, "" if domain name is not found
2.24 frystyk 711: */
712: PUBLIC CONST char *HTGetDomainName NOARGS
713: {
714: CONST char *host = HTGetHostName();
715: char *domain;
716: if (host && *host) {
717: if ((domain = strchr(host, '.')) != NULL)
718: return ++domain;
719: else
2.36 frystyk 720: return "";
2.24 frystyk 721: } else
722: return NULL;
723: }
724:
725:
726:
2.19 frystyk 727: /* HTSetHostName
728: ** Sets the current hostname inclusive domain name.
729: ** If this is not set then the default approach is used using
730: ** HTGetHostname().
731: */
732: PUBLIC void HTSetHostName ARGS1(char *, host)
733: {
2.24 frystyk 734: if (host && *host) {
735: char *strptr;
2.19 frystyk 736: StrAllocCopy(hostname, host);
2.24 frystyk 737: strptr = hostname;
738: while (*strptr) {
739: *strptr = TOLOWER(*strptr);
740: strptr++;
741: }
742: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
743: *(hostname+strlen(hostname)-1) = '\0';
744: } else {
2.36 frystyk 745: if (PROT_TRACE) fprintf(TDEST, "SetHostName. Bad argument ignored\n");
2.19 frystyk 746: }
747: }
748:
749:
750: /* HTGetHostName
2.18 frystyk 751: ** Returns the name of this host. It uses the following algoritm:
752: **
753: ** 1) gethostname()
754: ** 2) if the hostname doesn't contain any '.' try to read
755: ** /etc/resolv.conf. If there is no domain line in this file then
756: ** 3) Try getdomainname and do as the man pages say for resolv.conf (sun)
757: ** If there is no domain line in this file, then it is derived
758: ** from the domain name set by the domainname(1) command, usually
759: ** by removing the first component. For example, if the domain-
760: ** name is set to ``foo.podunk.edu'' then the default domain name
761: ** used will be ``pudunk.edu''.
762: **
763: ** This is the same procedure as used by res_init() and sendmail.
2.16 frystyk 764: **
765: ** Return: hostname on success else NULL
766: */
2.19 frystyk 767: PUBLIC CONST char * HTGetHostName NOARGS
1.1 timbl 768: {
2.18 frystyk 769: BOOL got_it = NO;
770: FILE *fp;
2.16 frystyk 771: char name[MAXHOSTNAMELEN+1];
2.18 frystyk 772: if (hostname) { /* If already done */
773: if (*hostname)
774: return hostname;
775: else
776: return NULL; /* We couldn't get the last time */
777: }
2.16 frystyk 778: *(name+MAXHOSTNAMELEN) = '\0';
779: if (gethostname(name, MAXHOSTNAMELEN)) { /* Maybe without domain */
2.27 frystyk 780: if (PROT_TRACE)
2.36 frystyk 781: fprintf(TDEST, "HostName.... Can't get host name\n");
2.16 frystyk 782: return NULL;
783: }
2.27 frystyk 784: if (PROT_TRACE)
2.36 frystyk 785: fprintf(TDEST, "HostName.... Local host name is `%s\'\n", name);
2.16 frystyk 786: StrAllocCopy(hostname, name);
2.24 frystyk 787: {
788: char *strptr = strchr(hostname, '.');
789: if (strptr != NULL) /* We have it all */
790: got_it = YES;
791: }
2.16 frystyk 792:
2.40 frystyk 793: #if !(defined(VMS) || defined(WINDOWS))
2.18 frystyk 794: /* Now try the resolver config file */
2.24 frystyk 795: if (!got_it && (fp = fopen(RESOLV_CONF, "r")) != NULL) {
2.18 frystyk 796: char buffer[80];
797: *(buffer+79) = '\0';
798: while (fgets(buffer, 79, fp)) {
799: if (!strncasecomp(buffer, "domain", 6)) {
800: char *domainstr = buffer+6;
801: char *end;
802: while (*domainstr == ' ' || *domainstr == '\t')
803: domainstr++;
804: end = domainstr;
805: while (*end && !isspace(*end))
806: end++;
807: *end = '\0';
808: if (*domainstr) {
809: StrAllocCat(hostname, ".");
810: StrAllocCat(hostname, domainstr);
811: got_it = YES;
812: break;
813: }
814: }
815: }
816: fclose(fp);
2.16 frystyk 817: }
818:
2.18 frystyk 819: /* If everything else has failed then try getdomainname */
2.36 frystyk 820: #ifndef NO_GETDOMAINNAME
2.18 frystyk 821: if (!got_it) {
822: if (getdomainname(name, MAXHOSTNAMELEN)) {
2.27 frystyk 823: if (PROT_TRACE)
2.36 frystyk 824: fprintf(TDEST, "HostName.... Can't get domain name\n");
2.24 frystyk 825: StrAllocCopy(hostname, "");
2.18 frystyk 826: return NULL;
827: }
828:
829: /* If the host name and the first part of the domain name are different
830: then use the former as it is more exact (I guess) */
831: if (strncmp(name, hostname, (int) strlen(hostname))) {
832: char *domain = strchr(name, '.');
2.36 frystyk 833: if (domain)
834: StrAllocCat(hostname, domain);
2.18 frystyk 835: }
2.16 frystyk 836: }
2.36 frystyk 837: #endif /* NO_GETDOMAINNAME */
838: #endif /* not VMS */
2.23 duns 839:
2.24 frystyk 840: {
841: char *strptr = hostname;
842: while (*strptr) {
843: *strptr = TOLOWER(*strptr);
844: strptr++;
845: }
846: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
847: *(hostname+strlen(hostname)-1) = '\0';
848: }
2.27 frystyk 849: if (PROT_TRACE)
2.36 frystyk 850: fprintf(TDEST, "HostName.... Full host name is `%s\'\n", hostname);
2.18 frystyk 851: return hostname;
852:
853: #ifndef DECNET /* Decnet ain't got no damn name server 8#OO */
854: #ifdef OLD_CODE
855: /* Now we try to get information on the domain */
2.16 frystyk 856: {
857: struct hostent *hostelement;
858: if ((hostelement = gethostbyname(hostname)) == NULL) {
2.27 frystyk 859: if (PROT_TRACE)
2.36 frystyk 860: fprintf(TDEST, "HostName.... Can't find host name on DNS\n");
2.16 frystyk 861: FREE(hostname);
862: return NULL;
863: }
2.17 frystyk 864: StrAllocCopy(hostname, (char *) hostelement->h_name);
2.16 frystyk 865: }
2.18 frystyk 866: #endif /* OLD_CODE */
2.16 frystyk 867: #endif /* not Decnet */
2.13 frystyk 868: }
869:
2.19 frystyk 870:
2.32 frystyk 871: /*
872: ** Free the host name. Called from HTLibTerminate
873: */
874: PUBLIC void HTFreeHostName NOARGS
875: {
876: FREE(hostname);
877: }
878:
879:
2.19 frystyk 880: /* HTSetMailAddress
881: ** Sets the current mail address plus host name and domain name.
882: ** If this is not set then the default approach is used using
2.27 frystyk 883: ** HTGetMailAddress(). If the argument is NULL or "" then HTGetMailAddress
884: ** returns NULL on a succeding request.
2.19 frystyk 885: */
886: PUBLIC void HTSetMailAddress ARGS1(char *, address)
887: {
2.27 frystyk 888: if (!address || !*address)
889: StrAllocCopy(mailaddress, "");
890: else
2.19 frystyk 891: StrAllocCopy(mailaddress, address);
2.27 frystyk 892: if (TRACE)
2.36 frystyk 893: fprintf(TDEST, "SetMailAdr.. Set mail address to `%s\'\n",
2.27 frystyk 894: mailaddress);
2.19 frystyk 895: }
896:
897:
898: /* HTGetMailAddress
899: **
900: ** Get the mail address of the current user on the current host. The
901: ** domain name used is the one initialized in HTSetHostName or
902: ** HTGetHostName. The login name is determined using (ordered):
903: **
904: ** getlogin
905: ** getpwuid(getuid())
906: **
907: ** The weakness about the last attempt is if the user has multiple
908: ** login names each with the same user ID. If this fails as well then:
909: **
910: ** LOGNAME environment variable
911: ** USER environment variable
912: **
913: ** Returns NULL if error else pointer to static string
914: */
915: PUBLIC CONST char * HTGetMailAddress NOARGS
916: {
917: char *login;
918: CONST char *domain;
919: struct passwd *pw_info;
2.21 frystyk 920: if (mailaddress) {
921: if (*mailaddress)
922: return mailaddress;
923: else
924: return NULL; /* No luck the last time so we wont try again */
925: }
2.23 duns 926:
2.36 frystyk 927: #ifdef VMS
2.23 duns 928: if ((login = (char *) cuserid(NULL)) == NULL) {
2.36 frystyk 929: if (PROT_TRACE) fprintf(TDEST, "MailAddress. cuserid returns NULL\n");
930: }
931:
932: #else
2.40 frystyk 933: #ifdef WIN32
2.41 frystyk 934: login = getenv("USERNAME") ;
2.40 frystyk 935: #else
2.41 frystyk 936: #ifdef _WINDOWS
2.36 frystyk 937: login = "PCUSER"; /* @@@ COULD BE BETTER @@@ */
938: #else /* Unix like... */
2.34 roeber 939: if ((login = (char *) getlogin()) == NULL) {
2.36 frystyk 940: if (PROT_TRACE)
941: fprintf(TDEST, "MailAddress. getlogin returns NULL\n");
942: if ((pw_info = getpwuid(getuid())) == NULL) {
943: if (PROT_TRACE)
944: fprintf(TDEST, "MailAddress. getpwid returns NULL\n");
945: if ((login = getenv("LOGNAME")) == NULL) {
946: if (PROT_TRACE)
947: fprintf(TDEST, "MailAddress. LOGNAME not found\n");
948: if ((login = getenv("USER")) == NULL) {
949: if (PROT_TRACE)
950: fprintf(TDEST,"MailAddress. USER not found\n");
951: return NULL; /* I GIVE UP */
952: }
953: }
954: } else
955: login = pw_info->pw_name;
956: }
2.41 frystyk 957: #endif /* WINDOWS 3.1 */
2.40 frystyk 958: #endif /* Unix like */
959: #endif /* VMS */
2.34 roeber 960:
2.19 frystyk 961: if (login) {
962: StrAllocCopy(mailaddress, login);
963: StrAllocCat(mailaddress, "@");
964: if ((domain = HTGetHostName()) != NULL)
965: StrAllocCat(mailaddress, domain);
2.21 frystyk 966: else {
967: *mailaddress = '\0';
968: return NULL; /* Domain name not available */
969: }
2.19 frystyk 970: return mailaddress;
971: }
972: return NULL;
973: }
2.32 frystyk 974:
975:
976: /*
977: ** Free the mail address. Called from HTLibTerminate
978: */
979: PUBLIC void HTFreeMailAddress NOARGS
980: {
981: FREE(mailaddress);
982: }
983:
2.19 frystyk 984:
985: /* ------------------------------------------------------------------------- */
986: /* CONNECTION ESTABLISHMENT MANAGEMENT */
987: /* ------------------------------------------------------------------------- */
2.13 frystyk 988:
989: /* HTDoConnect()
990: **
991: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
992: ** the default_port value.
993: **
2.40 frystyk 994: ** returns HT_ERROR Error has occured or interrupted
995: ** HT_OK if connected
996: ** HT_WOULD_BLOCK if operation would have blocked
997: ** HT_INTERRUPTED if interrupted
2.13 frystyk 998: */
2.24 frystyk 999: PUBLIC int HTDoConnect ARGS5(HTNetInfo *, net, char *, url,
1000: u_short, default_port, u_long *, addr,
1001: BOOL, use_cur)
2.13 frystyk 1002: {
1003: int status;
1004: char *p1 = HTParse(url, "", PARSE_HOST);
1005: char *at_sign;
1006: char *host;
1007:
1008: /* if theres an @ then use the stuff after it as a hostname */
2.27 frystyk 1009: if((at_sign = strchr(p1, '@')) != NULL)
2.13 frystyk 1010: host = at_sign+1;
1011: else
1012: host = p1;
2.24 frystyk 1013: if (!*host) {
1014: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_HOST,
1015: NULL, 0, "HTDoConnect");
1016: free(p1);
2.40 frystyk 1017: return HT_ERROR;
2.27 frystyk 1018: }
2.13 frystyk 1019:
2.44 frystyk 1020: /* Set up defaults */
2.36 frystyk 1021: if (net->sockfd==INVSOC) {
2.27 frystyk 1022: memset((void *) &net->sock_addr, '\0', sizeof(net->sock_addr));
2.13 frystyk 1023: #ifdef DECNET
2.27 frystyk 1024: net->sock_addr.sdn_family = AF_DECnet;/* Family = DECnet, host order */
1025: net->sock_addr.sdn_objnum = DNP_OBJ; /* Default: http object number */
2.13 frystyk 1026: #else /* Internet */
2.27 frystyk 1027: net->sock_addr.sin_family = AF_INET;
1028: net->sock_addr.sin_port = htons(default_port);
2.13 frystyk 1029: #endif
2.27 frystyk 1030: }
2.41 frystyk 1031:
2.24 frystyk 1032: /* If we are trying to connect to a multi-homed host then loop here until
1033: success or we have tried all IP-addresses */
1034: do {
2.36 frystyk 1035: if (net->sockfd==INVSOC) {
2.27 frystyk 1036: int hosts;
2.44 frystyk 1037: if (PROT_TRACE)
1038: fprintf(TDEST, "HTDoConnect. Looking up `%s\'\n", host);
2.27 frystyk 1039: if ((hosts = HTParseInet(&net->sock_addr, host, use_cur)) < 0) {
1040: if (PROT_TRACE)
2.36 frystyk 1041: fprintf(TDEST, "HTDoConnect. Can't locate remote host `%s\'\n", host);
2.27 frystyk 1042: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_REMOTE_HOST,
1043: (void *) host, strlen(host), "HTDoConnect");
1044: break;
2.41 frystyk 1045: }
2.27 frystyk 1046: if (!net->addressCount && hosts > 1)
1047: net->addressCount = hosts;
1048: #ifdef DECNET
2.36 frystyk 1049: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
2.27 frystyk 1050: #else
2.36 frystyk 1051: if ((net->sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVSOC)
2.27 frystyk 1052: #endif
1053: {
2.36 frystyk 1054: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO, "socket");
2.27 frystyk 1055: break;
1056: }
1057: if (addr)
1058: *addr = ntohl(net->sock_addr.sin_addr.s_addr);
1059: if (PROT_TRACE)
2.36 frystyk 1060: fprintf(TDEST, "HTDoConnect. Created socket number %d\n",
2.27 frystyk 1061: net->sockfd);
1062:
2.28 frystyk 1063: /* If non-blocking protocol then change socket status
2.41 frystyk 1064: ** I use FCNTL so that I can ask the status before I set it.
1065: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
1066: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
1067: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
1068: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
1069: */
2.45 ! frystyk 1070: if (!HTProtocol_isBlocking(net->request)) {
2.41 frystyk 1071: #ifdef _WINDOWS
1072: { /* begin windows scope */
1073: HTRequest * rq = net->request;
1074: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
1075: FD_CONNECT | FD_CLOSE ;
1076: int rv = 0 ;
1077:
1078: #ifndef _WIN32
1079: if (net->request->hwnd == 0) {
1080:
1081: }
1082: #endif
1083: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
1084:
1085: if (net->request->hwnd != 0) {
1086: rv = WSAAsyncSelect( net->sockfd, rq->hwnd,
1087: rq->winMsg, levents);
1088: if (rv == SOCKET_ERROR) {
1089: status = -1 ;
1090: if (PROT_TRACE)
1091: fprintf(TDEST,
1092: "HTDoConnect: WSAAsyncSelect() fails: %d\n",
1093: WSAGetLastError());
1094: } /* error returns */
1095: } else {
1096: int enable = 1 ;
1097: status = IOCTL(net->sockfd, FIONBIO, &enable);
1098: }
1099: } /* end scope */
1100: #else
1101: #if defined(VMS)
2.36 frystyk 1102: {
1103: int enable = 1;
1104: status = IOCTL(net->sockfd, FIONBIO, &enable);
1105: }
1106: #else
2.27 frystyk 1107: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.41 frystyk 1108: status |= O_NONBLOCK; /* POSIX */
2.27 frystyk 1109: status = FCNTL(net->sockfd, F_SETFL, status);
1110: }
2.41 frystyk 1111: #endif /* VMS */
1112: #endif /* WINDOW */
2.43 frystyk 1113: if (PROT_TRACE) {
1114: if (status == -1)
1115: fprintf(TDEST, "HTDoConnect. Can't make socket non-blocking\n");
1116: else
1117: fprintf(TDEST, "HTDoConnect. Using NON_BLOCKING I/O\n");
1118: }
2.27 frystyk 1119: }
1120:
1121: /* If multi-homed host then start timer on connection */
1122: if (net->addressCount >= 1)
1123: net->connecttime = time(NULL);
2.41 frystyk 1124: } /* IF socket is invalid */
1125:
2.27 frystyk 1126: /* Check for interrupt */
2.41 frystyk 1127: if (HTThreadIntr(net->sockfd)) {
2.27 frystyk 1128: if (NETCLOSE(net->sockfd) < 0)
2.36 frystyk 1129: HTErrorSysAdd(net->request, ERR_FATAL, socerrno,NO,"NETCLOSE");
2.27 frystyk 1130: HTThreadState(net->sockfd, THD_CLOSE);
2.36 frystyk 1131: net->sockfd = INVSOC;
2.27 frystyk 1132: free(p1);
1133: return HT_INTERRUPTED;
1134: }
2.41 frystyk 1135:
2.27 frystyk 1136: /* Do a connect */
1137: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
1138: sizeof(net->sock_addr));
1139: /*
1140: * According to the Sun man page for connect:
1141: * EINPROGRESS The socket is non-blocking and the con-
1142: * nection cannot be completed immediately.
1143: * It is possible to select(2) for comple-
1144: * tion by selecting the socket for writ-
1145: * ing.
1146: * According to the Motorola SVR4 man page for connect:
1147: * EAGAIN The socket is non-blocking and the con-
1148: * nection cannot be completed immediately.
1149: * It is possible to select for completion
1150: * by selecting the socket for writing.
1151: * However, this is only possible if the
1152: * socket STREAMS module is the topmost
1153: * module on the protocol stack with a
1154: * write service procedure. This will be
1155: * the normal case.
1156: */
2.41 frystyk 1157:
2.27 frystyk 1158: #ifdef EAGAIN
2.36 frystyk 1159: if ((status < 0) && ((socerrno==EINPROGRESS) || (socerrno==EAGAIN)))
2.41 frystyk 1160: #else
2.40 frystyk 1161: #ifdef WSAEWOULDBLOCK /* WinSock API */
1162: if ((status == SOCKET_ERROR) && (socerrno == WSAEWOULDBLOCK))
1163: #else
2.41 frystyk 1164: if ((status < 0) && (socerrno == EINPROGRESS))
1165: #endif /* WSAEWOULDBLOCK */
2.27 frystyk 1166: #endif /* EAGAIN */
2.24 frystyk 1167: {
2.27 frystyk 1168: if (PROT_TRACE)
2.36 frystyk 1169: fprintf(TDEST, "HTDoConnect. WOULD BLOCK `%s'\n", host);
2.27 frystyk 1170: HTThreadState(net->sockfd, THD_SET_WRITE);
1171: free(p1);
1172: return HT_WOULD_BLOCK;
2.24 frystyk 1173: }
2.41 frystyk 1174:
1175: /* We have 4 situations: single OK, Pb and multi OK, pb */
2.27 frystyk 1176: if (net->addressCount >= 1) {
2.40 frystyk 1177: net->connecttime = time((long *)0) - net->connecttime;
2.41 frystyk 1178: if (status < 0) { /* multi PB */
1179: if (socerrno == EISCONN) { /* connect multi after would block*/
2.27 frystyk 1180: HTThreadState(net->sockfd, THD_CLR_WRITE);
1181: HTTCPAddrWeights(host, net->connecttime);
1182: free(p1);
1183: net->addressCount = 0;
2.41 frystyk 1184: if (PROT_TRACE)
1185: fprintf(TDEST, "HTDoConnect: Socket %ld already connected\n", net->sockfd) ;
2.40 frystyk 1186: return HT_OK;
2.27 frystyk 1187: }
2.41 frystyk 1188:
2.36 frystyk 1189: HTErrorSysAdd(net->request, ERR_NON_FATAL, socerrno, NO,
1190: "connect");
2.27 frystyk 1191:
1192: /* I have added EINVAL `invalid argument' as this is what I
1193: get back from a non-blocking connect where I should
2.36 frystyk 1194: get `connection refused' on SVR4 */
2.41 frystyk 1195:
2.36 frystyk 1196: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
1197: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
1198: #ifdef __srv4__
1199: socerrno==EHOSTDOWN || socerrno==EINVAL)
1200: #else
1201: socerrno==EHOSTDOWN)
2.35 roeber 1202: #endif
2.27 frystyk 1203: net->connecttime += TCP_DELAY;
2.41 frystyk 1204: else
2.27 frystyk 1205: net->connecttime += TCP_PENALTY;
2.40 frystyk 1206:
2.41 frystyk 1207: if (NETCLOSE(net->sockfd) < 0)
1208: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
1209: "NETCLOSE");
1210: HTThreadState(net->sockfd, THD_CLOSE);
1211: net->sockfd = INVSOC;
1212: HTTCPAddrWeights(host, net->connecttime);
1213: } else { /* multi OK */
2.27 frystyk 1214: HTTCPAddrWeights(host, net->connecttime);
1215: free(p1);
1216: net->addressCount = 0;
2.40 frystyk 1217: return HT_OK;
2.27 frystyk 1218: }
2.41 frystyk 1219: } else if (status < 0) { /* single PB */
1220: if (socerrno==EISCONN) { /* Connect single after would block */
2.27 frystyk 1221: HTThreadState(net->sockfd, THD_CLR_WRITE);
1222: net->addressCount = 0;
1223: free(p1);
2.40 frystyk 1224: return HT_OK;
2.24 frystyk 1225: } else {
2.36 frystyk 1226: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
2.41 frystyk 1227: "connect");
2.27 frystyk 1228: HTTCPCacheRemoveHost(host);
1229: if (NETCLOSE(net->sockfd) < 0)
2.36 frystyk 1230: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO,
1231: "NETCLOSE");
2.27 frystyk 1232: HTThreadState(net->sockfd, THD_CLOSE);
2.24 frystyk 1233: break;
1234: }
2.41 frystyk 1235: } else { /* single OK */
2.27 frystyk 1236: free(p1);
1237: net->addressCount = 0;
2.40 frystyk 1238: return HT_OK;
2.24 frystyk 1239: }
2.41 frystyk 1240: } while (--net->addressCount); /* End of mega loop */
2.13 frystyk 1241:
2.27 frystyk 1242: if (PROT_TRACE)
2.41 frystyk 1243: fprintf(TDEST, "HTDoConnect. Connect failed\n");
2.24 frystyk 1244: free (p1);
1245: net->addressCount = 0;
2.36 frystyk 1246: net->sockfd = INVSOC;
2.40 frystyk 1247: return HT_ERROR;
2.13 frystyk 1248: }
1249:
1250:
1251: /* HTDoAccept()
1252: **
1253: ** This function makes a non-blocking accept on a port and polls every
1254: ** second until MAX_ACCEPT_POLL or interrupted by user.
1255: **
1256: ** BUGS Interrupted is not yet implemented!!!
1257: **
2.27 frystyk 1258: ** Returns HT_WOULD_BLOCK if waiting
1259: ** 0 if OK,
1260: ** -1 on error
2.13 frystyk 1261: */
2.15 frystyk 1262: PUBLIC int HTDoAccept ARGS1(HTNetInfo *, net)
2.13 frystyk 1263: {
1264: SockA soc_address; /* SockA is defined in tcp.h */
1265: int status;
1266: int cnt;
1267: int soc_addrlen = sizeof(soc_address);
2.36 frystyk 1268: if (net->sockfd==INVSOC) {
1269: if (PROT_TRACE) fprintf(TDEST, "HTDoAccept.. Bad socket number\n");
2.13 frystyk 1270: return -1;
1271: }
2.27 frystyk 1272:
2.13 frystyk 1273: /* First make the socket non-blocking */
2.36 frystyk 1274: #if defined(_WINDOWS) || defined(VMS)
2.23 duns 1275: {
2.36 frystyk 1276: int enable = 1; /* Need the variable! */
1277: status = IOCTL(net->sockfd, FIONBIO, enable);
2.23 duns 1278: }
2.36 frystyk 1279: #else
2.15 frystyk 1280: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.36 frystyk 1281: status |= O_NONBLOCK; /* POSIX */
2.15 frystyk 1282: status = FCNTL(net->sockfd, F_SETFL, status);
2.13 frystyk 1283: }
2.36 frystyk 1284: #endif
2.13 frystyk 1285: if (status == -1) {
2.36 frystyk 1286: HTErrorSysAdd(net->request, ERR_FATAL, socerrno, NO, "IOCTL");
2.13 frystyk 1287: return -1;
1288: }
1289:
1290: /* Now poll every sekund */
1291: for(cnt=0; cnt<MAX_ACCEPT_POLL; cnt++) {
2.15 frystyk 1292: if ((status = accept(net->sockfd, (struct sockaddr*) &soc_address,
2.36 frystyk 1293: &soc_addrlen)) != INVSOC) {
1294: if (PROT_TRACE) fprintf(TDEST,
2.13 frystyk 1295: "HTDoAccept.. Accepted new socket %d\n",
1296: status);
1297: return status;
1298: } else
2.36 frystyk 1299: HTErrorSysAdd(net->request, ERR_WARN, socerrno, YES, "accept");
2.38 frystyk 1300: SLEEP(1);
1301: }
2.13 frystyk 1302:
1303: /* If nothing has happened */
2.27 frystyk 1304: if (PROT_TRACE)
2.36 frystyk 1305: fprintf(TDEST, "HTDoAccept.. Timed out, no connection!\n");
2.15 frystyk 1306: HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_TIME_OUT, NULL, 0,
1307: "HTDoAccept");
2.13 frystyk 1308: return -1;
1.1 timbl 1309: }
2.27 frystyk 1310:
1.1 timbl 1311:
Webmaster