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