Annotation of libwww/Library/src/HTTCP.c, revision 2.56

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

Webmaster