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

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

Webmaster