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