1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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>
41 #include <yaz/errno.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 void 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 const 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 */
163 state->altsize = state->altlen = 0;
164 state->towrite = state->written = -1;
165 if (protocol == PROTO_WAIS)
166 state->complete = completeWAIS;
168 state->complete = cs_complete_auto;
170 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
171 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
177 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
182 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
183 add->sun_family = AF_UNIX;
184 strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
185 add->sun_path[sizeof(add->sun_path)-1] = 0;
186 cp = strchr (add->sun_path, ':');
192 static void *unix_straddr1(COMSTACK h, const char *str, char *f)
194 unix_state *sp = (unix_state *)h->cprivate;
196 const char * file = NULL;
199 sp->uid = sp->gid = sp->umask = -1;
201 if ((eol = strchr(s, ',')))
205 if ((eol = strchr(s, ',')))
207 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
210 if (strspn(arg, "0123456789") == strlen(arg))
216 struct passwd * pw = getpwnam(arg);
219 printf("No such user\n");
222 sp->uid = pw->pw_uid;
225 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
228 if (strspn(arg, "0123456789") == strlen(arg))
234 struct group * gr = getgrnam(arg);
237 printf("No such group\n");
240 sp->gid = gr->gr_gid;
243 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
248 sp->umask = strtol(arg, &end, 8);
249 if (errno == EINVAL ||
252 printf("Invalid umask\n");
256 else if (file == NULL && strncmp(s, "file=", 5) == 0)
263 printf("invalid or double argument: %s\n", s);
278 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
280 if (!unix_strtoaddr_ex (file, &sp->addr))
285 static void *unix_straddr(COMSTACK h, const char *str)
287 char *f = xstrdup(str);
288 void *vp = unix_straddr1(h, str, f);
293 struct sockaddr_un *unix_strtoaddr(const char *str)
295 static struct sockaddr_un add;
297 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
299 if (!unix_strtoaddr_ex (str, &add))
304 static int unix_more(COMSTACK h)
306 unix_state *sp = (unix_state *)h->cprivate;
308 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
312 * connect(2) will block (sometimes) - nothing we can do short of doing
313 * weird things like spawning subprocesses or threading or some weird junk
316 static int unix_connect(COMSTACK h, void *address)
318 struct sockaddr_un *add = (struct sockaddr_un *)address;
322 TRC(fprintf(stderr, "unix_connect\n"));
324 if (h->state != CS_ST_UNBND)
326 h->cerrno = CSOUTSTATE;
329 for (i = 0; i<3; i++)
331 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
332 if (r < 0 && yaz_errno() == EAGAIN)
335 usleep(i*10000+1000); /* 1ms, 11ms, 21ms */
346 if (yaz_errno() == EINPROGRESS)
348 h->event = CS_CONNECT;
349 h->state = CS_ST_CONNECTING;
350 h->io_pending = CS_WANT_WRITE;
356 h->event = CS_CONNECT;
357 h->state = CS_ST_CONNECTING;
359 return unix_rcvconnect (h);
365 static int unix_rcvconnect(COMSTACK h)
367 TRC(fprintf(stderr, "unix_rcvconnect\n"));
369 if (h->state == CS_ST_DATAXFER)
371 if (h->state != CS_ST_CONNECTING)
373 h->cerrno = CSOUTSTATE;
377 h->state = CS_ST_DATAXFER;
381 static int unix_bind(COMSTACK h, void *address, int mode)
383 unix_state *sp = (unix_state *)h->cprivate;
384 struct sockaddr *addr = (struct sockaddr *)address;
385 const char * path = ((struct sockaddr_un *)addr)->sun_path;
386 struct stat stat_buf;
388 TRC (fprintf (stderr, "unix_bind\n"));
390 if(stat(path, &stat_buf) != -1) {
391 struct sockaddr_un socket_unix;
394 if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
396 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
399 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
403 socket_unix.sun_family = AF_UNIX;
404 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
405 socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
406 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
407 if(yaz_errno() == ECONNREFUSED) {
408 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
416 yaz_set_errno(EADDRINUSE);
422 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
427 if (chown(path, sp->uid, sp->gid))
432 if (chmod(path, sp->umask != -1 ? sp->umask : 0666))
437 if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
442 h->state = CS_ST_IDLE;
443 h->event = CS_LISTEN;
447 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
448 int (*check_ip)(void *cd, const char *a, int len, int t),
451 struct sockaddr_un addr;
452 YAZ_SOCKLEN_T len = sizeof(addr);
454 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
455 if (h->state != CS_ST_IDLE)
457 h->cerrno = CSOUTSTATE;
460 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
464 yaz_errno() == EWOULDBLOCK
466 #if EAGAIN != EWOULDBLOCK
467 || yaz_errno() == EAGAIN
471 h->cerrno = CSNODATA;
476 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
477 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
480 h->state = CS_ST_INCON;
484 static COMSTACK unix_accept(COMSTACK h)
487 unix_state *state, *st = (unix_state *)h->cprivate;
489 TRC(fprintf(stderr, "unix_accept\n"));
490 if (h->state == CS_ST_INCON)
492 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
499 memcpy(cnew, h, sizeof(*h));
500 cnew->iofile = h->newfd;
501 cnew->io_pending = 0;
502 if (!(state = (unix_state *)
503 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
513 if (!(cnew->flags&CS_FLAGS_BLOCKING) &&
514 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
529 state->altsize = state->altlen = 0;
530 state->towrite = state->written = -1;
531 state->complete = st->complete;
532 memcpy(&state->addr, &st->addr, sizeof(state->addr));
533 cnew->state = CS_ST_ACCEPT;
534 cnew->event = CS_NONE;
535 h->state = CS_ST_IDLE;
539 if (h->state == CS_ST_ACCEPT)
544 h->cerrno = CSOUTSTATE;
548 h->state = CS_ST_DATAXFER;
553 #define CS_UNIX_BUFCHUNK 4096
556 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
557 * 0=connection closed.
559 static int unix_get(COMSTACK h, char **buf, int *bufsize)
561 unix_state *sp = (unix_state *)h->cprivate;
563 int tmpi, berlen, rest, req, tomove;
564 int hasread = 0, res;
566 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
567 if (sp->altlen) /* switch buffers */
569 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
570 (unsigned) sp->altbuf));
574 *bufsize = sp->altsize;
575 hasread = sp->altlen;
581 while (!(berlen = (*sp->complete)(*buf, hasread)))
585 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
588 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
589 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
591 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
592 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
595 if (yaz_errno() == EWOULDBLOCK
597 #if EAGAIN != EWOULDBLOCK
598 || yaz_errno() == EAGAIN
601 || yaz_errno() == EINPROGRESS
604 h->io_pending = CS_WANT_READ;
607 else if (yaz_errno() == 0)
616 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
618 /* move surplus buffer (or everything if we didn't get a BER rec.) */
619 if (hasread > berlen)
621 tomove = req = hasread - berlen;
622 rest = tomove % CS_UNIX_BUFCHUNK;
624 req += CS_UNIX_BUFCHUNK - rest;
627 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
629 } else if (sp->altsize < req)
630 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
632 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
633 (unsigned) sp->altbuf));
634 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
636 if (berlen < CS_UNIX_BUFCHUNK - 1)
637 *(*buf + berlen) = '\0';
638 return berlen ? berlen : 1;
645 * In nonblocking mode, you must call again with same buffer while
648 static int unix_put(COMSTACK h, char *buf, int size)
651 struct unix_state *state = (struct unix_state *)h->cprivate;
653 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
656 if (state->towrite < 0)
658 state->towrite = size;
661 else if (state->towrite != size)
663 h->cerrno = CSWRONGBUF;
666 while (state->towrite > state->written)
669 send(h->iofile, buf + state->written, size -
679 yaz_errno() == EWOULDBLOCK
681 #if EAGAIN != EWOULDBLOCK
682 || yaz_errno() == EAGAIN
687 TRC(fprintf(stderr, " Flow control stop\n"));
688 h->io_pending = CS_WANT_WRITE;
694 state->written += res;
695 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
696 res, state->written, size));
698 state->towrite = state->written = -1;
699 TRC(fprintf(stderr, " Ok\n"));
703 static void unix_close(COMSTACK h)
705 unix_state *sp = (struct unix_state *)h->cprivate;
707 TRC(fprintf(stderr, "unix_close\n"));
718 static const char *unix_addrstr(COMSTACK h)
720 unix_state *sp = (struct unix_state *)h->cprivate;
722 sprintf(buf, "unix:%s", sp->addr.sun_path);
726 static int unix_set_blocking(COMSTACK p, int flags)
730 if (p->flags == flags)
732 flag = fcntl(p->iofile, F_GETFL, 0);
733 if (flags & CS_FLAGS_BLOCKING)
734 flag = flag & ~O_NONBLOCK;
736 flag = flag | O_NONBLOCK;
737 if (fcntl(p->iofile, F_SETFL, flag) < 0)
746 * c-file-style: "Stroustrup"
747 * indent-tabs-mode: nil
749 * vim: shiftwidth=4 tabstop=8 expandtab