1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2014 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
pg_prewarmnull7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #ifndef LCB_CONNSPEC_H
19 #define LCB_CONNSPEC_H
20 
21 #include <libcouchbase/couchbase.h>
22 #include "config.h"
23 
24 #include <string>
25 #include <vector>
26 #include <set>
27 #include "hostlist.h"
28 
29 #ifdef _MSC_VER
30 /*
31  * Disable DLL interface warning. This isn't an issue since this API is
32  * private anyway
33  */
34 #pragma warning(push)
35 #pragma warning(disable : 4251)
36 #endif
37 
38 namespace lcb {
39 struct Spechost {
40     Spechost() : port(0), type(0) {}
41     lcb_U16 port;
42     short type;
43     std::string hostname;
44     bool isSSL() const { return type == LCB_CONFIG_MCD_SSL_PORT || type == LCB_CONFIG_HTTP_SSL_PORT; }
45     bool isHTTPS() const { return type == LCB_CONFIG_HTTP_SSL_PORT; }
46     bool isHTTP() const { return type == LCB_CONFIG_HTTP_PORT; }
47     bool isMCD() const { return type == LCB_CONFIG_MCD_PORT; }
48     bool isMCDS() const { return type == LCB_CONFIG_MCD_SSL_PORT; }
49     bool isTypeless() const { return type == 0 ; }
50 
51     bool isAnyMcd() const {
52         return isMCD() || isMCDS() || type == LCB_CONFIG_MCCOMPAT_PORT;
53     }
54     bool isAnyHttp() const {
55         return isHTTP() || isHTTPS();
56     }
57 };
58 
59 #define LCB_CONNSPEC_F_FILEONLY (1<<4)
60 
61 class LCB_CLASS_EXPORT Connspec {
62 public:
63     typedef std::vector<std::pair<std::string,std::string> > Options;
64     Connspec() : m_sslopts(0), m_implicit_port(0), m_loglevel(0), m_logredact(false), m_transports(), m_flags(0), m_ipv6(LCB_IPV6_DISABLED), m_logger(NULL) {}
65 
66     lcb_error_t parse(const char *connstr, const char **errmsg = NULL);
67     lcb_error_t load(const lcb_create_st&);
68 
69     bool has_bsmode(int mode) const {
70         return m_transports.find(mode) != m_transports.end();
71     }
72     bool is_bs_udef() const {
73         return !m_transports.empty() || (m_flags & LCB_CONNSPEC_F_FILEONLY);
74     }
75     bool is_bs_http() const { return has_bsmode(LCB_CONFIG_TRANSPORT_HTTP); }
76     bool is_bs_cccp() const { return has_bsmode(LCB_CONFIG_TRANSPORT_CCCP); }
77     bool is_bs_file() const { return m_flags & LCB_CONNSPEC_F_FILEONLY; }
78 
79     /**
80      * Whether a DNS SRV lookup can be performed on this connection string.
81      * @return true if a DNS SRV lookup is possible, or false if there is
82      * a parameter or format of the connection string preventing a lookup
83      */
84     bool can_dnssrv() const;
85 
86     /**
87      * Whether the explicit `couchbase{s}+dnssrv` internal scheme is used
88      */
89     bool is_explicit_dnssrv() const;
90     uint16_t default_port() const { return m_implicit_port; }
91     const std::vector<Spechost>& hosts() const { return m_hosts; }
92     const std::string& bucket() const { return m_bucket; }
93     const std::string& username() const { return m_username; }
94     const std::string& password() const { return m_password; }
95     const std::string& truststorepath() const { return m_truststorepath; }
96     const std::string& certpath() const { return m_certpath; }
97     const std::string& keypath() const { return m_keypath; }
98     unsigned sslopts() const { return m_sslopts; }
99     const Options& options() const { return m_ctlopts; }
100     lcb_logprocs * logger() const { return m_logger; }
101     unsigned loglevel() const { return m_loglevel; }
102     bool logredact() const { return m_logredact; }
103     const std::string& connstr() const { return m_connstr; }
104     void clear_hosts() { m_hosts.clear(); }
105     void add_host(const Spechost& host) { m_hosts.push_back(host); }
106     lcb_ipv6_t ipv6_policy() const { return m_ipv6; }
107 private:
108     Options m_ctlopts;
109     std::string m_bucket;
110     std::string m_username;
111     std::string m_password;
112     std::string m_truststorepath;
113     std::string m_certpath;
114     std::string m_keypath;
115     std::string m_connstr;
116     unsigned m_sslopts; /**< SSL Options */
117     std::vector<Spechost> m_hosts;
118     lcb_U16 m_implicit_port; /**< Implicit port, based on scheme */
119     int m_loglevel; /* cached loglevel */
120     bool m_logredact;
121 
122     inline lcb_error_t parse_options(
123         const char *options, const char *optend, const char **errmsg);
124     inline lcb_error_t parse_hosts(
125         const char *hostbegin, const char *hostend, const char **errmsg);
126 
127     std::set<int> m_transports;
128     unsigned m_flags; /**< Internal flags */
129     lcb_ipv6_t m_ipv6;
130     lcb_logprocs *m_logger;
131 };
132 
133 #define LCB_SPECSCHEME_RAW "couchbase+explicit://"
134 #define LCB_SPECSCHEME_MCD "couchbase://"
135 #define LCB_SPECSCHEME_MCD_SSL "couchbases://"
136 #define LCB_SPECSCHEME_HTTP "http://"
137 #define LCB_SPECSCHEME_HTTP_SSL "https-internal://"
138 #define LCB_SPECSCHEME_MCCOMPAT "memcached://"
139 #define LCB_SPECSCHEME_SRV "couchbase+dnssrv://"
140 #define LCB_SPECSCHEME_SRV_SSL "couchbases+dnssrv://"
141 
142 // Standalone functionality:
143 lcb_error_t
144 dnssrv_query(const char *name, Hostlist& hostlist);
145 
146 Hostlist *
147 dnssrv_getbslist(const char *addr, bool is_ssl, lcb_error_t& errout);
148 
149 } // namespace
150 
151 #endif
152 
153 #ifdef _MSC_VER
154 #pragma warning(pop)
155 #endif
156