Added include of string.h.
[yaz-moved-to-github.git] / comstack / comstack.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Log: comstack.c,v $
6  * Revision 1.8  2001-07-19 19:49:02  adam
7  * Added include of string.h.
8  *
9  * Revision 1.7  2001/03/21 12:43:36  adam
10  * Implemented cs_create_host. Better error reporting for SSL comstack.
11  *
12  * Revision 1.6  1999/11/30 13:47:11  adam
13  * Improved installation. Moved header files to include/yaz.
14  *
15  * Revision 1.5  1998/06/22 11:32:35  adam
16  * Added 'conditional cs_listen' feature.
17  *
18  * Revision 1.4  1997/09/29 07:16:14  adam
19  * Array cs_errlist no longer global.
20  *
21  * Revision 1.3  1997/09/01 08:49:14  adam
22  * New windows NT/95 port using MSV5.0. Minor changes only.
23  *
24  * Revision 1.2  1995/09/29 17:01:48  quinn
25  * More Windows work
26  *
27  * Revision 1.1  1995/06/14  09:58:20  quinn
28  * Renamed yazlib to comstack.
29  *
30  * Revision 1.2  1995/05/16  08:51:15  quinn
31  * License, documentation, and memory fixes
32  *
33  * Revision 1.1  1995/03/14  10:28:34  quinn
34  * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
35  *
36  *
37  */
38
39 #include <string.h>
40 #include <yaz/comstack.h>
41 #include <yaz/tcpip.h>
42
43 static const char *cs_errlist[] =
44 {
45     "No error or unspecified error",
46     "System (lower-layer) error",
47     "Operation out of state",
48     "No data (operation would block)",
49     "New data while half of old buffer is on the line (flow control)",
50     "Permission denied",
51     "SSL error"
52 };
53
54 const char *cs_errmsg(int n)
55 {
56     if (n < 0 || n > 6)
57         n = 0;
58     return cs_errlist[n];
59 }
60
61 const char *cs_strerror(COMSTACK h)
62 {
63     return cs_errmsg(h->cerrno);
64 }
65
66 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
67 {
68     const char *host = 0;
69     COMSTACK cs;
70     CS_TYPE t;
71
72     if (strncmp (type_and_host, "tcp:", 4) == 0)
73     {
74         t = tcpip_type;
75         host = type_and_host + 4;
76     }
77     else if (strncmp (type_and_host, "ssl:", 4) == 0)
78     {
79 #if HAVE_OPENSSL_SSL_H
80         t = ssl_type;
81         host = type_and_host + 4;
82 #else
83         return 0;
84 #endif
85     }
86     else
87     {
88         t = tcpip_type;
89         host = type_and_host;
90
91     }
92     cs = cs_create (t, blocking, PROTO_Z3950);
93     if (!cs)
94         return 0;
95
96     if (!(*vp = cs_straddr(cs, host)))
97     {
98         cs_close (cs);
99         return 0;
100     }    
101     return cs;
102 }