2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-11-01 13:54:27 quinn
10 * Revision 1.7 1995/10/30 12:41:16 quinn
11 * Added hostname lookup for server.
13 * Revision 1.6 1995/09/29 17:12:00 quinn
16 * Revision 1.5 1995/09/29 17:01:48 quinn
19 * Revision 1.4 1995/09/28 10:12:26 quinn
20 * Windows-support changes
22 * Revision 1.3 1995/09/27 15:02:45 quinn
23 * Modified function heads & prototypes.
25 * Revision 1.2 1995/06/15 12:30:06 quinn
26 * Added @ as hostname alias for INADDR ANY.
28 * Revision 1.1 1995/06/14 09:58:20 quinn
29 * Renamed yazlib to comstack.
31 * Revision 1.20 1995/05/16 08:51:16 quinn
32 * License, documentation, and memory fixes
34 * Revision 1.19 1995/04/10 10:24:08 quinn
37 * Revision 1.18 1995/03/30 13:29:27 quinn
38 * Added REUSEADDR in tcpip_bind
40 * Revision 1.17 1995/03/27 08:36:10 quinn
41 * Some work on nonblocking operation in xmosi.c and rfct.c.
42 * Added protocol parameter to cs_create()
44 * Revision 1.16 1995/03/21 15:53:41 quinn
47 * Revision 1.15 1995/03/21 12:31:27 quinn
48 * Added check for EINPROGRESS on connect.
50 * Revision 1.14 1995/03/20 09:47:21 quinn
51 * Added server-side support to xmosi.c
52 * Fixed possible problems in rfct
55 * Revision 1.13 1995/03/15 16:15:13 adam
58 * Revision 1.12 1995/03/15 15:36:27 quinn
59 * Mods to support nonblocking I/O
61 * Revision 1.11 1995/03/15 08:37:57 quinn
62 * Now we're pretty much set for nonblocking I/O.
64 * Revision 1.10 1995/03/14 17:00:07 quinn
65 * Bug-fixes - added tracing info to tcpip.c
67 * Revision 1.9 1995/03/14 10:28:42 quinn
68 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
70 * Revision 1.8 1995/03/10 14:22:50 quinn
71 * Removed debug output.
73 * Revision 1.7 1995/03/10 11:44:59 quinn
76 * Revision 1.6 1995/03/07 10:26:55 quinn
77 * Initialized type field in the comstacks.
79 * Revision 1.5 1995/02/14 20:40:07 quinn
82 * Revision 1.4 1995/02/14 11:54:49 quinn
83 * Beginning to add full CCL.
85 * Revision 1.3 1995/02/10 18:58:10 quinn
86 * Fixed tcpip_get (formerly tcpip_read).
87 * Turned tst (cli) into a proper, event-driven thingy.
89 * Revision 1.2 1995/02/10 15:55:47 quinn
92 * Revision 1.1 1995/02/09 15:51:52 quinn
104 #include <comstack.h>
108 #include <sys/time.h>
111 int tcpip_close(COMSTACK h);
112 int tcpip_put(COMSTACK h, char *buf, int size);
113 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
114 int tcpip_connect(COMSTACK h, void *address);
115 int tcpip_more(COMSTACK h);
116 int tcpip_rcvconnect(COMSTACK h);
117 int tcpip_bind(COMSTACK h, void *address, int mode);
118 int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);
119 COMSTACK tcpip_accept(COMSTACK h);
120 char *tcpip_addrstr(COMSTACK h);
122 int completeBER(unsigned char *buf, int len);
130 static int initialized = 0;
132 typedef struct tcpip_state
134 char *altbuf; /* alternate buffer for surplus data */
135 int altsize; /* size as xmalloced */
136 int altlen; /* length of data or 0 if none */
138 int written; /* -1 if we aren't writing */
139 int towrite; /* to verify against user input */
142 COMSTACK tcpip_type(int blocking, int protocol)
148 unsigned long tru = 1;
150 struct protoent *proto;
159 requested = MAKEWORD(1, 1);
160 if (WSAStartup(requested, &wd))
167 if (!(proto = getprotobyname("tcp")))
169 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
171 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
174 if (!(p = xmalloc(sizeof(struct comstack))))
176 if (!(state = p->private = xmalloc(sizeof(tcpip_state))))
179 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
181 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
185 p->type = tcpip_type;
186 p->protocol = protocol;
188 p->f_connect = tcpip_connect;
189 p->f_rcvconnect = tcpip_rcvconnect;
190 p->f_get = tcpip_get;
191 p->f_put = tcpip_put;
192 p->f_close = tcpip_close;
193 p->f_more = tcpip_more;
194 p->f_bind = tcpip_bind;
195 p->f_listen = tcpip_listen;
196 p->f_accept = tcpip_accept;
197 p->f_addrstr = tcpip_addrstr;
205 state->altsize = state->altlen = 0;
206 state->towrite = state->written = -1;
208 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
209 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
214 struct sockaddr_in *tcpip_strtoaddr(const char *str)
216 static struct sockaddr_in add;
219 short int port = 210;
222 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
223 add.sin_family = AF_INET;
225 if ((p = strchr(buf, ':')))
230 add.sin_port = htons(port);
231 if (!strcmp("@", buf))
232 add.sin_addr.s_addr = INADDR_ANY;
233 else if ((hp = gethostbyname(buf)))
234 memcpy(&add.sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
235 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
236 memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
242 int tcpip_more(COMSTACK h)
244 tcpip_state *sp = h->private;
246 return sp->altlen && completeBER((unsigned char *) sp->altbuf, sp->altlen);
250 * connect(2) will block (sometimes) - nothing we can do short of doing
251 * weird things like spawning subprocesses or threading or some weird junk
254 int tcpip_connect(COMSTACK h, void *address)
256 struct sockaddr_in *add = address;
258 TRC(fprintf(stderr, "tcpip_connect\n"));
259 if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0)
262 if (WSAGetLastError() == WSAEWOULDBLOCK)
264 if (errno == EINPROGRESS)
269 h->state = CS_DATAXFER;
276 int tcpip_rcvconnect(COMSTACK h)
278 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
282 int tcpip_bind(COMSTACK h, void *address, int mode)
284 struct sockaddr *addr = address;
285 unsigned long one = 1;
287 TRC(fprintf(stderr, "tcpip_bind\n"));
288 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
293 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
298 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
307 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
309 struct sockaddr_in addr;
310 int len = sizeof(addr);
312 TRC(fprintf(stderr, "tcpip_listen\n"));
313 if (h->state != CS_IDLE)
315 h->cerrno = CSOUTSTATE;
318 if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)
321 if (WSAGetLastError() == WSAEWOULDBLOCK)
323 if (errno == EWOULDBLOCK)
326 h->cerrno = CSNODATA;
331 if (addrlen && *addrlen > sizeof(struct sockaddr_in))
332 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
339 COMSTACK tcpip_accept(COMSTACK h)
344 unsigned long tru = 1;
347 TRC(fprintf(stderr, "tcpip_accept\n"));
348 if (h->state != CS_INCON)
350 h->cerrno = CSOUTSTATE;
353 if (!(new = xmalloc(sizeof(*new))))
358 memcpy(new, h, sizeof(*h));
359 new->iofile = h->newfd;
360 if (!(state = new->private = xmalloc(sizeof(tcpip_state))))
366 if (!new->blocking && ioctlsocket(new->iofile, FIONBIO, &tru) < 0)
368 if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
372 state->altsize = state->altlen = 0;
373 state->towrite = state->written = -1;
374 new->state = CS_DATAXFER;
379 #define CS_TCPIP_BUFCHUNK 4096
382 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
383 * 0=connection closed.
385 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
387 tcpip_state *sp = h->private;
389 int tmpi, berlen, rest, req, tomove;
390 int hasread = 0, res;
392 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
393 if (sp->altlen) /* switch buffers */
395 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
396 (unsigned) sp->altbuf));
400 *bufsize = sp->altsize;
401 hasread = sp->altlen;
406 while (!(berlen = completeBER((unsigned char *)*buf, hasread)))
410 if (!(*buf = xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
413 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
414 if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
416 if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0)
418 if (WSAGetLastError() == WSAEWOULDBLOCK)
420 if (errno == EWOULDBLOCK)
428 TRC(fprintf(stderr, " res=%d, hasread=%d\n", res, hasread));
430 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
432 /* move surplus buffer (or everything if we didn't get a BER rec.) */
433 if (hasread > berlen)
435 tomove = req = hasread - berlen;
436 rest = tomove % CS_TCPIP_BUFCHUNK;
438 req += CS_TCPIP_BUFCHUNK - rest;
441 if (!(sp->altbuf = xmalloc(sp->altsize = req)))
443 } else if (sp->altsize < req)
444 if (!(sp->altbuf =xrealloc(sp->altbuf, sp->altsize = req)))
446 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
447 (unsigned) sp->altbuf));
448 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
450 if (berlen < CS_TCPIP_BUFCHUNK - 1)
451 *(*buf + berlen) = '\0';
452 return berlen ? berlen : 1;
457 * In nonblocking mode, you must call again with same buffer while
460 int tcpip_put(COMSTACK h, char *buf, int size)
463 struct tcpip_state *state = h->private;
465 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
466 if (state->towrite < 0)
468 state->towrite = size;
471 else if (state->towrite != size)
473 h->cerrno = CSWRONGBUF;
476 while (state->towrite > state->written)
478 if ((res = send(h->iofile, buf + state->written, size -
479 state->written, 0)) < 0)
482 if (WSAGetLastError() == WSAEWOULDBLOCK)
487 TRC(fprintf(stderr, " Flow control stop\n"));
493 state->written += res;
494 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
495 res, state->written, size));
497 state->towrite = state->written = -1;
498 TRC(fprintf(stderr, " Ok\n"));
502 int tcpip_close(COMSTACK h)
504 tcpip_state *sp = h->private;
506 TRC(fprintf(stderr, "tcpip_close\n"));
515 char *tcpip_addrstr(COMSTACK h)
517 struct sockaddr_in addr;
521 struct hostent *host;
524 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
529 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
531 r = (char*) host->h_name;
533 r = inet_ntoa(addr.sin_addr);
534 sprintf(buf, "tcp:%s", r);