Add OID-database entries for the MARC, BIB-2 and ZeeRex attribute sets.
[yaz-moved-to-github.git] / comstack / unix.c
1 /*
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: unix.c,v 1.4 2002-07-22 23:16:10 adam Exp $
6  * UNIX socket COMSTACK. By Morten Bøgeskov.
7  */
8 #ifndef WIN32
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <signal.h>
17
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/un.h>
21
22 #include <yaz/comstack.h>
23 #include <yaz/unix.h>
24 #include <yaz/log.h>
25
26 /* Chas added the following, so we get the definition of completeBER */
27 #include <yaz/odr.h>
28
29 int unix_close(COMSTACK h);
30 int unix_put(COMSTACK h, char *buf, int size);
31 int unix_get(COMSTACK h, char **buf, int *bufsize);
32 int unix_connect(COMSTACK h, void *address);
33 int unix_more(COMSTACK h);
34 int unix_rcvconnect(COMSTACK h);
35 int unix_bind(COMSTACK h, void *address, int mode);
36 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
37                 int (*check_ip)(void *cd, const char *a, int len, int type),
38                 void *cd);
39 int static unix_set_blocking(COMSTACK p, int blocking);
40
41
42 COMSTACK unix_accept(COMSTACK h);
43 char *unix_addrstr(COMSTACK h);
44 void *unix_straddr(COMSTACK h, const char *str);
45
46 #ifndef SUN_LEN
47 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
48                       + strlen ((ptr)->sun_path))
49 #endif
50 #if 0
51 #define TRC(x) x
52 #else
53 #define TRC(X)
54 #endif
55
56 /* this state is used for both SSL and straight TCP/IP */
57 typedef struct unix_state
58 {
59     char *altbuf; /* alternate buffer for surplus data */
60     int altsize;  /* size as xmalloced */
61     int altlen;   /* length of data or 0 if none */
62
63     int written;  /* -1 if we aren't writing */
64     int towrite;  /* to verify against user input */
65     int (*complete)(const unsigned char *buf, int len); /* length/comple. */
66     struct sockaddr_un addr;  /* returned by cs_straddr */
67     char buf[128]; /* returned by cs_addrstr */
68 } unix_state;
69
70 static int unix_init (void)
71 {
72     return 1;
73 }
74
75 /*
76  * This function is always called through the cs_create() macro.
77  * s >= 0: socket has already been established for us.
78  */
79 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
80 {
81     COMSTACK p;
82     unix_state *state;
83     int new_socket;
84
85     if (!unix_init ())
86         return 0;
87     if (s < 0)
88     {
89         if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
90             return 0;
91         new_socket = 1;
92     }
93     else
94         new_socket = 0;
95     if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
96         return 0;
97     if (!(state = (struct unix_state *)(p->cprivate =
98                                         xmalloc(sizeof(unix_state)))))
99         return 0;
100
101     if (!(p->blocking = blocking))
102     {
103         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
104             return 0;
105 #ifndef MSG_NOSIGNAL
106         signal (SIGPIPE, SIG_IGN);
107 #endif
108     }
109
110     p->io_pending = 0;
111     p->iofile = s;
112     p->type = unix_type;
113     p->protocol = (enum oid_proto) protocol;
114
115     p->f_connect = unix_connect;
116     p->f_rcvconnect = unix_rcvconnect;
117     p->f_get = unix_get;
118     p->f_put = unix_put;
119     p->f_close = unix_close;
120     p->f_more = unix_more;
121     p->f_bind = unix_bind;
122     p->f_listen = unix_listen;
123     p->f_accept = unix_accept;
124     p->f_addrstr = unix_addrstr;
125     p->f_straddr = unix_straddr;
126     p->f_set_blocking = unix_set_blocking;
127
128     p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
129     p->event = CS_NONE;
130     p->cerrno = 0;
131     p->stackerr = 0;
132
133     state->altbuf = 0;
134     state->altsize = state->altlen = 0;
135     state->towrite = state->written = -1;
136     if (protocol == PROTO_WAIS)
137         state->complete = completeWAIS;
138     else
139         state->complete = completeBER;
140
141     p->timeout = COMSTACK_DEFAULT_TIMEOUT;
142     TRC(fprintf(stderr, "Created new UNIX comstack\n"));
143
144     return p;
145 }
146
147
148 int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
149 {
150     char *cp;
151     if (!unix_init ())
152         return 0;
153     TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
154     add->sun_family = AF_UNIX;
155     strncpy(add->sun_path, str, sizeof(add->sun_path));
156     cp = strchr (add->sun_path, ':');
157     if (cp)
158         *cp = '\0';
159     return 1;
160 }
161
162 void *unix_straddr(COMSTACK h, const char *str)
163 {
164     unix_state *sp = (unix_state *)h->cprivate;
165
166     TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
167
168     if (!unix_strtoaddr_ex (str, &sp->addr))
169         return 0;
170     return &sp->addr;
171 }
172
173 struct sockaddr_un *unix_strtoaddr(const char *str)
174 {
175     static struct sockaddr_un add;
176
177     TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
178
179     if (!unix_strtoaddr_ex (str, &add))
180         return 0;
181     return &add;
182 }
183
184 int unix_more(COMSTACK h)
185 {
186     unix_state *sp = (unix_state *)h->cprivate;
187
188     return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
189                                          sp->altlen);
190 }
191
192 /*
193  * connect(2) will block (sometimes) - nothing we can do short of doing
194  * weird things like spawning subprocesses or threading or some weird junk
195  * like that.
196  */
197 int unix_connect(COMSTACK h, void *address)
198 {
199     struct sockaddr_un *add = (struct sockaddr_un *)address;
200     int r;
201
202     TRC(fprintf(stderr, "unix_connect\n"));
203     h->io_pending = 0;
204     if (h->state != CS_ST_UNBND)
205     {
206         h->cerrno = CSOUTSTATE;
207         return -1;
208     }
209     r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
210     if (r < 0)
211     {
212         if (errno == EINPROGRESS)
213         {
214             h->event = CS_CONNECT;
215             h->state = CS_ST_CONNECTING;
216             h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
217             return 1;
218         }
219         h->cerrno = CSYSERR;
220         return -1;
221     }
222     h->event = CS_CONNECT;
223     h->state = CS_ST_CONNECTING;
224
225     return unix_rcvconnect (h);
226 }
227
228 /*
229  * nop
230  */
231 int unix_rcvconnect(COMSTACK h)
232 {
233     TRC(fprintf(stderr, "unix_rcvconnect\n"));
234
235     if (h->state == CS_ST_DATAXFER)
236         return 0;
237     if (h->state != CS_ST_CONNECTING)
238     {
239         h->cerrno = CSOUTSTATE;
240         return -1;
241     }
242     h->event = CS_DATA;
243     h->state = CS_ST_DATAXFER;
244     return 0;
245 }
246
247 #define CERTF "ztest.pem"
248 #define KEYF "ztest.pem"
249
250 int unix_bind(COMSTACK h, void *address, int mode)
251 {
252     struct sockaddr *addr = (struct sockaddr *)address;
253     const char * path = ((struct sockaddr_un *)addr)->sun_path;
254     struct stat stat_buf;
255
256     TRC (fprintf (stderr, "unix_bind\n"));
257
258     if(stat(path, &stat_buf) != -1) {
259         struct sockaddr_un socket_unix;
260         int socket_out = -1;
261         if(! S_ISSOCK(stat_buf.st_mode)) {
262             h->cerrno = CSYSERR;
263             errno = EEXIST; /* Not a socket (File exists) */
264             return -1;
265         }
266         if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
267             h->cerrno = CSYSERR;
268             return -1;
269         }
270         socket_unix.sun_family = AF_UNIX;
271         strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
272         if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
273             if(errno == ECONNREFUSED) {
274                 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
275             } else {
276                 h->cerrno = CSYSERR;
277                 return -1;
278             }
279         } else {
280             close(socket_out);
281             h->cerrno = CSYSERR;
282             errno = EADDRINUSE;
283             return -1;
284         }
285         unlink(path);
286     }
287
288     if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
289     {
290         h->cerrno = CSYSERR;
291         return -1;
292     }
293     if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
294     {
295         h->cerrno = CSYSERR;
296         return -1;
297     }
298     h->state = CS_ST_IDLE;
299     h->event = CS_LISTEN;
300     return 0;
301 }
302
303 int unix_listen(COMSTACK h, char *raddr, int *addrlen,
304                 int (*check_ip)(void *cd, const char *a, int len, int t),
305                 void *cd)
306 {
307     struct sockaddr_un addr;
308 #ifdef __cplusplus
309     socklen_t len = SUN_LEN(&addr);
310 #else
311     int len = SUN_LEN(&addr);
312 #endif
313
314     TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
315     if (h->state != CS_ST_IDLE)
316     {
317         h->cerrno = CSOUTSTATE;
318         return -1;
319     }
320     h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
321     if (h->newfd < 0)
322     {
323         if (
324             errno == EWOULDBLOCK
325 #ifdef EAGAIN
326 #if EAGAIN != EWOULDBLOCK
327             || errno == EAGAIN
328 #endif
329 #endif
330             )
331             h->cerrno = CSNODATA;
332         else
333             h->cerrno = CSYSERR;
334         return -1;
335     }
336     if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
337         memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
338     else if (addrlen)
339         *addrlen = 0;
340     h->state = CS_ST_INCON;
341     return 0;
342 }
343
344 COMSTACK unix_accept(COMSTACK h)
345 {
346     COMSTACK cnew;
347     unix_state *state, *st = (unix_state *)h->cprivate;
348
349     TRC(fprintf(stderr, "unix_accept\n"));
350     if (h->state == CS_ST_INCON)
351     {
352         if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
353         {
354             h->cerrno = CSYSERR;
355             close(h->newfd);
356             h->newfd = -1;
357             return 0;
358         }
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)))))
364         {
365             h->cerrno = CSYSERR;
366             if (h->newfd != -1)
367             {
368                 close(h->newfd);
369                 h->newfd = -1;
370             }
371             return 0;
372         }
373         if (!cnew->blocking &&
374             (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
375             )
376         {
377             h->cerrno = CSYSERR;
378             if (h->newfd != -1)
379             {
380                 close(h->newfd);
381                 h->newfd = -1;
382             }
383             xfree (cnew);
384             xfree (state);
385             return 0;
386         }
387         h->newfd = -1;
388         state->altbuf = 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;
395
396         h = cnew;
397     }
398     if (h->state == CS_ST_ACCEPT)
399     {
400     }
401     else
402     {
403         h->cerrno = CSOUTSTATE;
404         return 0;
405     }
406     h->io_pending = 0;
407     h->state = CS_ST_DATAXFER;
408     h->event = CS_DATA;
409     return h;
410 }
411
412 #define CS_UNIX_BUFCHUNK 4096
413
414 /*
415  * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
416  * 0=connection closed.
417  */
418 int unix_get(COMSTACK h, char **buf, int *bufsize)
419 {
420     unix_state *sp = (unix_state *)h->cprivate;
421     char *tmpc;
422     int tmpi, berlen, rest, req, tomove;
423     int hasread = 0, res;
424
425     TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
426     if (sp->altlen) /* switch buffers */
427     {
428         TRC(fprintf(stderr, "  %d bytes in altbuf (0x%x)\n", sp->altlen,
429                     (unsigned) sp->altbuf));
430         tmpc = *buf;
431         tmpi = *bufsize;
432         *buf = sp->altbuf;
433         *bufsize = sp->altsize;
434         hasread = sp->altlen;
435         sp->altlen = 0;
436         sp->altbuf = tmpc;
437         sp->altsize = tmpi;
438     }
439     h->io_pending = 0;
440     while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
441     {
442         if (!*bufsize)
443         {
444             if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
445                 return -1;
446         }
447         else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
448             if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
449                 return -1;
450         res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
451         TRC(fprintf(stderr, "  recv res=%d, hasread=%d\n", res, hasread));
452         if (res < 0)
453         {
454             if (errno == EWOULDBLOCK
455 #ifdef EAGAIN
456 #if EAGAIN != EWOULDBLOCK
457                 || errno == EAGAIN
458 #endif
459 #endif
460                 || errno == EINPROGRESS
461                 )
462             {
463                 h->io_pending = CS_WANT_READ;
464                 break;
465             }
466             else if (errno == 0)
467                 continue;
468             else
469                 return -1;
470         }
471         else if (!res)
472             return 0;
473         hasread += res;
474     }
475     TRC (fprintf (stderr, "  Out of read loop with hasread=%d, berlen=%d\n",
476                   hasread, berlen));
477     /* move surplus buffer (or everything if we didn't get a BER rec.) */
478     if (hasread > berlen)
479     {
480         tomove = req = hasread - berlen;
481         rest = tomove % CS_UNIX_BUFCHUNK;
482         if (rest)
483             req += CS_UNIX_BUFCHUNK - rest;
484         if (!sp->altbuf)
485         {
486             if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
487                 return -1;
488         } else if (sp->altsize < req)
489             if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
490                 return -1;
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);
494     }
495     if (berlen < CS_UNIX_BUFCHUNK - 1)
496         *(*buf + berlen) = '\0';
497     return berlen ? berlen : 1;
498 }
499
500
501
502 /*
503  * Returns 1, 0 or -1
504  * In nonblocking mode, you must call again with same buffer while
505  * return value is 1.
506  */
507 int unix_put(COMSTACK h, char *buf, int size)
508 {
509     int res;
510     struct unix_state *state = (struct unix_state *)h->cprivate;
511
512     TRC(fprintf(stderr, "unix_put: size=%d\n", size));
513     h->io_pending = 0;
514     h->event = CS_DATA;
515     if (state->towrite < 0)
516     {
517         state->towrite = size;
518         state->written = 0;
519     }
520     else if (state->towrite != size)
521     {
522         h->cerrno = CSWRONGBUF;
523         return -1;
524     }
525     while (state->towrite > state->written)
526     {
527         if ((res =
528              send(h->iofile, buf + state->written, size -
529                   state->written,
530 #ifdef MSG_NOSIGNAL
531                   MSG_NOSIGNAL
532 #else
533                   0
534 #endif
535                  )) < 0)
536         {
537             if (
538                 errno == EWOULDBLOCK
539 #ifdef EAGAIN
540 #if EAGAIN != EWOULDBLOCK
541                 || errno == EAGAIN
542 #endif
543 #endif
544                 )
545             {
546                 TRC(fprintf(stderr, "  Flow control stop\n"));
547                 h->io_pending = CS_WANT_WRITE;
548                 return 1;
549             }
550             h->cerrno = CSYSERR;
551             return -1;
552         }
553         state->written += res;
554         TRC(fprintf(stderr, "  Wrote %d, written=%d, nbytes=%d\n",
555                     res, state->written, size));
556     }
557     state->towrite = state->written = -1;
558     TRC(fprintf(stderr, "  Ok\n"));
559     return 0;
560 }
561
562
563
564 int unix_close(COMSTACK h)
565 {
566     unix_state *sp = (struct unix_state *)h->cprivate;
567
568     TRC(fprintf(stderr, "unix_close\n"));
569     if (h->iofile != -1)
570     {
571         close(h->iofile);
572     }
573     if (sp->altbuf)
574         xfree(sp->altbuf);
575     xfree(sp);
576     xfree(h);
577     return 0;
578 }
579
580 char *unix_addrstr(COMSTACK h)
581 {
582     unix_state *sp = (struct unix_state *)h->cprivate;
583     char *buf = sp->buf;
584     sprintf(buf, "unix:%s", sp->addr.sun_path);
585     return buf;
586 }
587
588 int static unix_set_blocking(COMSTACK p, int blocking)
589 {
590     unsigned long flag;
591
592     if (p->blocking == blocking)
593         return 1;
594     flag = fcntl(p->iofile, F_GETFL, 0);
595     if(!blocking)
596         flag = flag & ~O_NONBLOCK;
597     else
598         flag = flag | O_NONBLOCK;
599     if (fcntl(p->iofile, F_SETFL, flag) < 0)
600         return 0;
601     p->blocking = blocking;
602     return 1;
603 }
604 #endif