Prechádzať zdrojové kódy

Merge pull request #229 from kamailio/vseva/usrloc_server_id

usrloc|nathelper: add filtering by server_id support
Victor Seva 10 rokov pred
rodič
commit
aa02102cec

+ 7 - 3
modules/nathelper/nathelper.c

@@ -358,7 +358,8 @@ static unsigned int raw_ip = 0;
 static unsigned short raw_port = 0;
 static int nh_keepalive_timeout = 0;
 static request_method_t sipping_method_id = 0;
-
+/* filter contacts by server_id */
+static int nh_filter_srvid = 0;
 
 /*0-> disabled, 1 ->enabled*/
 unsigned int *natping_state=0;
@@ -426,6 +427,7 @@ static param_export_t params[] = {
 	{"keepalive_timeout",     INT_PARAM, &nh_keepalive_timeout  },
 	{"udpping_from_path",     INT_PARAM, &udpping_from_path     },
 	{"append_sdp_oldmediaip", INT_PARAM, &sdp_oldmediaip        },
+	{"filter_server_id",      INT_PARAM, &nh_filter_srvid },
 
 	{0, 0, 0}
 };
@@ -2067,6 +2069,7 @@ nh_timer(unsigned int ticks, void *timer_idx)
 	char *path_ip_str = NULL;
 	unsigned int path_ip = 0;
 	unsigned short path_port = 0;
+	int options = 0;
 
 	if((*natping_state) == 0)
 		goto done;
@@ -2079,9 +2082,10 @@ nh_timer(unsigned int ticks, void *timer_idx)
 			goto done;
 		}
 	}
+	if(nh_filter_srvid) options |= GAU_OPT_SERVER_ID;
 	rval = ul.get_all_ucontacts(buf, cblen, (ping_nated_only?ul.nat_flag:0),
 		((unsigned int)(unsigned long)timer_idx)*natping_interval+iteration,
-		natping_processes*natping_interval);
+		natping_processes*natping_interval, options);
 	if (rval<0) {
 		LM_ERR("failed to fetch contacts\n");
 		goto done;
@@ -2097,7 +2101,7 @@ nh_timer(unsigned int ticks, void *timer_idx)
 		}
 		rval = ul.get_all_ucontacts(buf,cblen,(ping_nated_only?ul.nat_flag:0),
 		   ((unsigned int)(unsigned long)timer_idx)*natping_interval+iteration,
-		   natping_processes*natping_interval);
+		   natping_processes*natping_interval, options);
 		if (rval != 0) {
 			pkg_free(buf);
 			goto done;

+ 25 - 8
modules/usrloc/dlist.c

@@ -85,10 +85,12 @@ static inline int find_dlist(str* _n, dlist_t** _d)
  * \param flags contact flags
  * \param part_idx part index
  * \param part_max maximal part
+ * \param GAU options
  * \return 0 on success, positive if buffer size was not sufficient, negative on failure
  */
 static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
-								unsigned int part_idx, unsigned int part_max)
+								unsigned int part_idx, unsigned int part_max,
+								int options)
 {
 	struct socket_info *sock;
 	unsigned int dbflags;
@@ -108,9 +110,9 @@ static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
 	int i;
 	void *cp;
 	int shortage, needed;
-	db_key_t keys1[3]; /* where */
-	db_val_t vals1[3];
-	db_op_t  ops1[3];
+	db_key_t keys1[4]; /* where */
+	db_val_t vals1[4];
+	db_op_t  ops1[4];
 	db_key_t keys2[6]; /* select */
 	int n[2] = {2,6}; /* number of dynamic values used on key1/key2 */
 
@@ -160,6 +162,14 @@ static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
 		vals1[n[0]].val.int_val = 1;
 		n[0]++;
 	}
+	if(options&GAU_OPT_SERVER_ID) {
+		keys1[n[0]] = &srv_id_col;
+		ops1[n[0]] = OP_EQ;
+		vals1[n[0]].type = DB1_INT;
+		vals1[n[0]].nul = 0;
+		vals1[n[0]].val.int_val = server_id;
+		n[0]++;
+	}
 
 	/* TODO: use part_idx and part_max on keys1 */
 
@@ -323,10 +333,12 @@ static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
  * \param flags contact flags
  * \param part_idx part index
  * \param part_max maximal part
+ * \param GAU options
  * \return 0 on success, positive if buffer size was not sufficient, negative on failure
  */
 static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags,
-								unsigned int part_idx, unsigned int part_max)
+								unsigned int part_idx, unsigned int part_max,
+								int options)
 {
 	dlist_t *p;
 	urecord_t *r;
@@ -370,6 +382,9 @@ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags,
 					if ((c->cflags & flags) != flags)
 						continue;
 
+					if(options&GAU_OPT_SERVER_ID && server_id!=c->server_id)
+						continue;
+
 					if(ul_keepalive_timeout>0 && c->last_keepalive>0)
 					{
 						if(c->sock!=NULL && c->sock->proto==PROTO_UDP)
@@ -468,15 +483,17 @@ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags,
  * \param flags contact flags
  * \param part_idx part index
  * \param part_max maximal part
+ * \param GAU options
  * \return 0 on success, positive if buffer size was not sufficient, negative on failure
  */
 int get_all_ucontacts(void *buf, int len, unsigned int flags,
-								unsigned int part_idx, unsigned int part_max)
+								unsigned int part_idx, unsigned int part_max,
+								int options)
 {
 	if (db_mode==DB_ONLY)
-		return get_all_db_ucontacts( buf, len, flags, part_idx, part_max);
+		return get_all_db_ucontacts( buf, len, flags, part_idx, part_max, options);
 	else
-		return get_all_mem_ucontacts( buf, len, flags, part_idx, part_max);
+		return get_all_mem_ucontacts( buf, len, flags, part_idx, part_max, options);
 }
 
 

+ 2 - 1
modules/usrloc/dlist.h

@@ -109,10 +109,11 @@ int synchronize_all_udomains(int istart, int istep);
  * \param flags contact flags
  * \param part_idx part index
  * \param part_max maximal part
+ * \param GAU options
  * \return 0 on success, positive if buffer size was not sufficient, negative on failure
  */
 int get_all_ucontacts(void *buf, int len, unsigned int flags,
-		unsigned int part_idx, unsigned int part_max);
+		unsigned int part_idx, unsigned int part_max, int options);
 
 
 /*!

+ 22 - 4
modules/usrloc/udomain.c

@@ -379,6 +379,9 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d)
 	db_row_t *row;
 	db_key_t columns[21];
 	db1_res_t* res = NULL;
+	db_key_t keys[1]; /* where */
+	db_val_t vals[1];
+	db_op_t  ops[1];
 	str user, contact;
 	char* domain;
 	int i;
@@ -418,9 +421,21 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d)
 	LM_NOTICE("load start time [%d]\n", (int)time(NULL));
 #endif
 
+	if (ul_db_srvid) {
+		LM_NOTICE("filtered by server_id[%d]\n", server_id);
+		keys[0] = &srv_id_col;
+		ops[0] = OP_EQ;
+		vals[0].type = DB1_INT;
+		vals[0].nul = 0;
+		vals[0].val.int_val = server_id;
+	}
+
 	if (DB_CAPABILITY(ul_dbf, DB_CAP_FETCH)) {
-		if (ul_dbf.query(_c, 0, 0, 0, columns, 0, (use_domain)?(21):(20), 0,
-		0) < 0) {
+		if (ul_dbf.query(_c, (ul_db_srvid)?(keys):(0),
+							(ul_db_srvid)?(ops):(0), (ul_db_srvid)?(vals):(0),
+							columns, (ul_db_srvid)?(1):(0),
+							(use_domain)?(21):(20), 0, 0) < 0)
+		{
 			LM_ERR("db_query (1) failed\n");
 			return -1;
 		}
@@ -429,8 +444,11 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d)
 			return -1;
 		}
 	} else {
-		if (ul_dbf.query(_c, 0, 0, 0, columns, 0, (use_domain)?(21):(20), 0,
-		&res) < 0) {
+		if (ul_dbf.query(_c, (ul_db_srvid)?(keys):(0),
+							(ul_db_srvid)?(ops):(0), (ul_db_srvid)?(vals):(0),
+							columns, (ul_db_srvid)?(1):(0),
+							(use_domain)?(21):(20), 0, &res) < 0)
+		{
 			LM_ERR("db_query failed\n");
 			return -1;
 		}

+ 3 - 1
modules/usrloc/ul_mod.c

@@ -173,7 +173,8 @@ unsigned int init_flag = 0;
 db1_con_t* ul_dbh = 0; /* Database connection handle */
 db_func_t ul_dbf;
 
-
+/* filter on load by server id */
+unsigned int ul_db_srvid = 0;
 
 /*! \brief
  * Exported functions
@@ -229,6 +230,7 @@ static param_export_t params[] = {
 	{"expires_type",        PARAM_INT, &ul_expires_type},
 	{"db_raw_fetch_type",   PARAM_INT, &ul_db_raw_fetch_type},
 	{"db_insert_null",      PARAM_INT, &ul_db_insert_null},
+	{"server_id_filter",    PARAM_INT, &ul_db_srvid},
 	{0, 0, 0}
 };
 

+ 2 - 0
modules/usrloc/ul_mod.h

@@ -98,6 +98,8 @@ extern str ul_xavp_contact_name;
 extern db1_con_t* ul_dbh;   /* Database connection handle */
 extern db_func_t ul_dbf;
 
+/* filter on load by server id */
+extern unsigned int ul_db_srvid;
 
 /*
  * Matching algorithms

+ 3 - 1
modules/usrloc/usrloc.h

@@ -40,6 +40,8 @@
 #define DB_ONLY       3
 #define DB_READONLY   4
 
+#define GAU_OPT_SERVER_ID  (1<<0)  /* filter query by server_id */
+
 /*forward declaration necessary for udomain*/
 
 struct udomain;
@@ -181,7 +183,7 @@ typedef void (*unlock_udomain_t)(struct udomain* _d, str *_aor);
 typedef int (*register_udomain_t)(const char* _n, struct udomain** _d);
 
 typedef int  (*get_all_ucontacts_t) (void* buf, int len, unsigned int flags,
-		unsigned int part_idx, unsigned int part_max);
+		unsigned int part_idx, unsigned int part_max, int options);
 
 typedef int (*get_udomain_t)(const char* _n, udomain_t** _d);