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