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

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

Webmaster