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

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

Webmaster