1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements UNIX domain socket COMSTACK
20 #include <sys/types.h>
26 #include <sys/socket.h>
44 #include <yaz/errno.h>
47 #define YAZ_SOCKLEN_T int
50 /* stat(2) masks: S_IFMT and S_IFSOCK may not be defined in gcc -ansi mode */
53 #define S_IFMT 0170000
54 #define S_IFSOCK 0140000
58 static void unix_close(COMSTACK h);
59 static int unix_put(COMSTACK h, char *buf, int size);
60 static int unix_get(COMSTACK h, char **buf, int *bufsize);
61 static int unix_connect(COMSTACK h, void *address);
62 static int unix_more(COMSTACK h);
63 static int unix_rcvconnect(COMSTACK h);
64 static int unix_bind(COMSTACK h, void *address, int mode);
65 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
66 int (*check_ip)(void *cd, const char *a, int len, int type),
68 static int unix_set_blocking(COMSTACK p, int blocking);
70 static COMSTACK unix_accept(COMSTACK h);
71 static const char *unix_addrstr(COMSTACK h);
72 static void *unix_straddr(COMSTACK h, const char *str);
75 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
76 + strlen ((ptr)->sun_path))
84 /* this state is used for both SSL and straight TCP/IP */
85 typedef struct unix_state
87 char *altbuf; /* alternate buffer for surplus data */
88 int altsize; /* size as xmalloced */
89 int altlen; /* length of data or 0 if none */
91 int written; /* -1 if we aren't writing */
92 int towrite; /* to verify against user input */
93 int (*complete)(const char *buf, int len); /* length/complete. */
94 struct sockaddr_un addr; /* returned by cs_straddr */
98 char buf[128]; /* returned by cs_addrstr */
101 static int unix_init (void)
107 * This function is always called through the cs_create() macro.
108 * s >= 0: socket has already been established for us.
110 COMSTACK unix_type(int s, int flags, int protocol, void *vp)
120 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
126 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
128 if (!(state = (struct unix_state *)(p->cprivate =
129 xmalloc(sizeof(unix_state)))))
133 if (!(p->flags&CS_FLAGS_BLOCKING))
135 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
138 signal (SIGPIPE, SIG_IGN);
145 p->protocol = (enum oid_proto) protocol;
147 p->f_connect = unix_connect;
148 p->f_rcvconnect = unix_rcvconnect;
151 p->f_close = unix_close;
152 p->f_more = unix_more;
153 p->f_bind = unix_bind;
154 p->f_listen = unix_listen;
155 p->f_accept = unix_accept;
156 p->f_addrstr = unix_addrstr;
157 p->f_straddr = unix_straddr;
158 p->f_set_blocking = unix_set_blocking;
160 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
166 state->altsize = state->altlen = 0;
167 state->towrite = state->written = -1;
168 state->complete = cs_complete_auto;
170 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
176 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
181 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
182 add->sun_family = AF_UNIX;
183 strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
184 add->sun_path[sizeof(add->sun_path)-1] = 0;
185 cp = strchr (add->sun_path, ':');
191 static void *unix_straddr1(COMSTACK h, const char *str, char *f)
193 unix_state *sp = (unix_state *)h->cprivate;
195 const char * file = NULL;
197 sp->uid = sp->gid = sp->umask = -1;
204 if ((eol = strchr(s, ',')))
206 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
209 if (strspn(arg, "0123456789") == strlen(arg))
215 struct passwd * pw = getpwnam(arg);
218 printf("No such user\n");
221 sp->uid = pw->pw_uid;
224 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
227 if (strspn(arg, "0123456789") == strlen(arg))
233 struct group * gr = getgrnam(arg);
236 printf("No such group\n");
239 sp->gid = gr->gr_gid;
242 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
247 sp->umask = strtol(arg, &end, 8);
248 if (errno == EINVAL ||
251 printf("Invalid umask\n");
255 else if (file == NULL && strncmp(s, "file=", 5) == 0)
262 printf("invalid or double argument: %s\n", s);
277 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
279 if (!unix_strtoaddr_ex (file, &sp->addr))
284 static void *unix_straddr(COMSTACK h, const char *str)
286 char *f = xstrdup(str);
287 void *vp = unix_straddr1(h, str, f);
292 struct sockaddr_un *unix_strtoaddr(const char *str)
294 static struct sockaddr_un add;
296 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
298 if (!unix_strtoaddr_ex (str, &add))
303 static int unix_more(COMSTACK h)
305 unix_state *sp = (unix_state *)h->cprivate;
307 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
311 * connect(2) will block (sometimes) - nothing we can do short of doing
312 * weird things like spawning subprocesses or threading or some weird junk
315 static int unix_connect(COMSTACK h, void *address)
317 struct sockaddr_un *add = (struct sockaddr_un *)address;
321 TRC(fprintf(stderr, "unix_connect\n"));
323 if (h->state != CS_ST_UNBND)
325 h->cerrno = CSOUTSTATE;
328 for (i = 0; i<3; i++)
330 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
331 if (r < 0 && yaz_errno() == EAGAIN)
334 usleep(i*10000+1000); /* 1ms, 11ms, 21ms */
345 if (yaz_errno() == EINPROGRESS)
347 h->event = CS_CONNECT;
348 h->state = CS_ST_CONNECTING;
349 h->io_pending = CS_WANT_WRITE;
355 h->event = CS_CONNECT;
356 h->state = CS_ST_CONNECTING;
358 return unix_rcvconnect (h);
364 static int unix_rcvconnect(COMSTACK h)
366 TRC(fprintf(stderr, "unix_rcvconnect\n"));
368 if (h->state == CS_ST_DATAXFER)
370 if (h->state != CS_ST_CONNECTING)
372 h->cerrno = CSOUTSTATE;
376 h->state = CS_ST_DATAXFER;
380 static int unix_bind(COMSTACK h, void *address, int mode)
382 unix_state *sp = (unix_state *)h->cprivate;
383 struct sockaddr *addr = (struct sockaddr *)address;
384 const char * path = ((struct sockaddr_un *)addr)->sun_path;
385 struct stat stat_buf;
387 TRC (fprintf (stderr, "unix_bind\n"));
389 if(stat(path, &stat_buf) != -1) {
390 struct sockaddr_un socket_unix;
393 if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
395 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
398 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
402 socket_unix.sun_family = AF_UNIX;
403 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
404 socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
405 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
406 if(yaz_errno() == ECONNREFUSED) {
407 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
415 yaz_set_errno(EADDRINUSE);
421 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
426 if (chown(path, sp->uid, sp->gid))
431 if (chmod(path, sp->umask != -1 ? sp->umask : 0666))
436 if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
441 h->state = CS_ST_IDLE;
442 h->event = CS_LISTEN;
446 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
447 int (*check_ip)(void *cd, const char *a, int len, int t),
450 struct sockaddr_un addr;
451 YAZ_SOCKLEN_T len = sizeof(addr);
453 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
454 if (h->state != CS_ST_IDLE)
456 h->cerrno = CSOUTSTATE;
459 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
463 yaz_errno() == EWOULDBLOCK
465 #if EAGAIN != EWOULDBLOCK
466 || yaz_errno() == EAGAIN
470 h->cerrno = CSNODATA;
475 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
476 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
479 h->state = CS_ST_INCON;
483 static COMSTACK unix_accept(COMSTACK h)
486 unix_state *state, *st = (unix_state *)h->cprivate;
488 TRC(fprintf(stderr, "unix_accept\n"));
489 if (h->state == CS_ST_INCON)
491 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
498 memcpy(cnew, h, sizeof(*h));
499 cnew->iofile = h->newfd;
500 cnew->io_pending = 0;
501 if (!(state = (unix_state *)
502 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
512 if (!(cnew->flags&CS_FLAGS_BLOCKING) &&
513 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
528 state->altsize = state->altlen = 0;
529 state->towrite = state->written = -1;
530 state->complete = st->complete;
531 memcpy(&state->addr, &st->addr, sizeof(state->addr));
532 cnew->state = CS_ST_ACCEPT;
533 cnew->event = CS_NONE;
534 h->state = CS_ST_IDLE;
538 if (h->state == CS_ST_ACCEPT)
543 h->cerrno = CSOUTSTATE;
547 h->state = CS_ST_DATAXFER;
552 #define CS_UNIX_BUFCHUNK 4096
555 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
556 * 0=connection closed.
558 static int unix_get(COMSTACK h, char **buf, int *bufsize)
560 unix_state *sp = (unix_state *)h->cprivate;
562 int tmpi, berlen, rest, req, tomove;
563 int hasread = 0, res;
565 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
566 if (sp->altlen) /* switch buffers */
568 TRC(fprintf(stderr, " %d bytes in altbuf (%p )\n", sp->altlen,
573 *bufsize = sp->altsize;
574 hasread = sp->altlen;
580 while (!(berlen = (*sp->complete)(*buf, hasread)))
584 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
587 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
588 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
590 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
591 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
594 if (yaz_errno() == EWOULDBLOCK
596 #if EAGAIN != EWOULDBLOCK
597 || yaz_errno() == EAGAIN
600 || yaz_errno() == EINPROGRESS
603 h->io_pending = CS_WANT_READ;
606 else if (yaz_errno() == 0)
615 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
617 /* move surplus buffer (or everything if we didn't get a BER rec.) */
618 if (hasread > berlen)
620 tomove = req = hasread - berlen;
621 rest = tomove % CS_UNIX_BUFCHUNK;
623 req += CS_UNIX_BUFCHUNK - rest;
626 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
628 } else if (sp->altsize < req)
629 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
631 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
633 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
635 if (berlen < CS_UNIX_BUFCHUNK - 1)
636 *(*buf + berlen) = '\0';
637 return berlen ? berlen : 1;
644 * In nonblocking mode, you must call again with same buffer while
647 static int unix_put(COMSTACK h, char *buf, int size)
650 struct unix_state *state = (struct unix_state *)h->cprivate;
652 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
655 if (state->towrite < 0)
657 state->towrite = size;
660 else if (state->towrite != size)
662 h->cerrno = CSWRONGBUF;
665 while (state->towrite > state->written)
668 send(h->iofile, buf + state->written, size -
678 yaz_errno() == EWOULDBLOCK
680 #if EAGAIN != EWOULDBLOCK
681 || yaz_errno() == EAGAIN
686 TRC(fprintf(stderr, " Flow control stop\n"));
687 h->io_pending = CS_WANT_WRITE;
693 state->written += res;
694 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
695 res, state->written, size));
697 state->towrite = state->written = -1;
698 TRC(fprintf(stderr, " Ok\n"));
702 static void unix_close(COMSTACK h)
704 unix_state *sp = (struct unix_state *)h->cprivate;
706 TRC(fprintf(stderr, "unix_close\n"));
717 static const char *unix_addrstr(COMSTACK h)
719 unix_state *sp = (struct unix_state *)h->cprivate;
721 sprintf(buf, "unix:%s", sp->addr.sun_path);
725 static int unix_set_blocking(COMSTACK p, int flags)
729 if (p->flags == flags)
731 flag = fcntl(p->iofile, F_GETFL, 0);
732 if (flags & CS_FLAGS_BLOCKING)
733 flag = flag & ~O_NONBLOCK;
735 flag = flag | O_NONBLOCK;
736 if (fcntl(p->iofile, F_SETFL, flag) < 0)
745 * c-file-style: "Stroustrup"
746 * indent-tabs-mode: nil
748 * vim: shiftwidth=4 tabstop=8 expandtab