2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.4 2004-04-29 21:19:23 adam Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
20 #include <sys/types.h>
22 #include <sys/socket.h>
26 #include <yaz/comstack.h>
32 #define YAZ_SOCKLEN_T int
35 static int unix_close(COMSTACK h);
36 static int unix_put(COMSTACK h, char *buf, int size);
37 static int unix_get(COMSTACK h, char **buf, int *bufsize);
38 static int unix_connect(COMSTACK h, void *address);
39 static int unix_more(COMSTACK h);
40 static int unix_rcvconnect(COMSTACK h);
41 static int unix_bind(COMSTACK h, void *address, int mode);
42 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
43 int (*check_ip)(void *cd, const char *a, int len, int type),
45 static int unix_set_blocking(COMSTACK p, int blocking);
47 static COMSTACK unix_accept(COMSTACK h);
48 static char *unix_addrstr(COMSTACK h);
49 static void *unix_straddr(COMSTACK h, const char *str);
52 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
53 + strlen ((ptr)->sun_path))
61 /* this state is used for both SSL and straight TCP/IP */
62 typedef struct unix_state
64 char *altbuf; /* alternate buffer for surplus data */
65 int altsize; /* size as xmalloced */
66 int altlen; /* length of data or 0 if none */
68 int written; /* -1 if we aren't writing */
69 int towrite; /* to verify against user input */
70 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
71 struct sockaddr_un addr; /* returned by cs_straddr */
75 char buf[128]; /* returned by cs_addrstr */
78 static int unix_init (void)
84 * This function is always called through the cs_create() macro.
85 * s >= 0: socket has already been established for us.
87 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
97 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
103 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
105 if (!(state = (struct unix_state *)(p->cprivate =
106 xmalloc(sizeof(unix_state)))))
109 if (!((p->blocking = blocking)&1))
111 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
114 signal (SIGPIPE, SIG_IGN);
121 p->protocol = (enum oid_proto) protocol;
123 p->f_connect = unix_connect;
124 p->f_rcvconnect = unix_rcvconnect;
127 p->f_close = unix_close;
128 p->f_more = unix_more;
129 p->f_bind = unix_bind;
130 p->f_listen = unix_listen;
131 p->f_accept = unix_accept;
132 p->f_addrstr = unix_addrstr;
133 p->f_straddr = unix_straddr;
134 p->f_set_blocking = unix_set_blocking;
136 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
142 state->altsize = state->altlen = 0;
143 state->towrite = state->written = -1;
144 if (protocol == PROTO_WAIS)
145 state->complete = completeWAIS;
147 state->complete = cs_complete_auto;
149 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
150 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
156 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
161 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
162 add->sun_family = AF_UNIX;
163 strncpy(add->sun_path, str, sizeof(add->sun_path));
164 cp = strchr (add->sun_path, ':');
170 static void *unix_straddr(COMSTACK h, const char *str)
172 unix_state *sp = (unix_state *)h->cprivate;
173 char * s = strdup(str);
175 const char * file = NULL;
178 sp->uid = sp->gid = sp->umask = -1;
180 if ((eol = strchr(s, ',')))
184 if ((eol = strchr(s, ',')))
186 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
189 if (strspn(arg, "0123456789") == strlen(arg))
195 struct passwd * pw = getpwnam(arg);
198 printf("No such user\n");
202 sp->uid = pw->pw_uid;
205 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
208 if (strspn(arg, "0123456789") == strlen(arg))
214 struct group * gr = getgrnam(arg);
217 printf("No such group\n");
221 sp->gid = gr->gr_gid;
224 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
229 sp->umask = strtol(arg, &end, 8);
230 if (errno == EINVAL ||
233 printf("Invalid umask\n");
238 else if (file == NULL && strncmp(s, "file=", 5) == 0)
245 printf("invalid or double argument: %s\n", s);
261 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
263 if (!unix_strtoaddr_ex (file, &sp->addr))
272 struct sockaddr_un *unix_strtoaddr(const char *str)
274 static struct sockaddr_un add;
276 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
278 if (!unix_strtoaddr_ex (str, &add))
283 static int unix_more(COMSTACK h)
285 unix_state *sp = (unix_state *)h->cprivate;
287 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
292 * connect(2) will block (sometimes) - nothing we can do short of doing
293 * weird things like spawning subprocesses or threading or some weird junk
296 static int unix_connect(COMSTACK h, void *address)
298 struct sockaddr_un *add = (struct sockaddr_un *)address;
301 TRC(fprintf(stderr, "unix_connect\n"));
303 if (h->state != CS_ST_UNBND)
305 h->cerrno = CSOUTSTATE;
308 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
311 if (yaz_errno() == EINPROGRESS)
313 h->event = CS_CONNECT;
314 h->state = CS_ST_CONNECTING;
315 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
321 h->event = CS_CONNECT;
322 h->state = CS_ST_CONNECTING;
324 return unix_rcvconnect (h);
330 static int unix_rcvconnect(COMSTACK h)
332 TRC(fprintf(stderr, "unix_rcvconnect\n"));
334 if (h->state == CS_ST_DATAXFER)
336 if (h->state != CS_ST_CONNECTING)
338 h->cerrno = CSOUTSTATE;
342 h->state = CS_ST_DATAXFER;
346 static int unix_bind(COMSTACK h, void *address, int mode)
348 unix_state *sp = (unix_state *)h->cprivate;
349 struct sockaddr *addr = (struct sockaddr *)address;
350 const char * path = ((struct sockaddr_un *)addr)->sun_path;
351 struct stat stat_buf;
353 TRC (fprintf (stderr, "unix_bind\n"));
355 if(stat(path, &stat_buf) != -1) {
356 struct sockaddr_un socket_unix;
358 if(! S_ISSOCK(stat_buf.st_mode)) {
360 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
363 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
367 socket_unix.sun_family = AF_UNIX;
368 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
369 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
370 if(yaz_errno() == ECONNREFUSED) {
371 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
379 yaz_set_errno(EADDRINUSE);
385 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
390 chown(path, sp->uid, sp->gid);
391 chmod(path, sp->umask != -1 ? sp->umask : 0666);
392 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
397 h->state = CS_ST_IDLE;
398 h->event = CS_LISTEN;
402 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
403 int (*check_ip)(void *cd, const char *a, int len, int t),
406 struct sockaddr_un addr;
407 YAZ_SOCKLEN_T len = sizeof(addr);
409 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
410 if (h->state != CS_ST_IDLE)
412 h->cerrno = CSOUTSTATE;
415 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
419 yaz_errno() == EWOULDBLOCK
421 #if EAGAIN != EWOULDBLOCK
422 || yaz_errno() == EAGAIN
426 h->cerrno = CSNODATA;
431 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
432 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
435 h->state = CS_ST_INCON;
439 static COMSTACK unix_accept(COMSTACK h)
442 unix_state *state, *st = (unix_state *)h->cprivate;
444 TRC(fprintf(stderr, "unix_accept\n"));
445 if (h->state == CS_ST_INCON)
447 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
454 memcpy(cnew, h, sizeof(*h));
455 cnew->iofile = h->newfd;
456 cnew->io_pending = 0;
457 if (!(state = (unix_state *)
458 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
468 if (!(cnew->blocking&1) &&
469 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
484 state->altsize = state->altlen = 0;
485 state->towrite = state->written = -1;
486 state->complete = st->complete;
487 memcpy(&state->addr, &st->addr, sizeof(state->addr));
488 cnew->state = CS_ST_ACCEPT;
489 cnew->event = CS_NONE;
490 h->state = CS_ST_IDLE;
494 if (h->state == CS_ST_ACCEPT)
499 h->cerrno = CSOUTSTATE;
503 h->state = CS_ST_DATAXFER;
508 #define CS_UNIX_BUFCHUNK 4096
511 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
512 * 0=connection closed.
514 static int unix_get(COMSTACK h, char **buf, int *bufsize)
516 unix_state *sp = (unix_state *)h->cprivate;
518 int tmpi, berlen, rest, req, tomove;
519 int hasread = 0, res;
521 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
522 if (sp->altlen) /* switch buffers */
524 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
525 (unsigned) sp->altbuf));
529 *bufsize = sp->altsize;
530 hasread = sp->altlen;
536 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
540 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
543 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
544 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
546 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
547 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
550 if (yaz_errno() == EWOULDBLOCK
552 #if EAGAIN != EWOULDBLOCK
553 || yaz_errno() == EAGAIN
556 || yaz_errno() == EINPROGRESS
559 h->io_pending = CS_WANT_READ;
562 else if (yaz_errno() == 0)
571 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
573 /* move surplus buffer (or everything if we didn't get a BER rec.) */
574 if (hasread > berlen)
576 tomove = req = hasread - berlen;
577 rest = tomove % CS_UNIX_BUFCHUNK;
579 req += CS_UNIX_BUFCHUNK - rest;
582 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
584 } else if (sp->altsize < req)
585 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
587 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
588 (unsigned) sp->altbuf));
589 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
591 if (berlen < CS_UNIX_BUFCHUNK - 1)
592 *(*buf + berlen) = '\0';
593 return berlen ? berlen : 1;
600 * In nonblocking mode, you must call again with same buffer while
603 static int unix_put(COMSTACK h, char *buf, int size)
606 struct unix_state *state = (struct unix_state *)h->cprivate;
608 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
611 if (state->towrite < 0)
613 state->towrite = size;
616 else if (state->towrite != size)
618 h->cerrno = CSWRONGBUF;
621 while (state->towrite > state->written)
624 send(h->iofile, buf + state->written, size -
634 yaz_errno() == EWOULDBLOCK
636 #if EAGAIN != EWOULDBLOCK
637 || yaz_errno() == EAGAIN
642 TRC(fprintf(stderr, " Flow control stop\n"));
643 h->io_pending = CS_WANT_WRITE;
649 state->written += res;
650 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
651 res, state->written, size));
653 state->towrite = state->written = -1;
654 TRC(fprintf(stderr, " Ok\n"));
658 static int unix_close(COMSTACK h)
660 unix_state *sp = (struct unix_state *)h->cprivate;
662 TRC(fprintf(stderr, "unix_close\n"));
674 static char *unix_addrstr(COMSTACK h)
676 unix_state *sp = (struct unix_state *)h->cprivate;
678 sprintf(buf, "unix:%s", sp->addr.sun_path);
682 static int unix_set_blocking(COMSTACK p, int blocking)
686 if (p->blocking == blocking)
688 flag = fcntl(p->iofile, F_GETFL, 0);
690 flag = flag & ~O_NONBLOCK;
692 flag = flag | O_NONBLOCK;
693 if (fcntl(p->iofile, F_SETFL, flag) < 0)
695 p->blocking = blocking;