2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.5 2002-09-10 20:56:34 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 NET_LEN_T socklen_t
32 #if GETPEERNAME_ACCEPTS_SIZE_T_FOR_THIRD_ARGUMENT
33 #define NET_LEN_T size_t
39 int unix_close(COMSTACK h);
40 int unix_put(COMSTACK h, char *buf, int size);
41 int unix_get(COMSTACK h, char **buf, int *bufsize);
42 int unix_connect(COMSTACK h, void *address);
43 int unix_more(COMSTACK h);
44 int unix_rcvconnect(COMSTACK h);
45 int unix_bind(COMSTACK h, void *address, int mode);
46 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
47 int (*check_ip)(void *cd, const char *a, int len, int type),
49 int static unix_set_blocking(COMSTACK p, int blocking);
52 COMSTACK unix_accept(COMSTACK h);
53 char *unix_addrstr(COMSTACK h);
54 void *unix_straddr(COMSTACK h, const char *str);
57 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
58 + strlen ((ptr)->sun_path))
66 /* this state is used for both SSL and straight TCP/IP */
67 typedef struct unix_state
69 char *altbuf; /* alternate buffer for surplus data */
70 int altsize; /* size as xmalloced */
71 int altlen; /* length of data or 0 if none */
73 int written; /* -1 if we aren't writing */
74 int towrite; /* to verify against user input */
75 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
76 struct sockaddr_un addr; /* returned by cs_straddr */
77 char buf[128]; /* returned by cs_addrstr */
80 static int unix_init (void)
86 * This function is always called through the cs_create() macro.
87 * s >= 0: socket has already been established for us.
89 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
99 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
105 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
107 if (!(state = (struct unix_state *)(p->cprivate =
108 xmalloc(sizeof(unix_state)))))
111 if (!(p->blocking = blocking))
113 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
116 signal (SIGPIPE, SIG_IGN);
123 p->protocol = (enum oid_proto) protocol;
125 p->f_connect = unix_connect;
126 p->f_rcvconnect = unix_rcvconnect;
129 p->f_close = unix_close;
130 p->f_more = unix_more;
131 p->f_bind = unix_bind;
132 p->f_listen = unix_listen;
133 p->f_accept = unix_accept;
134 p->f_addrstr = unix_addrstr;
135 p->f_straddr = unix_straddr;
136 p->f_set_blocking = unix_set_blocking;
138 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
144 state->altsize = state->altlen = 0;
145 state->towrite = state->written = -1;
146 if (protocol == PROTO_WAIS)
147 state->complete = completeWAIS;
149 state->complete = completeBER;
151 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
152 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
158 int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
163 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
164 add->sun_family = AF_UNIX;
165 strncpy(add->sun_path, str, sizeof(add->sun_path));
166 cp = strchr (add->sun_path, ':');
172 void *unix_straddr(COMSTACK h, const char *str)
174 unix_state *sp = (unix_state *)h->cprivate;
176 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
178 if (!unix_strtoaddr_ex (str, &sp->addr))
183 struct sockaddr_un *unix_strtoaddr(const char *str)
185 static struct sockaddr_un add;
187 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
189 if (!unix_strtoaddr_ex (str, &add))
194 int unix_more(COMSTACK h)
196 unix_state *sp = (unix_state *)h->cprivate;
198 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
203 * connect(2) will block (sometimes) - nothing we can do short of doing
204 * weird things like spawning subprocesses or threading or some weird junk
207 int unix_connect(COMSTACK h, void *address)
209 struct sockaddr_un *add = (struct sockaddr_un *)address;
212 TRC(fprintf(stderr, "unix_connect\n"));
214 if (h->state != CS_ST_UNBND)
216 h->cerrno = CSOUTSTATE;
219 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
222 if (errno == EINPROGRESS)
224 h->event = CS_CONNECT;
225 h->state = CS_ST_CONNECTING;
226 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
232 h->event = CS_CONNECT;
233 h->state = CS_ST_CONNECTING;
235 return unix_rcvconnect (h);
241 int unix_rcvconnect(COMSTACK h)
243 TRC(fprintf(stderr, "unix_rcvconnect\n"));
245 if (h->state == CS_ST_DATAXFER)
247 if (h->state != CS_ST_CONNECTING)
249 h->cerrno = CSOUTSTATE;
253 h->state = CS_ST_DATAXFER;
257 #define CERTF "ztest.pem"
258 #define KEYF "ztest.pem"
260 int unix_bind(COMSTACK h, void *address, int mode)
262 struct sockaddr *addr = (struct sockaddr *)address;
263 const char * path = ((struct sockaddr_un *)addr)->sun_path;
264 struct stat stat_buf;
266 TRC (fprintf (stderr, "unix_bind\n"));
268 if(stat(path, &stat_buf) != -1) {
269 struct sockaddr_un socket_unix;
271 if(! S_ISSOCK(stat_buf.st_mode)) {
273 errno = EEXIST; /* Not a socket (File exists) */
276 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
280 socket_unix.sun_family = AF_UNIX;
281 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
282 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
283 if(errno == ECONNREFUSED) {
284 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
298 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
303 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
308 h->state = CS_ST_IDLE;
309 h->event = CS_LISTEN;
313 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
314 int (*check_ip)(void *cd, const char *a, int len, int t),
317 struct sockaddr_un addr;
318 NET_LEN_T len = SUN_LEN(&addr);
320 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
321 if (h->state != CS_ST_IDLE)
323 h->cerrno = CSOUTSTATE;
326 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
332 #if EAGAIN != EWOULDBLOCK
337 h->cerrno = CSNODATA;
342 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
343 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
346 h->state = CS_ST_INCON;
350 COMSTACK unix_accept(COMSTACK h)
353 unix_state *state, *st = (unix_state *)h->cprivate;
355 TRC(fprintf(stderr, "unix_accept\n"));
356 if (h->state == CS_ST_INCON)
358 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
365 memcpy(cnew, h, sizeof(*h));
366 cnew->iofile = h->newfd;
367 cnew->io_pending = 0;
368 if (!(state = (unix_state *)
369 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
379 if (!cnew->blocking &&
380 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
395 state->altsize = state->altlen = 0;
396 state->towrite = state->written = -1;
397 state->complete = st->complete;
398 cnew->state = CS_ST_ACCEPT;
399 cnew->event = CS_NONE;
400 h->state = CS_ST_IDLE;
404 if (h->state == CS_ST_ACCEPT)
409 h->cerrno = CSOUTSTATE;
413 h->state = CS_ST_DATAXFER;
418 #define CS_UNIX_BUFCHUNK 4096
421 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
422 * 0=connection closed.
424 int unix_get(COMSTACK h, char **buf, int *bufsize)
426 unix_state *sp = (unix_state *)h->cprivate;
428 int tmpi, berlen, rest, req, tomove;
429 int hasread = 0, res;
431 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
432 if (sp->altlen) /* switch buffers */
434 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
435 (unsigned) sp->altbuf));
439 *bufsize = sp->altsize;
440 hasread = sp->altlen;
446 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
450 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
453 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
454 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
456 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
457 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
460 if (errno == EWOULDBLOCK
462 #if EAGAIN != EWOULDBLOCK
466 || errno == EINPROGRESS
469 h->io_pending = CS_WANT_READ;
481 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
483 /* move surplus buffer (or everything if we didn't get a BER rec.) */
484 if (hasread > berlen)
486 tomove = req = hasread - berlen;
487 rest = tomove % CS_UNIX_BUFCHUNK;
489 req += CS_UNIX_BUFCHUNK - rest;
492 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
494 } else if (sp->altsize < req)
495 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
497 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
498 (unsigned) sp->altbuf));
499 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
501 if (berlen < CS_UNIX_BUFCHUNK - 1)
502 *(*buf + berlen) = '\0';
503 return berlen ? berlen : 1;
510 * In nonblocking mode, you must call again with same buffer while
513 int unix_put(COMSTACK h, char *buf, int size)
516 struct unix_state *state = (struct unix_state *)h->cprivate;
518 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
521 if (state->towrite < 0)
523 state->towrite = size;
526 else if (state->towrite != size)
528 h->cerrno = CSWRONGBUF;
531 while (state->towrite > state->written)
534 send(h->iofile, buf + state->written, size -
546 #if EAGAIN != EWOULDBLOCK
552 TRC(fprintf(stderr, " Flow control stop\n"));
553 h->io_pending = CS_WANT_WRITE;
559 state->written += res;
560 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
561 res, state->written, size));
563 state->towrite = state->written = -1;
564 TRC(fprintf(stderr, " Ok\n"));
570 int unix_close(COMSTACK h)
572 unix_state *sp = (struct unix_state *)h->cprivate;
574 TRC(fprintf(stderr, "unix_close\n"));
586 char *unix_addrstr(COMSTACK h)
588 unix_state *sp = (struct unix_state *)h->cprivate;
590 sprintf(buf, "unix:%s", sp->addr.sun_path);
594 int static unix_set_blocking(COMSTACK p, int blocking)
598 if (p->blocking == blocking)
600 flag = fcntl(p->iofile, F_GETFL, 0);
602 flag = flag & ~O_NONBLOCK;
604 flag = flag | O_NONBLOCK;
605 if (fcntl(p->iofile, F_SETFL, flag) < 0)
607 p->blocking = blocking;