Annotation of libwww/Library/src/HTTCP.c, revision 2.85
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.85 ! eric 6: ** @(#) $Id: HTTCP.c,v 2.84 1996/04/14 01:23:23 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.65 frystyk 60: HTRequest * request = net->request;
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.65 frystyk 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:
322: HTEvent_UnRegister(net->sockfd, (SockOps) FD_CONNECT);
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.53 frystyk 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.71 frystyk 375: PUBLIC int HTDoAccept (HTNet * net)
2.13 frystyk 376: {
377: int status;
2.56 frystyk 378: int size = sizeof(net->sock_addr);
2.65 frystyk 379: HTRequest *request = net->request;
2.36 frystyk 380: if (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.65 frystyk 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:
418: /* Swap to new socket */
419: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ACCEPT);
420: net->sockfd = status;
2.83 frystyk 421:
422: /* Create a channel for the new socket */
423: net->channel = HTChannel_new(net, NO);
424:
2.77 eric 425: if (PROT_TRACE) HTTrace("Accepted.... socket %d\n", status);
2.56 frystyk 426: return HT_OK;
427: }
428:
429:
430: /* HTDoListen
431: ** ----------
432: ** Listens on the specified port. 0 means that we chose it here
433: ** If master==INVSOC then we listen on all local interfaces (using a
434: ** wildcard). If !INVSOC then use this as the local interface
435: ** returns HT_ERROR Error has occured or interrupted
436: ** HT_OK if connected
437: */
2.67 frystyk 438: PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog)
2.56 frystyk 439: {
440: int status;
441:
442: /* Jump into the state machine */
443: while (1) {
444: switch (net->tcpstate) {
445: case TCP_BEGIN:
446: {
447: SockA *sin = &net->sock_addr;
448: memset((void *) sin, '\0', sizeof(SockA));
449: #ifdef DECNET
450: sin->sdn_family = AF_DECnet;
451: sin->sdn_objnum = port;
2.36 frystyk 452: #else
2.56 frystyk 453: sin->sin_family = AF_INET;
454: if (master != INVSOC) {
455: int len = sizeof(SockA);
456: if (getsockname(master, (struct sockaddr *) sin, &len)<0) {
2.65 frystyk 457: HTRequest_addSystemError(net->request, ERR_FATAL,
458: socerrno, NO, "getsockname");
2.56 frystyk 459: net->tcpstate = TCP_ERROR;
460: break;
461: }
462: } else
463: sin->sin_addr.s_addr = INADDR_ANY;
464: sin->sin_port = htons(port);
465: #endif
466: }
467: if (PROT_TRACE)
2.83 frystyk 468: HTTrace("Socket...... Listen on port %d\n", port);
2.56 frystyk 469: net->tcpstate = TCP_NEED_SOCKET;
470: break;
471:
472: case TCP_NEED_SOCKET:
473: #ifdef DECNET
474: if ((net->sockfd=socket(AF_DECnet, SOCK_STREAM, 0))==INVSOC)
475: #else
476: if ((net->sockfd=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP))==INVSOC)
477: #endif
478: {
2.65 frystyk 479: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno,
480: NO, "socket");
2.56 frystyk 481: net->tcpstate = TCP_ERROR;
482: break;
483: }
484: if (PROT_TRACE)
2.83 frystyk 485: HTTrace("Socket...... Created %d\n", net->sockfd);
2.56 frystyk 486:
487: /* If non-blocking protocol then change socket status
2.82 frystyk 488: ** I use fcntl() so that I can ask the status before I set it.
2.56 frystyk 489: ** See W. Richard Stevens (Advan. Prog. in UNIX environment, p.364)
490: ** Be CAREFULL with the old `O_NDELAY' - it will not work as read()
491: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD and
492: ** does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
493: */
2.74 frystyk 494: if (!net->preemptive) {
2.73 frystyk 495: #ifdef _WINSOCKAPI_
2.56 frystyk 496: { /* begin windows scope */
497: long levents = FD_READ | FD_WRITE | FD_ACCEPT |
498: FD_CONNECT | FD_CLOSE ;
499: int rv = 0 ;
500:
2.64 frystyk 501: #ifdef WWW_WIN_ASYNC
2.56 frystyk 502: /* N.B WSAAsyncSelect() turns on non-blocking I/O */
2.69 frystyk 503: rv = WSAAsyncSelect(net->sockfd, net->request->hwnd,
2.66 frystyk 504: net->request->winMsg, levents);
2.64 frystyk 505: if (rv == SOCKET_ERROR) {
506: status = -1 ;
507: if (PROT_TRACE)
2.83 frystyk 508: HTTrace("Socket...... WSAAsyncSelect() fails: %d\n",
2.64 frystyk 509: WSAGetLastError());
2.56 frystyk 510: } /* error returns */
2.64 frystyk 511: #else
512: int enable = 1 ;
513: status = IOCTL(net->sockfd, FIONBIO, &enable);
514: #endif
2.56 frystyk 515: } /* end scope */
516: #else
517: #if defined(VMS)
518: {
519: int enable = 1;
520: status = IOCTL(net->sockfd, FIONBIO, &enable);
521: }
522: #else
2.82 frystyk 523: if((status = fcntl(net->sockfd, F_GETFL, 0)) != -1) {
2.81 frystyk 524: #ifdef O_NONBLOCK
2.56 frystyk 525: status |= O_NONBLOCK; /* POSIX */
2.81 frystyk 526: #else
527: #ifdef F_NDELAY
528: status |= F_NDELAY; /* BSD */
529: #endif /* F_NDELAY */
530: #endif /* O_NONBLOCK */
2.82 frystyk 531: status = fcntl(net->sockfd, F_SETFL, status);
2.56 frystyk 532: }
533: #endif /* VMS */
534: #endif /* WINDOW */
535: if (PROT_TRACE) {
536: if (status == -1)
2.83 frystyk 537: HTTrace("Sockrt...... Blocking socket\n");
2.56 frystyk 538: else
2.83 frystyk 539: HTTrace("Socket...... Non-blocking socket\n");
2.56 frystyk 540: }
541: }
542: net->tcpstate = TCP_NEED_BIND;
543: break;
544:
545: case TCP_NEED_BIND:
546: status = bind(net->sockfd, (struct sockaddr *) &net->sock_addr,
547: sizeof(net->sock_addr));
548: #ifdef _WINSOCKAPI_
549: if (status == SOCKET_ERROR)
550: #else
551: if (status < 0)
552: #endif
553: {
554: if (PROT_TRACE)
2.83 frystyk 555: HTTrace("Socket...... Bind failed %d\n", socerrno);
2.56 frystyk 556: net->tcpstate = TCP_ERROR;
557: } else
558: net->tcpstate = TCP_NEED_LISTEN;
559: break;
560:
561: case TCP_NEED_LISTEN:
2.67 frystyk 562: status = listen(net->sockfd, backlog);
2.56 frystyk 563: #ifdef _WINSOCKAPI_
564: if (status == SOCKET_ERROR)
565: #else
566: if (status < 0)
2.36 frystyk 567: #endif
2.56 frystyk 568: net->tcpstate = TCP_ERROR;
569: else
570: net->tcpstate = TCP_CONNECTED;
571: break;
572:
573: case TCP_CONNECTED:
574: net->tcpstate = TCP_BEGIN;
575: if (PROT_TRACE)
2.83 frystyk 576: HTTrace("Socket...... Bind and listen on port %d %s\n",
2.56 frystyk 577: (int) ntohs(net->sock_addr.sin_port),
578: HTInetString(&net->sock_addr));
579: return HT_OK;
580: break;
2.13 frystyk 581:
2.56 frystyk 582: case TCP_NEED_CONNECT:
583: case TCP_DNS:
584: case TCP_ERROR:
2.83 frystyk 585: if (PROT_TRACE) HTTrace("Socket...... Listen failed\n");
2.62 frystyk 586: HTRequest_addSystemError(net->request, ERR_FATAL, socerrno, NO, "HTDoListen");
2.56 frystyk 587: net->tcpstate = TCP_BEGIN;
588: return HT_ERROR;
589: break;
590: }
2.38 frystyk 591: }
1.1 timbl 592: }
593:
2.83 frystyk 594: /* HTDoClose
595: ** ---------
596: ** Closes a file descriptor whatever means are available on the current
597: ** platform. If we have unix file descriptors then use this otherwise use
598: ** the ANSI C file descriptors
599: **
600: ** returns HT_ERROR Error has occured or interrupted
601: ** HT_OK if connected
602: ** HT_WOULD_BLOCK if operation would have blocked
603: */
604: PUBLIC int HTDoCLose (HTNet * net)
605: {
606: int status = -1;
607: if (net && net->sockfd != INVSOC) {
608: if (PROT_TRACE) HTTrace("Socket...... Close %d\n", net->sockfd);
609: status = NETCLOSE(net->sockfd);
610: HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
611: net->sockfd = INVSOC;
612: }
613: return status < 0 ? HT_ERROR : HT_OK;
614: }
615:
Webmaster