Annotation of libwww/Library/src/HTTCP.c, revision 2.89
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.89 ! frystyk 6: ** @(#) $Id: HTTCP.c,v 2.88 1996/07/02 22:55:07 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.89 ! frystyk 267: #ifdef EALREADY
! 268: if (socerrno==EINPROGRESS || socerrno==EALREADY)
! 269: #else
2.27 frystyk 270: #ifdef EAGAIN
2.55 frystyk 271: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
2.89 ! frystyk 272: #else
2.55 frystyk 273: #ifdef _WINSOCKAPI_
274: if (socerrno==WSAEWOULDBLOCK)
2.40 frystyk 275: #else
2.55 frystyk 276: if (socerrno==EINPROGRESS)
277: #endif /* _WINSOCKAPI_ */
2.27 frystyk 278: #endif /* EAGAIN */
2.89 ! frystyk 279: #endif /* EALREADY */
2.55 frystyk 280: {
281: if (PROT_TRACE)
2.77 eric 282: HTTrace("HTDoConnect. WOULD BLOCK `%s'\n",host);
2.86 eric 283: HTEvent_register(net->sockfd, request, (SockOps)FD_CONNECT,
2.55 frystyk 284: net->cbf, net->priority);
2.76 frystyk 285: HT_FREE(fullhost);
2.55 frystyk 286: return HT_WOULD_BLOCK;
287: }
288: if (socerrno == EISCONN) {
289: net->tcpstate = TCP_CONNECTED;
290: break;
291: }
2.58 frystyk 292: #ifdef _WINSOCKAPI_
2.59 frystyk 293: if (socerrno == WSAEBADF) /* We lost the socket */
2.58 frystyk 294: #else
295: if (socerrno == EBADF) /* We lost the socket */
296: #endif
297: {
2.55 frystyk 298: net->tcpstate = TCP_NEED_SOCKET;
299: break;
2.27 frystyk 300: }
2.55 frystyk 301: if (net->retry) {
302: net->connecttime -= time(NULL);
2.54 frystyk 303: /* Added EINVAL `invalid argument' as this is what I
304: get back from a non-blocking connect where I should
305: get `connection refused' on BSD. SVR4 gives SIG_PIPE */
2.58 frystyk 306: #if defined(__srv4__) || defined (_WINSOCKAPI_)
2.55 frystyk 307: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
308: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
309: socerrno==EHOSTDOWN)
310: #else
2.54 frystyk 311: if (socerrno==ECONNREFUSED || socerrno==ETIMEDOUT ||
312: socerrno==ENETUNREACH || socerrno==EHOSTUNREACH ||
313: socerrno==EHOSTDOWN || socerrno==EINVAL)
2.35 roeber 314: #endif
2.54 frystyk 315: net->connecttime += TCP_DELAY;
316: else
317: net->connecttime += TCP_PENALTY;
2.55 frystyk 318: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
319: }
320: net->tcpstate = TCP_ERROR;
321: } else
322: net->tcpstate = TCP_CONNECTED;
323: break;
324:
325: case TCP_CONNECTED:
2.86 eric 326: HTEvent_unregister(net->sockfd, (SockOps) FD_CONNECT);
2.55 frystyk 327: if (net->retry) {
328: net->connecttime -= time(NULL);
2.54 frystyk 329: HTDNS_updateWeigths(net->dns, net->home, net->connecttime);
2.27 frystyk 330: }
2.55 frystyk 331: net->retry = 0;
2.82 frystyk 332: HT_FREE(fullhost);
2.55 frystyk 333: net->tcpstate = TCP_BEGIN;
334: return HT_OK;
335: break;
336:
2.56 frystyk 337: case TCP_NEED_BIND:
338: case TCP_NEED_LISTEN:
2.55 frystyk 339: case TCP_ERROR:
2.89 ! frystyk 340: if (PROT_TRACE)
! 341: HTTrace("HTDoConnect. Connect failed %d\n", socerrno);
2.55 frystyk 342: if (net->sockfd != INVSOC) {
2.86 eric 343: HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.55 frystyk 344: NETCLOSE(net->sockfd);
345: net->sockfd = INVSOC;
2.83 frystyk 346: if (HTHost_isPersistent(net->host)) { /* Inherited socket */
347: HTHost_clearChannel(net->host);
2.55 frystyk 348: net->tcpstate = TCP_NEED_SOCKET;
349: break;
350: }
351: }
352:
353: /* Do we have more homes to try? */
354: if (--net->retry > 0) {
2.65 frystyk 355: HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,
2.55 frystyk 356: "connect");
357: net->tcpstate = TCP_DNS;
2.24 frystyk 358: break;
359: }
2.65 frystyk 360: HTRequest_addSystemError(request, ERR_FATAL,socerrno,NO,"connect");
2.55 frystyk 361: HTDNS_delete(host);
2.54 frystyk 362: net->retry = 0;
2.76 frystyk 363: HT_FREE(fullhost);
2.55 frystyk 364: net->tcpstate = TCP_BEGIN;
365: return HT_ERROR;
366: break;
2.24 frystyk 367: }
2.55 frystyk 368: }
2.13 frystyk 369: }
370:
2.56 frystyk 371: /* HTDoAccept()
372: ** ------------
373: ** This function makes a non-blocking accept which will turn up as ready
374: ** read in the select.
375: ** Returns
376: ** HT_ERROR Error has occured or interrupted
377: ** HT_OK if connected
378: ** HT_WOULD_BLOCK if operation would have blocked
2.13 frystyk 379: */
2.88 frystyk 380: PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted)
2.13 frystyk 381: {
382: int status;
2.56 frystyk 383: int size = sizeof(net->sock_addr);
2.88 frystyk 384: HTRequest * request = HTNet_request(net);
385: if (!request || net->sockfd==INVSOC) {
2.77 eric 386: if (PROT_TRACE) HTTrace("HTDoAccept.. Invalid socket\n");
2.56 frystyk 387: return HT_ERROR;
2.13 frystyk 388: }
2.65 frystyk 389:
390: /* Progress report */
391: {
392: HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT);
393: if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL);
394: }
2.56 frystyk 395: status = accept(net->sockfd, (struct sockaddr *) &net->sock_addr, &size);
396: #ifdef _WINSOCKAPI_
397: if (status == SOCKET_ERROR)
398: #else
399: if (status < 0)
400: #endif
2.23 duns 401: {
2.89 ! frystyk 402: #ifdef EALREADY
! 403: if (socerrno==EINPROGRESS || socerrno==EALREADY)
! 404: #else
2.56 frystyk 405: #ifdef EAGAIN
2.89 ! frystyk 406: if (socerrno==EINPROGRESS || socerrno==EAGAIN)
! 407: #else
2.56 frystyk 408: #ifdef _WINSOCKAPI_
2.89 ! frystyk 409: if (socerrno==WSAEWOULDBLOCK)
2.56 frystyk 410: #else
2.89 ! frystyk 411: if (socerrno==EINPROGRESS)
2.56 frystyk 412: #endif /* _WINSOCKAPI_ */
413: #endif /* EAGAIN */
2.89 ! frystyk 414: #endif /* EALREADY */
2.56 frystyk 415: {
416: if (PROT_TRACE)
2.77 eric 417: HTTrace("HTDoAccept.. WOULD BLOCK %d\n", net->sockfd);
2.86 eric 418: HTEvent_register(net->sockfd, request, (SockOps) FD_ACCEPT,
2.56 frystyk 419: net->cbf, net->priority);
420: return HT_WOULD_BLOCK;
421: }
2.65 frystyk 422: HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept");
2.77 eric 423: if (PROT_TRACE) HTTrace("HTDoAccept.. Accept failed\n");
2.56 frystyk 424: return HT_ERROR;
2.23 duns 425: }
2.71 frystyk 426:
2.88 frystyk 427: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
428:
429: /*
430: ** If accepted is the same as the net obejct then reuse it, else create
431: ** a new object and leave the original alone
432: */
433: if (*accepted == net)
434: HTDoClose(net);
435: else
436: *accepted = HTNet_dup(net);
437: (*accepted)->sockfd = status;
2.83 frystyk 438:
439: /* Create a channel for the new socket */
2.88 frystyk 440: (*accepted)->channel = HTChannel_new(*accepted, NO);
2.83 frystyk 441:
2.56 frystyk 442: return HT_OK;
443: }
444:
445:
446: /* HTDoListen
447: ** ----------
448: ** Listens on the specified port. 0 means that we chose it here
449: ** If master==INVSOC then we listen on all local interfaces (using a
450: ** wildcard). If !INVSOC then use this as the local interface
451: ** returns HT_ERROR Error has occured or interrupted
452: ** HT_OK if connected
453: */
2.67 frystyk 454: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 455: {
456: int status;
457:
458: /* Jump into the state machine */
459: while (1) {
460: switch (net->tcpstate) {
461: case TCP_BEGIN:
462: {
463: SockA *sin = &net->sock_addr;
464: memset((void *) sin, '\0', sizeof(SockA));
465: #ifdef DECNET
466: sin->sdn_family = AF_DECnet;
467: sin->sdn_objnum = port;
2.36 frystyk 468: #else
2.56 frystyk 469: sin->sin_family = AF_INET;
470: if (master != INVSOC) {
471: int len = sizeof(SockA);
472: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 473: HTRequest_addSystemError(net->request, ERR_FATAL,
474: socerrno, NO, "getsockname");
2.56 frystyk 475: net->tcpstate = TCP_ERROR;
476: break;
477: }
478: } else
479: sin->sin_addr.s_addr = INADDR_ANY;
480: sin->sin_port = htons(port);
481: #endif
482: }
483: if (PROT_TRACE)
2.83 frystyk 484: HTTrace("Socket...... Listen on port %d\n", port);
2.56 frystyk 485: net->tcpstate = TCP_NEED_SOCKET;
486: break;
487:
488: case TCP_NEED_SOCKET:
489: #ifdef DECNET
490: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
491: #else
492: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
493: #endif
494: {
2.65 frystyk 495: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
496: NO, "socket");
2.56 frystyk 497: net->tcpstate = TCP_ERROR;
498: break;
499: }
500: if (PROT_TRACE)
2.83 frystyk 501: HTTrace("Socket...... Created %d\n", net->sockfd);
2.56 frystyk 502:
503: /* If non-blocking protocol then change socket status
2.82 frystyk 504: ** I use fcntl() so that I can ask the status before I set it.
2.56 frystyk 505: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
506: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
507: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
508: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
509: */
2.74 frystyk 510: if (!net->preemptive) {
2.73 frystyk 511: #ifdef _WINSOCKAPI_
2.56 frystyk 512: { /* begin windows scope */
513: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
514: FD_CONNECT | FD_CLOSE ;
515: int rv = 0 ;
516:
2.64 frystyk 517: #ifdef WWW_WIN_ASYNC
2.56 frystyk 518: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 519: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 520: net->request->winMsg, levents);
2.64 frystyk 521: if (rv == SOCKET_ERROR) {
522: status = -1 ;
523: if (PROT_TRACE)
2.83 frystyk 524: HTTrace("Socket...... WSAAsyncSelect() fails: %d\n",
2.64 frystyk 525: WSAGetLastError());
2.56 frystyk 526: } /* error returns */
2.64 frystyk 527: #else
528: int enable = 1 ;
529: status = IOCTL(net->sockfd, FIONBIO, &enable);
530: #endif
2.56 frystyk 531: } /* end scope */
532: #else
533: #if defined(VMS)
534: {
535: int enable = 1;
536: status = IOCTL(net->sockfd, FIONBIO, &enable);
537: }
538: #else
2.82 frystyk 539: if((status = fcntl(net->sockfd, F_GETFL, 0)) != -1) {
2.81 frystyk 540: #ifdef O_NONBLOCK
2.56 frystyk 541: status |= O_NONBLOCK; /* POSIX */
2.81 frystyk 542: #else
543: #ifdef F_NDELAY
544: status |= F_NDELAY; /* BSD */
545: #endif /* F_NDELAY */
546: #endif /* O_NONBLOCK */
2.82 frystyk 547: status = fcntl(net->sockfd, F_SETFL, status);
2.56 frystyk 548: }
549: #endif /* VMS */
550: #endif /* WINDOW */
551: if (PROT_TRACE) {
552: if (status == -1)
2.83 frystyk 553: HTTrace("Sockrt...... Blocking socket\n");
2.56 frystyk 554: else
2.83 frystyk 555: HTTrace("Socket...... Non-blocking socket\n");
2.56 frystyk 556: }
557: }
558: net->tcpstate = TCP_NEED_BIND;
559: break;
560:
561: case TCP_NEED_BIND:
562: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
563: sizeof(net->sock_addr));
564: #ifdef _WINSOCKAPI_
565: if (status == SOCKET_ERROR)
566: #else
567: if (status < 0)
568: #endif
569: {
570: if (PROT_TRACE)
2.83 frystyk 571: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.56 frystyk 572: net->tcpstate = TCP_ERROR;
573: } else
574: net->tcpstate = TCP_NEED_LISTEN;
575: break;
576:
577: case TCP_NEED_LISTEN:
2.67 frystyk 578: status = listen(net->sockfd, backlog);
2.56 frystyk 579: #ifdef _WINSOCKAPI_
580: if (status == SOCKET_ERROR)
581: #else
582: if (status < 0)
2.36 frystyk 583: #endif
2.56 frystyk 584: net->tcpstate = TCP_ERROR;
585: else
586: net->tcpstate = TCP_CONNECTED;
587: break;
588:
589: case TCP_CONNECTED:
590: net->tcpstate = TCP_BEGIN;
591: if (PROT_TRACE)
2.83 frystyk 592: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.56 frystyk 593: (int) ntohs(net->sock_addr.sin_port),
594: HTInetString(&net->sock_addr));
595: return HT_OK;
596: break;
2.13 frystyk 597:
2.56 frystyk 598: case TCP_NEED_CONNECT:
599: case TCP_DNS:
600: case TCP_ERROR:
2.83 frystyk 601: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 602: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 603: net->tcpstate = TCP_BEGIN;
604: return HT_ERROR;
605: break;
606: }
2.38 frystyk 607: }
1.1 timbl 608: }
609:
2.83 frystyk 610: /* HTDoClose
611: ** ---------
612: ** Closes a file descriptor whatever means are available on the current
613: ** platform. If we have unix file descriptors then use this otherwise use
614: ** the ANSI C file descriptors
615: **
616: ** returns HT_ERROR Error has occured or interrupted
617: ** HT_OK if connected
618: ** HT_WOULD_BLOCK if operation would have blocked
619: */
2.87 frystyk 620: PUBLIC int HTDoClose (HTNet * net)
2.83 frystyk 621: {
622: int status = -1;
623: if (net && net->sockfd != INVSOC) {
624: if (PROT_TRACE) HTTrace("Socket...... Close %d\n", net->sockfd);
625: status = NETCLOSE(net->sockfd);
2.86 eric 626: HTEvent_unregister(net->sockfd, (SockOps) FD_ALL);
2.83 frystyk 627: net->sockfd = INVSOC;
628: }
629: return status < 0 ? HT_ERROR : HT_OK;
630: }
631:
Webmaster