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

1.1       timbl       1: /*                     Generic Communication Code              HTTCP.c
                      2: **                     ==========================
                      3: **
                      4: **     This code is in common between client and server sides.
                      5: **
                      6: **     16 Jan 92  TBL  Fix strtol() undefined on CMU Mach.
                      7: **     25 Jun 92  JFG  Added DECNET option through TCP socket emulation.
2.7       duns        8: **     13 Sep 93  MD   Added correct return of vmserrorno for HTInetStatus.
                      9: **                     Added decoding of vms error message for MULTINET.
2.13      frystyk    10: **     31 May 94  HF   Added cache on host id's; now use inet_ntoa() to
                     11: **                     HTInetString and some other fixes. Added HTDoConnect
                     12: **                     and HTDoAccept
1.1       timbl      13: */
                     14: 
2.23      duns       15: #ifndef VMS
2.19      frystyk    16: #include <pwd.h>
2.23      duns       17: #endif /* not VMS */
                     18: 
2.13      frystyk    19: #include "tcp.h"               /* Defines SHORT_NAMES if necessary */
2.19      frystyk    20: 
2.13      frystyk    21: #include "HTUtils.h"
                     22: #include "HTAtom.h"
                     23: #include "HTList.h"
                     24: #include "HTParse.h"
                     25: #include "HTAccess.h"
                     26: #include "HTError.h"
2.24      frystyk    27: #include "HTTCP.h"                                      /* Implemented here */
1.1       timbl      28: 
2.23      duns       29: #ifdef VMS 
                     30: #include "HTVMSUtils.h"
                     31: #endif /* VMS */
                     32: 
1.1       timbl      33: #ifdef SHORT_NAMES
                     34: #define HTInetStatus           HTInStat
2.12      luotonen   35: #define HTErrnoString          HTErrnoS
1.1       timbl      36: #define HTInetString           HTInStri
                     37: #define HTParseInet            HTPaInet
                     38: #endif
                     39: 
2.8       luotonen   40: 
2.11      duns       41: /* VMS stuff */
                     42: #ifdef VMS
                     43: #ifndef MULTINET
                     44: #define FD_SETSIZE 32
                     45: #else /* Multinet */
                     46: #define FD_SETSIZE 256
                     47: #endif /* Multinet */
                     48: #endif /* VMS */
                     49: 
2.13      frystyk    50: /* Macros and other defines */
2.24      frystyk    51: /* x seconds penalty on a multi-homed host if IP-address is down */
                     52: #define TCP_PENALTY            1200
                     53: 
                     54: /* x seconds penalty on a multi-homed host if IP-address is timed out */
                     55: #define TCP_DELAY              600
                     56: 
                     57: /* Max number of non-blocking accepts */
2.13      frystyk    58: #define MAX_ACCEPT_POLL                30
                     59: #define FCNTL(r, s, t)         fcntl(r, s, t)
                     60: 
2.18      frystyk    61: #ifndef RESOLV_CONF
                     62: #define RESOLV_CONF "/etc/resolv.conf"
                     63: #endif
                     64: 
2.13      frystyk    65: /* Globals */
                     66: PUBLIC unsigned int    HTConCacheSize = 512;    /* Number of cached servers */
                     67: 
                     68: /* Type definitions and global variables etc. local to this module */
                     69: 
                     70: /* This structure is a cache of hosts to whom we have connected over time.
                     71:    The structure contains the necessary parts from hostent. For Internet host
                     72:    hostent->h_addr_list is not an array of char pointers but an array of 
                     73:    pointers of type in_addr. */
                     74: typedef struct _host_info {
                     75:     HTAtom *           hostname;                   /* Official name of host */
                     76:     int                        hits;           /* Total number of hits on this host */
                     77:     int                        addrlength;            /* Length of address in bytes */
2.24      frystyk    78:     int                        homes;         /* Number of IP addresses on the host */
2.13      frystyk    79:     int                        offset;         /* Offset value of active IP address */
                     80:     char **            addrlist;      /* List of addresses from name server */
                     81:     float *            weight;                    /* Weight on each address */
                     82: } host_info;
                     83: 
                     84: PRIVATE char *hostname = NULL;                     /* The name of this host */
2.19      frystyk    85: PRIVATE char *mailaddress = NULL;                   /* Current mail address */
2.13      frystyk    86: PRIVATE HTList *hostcache = NULL;  /* List of servers that we have talked to */
                     87: PRIVATE unsigned int HTCacheSize = 0;              /* Current size of cache */
2.11      duns       88: 
2.13      frystyk    89: /* ------------------------------------------------------------------------- */
1.1       timbl      90: 
                     91: /*     Encode INET status (as in sys/errno.h)                    inet_status()
                     92: **     ------------------
                     93: **
                     94: ** On entry,
                     95: **     where           gives a description of what caused the error
                     96: **     global errno    gives the error number in the unix way.
                     97: **
                     98: ** On return,
                     99: **     returns         a negative status in the unix way.
                    100: */
                    101: #ifndef PCNFS
2.7       duns      102: #ifdef VMS
2.10      duns      103: #ifndef __DECC
1.1       timbl     104: extern int uerrno;     /* Deposit of error info (as per errno.h) */
2.7       duns      105: extern volatile noshare int socket_errno; /* socket VMS error info 
                    106:                                              (used for translation of vmserrno) */
1.1       timbl     107: extern volatile noshare int vmserrno;  /* Deposit of VMS error info */
                    108: extern volatile noshare int errno;  /* noshare to avoid PSECT conflict */
2.10      duns      109: #endif /* not DECC */
2.11      duns      110: #endif /* VMS */
                    111: 
1.1       timbl     112: #ifndef errno
                    113: extern int errno;
                    114: #endif /* errno */
                    115: 
                    116: #ifndef VM
2.7       duns      117: #ifndef VMS
1.1       timbl     118: #ifndef NeXT
                    119: #ifndef THINK_C
                    120: extern char *sys_errlist[];            /* see man perror on cernvax */
                    121: extern int sys_nerr;
                    122: #endif  /* think c */
                    123: #endif /* NeXT */
2.7       duns      124: #endif  /* VMS */
1.1       timbl     125: #endif /* VM */
                    126: 
                    127: #endif /* PCNFS */
                    128: 
2.12      luotonen  129: 
                    130: /*
                    131:  *     Returns the string equivalent of the current errno.
                    132:  */
                    133: PUBLIC CONST char * HTErrnoString NOARGS
1.1       timbl     134: {
2.11      duns      135: #ifndef VMS
2.7       duns      136: 
1.1       timbl     137: #ifdef VM
2.12      luotonen  138:     return "(Error number not translated)";    /* What Is the VM equiv? */
1.1       timbl     139: #define ER_NO_TRANS_DONE
                    140: #endif
2.12      luotonen  141: 
                    142: #if defined(NeXT) || defined(THINK_C)
                    143:     return strerror(errno);
1.1       timbl     144: #define ER_NO_TRANS_DONE
                    145: #endif
2.12      luotonen  146: 
                    147: #ifndef ER_NO_TRANS_DONE
                    148:     return (errno < sys_nerr ? sys_errlist[errno] : "Unknown error");
1.1       timbl     149: #endif
                    150: 
2.12      luotonen  151: #else /* VMS */
                    152: 
                    153:     static char buf[60];
                    154:     sprintf(buf,"Unix errno = %ld dec, VMS error = %lx hex",errno,vaxc$errno);
                    155:     return buf;
                    156: 
1.1       timbl     157: #endif
2.12      luotonen  158: }
                    159: 
                    160: 
                    161: /*     Report Internet Error
                    162: **     ---------------------
                    163: */
                    164: PUBLIC int HTInetStatus ARGS1(char *, where)
                    165: {
                    166: #ifndef VMS
                    167: 
                    168:     CTRACE(tfp,
                    169:           "TCP errno... %d after call to %s() failed.\n............ %s\n",
                    170:           errno, where, HTErrnoString());
1.1       timbl     171: 
2.11      duns      172: #else /* VMS */
2.12      luotonen  173: 
2.11      duns      174:     CTRACE(tfp, "         Unix error number          = %ld dec\n", errno);
                    175:     CTRACE(tfp, "         VMS error                  = %lx hex\n", vaxc$errno);
2.12      luotonen  176: 
2.11      duns      177: #ifdef MULTINET
                    178:     CTRACE(tfp, "         Multinet error             = %lx hex\n", socket_errno); 
2.23      duns      179:     CTRACE(tfp, "         Error String               = %s\n", vms_errno_string());
2.11      duns      180: #endif /* MULTINET */
2.12      luotonen  181: 
2.11      duns      182: #endif /* VMS */
2.7       duns      183: 
                    184: #ifdef VMS
2.11      duns      185:     /* errno happen to be zero if vaxc$errno <> 0 */
                    186:     return -vaxc$errno;
2.7       duns      187: #else
1.1       timbl     188:     return -errno;
2.7       duns      189: #endif
1.1       timbl     190: }
                    191: 
                    192: 
                    193: /*     Parse a cardinal value                                 parse_cardinal()
                    194: **     ----------------------
                    195: **
                    196: ** On entry,
                    197: **     *pp         points to first character to be interpreted, terminated by
                    198: **                 non 0:9 character.
                    199: **     *pstatus    points to status already valid
                    200: **     maxvalue    gives the largest allowable value.
                    201: **
                    202: ** On exit,
                    203: **     *pp         points to first unread character
                    204: **     *pstatus    points to status updated iff bad
                    205: */
                    206: 
                    207: PUBLIC unsigned int HTCardinal ARGS3
                    208:        (int *,         pstatus,
                    209:        char **,        pp,
                    210:        unsigned int,   max_value)
                    211: {
                    212:     int   n;
                    213:     if ( (**pp<'0') || (**pp>'9')) {       /* Null string is error */
                    214:        *pstatus = -3;  /* No number where one expeceted */
                    215:        return 0;
                    216:     }
                    217: 
                    218:     n=0;
                    219:     while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
                    220: 
                    221:     if (n>max_value) {
                    222:        *pstatus = -4;  /* Cardinal outside range */
                    223:        return 0;
                    224:     }
                    225: 
                    226:     return n;
                    227: }
                    228: 
2.19      frystyk   229: /* ------------------------------------------------------------------------- */
                    230: /*                          HOST CACHE MANAGEMENT                           */
                    231: /* ------------------------------------------------------------------------- */
1.1       timbl     232: 
2.13      frystyk   233: /*                                                     HTTCPCacheRemoveElement
                    234: **
                    235: **     Remove the element specified from the cache
                    236: */
                    237: PRIVATE void HTTCPCacheRemoveElement ARGS1(host_info *, element)
                    238: {
                    239:     if (!hostcache) {
                    240:         if (TRACE)
                    241:             fprintf(stderr, "HostCache... Remove not done, no cache\n");
                    242:         return;
                    243:     }
                    244:     if (TRACE) fprintf(stderr, "HostCache... Remove `%s' from cache\n",
                    245:                       HTAtom_name(element->hostname));
                    246:     HTList_removeObject(hostcache, element);
2.22      frystyk   247:     if (*element->addrlist)
                    248:        free(*element->addrlist);
2.13      frystyk   249:     if (element->addrlist)
                    250:        free(element->addrlist);
                    251:     if (element->weight)
                    252:        free(element->weight);
                    253:     free(element);
                    254: }
                    255: 
                    256: 
                    257: /*                                                     HTTCPCacheRemoveHost
                    258: **
                    259: **     Removes the corresponding entrance in the cache
                    260: */
                    261: PRIVATE void HTTCPCacheRemoveHost ARGS1(char *, host)
                    262: {
                    263:     HTAtom *hostatom = HTAtom_for(host);
                    264:     HTList *cur = hostcache;
                    265:     host_info *pres = NULL;
                    266:     if (!hostcache) {
                    267:        fprintf(stderr, "HostCache... Remove host not done, no cache\n");
                    268:        return;
                    269:     }
                    270:     while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
                    271:        if (pres->hostname == hostatom) {
                    272:            break;
                    273:        }
                    274:     }
                    275:     if (pres)
                    276:        HTTCPCacheRemoveElement(pres);
                    277: }
                    278: 
                    279: 
                    280: /*                                                     HTTCPCacheGarbage
                    281: **
                    282: **     Remove the element with the lowest hit rate
                    283: */
                    284: PRIVATE void HTTCPCacheGarbage NOARGS
                    285: {
                    286:     HTList *cur = hostcache;
                    287:     host_info *pres, *worst_match = NULL;
                    288:     unsigned int worst_hits = 30000;             /* Should use UINT_MAX :-( */
                    289:     if (!hostcache) {
                    290:        if (TRACE)
                    291:            fprintf(stderr, "HostCache... Garbage collection not done, no cache\n");
                    292:        return;
                    293:     }
                    294: 
                    295:     /* Seek for worst element */
                    296:     while ((pres = (host_info *) HTList_nextObject(cur))) {
                    297:        if (!worst_match || pres->hits <= worst_hits) {
                    298:            worst_match = pres;
                    299:            worst_hits = pres->hits;
                    300:        }
                    301:     }
                    302:     if (worst_match)
                    303:        HTTCPCacheRemoveElement(worst_match);
                    304: }
                    305: 
                    306: 
                    307: /*                                                     HTTCPCacheAddElement
                    308: **
                    309: **     Add an element to the cache of visited hosts. Note that this function
                    310: **     requires the system implemented structure hostent and not our own
2.24      frystyk   311: **     host_info. The homes variable indicates the number of
                    312: **     IP addresses found.
2.13      frystyk   313: **
                    314: **      Returns new element if OK NULL if error
                    315: */
                    316: PRIVATE host_info *HTTCPCacheAddElement ARGS2(HTAtom *, host,
                    317:                                              struct hostent *, element)
                    318: {
2.25      frystyk   319:     host_info *newhost;
2.22      frystyk   320:     char *addr;
                    321:     char **index = element->h_addr_list;
                    322:     int cnt = 1;
2.13      frystyk   323:     if (!host || !element) {
                    324:        if (TRACE)
                    325:            fprintf(stderr, "HostCache... Bad argument to add to cache\n");
                    326:        return NULL;
                    327:     }
2.22      frystyk   328:     while(*index++)
                    329:        cnt++;
2.25      frystyk   330:     if ((newhost = (host_info *) calloc(1, sizeof(host_info))) == NULL ||
                    331:        (newhost->addrlist = (char **) calloc(1, cnt*sizeof(char*))) == NULL ||
2.22      frystyk   332:        (addr = (char *) calloc(1, cnt*element->h_length)) == NULL)
2.18      frystyk   333:        outofmem(__FILE__, "HTTCPCacheAddElement");
2.25      frystyk   334:     newhost->hostname = host;
2.22      frystyk   335:     index = element->h_addr_list;
                    336:     cnt = 0;
                    337:     while (*index) {
2.25      frystyk   338:        *(newhost->addrlist+cnt) = addr+cnt*element->h_length;
                    339:        memcpy((void *) *(newhost->addrlist+cnt++), *index++,
                    340:               element->h_length);
2.22      frystyk   341:     }
2.25      frystyk   342:     newhost->homes = cnt;
                    343:     if ((newhost->weight = (float *) calloc(newhost->homes,
                    344:                                            sizeof(float))) == NULL)
2.24      frystyk   345:        outofmem(__FILE__, "HTTCPCacheAddElement");
                    346: 
2.25      frystyk   347:     newhost->addrlength = element->h_length;
2.13      frystyk   348:     if (!hostcache)
                    349:        hostcache = HTList_new();
                    350: 
                    351:     if (TRACE) {
2.25      frystyk   352:        if (newhost->homes == 1)
2.24      frystyk   353:            fprintf(stderr, "HostCache... Adding single-homed host `%s'\n",
                    354:                    HTAtom_name(host));
2.13      frystyk   355:        else
2.24      frystyk   356:            fprintf(stderr, "HostCache... Adding host `%s' with %d homes\n",
2.25      frystyk   357:                    HTAtom_name(host), newhost->homes);
2.13      frystyk   358:     }
2.25      frystyk   359:     HTList_addObject(hostcache, (void *) newhost);
2.13      frystyk   360:     HTCacheSize++;                             /* Update number of elements */
2.25      frystyk   361:     return newhost;
2.13      frystyk   362: }
                    363: 
                    364: 
                    365: /*                                                                   HTTCPAddrWeights
                    366: **
                    367: **     This function calculates the weights of the different IP addresses
                    368: **     on a multi homed host. Each weight is calculated as
                    369: **
                    370: **             w(n+1) = w(n)*a + (1-a) * deltatime
                    371: **             a = exp(-1/Neff)
                    372: **             Neff is the effective number of samples used
                    373: **             deltatime is time spend on making a connection
                    374: **
                    375: **     A short window (low Neff) gives a high sensibility, but this is
                    376: **     required as we can't expect a lot of data to test on.
                    377: **
                    378: */
                    379: PUBLIC void HTTCPAddrWeights ARGS2(char *, host, time_t, deltatime)
                    380: {
                    381:     HTAtom *hostatom = HTAtom_for(host);
                    382:     HTList *cur = hostcache;
                    383:     host_info *pres = NULL;
                    384:     if (!hostcache) {
                    385:        fprintf(stderr, "HostCache... Weights not calculated, no cache\n");
                    386:        return;
                    387:     }
2.24      frystyk   388:     /* Skip any port number from host name */
                    389:     if (strchr(host, ':')) {
                    390:        char *newhost = NULL;
                    391:        char *strptr;
                    392:        StrAllocCopy(newhost, host);
                    393:        strptr = strchr(newhost, ':');
                    394:        *strptr = '\0';
                    395:        hostatom = HTAtom_for(newhost);
                    396:        free(newhost);
                    397:     } else
                    398:        hostatom = HTAtom_for(host);
                    399:     
2.13      frystyk   400:     while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
                    401:        if (pres->hostname == hostatom) {
                    402:            break;
                    403:        }
                    404:     }
2.24      frystyk   405:     if (pres) {
2.13      frystyk   406:        int cnt;
2.14      frystyk   407:        CONST float passive = 0.9;        /* Factor for all passive IP_addrs */
2.13      frystyk   408: #if 0
2.14      frystyk   409:        CONST int Neff = 3;
                    410:        CONST float alpha = exp(-1.0/Neff);
2.13      frystyk   411: #else
2.24      frystyk   412:        CONST float alpha = 0.716531310574;     /* Doesn't need the math lib */
2.13      frystyk   413: #endif
2.24      frystyk   414:        for (cnt=0; cnt<pres->homes; cnt++) {
2.13      frystyk   415:            if (cnt == pres->offset) {
                    416:                *(pres->weight+pres->offset) = *(pres->weight+pres->offset)*alpha + (1.0-alpha)*deltatime;
                    417:            } else {
                    418:                *(pres->weight+cnt) = *(pres->weight+cnt) * passive;
                    419:            }
2.24      frystyk   420:            if (PROT_TRACE)
                    421:                fprintf(stderr, "AddrWeights. Home %d has weight %4.2f\n", cnt,
                    422:                        *(pres->weight+cnt));
2.13      frystyk   423:        }
                    424:     } else if (TRACE) {
2.24      frystyk   425:        fprintf(stderr, "HostCache... Weights not calculated, host not found in cache: `%s\'\n", host);
2.13      frystyk   426:     }
                    427: }
                    428: 
2.19      frystyk   429: /* ------------------------------------------------------------------------- */
                    430: /*                          HOST NAME FUNCTIONS                             */
                    431: /* ------------------------------------------------------------------------- */
                    432: 
                    433: #ifndef DECNET  /* Function only used below for a trace message */
                    434: 
                    435: /*     Produce a string for an Internet address
                    436: **     ----------------------------------------
                    437: **
                    438: ** On exit,
                    439: **     returns a pointer to a static string which must be copied if
                    440: **             it is to be kept.
                    441: */
                    442: 
                    443: PUBLIC CONST char * HTInetString ARGS1(SockA *, sin)
                    444: {
                    445: #if 0
2.24      frystyk   446:     /* This dumps core on some Sun systems :-(. The problem is now, that 
2.19      frystyk   447:        the current implememtation only works for IP-addresses and not in
                    448:        other address spaces. */
                    449:     return inet_ntoa(sin->sin_addr);
                    450: #endif
                    451:     static char string[16];
                    452:     sprintf(string, "%d.%d.%d.%d",
                    453:            (int)*((unsigned char *)(&sin->sin_addr)+0),
                    454:            (int)*((unsigned char *)(&sin->sin_addr)+1),
                    455:            (int)*((unsigned char *)(&sin->sin_addr)+2),
                    456:            (int)*((unsigned char *)(&sin->sin_addr)+3));
                    457:     return string;
                    458: }
                    459: #endif /* Decnet */
                    460: 
2.13      frystyk   461: 
                    462: /*                                                          HTGetHostByName
                    463: **
                    464: **     Searched first the local cache then asks the DNS for an address of
                    465: **     the host specified.
                    466: **
2.24      frystyk   467: **      Returns:       >0 if OK the number of homes are returned
                    468: **                     -1 if error
2.13      frystyk   469: */
2.24      frystyk   470: PUBLIC int HTGetHostByName ARGS3(char *, host, SockA *, sin,
                    471:                                 BOOL, use_cur)
2.13      frystyk   472: {
                    473:     HTAtom *hostatom = HTAtom_for(host);
                    474:     host_info *pres = NULL;
                    475:     if (!hostcache)
                    476:        hostcache = HTList_new();                      /* First time through */
                    477:     else {
                    478:        HTList *cur = hostcache;                             /* Search cache */
                    479:        while ((pres = (host_info *) HTList_nextObject(cur)) != NULL) {
                    480:            if (pres->hostname == hostatom) {
                    481:                if (TRACE)
2.18      frystyk   482:                    fprintf(stderr, "HostByName.. Host `%s\' found in cache.\n", host);
2.13      frystyk   483:                break;
                    484:            }
                    485:        }
                    486:     }
                    487:     
                    488:     /* If the host was not found in the cache, then do gethostbyname.
2.24      frystyk   489:        If we are talking to a multi homed host then take the IP address with
                    490:        the lowest weight. If `use_cur'=YES then use current IP-address */
2.13      frystyk   491:     if (pres) {
2.24      frystyk   492:        if (pres->homes > 1 && !use_cur) {
2.13      frystyk   493:            int cnt;
                    494:            float best_weight = 1e30;               /* Should be FLT_MAX :-( */
2.24      frystyk   495:            for (cnt=0; cnt<pres->homes; cnt++) {
2.13      frystyk   496:                if (*(pres->weight+cnt) < best_weight) {
                    497:                    best_weight = *(pres->weight+cnt);
                    498:                    pres->offset = cnt;
                    499:                }
                    500:            }
2.24      frystyk   501:        }
                    502:        pres->hits++;            /* Update total number of hits on this host */
2.13      frystyk   503:     } else {                                           /* Go and ask for it */
                    504:        struct hostent *hostelement;                          /* see netdb.h */
                    505: #ifdef MVS     /* Outstanding problem with crash in MVS gethostbyname */
                    506:        if (TRACE)
2.24      frystyk   507:            fprintf(stderr, "HTTCP on MVS gethostbyname(%s)\n", host);
2.13      frystyk   508: #endif
                    509:        if ((hostelement = gethostbyname(host)) == NULL) {
2.18      frystyk   510:            if (TRACE) fprintf(stderr, "HostByName.. Can't find internet node name `%s'.\n", host);
2.13      frystyk   511:            return -1;
                    512:        }
                    513:        
                    514:        /* Add element to the cache and maybe do garbage collection */
                    515:        if (HTCacheSize >= HTConCacheSize)
                    516:            HTTCPCacheGarbage();
                    517:        if ((pres = HTTCPCacheAddElement(hostatom, hostelement)) == NULL) {
                    518:            return -1;
                    519:        }
                    520:     }
                    521:     
                    522:     /* Update socket structure using the element with the lowest weight. On
                    523:        single homed hosts it means the first value */
                    524:     memcpy(&sin->sin_addr, *(pres->addrlist+pres->offset), pres->addrlength);
2.24      frystyk   525:     return pres->homes;
2.13      frystyk   526: }
                    527: 
                    528: 
2.19      frystyk   529: /*
                    530: **     Get host name of the machine on the other end of a socket.
                    531: **
                    532: */
                    533: PUBLIC char * HTGetHostBySock ARGS1(int, soc)
                    534: {
                    535:     struct sockaddr addr;
                    536:     int len = sizeof(struct sockaddr);
                    537:     struct in_addr *iaddr;
                    538:     struct hostent * phost;            /* Pointer to host -- See netdb.h */
                    539:     char *name = NULL;
                    540: 
                    541: #ifdef DECNET  /* Decnet ain't got no damn name server 8#OO */
                    542:     return NULL;
                    543: #else
                    544:     if (getpeername(soc, &addr, &len) < 0)
                    545:        return NULL;
                    546: 
                    547:     iaddr = &(((struct sockaddr_in *)&addr)->sin_addr);
                    548:     phost=gethostbyaddr((char*)iaddr,
                    549:                        sizeof(struct in_addr),
                    550:                        AF_INET);
                    551:     if (!phost) {
                    552:        if (TRACE) fprintf(stderr,
                    553:                           "TCP......... Can't find internet node name for peer!!\n");
                    554:        return NULL;
                    555:     }
                    556:     StrAllocCopy(name, phost->h_name);
                    557:     if (TRACE) fprintf(stderr, "TCP......... Peer name is `%s'\n", name);
                    558: 
                    559:     return name;
                    560: 
                    561: #endif /* not DECNET */
                    562: }
                    563: 
                    564: 
1.1       timbl     565: /*     Parse a network node address and port
                    566: **     -------------------------------------
                    567: **
                    568: ** On entry,
                    569: **     str     points to a string with a node name or number,
                    570: **             with optional trailing colon and port number.
                    571: **     sin     points to the binary internet or decnet address field.
                    572: **
2.24      frystyk   573: ** On exit,    -1      If error
                    574: **             >0      If OK the number of homes on the host
1.1       timbl     575: **     *sin    is filled in. If no port is specified in str, that
                    576: **             field is left unchanged in *sin.
2.13      frystyk   577: **
                    578: ** NOTE:       It is assumed that any portnumber and numeric host address
                    579: **             is given in decimal notation. Separation character is '.'
1.1       timbl     580: */
2.24      frystyk   581: PUBLIC int HTParseInet ARGS3(SockA *, sin, CONST char *, str,
                    582:                             BOOL, use_cur)
1.1       timbl     583: {
2.13      frystyk   584:     char *host = NULL;
2.24      frystyk   585:     int status = 0;
2.13      frystyk   586:     StrAllocCopy(host, str);                 /* Take a copy we can mutilate */
1.1       timbl     587: 
2.13      frystyk   588:     /* Parse port number if present. */    
                    589:     {
                    590:        char *port;
                    591:        if ((port=strchr(host, ':'))) {
                    592:            *port++ = 0;                                    /* Chop off port */
                    593:            if (isdigit(*port)) {
1.1       timbl     594: #ifdef DECNET
2.13      frystyk   595:                sin->sdn_objnum = (unsigned char)(strtol(port, (char**)0, 10));
                    596: #else /* Internet */
                    597:                sin->sin_port = htons(atol(port));
1.1       timbl     598: #endif
2.13      frystyk   599:            } else {
2.24      frystyk   600:                if (PROT_TRACE)
                    601:                    fprintf(stderr, "ParseInet... No port indicated\n");
                    602:                free(host);
                    603:                return -1;
2.13      frystyk   604:            }
1.1       timbl     605:        }
2.13      frystyk   606:     }
1.1       timbl     607: 
2.13      frystyk   608:     /* Parse Internet host */
1.1       timbl     609: #ifdef DECNET
                    610:     /* read Decnet node name. @@ Should know about DECnet addresses, but it's
                    611:        probably worth waiting until the Phase transition from IV to V. */
                    612: 
                    613:     sin->sdn_nam.n_len = min(DN_MAXNAML, strlen(host));  /* <=6 in phase 4 */
                    614:     strncpy (sin->sdn_nam.n_name, host, sin->sdn_nam.n_len + 1);
                    615: 
                    616:     if (TRACE) fprintf(stderr,  
                    617:        "DECnet: Parsed address as object number %d on host %.6s...\n",
                    618:                      sin->sdn_objnum, host);
                    619: 
2.13      frystyk   620: #else /* Internet */
1.1       timbl     621: 
2.13      frystyk   622:     /* Parse host number if present */
                    623:     {
                    624:        BOOL numeric = YES;
                    625:        char *strptr = host;
                    626:        while (*strptr) {
                    627:            if (!isdigit(*strptr) && *strptr != '.') {
                    628:                numeric = NO;
                    629:                break;
                    630:            }
                    631:            ++strptr;
                    632:        }
                    633:        if (numeric) {
                    634:            sin->sin_addr.s_addr = inet_addr(host);       /* See arpa/inet.h */
                    635:        } else {
2.24      frystyk   636:            if ((status = HTGetHostByName(host, sin, use_cur)) < 0) {
2.13      frystyk   637:                free(host);
                    638:                return -1;
                    639:            }
                    640:        }
                    641:        if (TRACE) {
                    642:            fprintf(stderr, "ParseInet... Parsed address as port %d on %s\n",
                    643:                    (int) ntohs(sin->sin_port),
                    644:                    HTInetString(sin));
1.1       timbl     645:        }
                    646:     }
                    647: #endif  /* Internet vs. Decnet */
2.13      frystyk   648:     free(host);
2.24      frystyk   649:     return status;
1.1       timbl     650: }
                    651: 
                    652: 
2.16      frystyk   653: #ifdef OLD_CODE
1.1       timbl     654: /*     Derive the name of the host on which we are
                    655: **     -------------------------------------------
                    656: **
                    657: */
2.8       luotonen  658: PRIVATE void get_host_details NOARGS
1.1       timbl     659: 
                    660: #ifndef MAXHOSTNAMELEN
                    661: #define MAXHOSTNAMELEN 64              /* Arbitrary limit */
                    662: #endif
                    663: 
                    664: {
                    665:     char name[MAXHOSTNAMELEN+1];       /* The name of this host */
                    666:     struct hostent * phost;            /* Pointer to host -- See netdb.h */
                    667:     int namelength = sizeof(name);
                    668:     
                    669:     if (hostname) return;              /* Already done */
                    670:     gethostname(name, namelength);     /* Without domain */
2.18      frystyk   671:     if (TRACE) fprintf(stderr, "TCP......... Local host name is %s\n", name);
1.1       timbl     672:     StrAllocCopy(hostname, name);
                    673: 
                    674: #ifndef DECNET  /* Decnet ain't got no damn name server 8#OO */
                    675:     phost=gethostbyname(name);         /* See netdb.h */
                    676:     if (!phost) {
                    677:        if (TRACE) fprintf(stderr, 
2.9       luotonen  678:                "TCP......... Can't find my own internet node address for `%s'!!\n",
1.1       timbl     679:                name);
                    680:        return;  /* Fail! */
                    681:     }
                    682:     StrAllocCopy(hostname, phost->h_name);
2.18      frystyk   683:     if (TRACE)
                    684:        fprintf(stderr, "TCP......... Full local host name is %s\n", hostname);
2.8       luotonen  685: 
                    686: #ifdef NEED_HOST_ADDRESS               /* no -- needs name server! */
1.1       timbl     687:     memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
                    688:     if (TRACE) fprintf(stderr, "     Name server says that I am `%s' = %s\n",
                    689:            hostname, HTInetString(&HTHostAddress));
2.8       luotonen  690: #endif /* NEED_HOST_ADDRESS */
1.1       timbl     691: 
                    692: #endif /* not Decnet */
                    693: }
2.16      frystyk   694: #endif /* OLD_CODE */
1.1       timbl     695: 
2.19      frystyk   696: 
2.24      frystyk   697: /*                                                             HTGetDomainName
                    698: **     Returns the current domain name without the local host name.
                    699: **     The response is pointing to a static area that might be changed
                    700: **     using HTSetHostName(). Returns NULL on error
                    701: */
                    702: PUBLIC CONST char *HTGetDomainName NOARGS
                    703: {
                    704:     CONST char *host = HTGetHostName();
                    705:     char *domain;
                    706:     if (host && *host) {
                    707:        if ((domain = strchr(host, '.')) != NULL)
                    708:            return ++domain;
                    709:        else
                    710:            return host;
                    711:     } else
                    712:        return NULL;
                    713: }
                    714: 
                    715: 
                    716: 
2.19      frystyk   717: /*                                                             HTSetHostName
                    718: **     Sets the current hostname inclusive domain name.
                    719: **     If this is not set then the default approach is used using
                    720: **     HTGetHostname().
                    721: */
                    722: PUBLIC void HTSetHostName ARGS1(char *, host)
                    723: {
2.24      frystyk   724:     if (host && *host) {
                    725:        char *strptr;
2.19      frystyk   726:        StrAllocCopy(hostname, host);
2.24      frystyk   727:        strptr = hostname;
                    728:        while (*strptr) {
                    729:            *strptr = TOLOWER(*strptr);
                    730:            strptr++;
                    731:        }
                    732:        if (*(hostname+strlen(hostname)-1) == '.')    /* Remove trailing dot */
                    733:            *(hostname+strlen(hostname)-1) = '\0';
                    734:     } else {
2.19      frystyk   735:        if (TRACE) fprintf(stderr, "SetHostName. Bad argument ignored\n");
                    736:     }
                    737: }
                    738: 
                    739: 
                    740: /*                                                             HTGetHostName
2.18      frystyk   741: **     Returns the name of this host. It uses the following algoritm:
                    742: **
                    743: **     1) gethostname()
                    744: **     2) if the hostname doesn't contain any '.' try to read
                    745: **        /etc/resolv.conf. If there is no domain line in this file then
                    746: **     3) Try getdomainname and do as the man pages say for resolv.conf (sun)
                    747: **             If there is no domain line in this file, then it is derived
                    748: **             from the domain name set by the domainname(1) command, usually
                    749: **             by removing the first component. For example, if the domain-
                    750: **             name is set to ``foo.podunk.edu'' then the default domain name
                    751: **             used will be ``pudunk.edu''.
                    752: **
                    753: **     This is the same procedure as used by res_init() and sendmail.
2.16      frystyk   754: **
                    755: **     Return: hostname on success else NULL
                    756: */
2.19      frystyk   757: PUBLIC CONST char * HTGetHostName NOARGS
1.1       timbl     758: {
2.18      frystyk   759:     BOOL got_it = NO;
                    760:     FILE *fp;
2.16      frystyk   761:     char name[MAXHOSTNAMELEN+1];
2.18      frystyk   762:     if (hostname) {                                      /* If already done */
                    763:        if (*hostname)
                    764:            return hostname;
                    765:        else
                    766:            return NULL;                    /* We couldn't get the last time */
                    767:     }
2.16      frystyk   768:     *(name+MAXHOSTNAMELEN) = '\0';
                    769:     if (gethostname(name, MAXHOSTNAMELEN)) {        /* Maybe without domain */
                    770:        if (TRACE)
                    771:            fprintf(stderr, "HostName.... Can't get host name\n");
                    772:        return NULL;
                    773:     }
2.18      frystyk   774:     if (TRACE)
2.24      frystyk   775:        fprintf(stderr, "HostName.... Local host name is  `%s\'\n", name);
2.16      frystyk   776:     StrAllocCopy(hostname, name);
2.24      frystyk   777:     {
                    778:        char *strptr = strchr(hostname, '.');
                    779:        if (strptr != NULL)                                /* We have it all */
                    780:            got_it = YES;
                    781:     }
2.16      frystyk   782: 
2.23      duns      783: #ifndef VMS
2.18      frystyk   784:     /* Now try the resolver config file */
2.24      frystyk   785:     if (!got_it && (fp = fopen(RESOLV_CONF, "r")) != NULL) {
2.18      frystyk   786:        char buffer[80];
                    787:        *(buffer+79) = '\0';
                    788:        while (fgets(buffer, 79, fp)) {
                    789:            if (!strncasecomp(buffer, "domain", 6)) {   
                    790:                char *domainstr = buffer+6;
                    791:                char *end;
                    792:                while (*domainstr == ' ' || *domainstr == '\t')
                    793:                    domainstr++;
                    794:                end = domainstr;
                    795:                while (*end && !isspace(*end))
                    796:                    end++;
                    797:                *end = '\0';
                    798:                if (*domainstr) {
                    799:                    StrAllocCat(hostname, ".");
                    800:                    StrAllocCat(hostname, domainstr);
                    801:                    got_it = YES;
                    802:                    break;
                    803:                }
                    804:            }
                    805:        }
                    806:        fclose(fp);
2.16      frystyk   807:     }
                    808: 
2.18      frystyk   809:     /* If everything else has failed then try getdomainname */
2.26    ! frystyk   810: #ifndef sco
2.18      frystyk   811:     if (!got_it) {
                    812:        if (getdomainname(name, MAXHOSTNAMELEN)) {
                    813:            if (TRACE)
                    814:                fprintf(stderr, "HostName.... Can't get domain name\n");
2.24      frystyk   815:            StrAllocCopy(hostname, "");
2.18      frystyk   816:            return NULL;
                    817:        }
                    818: 
                    819:        /* If the host name and the first part of the domain name are different
                    820:           then use the former as it is more exact (I guess) */
                    821:        if (strncmp(name, hostname, (int) strlen(hostname))) {
                    822:            char *domain = strchr(name, '.');
                    823:            if (!domain)
                    824:                domain = name;
2.26    ! frystyk   825:            StrAllocCat(hostname, ".");
2.18      frystyk   826:            StrAllocCat(hostname, domain);
                    827:        }
2.16      frystyk   828:     }
2.26    ! frystyk   829: #endif /* not sco */
2.23      duns      830: #endif /* not VMS */
                    831: 
2.24      frystyk   832:     {
                    833:        char *strptr = hostname;
                    834:        while (*strptr) {           
                    835:            *strptr = TOLOWER(*strptr);
                    836:            strptr++;
                    837:        }
                    838:        if (*(hostname+strlen(hostname)-1) == '.')    /* Remove trailing dot */
                    839:            *(hostname+strlen(hostname)-1) = '\0';
                    840:     }
2.18      frystyk   841:     if (TRACE)
2.24      frystyk   842:        fprintf(stderr, "HostName.... Full host name is `%s\'\n", hostname);
2.18      frystyk   843:     return hostname;
                    844: 
                    845: #ifndef DECNET  /* Decnet ain't got no damn name server 8#OO */
                    846: #ifdef OLD_CODE
                    847:                              /* Now we try to get information on the domain */
2.16      frystyk   848:     {
                    849:        struct hostent *hostelement;
                    850:        if ((hostelement = gethostbyname(hostname)) == NULL) {
                    851:            if (TRACE)
                    852:                fprintf(stderr, "HostName.... Can't find host name on DNS\n");
                    853:            FREE(hostname);
                    854:            return NULL;
                    855:        }
2.17      frystyk   856:        StrAllocCopy(hostname, (char *) hostelement->h_name);
2.16      frystyk   857:     }
2.18      frystyk   858: #endif /* OLD_CODE */
2.16      frystyk   859: #endif /* not Decnet */
2.13      frystyk   860: }
                    861: 
2.19      frystyk   862: 
                    863: /*                                                            HTSetMailAddress
                    864: **     Sets the current mail address plus host name and domain name.
                    865: **     If this is not set then the default approach is used using
                    866: **     HTGetMailAddress().
                    867: */
                    868: PUBLIC void HTSetMailAddress ARGS1(char *, address)
                    869: {
                    870:     if (address && *address)
                    871:        StrAllocCopy(mailaddress, address);
                    872:     else {
                    873:        if (TRACE) fprintf(stderr, "SetMailAddress. Bad argument ignored\n");
                    874:     }
                    875: }
                    876: 
                    877: 
                    878: /*                                                            HTGetMailAddress
                    879: **
                    880: **     Get the mail address of the current user on the current host. The
                    881: **     domain name used is the one initialized in HTSetHostName or
                    882: **     HTGetHostName. The login name is determined using (ordered):
                    883: **
                    884: **             getlogin
                    885: **             getpwuid(getuid())
                    886: **
                    887: **     The weakness about the last attempt is if the user has multiple
                    888: **     login names each with the same user ID. If this fails as well then:
                    889: **
                    890: **             LOGNAME environment variable
                    891: **             USER environment variable
                    892: **
                    893: **     Returns NULL if error else pointer to static string
                    894: */
                    895: PUBLIC CONST char * HTGetMailAddress NOARGS
                    896: {
                    897:     char *login;
                    898:     CONST char *domain;
                    899:     struct passwd *pw_info;
2.21      frystyk   900:     if (mailaddress) {
                    901:        if (*mailaddress)
                    902:            return mailaddress;
                    903:        else
                    904:            return NULL;       /* No luck the last time so we wont try again */
                    905:     }
2.23      duns      906: 
                    907: #ifdef VMS
                    908:     if ((login = (char *) cuserid(NULL)) == NULL) {
                    909:         if (TRACE) fprintf(stderr, "MailAddress. cuserid returns NULL\n");
                    910:     }
                    911: 
                    912: #else /* not VMS */
2.19      frystyk   913:     if ((login = (char *) getlogin()) == NULL) {
                    914:        if (TRACE) fprintf(stderr, "MailAddress. getlogin returns NULL\n");
                    915:        if ((pw_info = getpwuid(getuid())) == NULL) {
                    916:            if (TRACE) fprintf(stderr, "MailAddress. getpwid returns NULL\n");
                    917:            if ((login = getenv("LOGNAME")) == NULL) {
                    918:                if (TRACE) fprintf(stderr, "MailAddress. LOGNAME not found\n");
                    919:                if ((login = getenv("USER")) == NULL) {
                    920:                    if (TRACE) fprintf(stderr,"MailAddress. USER not found\n");
                    921:                    return NULL;                /* I GIVE UP */
                    922:                }
                    923:            }
                    924:        } else
                    925:            login = pw_info->pw_name;
                    926:     }
2.23      duns      927: #endif /* not VMS */
                    928: 
2.19      frystyk   929:     if (login) {
                    930:        StrAllocCopy(mailaddress, login);
                    931:        StrAllocCat(mailaddress, "@");
                    932:        if ((domain = HTGetHostName()) != NULL)
                    933:            StrAllocCat(mailaddress, domain);
2.21      frystyk   934:        else {
                    935:            *mailaddress = '\0';
                    936:            return NULL;                        /* Domain name not available */
                    937:        }
2.19      frystyk   938:        return mailaddress;
                    939:     }
                    940:     return NULL;
                    941: }
                    942: 
                    943: /* ------------------------------------------------------------------------- */
                    944: /*                   CONNECTION ESTABLISHMENT MANAGEMENT                    */
                    945: /* ------------------------------------------------------------------------- */
2.13      frystyk   946: 
                    947: /*                                                             HTDoConnect()
                    948: **
                    949: **     Note: Any port indication in URL, e.g., as `host:port' overwrites
                    950: **     the default_port value.
                    951: **
                    952: **     Returns 0 if OK, -1 on error
                    953: */
2.24      frystyk   954: PUBLIC int HTDoConnect ARGS5(HTNetInfo *, net, char *, url,
                    955:                             u_short, default_port, u_long *, addr,
                    956:                             BOOL, use_cur)
2.13      frystyk   957: {
                    958:     time_t deltatime;
2.24      frystyk   959:     int hosts;
2.13      frystyk   960:     int status;
                    961:     SockA sock_addr;                           /* SockA is defined in tcp.h */
                    962:     char *p1 = HTParse(url, "", PARSE_HOST);
                    963:     char *at_sign;
                    964:     char *host;
                    965: 
                    966:     /* if theres an @ then use the stuff after it as a hostname */
                    967:     if((at_sign = strchr(p1,'@')) != NULL)
                    968:        host = at_sign+1;
                    969:     else
                    970:        host = p1;
2.24      frystyk   971:     if (!*host) {
                    972:        HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_HOST,
                    973:                   NULL, 0, "HTDoConnect");
                    974:        free(p1);
                    975:        return -1;
                    976:     } else
                    977:        if (TRACE) fprintf(stderr, "HTDoConnect. Looking up `%s\'\n", host);
2.13      frystyk   978: 
                    979:    /* Set up defaults */
                    980:     memset((void *) &sock_addr, '\0', sizeof(sock_addr));
                    981: #ifdef DECNET
                    982:     sock_addr.sdn_family = AF_DECnet;        /* Family = DECnet, host order */
                    983:     sock_addr.sdn_objnum = DNP_OBJ;          /* Default: http object number */
                    984: #else  /* Internet */
                    985:     sock_addr.sin_family = AF_INET;
                    986:     sock_addr.sin_port = htons(default_port);
                    987: #endif
                    988: 
2.24      frystyk   989:     /* If we are trying to connect to a multi-homed host then loop here until
                    990:        success or we have tried all IP-addresses */
                    991:     do {
                    992:        if ((hosts = HTParseInet(&sock_addr, host, use_cur)) < 0) {
                    993:            if (TRACE) fprintf(stderr, "HTDoConnect. Can't locate remote host `%s\'\n", host);
                    994:            HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_NO_REMOTE_HOST,
                    995:                       (void *) host, strlen(host), "HTDoConnect");
                    996:            goto errorend;
                    997:        }
2.13      frystyk   998: 
                    999: #ifdef DECNET
2.24      frystyk  1000:        if ((net->sockfd = socket(AF_DECnet, SOCK_STREAM, 0)) < 0)
2.13      frystyk  1001: #else
2.24      frystyk  1002:        if ((net->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
2.13      frystyk  1003: #endif
2.24      frystyk  1004:        {
                   1005:            HTErrorSysAdd(net->request, ERR_FATAL, NO, "socket");
                   1006:            goto errorend;
                   1007:        }
                   1008:        if (addr)
                   1009:            *addr = ntohl(sock_addr.sin_addr.s_addr);
                   1010:        if (PROT_TRACE)
                   1011:            fprintf(stderr, "HTDoConnect. Created socket number %d\n",
                   1012:                    net->sockfd);
                   1013:        
                   1014:        /* If multi-homed host then start timer on connection */
                   1015:        if (hosts > 1)
                   1016:            deltatime = time(NULL);
                   1017: 
                   1018:        status = connect(net->sockfd, (struct sockaddr *) &sock_addr,
                   1019:                         sizeof(sock_addr));
                   1020:        if (hosts > 1) {
                   1021:            deltatime = time(NULL) - deltatime;
                   1022:            if (status < 0) {
                   1023:                HTErrorSysAdd(net->request, ERR_NON_FATAL, NO, "connect");
                   1024:                if (errno==ECONNREFUSED || errno==ETIMEDOUT ||
                   1025:                    errno==ENETUNREACH || errno==EHOSTUNREACH ||
                   1026:                    errno==EHOSTDOWN)
                   1027:                    deltatime += TCP_DELAY;
                   1028:                else
                   1029:                    deltatime += TCP_PENALTY;
                   1030:                if (NETCLOSE(net->sockfd) < 0)
                   1031:                    HTErrorSysAdd(net->request, ERR_FATAL, NO, "close");
                   1032:                HTTCPAddrWeights(host, deltatime);
                   1033:            } else {
                   1034:                HTTCPAddrWeights(host, deltatime);
                   1035:                break;
                   1036:            }
                   1037:        } else if (status < 0) {
                   1038:            HTErrorSysAdd(net->request, ERR_FATAL, NO, "connect");
                   1039:            HTTCPCacheRemoveHost(host);
                   1040:            if (NETCLOSE(net->sockfd) < 0)
                   1041:                HTErrorSysAdd(net->request, ERR_FATAL, NO, "close");
                   1042:            goto errorend;
                   1043:        }
                   1044:     } while (net->addressCount++ < hosts-1);
2.13      frystyk  1045: 
2.24      frystyk  1046:     if (hosts > 1 && net->addressCount >= hosts) {
                   1047:        if (PROT_TRACE) fprintf(stderr, "HTDoConnect. None of the %d addresses on multi-homed host is accessible\n", hosts);
                   1048:        goto errorend;
2.13      frystyk  1049:     }
                   1050: 
                   1051:     free(p1);
2.24      frystyk  1052:     net->addressCount = 0;
2.13      frystyk  1053:     return status;
2.24      frystyk  1054: 
                   1055:   errorend:
                   1056:     free (p1);
                   1057:     net->addressCount = 0;
                   1058:     net->sockfd = -1;
                   1059:     return -1;
2.13      frystyk  1060: }
                   1061: 
                   1062: 
                   1063: /*                                                             HTDoAccept()
                   1064: **
                   1065: **     This function makes a non-blocking accept on a port and polls every
                   1066: **     second until MAX_ACCEPT_POLL or interrupted by user.
                   1067: **
                   1068: **     BUGS Interrupted is not yet implemented!!!
                   1069: **
                   1070: **     Returns 0 if OK, -1 on error
                   1071: */
2.15      frystyk  1072: PUBLIC int HTDoAccept ARGS1(HTNetInfo *, net)
2.13      frystyk  1073: {
                   1074:     SockA soc_address;                         /* SockA is defined in tcp.h */
                   1075:     int status;
                   1076:     int cnt;
                   1077:     int soc_addrlen = sizeof(soc_address);
2.15      frystyk  1078:     if (net->sockfd < 0) {
2.13      frystyk  1079:        if (TRACE) fprintf(stderr, "HTDoAccept.. Bad socket number\n");
                   1080:        return -1;
                   1081:     }
                   1082:        
                   1083:     /* First make the socket non-blocking */
2.23      duns     1084: #ifdef VMS
                   1085: #ifdef MULTINET
                   1086:     {
                   1087:        int enable = 1;
                   1088:        status = socket_ioctl(net->sockfd, FIONBIO, &enable);
                   1089:     }
                   1090: #endif /* MULTINET */
                   1091: #else /* not VMS */
2.15      frystyk  1092:     if((status = FCNTL(net->sockfd, F_GETFL, 0)) != -1) {
2.23      duns     1093:        status |= FNDELAY;                                      /* O_NDELAY; */
2.15      frystyk  1094:        status = FCNTL(net->sockfd, F_SETFL, status);
2.13      frystyk  1095:     }
2.23      duns     1096: #endif /* not VMS */
2.13      frystyk  1097:     if (status == -1) {
2.15      frystyk  1098:        HTErrorSysAdd(net->request, ERR_FATAL, NO, "fcntl");
2.13      frystyk  1099:        return -1;
                   1100:     }
                   1101: 
                   1102:     /* Now poll every sekund */
                   1103:     for(cnt=0; cnt<MAX_ACCEPT_POLL; cnt++) {
2.15      frystyk  1104:        if ((status = accept(net->sockfd, (struct sockaddr*) &soc_address,
2.13      frystyk  1105:                             &soc_addrlen)) >= 0) {
                   1106:            if (TRACE) fprintf(stderr,
                   1107:                               "HTDoAccept.. Accepted new socket %d\n", 
                   1108:                               status);
                   1109:            return status;
                   1110:        } else
2.15      frystyk  1111:            HTErrorSysAdd(net->request, ERR_WARNING, YES, "accept");
2.13      frystyk  1112:        sleep(1);
                   1113:     }  
                   1114:     
                   1115:     /* If nothing has happened */    
                   1116:     if (TRACE)
                   1117:        fprintf(stderr, "HTDoAccept.. Timed out, no connection!\n");
2.15      frystyk  1118:     HTErrorAdd(net->request, ERR_FATAL, NO, HTERR_TIME_OUT, NULL, 0,
                   1119:               "HTDoAccept");
2.13      frystyk  1120:     return -1;
1.1       timbl    1121: }
                   1122: 

Webmaster