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