1 /*
2  * Copyright 2013 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MONGOC_CLUSTER_PRIVATE_H
18 #define MONGOC_CLUSTER_PRIVATE_H
19 
20 #if !defined(MONGOC_COMPILATION)
21 #error "Only <mongoc.h> can be included directly."
22 #endif
23 
24 #include <bson.h>
25 
26 #include "mongoc-array-private.h"
27 #include "mongoc-buffer-private.h"
28 #include "mongoc-config.h"
29 #include "mongoc-client.h"
30 #include "mongoc-list-private.h"
31 #include "mongoc-opcode.h"
32 #include "mongoc-read-prefs.h"
33 #include "mongoc-rpc-private.h"
34 #include "mongoc-server-stream-private.h"
35 #include "mongoc-set-private.h"
36 #include "mongoc-stream.h"
37 #include "mongoc-topology-description-private.h"
38 #include "mongoc-uri.h"
39 #include "mongoc-write-concern.h"
40 #include "mongoc-scram-private.h"
41 #include "mongoc-cmd-private.h"
42 
43 BSON_BEGIN_DECLS
44 
45 
46 typedef struct _mongoc_cluster_node_t {
47    mongoc_stream_t *stream;
48    char *connection_address;
49 
50    int32_t max_wire_version;
51    int32_t min_wire_version;
52    int32_t max_write_batch_size;
53    int32_t max_bson_obj_size;
54    int32_t max_msg_size;
55 
56    int64_t timestamp;
57 } mongoc_cluster_node_t;
58 
59 typedef struct _mongoc_cluster_t {
60    int64_t operation_id;
61    uint32_t request_id;
62    uint32_t sockettimeoutms;
63    uint8_t scram_client_key[MONGOC_SCRAM_HASH_SIZE];
64    uint8_t scram_server_key[MONGOC_SCRAM_HASH_SIZE];
65    uint8_t scram_salted_password[MONGOC_SCRAM_HASH_SIZE];
66    uint32_t socketcheckintervalms;
67    mongoc_uri_t *uri;
68    unsigned requires_auth : 1;
69 
70    mongoc_client_t *client;
71 
72    mongoc_set_t *nodes;
73    mongoc_array_t iov;
74 } mongoc_cluster_t;
75 
76 void
77 mongoc_cluster_init (mongoc_cluster_t *cluster,
78                      const mongoc_uri_t *uri,
79                      void *client);
80 
81 void
82 mongoc_cluster_destroy (mongoc_cluster_t *cluster);
83 
84 void
85 mongoc_cluster_disconnect_node (mongoc_cluster_t *cluster,
86                                 uint32_t id,
87                                 bool invalidate,
88                                 const bson_error_t *why);
89 
90 int32_t
91 mongoc_cluster_get_max_bson_obj_size (mongoc_cluster_t *cluster);
92 
93 int32_t
94 mongoc_cluster_get_max_msg_size (mongoc_cluster_t *cluster);
95 
96 int32_t
97 mongoc_cluster_node_max_wire_version (mongoc_cluster_t *cluster,
98                                       uint32_t server_id);
99 
100 size_t
101 _mongoc_cluster_buffer_iovec (mongoc_iovec_t *iov,
102                               size_t iovcnt,
103                               int skip,
104                               char *buffer);
105 
106 bool
107 mongoc_cluster_check_interval (mongoc_cluster_t *cluster, uint32_t server_id);
108 
109 bool
110 mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
111                                 mongoc_rpc_t *rpcs,
112                                 mongoc_server_stream_t *server_stream,
113                                 const mongoc_write_concern_t *write_concern,
114                                 bson_error_t *error);
115 
116 bool
117 mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
118                          mongoc_rpc_t *rpc,
119                          mongoc_buffer_t *buffer,
120                          mongoc_server_stream_t *server_stream,
121                          bson_error_t *error);
122 
123 mongoc_server_stream_t *
124 mongoc_cluster_stream_for_reads (mongoc_cluster_t *cluster,
125                                  const mongoc_read_prefs_t *read_prefs,
126                                  bson_error_t *error);
127 
128 mongoc_server_stream_t *
129 mongoc_cluster_stream_for_writes (mongoc_cluster_t *cluster,
130                                   bson_error_t *error);
131 
132 mongoc_server_stream_t *
133 mongoc_cluster_stream_for_server (mongoc_cluster_t *cluster,
134                                   uint32_t server_id,
135                                   bool reconnect_ok,
136                                   bson_error_t *error);
137 
138 bool
139 mongoc_cluster_run_command_monitored (mongoc_cluster_t *cluster,
140                                       mongoc_cmd_parts_t *parts,
141                                       mongoc_server_stream_t *server_stream,
142                                       bson_t *reply,
143                                       bson_error_t *error);
144 
145 bool
146 mongoc_cluster_run_command_private (mongoc_cluster_t *cluster,
147                                     mongoc_cmd_parts_t *parts,
148                                     mongoc_stream_t *stream,
149                                     uint32_t server_id,
150                                     bson_t *reply,
151                                     bson_error_t *error);
152 
153 void
154 _mongoc_cluster_build_sasl_start (bson_t *cmd,
155                                   const char *mechanism,
156                                   const char *buf,
157                                   uint32_t buflen);
158 
159 void
160 _mongoc_cluster_build_sasl_continue (bson_t *cmd,
161                                      int conv_id,
162                                      const char *buf,
163                                      uint32_t buflen);
164 
165 int
166 _mongoc_cluster_get_conversation_id (const bson_t *reply);
167 
168 BSON_END_DECLS
169 
170 
171 #endif /* MONGOC_CLUSTER_PRIVATE_H */
172