Annotation of libwww/Library/src/HTTCP.c, revision 2.88
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.88 ! frystyk 6: ** @(#) $Id: HTTCP.c,v 2.87 1996/06/08 01:52: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.79 frystyk 20: #include "sysdep.h"
2.83 frystyk 21: #include "WWWUtil.h"
22: #include "WWWCore.h"
23:
2.58 frystyk 24: #include "HTReqMan.h"
2.54 frystyk 25: #include "HTNetMan.h"
2.36 frystyk 26: #include "HTTCP.h" /* Implemented here */
2.29 frystyk 27:
2.36 frystyk 28: /* VMS stuff */
29: #ifdef VMS
30: #ifndef MULTINET
31: #define FD_SETSIZE 32
32: #else /* Multinet */
33: #define FD_SETSIZE 256
34: #endif /* Multinet */
35: #endif /* VMS */
36:
2.13 frystyk 37: /* Macros and other defines */
2.24 frystyk 38: /* x seconds penalty on a multi-homed host if IP-address is down */
39: #define TCP_PENALTY 1200
40:
41: /* x seconds penalty on a multi-homed host if IP-address is timed out */
42: #define TCP_DELAY 600
43:
2.13 frystyk 44: /* ------------------------------------------------------------------------- */
2.19 frystyk 45: /* CONNECTION ESTABLISHMENT MANAGEMENT */
46: /* ------------------------------------------------------------------------- */
2.13 frystyk 47:
48: /* HTDoConnect()
49: **
50: ** Note: Any port indication in URL, e.g., as `host:port' overwrites
2.54 frystyk 51: ** the default port value.
2.13 frystyk 52: **
2.40 frystyk 53: ** returns HT_ERROR Error has occured or interrupted
54: ** HT_OK if connected
55: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 56: */
2.54 frystyk 57: PUBLIC int HTDoConnect (HTNet * net, char * url, u_short default_port)
2.13 frystyk 58: {
59: int status;
2.88 ! frystyk 60: HTRequest * request = HTNet_request(net);
2.55 frystyk 61: char *fullhost = HTParse(url, "", PARSE_HOST);
2.13 frystyk 62: char *at_sign;
63: char *host;
64:
2.54 frystyk 65: /* if there's an @ then use the stuff after it as a hostname */
2.55 frystyk 66: if ((at_sign = strchr(fullhost, '@')) != NULL)
2.13 frystyk 67: host = at_sign+1;
68: else
2.55 frystyk 69: host = fullhost;
2.24 frystyk 70: if (!*host) {
2.65 frystyk 71: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
2.24 frystyk 72: NULL, 0, "HTDoConnect");
2.76 frystyk 73: HT_FREE(fullhost);
2.40 frystyk 74: return HT_ERROR;
2.27 frystyk 75: }
2.13 frystyk 76:
2.55 frystyk 77: /* Jump into the state machine */
78: while (1) {
79: switch (net->tcpstate) {
80: case TCP_BEGIN:
2.83 frystyk 81: if (PROT_TRACE) HTTrace("HTDoConnect. Looking up `%s\'\n", host);
2.55 frystyk 82: {
83: char *port = strchr(host, ':');
84: SockA *sin = &net->sock_addr;
85: memset((void *) sin, '\0', sizeof(SockA));
2.84 frystyk 86: if (port) {
87: *port++ = '\0';
88: if (*port && isdigit(*port)) {
2.54 frystyk 89: #ifdef DECNET
2.84 frystyk 90: sin->sdn_family = AF_DECnet;
91: sin->sdn_objnum=(unsigned char)(strtol(port,(char**)0,10));
2.55 frystyk 92: #else
2.84 frystyk 93: sin->sin_family = AF_INET;
94: sin->sin_port = htons(atol(port));
2.54 frystyk 95: #endif
2.84 frystyk 96: }
2.55 frystyk 97: } else {
2.13 frystyk 98: #ifdef DECNET
2.55 frystyk 99: sin->sdn_family = AF_DECnet;
100: net->sock_addr.sdn_objnum = DNP_OBJ;
2.13 frystyk 101: #else /* Internet */
2.55 frystyk 102: sin->sin_family = AF_INET;
103: sin->sin_port = htons(default_port);
2.13 frystyk 104: #endif
2.55 frystyk 105: }
106: }
2.83 frystyk 107:
108: /* Find information about this host */
109: if ((net->host = HTHost_new(host)) == NULL) {
110: if (PROT_TRACE) HTTrace("HTDoConnect. Can't get host info\n");
111: net->tcpstate = TCP_ERROR;
112: break;
113: }
114:
115: /*
116: ** Check whether we have a cached channel for this host. If not
117: ** then go ahead and create a new socket and a new channel. If we
118: ** have to wait for a persistent connection the return. When we
119: ** get back, we check that the socket hasn't been closed in the
120: ** meantime
121: */
122: if ((net->channel = HTHost_channel(net->host)) != NULL) {
123: net->sockfd = HTChannel_socket(net->channel);
124: if (HTChannel_idle(net->channel)) {
2.85 eric 125: HTChannel_upSemaphore(net->channel);
2.83 frystyk 126: net->tcpstate = TCP_CONNECTED;
127: break;
128: } else {
129: if (PROT_TRACE) HTTrace("HTDoConnect. Waiting...\n");
130: net->tcpstate = TCP_NEED_CONNECT;
131: HT_FREE(fullhost);
132: HTNet_wait(net);
133: return HT_PERSISTENT;
134: }
135: }
2.55 frystyk 136: net->tcpstate = TCP_DNS;
137: break;
138:
139: case TCP_DNS:
140: if ((status = HTParseInet(net, host)) < 0) {
2.27 frystyk 141: if (PROT_TRACE)
2.77 eric 142: HTTrace("HTDoConnect. Can't locate `%s\'\n", host);
2.65 frystyk 143: HTRequest_addError(request, ERR_FATAL, NO,HTERR_NO_REMOTE_HOST,
2.27 frystyk 144: (void *) host, strlen(host), "HTDoConnect");
2.55 frystyk 145: net->tcpstate = TCP_ERROR;
2.27 frystyk 146: break;
2.55 frystyk 147: }
148: if (!net->retry && status > 1) /* If multiple homes */
149: net->retry = status;
2.83 frystyk 150: net->tcpstate = TCP_NEED_SOCKET;
2.55 frystyk 151: break;
152:
153: case TCP_NEED_SOCKET:
2.27 frystyk 154: #ifdef DECNET
2.36 frystyk 155: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
2.27 frystyk 156: #else
2.55 frystyk 157: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
2.27 frystyk 158: #endif
159: {
2.65 frystyk 160: HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, "socket");
2.55 frystyk 161: net->tcpstate = TCP_ERROR;
2.27 frystyk 162: break;
163: }
164: if (PROT_TRACE)
2.77 eric 165: HTTrace("HTDoConnect. Created socket %d\n",net->sockfd);
2.27 frystyk 166:
2.28 frystyk 167: /* If non-blocking protocol then change socket status
2.82 frystyk 168: ** I use fcntl() so that I can ask the status before I set it.
2.50 frystyk 169: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
170: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
171: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
172: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
173: */
2.74 frystyk 174: if (!net->preemptive) {
2.73 frystyk 175: #ifdef _WINSOCKAPI_
2.41 frystyk 176: { /* begin windows scope */
2.65 frystyk 177: HTRequest * rq = request;
2.41 frystyk 178: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
179: FD_CONNECT | FD_CLOSE ;
180: int rv = 0 ;
181:
2.64 frystyk 182: #ifdef WWW_WIN_ASYNC
2.41 frystyk 183: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.64 frystyk 184: rv = WSAAsyncSelect( net->sockfd, rq->hwnd,
185: rq->winMsg, levents);
186: if (rv == SOCKET_ERROR) {
187: status = -1 ;
188: if (PROT_TRACE)
2.77 eric 189: HTTrace("HTDoConnect. WSAAsyncSelect() fails: %d\n",
2.64 frystyk 190: WSAGetLastError());
191: } /* error returns */
192: #else
193: int enable = 1;
194: status = IOCTL(net->sockfd, FIONBIO, &enable);
195: #endif
2.41 frystyk 196: } /* end scope */
197: #else
198: #if defined(VMS)
2.36 frystyk 199: {
200: int enable = 1;
201: status = IOCTL(net->sockfd, FIONBIO, &enable);
202: }
203: #else
2.82 frystyk 204: if((status = fcntl(net->sockfd, F_GETFL, 0)) != -1) {
2.81 frystyk 205: #ifdef O_NONBLOCK
206: status |= O_NONBLOCK; /* POSIX */
207: #else
208: #ifdef F_NDELAY
209: status |= F_NDELAY; /* BSD */
210: #endif /* F_NDELAY */
211: #endif /* O_NONBLOCK */
2.82 frystyk 212: status = fcntl(net->sockfd, F_SETFL, status);
2.27 frystyk 213: }
2.41 frystyk 214: #endif /* VMS */
215: #endif /* WINDOW */
2.43 frystyk 216: if (PROT_TRACE) {
217: if (status == -1)
2.77 eric 218: HTTrace("HTDoConnect. Only blocking works\n");
2.43 frystyk 219: else
2.77 eric 220: HTTrace("HTDoConnect. Non-blocking socket\n");
2.43 frystyk 221: }
2.57 frystyk 222: } else if (PROT_TRACE)
2.77 eric 223: HTTrace("HTDoConnect. Blocking socket\n");
2.56 frystyk 224:
2.83 frystyk 225: /* Create a channel for this socket */
226: net->channel = HTChannel_new(net, YES);
227:
2.27 frystyk 228: /* If multi-homed host then start timer on connection */
2.55 frystyk 229: if (net->retry) net->connecttime = time(NULL);
2.65 frystyk 230:
231: /* Progress */
232: {
233: HTAlertCallback *cbf = HTAlert_find(HT_PROG_CONNECT);
234: if (cbf)
2.78 frystyk 235: (*cbf)(request,HT_PROG_CONNECT,HT_MSG_NULL,NULL,host,NULL);
2.65 frystyk 236: }
2.56 frystyk 237: net->tcpstate = TCP_NEED_CONNECT;
2.55 frystyk 238: break;
2.54 frystyk 239:
2.56 frystyk 240: case TCP_NEED_CONNECT:
2.55 frystyk 241: status = connect(net->sockfd, (struct sockaddr *) &net->sock_addr,
242: sizeof(net->sock_addr));
243: /*
244: * According to the Sun man page for connect:
245: * EINPROGRESS The socket is non-blocking and the con-
246: * nection cannot be completed immediately.
247: * It is possible to select(2) for comple-
248: * tion by selecting the socket for writ-
249: * ing.
250: * According to the Motorola SVR4 man page for connect:
251: * EAGAIN The socket is non-blocking and the con-
252: * nection cannot be completed immediately.
253: * It is possible to select for completion
254: * by selecting the socket for writing.
255: * However, this is only possible if the
256: * socket STREAMS module is the topmost
257: * module on the protocol stack with a
258: * write service procedure. This will be
259: * the normal case.
260: */
261: #ifdef _WINSOCKAPI_
262: if (status == SOCKET_ERROR)
263: #else
264: if (status < 0)
265: #endif
266: {
2.27 frystyk 267: #ifdef EAGAIN
2.55 frystyk 268: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
2.41 frystyk 269: #else
2.55 frystyk 270: #ifdef _WINSOCKAPI_
271: if (socerrno==WSAEWOULDBLOCK)
2.40 frystyk 272: #else
2.55 frystyk 273: if (socerrno==EINPROGRESS)
274: #endif /* _WINSOCKAPI_ */
2.27 frystyk 275: #endif /* EAGAIN */
2.55 frystyk 276: {
277: if (PROT_TRACE)
2.77 eric 278: HTTrace("HTDoConnect. WOULD BLOCK `%s'\n",host);
2.86 eric 279: HTEvent_register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55 frystyk 280: net->cbf, net->priority);
2.76 frystyk 281: HT_FREE(fullhost);
2.55 frystyk 282: return HT_WOULD_BLOCK;
283: }
284: if (socerrno == EISCONN) {
285: net->tcpstate = TCP_CONNECTED;
286: break;
287: }
2.58 frystyk 288: #ifdef _WINSOCKAPI_
2.59 frystyk 289: if (socerrno == WSAEBADF) /* We lost the socket */
2.58 frystyk 290: #else
291: if (socerrno == EBADF) /* We lost the socket */
292: #endif
293: {
2.55 frystyk 294: net->tcpstate = TCP_NEED_SOCKET;
295: break;
2.27 frystyk 296: }
2.55 frystyk 297: if (net->retry) {
298: net->connecttime -= time(NULL);
2.54 frystyk 299: /* Added EINVAL `invalid argument' as this is what I
300: get back from a non-blocking connect where I should
301: get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.58 frystyk 302: #if defined(__srv4__) || defined (_WINSOCKAPI_)
2.55 frystyk 303: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
304: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
305: socerrno==EHOSTDOWN)
306: #else
2.54 frystyk 307: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
308: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
309: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 310: #endif
2.54 frystyk 311: net->connecttime += TCP_DELAY;
312: else
313: net->connecttime += TCP_PENALTY;
2.55 frystyk 314: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
315: }
316: net->tcpstate = TCP_ERROR;
317: } else
318: net->tcpstate = TCP_CONNECTED;
319: break;
320:
321: case TCP_CONNECTED:
2.86 eric 322: HTEvent_unregister(net->sockfd, (SockOps) FD_CONNECT);
2.55 frystyk 323: if (net->retry) {
324: net->connecttime -= time(NULL);
2.54 frystyk 325: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27 frystyk 326: }
2.55 frystyk 327: net->retry = 0;
2.82 frystyk 328: HT_FREE(fullhost);
2.55 frystyk 329: net->tcpstate = TCP_BEGIN;
330: return HT_OK;
331: break;
332:
2.56 frystyk 333: case TCP_NEED_BIND:
334: case TCP_NEED_LISTEN:
2.55 frystyk 335: case TCP_ERROR:
2.77 eric 336: if (PROT_TRACE) HTTrace("HTDoConnect. Connect failed\n");
2.55 frystyk 337: if (net->sockfd != INVSOC) {
2.86 eric 338: HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.55 frystyk 339: NETCLOSE(net->sockfd);
340: net->sockfd = INVSOC;
2.83 frystyk 341: if (HTHost_isPersistent(net->host)) { /* Inherited socket */
342: HTHost_clearChannel(net->host);
2.55 frystyk 343: net->tcpstate = TCP_NEED_SOCKET;
344: break;
345: }
346: }
347:
348: /* Do we have more homes to try? */
349: if (--net->retry > 0) {
2.65 frystyk 350: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 351: "connect");
352: net->tcpstate = TCP_DNS;
2.24 frystyk 353: break;
354: }
2.65 frystyk 355: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.55 frystyk 356: HTDNS_delete(host);
2.54 frystyk 357: net->retry = 0;
2.76 frystyk 358: HT_FREE(fullhost);
2.55 frystyk 359: net->tcpstate = TCP_BEGIN;
360: return HT_ERROR;
361: break;
2.24 frystyk 362: }
2.55 frystyk 363: }
2.13 frystyk 364: }
365:
2.56 frystyk 366: /* HTDoAccept()
367: ** ------------
368: ** This function makes a non-blocking accept which will turn up as ready
369: ** read in the select.
370: ** Returns
371: ** HT_ERROR Error has occured or interrupted
372: ** HT_OK if connected
373: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 374: */
2.88 ! frystyk 375: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13 frystyk 376: {
377: int status;
2.56 frystyk 378: int size = sizeof(net->sock_addr);
2.88 ! frystyk 379: HTRequest * request = HTNet_request(net);
! 380: if (!request || net->sockfd==INVSOC) {
2.77 eric 381: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 382: return HT_ERROR;
2.13 frystyk 383: }
2.65 frystyk 384:
385: /* Progress report */
386: {
387: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
388: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
389: }
2.56 frystyk 390: status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
391: #ifdef _WINSOCKAPI_
392: if (status == SOCKET_ERROR)
393: #else
394: if (status < 0)
395: #endif
2.23 duns 396: {
2.56 frystyk 397: #ifdef EAGAIN
398: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
399: #else
400: #ifdef _WINSOCKAPI_
401: if (socerrno==WSAEWOULDBLOCK)
402: #else
403: if (socerrno==EINPROGRESS)
404: #endif /* _WINSOCKAPI_ */
405: #endif /* EAGAIN */
406: {
407: if (PROT_TRACE)
2.77 eric 408: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.86 eric 409: HTEvent_register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56 frystyk 410: net->cbf, net->priority);
411: return HT_WOULD_BLOCK;
412: }
2.65 frystyk 413: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 414: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 415: return HT_ERROR;
2.23 duns 416: }
2.71 frystyk 417:
2.88 ! frystyk 418: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
! 419:
! 420: /*
! 421: ** If accepted is the same as the net obejct then reuse it, else create
! 422: ** a new object and leave the original alone
! 423: */
! 424: if (*accepted == net)
! 425: HTDoClose(net);
! 426: else
! 427: *accepted = HTNet_dup(net);
! 428: (*accepted)->sockfd = status;
2.83 frystyk 429:
430: /* Create a channel for the new socket */
2.88 ! frystyk 431: (*accepted)->channel = HTChannel_new(*accepted, NO);
2.83 frystyk 432:
2.56 frystyk 433: return HT_OK;
434: }
435:
436:
437: /* HTDoListen
438: ** ----------
439: ** Listens on the specified port. 0 means that we chose it here
440: ** If master==INVSOC then we listen on all local interfaces (using a
441: ** wildcard). If !INVSOC then use this as the local interface
442: ** returns HT_ERROR Error has occured or interrupted
443: ** HT_OK if connected
444: */
2.67 frystyk 445: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 446: {
447: int status;
448:
449: /* Jump into the state machine */
450: while (1) {
451: switch (net->tcpstate) {
452: case TCP_BEGIN:
453: {
454: SockA *sin = &net->sock_addr;
455: memset((void *) sin, '\0', sizeof(SockA));
456: #ifdef DECNET
457: sin->sdn_family = AF_DECnet;
458: sin->sdn_objnum = port;
2.36 frystyk 459: #else
2.56 frystyk 460: sin->sin_family = AF_INET;
461: if (master != INVSOC) {
462: int len = sizeof(SockA);
463: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 464: HTRequest_addSystemError(net->request, ERR_FATAL,
465: socerrno, NO, "getsockname");
2.56 frystyk 466: net->tcpstate = TCP_ERROR;
467: break;
468: }
469: } else
470: sin->sin_addr.s_addr = INADDR_ANY;
471: sin->sin_port = htons(port);
472: #endif
473: }
474: if (PROT_TRACE)
2.83 frystyk 475: HTTrace("Socket...... Listen on port %d\n", port);
2.56 frystyk 476: net->tcpstate = TCP_NEED_SOCKET;
477: break;
478:
479: case TCP_NEED_SOCKET:
480: #ifdef DECNET
481: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
482: #else
483: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
484: #endif
485: {
2.65 frystyk 486: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
487: NO, "socket");
2.56 frystyk 488: net->tcpstate = TCP_ERROR;
489: break;
490: }
491: if (PROT_TRACE)
2.83 frystyk 492: HTTrace("Socket...... Created %d\n", net->sockfd);
2.56 frystyk 493:
494: /* If non-blocking protocol then change socket status
2.82 frystyk 495: ** I use fcntl() so that I can ask the status before I set it.
2.56 frystyk 496: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
497: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
498: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
499: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
500: */
2.74 frystyk 501: if (!net->preemptive) {
2.73 frystyk 502: #ifdef _WINSOCKAPI_
2.56 frystyk 503: { /* begin windows scope */
504: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
505: FD_CONNECT | FD_CLOSE ;
506: int rv = 0 ;
507:
2.64 frystyk 508: #ifdef WWW_WIN_ASYNC
2.56 frystyk 509: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 510: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 511: net->request->winMsg, levents);
2.64 frystyk 512: if (rv == SOCKET_ERROR) {
513: status = -1 ;
514: if (PROT_TRACE)
2.83 frystyk 515: HTTrace("Socket...... WSAAsyncSelect() fails: %d\n",
2.64 frystyk 516: WSAGetLastError());
2.56 frystyk 517: } /* error returns */
2.64 frystyk 518: #else
519: int enable = 1 ;
520: status = IOCTL(net->sockfd, FIONBIO, &enable);
521: #endif
2.56 frystyk 522: } /* end scope */
523: #else
524: #if defined(VMS)
525: {
526: int enable = 1;
527: status = IOCTL(net->sockfd, FIONBIO, &enable);
528: }
529: #else
2.82 frystyk 530: if((status = fcntl(net->sockfd, F_GETFL, 0)) != -1) {
2.81 frystyk 531: #ifdef O_NONBLOCK
2.56 frystyk 532: status |= O_NONBLOCK; /* POSIX */
2.81 frystyk 533: #else
534: #ifdef F_NDELAY
535: status |= F_NDELAY; /* BSD */
536: #endif /* F_NDELAY */
537: #endif /* O_NONBLOCK */
2.82 frystyk 538: status = fcntl(net->sockfd, F_SETFL, status);
2.56 frystyk 539: }
540: #endif /* VMS */
541: #endif /* WINDOW */
542: if (PROT_TRACE) {
543: if (status == -1)
2.83 frystyk 544: HTTrace("Sockrt...... Blocking socket\n");
2.56 frystyk 545: else
2.83 frystyk 546: HTTrace("Socket...... Non-blocking socket\n");
2.56 frystyk 547: }
548: }
549: net->tcpstate = TCP_NEED_BIND;
550: break;
551:
552: case TCP_NEED_BIND:
553: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
554: sizeof(net->sock_addr));
555: #ifdef _WINSOCKAPI_
556: if (status == SOCKET_ERROR)
557: #else
558: if (status < 0)
559: #endif
560: {
561: if (PROT_TRACE)
2.83 frystyk 562: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.56 frystyk 563: net->tcpstate = TCP_ERROR;
564: } else
565: net->tcpstate = TCP_NEED_LISTEN;
566: break;
567:
568: case TCP_NEED_LISTEN:
2.67 frystyk 569: status = listen(net->sockfd, backlog);
2.56 frystyk 570: #ifdef _WINSOCKAPI_
571: if (status == SOCKET_ERROR)
572: #else
573: if (status < 0)
2.36 frystyk 574: #endif
2.56 frystyk 575: net->tcpstate = TCP_ERROR;
576: else
577: net->tcpstate = TCP_CONNECTED;
578: break;
579:
580: case TCP_CONNECTED:
581: net->tcpstate = TCP_BEGIN;
582: if (PROT_TRACE)
2.83 frystyk 583: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.56 frystyk 584: (int) ntohs(net->sock_addr.sin_port),
585: HTInetString(&net->sock_addr));
586: return HT_OK;
587: break;
2.13 frystyk 588:
2.56 frystyk 589: case TCP_NEED_CONNECT:
590: case TCP_DNS:
591: case TCP_ERROR:
2.83 frystyk 592: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 593: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 594: net->tcpstate = TCP_BEGIN;
595: return HT_ERROR;
596: break;
597: }
2.38 frystyk 598: }
1.1 timbl 599: }
600:
2.83 frystyk 601: /* HTDoClose
602: ** ---------
603: ** Closes a file descriptor whatever means are available on the current
604: ** platform. If we have unix file descriptors then use this otherwise use
605: ** the ANSI C file descriptors
606: **
607: ** returns HT_ERROR Error has occured or interrupted
608: ** HT_OK if connected
609: ** HT_WOULD_BLOCK if operation would have blocked
610: */
2.87 frystyk 611: PUBLIC int HTDoClose (HTNet * net)
2.83 frystyk 612: {
613: int status = -1;
614: if (net && net->sockfd != INVSOC) {
615: if (PROT_TRACE) HTTrace("Socket...... Close %d\n", net->sockfd);
616: status = NETCLOSE(net->sockfd);
2.86 eric 617: HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.83 frystyk 618: net->sockfd = INVSOC;
619: }
620: return status < 0 ? HT_ERROR : HT_OK;
621: }
622:
Webmaster