2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.3 2003-12-30 00:29:53 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 #define CERTF "ztest.pem"
347 #define KEYF "ztest.pem"
349 static int unix_bind(COMSTACK h, void *address, int mode)
351 unix_state *sp = (unix_state *)h->cprivate;
352 struct sockaddr *addr = (struct sockaddr *)address;
353 const char * path = ((struct sockaddr_un *)addr)->sun_path;
354 struct stat stat_buf;
356 TRC (fprintf (stderr, "unix_bind\n"));
358 if(stat(path, &stat_buf) != -1) {
359 struct sockaddr_un socket_unix;
361 if(! S_ISSOCK(stat_buf.st_mode)) {
363 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
366 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
370 socket_unix.sun_family = AF_UNIX;
371 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
372 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
373 if(yaz_errno() == ECONNREFUSED) {
374 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
382 yaz_set_errno(EADDRINUSE);
388 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
393 chown(path, sp->uid, sp->gid);
394 chmod(path, sp->umask != -1 ? sp->umask : 0666);
395 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
400 h->state = CS_ST_IDLE;
401 h->event = CS_LISTEN;
405 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
406 int (*check_ip)(void *cd, const char *a, int len, int t),
409 struct sockaddr_un addr;
410 YAZ_SOCKLEN_T len = sizeof(addr);
412 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
413 if (h->state != CS_ST_IDLE)
415 h->cerrno = CSOUTSTATE;
418 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
422 yaz_errno() == EWOULDBLOCK
424 #if EAGAIN != EWOULDBLOCK
425 || yaz_errno() == EAGAIN
429 h->cerrno = CSNODATA;
434 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
435 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
438 h->state = CS_ST_INCON;
442 static COMSTACK unix_accept(COMSTACK h)
445 unix_state *state, *st = (unix_state *)h->cprivate;
447 TRC(fprintf(stderr, "unix_accept\n"));
448 if (h->state == CS_ST_INCON)
450 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
457 memcpy(cnew, h, sizeof(*h));
458 cnew->iofile = h->newfd;
459 cnew->io_pending = 0;
460 if (!(state = (unix_state *)
461 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
471 if (!(cnew->blocking&1) &&
472 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
487 state->altsize = state->altlen = 0;
488 state->towrite = state->written = -1;
489 state->complete = st->complete;
490 memcpy(&state->addr, &st->addr, sizeof(state->addr));
491 cnew->state = CS_ST_ACCEPT;
492 cnew->event = CS_NONE;
493 h->state = CS_ST_IDLE;
497 if (h->state == CS_ST_ACCEPT)
502 h->cerrno = CSOUTSTATE;
506 h->state = CS_ST_DATAXFER;
511 #define CS_UNIX_BUFCHUNK 4096
514 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
515 * 0=connection closed.
517 static int unix_get(COMSTACK h, char **buf, int *bufsize)
519 unix_state *sp = (unix_state *)h->cprivate;
521 int tmpi, berlen, rest, req, tomove;
522 int hasread = 0, res;
524 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
525 if (sp->altlen) /* switch buffers */
527 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
528 (unsigned) sp->altbuf));
532 *bufsize = sp->altsize;
533 hasread = sp->altlen;
539 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
543 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
546 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
547 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
549 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
550 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
553 if (yaz_errno() == EWOULDBLOCK
555 #if EAGAIN != EWOULDBLOCK
556 || yaz_errno() == EAGAIN
559 || yaz_errno() == EINPROGRESS
562 h->io_pending = CS_WANT_READ;
565 else if (yaz_errno() == 0)
574 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
576 /* move surplus buffer (or everything if we didn't get a BER rec.) */
577 if (hasread > berlen)
579 tomove = req = hasread - berlen;
580 rest = tomove % CS_UNIX_BUFCHUNK;
582 req += CS_UNIX_BUFCHUNK - rest;
585 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
587 } else if (sp->altsize < req)
588 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
590 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
591 (unsigned) sp->altbuf));
592 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
594 if (berlen < CS_UNIX_BUFCHUNK - 1)
595 *(*buf + berlen) = '\0';
596 return berlen ? berlen : 1;
603 * In nonblocking mode, you must call again with same buffer while
606 static int unix_put(COMSTACK h, char *buf, int size)
609 struct unix_state *state = (struct unix_state *)h->cprivate;
611 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
614 if (state->towrite < 0)
616 state->towrite = size;
619 else if (state->towrite != size)
621 h->cerrno = CSWRONGBUF;
624 while (state->towrite > state->written)
627 send(h->iofile, buf + state->written, size -
637 yaz_errno() == EWOULDBLOCK
639 #if EAGAIN != EWOULDBLOCK
640 || yaz_errno() == EAGAIN
645 TRC(fprintf(stderr, " Flow control stop\n"));
646 h->io_pending = CS_WANT_WRITE;
652 state->written += res;
653 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
654 res, state->written, size));
656 state->towrite = state->written = -1;
657 TRC(fprintf(stderr, " Ok\n"));
661 static int unix_close(COMSTACK h)
663 unix_state *sp = (struct unix_state *)h->cprivate;
665 TRC(fprintf(stderr, "unix_close\n"));
677 static char *unix_addrstr(COMSTACK h)
679 unix_state *sp = (struct unix_state *)h->cprivate;
681 sprintf(buf, "unix:%s", sp->addr.sun_path);
685 static int unix_set_blocking(COMSTACK p, int blocking)
689 if (p->blocking == blocking)
691 flag = fcntl(p->iofile, F_GETFL, 0);
693 flag = flag & ~O_NONBLOCK;
695 flag = flag | O_NONBLOCK;
696 if (fcntl(p->iofile, F_SETFL, flag) < 0)
698 p->blocking = blocking;