1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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 if (protocol == PROTO_WAIS)
169 state->complete = completeWAIS;
171 state->complete = cs_complete_auto;
173 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
174 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
180 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
185 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
186 add->sun_family = AF_UNIX;
187 strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
188 add->sun_path[sizeof(add->sun_path)-1] = 0;
189 cp = strchr (add->sun_path, ':');
195 static void *unix_straddr1(COMSTACK h, const char *str, char *f)
197 unix_state *sp = (unix_state *)h->cprivate;
199 const char * file = NULL;
202 sp->uid = sp->gid = sp->umask = -1;
204 if ((eol = strchr(s, ',')))
208 if ((eol = strchr(s, ',')))
210 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
213 if (strspn(arg, "0123456789") == strlen(arg))
219 struct passwd * pw = getpwnam(arg);
222 printf("No such user\n");
225 sp->uid = pw->pw_uid;
228 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
231 if (strspn(arg, "0123456789") == strlen(arg))
237 struct group * gr = getgrnam(arg);
240 printf("No such group\n");
243 sp->gid = gr->gr_gid;
246 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
251 sp->umask = strtol(arg, &end, 8);
252 if (errno == EINVAL ||
255 printf("Invalid umask\n");
259 else if (file == NULL && strncmp(s, "file=", 5) == 0)
266 printf("invalid or double argument: %s\n", s);
281 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
283 if (!unix_strtoaddr_ex (file, &sp->addr))
288 static void *unix_straddr(COMSTACK h, const char *str)
290 char *f = xstrdup(str);
291 void *vp = unix_straddr1(h, str, f);
296 struct sockaddr_un *unix_strtoaddr(const char *str)
298 static struct sockaddr_un add;
300 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
302 if (!unix_strtoaddr_ex (str, &add))
307 static int unix_more(COMSTACK h)
309 unix_state *sp = (unix_state *)h->cprivate;
311 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
315 * connect(2) will block (sometimes) - nothing we can do short of doing
316 * weird things like spawning subprocesses or threading or some weird junk
319 static int unix_connect(COMSTACK h, void *address)
321 struct sockaddr_un *add = (struct sockaddr_un *)address;
325 TRC(fprintf(stderr, "unix_connect\n"));
327 if (h->state != CS_ST_UNBND)
329 h->cerrno = CSOUTSTATE;
332 for (i = 0; i<3; i++)
334 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
335 if (r < 0 && yaz_errno() == EAGAIN)
338 usleep(i*10000+1000); /* 1ms, 11ms, 21ms */
349 if (yaz_errno() == EINPROGRESS)
351 h->event = CS_CONNECT;
352 h->state = CS_ST_CONNECTING;
353 h->io_pending = CS_WANT_WRITE;
359 h->event = CS_CONNECT;
360 h->state = CS_ST_CONNECTING;
362 return unix_rcvconnect (h);
368 static int unix_rcvconnect(COMSTACK h)
370 TRC(fprintf(stderr, "unix_rcvconnect\n"));
372 if (h->state == CS_ST_DATAXFER)
374 if (h->state != CS_ST_CONNECTING)
376 h->cerrno = CSOUTSTATE;
380 h->state = CS_ST_DATAXFER;
384 static int unix_bind(COMSTACK h, void *address, int mode)
386 unix_state *sp = (unix_state *)h->cprivate;
387 struct sockaddr *addr = (struct sockaddr *)address;
388 const char * path = ((struct sockaddr_un *)addr)->sun_path;
389 struct stat stat_buf;
391 TRC (fprintf (stderr, "unix_bind\n"));
393 if(stat(path, &stat_buf) != -1) {
394 struct sockaddr_un socket_unix;
397 if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
399 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
402 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
406 socket_unix.sun_family = AF_UNIX;
407 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
408 socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
409 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
410 if(yaz_errno() == ECONNREFUSED) {
411 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
419 yaz_set_errno(EADDRINUSE);
425 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
430 if (chown(path, sp->uid, sp->gid))
435 if (chmod(path, sp->umask != -1 ? sp->umask : 0666))
440 if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
445 h->state = CS_ST_IDLE;
446 h->event = CS_LISTEN;
450 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
451 int (*check_ip)(void *cd, const char *a, int len, int t),
454 struct sockaddr_un addr;
455 YAZ_SOCKLEN_T len = sizeof(addr);
457 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
458 if (h->state != CS_ST_IDLE)
460 h->cerrno = CSOUTSTATE;
463 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
467 yaz_errno() == EWOULDBLOCK
469 #if EAGAIN != EWOULDBLOCK
470 || yaz_errno() == EAGAIN
474 h->cerrno = CSNODATA;
479 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
480 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
483 h->state = CS_ST_INCON;
487 static COMSTACK unix_accept(COMSTACK h)
490 unix_state *state, *st = (unix_state *)h->cprivate;
492 TRC(fprintf(stderr, "unix_accept\n"));
493 if (h->state == CS_ST_INCON)
495 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
502 memcpy(cnew, h, sizeof(*h));
503 cnew->iofile = h->newfd;
504 cnew->io_pending = 0;
505 if (!(state = (unix_state *)
506 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
516 if (!(cnew->flags&CS_FLAGS_BLOCKING) &&
517 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
532 state->altsize = state->altlen = 0;
533 state->towrite = state->written = -1;
534 state->complete = st->complete;
535 memcpy(&state->addr, &st->addr, sizeof(state->addr));
536 cnew->state = CS_ST_ACCEPT;
537 cnew->event = CS_NONE;
538 h->state = CS_ST_IDLE;
542 if (h->state == CS_ST_ACCEPT)
547 h->cerrno = CSOUTSTATE;
551 h->state = CS_ST_DATAXFER;
556 #define CS_UNIX_BUFCHUNK 4096
559 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
560 * 0=connection closed.
562 static int unix_get(COMSTACK h, char **buf, int *bufsize)
564 unix_state *sp = (unix_state *)h->cprivate;
566 int tmpi, berlen, rest, req, tomove;
567 int hasread = 0, res;
569 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
570 if (sp->altlen) /* switch buffers */
572 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
573 (unsigned) sp->altbuf));
577 *bufsize = sp->altsize;
578 hasread = sp->altlen;
584 while (!(berlen = (*sp->complete)(*buf, hasread)))
588 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
591 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
592 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
594 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
595 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
598 if (yaz_errno() == EWOULDBLOCK
600 #if EAGAIN != EWOULDBLOCK
601 || yaz_errno() == EAGAIN
604 || yaz_errno() == EINPROGRESS
607 h->io_pending = CS_WANT_READ;
610 else if (yaz_errno() == 0)
619 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
621 /* move surplus buffer (or everything if we didn't get a BER rec.) */
622 if (hasread > berlen)
624 tomove = req = hasread - berlen;
625 rest = tomove % CS_UNIX_BUFCHUNK;
627 req += CS_UNIX_BUFCHUNK - rest;
630 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
632 } else if (sp->altsize < req)
633 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
635 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
636 (unsigned) sp->altbuf));
637 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
639 if (berlen < CS_UNIX_BUFCHUNK - 1)
640 *(*buf + berlen) = '\0';
641 return berlen ? berlen : 1;
648 * In nonblocking mode, you must call again with same buffer while
651 static int unix_put(COMSTACK h, char *buf, int size)
654 struct unix_state *state = (struct unix_state *)h->cprivate;
656 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
659 if (state->towrite < 0)
661 state->towrite = size;
664 else if (state->towrite != size)
666 h->cerrno = CSWRONGBUF;
669 while (state->towrite > state->written)
672 send(h->iofile, buf + state->written, size -
682 yaz_errno() == EWOULDBLOCK
684 #if EAGAIN != EWOULDBLOCK
685 || yaz_errno() == EAGAIN
690 TRC(fprintf(stderr, " Flow control stop\n"));
691 h->io_pending = CS_WANT_WRITE;
697 state->written += res;
698 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
699 res, state->written, size));
701 state->towrite = state->written = -1;
702 TRC(fprintf(stderr, " Ok\n"));
706 static void unix_close(COMSTACK h)
708 unix_state *sp = (struct unix_state *)h->cprivate;
710 TRC(fprintf(stderr, "unix_close\n"));
721 static const char *unix_addrstr(COMSTACK h)
723 unix_state *sp = (struct unix_state *)h->cprivate;
725 sprintf(buf, "unix:%s", sp->addr.sun_path);
729 static int unix_set_blocking(COMSTACK p, int flags)
733 if (p->flags == flags)
735 flag = fcntl(p->iofile, F_GETFL, 0);
736 if (flags & CS_FLAGS_BLOCKING)
737 flag = flag & ~O_NONBLOCK;
739 flag = flag | O_NONBLOCK;
740 if (fcntl(p->iofile, F_SETFL, flag) < 0)
749 * c-file-style: "Stroustrup"
750 * indent-tabs-mode: nil
752 * vim: shiftwidth=4 tabstop=8 expandtab