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

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

Webmaster