26 RCSID(
"$Id: f8ce8bc28a738ff71498ad83a8bed032c5d8118d $")
28 #define LOG_PREFIX "couchbase"
30 #include <freeradius-devel/server/base.h>
31 #include <freeradius-devel/json/base.h>
44 if (error != LCB_SUCCESS) {
46 ERROR(
"(stats_callback) %s (0x%x)", lcb_strerror(instance, error), error);
62 lcb_error_t error,
const lcb_store_resp_t *resp)
64 if (error != LCB_SUCCESS) {
66 ERROR(
"(store_callback) %s (0x%x)", lcb_strerror(instance, error), error);
86 const char *bytes = resp->v.v0.bytes;
87 lcb_size_t nbytes = resp->v.v0.nbytes;
93 if (bytes && nbytes > 1) {
95 DEBUG(
"(get_callback) got %zu bytes", nbytes);
97 c->
jobj = json_tokener_parse_ex(c->
jtok, bytes, nbytes);
99 switch ((c->
jerr = json_tokener_get_error(c->
jtok))) {
100 case json_tokener_continue:
102 if (c->
jobj != NULL) {
103 ERROR(
"(get_callback) object not null on continue!");
106 case json_tokener_success:
111 ERROR(
"(get_callback) json parsing error: %s",
112 json_tokener_error_desc(c->
jerr));
120 DEBUG(
"(get_callback) key does not exist");
125 ERROR(
"(get_callback) %s (0x%x)", lcb_strerror(instance, error), error);
139 lcb_error_t error,
const lcb_http_resp_t *resp)
144 const char *bytes = resp->v.v0.bytes;
145 lcb_size_t nbytes = resp->v.v0.nbytes;
151 if (bytes && nbytes > 1) {
153 DEBUG(
"(http_data_callback) got %zu bytes", nbytes);
155 c->
jobj = json_tokener_parse_ex(c->
jtok, bytes, nbytes);
157 switch ((c->
jerr = json_tokener_get_error(c->
jtok))) {
158 case json_tokener_continue:
160 if (c->
jobj != NULL)
ERROR(
"(http_data_callback) object not null on continue!");
162 case json_tokener_success:
167 ERROR(
"(http_data_callback) json parsing error: %s", json_tokener_error_desc(c->
jerr));
175 ERROR(
"(http_data_callback) %s (0x%x)", lcb_strerror(instance, error), error);
200 struct lcb_create_st options;
203 memset(&options, 0,
sizeof(options));
206 options.v.v0.host = host;
207 options.v.v0.bucket = bucket;
208 options.v.v0.user = user;
209 options.v.v0.passwd = pass;
212 error = lcb_create(instance, &options);
213 if (error != LCB_SUCCESS)
return error;
215 error = lcb_cntl(*instance, LCB_CNTL_SET, LCB_CNTL_CONFIGURATION_TIMEOUT, &
timeout);
216 if (error != LCB_SUCCESS)
return error;
222 for (; o != NULL; o = o->
next) {
223 error = lcb_cntl_string(*instance, o->
key, o->
val);
224 if (error != LCB_SUCCESS) {
225 ERROR(
"Failed to configure the couchbase with %s=%s", o->
key, o->
val);
232 error = lcb_connect(*instance);
233 if (error != LCB_SUCCESS)
return error;
257 lcb_server_stats_cmd_t cmd;
258 const lcb_server_stats_cmd_t *
commands[1];
262 memset(&cmd, 0,
sizeof(cmd));
265 cmd.v.v0.name =
"tap";
266 cmd.v.v0.nname = strlen(cmd.v.v0.name);
269 if ((error = lcb_server_stats(instance, cookie, 1,
commands)) == LCB_SUCCESS) {
288 lcb_error_t
couchbase_set_key(lcb_t instance,
const char *key,
const char *document,
int expire)
296 memset(&cmd, 0,
sizeof(cmd));
300 cmd.v.v0.nkey = strlen(cmd.v.v0.key);
301 cmd.v.v0.bytes = document;
302 cmd.v.v0.nbytes = strlen(cmd.v.v0.bytes);
303 cmd.v.v0.exptime = expire;
304 cmd.v.v0.operation = LCB_SET;
307 if ((error = lcb_store(instance, NULL, 1,
commands)) == LCB_SUCCESS) {
336 memset(&cmd, 0,
sizeof(cmd));
340 cmd.v.v0.nkey = strlen(cmd.v.v0.key);
346 c->
jerr = json_tokener_success;
349 c->
jtok = json_tokener_new();
352 DEBUG3(
"fetching document %s", key);
355 if ((error = lcb_get(instance, c, 1,
commands)) == LCB_SUCCESS) {
361 json_tokener_free(c->
jtok);
387 memset(&cmd, 0,
sizeof(cmd));
390 cmd.v.v0.path = path;
391 cmd.v.v0.npath = strlen(cmd.v.v0.path);
392 cmd.v.v0.body = post;
393 cmd.v.v0.nbody = post ? strlen(post) : 0;
394 cmd.v.v0.method = post ? LCB_HTTP_METHOD_POST : LCB_HTTP_METHOD_GET;
395 cmd.v.v0.chunked = 1;
396 cmd.v.v0.content_type =
"application/json";
402 c->
jerr = json_tokener_success;
405 c->
jtok = json_tokener_new();
408 DEBUG3(
"fetching view %s", path);
411 if ((error = lcb_make_http_request(instance, c, LCB_HTTP_TYPE_VIEW,
commands, NULL)) == LCB_SUCCESS) {
417 json_tokener_free(c->
jtok);
lcb_error_t couchbase_query_view(lcb_t instance, const void *cookie, const char *path, const char *post)
Query a Couchbase design document view.
void couchbase_store_callback(lcb_t instance, const void *cookie, lcb_storage_t operation, lcb_error_t error, const lcb_store_resp_t *resp)
Couchbase callback for store (write) operations.
lcb_error_t couchbase_server_stats(lcb_t instance, const void *cookie)
Request Couchbase server statistics.
lcb_error_t couchbase_init_connection(lcb_t *instance, const char *host, const char *bucket, const char *user, const char *pass, lcb_uint32_t timeout, const couchbase_opts_t *opts)
Initialize a Couchbase connection instance.
lcb_error_t couchbase_set_key(lcb_t instance, const char *key, const char *document, int expire)
Store a document by key in Couchbase.
void couchbase_http_data_callback(lcb_http_request_t request, lcb_t instance, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
Couchbase callback for http (view) operations.
void couchbase_stat_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_server_stat_resp_t *resp)
Couchbase callback for cluster statistics requests.
void couchbase_get_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp)
Couchbase callback for get (read) operations.
lcb_error_t couchbase_get_key(lcb_t instance, const void *cookie, const char *key)
Retrieve a document by key from Couchbase.
Couchbase wrapper function prototypes and datatypes.
json_tokener * jtok
JSON tokener objects handled by the json-c library.
json_object * jobj
JSON objects handled by the json-c library.
const void * cdata
Constant pointer to cookie payload (cookie_t).
void * data
Non-constant pointer to data payload (cookie_t).
char * val
Value for the key used in lcb_cntl_string().
char * key
Key value for lcb_cntl_string().
enum json_tokener_error jerr
Error values produced by the json-c library.
couchbase_opts_t * next
Linked list.
Information relating to the parsing of Couchbase document payloads.
Union of constant and non-constant pointers.
static fr_time_delta_t timeout
static fr_table_ptr_sorted_t commands[]