1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements UNIX domain socket COMSTACK
17 #include <sys/types.h>
23 #include <sys/socket.h>
44 #define YAZ_SOCKLEN_T int
47 /* stat(2) masks: S_IFMT and S_IFSOCK may not be defined in gcc -ansi mode */
50 #define S_IFMT 0170000
51 #define S_IFSOCK 0140000
55 static int unix_close(COMSTACK h);
56 static int unix_put(COMSTACK h, char *buf, int size);
57 static int unix_get(COMSTACK h, char **buf, int *bufsize);
58 static int unix_connect(COMSTACK h, void *address);
59 static int unix_more(COMSTACK h);
60 static int unix_rcvconnect(COMSTACK h);
61 static int unix_bind(COMSTACK h, void *address, int mode);
62 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
63 int (*check_ip)(void *cd, const char *a, int len, int type),
65 static int unix_set_blocking(COMSTACK p, int blocking);
67 static COMSTACK unix_accept(COMSTACK h);
68 static char *unix_addrstr(COMSTACK h);
69 static void *unix_straddr(COMSTACK h, const char *str);
72 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
73 + strlen ((ptr)->sun_path))
81 /* this state is used for both SSL and straight TCP/IP */
82 typedef struct unix_state
84 char *altbuf; /* alternate buffer for surplus data */
85 int altsize; /* size as xmalloced */
86 int altlen; /* length of data or 0 if none */
88 int written; /* -1 if we aren't writing */
89 int towrite; /* to verify against user input */
90 int (*complete)(const char *buf, int len); /* length/complete. */
91 struct sockaddr_un addr; /* returned by cs_straddr */
95 char buf[128]; /* returned by cs_addrstr */
98 static int unix_init (void)
104 * This function is always called through the cs_create() macro.
105 * s >= 0: socket has already been established for us.
107 COMSTACK unix_type(int s, int flags, int protocol, void *vp)
117 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
123 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
125 if (!(state = (struct unix_state *)(p->cprivate =
126 xmalloc(sizeof(unix_state)))))
130 if (!(p->flags&CS_FLAGS_BLOCKING))
132 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
135 signal (SIGPIPE, SIG_IGN);
142 p->protocol = (enum oid_proto) protocol;
144 p->f_connect = unix_connect;
145 p->f_rcvconnect = unix_rcvconnect;
148 p->f_close = unix_close;
149 p->f_more = unix_more;
150 p->f_bind = unix_bind;
151 p->f_listen = unix_listen;
152 p->f_accept = unix_accept;
153 p->f_addrstr = unix_addrstr;
154 p->f_straddr = unix_straddr;
155 p->f_set_blocking = unix_set_blocking;
157 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
164 state->altsize = state->altlen = 0;
165 state->towrite = state->written = -1;
166 if (protocol == PROTO_WAIS)
167 state->complete = completeWAIS;
169 state->complete = cs_complete_auto;
171 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
172 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
178 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
183 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
184 add->sun_family = AF_UNIX;
185 strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
186 add->sun_path[sizeof(add->sun_path)-1] = 0;
187 cp = strchr (add->sun_path, ':');
193 static void *unix_straddr1(COMSTACK h, const char *str, char *f)
195 unix_state *sp = (unix_state *)h->cprivate;
197 const char * file = NULL;
200 sp->uid = sp->gid = sp->umask = -1;
202 if ((eol = strchr(s, ',')))
206 if ((eol = strchr(s, ',')))
208 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
211 if (strspn(arg, "0123456789") == strlen(arg))
217 struct passwd * pw = getpwnam(arg);
220 printf("No such user\n");
223 sp->uid = pw->pw_uid;
226 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
229 if (strspn(arg, "0123456789") == strlen(arg))
235 struct group * gr = getgrnam(arg);
238 printf("No such group\n");
241 sp->gid = gr->gr_gid;
244 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
249 sp->umask = strtol(arg, &end, 8);
250 if (errno == EINVAL ||
253 printf("Invalid umask\n");
257 else if (file == NULL && strncmp(s, "file=", 5) == 0)
264 printf("invalid or double argument: %s\n", s);
279 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
281 if (!unix_strtoaddr_ex (file, &sp->addr))
286 static void *unix_straddr(COMSTACK h, const char *str)
288 char *f = xstrdup(str);
289 void *vp = unix_straddr1(h, str, f);
294 struct sockaddr_un *unix_strtoaddr(const char *str)
296 static struct sockaddr_un add;
298 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
300 if (!unix_strtoaddr_ex (str, &add))
305 static int unix_more(COMSTACK h)
307 unix_state *sp = (unix_state *)h->cprivate;
309 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
313 * connect(2) will block (sometimes) - nothing we can do short of doing
314 * weird things like spawning subprocesses or threading or some weird junk
317 static int unix_connect(COMSTACK h, void *address)
319 struct sockaddr_un *add = (struct sockaddr_un *)address;
323 TRC(fprintf(stderr, "unix_connect\n"));
325 if (h->state != CS_ST_UNBND)
327 h->cerrno = CSOUTSTATE;
330 for (i = 0; i<3; i++)
332 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
333 if (r < 0 && yaz_errno() == EAGAIN)
336 usleep(i*10000+1000); /* 1ms, 11ms, 21ms */
347 if (yaz_errno() == EINPROGRESS)
349 h->event = CS_CONNECT;
350 h->state = CS_ST_CONNECTING;
351 h->io_pending = CS_WANT_WRITE;
357 h->event = CS_CONNECT;
358 h->state = CS_ST_CONNECTING;
360 return unix_rcvconnect (h);
366 static int unix_rcvconnect(COMSTACK h)
368 TRC(fprintf(stderr, "unix_rcvconnect\n"));
370 if (h->state == CS_ST_DATAXFER)
372 if (h->state != CS_ST_CONNECTING)
374 h->cerrno = CSOUTSTATE;
378 h->state = CS_ST_DATAXFER;
382 static int unix_bind(COMSTACK h, void *address, int mode)
384 unix_state *sp = (unix_state *)h->cprivate;
385 struct sockaddr *addr = (struct sockaddr *)address;
386 const char * path = ((struct sockaddr_un *)addr)->sun_path;
387 struct stat stat_buf;
389 TRC (fprintf (stderr, "unix_bind\n"));
391 if(stat(path, &stat_buf) != -1) {
392 struct sockaddr_un socket_unix;
395 if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
397 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
400 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
404 socket_unix.sun_family = AF_UNIX;
405 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
406 socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
407 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
408 if(yaz_errno() == ECONNREFUSED) {
409 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
417 yaz_set_errno(EADDRINUSE);
423 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
428 chown(path, sp->uid, sp->gid);
429 chmod(path, sp->umask != -1 ? sp->umask : 0666);
430 if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
435 h->state = CS_ST_IDLE;
436 h->event = CS_LISTEN;
440 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
441 int (*check_ip)(void *cd, const char *a, int len, int t),
444 struct sockaddr_un addr;
445 YAZ_SOCKLEN_T len = sizeof(addr);
447 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
448 if (h->state != CS_ST_IDLE)
450 h->cerrno = CSOUTSTATE;
453 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
457 yaz_errno() == EWOULDBLOCK
459 #if EAGAIN != EWOULDBLOCK
460 || yaz_errno() == EAGAIN
464 h->cerrno = CSNODATA;
469 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
470 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
473 h->state = CS_ST_INCON;
477 static COMSTACK unix_accept(COMSTACK h)
480 unix_state *state, *st = (unix_state *)h->cprivate;
482 TRC(fprintf(stderr, "unix_accept\n"));
483 if (h->state == CS_ST_INCON)
485 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
492 memcpy(cnew, h, sizeof(*h));
493 cnew->iofile = h->newfd;
494 cnew->io_pending = 0;
495 if (!(state = (unix_state *)
496 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
506 if (!(cnew->flags&CS_FLAGS_BLOCKING) &&
507 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
522 state->altsize = state->altlen = 0;
523 state->towrite = state->written = -1;
524 state->complete = st->complete;
525 memcpy(&state->addr, &st->addr, sizeof(state->addr));
526 cnew->state = CS_ST_ACCEPT;
527 cnew->event = CS_NONE;
528 h->state = CS_ST_IDLE;
532 if (h->state == CS_ST_ACCEPT)
537 h->cerrno = CSOUTSTATE;
541 h->state = CS_ST_DATAXFER;
546 #define CS_UNIX_BUFCHUNK 4096
549 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
550 * 0=connection closed.
552 static int unix_get(COMSTACK h, char **buf, int *bufsize)
554 unix_state *sp = (unix_state *)h->cprivate;
556 int tmpi, berlen, rest, req, tomove;
557 int hasread = 0, res;
559 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
560 if (sp->altlen) /* switch buffers */
562 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
563 (unsigned) sp->altbuf));
567 *bufsize = sp->altsize;
568 hasread = sp->altlen;
574 while (!(berlen = (*sp->complete)(*buf, hasread)))
578 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
581 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
582 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
584 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
585 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
588 if (yaz_errno() == EWOULDBLOCK
590 #if EAGAIN != EWOULDBLOCK
591 || yaz_errno() == EAGAIN
594 || yaz_errno() == EINPROGRESS
597 h->io_pending = CS_WANT_READ;
600 else if (yaz_errno() == 0)
609 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
611 /* move surplus buffer (or everything if we didn't get a BER rec.) */
612 if (hasread > berlen)
614 tomove = req = hasread - berlen;
615 rest = tomove % CS_UNIX_BUFCHUNK;
617 req += CS_UNIX_BUFCHUNK - rest;
620 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
622 } else if (sp->altsize < req)
623 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
625 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
626 (unsigned) sp->altbuf));
627 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
629 if (berlen < CS_UNIX_BUFCHUNK - 1)
630 *(*buf + berlen) = '\0';
631 return berlen ? berlen : 1;
638 * In nonblocking mode, you must call again with same buffer while
641 static int unix_put(COMSTACK h, char *buf, int size)
644 struct unix_state *state = (struct unix_state *)h->cprivate;
646 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
649 if (state->towrite < 0)
651 state->towrite = size;
654 else if (state->towrite != size)
656 h->cerrno = CSWRONGBUF;
659 while (state->towrite > state->written)
662 send(h->iofile, buf + state->written, size -
672 yaz_errno() == EWOULDBLOCK
674 #if EAGAIN != EWOULDBLOCK
675 || yaz_errno() == EAGAIN
680 TRC(fprintf(stderr, " Flow control stop\n"));
681 h->io_pending = CS_WANT_WRITE;
687 state->written += res;
688 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
689 res, state->written, size));
691 state->towrite = state->written = -1;
692 TRC(fprintf(stderr, " Ok\n"));
696 static int unix_close(COMSTACK h)
698 unix_state *sp = (struct unix_state *)h->cprivate;
700 TRC(fprintf(stderr, "unix_close\n"));
712 static char *unix_addrstr(COMSTACK h)
714 unix_state *sp = (struct unix_state *)h->cprivate;
716 sprintf(buf, "unix:%s", sp->addr.sun_path);
720 static int unix_set_blocking(COMSTACK p, int flags)
724 if (p->flags == flags)
726 flag = fcntl(p->iofile, F_GETFL, 0);
727 if (flags & CS_FLAGS_BLOCKING)
728 flag = flag & ~O_NONBLOCK;
730 flag = flag | O_NONBLOCK;
731 if (fcntl(p->iofile, F_SETFL, flag) < 0)
740 * indent-tabs-mode: nil
742 * vim: shiftwidth=4 tabstop=8 expandtab