2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1996-02-20 12:52:11 quinn
8 * WAIS protocol support.
10 * Revision 1.9 1996/02/10 12:23:11 quinn
11 * Enablie inetd operations fro TCP/IP stack
13 * Revision 1.8 1995/11/01 13:54:27 quinn
16 * Revision 1.7 1995/10/30 12:41:16 quinn
17 * Added hostname lookup for server.
19 * Revision 1.6 1995/09/29 17:12:00 quinn
22 * Revision 1.5 1995/09/29 17:01:48 quinn
25 * Revision 1.4 1995/09/28 10:12:26 quinn
26 * Windows-support changes
28 * Revision 1.3 1995/09/27 15:02:45 quinn
29 * Modified function heads & prototypes.
31 * Revision 1.2 1995/06/15 12:30:06 quinn
32 * Added @ as hostname alias for INADDR ANY.
34 * Revision 1.1 1995/06/14 09:58:20 quinn
35 * Renamed yazlib to comstack.
37 * Revision 1.20 1995/05/16 08:51:16 quinn
38 * License, documentation, and memory fixes
40 * Revision 1.19 1995/04/10 10:24:08 quinn
43 * Revision 1.18 1995/03/30 13:29:27 quinn
44 * Added REUSEADDR in tcpip_bind
46 * Revision 1.17 1995/03/27 08:36:10 quinn
47 * Some work on nonblocking operation in xmosi.c and rfct.c.
48 * Added protocol parameter to cs_create()
50 * Revision 1.16 1995/03/21 15:53:41 quinn
53 * Revision 1.15 1995/03/21 12:31:27 quinn
54 * Added check for EINPROGRESS on connect.
56 * Revision 1.14 1995/03/20 09:47:21 quinn
57 * Added server-side support to xmosi.c
58 * Fixed possible problems in rfct
61 * Revision 1.13 1995/03/15 16:15:13 adam
64 * Revision 1.12 1995/03/15 15:36:27 quinn
65 * Mods to support nonblocking I/O
67 * Revision 1.11 1995/03/15 08:37:57 quinn
68 * Now we're pretty much set for nonblocking I/O.
70 * Revision 1.10 1995/03/14 17:00:07 quinn
71 * Bug-fixes - added tracing info to tcpip.c
73 * Revision 1.9 1995/03/14 10:28:42 quinn
74 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
76 * Revision 1.8 1995/03/10 14:22:50 quinn
77 * Removed debug output.
79 * Revision 1.7 1995/03/10 11:44:59 quinn
82 * Revision 1.6 1995/03/07 10:26:55 quinn
83 * Initialized type field in the comstacks.
85 * Revision 1.5 1995/02/14 20:40:07 quinn
88 * Revision 1.4 1995/02/14 11:54:49 quinn
89 * Beginning to add full CCL.
91 * Revision 1.3 1995/02/10 18:58:10 quinn
92 * Fixed tcpip_get (formerly tcpip_read).
93 * Turned tst (cli) into a proper, event-driven thingy.
95 * Revision 1.2 1995/02/10 15:55:47 quinn
98 * Revision 1.1 1995/02/09 15:51:52 quinn
110 #include <comstack.h>
114 #include <sys/time.h>
117 int tcpip_close(COMSTACK h);
118 int tcpip_put(COMSTACK h, char *buf, int size);
119 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
120 int tcpip_connect(COMSTACK h, void *address);
121 int tcpip_more(COMSTACK h);
122 int tcpip_rcvconnect(COMSTACK h);
123 int tcpip_bind(COMSTACK h, void *address, int mode);
124 int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);
125 COMSTACK tcpip_accept(COMSTACK h);
126 char *tcpip_addrstr(COMSTACK h);
129 * Determine length/completeness of incoming packages
131 int completeBER(unsigned char *buf, int len); /* from the ODR module */
132 int completeWAIS(unsigned char *buf, int len); /* from waislen.c */
140 static int initialized = 0;
142 typedef struct tcpip_state
144 char *altbuf; /* alternate buffer for surplus data */
145 int altsize; /* size as xmalloced */
146 int altlen; /* length of data or 0 if none */
148 int written; /* -1 if we aren't writing */
149 int towrite; /* to verify against user input */
150 int (*complete)(unsigned char *buf, int len); /* length/completeness */
154 * s >= 0: socket has already been established for us.
156 COMSTACK tcpip_type(int s, int blocking, int protocol)
162 unsigned long tru = 1;
164 struct protoent *proto;
173 requested = MAKEWORD(1, 1);
174 if (WSAStartup(requested, &wd))
183 if (!(proto = getprotobyname("tcp")))
185 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
187 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
194 if (!(p = xmalloc(sizeof(struct comstack))))
196 if (!(state = p->private = xmalloc(sizeof(tcpip_state))))
200 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
202 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
207 p->type = tcpip_type;
208 p->protocol = protocol;
210 p->f_connect = tcpip_connect;
211 p->f_rcvconnect = tcpip_rcvconnect;
212 p->f_get = tcpip_get;
213 p->f_put = tcpip_put;
214 p->f_close = tcpip_close;
215 p->f_more = tcpip_more;
216 p->f_bind = tcpip_bind;
217 p->f_listen = tcpip_listen;
218 p->f_accept = tcpip_accept;
219 p->f_addrstr = tcpip_addrstr;
221 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
227 state->altsize = state->altlen = 0;
228 state->towrite = state->written = -1;
229 if (protocol == PROTO_WAIS)
230 state->complete = completeWAIS;
232 state->complete = completeBER;
234 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
235 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
240 struct sockaddr_in *tcpip_strtoaddr(const char *str)
242 static struct sockaddr_in add;
245 short int port = 210;
248 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
249 add.sin_family = AF_INET;
251 if ((p = strchr(buf, ':')))
256 add.sin_port = htons(port);
257 if (!strcmp("@", buf))
258 add.sin_addr.s_addr = INADDR_ANY;
259 else if ((hp = gethostbyname(buf)))
260 memcpy(&add.sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
261 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
262 memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
268 int tcpip_more(COMSTACK h)
270 tcpip_state *sp = h->private;
272 return sp->altlen && completeBER((unsigned char *) sp->altbuf, sp->altlen);
276 * connect(2) will block (sometimes) - nothing we can do short of doing
277 * weird things like spawning subprocesses or threading or some weird junk
280 int tcpip_connect(COMSTACK h, void *address)
282 struct sockaddr_in *add = address;
284 TRC(fprintf(stderr, "tcpip_connect\n"));
285 if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0)
288 if (WSAGetLastError() == WSAEWOULDBLOCK)
290 if (errno == EINPROGRESS)
295 h->state = CS_DATAXFER;
302 int tcpip_rcvconnect(COMSTACK h)
304 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
308 int tcpip_bind(COMSTACK h, void *address, int mode)
310 struct sockaddr *addr = address;
311 unsigned long one = 1;
313 TRC(fprintf(stderr, "tcpip_bind\n"));
314 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
319 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
324 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
333 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
335 struct sockaddr_in addr;
336 int len = sizeof(addr);
338 TRC(fprintf(stderr, "tcpip_listen\n"));
339 if (h->state != CS_IDLE)
341 h->cerrno = CSOUTSTATE;
344 if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)
347 if (WSAGetLastError() == WSAEWOULDBLOCK)
349 if (errno == EWOULDBLOCK)
352 h->cerrno = CSNODATA;
357 if (addrlen && *addrlen > sizeof(struct sockaddr_in))
358 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
365 COMSTACK tcpip_accept(COMSTACK h)
370 unsigned long tru = 1;
373 TRC(fprintf(stderr, "tcpip_accept\n"));
374 if (h->state != CS_INCON)
376 h->cerrno = CSOUTSTATE;
379 if (!(new = xmalloc(sizeof(*new))))
384 memcpy(new, h, sizeof(*h));
385 new->iofile = h->newfd;
386 if (!(state = new->private = xmalloc(sizeof(tcpip_state))))
392 if (!new->blocking && ioctlsocket(new->iofile, FIONBIO, &tru) < 0)
394 if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
398 state->altsize = state->altlen = 0;
399 state->towrite = state->written = -1;
400 new->state = CS_DATAXFER;
405 #define CS_TCPIP_BUFCHUNK 4096
408 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
409 * 0=connection closed.
411 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
413 tcpip_state *sp = h->private;
415 int tmpi, berlen, rest, req, tomove;
416 int hasread = 0, res;
418 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
419 if (sp->altlen) /* switch buffers */
421 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
422 (unsigned) sp->altbuf));
426 *bufsize = sp->altsize;
427 hasread = sp->altlen;
432 while (!(berlen = completeBER((unsigned char *)*buf, hasread)))
436 if (!(*buf = xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
439 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
440 if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
442 if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0)
444 if (WSAGetLastError() == WSAEWOULDBLOCK)
446 if (errno == EWOULDBLOCK)
454 TRC(fprintf(stderr, " res=%d, hasread=%d\n", res, hasread));
456 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
458 /* move surplus buffer (or everything if we didn't get a BER rec.) */
459 if (hasread > berlen)
461 tomove = req = hasread - berlen;
462 rest = tomove % CS_TCPIP_BUFCHUNK;
464 req += CS_TCPIP_BUFCHUNK - rest;
467 if (!(sp->altbuf = xmalloc(sp->altsize = req)))
469 } else if (sp->altsize < req)
470 if (!(sp->altbuf =xrealloc(sp->altbuf, sp->altsize = req)))
472 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
473 (unsigned) sp->altbuf));
474 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
476 if (berlen < CS_TCPIP_BUFCHUNK - 1)
477 *(*buf + berlen) = '\0';
478 return berlen ? berlen : 1;
483 * In nonblocking mode, you must call again with same buffer while
486 int tcpip_put(COMSTACK h, char *buf, int size)
489 struct tcpip_state *state = h->private;
491 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
492 if (state->towrite < 0)
494 state->towrite = size;
497 else if (state->towrite != size)
499 h->cerrno = CSWRONGBUF;
502 while (state->towrite > state->written)
504 if ((res = send(h->iofile, buf + state->written, size -
505 state->written, 0)) < 0)
508 if (WSAGetLastError() == WSAEWOULDBLOCK)
513 TRC(fprintf(stderr, " Flow control stop\n"));
519 state->written += res;
520 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
521 res, state->written, size));
523 state->towrite = state->written = -1;
524 TRC(fprintf(stderr, " Ok\n"));
528 int tcpip_close(COMSTACK h)
530 tcpip_state *sp = h->private;
532 TRC(fprintf(stderr, "tcpip_close\n"));
541 char *tcpip_addrstr(COMSTACK h)
543 struct sockaddr_in addr;
547 struct hostent *host;
550 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
555 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
557 r = (char*) host->h_name;
559 r = inet_ntoa(addr.sin_addr);
560 sprintf(buf, "tcp:%s", r);