2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.9 2003-01-06 08:20:27 adam Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
18 #include <sys/socket.h>
22 #include <yaz/comstack.h>
26 /* Chas added the following, so we get the definition of completeBER */
30 #define YAZ_SOCKLEN_T int
33 static int unix_close(COMSTACK h);
34 static int unix_put(COMSTACK h, char *buf, int size);
35 static int unix_get(COMSTACK h, char **buf, int *bufsize);
36 static int unix_connect(COMSTACK h, void *address);
37 static int unix_more(COMSTACK h);
38 static int unix_rcvconnect(COMSTACK h);
39 static int unix_bind(COMSTACK h, void *address, int mode);
40 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
41 int (*check_ip)(void *cd, const char *a, int len, int type),
43 static int unix_set_blocking(COMSTACK p, int blocking);
46 static COMSTACK unix_accept(COMSTACK h);
47 static char *unix_addrstr(COMSTACK h);
48 static void *unix_straddr(COMSTACK h, const char *str);
51 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
52 + strlen ((ptr)->sun_path))
60 /* this state is used for both SSL and straight TCP/IP */
61 typedef struct unix_state
63 char *altbuf; /* alternate buffer for surplus data */
64 int altsize; /* size as xmalloced */
65 int altlen; /* length of data or 0 if none */
67 int written; /* -1 if we aren't writing */
68 int towrite; /* to verify against user input */
69 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
70 struct sockaddr_un addr; /* returned by cs_straddr */
71 char buf[128]; /* returned by cs_addrstr */
74 static int unix_init (void)
80 * This function is always called through the cs_create() macro.
81 * s >= 0: socket has already been established for us.
83 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
93 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
99 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
101 if (!(state = (struct unix_state *)(p->cprivate =
102 xmalloc(sizeof(unix_state)))))
105 if (!(p->blocking = blocking))
107 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
110 signal (SIGPIPE, SIG_IGN);
117 p->protocol = (enum oid_proto) protocol;
119 p->f_connect = unix_connect;
120 p->f_rcvconnect = unix_rcvconnect;
123 p->f_close = unix_close;
124 p->f_more = unix_more;
125 p->f_bind = unix_bind;
126 p->f_listen = unix_listen;
127 p->f_accept = unix_accept;
128 p->f_addrstr = unix_addrstr;
129 p->f_straddr = unix_straddr;
130 p->f_set_blocking = unix_set_blocking;
132 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
138 state->altsize = state->altlen = 0;
139 state->towrite = state->written = -1;
140 if (protocol == PROTO_WAIS)
141 state->complete = completeWAIS;
143 state->complete = completeBER;
145 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
146 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
152 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
157 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
158 add->sun_family = AF_UNIX;
159 strncpy(add->sun_path, str, sizeof(add->sun_path));
160 cp = strchr (add->sun_path, ':');
166 static void *unix_straddr(COMSTACK h, const char *str)
168 unix_state *sp = (unix_state *)h->cprivate;
170 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
172 if (!unix_strtoaddr_ex (str, &sp->addr))
177 struct sockaddr_un *unix_strtoaddr(const char *str)
179 static struct sockaddr_un add;
181 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
183 if (!unix_strtoaddr_ex (str, &add))
188 static int unix_more(COMSTACK h)
190 unix_state *sp = (unix_state *)h->cprivate;
192 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
197 * connect(2) will block (sometimes) - nothing we can do short of doing
198 * weird things like spawning subprocesses or threading or some weird junk
201 static int unix_connect(COMSTACK h, void *address)
203 struct sockaddr_un *add = (struct sockaddr_un *)address;
206 TRC(fprintf(stderr, "unix_connect\n"));
208 if (h->state != CS_ST_UNBND)
210 h->cerrno = CSOUTSTATE;
213 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
216 if (yaz_errno() == EINPROGRESS)
218 h->event = CS_CONNECT;
219 h->state = CS_ST_CONNECTING;
220 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
226 h->event = CS_CONNECT;
227 h->state = CS_ST_CONNECTING;
229 return unix_rcvconnect (h);
235 static int unix_rcvconnect(COMSTACK h)
237 TRC(fprintf(stderr, "unix_rcvconnect\n"));
239 if (h->state == CS_ST_DATAXFER)
241 if (h->state != CS_ST_CONNECTING)
243 h->cerrno = CSOUTSTATE;
247 h->state = CS_ST_DATAXFER;
251 #define CERTF "ztest.pem"
252 #define KEYF "ztest.pem"
254 static int unix_bind(COMSTACK h, void *address, int mode)
256 struct sockaddr *addr = (struct sockaddr *)address;
257 const char * path = ((struct sockaddr_un *)addr)->sun_path;
258 struct stat stat_buf;
260 TRC (fprintf (stderr, "unix_bind\n"));
262 if(stat(path, &stat_buf) != -1) {
263 struct sockaddr_un socket_unix;
265 if(! S_ISSOCK(stat_buf.st_mode)) {
267 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
270 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
274 socket_unix.sun_family = AF_UNIX;
275 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
276 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
277 if(yaz_errno() == ECONNREFUSED) {
278 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
286 yaz_set_errno(EADDRINUSE);
292 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
297 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
302 h->state = CS_ST_IDLE;
303 h->event = CS_LISTEN;
307 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
308 int (*check_ip)(void *cd, const char *a, int len, int t),
311 struct sockaddr_un addr;
312 YAZ_SOCKLEN_T len = SUN_LEN(&addr);
314 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
315 if (h->state != CS_ST_IDLE)
317 h->cerrno = CSOUTSTATE;
320 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
324 yaz_errno() == EWOULDBLOCK
326 #if EAGAIN != EWOULDBLOCK
327 || yaz_errno() == EAGAIN
331 h->cerrno = CSNODATA;
336 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
337 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
340 h->state = CS_ST_INCON;
344 static COMSTACK unix_accept(COMSTACK h)
347 unix_state *state, *st = (unix_state *)h->cprivate;
349 TRC(fprintf(stderr, "unix_accept\n"));
350 if (h->state == CS_ST_INCON)
352 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
359 memcpy(cnew, h, sizeof(*h));
360 cnew->iofile = h->newfd;
361 cnew->io_pending = 0;
362 if (!(state = (unix_state *)
363 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
373 if (!cnew->blocking &&
374 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
389 state->altsize = state->altlen = 0;
390 state->towrite = state->written = -1;
391 state->complete = st->complete;
392 cnew->state = CS_ST_ACCEPT;
393 cnew->event = CS_NONE;
394 h->state = CS_ST_IDLE;
398 if (h->state == CS_ST_ACCEPT)
403 h->cerrno = CSOUTSTATE;
407 h->state = CS_ST_DATAXFER;
412 #define CS_UNIX_BUFCHUNK 4096
415 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
416 * 0=connection closed.
418 static int unix_get(COMSTACK h, char **buf, int *bufsize)
420 unix_state *sp = (unix_state *)h->cprivate;
422 int tmpi, berlen, rest, req, tomove;
423 int hasread = 0, res;
425 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
426 if (sp->altlen) /* switch buffers */
428 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
429 (unsigned) sp->altbuf));
433 *bufsize = sp->altsize;
434 hasread = sp->altlen;
440 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
444 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
447 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
448 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
450 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
451 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
454 if (yaz_errno() == EWOULDBLOCK
456 #if EAGAIN != EWOULDBLOCK
457 || yaz_errno() == EAGAIN
460 || yaz_errno() == EINPROGRESS
463 h->io_pending = CS_WANT_READ;
466 else if (yaz_errno() == 0)
475 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
477 /* move surplus buffer (or everything if we didn't get a BER rec.) */
478 if (hasread > berlen)
480 tomove = req = hasread - berlen;
481 rest = tomove % CS_UNIX_BUFCHUNK;
483 req += CS_UNIX_BUFCHUNK - rest;
486 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
488 } else if (sp->altsize < req)
489 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
491 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
492 (unsigned) sp->altbuf));
493 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
495 if (berlen < CS_UNIX_BUFCHUNK - 1)
496 *(*buf + berlen) = '\0';
497 return berlen ? berlen : 1;
504 * In nonblocking mode, you must call again with same buffer while
507 static int unix_put(COMSTACK h, char *buf, int size)
510 struct unix_state *state = (struct unix_state *)h->cprivate;
512 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
515 if (state->towrite < 0)
517 state->towrite = size;
520 else if (state->towrite != size)
522 h->cerrno = CSWRONGBUF;
525 while (state->towrite > state->written)
528 send(h->iofile, buf + state->written, size -
538 yaz_errno() == EWOULDBLOCK
540 #if EAGAIN != EWOULDBLOCK
541 || yaz_errno() == EAGAIN
546 TRC(fprintf(stderr, " Flow control stop\n"));
547 h->io_pending = CS_WANT_WRITE;
553 state->written += res;
554 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
555 res, state->written, size));
557 state->towrite = state->written = -1;
558 TRC(fprintf(stderr, " Ok\n"));
562 static int unix_close(COMSTACK h)
564 unix_state *sp = (struct unix_state *)h->cprivate;
566 TRC(fprintf(stderr, "unix_close\n"));
578 static char *unix_addrstr(COMSTACK h)
580 unix_state *sp = (struct unix_state *)h->cprivate;
582 sprintf(buf, "unix:%s", sp->addr.sun_path);
586 static int unix_set_blocking(COMSTACK p, int blocking)
590 if (p->blocking == blocking)
592 flag = fcntl(p->iofile, F_GETFL, 0);
594 flag = flag & ~O_NONBLOCK;
596 flag = flag | O_NONBLOCK;
597 if (fcntl(p->iofile, F_SETFL, flag) < 0)
599 p->blocking = blocking;