2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.57 2003-05-13 14:21:13 adam Exp $
20 #if HAVE_OPENSSL_SSL_H
21 #include <openssl/ssl.h>
22 #include <openssl/err.h>
25 #include <yaz/comstack.h>
26 #include <yaz/tcpip.h>
32 #include <netinet/tcp.h>
35 static int tcpip_close(COMSTACK h);
36 static int tcpip_put(COMSTACK h, char *buf, int size);
37 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
38 static int tcpip_connect(COMSTACK h, void *address);
39 static int tcpip_more(COMSTACK h);
40 static int tcpip_rcvconnect(COMSTACK h);
41 static int tcpip_bind(COMSTACK h, void *address, int mode);
42 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
43 int (*check_ip)(void *cd, const char *a, int len, int type),
45 static int tcpip_set_blocking(COMSTACK p, int blocking);
47 #if HAVE_OPENSSL_SSL_H
48 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
49 static int ssl_put(COMSTACK h, char *buf, int size);
52 static COMSTACK tcpip_accept(COMSTACK h);
53 static char *tcpip_addrstr(COMSTACK h);
54 static void *tcpip_straddr(COMSTACK h, const char *str);
63 #define YAZ_SOCKLEN_T int
66 /* this state is used for both SSL and straight TCP/IP */
67 typedef struct tcpip_state
69 char *altbuf; /* alternate buffer for surplus data */
70 int altsize; /* size as xmalloced */
71 int altlen; /* length of data or 0 if none */
73 int written; /* -1 if we aren't writing */
74 int towrite; /* to verify against user input */
75 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
76 struct sockaddr_in addr; /* returned by cs_straddr */
77 char buf[128]; /* returned by cs_addrstr */
78 #if HAVE_OPENSSL_SSL_H
86 static int tcpip_init (void)
88 static int initialized = 0;
94 requested = MAKEWORD(1, 1);
95 if (WSAStartup(requested, &wd))
102 static int tcpip_init (void)
109 * This function is always called through the cs_create() macro.
110 * s >= 0: socket has already been established for us.
112 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
118 unsigned long tru = 1;
125 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
131 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
133 if (!(state = (struct tcpip_state *)(p->cprivate =
134 xmalloc(sizeof(tcpip_state)))))
138 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
141 if (!(p->blocking = blocking))
143 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
146 signal (SIGPIPE, SIG_IGN);
153 p->type = tcpip_type;
154 p->protocol = (enum oid_proto) protocol;
156 p->f_connect = tcpip_connect;
157 p->f_rcvconnect = tcpip_rcvconnect;
158 p->f_get = tcpip_get;
159 p->f_put = tcpip_put;
160 p->f_close = tcpip_close;
161 p->f_more = tcpip_more;
162 p->f_bind = tcpip_bind;
163 p->f_listen = tcpip_listen;
164 p->f_accept = tcpip_accept;
165 p->f_addrstr = tcpip_addrstr;
166 p->f_straddr = tcpip_straddr;
167 p->f_set_blocking = tcpip_set_blocking;
169 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
174 #if HAVE_OPENSSL_SSL_H
175 state->ctx = state->ctx_alloc = 0;
180 state->altsize = state->altlen = 0;
181 state->towrite = state->written = -1;
182 if (protocol == PROTO_WAIS)
183 state->complete = completeWAIS;
185 state->complete = cs_complete_auto;
187 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
188 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
193 #if HAVE_OPENSSL_SSL_H
195 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
199 yaz_log(LOG_LOG, "ssl_type begin");
201 p = tcpip_type (s, blocking, protocol, 0);
207 state = (tcpip_state *) p->cprivate;
212 SSL_load_error_strings();
213 SSLeay_add_all_algorithms();
215 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
222 /* note: we don't handle already opened socket in SSL mode - yet */
223 yaz_log(LOG_LOG, "ssl_type end");
228 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
233 short int port = default_port;
238 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
239 add->sin_family = AF_INET;
240 strncpy(buf, str, 511);
242 if ((p = strchr(buf, '/')))
244 if ((p = strchr(buf, ':')))
249 add->sin_port = htons(port);
250 if (!strcmp("@", buf))
251 add->sin_addr.s_addr = INADDR_ANY;
252 else if ((hp = gethostbyname(buf)))
253 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
254 sizeof(struct in_addr));
255 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
256 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
262 void *tcpip_straddr(COMSTACK h, const char *str)
264 tcpip_state *sp = (tcpip_state *)h->cprivate;
267 if (h->protocol == PROTO_HTTP)
270 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
275 struct sockaddr_in *tcpip_strtoaddr(const char *str)
277 static struct sockaddr_in add;
279 if (!tcpip_strtoaddr_ex (str, &add, 210))
284 int tcpip_more(COMSTACK h)
286 tcpip_state *sp = (tcpip_state *)h->cprivate;
288 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
293 * connect(2) will block (sometimes) - nothing we can do short of doing
294 * weird things like spawning subprocesses or threading or some weird junk
297 int tcpip_connect(COMSTACK h, void *address)
299 struct sockaddr_in *add = (struct sockaddr_in *)address;
300 #if HAVE_OPENSSL_SSL_H
301 tcpip_state *sp = (tcpip_state *)h->cprivate;
306 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
308 TRC(fprintf(stderr, "tcpip_connect\n"));
310 if (h->state != CS_ST_UNBND)
312 h->cerrno = CSOUTSTATE;
316 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
317 * This gives the connect a chance to negotiate with the other side
320 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
325 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
327 recbuflen *= 10; /* lets be optimistic */
328 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
333 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
338 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
341 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
345 if (WSAGetLastError() == WSAEWOULDBLOCK)
347 h->event = CS_CONNECT;
348 h->state = CS_ST_CONNECTING;
349 h->io_pending = CS_WANT_WRITE;
353 if (yaz_errno() == EINPROGRESS)
355 h->event = CS_CONNECT;
356 h->state = CS_ST_CONNECTING;
357 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
364 h->event = CS_CONNECT;
365 h->state = CS_ST_CONNECTING;
367 return tcpip_rcvconnect (h);
373 int tcpip_rcvconnect(COMSTACK h)
375 #if HAVE_OPENSSL_SSL_H
376 tcpip_state *sp = (tcpip_state *)h->cprivate;
378 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
380 if (h->state == CS_ST_DATAXFER)
382 if (h->state != CS_ST_CONNECTING)
384 h->cerrno = CSOUTSTATE;
387 #if HAVE_OPENSSL_SSL_H
394 sp->ssl = SSL_new (sp->ctx);
395 SSL_set_fd (sp->ssl, h->iofile);
397 res = SSL_connect (sp->ssl);
400 int err = SSL_get_error(sp->ssl, res);
401 if (err == SSL_ERROR_WANT_READ)
403 h->io_pending = CS_WANT_READ;
406 if (err == SSL_ERROR_WANT_WRITE)
408 h->io_pending = CS_WANT_WRITE;
411 h->cerrno = CSERRORSSL;
417 h->state = CS_ST_DATAXFER;
421 #define CERTF "ztest.pem"
422 #define KEYF "ztest.pem"
424 static void tcpip_setsockopt (int fd)
430 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
432 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
434 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
436 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
438 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
440 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
445 static int tcpip_bind(COMSTACK h, void *address, int mode)
447 struct sockaddr *addr = (struct sockaddr *)address;
451 unsigned long one = 1;
454 #if HAVE_OPENSSL_SSL_H
455 tcpip_state *sp = (tcpip_state *)h->cprivate;
461 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
465 ERR_print_errors_fp(stderr);
468 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
472 ERR_print_errors_fp(stderr);
475 res = SSL_CTX_check_private_key (sp->ctx);
478 ERR_print_errors_fp(stderr);
482 TRC (fprintf (stderr, "ssl_bind\n"));
486 TRC (fprintf (stderr, "tcpip_bind\n"));
489 TRC (fprintf (stderr, "tcpip_bind\n"));
492 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
493 &one, sizeof(one)) < 0)
499 tcpip_setsockopt(h->iofile);
500 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
505 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
510 h->state = CS_ST_IDLE;
511 h->event = CS_LISTEN;
515 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
516 int (*check_ip)(void *cd, const char *a, int len, int t),
519 struct sockaddr_in addr;
520 YAZ_SOCKLEN_T len = sizeof(addr);
522 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
523 if (h->state != CS_ST_IDLE)
525 h->cerrno = CSOUTSTATE;
528 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
533 WSAGetLastError() == WSAEWOULDBLOCK
535 yaz_errno() == EWOULDBLOCK
537 #if EAGAIN != EWOULDBLOCK
538 || yaz_errno() == EAGAIN
543 h->cerrno = CSNODATA;
548 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
549 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
552 if (check_ip && (*check_ip)(cd, (const char *) &addr,
553 sizeof(addr), AF_INET))
557 closesocket(h->newfd);
564 h->state = CS_ST_INCON;
565 tcpip_setsockopt (h->newfd);
569 COMSTACK tcpip_accept(COMSTACK h)
572 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
574 unsigned long tru = 1;
577 TRC(fprintf(stderr, "tcpip_accept\n"));
578 if (h->state == CS_ST_INCON)
580 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
584 closesocket(h->newfd);
591 memcpy(cnew, h, sizeof(*h));
592 cnew->iofile = h->newfd;
593 cnew->io_pending = 0;
594 if (!(state = (tcpip_state *)
595 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
601 closesocket(h->newfd);
609 if (!cnew->blocking &&
611 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
613 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
621 closesocket(h->newfd);
633 state->altsize = state->altlen = 0;
634 state->towrite = state->written = -1;
635 state->complete = st->complete;
636 cnew->state = CS_ST_ACCEPT;
637 h->state = CS_ST_IDLE;
639 #if HAVE_OPENSSL_SSL_H
640 state->ctx = st->ctx;
641 state->ctx_alloc = 0;
642 state->ssl = st->ssl;
645 state->ssl = SSL_new (state->ctx);
646 SSL_set_fd (state->ssl, cnew->iofile);
651 if (h->state == CS_ST_ACCEPT)
653 #if HAVE_OPENSSL_SSL_H
654 tcpip_state *state = (tcpip_state *)h->cprivate;
657 int res = SSL_accept (state->ssl);
658 TRC(fprintf(stderr, "SSL_accept\n"));
661 int err = SSL_get_error(state->ssl, res);
662 if (err == SSL_ERROR_WANT_READ)
664 h->io_pending = CS_WANT_READ;
667 if (err == SSL_ERROR_WANT_WRITE)
669 h->io_pending = CS_WANT_WRITE;
680 h->cerrno = CSOUTSTATE;
684 h->state = CS_ST_DATAXFER;
689 #define CS_TCPIP_BUFCHUNK 4096
692 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
693 * 0=connection closed.
695 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
697 tcpip_state *sp = (tcpip_state *)h->cprivate;
699 int tmpi, berlen, rest, req, tomove;
700 int hasread = 0, res;
702 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
703 if (sp->altlen) /* switch buffers */
705 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
706 (unsigned) sp->altbuf));
710 *bufsize = sp->altsize;
711 hasread = sp->altlen;
717 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
721 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
724 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
725 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
729 // unfortunatly, sun sometimes forgets to set errno in recv
730 // when EWOULDBLOCK etc. would be required (res = -1)
732 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
733 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
736 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
737 strerror(yaz_errno())));
739 if (WSAGetLastError() == WSAEWOULDBLOCK)
741 h->io_pending = CS_WANT_READ;
747 if (yaz_errno() == EWOULDBLOCK
749 #if EAGAIN != EWOULDBLOCK
750 || yaz_errno() == EAGAIN
753 || yaz_errno() == EINPROGRESS
755 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
759 h->io_pending = CS_WANT_READ;
762 else if (yaz_errno() == 0)
772 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
774 /* move surplus buffer (or everything if we didn't get a BER rec.) */
775 if (hasread > berlen)
777 tomove = req = hasread - berlen;
778 rest = tomove % CS_TCPIP_BUFCHUNK;
780 req += CS_TCPIP_BUFCHUNK - rest;
783 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
785 } else if (sp->altsize < req)
786 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
788 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
789 (unsigned) sp->altbuf));
790 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
792 if (berlen < CS_TCPIP_BUFCHUNK - 1)
793 *(*buf + berlen) = '\0';
794 return berlen ? berlen : 1;
798 #if HAVE_OPENSSL_SSL_H
800 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
801 * 0=connection closed.
803 int ssl_get(COMSTACK h, char **buf, int *bufsize)
805 tcpip_state *sp = (tcpip_state *)h->cprivate;
807 int tmpi, berlen, rest, req, tomove;
808 int hasread = 0, res;
810 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
811 if (sp->altlen) /* switch buffers */
813 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
814 (unsigned) sp->altbuf));
818 *bufsize = sp->altsize;
819 hasread = sp->altlen;
825 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
829 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
832 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
833 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
835 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
836 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
839 int ssl_err = SSL_get_error(sp->ssl, res);
840 if (ssl_err == SSL_ERROR_WANT_READ)
842 h->io_pending = CS_WANT_READ;
845 if (ssl_err == SSL_ERROR_WANT_WRITE)
847 h->io_pending = CS_WANT_WRITE;
852 h->cerrno = CSERRORSSL;
857 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
859 /* move surplus buffer (or everything if we didn't get a BER rec.) */
860 if (hasread > berlen)
862 tomove = req = hasread - berlen;
863 rest = tomove % CS_TCPIP_BUFCHUNK;
865 req += CS_TCPIP_BUFCHUNK - rest;
868 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
870 } else if (sp->altsize < req)
871 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
873 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
874 (unsigned) sp->altbuf));
875 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
877 if (berlen < CS_TCPIP_BUFCHUNK - 1)
878 *(*buf + berlen) = '\0';
879 return berlen ? berlen : 1;
885 * In nonblocking mode, you must call again with same buffer while
888 int tcpip_put(COMSTACK h, char *buf, int size)
891 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
893 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
896 if (state->towrite < 0)
898 state->towrite = size;
901 else if (state->towrite != size)
903 h->cerrno = CSWRONGBUF;
906 while (state->towrite > state->written)
909 send(h->iofile, buf + state->written, size -
920 WSAGetLastError() == WSAEWOULDBLOCK
922 yaz_errno() == EWOULDBLOCK
924 #if EAGAIN != EWOULDBLOCK
925 || yaz_errno() == EAGAIN
929 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
931 || yaz_errno() == EINPROGRESS
935 TRC(fprintf(stderr, " Flow control stop\n"));
936 h->io_pending = CS_WANT_WRITE;
942 state->written += res;
943 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
944 res, state->written, size));
946 state->towrite = state->written = -1;
947 TRC(fprintf(stderr, " Ok\n"));
952 #if HAVE_OPENSSL_SSL_H
955 * In nonblocking mode, you must call again with same buffer while
958 int ssl_put(COMSTACK h, char *buf, int size)
961 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
963 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
966 if (state->towrite < 0)
968 state->towrite = size;
971 else if (state->towrite != size)
973 h->cerrno = CSWRONGBUF;
976 while (state->towrite > state->written)
978 res = SSL_write (state->ssl, buf + state->written,
979 size - state->written);
982 int ssl_err = SSL_get_error(state->ssl, res);
983 if (ssl_err == SSL_ERROR_WANT_READ)
985 h->io_pending = CS_WANT_READ;
988 if (ssl_err == SSL_ERROR_WANT_WRITE)
990 h->io_pending = CS_WANT_WRITE;
993 h->cerrno = CSERRORSSL;
996 state->written += res;
997 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
998 res, state->written, size));
1000 state->towrite = state->written = -1;
1001 TRC(fprintf(stderr, " Ok\n"));
1006 int tcpip_close(COMSTACK h)
1008 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1010 TRC(fprintf(stderr, "tcpip_close\n"));
1011 if (h->iofile != -1)
1013 #if HAVE_OPENSSL_SSL_H
1016 SSL_shutdown (sp->ssl);
1020 closesocket(h->iofile);
1027 #if HAVE_OPENSSL_SSL_H
1030 TRC (fprintf(stderr, "SSL_free\n"));
1035 SSL_CTX_free (sp->ctx_alloc);
1042 char *tcpip_addrstr(COMSTACK h)
1044 struct sockaddr_in addr;
1045 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1046 char *r, *buf = sp->buf;
1048 struct hostent *host;
1051 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1053 h->cerrno = CSYSERR;
1056 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1058 r = (char*) host->h_name;
1060 r = inet_ntoa(addr.sin_addr);
1061 if (h->protocol == PROTO_HTTP)
1062 sprintf(buf, "http:%s", r);
1064 sprintf(buf, "tcp:%s", r);
1065 #if HAVE_OPENSSL_SSL_H
1068 if (h->protocol == PROTO_HTTP)
1069 sprintf(buf, "https:%s", r);
1071 sprintf(buf, "ssl:%s", r);
1077 int static tcpip_set_blocking(COMSTACK p, int blocking)
1081 if (p->blocking == blocking)
1085 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1088 flag = fcntl(p->iofile, F_GETFL, 0);
1090 flag = flag & ~O_NONBLOCK;
1092 flag = flag | O_NONBLOCK;
1093 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1096 p->blocking = blocking;