2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.14 2003-10-08 21:47:15 adam Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
18 #include <sys/socket.h>
22 #include <yaz/comstack.h>
28 #define YAZ_SOCKLEN_T int
31 static int unix_close(COMSTACK h);
32 static int unix_put(COMSTACK h, char *buf, int size);
33 static int unix_get(COMSTACK h, char **buf, int *bufsize);
34 static int unix_connect(COMSTACK h, void *address);
35 static int unix_more(COMSTACK h);
36 static int unix_rcvconnect(COMSTACK h);
37 static int unix_bind(COMSTACK h, void *address, int mode);
38 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
39 int (*check_ip)(void *cd, const char *a, int len, int type),
41 static int unix_set_blocking(COMSTACK p, int blocking);
43 static COMSTACK unix_accept(COMSTACK h);
44 static char *unix_addrstr(COMSTACK h);
45 static void *unix_straddr(COMSTACK h, const char *str);
48 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
49 + strlen ((ptr)->sun_path))
57 /* this state is used for both SSL and straight TCP/IP */
58 typedef struct unix_state
60 char *altbuf; /* alternate buffer for surplus data */
61 int altsize; /* size as xmalloced */
62 int altlen; /* length of data or 0 if none */
64 int written; /* -1 if we aren't writing */
65 int towrite; /* to verify against user input */
66 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
67 struct sockaddr_un addr; /* returned by cs_straddr */
68 char buf[128]; /* returned by cs_addrstr */
71 static int unix_init (void)
77 * This function is always called through the cs_create() macro.
78 * s >= 0: socket has already been established for us.
80 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
90 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
96 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
98 if (!(state = (struct unix_state *)(p->cprivate =
99 xmalloc(sizeof(unix_state)))))
102 if (!((p->blocking = blocking)&1))
104 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
107 signal (SIGPIPE, SIG_IGN);
114 p->protocol = (enum oid_proto) protocol;
116 p->f_connect = unix_connect;
117 p->f_rcvconnect = unix_rcvconnect;
120 p->f_close = unix_close;
121 p->f_more = unix_more;
122 p->f_bind = unix_bind;
123 p->f_listen = unix_listen;
124 p->f_accept = unix_accept;
125 p->f_addrstr = unix_addrstr;
126 p->f_straddr = unix_straddr;
127 p->f_set_blocking = unix_set_blocking;
129 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
135 state->altsize = state->altlen = 0;
136 state->towrite = state->written = -1;
137 if (protocol == PROTO_WAIS)
138 state->complete = completeWAIS;
140 state->complete = cs_complete_auto;
142 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
143 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
149 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
154 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
155 add->sun_family = AF_UNIX;
156 strncpy(add->sun_path, str, sizeof(add->sun_path));
157 cp = strchr (add->sun_path, ':');
163 static void *unix_straddr(COMSTACK h, const char *str)
165 unix_state *sp = (unix_state *)h->cprivate;
167 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
169 if (!unix_strtoaddr_ex (str, &sp->addr))
174 struct sockaddr_un *unix_strtoaddr(const char *str)
176 static struct sockaddr_un add;
178 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
180 if (!unix_strtoaddr_ex (str, &add))
185 static int unix_more(COMSTACK h)
187 unix_state *sp = (unix_state *)h->cprivate;
189 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
194 * connect(2) will block (sometimes) - nothing we can do short of doing
195 * weird things like spawning subprocesses or threading or some weird junk
198 static int unix_connect(COMSTACK h, void *address)
200 struct sockaddr_un *add = (struct sockaddr_un *)address;
203 TRC(fprintf(stderr, "unix_connect\n"));
205 if (h->state != CS_ST_UNBND)
207 h->cerrno = CSOUTSTATE;
210 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
213 if (yaz_errno() == EINPROGRESS)
215 h->event = CS_CONNECT;
216 h->state = CS_ST_CONNECTING;
217 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
223 h->event = CS_CONNECT;
224 h->state = CS_ST_CONNECTING;
226 return unix_rcvconnect (h);
232 static int unix_rcvconnect(COMSTACK h)
234 TRC(fprintf(stderr, "unix_rcvconnect\n"));
236 if (h->state == CS_ST_DATAXFER)
238 if (h->state != CS_ST_CONNECTING)
240 h->cerrno = CSOUTSTATE;
244 h->state = CS_ST_DATAXFER;
248 #define CERTF "ztest.pem"
249 #define KEYF "ztest.pem"
251 static int unix_bind(COMSTACK h, void *address, int mode)
253 struct sockaddr *addr = (struct sockaddr *)address;
254 const char * path = ((struct sockaddr_un *)addr)->sun_path;
255 struct stat stat_buf;
257 TRC (fprintf (stderr, "unix_bind\n"));
259 if(stat(path, &stat_buf) != -1) {
260 struct sockaddr_un socket_unix;
262 if(! S_ISSOCK(stat_buf.st_mode)) {
264 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
267 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
271 socket_unix.sun_family = AF_UNIX;
272 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
273 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
274 if(yaz_errno() == ECONNREFUSED) {
275 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
283 yaz_set_errno(EADDRINUSE);
289 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
295 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
300 h->state = CS_ST_IDLE;
301 h->event = CS_LISTEN;
305 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
306 int (*check_ip)(void *cd, const char *a, int len, int t),
309 struct sockaddr_un addr;
310 YAZ_SOCKLEN_T len = sizeof(addr);
312 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
313 if (h->state != CS_ST_IDLE)
315 h->cerrno = CSOUTSTATE;
318 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
322 yaz_errno() == EWOULDBLOCK
324 #if EAGAIN != EWOULDBLOCK
325 || yaz_errno() == EAGAIN
329 h->cerrno = CSNODATA;
334 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
335 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
338 h->state = CS_ST_INCON;
342 static COMSTACK unix_accept(COMSTACK h)
345 unix_state *state, *st = (unix_state *)h->cprivate;
347 TRC(fprintf(stderr, "unix_accept\n"));
348 if (h->state == CS_ST_INCON)
350 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
357 memcpy(cnew, h, sizeof(*h));
358 cnew->iofile = h->newfd;
359 cnew->io_pending = 0;
360 if (!(state = (unix_state *)
361 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
371 if (!(cnew->blocking&1) &&
372 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
387 state->altsize = state->altlen = 0;
388 state->towrite = state->written = -1;
389 state->complete = st->complete;
390 memcpy(&state->addr, &st->addr, sizeof(state->addr));
391 cnew->state = CS_ST_ACCEPT;
392 cnew->event = CS_NONE;
393 h->state = CS_ST_IDLE;
397 if (h->state == CS_ST_ACCEPT)
402 h->cerrno = CSOUTSTATE;
406 h->state = CS_ST_DATAXFER;
411 #define CS_UNIX_BUFCHUNK 4096
414 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
415 * 0=connection closed.
417 static int unix_get(COMSTACK h, char **buf, int *bufsize)
419 unix_state *sp = (unix_state *)h->cprivate;
421 int tmpi, berlen, rest, req, tomove;
422 int hasread = 0, res;
424 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
425 if (sp->altlen) /* switch buffers */
427 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
428 (unsigned) sp->altbuf));
432 *bufsize = sp->altsize;
433 hasread = sp->altlen;
439 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
443 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
446 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
447 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
449 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
450 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
453 if (yaz_errno() == EWOULDBLOCK
455 #if EAGAIN != EWOULDBLOCK
456 || yaz_errno() == EAGAIN
459 || yaz_errno() == EINPROGRESS
462 h->io_pending = CS_WANT_READ;
465 else if (yaz_errno() == 0)
474 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
476 /* move surplus buffer (or everything if we didn't get a BER rec.) */
477 if (hasread > berlen)
479 tomove = req = hasread - berlen;
480 rest = tomove % CS_UNIX_BUFCHUNK;
482 req += CS_UNIX_BUFCHUNK - rest;
485 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
487 } else if (sp->altsize < req)
488 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
490 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
491 (unsigned) sp->altbuf));
492 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
494 if (berlen < CS_UNIX_BUFCHUNK - 1)
495 *(*buf + berlen) = '\0';
496 return berlen ? berlen : 1;
503 * In nonblocking mode, you must call again with same buffer while
506 static int unix_put(COMSTACK h, char *buf, int size)
509 struct unix_state *state = (struct unix_state *)h->cprivate;
511 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
514 if (state->towrite < 0)
516 state->towrite = size;
519 else if (state->towrite != size)
521 h->cerrno = CSWRONGBUF;
524 while (state->towrite > state->written)
527 send(h->iofile, buf + state->written, size -
537 yaz_errno() == EWOULDBLOCK
539 #if EAGAIN != EWOULDBLOCK
540 || yaz_errno() == EAGAIN
545 TRC(fprintf(stderr, " Flow control stop\n"));
546 h->io_pending = CS_WANT_WRITE;
552 state->written += res;
553 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
554 res, state->written, size));
556 state->towrite = state->written = -1;
557 TRC(fprintf(stderr, " Ok\n"));
561 static int unix_close(COMSTACK h)
563 unix_state *sp = (struct unix_state *)h->cprivate;
565 TRC(fprintf(stderr, "unix_close\n"));
577 static char *unix_addrstr(COMSTACK h)
579 unix_state *sp = (struct unix_state *)h->cprivate;
581 sprintf(buf, "unix:%s", sp->addr.sun_path);
585 static int unix_set_blocking(COMSTACK p, int blocking)
589 if (p->blocking == blocking)
591 flag = fcntl(p->iofile, F_GETFL, 0);
593 flag = flag & ~O_NONBLOCK;
595 flag = flag | O_NONBLOCK;
596 if (fcntl(p->iofile, F_SETFL, flag) < 0)
598 p->blocking = blocking;