ソースを参照

kazoo : compatibility issues with json-c & rabbitmq recent versions

Luis Azedo 11 年 前
コミット
602c4a7cdb

+ 25 - 3
modules/kazoo/Makefile

@@ -5,11 +5,33 @@
 include ../../Makefile.defs
 auto_gen=
 NAME=kazoo.so
+JLIB=json
 
-DEFS+=-I$(LOCALBASE)/include -I$(SYSBASE)/include -DKAMAILIO_MOD_INTERFACE
-LIBS=-L$(LOCALBASE)/lib -I$(SYSBASE)/lib -lrabbitmq -ljson -luuid
+BUILDER = $(shell which pkg-config)
+ifeq ($(BUILDER),)
+	JSONC=$(shell ls $(SYSBASE)/include/lib/libjson*.so $(LOCALBASE)/lib/libjson*.so 2>/dev/null | grep json-c)
+else
+	JSONC=$(shell pkg-config --libs json-c 2>/dev/null | grep json-c)
+endif
 
-DEFS += -DSER_MOD_INTERFACE
+ifneq ($(JSONC),)
+	JLIB=json-c
+endif
+
+ifeq ($(BUILDER),)
+	DEFS+=-I$(LOCALBASE)/include -I$(SYSBASE)/include \
+		-I$(SYSBASE)/include/$(JLIB)
+	LIBS=-L$(LOCALBASE)/lib -I$(SYSBASE)/lib -lrabbitmq -l$(JLIB) -luuid
+else
+	DEFS += $(shell $(BUILDER) --cflags $(JLIB))
+	LIBS += $(shell $(BUILDER) --libs $(JLIB))
+	DEFS += $(shell $(BUILDER) --cflags uuid)
+	LIBS += $(shell $(BUILDER) --libs uuid)
+	DEFS += $(shell $(BUILDER) --cflags librabbitmq)
+	LIBS += $(shell $(BUILDER) --libs librabbitmq)
+endif
+
+DEFS += -DKAMAILIO_MOD_INTERFACE
 
 
 SERLIBPATH=../../lib

+ 1 - 1
modules/kazoo/defs.h

@@ -103,7 +103,7 @@
 </dialog-info>"
 
 #define json_extract_field(json_name, field)  do {                      \
-    struct json_object* obj = json_object_object_get(json_obj, json_name); \
+    struct json_object* obj =  kz_json_get_object(json_obj, json_name); \
     field.s = (char*)json_object_get_string(obj);                       \
     if (field.s == NULL) {                                              \
       LM_DBG("Json-c error - failed to extract field [%s]\n", json_name); \

+ 39 - 59
modules/kazoo/kz_amqp.c

@@ -4,7 +4,7 @@
 #include <amqp.h>
 #include <amqp_framing.h>
 #include <amqp_tcp_socket.h>
-#include <json/json.h>
+#include <json.h>
 #include <uuid/uuid.h>
 #include "../../mem/mem.h"
 #include "../../timer_proc.h"
@@ -15,6 +15,7 @@
 
 
 #include "kz_amqp.h"
+#include "kz_json.h"
 
 #define RET_AMQP_ERROR 2
 
@@ -43,6 +44,10 @@ extern int dbk_consumer_ack_loop_count;
 extern int dbk_single_consumer_on_reconnect;
 extern int dbk_consume_messages_on_reconnect;
 
+const amqp_bytes_t kz_amqp_empty_bytes = { 0, NULL };
+const amqp_table_t kz_amqp_empty_table = { 0, NULL };
+
+
 static char *kz_amqp_str_dup(str *src)
 {
 	char *res;
@@ -599,8 +604,6 @@ void kz_amqp_add_payload_common_properties(json_obj_ptr json_obj, char* server_i
     sprintf(node_name, "kamailio@%.*s", dbk_node_hostname.len, dbk_node_hostname.s);
     json_object_object_add(json_obj, BLF_JSON_NODE,
 			   json_object_new_string(node_name));
-//    json_object_object_add(json_obj, BLF_JSON_SERVERID,
-//			   json_object_new_string(server_id));
     json_object_object_add(json_obj, BLF_JSON_MSG_ID,
 			   json_object_new_string_len(unique->s, unique->len));
 
@@ -627,13 +630,9 @@ int kz_amqp_pipe_send(str *str_exchange, str *str_routing_key, str *str_payload)
 
 
     /* parse json  and add extra fields */
-    json_obj = json_tokener_parse(str_payload->s);
-    if (is_error(json_obj))
-    {
-		LM_ERR("Error parsing json: %s\n",json_tokener_errors[-(unsigned long)json_obj]);
-		LM_ERR("%s\n", str_payload->s);
-		goto error;
-    }
+    json_obj = kz_json_parse(str_payload->s);
+    if (json_obj == NULL)
+	goto error;
 
     kz_amqp_add_payload_common_properties(json_obj, serverid, &unique_string);
 
@@ -701,13 +700,9 @@ int kz_amqp_pipe_send_receive(str *str_exchange, str *str_routing_key, str *str_
 
 
     /* parse json  and add extra fields */
-    json_obj = json_tokener_parse(str_payload->s);
-    if (is_error(json_obj))
-    {
-		LM_ERR("Error parsing json: %s\n",json_tokener_errors[-(unsigned long)json_obj]);
-		LM_ERR("%s\n", str_payload->s);
-		goto error;
-    }
+    json_obj = kz_json_parse(str_payload->s);
+    if (json_obj == NULL)
+	goto error;
 
     kz_amqp_add_payload_common_properties(json_obj, serverid, &unique_string);
 
@@ -743,13 +738,9 @@ int kz_amqp_pipe_send_receive(str *str_exchange, str *str_routing_key, str *str_
 		lock_get(&cmd->lock);
 		switch(cmd->return_code) {
 		case AMQP_RESPONSE_NORMAL:
-			json_body = json_tokener_parse(cmd->return_payload);
-		    if (is_error(json_body))
-		    {
-				LM_ERR("Error parsing body json: %s\n",json_tokener_errors[-(unsigned long)json_body]);
-				LM_ERR("JSON : %s\n", cmd->return_payload);
-				goto error;
-		    }
+			json_body = kz_json_parse(cmd->return_payload);
+		    if (json_body == NULL)
+			goto error;
 		    *json_ret = json_body;
 		    ret = 0;
 		    break;
@@ -979,13 +970,10 @@ int kz_amqp_subscribe(struct sip_msg* msg, char* payload)
 		return -1;
 	}
 
-    json_obj = json_tokener_parse(payload_s.s);
-    if (is_error(json_obj))
-    {
-		LM_ERR("Error parsing json: %s\n",json_tokener_errors[-(unsigned long)json_obj]);
-		LM_ERR("%s\n", payload_s.s);
-		return -1;
-    }
+    json_obj = kz_json_parse(payload_s.s);
+    if (json_obj == NULL)
+	return -1;
+    
 
     json_extract_field("exchange", exchange_s);
     json_extract_field("type", exchange_type_s);
@@ -994,32 +982,32 @@ int kz_amqp_subscribe(struct sip_msg* msg, char* payload)
     json_extract_field("event_key", key_s);
     json_extract_field("event_subkey", subkey_s);
 
-    tmpObj = json_object_object_get(json_obj, "passive");
+    tmpObj = kz_json_get_object(json_obj, "passive");
     if(tmpObj != NULL) {
     	passive = json_object_get_int(tmpObj);
     }
 
-    tmpObj = json_object_object_get(json_obj, "durable");
+    tmpObj = kz_json_get_object(json_obj, "durable");
     if(tmpObj != NULL) {
     	durable = json_object_get_int(tmpObj);
     }
 
-    tmpObj = json_object_object_get(json_obj, "exclusive");
+    tmpObj = kz_json_get_object(json_obj, "exclusive");
     if(tmpObj != NULL) {
     	exclusive = json_object_get_int(tmpObj);
     }
 
-    tmpObj = json_object_object_get(json_obj, "auto_delete");
+    tmpObj = kz_json_get_object(json_obj, "auto_delete");
     if(tmpObj != NULL) {
     	auto_delete = json_object_get_int(tmpObj);
     }
 
-    tmpObj = json_object_object_get(json_obj, "no_ack");
+    tmpObj = kz_json_get_object(json_obj, "no_ack");
     if(tmpObj != NULL) {
     	no_ack = json_object_get_int(tmpObj);
     }
 
-    tmpObj = json_object_object_get(json_obj, "wait_for_consumer_ack");
+    tmpObj = kz_json_get_object(json_obj, "wait_for_consumer_ack");
     if(tmpObj != NULL) {
     	wait_for_consumer_ack = json_object_get_int(tmpObj);
     }
@@ -1194,26 +1182,26 @@ int kz_amqp_bind_targeted_channel(kz_amqp_conn_ptr kz_conn, int loopcount, int i
 		goto error;
     }
 
-    r = amqp_queue_declare(kz_conn->conn, channels[idx].channel, bind->queue, 0, 0, 1, 1, amqp_empty_table);
+    r = amqp_queue_declare(kz_conn->conn, channels[idx].channel, bind->queue, 0, 0, 1, 1, kz_amqp_empty_table);
     if (kz_amqp_error("Declaring queue", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		goto error;
     }
 
-	amqp_exchange_declare(kz_conn->conn, channels[idx].channel, bind->exchange, bind->exchange_type, 0, 0, amqp_empty_table);
+	amqp_exchange_declare(kz_conn->conn, channels[idx].channel, bind->exchange, bind->exchange_type, 0, 0, kz_amqp_empty_table);
     if (kz_amqp_error("Declaring exchange", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		ret = -RET_AMQP_ERROR;
 		goto error;
     }
 
-    if (amqp_queue_bind(kz_conn->conn, channels[idx].channel, bind->queue, bind->exchange, bind->routing_key, amqp_empty_table) < 0
+    if (amqp_queue_bind(kz_conn->conn, channels[idx].channel, bind->queue, bind->exchange, bind->routing_key, kz_amqp_empty_table) < 0
 	    || kz_amqp_error("Binding queue", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		goto error;
     }
 
-    if (amqp_basic_consume(kz_conn->conn, channels[idx].channel, bind->queue, amqp_empty_bytes, 0, 1, 1, amqp_empty_table) < 0
+    if (amqp_basic_consume(kz_conn->conn, channels[idx].channel, bind->queue, kz_amqp_empty_bytes, 0, 1, 1, kz_amqp_empty_table) < 0
 	    || kz_amqp_error("Consuming", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		goto error;
@@ -1244,14 +1232,14 @@ int kz_amqp_bind_consumer(kz_amqp_conn_ptr kz_conn, kz_amqp_bind_ptr bind)
 
     int	idx = get_channel_index();
 
-    amqp_queue_declare(kz_conn->conn, channels[idx].channel, bind->queue, bind->passive, bind->durable, bind->exclusive, bind->auto_delete, amqp_empty_table);
+    amqp_queue_declare(kz_conn->conn, channels[idx].channel, bind->queue, bind->passive, bind->durable, bind->exclusive, bind->auto_delete, kz_amqp_empty_table);
     if (kz_amqp_error("Declaring queue", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		ret = -RET_AMQP_ERROR;
 		goto error;
     }
 
-	amqp_exchange_declare(kz_conn->conn, channels[idx].channel, bind->exchange, bind->exchange_type, 0, 0, amqp_empty_table);
+	amqp_exchange_declare(kz_conn->conn, channels[idx].channel, bind->exchange, bind->exchange_type, 0, 0, kz_amqp_empty_table);
     if (kz_amqp_error("Declaring exchange", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		ret = -RET_AMQP_ERROR;
@@ -1259,7 +1247,7 @@ int kz_amqp_bind_consumer(kz_amqp_conn_ptr kz_conn, kz_amqp_bind_ptr bind)
     }
 
     LM_DBG("QUEUE BIND\n");
-    if (amqp_queue_bind(kz_conn->conn, channels[idx].channel, bind->queue, bind->exchange, bind->routing_key, amqp_empty_table) < 0
+    if (amqp_queue_bind(kz_conn->conn, channels[idx].channel, bind->queue, bind->exchange, bind->routing_key, kz_amqp_empty_table) < 0
 	    || kz_amqp_error("Binding queue", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		ret = -RET_AMQP_ERROR;
@@ -1267,7 +1255,7 @@ int kz_amqp_bind_consumer(kz_amqp_conn_ptr kz_conn, kz_amqp_bind_ptr bind)
     }
 
     LM_DBG("BASIC CONSUME\n");
-    if (amqp_basic_consume(kz_conn->conn, channels[idx].channel, bind->queue, amqp_empty_bytes, 0, bind->no_ack, 0, amqp_empty_table) < 0
+    if (amqp_basic_consume(kz_conn->conn, channels[idx].channel, bind->queue, kz_amqp_empty_bytes, 0, bind->no_ack, 0, kz_amqp_empty_table) < 0
 	    || kz_amqp_error("Consuming", amqp_get_rpc_reply(kz_conn->conn)))
     {
 		ret = -RET_AMQP_ERROR;
@@ -1308,15 +1296,11 @@ int kz_amqp_send_ex(kz_amqp_conn_ptr kz_conn, kz_amqp_cmd_ptr cmd, kz_amqp_chann
     routing_key = amqp_bytes_malloc_dup(amqp_cstring_bytes(cmd->routing_key));
     payload = amqp_bytes_malloc_dup(amqp_cstring_bytes(cmd->payload));
 
-    json_obj = json_tokener_parse(cmd->payload);
-    if (is_error(json_obj))
-    {
-		LM_ERR("Error parsing json: %s\n",json_tokener_errors[-(unsigned long)json_obj]);
-		LM_ERR("%s\n", cmd->payload);
-		goto error;
-    }
+    json_obj = kz_json_parse(cmd->payload);
+    if (json_obj == NULL)
+	goto error;
 
-    if(json_object_object_get(json_obj, BLF_JSON_SERVERID) == NULL) {
+    if(kz_json_get_object(json_obj, BLF_JSON_SERVERID) == NULL) {
         json_object_object_add(json_obj, BLF_JSON_SERVERID, json_object_new_string((char*)channels[idx].targeted->routing_key.bytes));
     	amqp_bytes_free(payload);
         payload = amqp_bytes_malloc_dup(amqp_cstring_bytes((char*)json_object_to_json_string(json_obj)));
@@ -1412,13 +1396,9 @@ void kz_amqp_consumer_event(int child_no, char *payload, char* event_key, char*
 
     eventData = payload;
 
-    json_obj = json_tokener_parse(payload);
-    if (is_error(json_obj))
-    {
-		LM_ERR("Error parsing json: %s\n",json_tokener_errors[-(unsigned long)json_obj]);
-		LM_ERR("%s\n", payload);
+    json_obj = kz_json_parse(payload);
+    if (json_obj == NULL)
 		return;
-    }
 
     char* key = (event_key == NULL ? dbk_consumer_event_key.s : event_key);
     char* subkey = (event_subkey == NULL ? dbk_consumer_event_subkey.s : event_subkey);

+ 32 - 2
modules/kazoo/kz_json.c

@@ -24,7 +24,6 @@
 
 #include <stdio.h>
 #include <string.h>
-#include <json/json.h>
 
 #include "../../mod_fix.h"
 #include "../../lvalue.h"
@@ -124,7 +123,7 @@ int kz_json_get_field_ex(str* json, str* field, pv_value_p dst_val)
 				int sresult = sscanf(field.s, "%[^[][%[^]]]", f1, f2); //, f3);
 				LM_DBG("CHECK IDX %d - %s , %s, %s\n", sresult, field.s, f1, f2);
 
-				jtree = json_object_object_get(jtree, f1);
+				jtree = kz_json_get_object(jtree, f1);
 				if(jtree != NULL) {
 					char *value = (char*)json_object_get_string(jtree);
 					LM_DBG("JTREE OK %s\n", value);
@@ -191,3 +190,34 @@ int kz_json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
 
 	return 1;
 }
+
+struct json_object* kz_json_parse(const char *str)
+{
+    struct json_tokener* tok;
+    struct json_object* obj;
+
+    tok = json_tokener_new();
+    if (!tok) {
+      LM_ERR("Error parsing json: cpuld not allocate tokener\n");
+      return NULL;
+    }
+
+    obj = json_tokener_parse_ex(tok, str, -1);
+    if(tok->err != json_tokener_success) {        
+      LM_ERR("Error parsing json: %s\n", json_tokener_error_desc(tok->err));
+      LM_ERR("%s\n", str);
+      if (obj != NULL)
+	   json_object_put(obj);
+      obj = NULL;
+    }
+
+    json_tokener_free(tok);
+    return obj;
+}
+
+struct json_object* kz_json_get_object(struct json_object* jso, const char *key)
+{
+	struct json_object *result = NULL;
+	json_object_object_get_ex(jso, key, &result);
+	return result;
+}

+ 3 - 1
modules/kazoo/kz_json.h

@@ -9,11 +9,13 @@
 #define KZ_JSON_H_
 
 #include "../../parser/msg_parser.h"
+#include <json.h>
 
 
 int kz_json_get_field(struct sip_msg* msg, char* json, char* field, char* dst);
 int kz_json_get_field_ex(str* json, str* field, pv_value_p dst_val);
 
-
+struct json_object* kz_json_parse(const char *str);
+struct json_object* kz_json_get_object(struct json_object* jso, const char *key);
 
 #endif /* KZ_JSON_H_ */

+ 8 - 9
modules/kazoo/kz_pua.c

@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
-#include <json/json.h>
+#include <json-c/json.h>
 #include "../../mem/mem.h"
 #include "../../timer_proc.h"
 #include "../../sr_module.h"
@@ -16,6 +16,7 @@
 #include "kz_pua.h"
 #include "defs.h"
 #include "const.h"
+#include "kz_json.h"
 
 
 extern int dbk_include_entity;
@@ -191,7 +192,7 @@ int kz_pua_publish_presence_to_presentity(struct json_object *json_obj) {
     json_extract_field(BLF_JSON_DIRECTION, direction);
     json_extract_field(BLF_JSON_STATE, state);
 
-    struct json_object* ExpiresObj = json_object_object_get(json_obj, BLF_JSON_EXPIRES);
+    struct json_object* ExpiresObj =  kz_json_get_object(json_obj, BLF_JSON_EXPIRES);
     if(ExpiresObj != NULL) {
     	expires = json_object_get_int(ExpiresObj);
     	if(expires > 0)
@@ -276,7 +277,7 @@ int kz_pua_publish_mwi_to_presentity(struct json_object *json_obj) {
     json_extract_field(MWI_JSON_URGENT_SAVED, mwi_urgent_saved);
     json_extract_field(MWI_JSON_ACCOUNT, mwi_account);
 
-    struct json_object* ExpiresObj = json_object_object_get(json_obj, BLF_JSON_EXPIRES);
+    struct json_object* ExpiresObj =  kz_json_get_object(json_obj, BLF_JSON_EXPIRES);
     if(ExpiresObj != NULL) {
     	expires = json_object_get_int(ExpiresObj);
     	if(expires > 0)
@@ -345,14 +346,14 @@ int kz_pua_publish_dialoginfo_to_presentity(struct json_object *json_obj) {
     json_extract_field(BLF_JSON_DIRECTION, direction);
     json_extract_field(BLF_JSON_STATE, state);
 
-    struct json_object* ExpiresObj = json_object_object_get(json_obj, BLF_JSON_EXPIRES);
+    struct json_object* ExpiresObj =  kz_json_get_object(json_obj, BLF_JSON_EXPIRES);
     if(ExpiresObj != NULL) {
     	expires = json_object_get_int(ExpiresObj);
     	if(expires > 0)
     		expires += (int)time(NULL);
     }
 
-    ExpiresObj = json_object_object_get(json_obj, "Flush-Level");
+    ExpiresObj =  kz_json_get_object(json_obj, "Flush-Level");
     if(ExpiresObj != NULL) {
     	reset = json_object_get_int(ExpiresObj);
     }
@@ -446,10 +447,8 @@ int kz_pua_publish(struct sip_msg* msg, char *json) {
     }
 
     /* extract info from json and construct xml */
-    json_obj = json_tokener_parse(json);
-    if (is_error(json_obj)) {
-    	LM_ERR("Error parsing json: %s\n", json_tokener_errors[-(unsigned long)json_obj]);
-    	LM_ERR("%s\n", json);
+    json_obj = kz_json_parse(json);
+    if (json_obj == NULL) {
     	ret = -1;
     	goto error;
     }