Annotation of libwww/Library/src/HTTCP.c, revision 2.80
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 */
2.79 frystyk 19: #include "sysdep.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.50 frystyk 25: #include "HTAlert.h"
2.13 frystyk 26: #include "HTError.h"
2.58 frystyk 27: #include "HTReqMan.h"
2.54 frystyk 28: #include "HTNetMan.h"
29: #include "HTDNS.h"
2.36 frystyk 30: #include "HTTCP.h" /* Implemented here */
2.29 frystyk 31:
2.36 frystyk 32: /* VMS stuff */
33: #ifdef VMS
34: #ifndef MULTINET
35: #define FD_SETSIZE 32
36: #else /* Multinet */
37: #define FD_SETSIZE 256
38: #endif /* Multinet */
39: #endif /* VMS */
40:
2.13 frystyk 41: /* Macros and other defines */
2.24 frystyk 42: /* x seconds penalty on a multi-homed host if IP-address is down */
43: #define TCP_PENALTY 1200
44:
45: /* x seconds penalty on a multi-homed host if IP-address is timed out */
46: #define TCP_DELAY 600
47:
2.54 frystyk 48: PRIVATE char *hostname = NULL; /* The name of this host */
2.19 frystyk 49: PRIVATE char *mailaddress = NULL; /* Current mail address */
2.11 duns 50:
2.13 frystyk 51: /* ------------------------------------------------------------------------- */
1.1 timbl 52:
2.36 frystyk 53: /*
54: ** Returns the string equivalent to the errno passed in the argument.
2.37 frystyk 55: ** We can't use errno directly as we have both errno and socerrno. The
2.36 frystyk 56: ** result is a static buffer.
1.1 timbl 57: */
2.79 frystyk 58: PUBLIC const char * HTErrnoString (int errornumber)
1.1 timbl 59: {
2.40 frystyk 60: #ifdef HAVE_STRERROR
2.36 frystyk 61: return strerror(errornumber);
2.34 roeber 62: #else
2.79 frystyk 63: #ifdef HAVE_SYS_ERRLIST
64: #ifdef HAVE_SYS_NERR
65: return (errno < sys_nerr ? sys_errlist[errno] : "Unknown error");
66: #else
67: return sys_errlist[errno];
68: #endif /* HAVE_SYS_NERR */
69: #else
2.36 frystyk 70: #ifdef VMS
2.12 luotonen 71: static char buf[60];
2.79 frystyk 72: sprintf(buf, "Unix errno=%ld dec, VMS error=%lx hex", errornumber,
2.36 frystyk 73: vaxc$errno);
2.12 luotonen 74: return buf;
2.79 frystyk 75: #else
76: #ifdef _WINSOCKAPI_
77: static char buf[60];
78: sprintf(buf, "Unix errno=%ld dec, WinSock error=%ld", errornumber,
79: WSAGetLastError());
80: return buf;
2.40 frystyk 81: #else
2.79 frystyk 82: return "(Error number not translated)";
83: #endif /* _WINSOCKAPI_ */
2.40 frystyk 84: #endif /* VMS */
2.79 frystyk 85: #endif /* HAVE_SYS_ERRLIST */
2.51 frystyk 86: #endif /* HAVE_STRERROR */
2.12 luotonen 87: }
88:
2.36 frystyk 89:
90: /* Debug error message
2.12 luotonen 91: */
2.63 frystyk 92: PUBLIC int HTInetStatus (int errnum, char * where)
2.12 luotonen 93: {
2.79 frystyk 94: #ifdef VMS
95: if (PROT_TRACE) HTTrace("System Error Unix = %ld dec\n", errno);
96: if (PROT_TRACE) HTTrace("System Error VMS = %lx hex\n", vaxc$errno);
97: return (-vaxc$errno);
98: #else
99: #ifdef _WINSOCKAPI_
100: if (PROT_TRACE) HTTrace("System Error Unix = %ld dec\n", errno);
101: if (PROT_TRACE) HTTrace("System Error WinSock error=%lx hex\n",
102: WSAGetLastError());
103: return (-errnum);
104: #else
2.27 frystyk 105: if (PROT_TRACE)
2.79 frystyk 106: HTTrace("System Error %d after call to %s() failed\n............ %s\n",
107: errno, where, HTErrnoString(errnum));
108: return (-errnum);
109: #endif /* _WINSOCKAPI_ */
2.36 frystyk 110: #endif /* VMS */
1.1 timbl 111: }
112:
113:
114: /* Parse a cardinal value parse_cardinal()
115: ** ----------------------
116: **
117: ** On entry,
118: ** *pp points to first character to be interpreted, terminated by
119: ** non 0:9 character.
120: ** *pstatus points to status already valid
121: ** maxvalue gives the largest allowable value.
122: **
123: ** On exit,
124: ** *pp points to first unread character
125: ** *pstatus points to status updated iff bad
126: */
127:
2.63 frystyk 128: PUBLIC unsigned int HTCardinal (int * pstatus,
129: char ** pp,
130: unsigned int max_value)
1.1 timbl 131: {
2.36 frystyk 132: unsigned int n=0;
1.1 timbl 133: if ( (**pp<'0') || (**pp>'9')) { /* Null string is error */
134: *pstatus = -3; /* No number where one expeceted */
135: return 0;
136: }
137: while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
138:
139: if (n>max_value) {
140: *pstatus = -4; /* Cardinal outside range */
141: return 0;
142: }
143:
144: return n;
145: }
146:
2.19 frystyk 147: /* ------------------------------------------------------------------------- */
2.27 frystyk 148: /* SIGNAL HANDLING */
149: /* ------------------------------------------------------------------------- */
150:
2.70 frystyk 151: #ifdef WWWLIB_SIG
2.27 frystyk 152: /* HTSetSignal
153: ** This function sets up signal handlers. This might not be necessary to
154: ** call if the application has its own handlers.
155: */
156: #include <signal.h>
2.56 frystyk 157: PUBLIC void HTSetSignal (void)
2.27 frystyk 158: {
159: /* On some systems (SYSV) it is necessary to catch the SIGPIPE signal
160: ** when attemting to connect to a remote host where you normally should
161: ** get `connection refused' back
162: */
163: if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
2.77 eric 164: if (PROT_TRACE) HTTrace("HTSignal.... Can't catch SIGPIPE\n");
2.27 frystyk 165: } else {
2.77 eric 166: if (PROT_TRACE) HTTrace("HTSignal.... Ignoring SIGPIPE\n");
2.27 frystyk 167: }
168: }
2.70 frystyk 169: #else
170: #ifdef WWW_WIN_DLL
171: PUBLIC void HTSetSignal (void) {}
172: #endif /* WWW_WIN_DLL */
2.27 frystyk 173: #endif /* WWWLIB_SIG */
174:
175: /* ------------------------------------------------------------------------- */
2.19 frystyk 176: /* HOST NAME FUNCTIONS */
177: /* ------------------------------------------------------------------------- */
178:
179: /* Produce a string for an Internet address
180: ** ----------------------------------------
181: **
182: ** On exit,
183: ** returns a pointer to a static string which must be copied if
2.41 frystyk 184: ** it is to be kept.
2.19 frystyk 185: */
2.79 frystyk 186: PUBLIC const char * HTInetString (SockA * sin)
2.19 frystyk 187: {
2.53 frystyk 188: #ifndef DECNET /* Function only used below for a trace message */
2.41 frystyk 189: #if 0
190: /* This dumps core on some Sun systems :-(. The problem is now, that
191: the current implememtation only works for IP-addresses and not in
192: other address spaces. */
193: return inet_ntoa(sin->sin_addr);
194: #endif
2.19 frystyk 195: static char string[16];
196: sprintf(string, "%d.%d.%d.%d",
197: (int)*((unsigned char *)(&sin->sin_addr)+0),
198: (int)*((unsigned char *)(&sin->sin_addr)+1),
199: (int)*((unsigned char *)(&sin->sin_addr)+2),
200: (int)*((unsigned char *)(&sin->sin_addr)+3));
201: return string;
2.53 frystyk 202: #else
203: return "";
204: #endif /* Decnet */
2.19 frystyk 205: }
206:
1.1 timbl 207: /* Parse a network node address and port
208: ** -------------------------------------
2.54 frystyk 209: ** It is assumed that any portnumber and numeric host address
210: ** is given in decimal notation. Separation character is '.'
211: ** Any port number given in host name overrides all other values.
212: ** 'host' might be modified.
213: ** Returns:
214: ** >0 Number of homes
215: ** 0 Wait for persistent socket
216: ** -1 Error
1.1 timbl 217: */
2.54 frystyk 218: PRIVATE int HTParseInet (HTNet * net, char * host)
1.1 timbl 219: {
2.54 frystyk 220: int status = 1;
221: SockA *sin = &net->sock_addr;
2.27 frystyk 222:
1.1 timbl 223: #ifdef DECNET
224: /* read Decnet node name. @@ Should know about DECnet addresses, but it's
225: probably worth waiting until the Phase transition from IV to V. */
226:
227: sin->sdn_nam.n_len = min(DN_MAXNAML, strlen(host)); /* <=6 in phase 4 */
228: strncpy (sin->sdn_nam.n_name, host, sin->sdn_nam.n_len + 1);
229:
2.77 eric 230: if (PROT_TRACE) HTTrace(
1.1 timbl 231: "DECnet: Parsed address as object number %d on host %.6s...\n",
232: sin->sdn_objnum, host);
2.13 frystyk 233: #else /* Internet */
234: {
235: char *strptr = host;
236: while (*strptr) {
2.54 frystyk 237: if (*strptr == ':') {
238: *strptr = '\0'; /* Don't want port number in numeric host */
2.13 frystyk 239: break;
240: }
2.54 frystyk 241: if (!isdigit(*strptr) && *strptr != '.')
242: break;
243: strptr++;
2.13 frystyk 244: }
2.54 frystyk 245: if (!*strptr) {
2.51 frystyk 246: #ifdef GUSI
247: sin->sin_addr = inet_addr(host); /* See netinet/in.h */
248: #else
2.13 frystyk 249: sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
2.51 frystyk 250: #endif
2.54 frystyk 251: } else
252: status = HTGetHostByName(net, host);
253:
2.27 frystyk 254: if (PROT_TRACE) {
2.54 frystyk 255: if (status > 0)
2.77 eric 256: HTTrace("ParseInet... as port %d on %s with %d homes\n",
2.54 frystyk 257: (int) ntohs(sin->sin_port), HTInetString(sin), status);
1.1 timbl 258: }
259: }
2.54 frystyk 260: #endif /* Internet vs. Decnet */
2.24 frystyk 261: return status;
1.1 timbl 262: }
263:
264:
2.24 frystyk 265: /* HTGetDomainName
266: ** Returns the current domain name without the local host name.
267: ** The response is pointing to a static area that might be changed
2.36 frystyk 268: ** using HTSetHostName().
269: **
270: ** Returns NULL on error, "" if domain name is not found
2.24 frystyk 271: */
2.79 frystyk 272: PUBLIC const char *HTGetDomainName (void)
2.24 frystyk 273: {
2.79 frystyk 274: const char *host = HTGetHostName();
2.24 frystyk 275: char *domain;
276: if (host && *host) {
277: if ((domain = strchr(host, '.')) != NULL)
278: return ++domain;
279: else
2.36 frystyk 280: return "";
2.24 frystyk 281: } else
282: return NULL;
283: }
284:
285:
2.19 frystyk 286: /* HTSetHostName
287: ** Sets the current hostname inclusive domain name.
288: ** If this is not set then the default approach is used using
289: ** HTGetHostname().
290: */
2.63 frystyk 291: PUBLIC void HTSetHostName (char * host)
2.19 frystyk 292: {
2.24 frystyk 293: if (host && *host) {
294: char *strptr;
2.19 frystyk 295: StrAllocCopy(hostname, host);
2.24 frystyk 296: strptr = hostname;
297: while (*strptr) {
298: *strptr = TOLOWER(*strptr);
299: strptr++;
300: }
301: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
302: *(hostname+strlen(hostname)-1) = '\0';
303: } else {
2.77 eric 304: if (PROT_TRACE) HTTrace("SetHostName. Bad argument ignored\n");
2.19 frystyk 305: }
306: }
307:
308:
309: /* HTGetHostName
2.18 frystyk 310: ** Returns the name of this host. It uses the following algoritm:
311: **
312: ** 1) gethostname()
313: ** 2) if the hostname doesn't contain any '.' try to read
314: ** /etc/resolv.conf. If there is no domain line in this file then
315: ** 3) Try getdomainname and do as the man pages say for resolv.conf (sun)
2.49 frystyk 316: ** If there is no domain line in this file, then it is derived
317: ** from the domain name set by the domainname(1) command, usually
318: ** by removing the first component. For example, if the domain-
319: ** name is set to ``foo.podunk.edu'' then the default domain name
320: ** used will be ``pudunk.edu''.
2.18 frystyk 321: **
322: ** This is the same procedure as used by res_init() and sendmail.
2.16 frystyk 323: **
324: ** Return: hostname on success else NULL
325: */
2.79 frystyk 326: PUBLIC const char * HTGetHostName (void)
1.1 timbl 327: {
2.79 frystyk 328: int fqdn = 0; /* 0=no, 1=host, 2=fqdn */
2.18 frystyk 329: FILE *fp;
2.16 frystyk 330: char name[MAXHOSTNAMELEN+1];
2.18 frystyk 331: if (hostname) { /* If already done */
332: if (*hostname)
333: return hostname;
334: else
335: return NULL; /* We couldn't get the last time */
336: }
2.16 frystyk 337: *(name+MAXHOSTNAMELEN) = '\0';
2.52 frystyk 338:
2.79 frystyk 339: #ifdef HAVE_SYSINFO
340: if (!fqdn && sysinfo(SI_HOSTNAME, name, MAXHOSTNAMELEN) > 0) {
341: char * dot = strchr(name, '.');
342: if (PROT_TRACE) HTTrace("HostName.... sysinfo says `%s\'\n", name);
343: StrAllocCopy(hostname, name);
344: fqdn = dot ? 2 : 1;
345: }
346: #endif /* HAVE_SYSINFO */
347:
348: #ifdef HAVE_GETHOSTNAME
349: if (!fqdn && gethostname(name, MAXHOSTNAMELEN) == 0) {
350: char * dot = strchr(name, '.');
351: if (PROT_TRACE) HTTrace("HostName.... gethostname says `%s\'\n", name);
352: StrAllocCopy(hostname, name);
353: fqdn = dot ? 2 : 1;
2.24 frystyk 354: }
2.79 frystyk 355: #endif /* HAVE_GETHOSTNAME */
2.16 frystyk 356:
2.79 frystyk 357: #ifdef RESOLV_CONF
2.18 frystyk 358: /* Now try the resolver config file */
2.79 frystyk 359: if (fqdn==1 && (fp = fopen(RESOLV_CONF, "r")) != NULL) {
2.18 frystyk 360: char buffer[80];
361: *(buffer+79) = '\0';
362: while (fgets(buffer, 79, fp)) {
363: if (!strncasecomp(buffer, "domain", 6)) {
364: char *domainstr = buffer+6;
365: char *end;
366: while (*domainstr == ' ' || *domainstr == '\t')
367: domainstr++;
368: end = domainstr;
369: while (*end && !isspace(*end))
370: end++;
371: *end = '\0';
372: if (*domainstr) {
373: StrAllocCat(hostname, ".");
374: StrAllocCat(hostname, domainstr);
2.79 frystyk 375: fqdn = YES;
2.18 frystyk 376: break;
377: }
378: }
379: }
380: fclose(fp);
2.16 frystyk 381: }
2.79 frystyk 382: #endif /* RESOLV_CONF */
2.16 frystyk 383:
2.79 frystyk 384: #ifdef HAVE_GETDOMAINNAME
2.18 frystyk 385: /* If everything else has failed then try getdomainname */
2.79 frystyk 386: if (fqdn==1) {
2.18 frystyk 387: if (getdomainname(name, MAXHOSTNAMELEN)) {
2.27 frystyk 388: if (PROT_TRACE)
2.77 eric 389: HTTrace("HostName.... Can't get domain name\n");
2.24 frystyk 390: StrAllocCopy(hostname, "");
2.18 frystyk 391: return NULL;
392: }
393:
394: /* If the host name and the first part of the domain name are different
395: then use the former as it is more exact (I guess) */
396: if (strncmp(name, hostname, (int) strlen(hostname))) {
397: char *domain = strchr(name, '.');
2.50 frystyk 398: if (!domain)
399: domain = name;
400: StrAllocCat(hostname, domain);
2.18 frystyk 401: }
2.16 frystyk 402: }
2.79 frystyk 403: #endif /* HAVE_GETDOMAINNAME */
2.23 duns 404:
2.79 frystyk 405: if (hostname) {
2.24 frystyk 406: char *strptr = hostname;
407: while (*strptr) {
408: *strptr = TOLOWER(*strptr);
409: strptr++;
410: }
411: if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */
412: *(hostname+strlen(hostname)-1) = '\0';
2.79 frystyk 413: if (PROT_TRACE) HTTrace("HostName.... FQDN is `%s\'\n", hostname);
2.24 frystyk 414: }
2.18 frystyk 415: return hostname;
2.13 frystyk 416: }
417:
2.19 frystyk 418:
2.32 frystyk 419: /*
420: ** Free the host name. Called from HTLibTerminate
421: */
2.56 frystyk 422: PUBLIC void HTFreeHostName (void)
2.32 frystyk 423: {
2.76 frystyk 424: HT_FREE(hostname);
2.32 frystyk 425: }
426:
427:
2.19 frystyk 428: /* HTSetMailAddress
429: ** Sets the current mail address plus host name and domain name.
430: ** If this is not set then the default approach is used using
2.27 frystyk 431: ** HTGetMailAddress(). If the argument is NULL or "" then HTGetMailAddress
432: ** returns NULL on a succeding request.
2.19 frystyk 433: */
2.63 frystyk 434: PUBLIC void HTSetMailAddress (char * address)
2.19 frystyk 435: {
2.27 frystyk 436: if (!address || !*address)
437: StrAllocCopy(mailaddress, "");
438: else
2.19 frystyk 439: StrAllocCopy(mailaddress, address);
2.59 frystyk 440: if (WWWTRACE)
2.77 eric 441: HTTrace("SetMailAdr.. Set mail address to `%s\'\n",
2.27 frystyk 442: mailaddress);
2.19 frystyk 443: }
444:
445:
446: /* HTGetMailAddress
447: **
448: ** Get the mail address of the current user on the current host. The
449: ** domain name used is the one initialized in HTSetHostName or
450: ** HTGetHostName. The login name is determined using (ordered):
451: **
452: ** getlogin
453: ** getpwuid(getuid())
454: **
455: ** The weakness about the last attempt is if the user has multiple
456: ** login names each with the same user ID. If this fails as well then:
457: **
458: ** LOGNAME environment variable
459: ** USER environment variable
460: **
461: ** Returns NULL if error else pointer to static string
462: */
2.79 frystyk 463: PUBLIC const char * HTGetMailAddress (void)
2.19 frystyk 464: {
2.80 ! eric 465: #ifdef HT_REENTRANT
! 466: char name[LOGNAME_MAX+1]; /* For getlogin_r or getUserName */
! 467: #endif
! 468: #ifdef WWW_MSWINDOWS/* what was the plan for this under windows? - EGP */
! 469: char name[256]; /* For getlogin_r or getUserName */
! 470: unsigned int bufSize = sizeof(name);
2.79 frystyk 471: #endif
472: #ifdef HAVE_PWD_H
473: struct passwd * pw_info = NULL;
2.47 frystyk 474: #endif
2.79 frystyk 475: char * login = NULL;
476: const char * domain;
2.21 frystyk 477: if (mailaddress) {
478: if (*mailaddress)
479: return mailaddress;
480: else
481: return NULL; /* No luck the last time so we wont try again */
482: }
2.23 duns 483:
2.79 frystyk 484: #ifdef WWW_MSWINDOWS
2.80 ! eric 485: if (!login && GetUserName(name, &bufSize) != TRUE)
2.79 frystyk 486: if (PROT_TRACE) HTTrace("MailAddress. GetUsername returns NO\n");
487: #endif /* WWW_MSWINDOWS */
488:
489: #ifdef HAVE_CUSERID
490: if (!login && (login = (char *) cuserid(NULL)) == NULL)
2.77 eric 491: if (PROT_TRACE) HTTrace("MailAddress. cuserid returns NULL\n");
2.79 frystyk 492: #endif /* HAVE_CUSERID */
493:
494: #ifdef HAVE_GETLOGIN
2.47 frystyk 495: #ifdef HT_REENTRANT
2.79 frystyk 496: if (!login && (login = (char *) getlogin_r(name, LOGNAME_MAX)) == NULL)
2.47 frystyk 497: #else
2.79 frystyk 498: if (!login && (login = (char *) getlogin()) == NULL)
499: #endif /* HT_REENTRANT */
500: if (PROT_TRACE) HTTrace("MailAddress. getlogin returns NULL\n");
501: #endif /* HAVE_GETLOGIN */
502:
503: #ifdef HAVE_PWD_H
504: if (!login && (pw_info = getpwuid(getuid())) != NULL)
505: login = pw_info->pw_name;
506: #endif /* HAVE_PWD_H */
507:
508: if (!login && (login = getenv("LOGNAME")) == NULL)
509: if (PROT_TRACE) HTTrace("MailAddress. LOGNAME not found\n");
510:
511: if (!login && (login = getenv("USER")) == NULL)
512: if (PROT_TRACE) HTTrace("MailAddress. USER not found\n");
513:
514: if (!login) login = HT_DEFAULT_LOGIN;
2.34 roeber 515:
2.19 frystyk 516: if (login) {
517: StrAllocCopy(mailaddress, login);
518: StrAllocCat(mailaddress, "@");
519: if ((domain = HTGetHostName()) != NULL)
520: StrAllocCat(mailaddress, domain);
2.21 frystyk 521: else {
522: *mailaddress = '\0';
523: return NULL; /* Domain name not available */
524: }
2.19 frystyk 525: return mailaddress;
526: }
527: return NULL;
528: }
2.32 frystyk 529:
530:
531: /*
532: ** Free the mail address. Called from HTLibTerminate
533: */
2.56 frystyk 534: PUBLIC void HTFreeMailAddress (void)
2.32 frystyk 535: {
2.76 frystyk 536: HT_FREE(mailaddress);
2.32 frystyk 537: }
538:
2.19 frystyk 539:
540: /* ------------------------------------------------------------------------- */
541: /* CONNECTION ESTABLISHMENT MANAGEMENT */
542: /* ------------------------------------------------------------------------- */
2.13 frystyk 543:
544: /* HTDoConnect()
545: **
546: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
2.54 frystyk 547: ** the default port value.
2.13 frystyk 548: **
2.40 frystyk 549: ** returns HT_ERROR Error has occured or interrupted
550: ** HT_OK if connected
551: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 552: */
2.54 frystyk 553: PUBLIC int HTDoConnect (HTNet * net, char * url, u_short default_port)
2.13 frystyk 554: {
555: int status;
2.65 frystyk 556: HTRequest * request = net->request;
2.55 frystyk 557: char *fullhost = HTParse(url, "", PARSE_HOST);
2.13 frystyk 558: char *at_sign;
559: char *host;
560:
2.54 frystyk 561: /* if there's an @ then use the stuff after it as a hostname */
2.55 frystyk 562: if ((at_sign = strchr(fullhost, '@')) != NULL)
2.13 frystyk 563: host = at_sign+1;
564: else
2.55 frystyk 565: host = fullhost;
2.24 frystyk 566: if (!*host) {
2.65 frystyk 567: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
2.24 frystyk 568: NULL, 0, "HTDoConnect");
2.76 frystyk 569: HT_FREE(fullhost);
2.40 frystyk 570: return HT_ERROR;
2.27 frystyk 571: }
2.13 frystyk 572:
2.55 frystyk 573: /* Jump into the state machine */
574: while (1) {
575: switch (net->tcpstate) {
576: case TCP_BEGIN:
577: {
578: char *port = strchr(host, ':');
579: SockA *sin = &net->sock_addr;
580: memset((void *) sin, '\0', sizeof(SockA));
581: if (port++ && isdigit(*port)) {
2.54 frystyk 582: #ifdef DECNET
2.55 frystyk 583: sin->sdn_family = AF_DECnet;
584: sin->sdn_objnum=(unsigned char)(strtol(port,(char**)0,10));
585: #else
586: sin->sin_family = AF_INET;
587: sin->sin_port = htons(atol(port));
2.54 frystyk 588: #endif
2.55 frystyk 589: } else {
2.13 frystyk 590: #ifdef DECNET
2.55 frystyk 591: sin->sdn_family = AF_DECnet;
592: net->sock_addr.sdn_objnum = DNP_OBJ;
2.13 frystyk 593: #else /* Internet */
2.55 frystyk 594: sin->sin_family = AF_INET;
595: sin->sin_port = htons(default_port);
2.13 frystyk 596: #endif
2.55 frystyk 597: }
598: }
2.44 frystyk 599: if (PROT_TRACE)
2.77 eric 600: HTTrace("HTDoConnect. Looking up `%s\'\n", host);
2.55 frystyk 601: net->tcpstate = TCP_DNS;
602: break;
603:
604: case TCP_DNS:
605: if ((status = HTParseInet(net, host)) < 0) {
2.27 frystyk 606: if (PROT_TRACE)
2.77 eric 607: HTTrace("HTDoConnect. Can't locate `%s\'\n", host);
2.65 frystyk 608: HTRequest_addError(request, ERR_FATAL, NO,HTERR_NO_REMOTE_HOST,
2.27 frystyk 609: (void *) host, strlen(host), "HTDoConnect");
2.55 frystyk 610: net->tcpstate = TCP_ERROR;
2.27 frystyk 611: break;
2.55 frystyk 612: }
613:
614: /*
615: ** Wait for a persistent connection. When we return, we check
616: ** that the socket hasn't been closed in the meantime
617: */
618: if (!status) {
2.56 frystyk 619: net->tcpstate = TCP_NEED_CONNECT;
2.76 frystyk 620: HT_FREE(fullhost);
2.55 frystyk 621: HTNet_wait(net);
2.54 frystyk 622: return HT_PERSISTENT;
2.55 frystyk 623: }
2.54 frystyk 624:
2.55 frystyk 625: if (!net->retry && status > 1) /* If multiple homes */
626: net->retry = status;
627: if (net->sockfd != INVSOC) { /* Reusing socket */
2.54 frystyk 628: if (PROT_TRACE)
2.77 eric 629: HTTrace("HTDoConnect. REUSING SOCKET %d\n",
2.55 frystyk 630: net->sockfd);
631: net->tcpstate = TCP_CONNECTED;
632: } else
633: net->tcpstate = TCP_NEED_SOCKET;
634: break;
635:
636: case TCP_NEED_SOCKET:
2.27 frystyk 637: #ifdef DECNET
2.36 frystyk 638: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
2.27 frystyk 639: #else
2.55 frystyk 640: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
2.27 frystyk 641: #endif
642: {
2.65 frystyk 643: HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, "socket");
2.55 frystyk 644: net->tcpstate = TCP_ERROR;
2.27 frystyk 645: break;
646: }
647: if (PROT_TRACE)
2.77 eric 648: HTTrace("HTDoConnect. Created socket %d\n",net->sockfd);
2.27 frystyk 649:
2.28 frystyk 650: /* If non-blocking protocol then change socket status
2.50 frystyk 651: ** I use FCNTL so that I can ask the status before I set it.
652: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
653: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
654: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
655: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
656: */
2.74 frystyk 657: if (!net->preemptive) {
2.73 frystyk 658: #ifdef _WINSOCKAPI_
2.41 frystyk 659: { /* begin windows scope */
2.65 frystyk 660: HTRequest * rq = request;
2.41 frystyk 661: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
662: FD_CONNECT | FD_CLOSE ;
663: int rv = 0 ;
664:
2.64 frystyk 665: #ifdef WWW_WIN_ASYNC
2.41 frystyk 666: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.64 frystyk 667: rv = WSAAsyncSelect( net->sockfd, rq->hwnd,
668: rq->winMsg, levents);
669: if (rv == SOCKET_ERROR) {
670: status = -1 ;
671: if (PROT_TRACE)
2.77 eric 672: HTTrace("HTDoConnect. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 673: WSAGetLastError());
674: } /* error returns */
675: #else
676: int enable = 1;
677: status = IOCTL(net->sockfd, FIONBIO, &enable);
678: #endif
2.41 frystyk 679: } /* end scope */
680: #else
681: #if defined(VMS)
2.36 frystyk 682: {
683: int enable = 1;
684: status = IOCTL(net->sockfd, FIONBIO, &enable);
685: }
686: #else
2.27 frystyk 687: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.41 frystyk 688: status |= O_NONBLOCK; /* POSIX */
2.27 frystyk 689: status = FCNTL(net->sockfd, F_SETFL, status);
690: }
2.41 frystyk 691: #endif /* VMS */
692: #endif /* WINDOW */
2.43 frystyk 693: if (PROT_TRACE) {
694: if (status == -1)
2.77 eric 695: HTTrace("HTDoConnect. Only blocking works\n");
2.43 frystyk 696: else
2.77 eric 697: HTTrace("HTDoConnect. Non-blocking socket\n");
2.43 frystyk 698: }
2.57 frystyk 699: } else if (PROT_TRACE)
2.77 eric 700: HTTrace("HTDoConnect. Blocking socket\n");
2.56 frystyk 701:
2.27 frystyk 702: /* If multi-homed host then start timer on connection */
2.55 frystyk 703: if (net->retry) net->connecttime = time(NULL);
2.65 frystyk 704:
705: /* Progress */
706: {
707: HTAlertCallback *cbf = HTAlert_find(HT_PROG_CONNECT);
708: if (cbf)
2.78 frystyk 709: (*cbf)(request,HT_PROG_CONNECT,HT_MSG_NULL,NULL,host,NULL);
2.65 frystyk 710: }
2.56 frystyk 711: net->tcpstate = TCP_NEED_CONNECT;
2.55 frystyk 712: break;
2.54 frystyk 713:
2.56 frystyk 714: case TCP_NEED_CONNECT:
2.55 frystyk 715: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
716: sizeof(net->sock_addr));
717: /*
718: * According to the Sun man page for connect:
719: * EINPROGRESS The socket is non-blocking and the con-
720: * nection cannot be completed immediately.
721: * It is possible to select(2) for comple-
722: * tion by selecting the socket for writ-
723: * ing.
724: * According to the Motorola SVR4 man page for connect:
725: * EAGAIN The socket is non-blocking and the con-
726: * nection cannot be completed immediately.
727: * It is possible to select for completion
728: * by selecting the socket for writing.
729: * However, this is only possible if the
730: * socket STREAMS module is the topmost
731: * module on the protocol stack with a
732: * write service procedure. This will be
733: * the normal case.
734: */
735: #ifdef _WINSOCKAPI_
736: if (status == SOCKET_ERROR)
737: #else
738: if (status < 0)
739: #endif
740: {
2.27 frystyk 741: #ifdef EAGAIN
2.55 frystyk 742: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
2.41 frystyk 743: #else
2.55 frystyk 744: #ifdef _WINSOCKAPI_
745: if (socerrno==WSAEWOULDBLOCK)
2.40 frystyk 746: #else
2.55 frystyk 747: if (socerrno==EINPROGRESS)
748: #endif /* _WINSOCKAPI_ */
2.27 frystyk 749: #endif /* EAGAIN */
2.55 frystyk 750: {
751: if (PROT_TRACE)
2.77 eric 752: HTTrace("HTDoConnect. WOULD BLOCK `%s'\n",host);
2.65 frystyk 753: HTEvent_Register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55 frystyk 754: net->cbf, net->priority);
2.76 frystyk 755: HT_FREE(fullhost);
2.55 frystyk 756: return HT_WOULD_BLOCK;
757: }
758: if (socerrno == EISCONN) {
759: net->tcpstate = TCP_CONNECTED;
760: break;
761: }
2.58 frystyk 762: #ifdef _WINSOCKAPI_
2.59 frystyk 763: if (socerrno == WSAEBADF) /* We lost the socket */
2.58 frystyk 764: #else
765: if (socerrno == EBADF) /* We lost the socket */
766: #endif
767: {
2.55 frystyk 768: net->tcpstate = TCP_NEED_SOCKET;
769: break;
2.27 frystyk 770: }
2.55 frystyk 771: if (net->retry) {
772: net->connecttime -= time(NULL);
2.54 frystyk 773: /* Added EINVAL `invalid argument' as this is what I
774: get back from a non-blocking connect where I should
775: get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.58 frystyk 776: #if defined(__srv4__) || defined (_WINSOCKAPI_)
2.55 frystyk 777: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
778: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
779: socerrno==EHOSTDOWN)
780: #else
2.54 frystyk 781: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
782: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
783: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 784: #endif
2.54 frystyk 785: net->connecttime += TCP_DELAY;
786: else
787: net->connecttime += TCP_PENALTY;
2.55 frystyk 788: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
789: }
790: net->tcpstate = TCP_ERROR;
791: } else
792: net->tcpstate = TCP_CONNECTED;
793: break;
794:
795: case TCP_CONNECTED:
796: HTEvent_UnRegister(net->sockfd, (SockOps) FD_CONNECT);
797: if (net->retry) {
798: net->connecttime -= time(NULL);
2.54 frystyk 799: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27 frystyk 800: }
2.55 frystyk 801: net->retry = 0;
2.76 frystyk 802: HT_FREE(fullhost);
2.55 frystyk 803: net->tcpstate = TCP_BEGIN;
804: return HT_OK;
805: break;
806:
2.56 frystyk 807: case TCP_NEED_BIND:
808: case TCP_NEED_LISTEN:
2.55 frystyk 809: case TCP_ERROR:
2.77 eric 810: if (PROT_TRACE) HTTrace("HTDoConnect. Connect failed\n");
2.55 frystyk 811: if (net->sockfd != INVSOC) {
2.53 frystyk 812: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
2.55 frystyk 813: NETCLOSE(net->sockfd);
814: net->sockfd = INVSOC;
815: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
816: HTDNS_setSocket(net->dns, INVSOC);
817: net->tcpstate = TCP_NEED_SOCKET;
818: break;
819: }
820: }
821:
822: /* Do we have more homes to try? */
823: if (--net->retry > 0) {
2.65 frystyk 824: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 825: "connect");
826: net->tcpstate = TCP_DNS;
2.24 frystyk 827: break;
828: }
2.65 frystyk 829: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.55 frystyk 830: HTDNS_delete(host);
2.54 frystyk 831: net->retry = 0;
2.76 frystyk 832: HT_FREE(fullhost);
2.55 frystyk 833: net->tcpstate = TCP_BEGIN;
834: return HT_ERROR;
835: break;
2.24 frystyk 836: }
2.55 frystyk 837: }
2.13 frystyk 838: }
839:
2.56 frystyk 840: /* HTDoAccept()
841: ** ------------
842: ** This function makes a non-blocking accept which will turn up as ready
843: ** read in the select.
844: ** Returns
845: ** HT_ERROR Error has occured or interrupted
846: ** HT_OK if connected
847: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 848: */
2.71 frystyk 849: PUBLIC int HTDoAccept (HTNet * net)
2.13 frystyk 850: {
851: int status;
2.56 frystyk 852: int size = sizeof(net->sock_addr);
2.65 frystyk 853: HTRequest *request = net->request;
2.36 frystyk 854: if (net->sockfd==INVSOC) {
2.77 eric 855: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 856: return HT_ERROR;
2.13 frystyk 857: }
2.65 frystyk 858:
859: /* Progress report */
860: {
861: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
862: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
863: }
2.56 frystyk 864: status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
865: #ifdef _WINSOCKAPI_
866: if (status == SOCKET_ERROR)
867: #else
868: if (status < 0)
869: #endif
2.23 duns 870: {
2.56 frystyk 871: #ifdef EAGAIN
872: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
873: #else
874: #ifdef _WINSOCKAPI_
875: if (socerrno==WSAEWOULDBLOCK)
876: #else
877: if (socerrno==EINPROGRESS)
878: #endif /* _WINSOCKAPI_ */
879: #endif /* EAGAIN */
880: {
881: if (PROT_TRACE)
2.77 eric 882: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.65 frystyk 883: HTEvent_Register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56 frystyk 884: net->cbf, net->priority);
885: return HT_WOULD_BLOCK;
886: }
2.65 frystyk 887: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 888: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 889: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
890: HTDNS_setSocket(net->dns, INVSOC);
891: }
892: return HT_ERROR;
2.23 duns 893: }
2.71 frystyk 894:
895: /* Swap to new socket */
896: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ACCEPT);
897: net->sockfd = status;
2.77 eric 898: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
2.56 frystyk 899: return HT_OK;
900: }
901:
902:
903: /* HTDoListen
904: ** ----------
905: ** Listens on the specified port. 0 means that we chose it here
906: ** If master==INVSOC then we listen on all local interfaces (using a
907: ** wildcard). If !INVSOC then use this as the local interface
908: ** returns HT_ERROR Error has occured or interrupted
909: ** HT_OK if connected
910: */
2.67 frystyk 911: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 912: {
913: int status;
914:
915: /* Jump into the state machine */
916: while (1) {
917: switch (net->tcpstate) {
918: case TCP_BEGIN:
919: {
920: SockA *sin = &net->sock_addr;
921: memset((void *) sin, '\0', sizeof(SockA));
922: #ifdef DECNET
923: sin->sdn_family = AF_DECnet;
924: sin->sdn_objnum = port;
2.36 frystyk 925: #else
2.56 frystyk 926: sin->sin_family = AF_INET;
927: if (master != INVSOC) {
928: int len = sizeof(SockA);
929: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 930: HTRequest_addSystemError(net->request, ERR_FATAL,
931: socerrno, NO, "getsockname");
2.56 frystyk 932: net->tcpstate = TCP_ERROR;
933: break;
934: }
935: } else
936: sin->sin_addr.s_addr = INADDR_ANY;
937: sin->sin_port = htons(port);
938: #endif
939: }
940: if (PROT_TRACE)
2.77 eric 941: HTTrace("HTDoListen.. Listen on port %d\n", port);
2.56 frystyk 942: net->tcpstate = TCP_NEED_SOCKET;
943: break;
944:
945: case TCP_NEED_SOCKET:
946: #ifdef DECNET
947: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
948: #else
949: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
950: #endif
951: {
2.65 frystyk 952: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
953: NO, "socket");
2.56 frystyk 954: net->tcpstate = TCP_ERROR;
955: break;
956: }
957: if (PROT_TRACE)
2.77 eric 958: HTTrace("HTDoListen.. Created socket %d\n",net->sockfd);
2.56 frystyk 959:
960: /* If non-blocking protocol then change socket status
961: ** I use FCNTL so that I can ask the status before I set it.
962: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
963: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
964: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
965: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
966: */
2.74 frystyk 967: if (!net->preemptive) {
2.73 frystyk 968: #ifdef _WINSOCKAPI_
2.56 frystyk 969: { /* begin windows scope */
970: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
971: FD_CONNECT | FD_CLOSE ;
972: int rv = 0 ;
973:
2.64 frystyk 974: #ifdef WWW_WIN_ASYNC
2.56 frystyk 975: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 976: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 977: net->request->winMsg, levents);
2.64 frystyk 978: if (rv == SOCKET_ERROR) {
979: status = -1 ;
980: if (PROT_TRACE)
2.77 eric 981: HTTrace("HTDoListen.. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 982: WSAGetLastError());
2.56 frystyk 983: } /* error returns */
2.64 frystyk 984: #else
985: int enable = 1 ;
986: status = IOCTL(net->sockfd, FIONBIO, &enable);
987: #endif
2.56 frystyk 988: } /* end scope */
989: #else
990: #if defined(VMS)
991: {
992: int enable = 1;
993: status = IOCTL(net->sockfd, FIONBIO, &enable);
994: }
995: #else
996: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
997: status |= O_NONBLOCK; /* POSIX */
998: status = FCNTL(net->sockfd, F_SETFL, status);
999: }
1000: #endif /* VMS */
1001: #endif /* WINDOW */
1002: if (PROT_TRACE) {
1003: if (status == -1)
2.77 eric 1004: HTTrace("HTDoListen.. Blocking socket\n");
2.56 frystyk 1005: else
2.77 eric 1006: HTTrace("HTDoListen.. Non-blocking socket\n");
2.56 frystyk 1007: }
1008: }
1009: net->tcpstate = TCP_NEED_BIND;
1010: break;
1011:
1012: case TCP_NEED_BIND:
1013: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
1014: sizeof(net->sock_addr));
1015: #ifdef _WINSOCKAPI_
1016: if (status == SOCKET_ERROR)
1017: #else
1018: if (status < 0)
1019: #endif
1020: {
1021: if (PROT_TRACE)
2.77 eric 1022: HTTrace("Bind........ failed %d\n", socerrno);
2.56 frystyk 1023: net->tcpstate = TCP_ERROR;
1024: } else
1025: net->tcpstate = TCP_NEED_LISTEN;
1026: break;
1027:
1028: case TCP_NEED_LISTEN:
2.67 frystyk 1029: status = listen(net->sockfd, backlog);
2.56 frystyk 1030: #ifdef _WINSOCKAPI_
1031: if (status == SOCKET_ERROR)
1032: #else
1033: if (status < 0)
2.36 frystyk 1034: #endif
2.56 frystyk 1035: net->tcpstate = TCP_ERROR;
1036: else
1037: net->tcpstate = TCP_CONNECTED;
1038: break;
1039:
1040: case TCP_CONNECTED:
1041: net->tcpstate = TCP_BEGIN;
1042: if (PROT_TRACE)
2.77 eric 1043: HTTrace("HTDoListen.. Bind and listen on port %d %s\n",
2.56 frystyk 1044: (int) ntohs(net->sock_addr.sin_port),
1045: HTInetString(&net->sock_addr));
1046: return HT_OK;
1047: break;
2.13 frystyk 1048:
2.56 frystyk 1049: case TCP_NEED_CONNECT:
1050: case TCP_DNS:
1051: case TCP_ERROR:
2.77 eric 1052: if (PROT_TRACE) HTTrace("HTDoListen.. Listen failed\n");
2.62 frystyk 1053: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 1054: net->tcpstate = TCP_BEGIN;
1055: return HT_ERROR;
1056: break;
1057: }
2.38 frystyk 1058: }
1.1 timbl 1059: }
1060:
Webmaster