Annotation of libwww/Library/src/HTTCP.c, revision 2.106
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.106 ! frystyk 6: ** @(#) $Id: HTTCP.c,v 2.105 1997/12/16 21:09:28 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.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 */
2.100 eric 189: if (!me) {
190: char * proxy = HTRequest_proxy(request);
191: HTProtocol * protocol = HTNet_protocol(net);
192:
193: /* Check to see whether we connect directly or via a proxy */
194: if ((me = HTHost_newWParse(request, proxy ? proxy : url,
195: HTProtocol_id(protocol))) == NULL)
196: return NO;
197:
198: net->host = me;
199: }
2.55 frystyk 200: while (1) {
2.95 frystyk 201: switch (me->tcpstate) {
2.55 frystyk 202: case TCP_BEGIN:
2.91 frystyk 203: {
204: /*
205: ** Add the net object to the host object found above. If the
206: ** host is idle then we can start the request right away,
207: ** otherwise we must wait until it is free.
208: */
2.98 frystyk 209: if ((status = HTHost_addNet(net->host, net)) == HT_PENDING)
210: if (PROT_TRACE) HTTrace("HTDoConnect. Pending...\n");
2.91 frystyk 211:
212: /*
2.95 frystyk 213: ** If we are pending then return here, otherwise go to next state
2.91 frystyk 214: ** which is setting up a channel
215: */
2.95 frystyk 216: me->tcpstate = TCP_CHANNEL;
2.104 frystyk 217: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_CHANNEL.\n", me);
2.91 frystyk 218: if (status == HT_PENDING) return HT_PENDING;
219: }
220: break;
2.83 frystyk 221:
2.91 frystyk 222: case TCP_CHANNEL:
2.83 frystyk 223: /*
2.91 frystyk 224: ** The next state depends on whether we have a connection
225: ** or not - if so then we can jump directly to connect() to
226: ** test it - otherwise we must around DNS to get the name
2.93 eric 227: ** Resolved
2.83 frystyk 228: */
2.95 frystyk 229: if (HTHost_channel(me) == NULL) {
230: me->tcpstate = TCP_DNS;
2.104 frystyk 231: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_DNS.\n", me);
2.95 frystyk 232: } else {
233:
234: /*
235: ** There is now one more using the channel
236: */
237: HTChannel_upSemaphore(me->channel);
238:
239: /*
240: ** We are now all set and can jump to connected mode
241: */
242: me->tcpstate = TCP_CONNECTED;
2.104 frystyk 243: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_CONNECTED.\n", me);
2.95 frystyk 244: }
245: hostname = HTHost_name(me);
2.55 frystyk 246: break;
247:
2.91 frystyk 248: case TCP_DNS:
2.95 frystyk 249: if ((status = HTParseInet(me, hostname, request)) < 0) {
2.104 frystyk 250: if (PROT_TRACE) HTTrace("HTDoConnect. Can't locate `%s\'\n", hostname);
2.91 frystyk 251: HTRequest_addError(request, ERR_FATAL, NO,
252: HTERR_NO_REMOTE_HOST,
253: (void *) hostname, strlen(hostname),
254: "HTDoConnect");
2.95 frystyk 255: me->tcpstate = TCP_ERROR;
2.104 frystyk 256: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_ERROR.\n", me);
2.27 frystyk 257: break;
2.55 frystyk 258: }
2.102 frystyk 259: if (!HTHost_retry(me) && status > 1) /* If multiple homes */
260: HTHost_setRetry(me, status);
2.95 frystyk 261: me->tcpstate = TCP_NEED_SOCKET;
2.104 frystyk 262: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_NEED_SOCKET.\n", me);
2.55 frystyk 263: break;
264:
265: case TCP_NEED_SOCKET:
2.95 frystyk 266: if (_makeSocket(me, request, preemptive, net->transport) == -1)
2.27 frystyk 267: break;
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)) {
322: me->connecttime -= HTGetTimeInMillis();
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.94 frystyk 326: #if defined(__svr4__) || defined (_WINSOCKAPI_)
2.55 frystyk 327: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
328: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
329: socerrno==EHOSTDOWN)
330: #else
2.54 frystyk 331: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
332: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
333: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 334: #endif
2.95 frystyk 335: me->connecttime += TCP_DELAY;
2.54 frystyk 336: else
2.95 frystyk 337: me->connecttime += TCP_PENALTY;
2.102 frystyk 338: HTDNS_updateWeigths(me->dns, HTHost_home(me), me->connecttime);
2.55 frystyk 339: }
2.95 frystyk 340: me->tcpstate = TCP_ERROR;
2.104 frystyk 341: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_ERROR.\n", me);
2.95 frystyk 342: } else {
343: me->tcpstate = TCP_CONNECTED;
2.104 frystyk 344: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_CONNECTED.\n", me);
2.95 frystyk 345: }
2.55 frystyk 346: break;
347:
348: case TCP_CONNECTED:
2.95 frystyk 349: HTHost_unregister(me, net, HTEvent_CONNECT);
2.102 frystyk 350: if (HTHost_retry(me)) {
351: me->connecttime -= HTGetTimeInMillis();
352: HTDNS_updateWeigths(me->dns, HTHost_home(me), me->connecttime);
2.27 frystyk 353: }
2.102 frystyk 354: HTHost_setRetry(me, 0);
2.100 eric 355: me->tcpstate = TCP_IN_USE;
2.104 frystyk 356: if (PROT_TRACE) HTTrace("HTHost %p connected.\n", me);
2.55 frystyk 357: return HT_OK;
358: break;
2.100 eric 359:
360: /* once a host is connected, subsequent connections are immediately OK */
361: case TCP_IN_USE:
362: if ((status = HTHost_addNet(net->host, net)) == HT_PENDING) {
363: if (PROT_TRACE) HTTrace("HTDoConnect. Pending...\n");
364: return HT_PENDING;
365: }
366:
367: HTChannel_upSemaphore(me->channel);
368: return HT_OK;
2.55 frystyk 369:
2.56 frystyk 370: case TCP_NEED_BIND:
371: case TCP_NEED_LISTEN:
2.55 frystyk 372: case TCP_ERROR:
2.104 frystyk 373: HTTrace("HTDoConnect. Connect failed %d\n", socerrno);
2.95 frystyk 374: if (HTChannel_socket(me->channel) != INVSOC) {
375: /* HTEvent_unregister(HTChannel_socket(me->channel), (SockOps) FD_ALL); */
376: NETCLOSE(HTChannel_socket(me->channel));
377: /* HTChannel_setSocket(me->channel, INVSOC); */
378: #if 1 /* @@@ */
379: if (HTHost_isPersistent(me)) { /* Inherited socket */
380: HTHost_setPersistent(me, NO, HT_TP_SINGLE);
381: me->tcpstate = TCP_NEED_SOCKET;
2.104 frystyk 382: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_NEED_SOCKET.\n", me);
2.55 frystyk 383: break;
384: }
2.95 frystyk 385: #endif
2.55 frystyk 386: }
387:
388: /* Do we have more homes to try? */
2.102 frystyk 389: HTHost_decreaseRetry(me);
390: if (HTHost_retry(me) > 0) {
2.65 frystyk 391: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 392: "connect");
2.95 frystyk 393: me->tcpstate = TCP_DNS;
2.104 frystyk 394: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_DNS.\n", me);
2.24 frystyk 395: break;
396: }
2.65 frystyk 397: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.91 frystyk 398: HTDNS_delete(hostname);
2.102 frystyk 399: HTHost_setRetry(me, 0);
2.95 frystyk 400: me->tcpstate = TCP_BEGIN;
2.104 frystyk 401: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_BEGIN.\n", me);
2.55 frystyk 402: return HT_ERROR;
403: break;
2.24 frystyk 404: }
2.55 frystyk 405: }
2.13 frystyk 406: }
407:
2.56 frystyk 408: /* HTDoAccept()
409: ** ------------
410: ** This function makes a non-blocking accept which will turn up as ready
411: ** read in the select.
412: ** Returns
413: ** HT_ERROR Error has occured or interrupted
414: ** HT_OK if connected
415: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 416: */
2.95 frystyk 417:
2.88 frystyk 418: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13 frystyk 419: {
420: int status;
2.95 frystyk 421: int size = sizeof(net->host->sock_addr);
2.88 frystyk 422: HTRequest * request = HTNet_request(net);
2.95 frystyk 423: if (!request || HTNet_socket(net)==INVSOC) {
2.77 eric 424: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 425: return HT_ERROR;
2.13 frystyk 426: }
2.65 frystyk 427:
428: /* Progress report */
429: {
430: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
431: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
432: }
2.95 frystyk 433: status = accept(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr, &size);
2.93 eric 434: if (NETCALL_ERROR(status))
2.23 duns 435: {
2.93 eric 436: if (NETCALL_WOULDBLOCK(socerrno))
2.56 frystyk 437: {
438: if (PROT_TRACE)
2.95 frystyk 439: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", HTNet_socket(net));
440: HTEvent_register(HTNet_socket(net), HTEvent_ACCEPT, &net->event);
2.56 frystyk 441: return HT_WOULD_BLOCK;
442: }
2.65 frystyk 443: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 444: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 445: return HT_ERROR;
2.23 duns 446: }
2.71 frystyk 447:
2.88 frystyk 448: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
449:
450: /*
451: ** If accepted is the same as the net obejct then reuse it, else create
452: ** a new object and leave the original alone
453: */
454: if (*accepted == net)
455: HTDoClose(net);
456: else
457: *accepted = HTNet_dup(net);
2.95 frystyk 458: HTNet_setSocket(*accepted, status);
2.83 frystyk 459:
460: /* Create a channel for the new socket */
2.95 frystyk 461: {
462: HTHost * host = (*accepted)->host;
2.103 frystyk 463: HTChannel * ch = HTChannel_new(HTNet_socket(*accepted), NULL, NO);
2.95 frystyk 464: HTHost_setChannel(host, ch);
465: }
2.56 frystyk 466: return HT_OK;
467: }
468:
469:
470: /* HTDoListen
471: ** ----------
472: ** Listens on the specified port. 0 means that we chose it here
473: ** If master==INVSOC then we listen on all local interfaces (using a
474: ** wildcard). If !INVSOC then use this as the local interface
475: ** returns HT_ERROR Error has occured or interrupted
476: ** HT_OK if connected
477: */
2.67 frystyk 478: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 479: {
480: int status;
481:
482: /* Jump into the state machine */
483: while (1) {
2.95 frystyk 484: switch (net->host->tcpstate) {
2.56 frystyk 485: case TCP_BEGIN:
486: {
2.95 frystyk 487: SockA *sin = &net->host->sock_addr;
2.56 frystyk 488: memset((void *) sin, '\0', sizeof(SockA));
489: #ifdef DECNET
490: sin->sdn_family = AF_DECnet;
491: sin->sdn_objnum = port;
2.36 frystyk 492: #else
2.56 frystyk 493: sin->sin_family = AF_INET;
494: if (master != INVSOC) {
495: int len = sizeof(SockA);
496: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 497: HTRequest_addSystemError(net->request, ERR_FATAL,
498: socerrno, NO, "getsockname");
2.95 frystyk 499: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 500: break;
501: }
502: } else
503: sin->sin_addr.s_addr = INADDR_ANY;
504: sin->sin_port = htons(port);
505: #endif
506: }
507: if (PROT_TRACE)
2.83 frystyk 508: HTTrace("Socket...... Listen on port %d\n", port);
2.95 frystyk 509: net->host->tcpstate = TCP_NEED_SOCKET;
2.56 frystyk 510: break;
511:
512: case TCP_NEED_SOCKET:
2.95 frystyk 513: if (_makeSocket(net->host, net->request, net->preemptive, net->transport) == -1)
2.93 eric 514: break;
2.95 frystyk 515: net->host->tcpstate = TCP_NEED_BIND;
2.56 frystyk 516: break;
517:
518: case TCP_NEED_BIND:
2.95 frystyk 519: status = bind(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr,
520: sizeof(net->host->sock_addr));
2.93 eric 521: if (NETCALL_ERROR(status))
2.56 frystyk 522: {
523: if (PROT_TRACE)
2.83 frystyk 524: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.95 frystyk 525: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 526: } else
2.95 frystyk 527: net->host->tcpstate = TCP_NEED_LISTEN;
2.56 frystyk 528: break;
529:
530: case TCP_NEED_LISTEN:
2.95 frystyk 531: status = listen(HTNet_socket(net), backlog);
2.93 eric 532: if (NETCALL_ERROR(status))
2.95 frystyk 533: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 534: else
2.95 frystyk 535: net->host->tcpstate = TCP_CONNECTED;
2.56 frystyk 536: break;
537:
538: case TCP_CONNECTED:
2.95 frystyk 539: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 540: if (PROT_TRACE)
2.83 frystyk 541: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.95 frystyk 542: (int) ntohs(net->host->sock_addr.sin_port),
543: HTInetString(&net->host->sock_addr));
2.56 frystyk 544: return HT_OK;
545: break;
2.13 frystyk 546:
2.91 frystyk 547: case TCP_CHANNEL:
2.56 frystyk 548: case TCP_NEED_CONNECT:
549: case TCP_DNS:
550: case TCP_ERROR:
2.83 frystyk 551: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 552: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.95 frystyk 553: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 554: return HT_ERROR;
555: break;
556: }
2.38 frystyk 557: }
1.1 timbl 558: }
559:
2.83 frystyk 560: /* HTDoClose
561: ** ---------
562: ** Closes a file descriptor whatever means are available on the current
563: ** platform. If we have unix file descriptors then use this otherwise use
564: ** the ANSI C file descriptors
565: **
566: ** returns HT_ERROR Error has occured or interrupted
567: ** HT_OK if connected
568: ** HT_WOULD_BLOCK if operation would have blocked
569: */
2.87 frystyk 570: PUBLIC int HTDoClose (HTNet * net)
2.83 frystyk 571: {
572: int status = -1;
2.95 frystyk 573: if (net && HTNet_socket(net) != INVSOC) {
574: if (PROT_TRACE) HTTrace("HTDoClose... Close %d\n", HTNet_socket(net));
575: status = NETCLOSE(HTNet_socket(net));
576: /* HTEvent_unregister(HTNet_socket(net), (SockOps) FD_ALL); */
2.91 frystyk 577: HTNet_decreaseSocket();
2.95 frystyk 578: HTNet_setSocket(net, INVSOC);
2.91 frystyk 579:
580: /*
581: ** As we have a socket available we check for whether
582: ** we can start any pending requests. We do this by asking for
583: ** pending Host objects. If none then use the current object
584: */
585: HTHost_launchPending(net->host);
586:
587: } else
588: if (PROT_TRACE) HTTrace("HTDoClose... No pending requests\n");
2.83 frystyk 589: return status < 0 ? HT_ERROR : HT_OK;
590: }
2.91 frystyk 591:
2.83 frystyk 592:
Webmaster