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

2.31      frystyk     1: /*                                                                     HTTCP.c
2.83      frystyk     2: **     TCP SPECIFIC CODE
2.31      frystyk     3: **
2.42      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.31      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
2.94    ! frystyk     6: **     @(#) $Id: HTTCP.c,v 2.93 1996/09/16 21:33:51 eric Exp $
1.1       timbl       7: **
                      8: **     This code is in common between client and server sides.
                      9: **
                     10: **     16 Jan 92  TBL  Fix strtol() undefined on CMU Mach.
                     11: **     25 Jun 92  JFG  Added DECNET option through TCP socket emulation.
2.7       duns       12: **     13 Sep 93  MD   Added correct return of vmserrorno for HTInetStatus.
                     13: **                     Added decoding of vms error message for MULTINET.
2.13      frystyk    14: **     31 May 94  HF   Added cache on host id's; now use inet_ntoa() to
                     15: **                     HTInetString and some other fixes. Added HTDoConnect
                     16: **                     and HTDoAccept
1.1       timbl      17: */
                     18: 
2.36      frystyk    19: /* Library include files */
2.79      frystyk    20: #include "sysdep.h"
2.83      frystyk    21: #include "WWWUtil.h"
                     22: #include "WWWCore.h"
                     23: 
2.58      frystyk    24: #include "HTReqMan.h"
2.54      frystyk    25: #include "HTNetMan.h"
2.36      frystyk    26: #include "HTTCP.h"                                      /* Implemented here */
2.29      frystyk    27: 
2.36      frystyk    28: /* VMS stuff */
                     29: #ifdef VMS
                     30: #ifndef MULTINET
                     31: #define FD_SETSIZE 32
                     32: #else /* Multinet */
                     33: #define FD_SETSIZE 256
                     34: #endif /* Multinet */
                     35: #endif /* VMS */
                     36: 
2.13      frystyk    37: /* Macros and other defines */
2.24      frystyk    38: /* x seconds penalty on a multi-homed host if IP-address is down */
                     39: #define TCP_PENALTY            1200
                     40: 
                     41: /* x seconds penalty on a multi-homed host if IP-address is timed out */
                     42: #define TCP_DELAY              600
                     43: 
2.93      eric       44: /* imperical study in socket call error codes
                     45:  */
                     46: #ifdef _WINSOCKAPI_                                    /* windows */
                     47: #define NETCALL_ERROR(ret)     (ret == SOCKET_ERROR)
                     48: #define NETCALL_DEADSOCKET(err)        (err == WSAEBADF)
                     49: #define NETCALL_WOULDBLOCK(err)        (err == WSAEWOULDBLOCK)
                     50: #else /* _WINSOCKAPI_                                     unix    */
                     51: #define NETCALL_ERROR(ret)     (ret < 0)
                     52: #define NETCALL_DEADSOCKET(err)        (err == EBADF)
                     53: #if defined(EAGAIN) && defined(EALREADY)
                     54: #define NETCALL_WOULDBLOCK(err)        (err == EINPROGRESS || \
                     55:                                 err == EALREADY || \
                     56:                                 err == EAGAIN)
                     57: #else /* (EAGAIN && EALREADY) */
                     58: #ifdef EALREADY
                     59: #define NETCALL_WOULDBLOCK(err)        (err == EINPROGRESS || err == EALREADY)
                     60: #else /* EALREADY */
                     61: #ifdef EAGAIN
                     62: #define NETCALL_WOULDBLOCK(err)        (err == EINPROGRESS || err == EAGAIN)
                     63: #else /* EAGAIN */
                     64: #define NETCALL_WOULDBLOCK(err)        (err == EINPROGRESS)
                     65: #endif /* !EAGAIN */
                     66: #endif /* !EALREADY */
                     67: #endif /* !(EAGAIN && EALREADY) */
                     68: #endif /* !_WINSOCKAPI_                                   done */
2.13      frystyk    69: /* ------------------------------------------------------------------------- */
2.19      frystyk    70: /*                   CONNECTION ESTABLISHMENT MANAGEMENT                    */
                     71: /* ------------------------------------------------------------------------- */
2.13      frystyk    72: 
2.93      eric       73: /* _makeSocket - create a socket, if !net->preemptive, set FIONBIO
                     74:  * returns 1: blocking
                     75:  *        0: non-blocking
                     76:  *       -1: creation error
                     77:  */
                     78: int _makeSocket(HTNet * net)
                     79: {
                     80:     int status;
                     81: #ifdef DECNET
                     82:            if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
                     83: #else
                     84:            if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
                     85: #endif
                     86:            {
                     87:                HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, 
                     88:                                         NO, "socket");
                     89:                net->tcpstate = TCP_ERROR;
                     90:                return -1;
                     91:            }
                     92:            if (PROT_TRACE)
                     93:                HTTrace("Socket...... Created %d\n", net->sockfd);
                     94: 
                     95:            /* Increase the number of sockets by one */
                     96:            HTNet_increaseSocket();
                     97: 
                     98:            /* If non-blocking protocol then change socket status
                     99:            ** I use fcntl() so that I can ask the status before I set it.
                    100:            ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
                    101:            ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
                    102:            ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
                    103:            ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
                    104:            */
                    105:            if (!net->preemptive) {
                    106: #ifdef _WINSOCKAPI_
                    107:                {               /* begin windows scope  */
                    108:                    long levents = FD_READ | FD_WRITE | FD_ACCEPT | 
                    109:                        FD_CONNECT | FD_CLOSE ;
                    110:                    int rv = 0 ;
                    111:                    u_long one = 1;
                    112:                                    
                    113:                    status = ioctlsocket(net->sockfd, FIONBIO, &one) == 
                    114:                             SOCKET_ERROR ? -1 : 0;
                    115:                } /* end scope */
                    116: #else /* _WINSOCKAPI_ */
                    117: #if defined(VMS)
                    118:                {
                    119:                    int enable = 1;
                    120:                    status = IOCTL(net->sockfd, FIONBIO, &enable);
                    121:                }
                    122: #else /* VMS */
                    123:                if((status = fcntl(net->sockfd, F_GETFL, 0)) != -1) {
                    124: #ifdef O_NONBLOCK
                    125:                    status |= O_NONBLOCK;                           /* POSIX */
                    126: #else /* O_NONBLOCK */
                    127: #ifdef F_NDELAY
                    128:                    status |= F_NDELAY;                               /* BSD */
                    129: #endif /* F_NDELAY */
                    130: #endif /* !O_NONBLOCK */
                    131:                    status = fcntl(net->sockfd, F_SETFL, status);
                    132:                }
                    133: #endif /* !VMS */
                    134: #endif /* !_WINSOCKAPI_ */
                    135:                if (PROT_TRACE) {
                    136:                    if (status == -1)
                    137:                        HTTrace("Sockrt...... Blocking socket\n");
                    138:                    else
                    139:                        HTTrace("Socket...... Non-blocking socket\n");
                    140:                }
                    141:            } else if (PROT_TRACE)
                    142:                HTTrace("Socket...... Blocking socket\n");
                    143: 
                    144:     return status == -1 ? 1 : 0;
                    145: }
                    146: 
2.13      frystyk   147: /*                                                             HTDoConnect()
                    148: **
                    149: **     Note: Any port indication in URL, e.g., as `host:port' overwrites
2.54      frystyk   150: **     the default port value.
2.13      frystyk   151: **
2.40      frystyk   152: **     returns         HT_ERROR        Error has occured or interrupted
                    153: **                     HT_OK           if connected
                    154: **                     HT_WOULD_BLOCK  if operation would have blocked
2.13      frystyk   155: */
2.54      frystyk   156: PUBLIC int HTDoConnect (HTNet * net, char * url, u_short default_port)
2.13      frystyk   157: {
                    158:     int status;
2.88      frystyk   159:     HTRequest * request = HTNet_request(net);
2.91      frystyk   160:     char * hostname = HTHost_name(HTNet_host(net));
2.13      frystyk   161: 
2.55      frystyk   162:     /* Jump into the state machine */
                    163:     while (1) {
                    164:        switch (net->tcpstate) {
                    165:          case TCP_BEGIN:
2.91      frystyk   166:          {
                    167:              char * proxy = HTRequest_proxy(request);
                    168:              char * fullhost = NULL;
                    169:              char * host = NULL;
                    170: 
                    171:              /* Check to see whether we connect directly or via a proxy */
                    172:              fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
                    173: 
                    174:              /* If there's an @ then use the stuff after it as a hostname */
                    175:              if (fullhost) {
                    176:                  char * at_sign;
                    177:                  if ((at_sign = strchr(fullhost, '@')) != NULL)
                    178:                      host = at_sign+1;
                    179:                  else
                    180:                      host = fullhost;
                    181:              }
                    182:              if (!host || !*host) {
                    183:                  HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
                    184:                                     NULL, 0, "HTDoConnect");
                    185:                  HT_FREE(fullhost);
                    186:                  return HT_ERROR;
                    187:              } else {
                    188:                  char *port = strchr(host, ':');
                    189:                  SockA *sin = &net->sock_addr;
                    190:                  memset((void *) sin, '\0', sizeof(SockA));
                    191:                  if (PROT_TRACE)
                    192:                      HTTrace("HTDoConnect. Looking up `%s\'\n", host);
                    193:                  if (port) {
                    194:                      *port++ = '\0';
                    195:                      if (*port && isdigit(*port)) {
2.54      frystyk   196: #ifdef DECNET
2.91      frystyk   197:                          sin->sdn_family = AF_DECnet;
                    198:                          sin->sdn_objnum =
                    199:                              (unsigned char)(strtol(port, (char **) 0, 10));
2.55      frystyk   200: #else
2.91      frystyk   201:                          sin->sin_family = AF_INET;
                    202:                          sin->sin_port = htons(atol(port));
2.54      frystyk   203: #endif
2.91      frystyk   204:                      }
                    205:                  } else {
2.13      frystyk   206: #ifdef DECNET
2.91      frystyk   207:                      sin->sdn_family = AF_DECnet;
                    208:                      net->sock_addr.sdn_objnum = DNP_OBJ;
2.13      frystyk   209: #else  /* Internet */
2.91      frystyk   210:                      sin->sin_family = AF_INET;
                    211:                      sin->sin_port = htons(default_port);
2.13      frystyk   212: #endif
2.91      frystyk   213:                  }
                    214:              }
2.83      frystyk   215: 
2.91      frystyk   216:              /* Find information about this host */
                    217:              if ((net->host = HTHost_new(host)) == NULL) {
                    218:                  if (PROT_TRACE)HTTrace("HTDoConnect. Can't get host info\n");
                    219:                  net->tcpstate = TCP_ERROR;
                    220:                  break;
                    221:              }
                    222: 
                    223:              /*
                    224:              ** Add the net object to the host object found above. If the
                    225:              ** host is idle then we can start the request right away,
                    226:              ** otherwise we must wait until it is free. 
                    227:              */
                    228:              if ((status = HTHost_addNet(net->host, net)) == HT_PENDING)
                    229:                  if (PROT_TRACE) HTTrace("HTDoConnect. Pending...\n");
                    230:              HT_FREE(fullhost);
                    231: 
                    232:              /*
                    233:              ** If we are pending hen return here, otherwise go to next state
                    234:              ** which is setting up a channel
                    235:              */
                    236:              net->tcpstate = TCP_CHANNEL;
                    237:              if (status == HT_PENDING) return HT_PENDING;
                    238:          }
                    239:          break;
2.83      frystyk   240: 
2.91      frystyk   241:        case TCP_CHANNEL:
2.83      frystyk   242:            /*
2.91      frystyk   243:            **  The next state depends on whether we have a connection
                    244:            **  or not - if so then we can jump directly to connect() to
                    245:            **  test it - otherwise we must around DNS to get the name
2.93      eric      246:            **  Resolved
2.83      frystyk   247:            */
                    248:            if ((net->channel = HTHost_channel(net->host)) != NULL) {
                    249:                net->sockfd = HTChannel_socket(net->channel);
2.91      frystyk   250:                HTChannel_upSemaphore(net->channel);
                    251:                net->tcpstate = TCP_CONNECTED;
                    252:            } else
                    253:                net->tcpstate = TCP_DNS;
                    254:            hostname = HTHost_name(HTNet_host(net));
2.55      frystyk   255:            break;
                    256: 
2.91      frystyk   257:        case TCP_DNS:
                    258:            if ((status = HTParseInet(net, hostname)) < 0) {
2.27      frystyk   259:                if (PROT_TRACE)
2.91      frystyk   260:                    HTTrace("HTDoConnect. Can't locate `%s\'\n", hostname);
                    261:                HTRequest_addError(request, ERR_FATAL, NO,
                    262:                                   HTERR_NO_REMOTE_HOST,
                    263:                                   (void *) hostname, strlen(hostname),
                    264:                                   "HTDoConnect");
2.55      frystyk   265:                net->tcpstate = TCP_ERROR;
2.27      frystyk   266:                break;
2.55      frystyk   267:            }
                    268:            if (!net->retry && status > 1)              /* If multiple homes */
                    269:                net->retry = status;
2.83      frystyk   270:            net->tcpstate = TCP_NEED_SOCKET;
2.55      frystyk   271:            break;
                    272: 
                    273:          case TCP_NEED_SOCKET:
2.93      eric      274:            if (_makeSocket(net) == -1)
2.27      frystyk   275:                break;
2.83      frystyk   276:            /* Create a channel for this socket */
                    277:            net->channel = HTChannel_new(net, YES);
                    278: 
2.27      frystyk   279:            /* If multi-homed host then start timer on connection */
2.55      frystyk   280:            if (net->retry) net->connecttime = time(NULL);
2.65      frystyk   281: 
                    282:            /* Progress */
                    283:            {
                    284:                HTAlertCallback *cbf = HTAlert_find(HT_PROG_CONNECT);
2.93      eric      285:                if (cbf) (*cbf)(net->request, HT_PROG_CONNECT, HT_MSG_NULL,
2.91      frystyk   286:                                NULL, hostname, NULL);
2.65      frystyk   287:            }
2.56      frystyk   288:            net->tcpstate = TCP_NEED_CONNECT;
2.55      frystyk   289:            break;
2.56      frystyk   290:          case TCP_NEED_CONNECT:
2.55      frystyk   291:            status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
                    292:                             sizeof(net->sock_addr));
                    293:            /*
                    294:             * According to the Sun man page for connect:
                    295:             *     EINPROGRESS         The socket is non-blocking and the  con-
                    296:             *                         nection cannot be completed immediately.
                    297:             *                         It is possible to select(2) for  comple-
                    298:             *                         tion  by  selecting the socket for writ-
                    299:             *                         ing.
                    300:             * According to the Motorola SVR4 man page for connect:
                    301:             *     EAGAIN              The socket is non-blocking and the  con-
                    302:             *                         nection cannot be completed immediately.
                    303:             *                         It is possible to select for  completion
                    304:             *                         by  selecting  the  socket  for writing.
                    305:             *                         However, this is only  possible  if  the
                    306:             *                         socket  STREAMS  module  is  the topmost
                    307:             *                         module on  the  protocol  stack  with  a
                    308:             *                         write  service  procedure.  This will be
                    309:             *                         the normal case.
                    310:             */
2.93      eric      311:            if (NETCALL_ERROR(status))
2.55      frystyk   312:            {
2.93      eric      313:                if (NETCALL_WOULDBLOCK(socerrno))
2.55      frystyk   314:                {
                    315:                    if (PROT_TRACE)
2.91      frystyk   316:                        HTTrace("HTDoConnect. WOULD BLOCK `%s'\n", hostname);
2.86      eric      317:                    HTEvent_register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55      frystyk   318:                                     net->cbf, net->priority);
                    319:                    return HT_WOULD_BLOCK;
                    320:                }
                    321:                if (socerrno == EISCONN) {
                    322:                    net->tcpstate = TCP_CONNECTED;
                    323:                    break;
                    324:                }
2.93      eric      325:                if (NETCALL_DEADSOCKET(socerrno))     /* We lost the socket */
2.58      frystyk   326:                {
2.55      frystyk   327:                    net->tcpstate = TCP_NEED_SOCKET;
                    328:                    break;
2.27      frystyk   329:                }
2.55      frystyk   330:                if (net->retry) {
                    331:                    net->connecttime -= time(NULL);
2.54      frystyk   332:                    /* Added EINVAL `invalid argument' as this is what I 
                    333:                       get back from a non-blocking connect where I should 
                    334:                       get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.94    ! frystyk   335: #if defined(__svr4__) || defined (_WINSOCKAPI_)
2.55      frystyk   336:                    if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
                    337:                        socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
                    338:                        socerrno==EHOSTDOWN)
                    339: #else
2.54      frystyk   340:                    if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
                    341:                        socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
                    342:                        socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35      roeber    343: #endif
2.54      frystyk   344:                        net->connecttime += TCP_DELAY;
                    345:                    else
                    346:                        net->connecttime += TCP_PENALTY;
2.55      frystyk   347:                    HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
                    348:                }
                    349:                net->tcpstate = TCP_ERROR;              
                    350:            } else
                    351:                net->tcpstate = TCP_CONNECTED;
                    352:            break;
                    353: 
                    354:          case TCP_CONNECTED:
2.86      eric      355:            HTEvent_unregister(net->sockfd, (SockOps) FD_CONNECT);
2.55      frystyk   356:            if (net->retry) {
                    357:                net->connecttime -= time(NULL);
2.54      frystyk   358:                HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27      frystyk   359:            }
2.55      frystyk   360:            net->retry = 0;
                    361:            net->tcpstate = TCP_BEGIN;
                    362:            return HT_OK;
                    363:            break;
                    364: 
2.56      frystyk   365:          case TCP_NEED_BIND:
                    366:          case TCP_NEED_LISTEN:
2.55      frystyk   367:          case TCP_ERROR:
2.89      frystyk   368:            if (PROT_TRACE)
                    369:                HTTrace("HTDoConnect. Connect failed %d\n", socerrno);
2.55      frystyk   370:            if (net->sockfd != INVSOC) {
2.86      eric      371:                HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.55      frystyk   372:                NETCLOSE(net->sockfd);
                    373:                net->sockfd = INVSOC;
2.83      frystyk   374:                if (HTHost_isPersistent(net->host)) {    /* Inherited socket */
2.91      frystyk   375:                    HTHost_clearChannel(net->host, HT_ERROR);
2.55      frystyk   376:                    net->tcpstate = TCP_NEED_SOCKET;
                    377:                    break;
                    378:                }
                    379:            }
                    380: 
                    381:            /* Do we have more homes to try? */
                    382:            if (--net->retry > 0) {
2.65      frystyk   383:                HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55      frystyk   384:                              "connect");
                    385:                net->tcpstate = TCP_DNS;
2.24      frystyk   386:                break;
                    387:            }
2.65      frystyk   388:            HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.91      frystyk   389:            HTDNS_delete(hostname);
2.54      frystyk   390:            net->retry = 0;
2.55      frystyk   391:            net->tcpstate = TCP_BEGIN;
                    392:            return HT_ERROR;
                    393:            break;
2.24      frystyk   394:        }
2.55      frystyk   395:     }
2.13      frystyk   396: }
                    397: 
2.56      frystyk   398: /*     HTDoAccept()
                    399: **     ------------
                    400: **     This function makes a non-blocking accept which will turn up as ready
                    401: **     read in the select.
                    402: **     Returns
                    403: **             HT_ERROR        Error has occured or interrupted
                    404: **             HT_OK           if connected
                    405: **             HT_WOULD_BLOCK  if operation would have blocked
2.13      frystyk   406: */
2.88      frystyk   407: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13      frystyk   408: {
                    409:     int status;
2.56      frystyk   410:     int size = sizeof(net->sock_addr);
2.88      frystyk   411:     HTRequest * request = HTNet_request(net);
                    412:     if (!request || net->sockfd==INVSOC) {
2.77      eric      413:        if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56      frystyk   414:        return HT_ERROR;
2.13      frystyk   415:     }
2.65      frystyk   416: 
                    417:     /* Progress report */
                    418:     {
                    419:        HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
                    420:        if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
                    421:     }
2.56      frystyk   422:     status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
2.93      eric      423:     if (NETCALL_ERROR(status))
2.23      duns      424:     {
2.93      eric      425:        if (NETCALL_WOULDBLOCK(socerrno))
2.56      frystyk   426:        {
                    427:            if (PROT_TRACE)
2.77      eric      428:                HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.86      eric      429:            HTEvent_register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56      frystyk   430:                             net->cbf, net->priority);
                    431:            return HT_WOULD_BLOCK;
                    432:        }
2.65      frystyk   433:        HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77      eric      434:        if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56      frystyk   435:        return HT_ERROR;
2.23      duns      436:     }
2.71      frystyk   437: 
2.88      frystyk   438:     if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
                    439: 
                    440:     /*
                    441:     ** If accepted is the same as the net obejct then reuse it, else create
                    442:     ** a new object and leave the original alone
                    443:     */
                    444:     if (*accepted == net)
                    445:        HTDoClose(net);
                    446:     else
                    447:        *accepted = HTNet_dup(net);
                    448:     (*accepted)->sockfd = status;      
2.83      frystyk   449: 
                    450:     /* Create a channel for the new socket */
2.88      frystyk   451:     (*accepted)->channel = HTChannel_new(*accepted, NO);
2.83      frystyk   452: 
2.56      frystyk   453:     return HT_OK;
                    454: }
                    455: 
                    456: 
                    457: /*     HTDoListen
                    458: **     ----------
                    459: **     Listens on the specified port. 0 means that we chose it here
                    460: **     If master==INVSOC then we listen on all local interfaces (using a 
                    461: **     wildcard). If !INVSOC then use this as the local interface
                    462: **     returns         HT_ERROR        Error has occured or interrupted
                    463: **                     HT_OK           if connected
                    464: */
2.67      frystyk   465: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56      frystyk   466: {
                    467:     int status;
                    468: 
                    469:     /* Jump into the state machine */
                    470:     while (1) {
                    471:        switch (net->tcpstate) {
                    472:          case TCP_BEGIN:
                    473:            {
                    474:                SockA *sin = &net->sock_addr;
                    475:                memset((void *) sin, '\0', sizeof(SockA));
                    476: #ifdef DECNET
                    477:                sin->sdn_family = AF_DECnet;
                    478:                sin->sdn_objnum = port;
2.36      frystyk   479: #else
2.56      frystyk   480:                sin->sin_family = AF_INET;
                    481:                if (master != INVSOC) {
                    482:                    int len = sizeof(SockA);
                    483:                    if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65      frystyk   484:                        HTRequest_addSystemError(net->request, ERR_FATAL,
                    485:                                                 socerrno, NO, "getsockname");
2.56      frystyk   486:                        net->tcpstate = TCP_ERROR;
                    487:                        break;
                    488:                    }
                    489:                } else
                    490:                    sin->sin_addr.s_addr = INADDR_ANY;
                    491:                sin->sin_port = htons(port);
                    492: #endif
                    493:            }
                    494:            if (PROT_TRACE)
2.83      frystyk   495:                HTTrace("Socket...... Listen on port %d\n", port);
2.56      frystyk   496:            net->tcpstate = TCP_NEED_SOCKET;
                    497:            break;
                    498: 
                    499:          case TCP_NEED_SOCKET:
2.93      eric      500:            if (_makeSocket(net) == -1)
                    501:                break;
2.56      frystyk   502:            net->tcpstate = TCP_NEED_BIND;
                    503:            break;
                    504: 
                    505:          case TCP_NEED_BIND:
                    506:            status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
                    507:                          sizeof(net->sock_addr));
2.93      eric      508:            if (NETCALL_ERROR(status))
2.56      frystyk   509:            {
                    510:                if (PROT_TRACE)
2.83      frystyk   511:                    HTTrace("Socket...... Bind failed %d\n", socerrno);
2.56      frystyk   512:                net->tcpstate = TCP_ERROR;              
                    513:            } else
                    514:                net->tcpstate = TCP_NEED_LISTEN;
                    515:            break;
                    516: 
                    517:          case TCP_NEED_LISTEN:
2.67      frystyk   518:            status = listen(net->sockfd, backlog);
2.93      eric      519:            if (NETCALL_ERROR(status))
2.56      frystyk   520:                net->tcpstate = TCP_ERROR;              
                    521:            else
                    522:                net->tcpstate = TCP_CONNECTED;
                    523:            break;
                    524: 
                    525:          case TCP_CONNECTED:
                    526:            net->tcpstate = TCP_BEGIN;
                    527:            if (PROT_TRACE)
2.83      frystyk   528:                HTTrace("Socket...... Bind and listen on port %d %s\n",
2.56      frystyk   529:                        (int) ntohs(net->sock_addr.sin_port),
                    530:                        HTInetString(&net->sock_addr));
                    531:            return HT_OK;
                    532:            break;
2.13      frystyk   533: 
2.91      frystyk   534:          case TCP_CHANNEL:
2.56      frystyk   535:          case TCP_NEED_CONNECT:
                    536:          case TCP_DNS:
                    537:          case TCP_ERROR:
2.83      frystyk   538:            if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62      frystyk   539:            HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56      frystyk   540:            net->tcpstate = TCP_BEGIN;
                    541:            return HT_ERROR;
                    542:            break;
                    543:        }
2.38      frystyk   544:     }
1.1       timbl     545: }
                    546: 
2.83      frystyk   547: /*     HTDoClose
                    548: **     ---------
                    549: **     Closes a file descriptor whatever means are available on the current
                    550: **     platform. If we have unix file descriptors then use this otherwise use
                    551: **     the ANSI C file descriptors
                    552: **
                    553: **     returns         HT_ERROR        Error has occured or interrupted
                    554: **                     HT_OK           if connected
                    555: **                     HT_WOULD_BLOCK  if operation would have blocked
                    556: */
2.87      frystyk   557: PUBLIC int HTDoClose (HTNet * net)
2.83      frystyk   558: {
                    559:     int status = -1;
                    560:     if (net && net->sockfd != INVSOC) {
2.91      frystyk   561:        if (PROT_TRACE) HTTrace("HTDoClose... Close %d\n", net->sockfd);
2.83      frystyk   562:        status = NETCLOSE(net->sockfd);
2.86      eric      563:        HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.91      frystyk   564:        HTNet_decreaseSocket();
2.83      frystyk   565:        net->sockfd = INVSOC;
2.91      frystyk   566:        
                    567:        /*
                    568:        **  As we have a socket available we check for whether
                    569:        **  we can start any pending requests. We do this by asking for
                    570:        **  pending Host objects. If none then use the current object
                    571:        */
                    572:        HTHost_launchPending(net->host);
                    573: 
                    574:     } else
                    575:        if (PROT_TRACE) HTTrace("HTDoClose... No pending requests\n");
2.83      frystyk   576:     return status < 0 ? HT_ERROR : HT_OK;
                    577: }
2.91      frystyk   578: 
2.83      frystyk   579: 

Webmaster