2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-09-29 17:12:00 quinn
10 * Revision 1.5 1995/09/29 17:01:48 quinn
13 * Revision 1.4 1995/09/28 10:12:26 quinn
14 * Windows-support changes
16 * Revision 1.3 1995/09/27 15:02:45 quinn
17 * Modified function heads & prototypes.
19 * Revision 1.2 1995/06/15 12:30:06 quinn
20 * Added @ as hostname alias for INADDR ANY.
22 * Revision 1.1 1995/06/14 09:58:20 quinn
23 * Renamed yazlib to comstack.
25 * Revision 1.20 1995/05/16 08:51:16 quinn
26 * License, documentation, and memory fixes
28 * Revision 1.19 1995/04/10 10:24:08 quinn
31 * Revision 1.18 1995/03/30 13:29:27 quinn
32 * Added REUSEADDR in tcpip_bind
34 * Revision 1.17 1995/03/27 08:36:10 quinn
35 * Some work on nonblocking operation in xmosi.c and rfct.c.
36 * Added protocol parameter to cs_create()
38 * Revision 1.16 1995/03/21 15:53:41 quinn
41 * Revision 1.15 1995/03/21 12:31:27 quinn
42 * Added check for EINPROGRESS on connect.
44 * Revision 1.14 1995/03/20 09:47:21 quinn
45 * Added server-side support to xmosi.c
46 * Fixed possible problems in rfct
49 * Revision 1.13 1995/03/15 16:15:13 adam
52 * Revision 1.12 1995/03/15 15:36:27 quinn
53 * Mods to support nonblocking I/O
55 * Revision 1.11 1995/03/15 08:37:57 quinn
56 * Now we're pretty much set for nonblocking I/O.
58 * Revision 1.10 1995/03/14 17:00:07 quinn
59 * Bug-fixes - added tracing info to tcpip.c
61 * Revision 1.9 1995/03/14 10:28:42 quinn
62 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
64 * Revision 1.8 1995/03/10 14:22:50 quinn
65 * Removed debug output.
67 * Revision 1.7 1995/03/10 11:44:59 quinn
70 * Revision 1.6 1995/03/07 10:26:55 quinn
71 * Initialized type field in the comstacks.
73 * Revision 1.5 1995/02/14 20:40:07 quinn
76 * Revision 1.4 1995/02/14 11:54:49 quinn
77 * Beginning to add full CCL.
79 * Revision 1.3 1995/02/10 18:58:10 quinn
80 * Fixed tcpip_get (formerly tcpip_read).
81 * Turned tst (cli) into a proper, event-driven thingy.
83 * Revision 1.2 1995/02/10 15:55:47 quinn
86 * Revision 1.1 1995/02/09 15:51:52 quinn
102 #include <sys/time.h>
105 int tcpip_close(COMSTACK h);
106 int tcpip_put(COMSTACK h, char *buf, int size);
107 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
108 int tcpip_connect(COMSTACK h, void *address);
109 int tcpip_more(COMSTACK h);
110 int tcpip_rcvconnect(COMSTACK h);
111 int tcpip_bind(COMSTACK h, void *address, int mode);
112 int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);
113 COMSTACK tcpip_accept(COMSTACK h);
115 int completeBER(unsigned char *buf, int len);
123 static int initialized = 0;
125 typedef struct tcpip_state
127 char *altbuf; /* alternate buffer for surplus data */
128 int altsize; /* size as malloced */
129 int altlen; /* length of data or 0 if none */
131 int written; /* -1 if we aren't writing */
132 int towrite; /* to verify against user input */
135 COMSTACK tcpip_type(int blocking, int protocol)
141 unsigned long tru = 1;
143 struct protoent *proto;
152 requested = MAKEWORD(1, 1);
153 if (WSAStartup(requested, &wd))
160 if (!(proto = getprotobyname("tcp")))
162 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
164 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
167 if (!(p = malloc(sizeof(struct comstack))))
169 if (!(state = p->private = malloc(sizeof(tcpip_state))))
172 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
174 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
178 p->type = tcpip_type;
179 p->protocol = protocol;
181 p->f_connect = tcpip_connect;
182 p->f_rcvconnect = tcpip_rcvconnect;
183 p->f_get = tcpip_get;
184 p->f_put = tcpip_put;
185 p->f_close = tcpip_close;
186 p->f_more = tcpip_more;
187 p->f_bind = tcpip_bind;
188 p->f_listen = tcpip_listen;
189 p->f_accept = tcpip_accept;
197 state->altsize = state->altlen = 0;
198 state->towrite = state->written = -1;
200 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
201 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
206 struct sockaddr_in *tcpip_strtoaddr(const char *str)
208 static struct sockaddr_in add;
211 short int port = 210;
214 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
215 add.sin_family = AF_INET;
217 if ((p = strchr(buf, ':')))
222 add.sin_port = htons(port);
223 if (!strcmp("@", buf))
224 add.sin_addr.s_addr = INADDR_ANY;
225 else if ((hp = gethostbyname(buf)))
226 memcpy(&add.sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
227 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
228 memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
234 int tcpip_more(COMSTACK h)
236 tcpip_state *sp = h->private;
238 return sp->altlen && completeBER((unsigned char *) sp->altbuf, sp->altlen);
242 * connect(2) will block (sometimes) - nothing we can do short of doing
243 * weird things like spawning subprocesses or threading or some weird junk
246 int tcpip_connect(COMSTACK h, void *address)
248 struct sockaddr_in *add = address;
250 TRC(fprintf(stderr, "tcpip_connect\n"));
251 if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0)
254 if (WSAGetLastError() == WSAEWOULDBLOCK)
256 if (errno == EINPROGRESS)
261 h->state = CS_DATAXFER;
268 int tcpip_rcvconnect(COMSTACK h)
270 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
274 int tcpip_bind(COMSTACK h, void *address, int mode)
276 struct sockaddr *addr = address;
277 unsigned long one = 1;
279 TRC(fprintf(stderr, "tcpip_bind\n"));
280 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
285 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
290 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
299 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
301 struct sockaddr_in addr;
302 int len = sizeof(addr);
304 TRC(fprintf(stderr, "tcpip_listen\n"));
305 if (h->state != CS_IDLE)
307 h->cerrno = CSOUTSTATE;
310 if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)
313 if (WSAGetLastError() == WSAEWOULDBLOCK)
315 if (errno == EWOULDBLOCK)
318 h->cerrno = CSNODATA;
323 if (addrlen && *addrlen > sizeof(struct sockaddr_in))
324 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
331 COMSTACK tcpip_accept(COMSTACK h)
336 unsigned long tru = 1;
339 TRC(fprintf(stderr, "tcpip_accept\n"));
340 if (h->state != CS_INCON)
342 h->cerrno = CSOUTSTATE;
345 if (!(new = malloc(sizeof(*new))))
350 memcpy(new, h, sizeof(*h));
351 new->iofile = h->newfd;
352 if (!(state = new->private = malloc(sizeof(tcpip_state))))
358 if (!new->blocking && ioctlsocket(new->iofile, FIONBIO, &tru) < 0)
360 if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
364 state->altsize = state->altlen = 0;
365 state->towrite = state->written = -1;
366 new->state = CS_DATAXFER;
371 #define CS_TCPIP_BUFCHUNK 4096
374 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
375 * 0=connection closed.
377 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
379 tcpip_state *sp = h->private;
381 int tmpi, berlen, rest, req, tomove;
382 int hasread = 0, res;
384 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
385 if (sp->altlen) /* switch buffers */
387 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
388 (unsigned) sp->altbuf));
392 *bufsize = sp->altsize;
393 hasread = sp->altlen;
398 while (!(berlen = completeBER((unsigned char *)*buf, hasread)))
402 if (!(*buf = malloc(*bufsize = CS_TCPIP_BUFCHUNK)))
405 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
406 if (!(*buf = realloc(*buf, *bufsize *= 2)))
408 if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0)
410 if (WSAGetLastError() == WSAEWOULDBLOCK)
412 if (errno == EWOULDBLOCK)
420 TRC(fprintf(stderr, " res=%d, hasread=%d\n", res, hasread));
422 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
424 /* move surplus buffer (or everything if we didn't get a BER rec.) */
425 if (hasread > berlen)
427 tomove = req = hasread - berlen;
428 rest = tomove % CS_TCPIP_BUFCHUNK;
430 req += CS_TCPIP_BUFCHUNK - rest;
433 if (!(sp->altbuf = malloc(sp->altsize = req)))
435 } else if (sp->altsize < req)
436 if (!(sp->altbuf = realloc(sp->altbuf, sp->altsize = req)))
438 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
439 (unsigned) sp->altbuf));
440 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
442 if (berlen < CS_TCPIP_BUFCHUNK - 1)
443 *(*buf + berlen) = '\0';
444 return berlen ? berlen : 1;
449 * In nonblocking mode, you must call again with same buffer while
452 int tcpip_put(COMSTACK h, char *buf, int size)
455 struct tcpip_state *state = h->private;
457 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
458 if (state->towrite < 0)
460 state->towrite = size;
463 else if (state->towrite != size)
465 h->cerrno = CSWRONGBUF;
468 while (state->towrite > state->written)
470 if ((res = send(h->iofile, buf + state->written, size -
471 state->written, 0)) < 0)
474 if (WSAGetLastError() == WSAEWOULDBLOCK)
479 TRC(fprintf(stderr, " Flow control stop\n"));
485 state->written += res;
486 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
487 res, state->written, size));
489 state->towrite = state->written = -1;
490 TRC(fprintf(stderr, " Ok\n"));
494 int tcpip_close(COMSTACK h)
496 tcpip_state *sp = h->private;
498 TRC(fprintf(stderr, "tcpip_close\n"));