2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.3 2002-07-03 13:36:55 adam Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
18 #include <sys/socket.h>
22 #include <yaz/comstack.h>
26 /* Chas added the following, so we get the definition of completeBER */
29 int unix_close(COMSTACK h);
30 int unix_put(COMSTACK h, char *buf, int size);
31 int unix_get(COMSTACK h, char **buf, int *bufsize);
32 int unix_connect(COMSTACK h, void *address);
33 int unix_more(COMSTACK h);
34 int unix_rcvconnect(COMSTACK h);
35 int unix_bind(COMSTACK h, void *address, int mode);
36 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
37 int (*check_ip)(void *cd, const char *a, int len, int type),
39 int static unix_set_blocking(COMSTACK p, int blocking);
42 COMSTACK unix_accept(COMSTACK h);
43 char *unix_addrstr(COMSTACK h);
44 void *unix_straddr(COMSTACK h, const char *str);
52 /* this state is used for both SSL and straight TCP/IP */
53 typedef struct unix_state
55 char *altbuf; /* alternate buffer for surplus data */
56 int altsize; /* size as xmalloced */
57 int altlen; /* length of data or 0 if none */
59 int written; /* -1 if we aren't writing */
60 int towrite; /* to verify against user input */
61 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
62 struct sockaddr_un addr; /* returned by cs_straddr */
63 char buf[128]; /* returned by cs_addrstr */
66 static int unix_init (void)
72 * This function is always called through the cs_create() macro.
73 * s >= 0: socket has already been established for us.
75 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
85 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
91 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
93 if (!(state = (struct unix_state *)(p->cprivate =
94 xmalloc(sizeof(unix_state)))))
97 if (!(p->blocking = blocking))
99 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
102 signal (SIGPIPE, SIG_IGN);
109 p->protocol = (enum oid_proto) protocol;
111 p->f_connect = unix_connect;
112 p->f_rcvconnect = unix_rcvconnect;
115 p->f_close = unix_close;
116 p->f_more = unix_more;
117 p->f_bind = unix_bind;
118 p->f_listen = unix_listen;
119 p->f_accept = unix_accept;
120 p->f_addrstr = unix_addrstr;
121 p->f_straddr = unix_straddr;
122 p->f_set_blocking = unix_set_blocking;
124 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
130 state->altsize = state->altlen = 0;
131 state->towrite = state->written = -1;
132 if (protocol == PROTO_WAIS)
133 state->complete = completeWAIS;
135 state->complete = completeBER;
137 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
138 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
144 int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
149 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
150 add->sun_family = AF_UNIX;
151 strncpy(add->sun_path, str, sizeof(add->sun_path));
152 cp = strchr (add->sun_path, ':');
158 void *unix_straddr(COMSTACK h, const char *str)
160 unix_state *sp = (unix_state *)h->cprivate;
162 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
164 if (!unix_strtoaddr_ex (str, &sp->addr))
169 struct sockaddr_un *unix_strtoaddr(const char *str)
171 static struct sockaddr_un add;
173 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
175 if (!unix_strtoaddr_ex (str, &add))
180 int unix_more(COMSTACK h)
182 unix_state *sp = (unix_state *)h->cprivate;
184 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
189 * connect(2) will block (sometimes) - nothing we can do short of doing
190 * weird things like spawning subprocesses or threading or some weird junk
193 int unix_connect(COMSTACK h, void *address)
195 struct sockaddr_un *add = (struct sockaddr_un *)address;
198 TRC(fprintf(stderr, "unix_connect\n"));
200 if (h->state != CS_ST_UNBND)
202 h->cerrno = CSOUTSTATE;
205 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
208 if (errno == EINPROGRESS)
210 h->event = CS_CONNECT;
211 h->state = CS_ST_CONNECTING;
212 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
218 h->event = CS_CONNECT;
219 h->state = CS_ST_CONNECTING;
221 return unix_rcvconnect (h);
227 int unix_rcvconnect(COMSTACK h)
229 TRC(fprintf(stderr, "unix_rcvconnect\n"));
231 if (h->state == CS_ST_DATAXFER)
233 if (h->state != CS_ST_CONNECTING)
235 h->cerrno = CSOUTSTATE;
239 h->state = CS_ST_DATAXFER;
243 #define CERTF "ztest.pem"
244 #define KEYF "ztest.pem"
246 int unix_bind(COMSTACK h, void *address, int mode)
248 struct sockaddr *addr = (struct sockaddr *)address;
249 const char * path = ((struct sockaddr_un *)addr)->sun_path;
250 struct stat stat_buf;
252 TRC (fprintf (stderr, "unix_bind\n"));
254 if(stat(path, &stat_buf) != -1) {
255 struct sockaddr_un socket_unix;
257 if(! S_ISSOCK(stat_buf.st_mode)) {
259 errno = EEXIST; /* Not a socket (File exists) */
262 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
266 socket_unix.sun_family = AF_UNIX;
267 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
268 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
269 if(errno == ECONNREFUSED) {
270 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
284 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
289 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
294 h->state = CS_ST_IDLE;
295 h->event = CS_LISTEN;
299 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
300 int (*check_ip)(void *cd, const char *a, int len, int t),
303 struct sockaddr_un addr;
305 socklen_t len = SUN_LEN(&addr);
307 int len = SUN_LEN(&addr);
310 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
311 if (h->state != CS_ST_IDLE)
313 h->cerrno = CSOUTSTATE;
316 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
322 #if EAGAIN != EWOULDBLOCK
327 h->cerrno = CSNODATA;
332 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
333 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
336 h->state = CS_ST_INCON;
340 COMSTACK unix_accept(COMSTACK h)
343 unix_state *state, *st = (unix_state *)h->cprivate;
345 TRC(fprintf(stderr, "unix_accept\n"));
346 if (h->state == CS_ST_INCON)
348 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
355 memcpy(cnew, h, sizeof(*h));
356 cnew->iofile = h->newfd;
357 cnew->io_pending = 0;
358 if (!(state = (unix_state *)
359 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
369 if (!cnew->blocking &&
370 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
385 state->altsize = state->altlen = 0;
386 state->towrite = state->written = -1;
387 state->complete = st->complete;
388 cnew->state = CS_ST_ACCEPT;
389 cnew->event = CS_NONE;
390 h->state = CS_ST_IDLE;
394 if (h->state == CS_ST_ACCEPT)
399 h->cerrno = CSOUTSTATE;
403 h->state = CS_ST_DATAXFER;
408 #define CS_UNIX_BUFCHUNK 4096
411 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
412 * 0=connection closed.
414 int unix_get(COMSTACK h, char **buf, int *bufsize)
416 unix_state *sp = (unix_state *)h->cprivate;
418 int tmpi, berlen, rest, req, tomove;
419 int hasread = 0, res;
421 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
422 if (sp->altlen) /* switch buffers */
424 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
425 (unsigned) sp->altbuf));
429 *bufsize = sp->altsize;
430 hasread = sp->altlen;
436 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
440 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
443 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
444 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
446 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
447 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
450 if (errno == EWOULDBLOCK
452 #if EAGAIN != EWOULDBLOCK
456 || errno == EINPROGRESS
459 h->io_pending = CS_WANT_READ;
471 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
473 /* move surplus buffer (or everything if we didn't get a BER rec.) */
474 if (hasread > berlen)
476 tomove = req = hasread - berlen;
477 rest = tomove % CS_UNIX_BUFCHUNK;
479 req += CS_UNIX_BUFCHUNK - rest;
482 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
484 } else if (sp->altsize < req)
485 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
487 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
488 (unsigned) sp->altbuf));
489 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
491 if (berlen < CS_UNIX_BUFCHUNK - 1)
492 *(*buf + berlen) = '\0';
493 return berlen ? berlen : 1;
500 * In nonblocking mode, you must call again with same buffer while
503 int unix_put(COMSTACK h, char *buf, int size)
506 struct unix_state *state = (struct unix_state *)h->cprivate;
508 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
511 if (state->towrite < 0)
513 state->towrite = size;
516 else if (state->towrite != size)
518 h->cerrno = CSWRONGBUF;
521 while (state->towrite > state->written)
524 send(h->iofile, buf + state->written, size -
536 #if EAGAIN != EWOULDBLOCK
542 TRC(fprintf(stderr, " Flow control stop\n"));
543 h->io_pending = CS_WANT_WRITE;
549 state->written += res;
550 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
551 res, state->written, size));
553 state->towrite = state->written = -1;
554 TRC(fprintf(stderr, " Ok\n"));
560 int unix_close(COMSTACK h)
562 unix_state *sp = (struct unix_state *)h->cprivate;
564 TRC(fprintf(stderr, "unix_close\n"));
576 char *unix_addrstr(COMSTACK h)
578 unix_state *sp = (struct unix_state *)h->cprivate;
580 sprintf(buf, "unix:%s", sp->addr.sun_path);
584 int static unix_set_blocking(COMSTACK p, int blocking)
588 if (p->blocking == blocking)
590 flag = fcntl(p->iofile, F_GETFL, 0);
592 flag = flag & ~O_NONBLOCK;
594 flag = flag | O_NONBLOCK;
595 if (fcntl(p->iofile, F_SETFL, flag) < 0)
597 p->blocking = blocking;