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