Annotation of libwww/Library/src/HTTCP.c, revision 2.81
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.81 ! frystyk 688: #ifdef O_NONBLOCK
! 689: status |= O_NONBLOCK; /* POSIX */
! 690: #else
! 691: #ifdef F_NDELAY
! 692: status |= F_NDELAY; /* BSD */
! 693: #endif /* F_NDELAY */
! 694: #endif /* O_NONBLOCK */
2.27 frystyk 695: status = FCNTL(net->sockfd, F_SETFL, status);
696: }
2.41 frystyk 697: #endif /* VMS */
698: #endif /* WINDOW */
2.43 frystyk 699: if (PROT_TRACE) {
700: if (status == -1)
2.77 eric 701: HTTrace("HTDoConnect. Only blocking works\n");
2.43 frystyk 702: else
2.77 eric 703: HTTrace("HTDoConnect. Non-blocking socket\n");
2.43 frystyk 704: }
2.57 frystyk 705: } else if (PROT_TRACE)
2.77 eric 706: HTTrace("HTDoConnect. Blocking socket\n");
2.56 frystyk 707:
2.27 frystyk 708: /* If multi-homed host then start timer on connection */
2.55 frystyk 709: if (net->retry) net->connecttime = time(NULL);
2.65 frystyk 710:
711: /* Progress */
712: {
713: HTAlertCallback *cbf = HTAlert_find(HT_PROG_CONNECT);
714: if (cbf)
2.78 frystyk 715: (*cbf)(request,HT_PROG_CONNECT,HT_MSG_NULL,NULL,host,NULL);
2.65 frystyk 716: }
2.56 frystyk 717: net->tcpstate = TCP_NEED_CONNECT;
2.55 frystyk 718: break;
2.54 frystyk 719:
2.56 frystyk 720: case TCP_NEED_CONNECT:
2.55 frystyk 721: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
722: sizeof(net->sock_addr));
723: /*
724: * According to the Sun man page for connect:
725: * EINPROGRESS The socket is non-blocking and the con-
726: * nection cannot be completed immediately.
727: * It is possible to select(2) for comple-
728: * tion by selecting the socket for writ-
729: * ing.
730: * According to the Motorola SVR4 man page for connect:
731: * EAGAIN The socket is non-blocking and the con-
732: * nection cannot be completed immediately.
733: * It is possible to select for completion
734: * by selecting the socket for writing.
735: * However, this is only possible if the
736: * socket STREAMS module is the topmost
737: * module on the protocol stack with a
738: * write service procedure. This will be
739: * the normal case.
740: */
741: #ifdef _WINSOCKAPI_
742: if (status == SOCKET_ERROR)
743: #else
744: if (status < 0)
745: #endif
746: {
2.27 frystyk 747: #ifdef EAGAIN
2.55 frystyk 748: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
2.41 frystyk 749: #else
2.55 frystyk 750: #ifdef _WINSOCKAPI_
751: if (socerrno==WSAEWOULDBLOCK)
2.40 frystyk 752: #else
2.55 frystyk 753: if (socerrno==EINPROGRESS)
754: #endif /* _WINSOCKAPI_ */
2.27 frystyk 755: #endif /* EAGAIN */
2.55 frystyk 756: {
757: if (PROT_TRACE)
2.77 eric 758: HTTrace("HTDoConnect. WOULD BLOCK `%s'\n",host);
2.65 frystyk 759: HTEvent_Register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55 frystyk 760: net->cbf, net->priority);
2.76 frystyk 761: HT_FREE(fullhost);
2.55 frystyk 762: return HT_WOULD_BLOCK;
763: }
764: if (socerrno == EISCONN) {
765: net->tcpstate = TCP_CONNECTED;
766: break;
767: }
2.58 frystyk 768: #ifdef _WINSOCKAPI_
2.59 frystyk 769: if (socerrno == WSAEBADF) /* We lost the socket */
2.58 frystyk 770: #else
771: if (socerrno == EBADF) /* We lost the socket */
772: #endif
773: {
2.55 frystyk 774: net->tcpstate = TCP_NEED_SOCKET;
775: break;
2.27 frystyk 776: }
2.55 frystyk 777: if (net->retry) {
778: net->connecttime -= time(NULL);
2.54 frystyk 779: /* Added EINVAL `invalid argument' as this is what I
780: get back from a non-blocking connect where I should
781: get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.58 frystyk 782: #if defined(__srv4__) || defined (_WINSOCKAPI_)
2.55 frystyk 783: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
784: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
785: socerrno==EHOSTDOWN)
786: #else
2.54 frystyk 787: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
788: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
789: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 790: #endif
2.54 frystyk 791: net->connecttime += TCP_DELAY;
792: else
793: net->connecttime += TCP_PENALTY;
2.55 frystyk 794: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
795: }
796: net->tcpstate = TCP_ERROR;
797: } else
798: net->tcpstate = TCP_CONNECTED;
799: break;
800:
801: case TCP_CONNECTED:
802: HTEvent_UnRegister(net->sockfd, (SockOps) FD_CONNECT);
803: if (net->retry) {
804: net->connecttime -= time(NULL);
2.54 frystyk 805: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27 frystyk 806: }
2.55 frystyk 807: net->retry = 0;
2.76 frystyk 808: HT_FREE(fullhost);
2.55 frystyk 809: net->tcpstate = TCP_BEGIN;
810: return HT_OK;
811: break;
812:
2.56 frystyk 813: case TCP_NEED_BIND:
814: case TCP_NEED_LISTEN:
2.55 frystyk 815: case TCP_ERROR:
2.77 eric 816: if (PROT_TRACE) HTTrace("HTDoConnect. Connect failed\n");
2.55 frystyk 817: if (net->sockfd != INVSOC) {
2.53 frystyk 818: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
2.55 frystyk 819: NETCLOSE(net->sockfd);
820: net->sockfd = INVSOC;
821: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
822: HTDNS_setSocket(net->dns, INVSOC);
823: net->tcpstate = TCP_NEED_SOCKET;
824: break;
825: }
826: }
827:
828: /* Do we have more homes to try? */
829: if (--net->retry > 0) {
2.65 frystyk 830: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 831: "connect");
832: net->tcpstate = TCP_DNS;
2.24 frystyk 833: break;
834: }
2.65 frystyk 835: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.55 frystyk 836: HTDNS_delete(host);
2.54 frystyk 837: net->retry = 0;
2.76 frystyk 838: HT_FREE(fullhost);
2.55 frystyk 839: net->tcpstate = TCP_BEGIN;
840: return HT_ERROR;
841: break;
2.24 frystyk 842: }
2.55 frystyk 843: }
2.13 frystyk 844: }
845:
2.56 frystyk 846: /* HTDoAccept()
847: ** ------------
848: ** This function makes a non-blocking accept which will turn up as ready
849: ** read in the select.
850: ** Returns
851: ** HT_ERROR Error has occured or interrupted
852: ** HT_OK if connected
853: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 854: */
2.71 frystyk 855: PUBLIC int HTDoAccept (HTNet * net)
2.13 frystyk 856: {
857: int status;
2.56 frystyk 858: int size = sizeof(net->sock_addr);
2.65 frystyk 859: HTRequest *request = net->request;
2.36 frystyk 860: if (net->sockfd==INVSOC) {
2.77 eric 861: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 862: return HT_ERROR;
2.13 frystyk 863: }
2.65 frystyk 864:
865: /* Progress report */
866: {
867: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
868: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
869: }
2.56 frystyk 870: status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
871: #ifdef _WINSOCKAPI_
872: if (status == SOCKET_ERROR)
873: #else
874: if (status < 0)
875: #endif
2.23 duns 876: {
2.56 frystyk 877: #ifdef EAGAIN
878: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
879: #else
880: #ifdef _WINSOCKAPI_
881: if (socerrno==WSAEWOULDBLOCK)
882: #else
883: if (socerrno==EINPROGRESS)
884: #endif /* _WINSOCKAPI_ */
885: #endif /* EAGAIN */
886: {
887: if (PROT_TRACE)
2.77 eric 888: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.65 frystyk 889: HTEvent_Register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56 frystyk 890: net->cbf, net->priority);
891: return HT_WOULD_BLOCK;
892: }
2.65 frystyk 893: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 894: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 895: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
896: HTDNS_setSocket(net->dns, INVSOC);
897: }
898: return HT_ERROR;
2.23 duns 899: }
2.71 frystyk 900:
901: /* Swap to new socket */
902: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ACCEPT);
903: net->sockfd = status;
2.77 eric 904: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
2.56 frystyk 905: return HT_OK;
906: }
907:
908:
909: /* HTDoListen
910: ** ----------
911: ** Listens on the specified port. 0 means that we chose it here
912: ** If master==INVSOC then we listen on all local interfaces (using a
913: ** wildcard). If !INVSOC then use this as the local interface
914: ** returns HT_ERROR Error has occured or interrupted
915: ** HT_OK if connected
916: */
2.67 frystyk 917: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 918: {
919: int status;
920:
921: /* Jump into the state machine */
922: while (1) {
923: switch (net->tcpstate) {
924: case TCP_BEGIN:
925: {
926: SockA *sin = &net->sock_addr;
927: memset((void *) sin, '\0', sizeof(SockA));
928: #ifdef DECNET
929: sin->sdn_family = AF_DECnet;
930: sin->sdn_objnum = port;
2.36 frystyk 931: #else
2.56 frystyk 932: sin->sin_family = AF_INET;
933: if (master != INVSOC) {
934: int len = sizeof(SockA);
935: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 936: HTRequest_addSystemError(net->request, ERR_FATAL,
937: socerrno, NO, "getsockname");
2.56 frystyk 938: net->tcpstate = TCP_ERROR;
939: break;
940: }
941: } else
942: sin->sin_addr.s_addr = INADDR_ANY;
943: sin->sin_port = htons(port);
944: #endif
945: }
946: if (PROT_TRACE)
2.77 eric 947: HTTrace("HTDoListen.. Listen on port %d\n", port);
2.56 frystyk 948: net->tcpstate = TCP_NEED_SOCKET;
949: break;
950:
951: case TCP_NEED_SOCKET:
952: #ifdef DECNET
953: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
954: #else
955: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
956: #endif
957: {
2.65 frystyk 958: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
959: NO, "socket");
2.56 frystyk 960: net->tcpstate = TCP_ERROR;
961: break;
962: }
963: if (PROT_TRACE)
2.77 eric 964: HTTrace("HTDoListen.. Created socket %d\n",net->sockfd);
2.56 frystyk 965:
966: /* If non-blocking protocol then change socket status
967: ** I use FCNTL so that I can ask the status before I set it.
968: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
969: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
970: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
971: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
972: */
2.74 frystyk 973: if (!net->preemptive) {
2.73 frystyk 974: #ifdef _WINSOCKAPI_
2.56 frystyk 975: { /* begin windows scope */
976: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
977: FD_CONNECT | FD_CLOSE ;
978: int rv = 0 ;
979:
2.64 frystyk 980: #ifdef WWW_WIN_ASYNC
2.56 frystyk 981: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 982: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 983: net->request->winMsg, levents);
2.64 frystyk 984: if (rv == SOCKET_ERROR) {
985: status = -1 ;
986: if (PROT_TRACE)
2.77 eric 987: HTTrace("HTDoListen.. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 988: WSAGetLastError());
2.56 frystyk 989: } /* error returns */
2.64 frystyk 990: #else
991: int enable = 1 ;
992: status = IOCTL(net->sockfd, FIONBIO, &enable);
993: #endif
2.56 frystyk 994: } /* end scope */
995: #else
996: #if defined(VMS)
997: {
998: int enable = 1;
999: status = IOCTL(net->sockfd, FIONBIO, &enable);
1000: }
1001: #else
1002: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.81 ! frystyk 1003: #ifdef O_NONBLOCK
2.56 frystyk 1004: status |= O_NONBLOCK; /* POSIX */
2.81 ! frystyk 1005: #else
! 1006: #ifdef F_NDELAY
! 1007: status |= F_NDELAY; /* BSD */
! 1008: #endif /* F_NDELAY */
! 1009: #endif /* O_NONBLOCK */
2.56 frystyk 1010: status = FCNTL(net->sockfd, F_SETFL, status);
1011: }
1012: #endif /* VMS */
1013: #endif /* WINDOW */
1014: if (PROT_TRACE) {
1015: if (status == -1)
2.77 eric 1016: HTTrace("HTDoListen.. Blocking socket\n");
2.56 frystyk 1017: else
2.77 eric 1018: HTTrace("HTDoListen.. Non-blocking socket\n");
2.56 frystyk 1019: }
1020: }
1021: net->tcpstate = TCP_NEED_BIND;
1022: break;
1023:
1024: case TCP_NEED_BIND:
1025: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
1026: sizeof(net->sock_addr));
1027: #ifdef _WINSOCKAPI_
1028: if (status == SOCKET_ERROR)
1029: #else
1030: if (status < 0)
1031: #endif
1032: {
1033: if (PROT_TRACE)
2.77 eric 1034: HTTrace("Bind........ failed %d\n", socerrno);
2.56 frystyk 1035: net->tcpstate = TCP_ERROR;
1036: } else
1037: net->tcpstate = TCP_NEED_LISTEN;
1038: break;
1039:
1040: case TCP_NEED_LISTEN:
2.67 frystyk 1041: status = listen(net->sockfd, backlog);
2.56 frystyk 1042: #ifdef _WINSOCKAPI_
1043: if (status == SOCKET_ERROR)
1044: #else
1045: if (status < 0)
2.36 frystyk 1046: #endif
2.56 frystyk 1047: net->tcpstate = TCP_ERROR;
1048: else
1049: net->tcpstate = TCP_CONNECTED;
1050: break;
1051:
1052: case TCP_CONNECTED:
1053: net->tcpstate = TCP_BEGIN;
1054: if (PROT_TRACE)
2.77 eric 1055: HTTrace("HTDoListen.. Bind and listen on port %d %s\n",
2.56 frystyk 1056: (int) ntohs(net->sock_addr.sin_port),
1057: HTInetString(&net->sock_addr));
1058: return HT_OK;
1059: break;
2.13 frystyk 1060:
2.56 frystyk 1061: case TCP_NEED_CONNECT:
1062: case TCP_DNS:
1063: case TCP_ERROR:
2.77 eric 1064: if (PROT_TRACE) HTTrace("HTDoListen.. Listen failed\n");
2.62 frystyk 1065: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 1066: net->tcpstate = TCP_BEGIN;
1067: return HT_ERROR;
1068: break;
1069: }
2.38 frystyk 1070: }
1.1 timbl 1071: }
1072:
Webmaster