Annotation of libwww/Library/src/HTTCP.c, revision 2.108
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.108 ! frystyk 6: ** @(#) $Id: HTTCP.c,v 2.107 1998/03/20 17:53:06 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.95 frystyk 244: me->tcpstate = TCP_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.55 frystyk 358:
2.56 frystyk 359: case TCP_NEED_BIND:
360: case TCP_NEED_LISTEN:
2.55 frystyk 361: case TCP_ERROR:
2.104 frystyk 362: HTTrace("HTDoConnect. Connect failed %d\n", socerrno);
2.95 frystyk 363: if (HTChannel_socket(me->channel) != INVSOC) {
364: /* HTEvent_unregister(HTChannel_socket(me->channel), (SockOps) FD_ALL); */
365: NETCLOSE(HTChannel_socket(me->channel));
366: /* HTChannel_setSocket(me->channel, INVSOC); */
367: #if 1 /* @@@ */
368: if (HTHost_isPersistent(me)) { /* Inherited socket */
369: HTHost_setPersistent(me, NO, HT_TP_SINGLE);
370: me->tcpstate = TCP_NEED_SOCKET;
2.104 frystyk 371: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_NEED_SOCKET.\n", me);
2.55 frystyk 372: break;
373: }
2.95 frystyk 374: #endif
2.55 frystyk 375: }
376:
377: /* Do we have more homes to try? */
2.102 frystyk 378: HTHost_decreaseRetry(me);
379: if (HTHost_retry(me) > 0) {
2.65 frystyk 380: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 381: "connect");
2.95 frystyk 382: me->tcpstate = TCP_DNS;
2.104 frystyk 383: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_DNS.\n", me);
2.24 frystyk 384: break;
385: }
2.65 frystyk 386: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.91 frystyk 387: HTDNS_delete(hostname);
2.102 frystyk 388: HTHost_setRetry(me, 0);
2.95 frystyk 389: me->tcpstate = TCP_BEGIN;
2.104 frystyk 390: if (PROT_TRACE) HTTrace("HTHost %p going to state TCP_BEGIN.\n", me);
2.55 frystyk 391: return HT_ERROR;
392: break;
2.24 frystyk 393: }
2.55 frystyk 394: }
2.13 frystyk 395: }
396:
2.56 frystyk 397: /* HTDoAccept()
398: ** ------------
399: ** This function makes a non-blocking accept which will turn up as ready
400: ** read in the select.
401: ** Returns
402: ** HT_ERROR Error has occured or interrupted
403: ** HT_OK if connected
404: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 405: */
2.95 frystyk 406:
2.88 frystyk 407: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13 frystyk 408: {
409: int status;
2.95 frystyk 410: int size = sizeof(net->host->sock_addr);
2.88 frystyk 411: HTRequest * request = HTNet_request(net);
2.95 frystyk 412: if (!request || HTNet_socket(net)==INVSOC) {
2.77 eric 413: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 414: return HT_ERROR;
2.13 frystyk 415: }
2.65 frystyk 416:
417: /* Progress report */
418: {
419: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
420: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
421: }
2.95 frystyk 422: status = accept(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr, &size);
2.93 eric 423: if (NETCALL_ERROR(status))
2.23 duns 424: {
2.93 eric 425: if (NETCALL_WOULDBLOCK(socerrno))
2.56 frystyk 426: {
427: if (PROT_TRACE)
2.95 frystyk 428: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", HTNet_socket(net));
429: HTEvent_register(HTNet_socket(net), HTEvent_ACCEPT, &net->event);
2.56 frystyk 430: return HT_WOULD_BLOCK;
431: }
2.65 frystyk 432: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 433: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 434: return HT_ERROR;
2.23 duns 435: }
2.71 frystyk 436:
2.88 frystyk 437: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
438:
439: /*
440: ** If accepted is the same as the net obejct then reuse it, else create
441: ** a new object and leave the original alone
442: */
443: if (*accepted == net)
444: HTDoClose(net);
445: else
446: *accepted = HTNet_dup(net);
2.95 frystyk 447: HTNet_setSocket(*accepted, status);
2.83 frystyk 448:
449: /* Create a channel for the new socket */
2.95 frystyk 450: {
451: HTHost * host = (*accepted)->host;
2.103 frystyk 452: HTChannel * ch = HTChannel_new(HTNet_socket(*accepted), NULL, NO);
2.95 frystyk 453: HTHost_setChannel(host, ch);
454: }
2.56 frystyk 455: return HT_OK;
456: }
457:
458:
459: /* HTDoListen
460: ** ----------
461: ** Listens on the specified port. 0 means that we chose it here
462: ** If master==INVSOC then we listen on all local interfaces (using a
463: ** wildcard). If !INVSOC then use this as the local interface
464: ** returns HT_ERROR Error has occured or interrupted
465: ** HT_OK if connected
466: */
2.67 frystyk 467: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 468: {
469: int status;
470:
471: /* Jump into the state machine */
472: while (1) {
2.95 frystyk 473: switch (net->host->tcpstate) {
2.56 frystyk 474: case TCP_BEGIN:
475: {
2.95 frystyk 476: SockA *sin = &net->host->sock_addr;
2.56 frystyk 477: memset((void *) sin, '\0', sizeof(SockA));
478: #ifdef DECNET
479: sin->sdn_family = AF_DECnet;
480: sin->sdn_objnum = port;
2.36 frystyk 481: #else
2.56 frystyk 482: sin->sin_family = AF_INET;
483: if (master != INVSOC) {
484: int len = sizeof(SockA);
485: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 486: HTRequest_addSystemError(net->request, ERR_FATAL,
487: socerrno, NO, "getsockname");
2.95 frystyk 488: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 489: break;
490: }
491: } else
492: sin->sin_addr.s_addr = INADDR_ANY;
493: sin->sin_port = htons(port);
494: #endif
495: }
496: if (PROT_TRACE)
2.83 frystyk 497: HTTrace("Socket...... Listen on port %d\n", port);
2.95 frystyk 498: net->host->tcpstate = TCP_NEED_SOCKET;
2.56 frystyk 499: break;
500:
501: case TCP_NEED_SOCKET:
2.95 frystyk 502: if (_makeSocket(net->host, net->request, net->preemptive, net->transport) == -1)
2.93 eric 503: break;
2.95 frystyk 504: net->host->tcpstate = TCP_NEED_BIND;
2.56 frystyk 505: break;
506:
507: case TCP_NEED_BIND:
2.95 frystyk 508: status = bind(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr,
509: sizeof(net->host->sock_addr));
2.93 eric 510: if (NETCALL_ERROR(status))
2.56 frystyk 511: {
512: if (PROT_TRACE)
2.83 frystyk 513: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.95 frystyk 514: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 515: } else
2.95 frystyk 516: net->host->tcpstate = TCP_NEED_LISTEN;
2.56 frystyk 517: break;
518:
519: case TCP_NEED_LISTEN:
2.95 frystyk 520: status = listen(HTNet_socket(net), backlog);
2.93 eric 521: if (NETCALL_ERROR(status))
2.95 frystyk 522: net->host->tcpstate = TCP_ERROR;
2.56 frystyk 523: else
2.95 frystyk 524: net->host->tcpstate = TCP_CONNECTED;
2.56 frystyk 525: break;
526:
527: case TCP_CONNECTED:
2.95 frystyk 528: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 529: if (PROT_TRACE)
2.83 frystyk 530: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.95 frystyk 531: (int) ntohs(net->host->sock_addr.sin_port),
532: HTInetString(&net->host->sock_addr));
2.56 frystyk 533: return HT_OK;
534: break;
2.13 frystyk 535:
2.91 frystyk 536: case TCP_CHANNEL:
2.56 frystyk 537: case TCP_NEED_CONNECT:
538: case TCP_DNS:
539: case TCP_ERROR:
2.83 frystyk 540: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 541: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.95 frystyk 542: net->host->tcpstate = TCP_BEGIN;
2.56 frystyk 543: return HT_ERROR;
544: break;
545: }
2.38 frystyk 546: }
1.1 timbl 547: }
548:
2.83 frystyk 549: /* HTDoClose
550: ** ---------
551: ** Closes a file descriptor whatever means are available on the current
552: ** platform. If we have unix file descriptors then use this otherwise use
553: ** the ANSI C file descriptors
554: **
555: ** returns HT_ERROR Error has occured or interrupted
556: ** HT_OK if connected
557: ** HT_WOULD_BLOCK if operation would have blocked
558: */
2.87 frystyk 559: PUBLIC int HTDoClose (HTNet * net)
2.83 frystyk 560: {
561: int status = -1;
2.95 frystyk 562: if (net && HTNet_socket(net) != INVSOC) {
563: if (PROT_TRACE) HTTrace("HTDoClose... Close %d\n", HTNet_socket(net));
564: status = NETCLOSE(HTNet_socket(net));
565: /* HTEvent_unregister(HTNet_socket(net), (SockOps) FD_ALL); */
2.91 frystyk 566: HTNet_decreaseSocket();
2.95 frystyk 567: HTNet_setSocket(net, INVSOC);
2.91 frystyk 568:
569: /*
570: ** As we have a socket available we check for whether
571: ** we can start any pending requests. We do this by asking for
572: ** pending Host objects. If none then use the current object
573: */
574: HTHost_launchPending(net->host);
575:
576: } else
577: if (PROT_TRACE) HTTrace("HTDoClose... No pending requests\n");
2.83 frystyk 578: return status < 0 ? HT_ERROR : HT_OK;
579: }
2.91 frystyk 580:
2.83 frystyk 581:
Webmaster