Annotation of libwww/Library/src/HTTCP.c, revision 2.100
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.100 ! eric 6: ** @(#) $Id: HTTCP.c,v 2.99 1996/12/07 19:14:29 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: }
96: if (PROT_TRACE) HTTrace(HTHIDE("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: */
105: #ifdef HT_NO_NAGLE
106: {
107: int disable = 1;
108: status = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &disable, sizeof(int));
109: if (status == -1) {
110: if (PROT_TRACE)
111: HTTrace("Socket...... Could not disable Nagle's algorithm - error %d\n",
112: sockfd);
113: } else {
114: if (PROT_TRACE) HTTrace("Socket...... Turned off Nagle's algorithm\n");
115: }
116: }
117: #endif
2.93 eric 118:
2.95 frystyk 119: /* If non-blocking protocol then change socket status
120: ** I use fcntl() so that I can ask the status before I set it.
121: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
122: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
123: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
124: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
125: */
126: if (!preemptive) {
2.93 eric 127: #ifdef _WINSOCKAPI_
2.95 frystyk 128: { /* begin windows scope */
129: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
130: FD_CONNECT | FD_CLOSE ;
131: int rv = 0 ;
132: u_long one = 1;
133:
134: status = ioctlsocket(sockfd, FIONBIO, &one) ==
135: SOCKET_ERROR ? -1 : 0;
136: } /* end scope */
2.93 eric 137: #else /* _WINSOCKAPI_ */
138: #if defined(VMS)
2.95 frystyk 139: {
140: int enable = 1;
141: status = IOCTL(sockfd, FIONBIO, &enable);
142: }
2.93 eric 143: #else /* VMS */
2.95 frystyk 144: if((status = fcntl(sockfd, F_GETFL, 0)) != -1) {
2.93 eric 145: #ifdef O_NONBLOCK
2.95 frystyk 146: status |= O_NONBLOCK; /* POSIX */
2.93 eric 147: #else /* O_NONBLOCK */
148: #ifdef F_NDELAY
149: status |= F_NDELAY; /* BSD */
150: #endif /* F_NDELAY */
151: #endif /* !O_NONBLOCK */
2.95 frystyk 152: status = fcntl(sockfd, F_SETFL, status);
153: }
2.93 eric 154: #endif /* !VMS */
155: #endif /* !_WINSOCKAPI_ */
2.95 frystyk 156: if (PROT_TRACE) HTTrace(HTHIDE("Socket...... %slocking socket\n"), status == -1 ? "B" : "Non-b");
157: } else
158: if (PROT_TRACE) HTTrace(HTHIDE("Socket...... Blocking socket\n"));
159:
160: /*
161: ** Associate the channel with the host and create an input and and output stream
162: ** for this host/channel
163: */
164: HTHost_setChannel(host, HTChannel_new(sockfd, YES));
165: HTHost_getInput(host, transport, NULL, 0);
166: HTHost_getOutput(host, transport, NULL, 1024);
2.93 eric 167:
168: return status == -1 ? 1 : 0;
169: }
170:
2.13 frystyk 171: /* HTDoConnect()
172: **
173: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
2.54 frystyk 174: ** the default port value.
2.13 frystyk 175: **
2.40 frystyk 176: ** returns HT_ERROR Error has occured or interrupted
177: ** HT_OK if connected
178: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 179: */
2.54 frystyk 180: PUBLIC int HTDoConnect (HTNet * net, char * url, u_short default_port)
2.13 frystyk 181: {
2.95 frystyk 182: HTHost * me = HTNet_host(net);
2.88 frystyk 183: HTRequest * request = HTNet_request(net);
2.95 frystyk 184: int preemptive = net->preemptive;
185: int retry = net->retry;
186:
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;
219: if (PROT_TRACE) HTTrace(HTHIDE("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;
233: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_DNS.\n"), me);
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;
255: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_CONNECTED.\n"), me);
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) {
262: if (PROT_TRACE) HTTrace(HTHIDE("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;
268: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_ERROR.\n"), me);
2.27 frystyk 269: break;
2.55 frystyk 270: }
2.95 frystyk 271: if (!retry && status > 1) /* If multiple homes */
272: retry = status;
273: me->tcpstate = TCP_NEED_SOCKET;
274: if (PROT_TRACE) HTTrace(HTHIDE("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.95 frystyk 294: if (retry) me->connecttime = time(NULL);
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;
303: if (PROT_TRACE) HTTrace(HTHIDE("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.95 frystyk 330: if (PROT_TRACE) HTTrace(HTHIDE("HTDoConnect. WOULD BLOCK `%s'\n"), hostname);
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;
336: if (PROT_TRACE) HTTrace(HTHIDE("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;
342: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_NEED_SOCKET.\n"), me);
2.55 frystyk 343: break;
2.27 frystyk 344: }
2.95 frystyk 345: if (retry) {
346: me->connecttime -= time(NULL);
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;
362: HTDNS_updateWeigths(me->dns, me->home, me->connecttime);
2.55 frystyk 363: }
2.95 frystyk 364: me->tcpstate = TCP_ERROR;
365: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_ERROR.\n"), me);
366: } else {
367: me->tcpstate = TCP_CONNECTED;
368: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_CONNECTED.\n"), me);
369: }
2.55 frystyk 370: break;
371:
372: case TCP_CONNECTED:
2.95 frystyk 373: HTHost_unregister(me, net, HTEvent_CONNECT);
374: if (retry) {
375: me->connecttime -= time(NULL);
376: HTDNS_updateWeigths(me->dns, me->home, me->connecttime);
2.27 frystyk 377: }
2.95 frystyk 378: retry = 0;
2.100 ! eric 379: me->tcpstate = TCP_IN_USE;
2.95 frystyk 380: if (PROT_TRACE) HTTrace(HTHIDE("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.95 frystyk 397: HTTrace(HTHIDE("HTDoConnect. Connect failed %d\n"), socerrno);
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;
406: if (PROT_TRACE) HTTrace(HTHIDE("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.95 frystyk 413: if (--retry > 0) {
2.65 frystyk 414: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 415: "connect");
2.95 frystyk 416: me->tcpstate = TCP_DNS;
417: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_DNS.\n"), me);
2.24 frystyk 418: break;
419: }
2.65 frystyk 420: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.91 frystyk 421: HTDNS_delete(hostname);
2.95 frystyk 422: retry = 0;
423: me->tcpstate = TCP_BEGIN;
424: if (PROT_TRACE) HTTrace(HTHIDE("HTHost %p going to state TCP_BEGIN.\n"), me);
2.55 frystyk 425: return HT_ERROR;
426: break;
2.24 frystyk 427: }
2.55 frystyk 428: }
2.13 frystyk 429: }
430:
2.56 frystyk 431: /* HTDoAccept()
432: ** ------------
433: ** This function makes a non-blocking accept which will turn up as ready
434: ** read in the select.
435: ** Returns
436: ** HT_ERROR Error has occured or interrupted
437: ** HT_OK if connected
438: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 439: */
2.95 frystyk 440:
2.88 frystyk 441: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13 frystyk 442: {
443: int status;
2.95 frystyk 444: int size = sizeof(net->host->sock_addr);
2.88 frystyk 445: HTRequest * request = HTNet_request(net);
2.95 frystyk 446: if (!request || HTNet_socket(net)==INVSOC) {
2.77 eric 447: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 448: return HT_ERROR;
2.13 frystyk 449: }
2.65 frystyk 450:
451: /* Progress report */
452: {
453: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
454: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
455: }
2.95 frystyk 456: status = accept(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr, &size);
2.93 eric 457: if (NETCALL_ERROR(status))
2.23 duns 458: {
2.93 eric 459: if (NETCALL_WOULDBLOCK(socerrno))
2.56 frystyk 460: {
461: if (PROT_TRACE)
2.95 frystyk 462: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", HTNet_socket(net));
463: HTEvent_register(HTNet_socket(net), HTEvent_ACCEPT, &net->event);
2.56 frystyk 464: return HT_WOULD_BLOCK;
465: }
2.65 frystyk 466: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 467: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 468: return HT_ERROR;
2.23 duns 469: }
2.71 frystyk 470:
2.88 frystyk 471: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
472:
473: /*
474: ** If accepted is the same as the net obejct then reuse it, else create
475: ** a new object and leave the original alone
476: */
477: if (*accepted == net)
478: HTDoClose(net);
479: else
480: *accepted = HTNet_dup(net);
2.95 frystyk 481: HTNet_setSocket(*accepted, status);
2.83 frystyk 482:
483: /* Create a channel for the new socket */
2.95 frystyk 484: {
485: HTHost * host = (*accepted)->host;
486: HTChannel * ch = HTChannel_new(HTNet_socket(*accepted), NO);
487: #ifdef HT_MUX
488: HTMuxChannel_new(host);
489: #endif /* HT_MUX */
490: HTHost_setChannel(host, ch);
491: }
2.56 frystyk 492: return HT_OK;
493: }
494:
495:
496: /* HTDoListen
497: ** ----------
498: ** Listens on the specified port. 0 means that we chose it here
499: ** If master==INVSOC then we listen on all local interfaces (using a
500: ** wildcard). If !INVSOC then use this as the local interface
501: ** returns HT_ERROR Error has occured or interrupted
502: ** HT_OK if connected
503: */
2.67 frystyk 504: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 505: {
506: int status;
507:
508: /* Jump into the state machine */
509: while (1) {
2.95 frystyk 510: switch (net->host->tcpstate) {
2.56 frystyk 511: case TCP_BEGIN:
512: {
2.95 frystyk 513: SockA *sin = &net->host->sock_addr;
2.56 frystyk 514: memset((void *) sin, '\0', sizeof(SockA));
515: #ifdef DECNET
516: sin->sdn_family = AF_DECnet;
517: sin->sdn_objnum = port;
2.36 frystyk 518: #else
2.56 frystyk 519: sin->sin_family = AF_INET;
520: if (master != INVSOC) {
521: int len = sizeof(SockA);
522: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 523: HTRequest_addSystemError(net->request, ERR_FATAL,
524: socerrno, NO, "getsockname");
2.95 frystyk 525: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 526: break;
527: }
528: } else
529: sin->sin_addr.s_addr = INADDR_ANY;
530: sin->sin_port = htons(port);
531: #endif
532: }
533: if (PROT_TRACE)
2.83 frystyk 534: HTTrace("Socket...... Listen on port %d\n", port);
2.95 frystyk 535: net->host->tcpstate = TCP_NEED_SOCKET;
2.56 frystyk 536: break;
537:
538: case TCP_NEED_SOCKET:
2.95 frystyk 539: if (_makeSocket(net->host, net->request, net->preemptive, net->transport) == -1)
2.93 eric 540: break;
2.95 frystyk 541: net->host->tcpstate = TCP_NEED_BIND;
2.56 frystyk 542: break;
543:
544: case TCP_NEED_BIND:
2.95 frystyk 545: status = bind(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr,
546: sizeof(net->host->sock_addr));
2.93 eric 547: if (NETCALL_ERROR(status))
2.56 frystyk 548: {
549: if (PROT_TRACE)
2.83 frystyk 550: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.95 frystyk 551: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 552: } else
2.95 frystyk 553: net->host->tcpstate = TCP_NEED_LISTEN;
2.56 frystyk 554: break;
555:
556: case TCP_NEED_LISTEN:
2.95 frystyk 557: status = listen(HTNet_socket(net), backlog);
2.93 eric 558: if (NETCALL_ERROR(status))
2.95 frystyk 559: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 560: else
2.95 frystyk 561: net->host->tcpstate = TCP_CONNECTED;
2.56 frystyk 562: break;
563:
564: case TCP_CONNECTED:
2.95 frystyk 565: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 566: if (PROT_TRACE)
2.83 frystyk 567: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.95 frystyk 568: (int) ntohs(net->host->sock_addr.sin_port),
569: HTInetString(&net->host->sock_addr));
2.56 frystyk 570: return HT_OK;
571: break;
2.13 frystyk 572:
2.91 frystyk 573: case TCP_CHANNEL:
2.56 frystyk 574: case TCP_NEED_CONNECT:
575: case TCP_DNS:
576: case TCP_ERROR:
2.83 frystyk 577: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 578: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.95 frystyk 579: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 580: return HT_ERROR;
581: break;
582: }
2.38 frystyk 583: }
1.1 timbl 584: }
585:
2.83 frystyk 586: /* HTDoClose
587: ** ---------
588: ** Closes a file descriptor whatever means are available on the current
589: ** platform. If we have unix file descriptors then use this otherwise use
590: ** the ANSI C file descriptors
591: **
592: ** returns HT_ERROR Error has occured or interrupted
593: ** HT_OK if connected
594: ** HT_WOULD_BLOCK if operation would have blocked
595: */
2.87 frystyk 596: PUBLIC int HTDoClose (HTNet * net)
2.83 frystyk 597: {
598: int status = -1;
2.95 frystyk 599: if (net && HTNet_socket(net) != INVSOC) {
600: if (PROT_TRACE) HTTrace("HTDoClose... Close %d\n", HTNet_socket(net));
601: status = NETCLOSE(HTNet_socket(net));
602: /* HTEvent_unregister(HTNet_socket(net), (SockOps) FD_ALL); */
2.91 frystyk 603: HTNet_decreaseSocket();
2.95 frystyk 604: HTNet_setSocket(net, INVSOC);
2.91 frystyk 605:
606: /*
607: ** As we have a socket available we check for whether
608: ** we can start any pending requests. We do this by asking for
609: ** pending Host objects. If none then use the current object
610: */
611: HTHost_launchPending(net->host);
612:
613: } else
614: if (PROT_TRACE) HTTrace("HTDoClose... No pending requests\n");
2.83 frystyk 615: return status < 0 ? HT_ERROR : HT_OK;
616: }
2.91 frystyk 617:
2.83 frystyk 618:
Webmaster