Annotation of libwww/Library/src/HTTCP.c, revision 2.79
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.79 ! frystyk 465: #if defined(HT_REENTRANT) || defined(WWW_MSWINDOWS)
! 466: char name[LOGNAME_MAX+1]; /* For getlogin_r or getUserName */
! 467: #endif
! 468: #ifdef HAVE_PWD_H
! 469: struct passwd * pw_info = NULL;
2.47 frystyk 470: #endif
2.79 ! frystyk 471: char * login = NULL;
! 472: const char * domain;
2.21 frystyk 473: if (mailaddress) {
474: if (*mailaddress)
475: return mailaddress;
476: else
477: return NULL; /* No luck the last time so we wont try again */
478: }
2.23 duns 479:
2.79 ! frystyk 480: #ifdef WWW_MSWINDOWS
! 481: if (!login && GetUserName(name, LOGNAME_MAX) != TRUE)
! 482: if (PROT_TRACE) HTTrace("MailAddress. GetUsername returns NO\n");
! 483: #endif /* WWW_MSWINDOWS */
! 484:
! 485: #ifdef HAVE_CUSERID
! 486: if (!login && (login = (char *) cuserid(NULL)) == NULL)
2.77 eric 487: if (PROT_TRACE) HTTrace("MailAddress. cuserid returns NULL\n");
2.79 ! frystyk 488: #endif /* HAVE_CUSERID */
! 489:
! 490: #ifdef HAVE_GETLOGIN
2.47 frystyk 491: #ifdef HT_REENTRANT
2.79 ! frystyk 492: if (!login && (login = (char *) getlogin_r(name, LOGNAME_MAX)) == NULL)
2.47 frystyk 493: #else
2.79 ! frystyk 494: if (!login && (login = (char *) getlogin()) == NULL)
! 495: #endif /* HT_REENTRANT */
! 496: if (PROT_TRACE) HTTrace("MailAddress. getlogin returns NULL\n");
! 497: #endif /* HAVE_GETLOGIN */
! 498:
! 499: #ifdef HAVE_PWD_H
! 500: if (!login && (pw_info = getpwuid(getuid())) != NULL)
! 501: login = pw_info->pw_name;
! 502: #endif /* HAVE_PWD_H */
! 503:
! 504: if (!login && (login = getenv("LOGNAME")) == NULL)
! 505: if (PROT_TRACE) HTTrace("MailAddress. LOGNAME not found\n");
! 506:
! 507: if (!login && (login = getenv("USER")) == NULL)
! 508: if (PROT_TRACE) HTTrace("MailAddress. USER not found\n");
! 509:
! 510: if (!login) login = HT_DEFAULT_LOGIN;
2.34 roeber 511:
2.19 frystyk 512: if (login) {
513: StrAllocCopy(mailaddress, login);
514: StrAllocCat(mailaddress, "@");
515: if ((domain = HTGetHostName()) != NULL)
516: StrAllocCat(mailaddress, domain);
2.21 frystyk 517: else {
518: *mailaddress = '\0';
519: return NULL; /* Domain name not available */
520: }
2.19 frystyk 521: return mailaddress;
522: }
523: return NULL;
524: }
2.32 frystyk 525:
526:
527: /*
528: ** Free the mail address. Called from HTLibTerminate
529: */
2.56 frystyk 530: PUBLIC void HTFreeMailAddress (void)
2.32 frystyk 531: {
2.76 frystyk 532: HT_FREE(mailaddress);
2.32 frystyk 533: }
534:
2.19 frystyk 535:
536: /* ------------------------------------------------------------------------- */
537: /* CONNECTION ESTABLISHMENT MANAGEMENT */
538: /* ------------------------------------------------------------------------- */
2.13 frystyk 539:
540: /* HTDoConnect()
541: **
542: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
2.54 frystyk 543: ** the default port value.
2.13 frystyk 544: **
2.40 frystyk 545: ** returns HT_ERROR Error has occured or interrupted
546: ** HT_OK if connected
547: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 548: */
2.54 frystyk 549: PUBLIC int HTDoConnect (HTNet * net, char * url, u_short default_port)
2.13 frystyk 550: {
551: int status;
2.65 frystyk 552: HTRequest * request = net->request;
2.55 frystyk 553: char *fullhost = HTParse(url, "", PARSE_HOST);
2.13 frystyk 554: char *at_sign;
555: char *host;
556:
2.54 frystyk 557: /* if there's an @ then use the stuff after it as a hostname */
2.55 frystyk 558: if ((at_sign = strchr(fullhost, '@')) != NULL)
2.13 frystyk 559: host = at_sign+1;
560: else
2.55 frystyk 561: host = fullhost;
2.24 frystyk 562: if (!*host) {
2.65 frystyk 563: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
2.24 frystyk 564: NULL, 0, "HTDoConnect");
2.76 frystyk 565: HT_FREE(fullhost);
2.40 frystyk 566: return HT_ERROR;
2.27 frystyk 567: }
2.13 frystyk 568:
2.55 frystyk 569: /* Jump into the state machine */
570: while (1) {
571: switch (net->tcpstate) {
572: case TCP_BEGIN:
573: {
574: char *port = strchr(host, ':');
575: SockA *sin = &net->sock_addr;
576: memset((void *) sin, '\0', sizeof(SockA));
577: if (port++ && isdigit(*port)) {
2.54 frystyk 578: #ifdef DECNET
2.55 frystyk 579: sin->sdn_family = AF_DECnet;
580: sin->sdn_objnum=(unsigned char)(strtol(port,(char**)0,10));
581: #else
582: sin->sin_family = AF_INET;
583: sin->sin_port = htons(atol(port));
2.54 frystyk 584: #endif
2.55 frystyk 585: } else {
2.13 frystyk 586: #ifdef DECNET
2.55 frystyk 587: sin->sdn_family = AF_DECnet;
588: net->sock_addr.sdn_objnum = DNP_OBJ;
2.13 frystyk 589: #else /* Internet */
2.55 frystyk 590: sin->sin_family = AF_INET;
591: sin->sin_port = htons(default_port);
2.13 frystyk 592: #endif
2.55 frystyk 593: }
594: }
2.44 frystyk 595: if (PROT_TRACE)
2.77 eric 596: HTTrace("HTDoConnect. Looking up `%s\'\n", host);
2.55 frystyk 597: net->tcpstate = TCP_DNS;
598: break;
599:
600: case TCP_DNS:
601: if ((status = HTParseInet(net, host)) < 0) {
2.27 frystyk 602: if (PROT_TRACE)
2.77 eric 603: HTTrace("HTDoConnect. Can't locate `%s\'\n", host);
2.65 frystyk 604: HTRequest_addError(request, ERR_FATAL, NO,HTERR_NO_REMOTE_HOST,
2.27 frystyk 605: (void *) host, strlen(host), "HTDoConnect");
2.55 frystyk 606: net->tcpstate = TCP_ERROR;
2.27 frystyk 607: break;
2.55 frystyk 608: }
609:
610: /*
611: ** Wait for a persistent connection. When we return, we check
612: ** that the socket hasn't been closed in the meantime
613: */
614: if (!status) {
2.56 frystyk 615: net->tcpstate = TCP_NEED_CONNECT;
2.76 frystyk 616: HT_FREE(fullhost);
2.55 frystyk 617: HTNet_wait(net);
2.54 frystyk 618: return HT_PERSISTENT;
2.55 frystyk 619: }
2.54 frystyk 620:
2.55 frystyk 621: if (!net->retry && status > 1) /* If multiple homes */
622: net->retry = status;
623: if (net->sockfd != INVSOC) { /* Reusing socket */
2.54 frystyk 624: if (PROT_TRACE)
2.77 eric 625: HTTrace("HTDoConnect. REUSING SOCKET %d\n",
2.55 frystyk 626: net->sockfd);
627: net->tcpstate = TCP_CONNECTED;
628: } else
629: net->tcpstate = TCP_NEED_SOCKET;
630: break;
631:
632: case TCP_NEED_SOCKET:
2.27 frystyk 633: #ifdef DECNET
2.36 frystyk 634: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
2.27 frystyk 635: #else
2.55 frystyk 636: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
2.27 frystyk 637: #endif
638: {
2.65 frystyk 639: HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, "socket");
2.55 frystyk 640: net->tcpstate = TCP_ERROR;
2.27 frystyk 641: break;
642: }
643: if (PROT_TRACE)
2.77 eric 644: HTTrace("HTDoConnect. Created socket %d\n",net->sockfd);
2.27 frystyk 645:
2.28 frystyk 646: /* If non-blocking protocol then change socket status
2.50 frystyk 647: ** I use FCNTL so that I can ask the status before I set it.
648: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
649: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
650: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
651: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
652: */
2.74 frystyk 653: if (!net->preemptive) {
2.73 frystyk 654: #ifdef _WINSOCKAPI_
2.41 frystyk 655: { /* begin windows scope */
2.65 frystyk 656: HTRequest * rq = request;
2.41 frystyk 657: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
658: FD_CONNECT | FD_CLOSE ;
659: int rv = 0 ;
660:
2.64 frystyk 661: #ifdef WWW_WIN_ASYNC
2.41 frystyk 662: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.64 frystyk 663: rv = WSAAsyncSelect( net->sockfd, rq->hwnd,
664: rq->winMsg, levents);
665: if (rv == SOCKET_ERROR) {
666: status = -1 ;
667: if (PROT_TRACE)
2.77 eric 668: HTTrace("HTDoConnect. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 669: WSAGetLastError());
670: } /* error returns */
671: #else
672: int enable = 1;
673: status = IOCTL(net->sockfd, FIONBIO, &enable);
674: #endif
2.41 frystyk 675: } /* end scope */
676: #else
677: #if defined(VMS)
2.36 frystyk 678: {
679: int enable = 1;
680: status = IOCTL(net->sockfd, FIONBIO, &enable);
681: }
682: #else
2.27 frystyk 683: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.41 frystyk 684: status |= O_NONBLOCK; /* POSIX */
2.27 frystyk 685: status = FCNTL(net->sockfd, F_SETFL, status);
686: }
2.41 frystyk 687: #endif /* VMS */
688: #endif /* WINDOW */
2.43 frystyk 689: if (PROT_TRACE) {
690: if (status == -1)
2.77 eric 691: HTTrace("HTDoConnect. Only blocking works\n");
2.43 frystyk 692: else
2.77 eric 693: HTTrace("HTDoConnect. Non-blocking socket\n");
2.43 frystyk 694: }
2.57 frystyk 695: } else if (PROT_TRACE)
2.77 eric 696: HTTrace("HTDoConnect. Blocking socket\n");
2.56 frystyk 697:
2.27 frystyk 698: /* If multi-homed host then start timer on connection */
2.55 frystyk 699: if (net->retry) net->connecttime = time(NULL);
2.65 frystyk 700:
701: /* Progress */
702: {
703: HTAlertCallback *cbf = HTAlert_find(HT_PROG_CONNECT);
704: if (cbf)
2.78 frystyk 705: (*cbf)(request,HT_PROG_CONNECT,HT_MSG_NULL,NULL,host,NULL);
2.65 frystyk 706: }
2.56 frystyk 707: net->tcpstate = TCP_NEED_CONNECT;
2.55 frystyk 708: break;
2.54 frystyk 709:
2.56 frystyk 710: case TCP_NEED_CONNECT:
2.55 frystyk 711: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
712: sizeof(net->sock_addr));
713: /*
714: * According to the Sun man page for connect:
715: * EINPROGRESS The socket is non-blocking and the con-
716: * nection cannot be completed immediately.
717: * It is possible to select(2) for comple-
718: * tion by selecting the socket for writ-
719: * ing.
720: * According to the Motorola SVR4 man page for connect:
721: * EAGAIN The socket is non-blocking and the con-
722: * nection cannot be completed immediately.
723: * It is possible to select for completion
724: * by selecting the socket for writing.
725: * However, this is only possible if the
726: * socket STREAMS module is the topmost
727: * module on the protocol stack with a
728: * write service procedure. This will be
729: * the normal case.
730: */
731: #ifdef _WINSOCKAPI_
732: if (status == SOCKET_ERROR)
733: #else
734: if (status < 0)
735: #endif
736: {
2.27 frystyk 737: #ifdef EAGAIN
2.55 frystyk 738: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
2.41 frystyk 739: #else
2.55 frystyk 740: #ifdef _WINSOCKAPI_
741: if (socerrno==WSAEWOULDBLOCK)
2.40 frystyk 742: #else
2.55 frystyk 743: if (socerrno==EINPROGRESS)
744: #endif /* _WINSOCKAPI_ */
2.27 frystyk 745: #endif /* EAGAIN */
2.55 frystyk 746: {
747: if (PROT_TRACE)
2.77 eric 748: HTTrace("HTDoConnect. WOULD BLOCK `%s'\n",host);
2.65 frystyk 749: HTEvent_Register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55 frystyk 750: net->cbf, net->priority);
2.76 frystyk 751: HT_FREE(fullhost);
2.55 frystyk 752: return HT_WOULD_BLOCK;
753: }
754: if (socerrno == EISCONN) {
755: net->tcpstate = TCP_CONNECTED;
756: break;
757: }
2.58 frystyk 758: #ifdef _WINSOCKAPI_
2.59 frystyk 759: if (socerrno == WSAEBADF) /* We lost the socket */
2.58 frystyk 760: #else
761: if (socerrno == EBADF) /* We lost the socket */
762: #endif
763: {
2.55 frystyk 764: net->tcpstate = TCP_NEED_SOCKET;
765: break;
2.27 frystyk 766: }
2.55 frystyk 767: if (net->retry) {
768: net->connecttime -= time(NULL);
2.54 frystyk 769: /* Added EINVAL `invalid argument' as this is what I
770: get back from a non-blocking connect where I should
771: get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.58 frystyk 772: #if defined(__srv4__) || defined (_WINSOCKAPI_)
2.55 frystyk 773: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
774: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
775: socerrno==EHOSTDOWN)
776: #else
2.54 frystyk 777: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
778: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
779: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 780: #endif
2.54 frystyk 781: net->connecttime += TCP_DELAY;
782: else
783: net->connecttime += TCP_PENALTY;
2.55 frystyk 784: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
785: }
786: net->tcpstate = TCP_ERROR;
787: } else
788: net->tcpstate = TCP_CONNECTED;
789: break;
790:
791: case TCP_CONNECTED:
792: HTEvent_UnRegister(net->sockfd, (SockOps) FD_CONNECT);
793: if (net->retry) {
794: net->connecttime -= time(NULL);
2.54 frystyk 795: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27 frystyk 796: }
2.55 frystyk 797: net->retry = 0;
2.76 frystyk 798: HT_FREE(fullhost);
2.55 frystyk 799: net->tcpstate = TCP_BEGIN;
800: return HT_OK;
801: break;
802:
2.56 frystyk 803: case TCP_NEED_BIND:
804: case TCP_NEED_LISTEN:
2.55 frystyk 805: case TCP_ERROR:
2.77 eric 806: if (PROT_TRACE) HTTrace("HTDoConnect. Connect failed\n");
2.55 frystyk 807: if (net->sockfd != INVSOC) {
2.53 frystyk 808: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
2.55 frystyk 809: NETCLOSE(net->sockfd);
810: net->sockfd = INVSOC;
811: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
812: HTDNS_setSocket(net->dns, INVSOC);
813: net->tcpstate = TCP_NEED_SOCKET;
814: break;
815: }
816: }
817:
818: /* Do we have more homes to try? */
819: if (--net->retry > 0) {
2.65 frystyk 820: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 821: "connect");
822: net->tcpstate = TCP_DNS;
2.24 frystyk 823: break;
824: }
2.65 frystyk 825: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.55 frystyk 826: HTDNS_delete(host);
2.54 frystyk 827: net->retry = 0;
2.76 frystyk 828: HT_FREE(fullhost);
2.55 frystyk 829: net->tcpstate = TCP_BEGIN;
830: return HT_ERROR;
831: break;
2.24 frystyk 832: }
2.55 frystyk 833: }
2.13 frystyk 834: }
835:
2.56 frystyk 836: /* HTDoAccept()
837: ** ------------
838: ** This function makes a non-blocking accept which will turn up as ready
839: ** read in the select.
840: ** Returns
841: ** HT_ERROR Error has occured or interrupted
842: ** HT_OK if connected
843: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 844: */
2.71 frystyk 845: PUBLIC int HTDoAccept (HTNet * net)
2.13 frystyk 846: {
847: int status;
2.56 frystyk 848: int size = sizeof(net->sock_addr);
2.65 frystyk 849: HTRequest *request = net->request;
2.36 frystyk 850: if (net->sockfd==INVSOC) {
2.77 eric 851: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 852: return HT_ERROR;
2.13 frystyk 853: }
2.65 frystyk 854:
855: /* Progress report */
856: {
857: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
858: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
859: }
2.56 frystyk 860: status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
861: #ifdef _WINSOCKAPI_
862: if (status == SOCKET_ERROR)
863: #else
864: if (status < 0)
865: #endif
2.23 duns 866: {
2.56 frystyk 867: #ifdef EAGAIN
868: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
869: #else
870: #ifdef _WINSOCKAPI_
871: if (socerrno==WSAEWOULDBLOCK)
872: #else
873: if (socerrno==EINPROGRESS)
874: #endif /* _WINSOCKAPI_ */
875: #endif /* EAGAIN */
876: {
877: if (PROT_TRACE)
2.77 eric 878: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.65 frystyk 879: HTEvent_Register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56 frystyk 880: net->cbf, net->priority);
881: return HT_WOULD_BLOCK;
882: }
2.65 frystyk 883: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 884: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 885: if (HTDNS_socket(net->dns) != INVSOC) { /* Inherited socket */
886: HTDNS_setSocket(net->dns, INVSOC);
887: }
888: return HT_ERROR;
2.23 duns 889: }
2.71 frystyk 890:
891: /* Swap to new socket */
892: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ACCEPT);
893: net->sockfd = status;
2.77 eric 894: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
2.56 frystyk 895: return HT_OK;
896: }
897:
898:
899: /* HTDoListen
900: ** ----------
901: ** Listens on the specified port. 0 means that we chose it here
902: ** If master==INVSOC then we listen on all local interfaces (using a
903: ** wildcard). If !INVSOC then use this as the local interface
904: ** returns HT_ERROR Error has occured or interrupted
905: ** HT_OK if connected
906: */
2.67 frystyk 907: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 908: {
909: int status;
910:
911: /* Jump into the state machine */
912: while (1) {
913: switch (net->tcpstate) {
914: case TCP_BEGIN:
915: {
916: SockA *sin = &net->sock_addr;
917: memset((void *) sin, '\0', sizeof(SockA));
918: #ifdef DECNET
919: sin->sdn_family = AF_DECnet;
920: sin->sdn_objnum = port;
2.36 frystyk 921: #else
2.56 frystyk 922: sin->sin_family = AF_INET;
923: if (master != INVSOC) {
924: int len = sizeof(SockA);
925: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 926: HTRequest_addSystemError(net->request, ERR_FATAL,
927: socerrno, NO, "getsockname");
2.56 frystyk 928: net->tcpstate = TCP_ERROR;
929: break;
930: }
931: } else
932: sin->sin_addr.s_addr = INADDR_ANY;
933: sin->sin_port = htons(port);
934: #endif
935: }
936: if (PROT_TRACE)
2.77 eric 937: HTTrace("HTDoListen.. Listen on port %d\n", port);
2.56 frystyk 938: net->tcpstate = TCP_NEED_SOCKET;
939: break;
940:
941: case TCP_NEED_SOCKET:
942: #ifdef DECNET
943: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
944: #else
945: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
946: #endif
947: {
2.65 frystyk 948: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
949: NO, "socket");
2.56 frystyk 950: net->tcpstate = TCP_ERROR;
951: break;
952: }
953: if (PROT_TRACE)
2.77 eric 954: HTTrace("HTDoListen.. Created socket %d\n",net->sockfd);
2.56 frystyk 955:
956: /* If non-blocking protocol then change socket status
957: ** I use FCNTL so that I can ask the status before I set it.
958: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
959: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
960: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
961: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
962: */
2.74 frystyk 963: if (!net->preemptive) {
2.73 frystyk 964: #ifdef _WINSOCKAPI_
2.56 frystyk 965: { /* begin windows scope */
966: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
967: FD_CONNECT | FD_CLOSE ;
968: int rv = 0 ;
969:
2.64 frystyk 970: #ifdef WWW_WIN_ASYNC
2.56 frystyk 971: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 972: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 973: net->request->winMsg, levents);
2.64 frystyk 974: if (rv == SOCKET_ERROR) {
975: status = -1 ;
976: if (PROT_TRACE)
2.77 eric 977: HTTrace("HTDoListen.. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 978: WSAGetLastError());
2.56 frystyk 979: } /* error returns */
2.64 frystyk 980: #else
981: int enable = 1 ;
982: status = IOCTL(net->sockfd, FIONBIO, &enable);
983: #endif
2.56 frystyk 984: } /* end scope */
985: #else
986: #if defined(VMS)
987: {
988: int enable = 1;
989: status = IOCTL(net->sockfd, FIONBIO, &enable);
990: }
991: #else
992: if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
993: status |= O_NONBLOCK; /* POSIX */
994: status = FCNTL(net->sockfd, F_SETFL, status);
995: }
996: #endif /* VMS */
997: #endif /* WINDOW */
998: if (PROT_TRACE) {
999: if (status == -1)
2.77 eric 1000: HTTrace("HTDoListen.. Blocking socket\n");
2.56 frystyk 1001: else
2.77 eric 1002: HTTrace("HTDoListen.. Non-blocking socket\n");
2.56 frystyk 1003: }
1004: }
1005: net->tcpstate = TCP_NEED_BIND;
1006: break;
1007:
1008: case TCP_NEED_BIND:
1009: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
1010: sizeof(net->sock_addr));
1011: #ifdef _WINSOCKAPI_
1012: if (status == SOCKET_ERROR)
1013: #else
1014: if (status < 0)
1015: #endif
1016: {
1017: if (PROT_TRACE)
2.77 eric 1018: HTTrace("Bind........ failed %d\n", socerrno);
2.56 frystyk 1019: net->tcpstate = TCP_ERROR;
1020: } else
1021: net->tcpstate = TCP_NEED_LISTEN;
1022: break;
1023:
1024: case TCP_NEED_LISTEN:
2.67 frystyk 1025: status = listen(net->sockfd, backlog);
2.56 frystyk 1026: #ifdef _WINSOCKAPI_
1027: if (status == SOCKET_ERROR)
1028: #else
1029: if (status < 0)
2.36 frystyk 1030: #endif
2.56 frystyk 1031: net->tcpstate = TCP_ERROR;
1032: else
1033: net->tcpstate = TCP_CONNECTED;
1034: break;
1035:
1036: case TCP_CONNECTED:
1037: net->tcpstate = TCP_BEGIN;
1038: if (PROT_TRACE)
2.77 eric 1039: HTTrace("HTDoListen.. Bind and listen on port %d %s\n",
2.56 frystyk 1040: (int) ntohs(net->sock_addr.sin_port),
1041: HTInetString(&net->sock_addr));
1042: return HT_OK;
1043: break;
2.13 frystyk 1044:
2.56 frystyk 1045: case TCP_NEED_CONNECT:
1046: case TCP_DNS:
1047: case TCP_ERROR:
2.77 eric 1048: if (PROT_TRACE) HTTrace("HTDoListen.. Listen failed\n");
2.62 frystyk 1049: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 1050: net->tcpstate = TCP_BEGIN;
1051: return HT_ERROR;
1052: break;
1053: }
2.38 frystyk 1054: }
1.1 timbl 1055: }
1056:
Webmaster