Browse code

lrkproxy: Add new features : TURN support, behind_nat support, subset of range ports

Mojtaba Esfandiari.S authored on 28/09/2022 10:55:26
Showing 3 changed files
... ...
@@ -18,7 +18,7 @@
18 18
 		<title>Overview</title>
19 19
 		<para>
20 20
 			This is a module that enables media streams to be relayed via
21
-			pylrkproxy engine. This engine can be found at:
21
+			pylrkproxy engine that exist in:
22 22
 			https://github.com/mojtabaesfandiari/pylrkproxy
23 23
 			It does relaying audio streams between peers in
24 24
 			PREROUTING netfilter-hooking section in kernel-space linux.
... ...
@@ -77,7 +77,7 @@
77 77
 		</para>
78 78
 		<para>
79 79
 			IMPORTANT: This module does not support balancing inside a set like as is done RTPProxy module based on
80
-			the weight of each rtpproxy from the set. The balancing would be run on different machine.
80
+			the weight of each rtpproxy from the set. The balancing would be run on different machine
81 81
 		</para>
82 82
 	</section>
83 83
 
... ...
@@ -322,9 +322,59 @@
322 322
 					</programlisting>
323 323
 				</example>
324 324
 			</section>
325
-
326
-
327
-
325
+			<section id="lrkproxy.p.behind_nat">
326
+				<title><varname>behind_nat</varname> (integer)</title>
327
+				<para>
328
+					The behind_nat is used when the LRKProxy is behind NAT firewall and the external IP should be as Advertised IP.
329
+					It could be set 0:disable or 1:enable.
330
+					If you use AWS for LRKProxy, you might should set behind_nat option.
331
+ 				</para>
332
+				<para>
333
+					<emphasis>
334
+						Default value is <quote>0</quote>.
335
+					</emphasis>
336
+				</para>
337
+				<example>
338
+					<title>Set <varname>behind_nat</varname> parameter</title>
339
+					<programlisting format="linespecific">
340
+						...
341
+						modparam("lrkproxy", "behind_nat", "1")
342
+						...
343
+					</programlisting>
344
+				</example>
345
+			</section>
346
+			<section id="lrkproxy.p.start_port">
347
+				<title><varname>start_port</varname> (integer)</title>
348
+				<para>
349
+					This option use for local rang port for working with LRKProxy engine.
350
+					Default value is based on LRKProxy engine option.
351
+					For example if the range of ports in LRKProxy is set from 20000 to 30000, You could use this option to set a subset of the LRKProxy range.
352
+				</para>
353
+				<example>
354
+					<title>Set <varname>start_port</varname> parameter</title>
355
+					<programlisting format="linespecific">
356
+						...
357
+						modparam("lrkproxy", "start_port", "25000")
358
+						...
359
+					</programlisting>
360
+				</example>
361
+			</section>
362
+			<section id="lrkproxy.p.end_port">
363
+				<title><varname>end_port</varname> (integer)</title>
364
+				<para>
365
+					This option use for local rang port for working with LRKProxy engine.
366
+					Default value is based on LRKProxy engine option.
367
+					For example if the range of ports in LRKProxy is set from 20000 to 30000, You could use this option to set a subset of the LRKProxy range.
368
+				</para>
369
+				<example>
370
+					<title>Set <varname>start_port</varname> parameter</title>
371
+					<programlisting format="linespecific">
372
+						...
373
+						modparam("lrkproxy", "end_port", "28000")
374
+						...
375
+					</programlisting>
376
+				</example>
377
+			</section>
328 378
 
329 379
 
330 380
 		</section>
... ...
@@ -120,7 +120,9 @@ static str DEFAULT_LRKP_SET_ID_STR = str_init("0");
120 120
 static char *gencookie();
121 121
 static int lrkp_test(struct lrkp_node*);
122 122
 static int lrkp_get_config(struct lrkp_node *node);
123
+static int lrkp_keep_alive(struct lrkp_node *node);
123 124
 static int lrkp_set_conntrack_rule(struct lrkproxy_hash_entry *e);
125
+static int lrkp_remove_conntrack_rule(struct lrkproxy_hash_entry *e);
124 126
 
125 127
 
126 128
 static int lrkproxy_force(struct sip_msg *msg, const char *flags, enum lrk_operation op, int more);
... ...
@@ -164,7 +166,7 @@ static unsigned int current_msg_id = (unsigned int)-1;
164 166
 struct lrkp_set_head * lrkp_set_list =0;
165 167
 struct lrkp_set * selected_lrkp_set =0;
166 168
 struct lrkp_set * default_lrkp_set=0;
167
-struct lrkp_node *selected_lrkp_node = 0;
169
+static struct lrkp_node *selected_lrkp_node = 0;
168 170
 int lrkp_algorithm = LRK_LINER;
169 171
 static int hash_table_size = 0;
170 172
 static int hash_table_tout = 3600;
... ...
@@ -172,7 +174,19 @@ static int hash_table_tout = 3600;
172 174
 /*!< The gt is game-theory variable, It could be set 0:disable and 1:enable
173 175
  * default is 0.
174 176
  */
175
-int gt = 0;
177
+static int gt = 0;
178
+
179
+/*!< The behind_nat is used when the LRKProxy is behind nat and the external IP should be as Advertised IP.It could be set 0:disable and 1:enable
180
+ * default is 0.
181
+ */
182
+static int behind_nat = 0;
183
+
184
+/*!< These option used to local rang ports to work with lrkproxy engine.
185
+ * Default values are based on lrkproxy engine.
186
+ */
187
+
188
+static int startport = 0;
189
+static int endport = 0;
176 190
 
177 191
 /*
178 192
  * the custom_sdp_ip_spec variable is used for specific SDP information based $si (source address)
... ...
@@ -195,8 +209,8 @@ static int *lrkp_socks = 0;
195 209
 
196 210
 
197 211
 typedef struct lrkp_set_link {
198
-	struct lrkp_set *rset;
199
-	pv_spec_t *rpv;
212
+    struct lrkp_set *rset;
213
+    pv_spec_t *rpv;
200 214
 } lrkp_set_link_t;
201 215
 
202 216
 /* tm */
... ...
@@ -208,17 +222,17 @@ static struct tm_binds tmb;
208 222
 static cmd_export_t cmds[] = {
209 223
 
210 224
         {"set_lrkproxy_set",  (cmd_function)set_lrkproxy_set_f,    1,
211
-                fixup_set_id, 0,
225
+                  fixup_set_id, 0,
212 226
                 ANY_ROUTE},
213 227
         {"lrkproxy_manage",	(cmd_function)lrkproxy_manage0,     0,
214
-                                                       0, 0,
215
-                   ANY_ROUTE},
228
+                  0, 0,
229
+                ANY_ROUTE},
216 230
         {"lrkproxy_manage",	(cmd_function)lrkproxy_manage1,     1,
217
-                                                       fixup_spve_null, fixup_free_spve_null,
218
-                   ANY_ROUTE},
231
+                  fixup_spve_null, fixup_free_spve_null,
232
+                ANY_ROUTE},
219 233
         {"lrkproxy_manage",	(cmd_function)lrkproxy_manage2,     2,
220
-                                                       fixup_spve_spve, fixup_free_spve_spve,
221
-                   ANY_ROUTE},
234
+                  fixup_spve_spve, fixup_free_spve_spve,
235
+                ANY_ROUTE},
222 236
 
223 237
         {0, 0, 0, 0, 0, 0}
224 238
 };
... ...
@@ -234,6 +248,9 @@ static param_export_t params[] = {
234 248
         {"hash_table_size",       INT_PARAM, &hash_table_size        },
235 249
         {"custom_sdp_ip_avp",     PARAM_STR, &custom_sdp_ip_spec},
236 250
         {"gt",   INT_PARAM  , &gt},
251
+        {"behind_nat",   INT_PARAM  , &behind_nat},
252
+        {"start_port",   INT_PARAM  , &startport},
253
+        {"end_port",   INT_PARAM  , &endport},
237 254
 
238 255
         {0, 0, 0}
239 256
 };
... ...
@@ -507,51 +524,51 @@ static int lrkproxy_add_lrkproxy_set( char * lrk_proxies)
507 524
 
508 525
 static int fixup_set_id(void ** param, int param_no)
509 526
 {
510
-	int int_val, err;
511
-	struct lrkp_set* lrkp_list;
512
-	lrkp_set_link_t *lrkl = NULL;
513
-	str s;
514
-
515
-	lrkl = (lrkp_set_link_t*)pkg_malloc(sizeof(lrkp_set_link_t));
516
-	if(lrkl==NULL) {
517
-		LM_ERR("no more pkg memory\n");
518
-		return -1;
519
-	}
520
-	memset(lrkl, 0, sizeof(lrkp_set_link_t));
521
-	s.s = (char*)*param;
522
-	s.len = strlen(s.s);
523
-
524
-	if(s.s[0] == PV_MARKER) {
525
-		int_val = pv_locate_name(&s);
526
-		if(int_val<0 || int_val!=s.len) {
527
-			LM_ERR("invalid parameter %s\n", s.s);
528
-			pkg_free(lrkl);
529
-			return -1;
530
-		}
531
-		lrkl->rpv = pv_cache_get(&s);
532
-		if(lrkl->rpv == NULL) {
533
-			LM_ERR("invalid pv parameter %s\n", s.s);
534
-			pkg_free(lrkl);
535
-			return -1;
536
-		}
537
-	} else {
538
-		int_val = str2s(*param, strlen(*param), &err);
539
-		if (err == 0) {
540
-			pkg_free(*param);
541
-			if((lrkp_list = select_lrkp_set(int_val)) ==0){
542
-				LM_ERR("lrkp_proxy set %i not configured\n", int_val);
543
-				pkg_free(lrkl);
544
-				return E_CFG;
545
-			}
546
-			lrkl->rset = lrkp_list;
547
-		} else {
548
-			LM_ERR("bad number <%s>\n",	(char *)(*param));
549
-			pkg_free(lrkl);
550
-			return E_CFG;
551
-		}
552
-	}
553
-	*param = (void*)lrkl;
554
-	return 0;
527
+    int int_val, err;
528
+    struct lrkp_set* lrkp_list;
529
+    lrkp_set_link_t *lrkl = NULL;
530
+    str s;
531
+
532
+    lrkl = (lrkp_set_link_t*)pkg_malloc(sizeof(lrkp_set_link_t));
533
+    if(lrkl==NULL) {
534
+                LM_ERR("no more pkg memory\n");
535
+        return -1;
536
+    }
537
+    memset(lrkl, 0, sizeof(lrkp_set_link_t));
538
+    s.s = (char*)*param;
539
+    s.len = strlen(s.s);
540
+
541
+    if(s.s[0] == PV_MARKER) {
542
+        int_val = pv_locate_name(&s);
543
+        if(int_val<0 || int_val!=s.len) {
544
+                    LM_ERR("invalid parameter %s\n", s.s);
545
+            pkg_free(lrkl);
546
+            return -1;
547
+        }
548
+        lrkl->rpv = pv_cache_get(&s);
549
+        if(lrkl->rpv == NULL) {
550
+                    LM_ERR("invalid pv parameter %s\n", s.s);
551
+            pkg_free(lrkl);
552
+            return -1;
553
+        }
554
+    } else {
555
+        int_val = str2s(*param, strlen(*param), &err);
556
+        if (err == 0) {
557
+            pkg_free(*param);
558
+            if((lrkp_list = select_lrkp_set(int_val)) ==0){
559
+                        LM_ERR("lrkp_proxy set %i not configured\n", int_val);
560
+                pkg_free(lrkl);
561
+                return E_CFG;
562
+            }
563
+            lrkl->rset = lrkp_list;
564
+        } else {
565
+                    LM_ERR("bad number <%s>\n",	(char *)(*param));
566
+            pkg_free(lrkl);
567
+            return E_CFG;
568
+        }
569
+    }
570
+    *param = (void*)lrkl;
571
+    return 0;
555 572
 }
556 573
 
557 574
 
... ...
@@ -723,7 +740,7 @@ child_init(int rank)
723 740
                 return -1;
724 741
             }
725 742
             freeaddrinfo(res);
726
-rptest:
743
+            rptest:
727 744
             pnode->ln_enable = lrkp_test(pnode);
728 745
             if (pnode->ln_enable) {       //get lrk proxy config if it is enable.
729 746
 //                LM_INFO("lrkp_test test is calling here\n"); //enable next line.
... ...
@@ -794,14 +811,14 @@ static int lrkp_test(struct lrkp_node *node)
794 811
 
795 812
 //    if (buf == NULL) {
796 813
     if (!buf[0]) {
797
-        LM_ERR("can't ping the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
814
+                LM_ERR("can't ping the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
798 815
         return 0;
799 816
     }
800 817
 
801 818
     char *resp = buf + v[0].iov_len + v[1].iov_len + 1;
802 819
     if (memcmp(resp, "PONG", 4) == 0)
803 820
 //                LM_DBG("Recieve PONG response from lrk proxy server %s, Enable it right now.\n", node->ln_url.s);
804
-            LM_INFO("Recieve PONG response from lrk proxy server %s, Enable it right now.\n", node->ln_url.s);
821
+                LM_INFO("Recieve PONG response from lrk proxy server %s, Enable it right now.\n", node->ln_url.s);
805 822
 
806 823
     return 1;
807 824
 
... ...
@@ -819,33 +836,84 @@ static int lrkp_get_config(struct lrkp_node *node){
819 836
 
820 837
 //    if (buf == NULL) {
821 838
     if (!buf[0]) {
822
-        LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
839
+                LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
823 840
         return 0;
824 841
     }
825 842
 
826 843
     lnconf = (struct lrkp_node_conf *)(buf + v[0].iov_len + v[1].iov_len + 1);
827 844
 
828 845
     if (lnconf == NULL){
829
-        LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
846
+                LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
830 847
         return 0;
831 848
     }
832 849
 
833
-
850
+    if(startport != 0 && endport != 0){
851
+        lnconf->start_port = lnconf->current_port = startport;
852
+        lnconf->end_port = endport;
853
+    }
834 854
     memcpy(node->lrkp_n_c, lnconf, sizeof(struct lrkp_node_conf));
835 855
 
836 856
 //    node->lrkp_n_c = lnconf;
837
-    LM_INFO("the lrk proxy %s is configured successfully right now.\n", node->ln_url.s);
838
-    LM_INFO("buffer internal:%s\n", node->lrkp_n_c->internal_ip);
839
-    LM_INFO("buffer external:%s\n", node->lrkp_n_c->external_ip);
840
-    LM_INFO("buffer start_port:%d\n", node->lrkp_n_c->start_port);
841
-    LM_INFO("buffer end_port:%d\n", node->lrkp_n_c->end_port);
842
-    LM_INFO("buffer current_port:%d\n", node->lrkp_n_c->current_port);
857
+            LM_INFO("the lrk proxy %s is configured successfully right now.\n", node->ln_url.s);
858
+            LM_INFO("buffer internal:%s\n", node->lrkp_n_c->internal_ip);
859
+            LM_INFO("buffer external:%s\n", node->lrkp_n_c->external_ip);
860
+            LM_INFO("buffer start_port:%d\n", node->lrkp_n_c->start_port);
861
+            LM_INFO("buffer end_port:%d\n", node->lrkp_n_c->end_port);
862
+            LM_INFO("buffer current_port:%d\n", node->lrkp_n_c->current_port);
843 863
 
844 864
     return 1;
845 865
 
846 866
 
847 867
 }
848 868
 
869
+static int lrkp_keep_alive(struct lrkp_node *node){
870
+
871
+    int buflen = 256;
872
+    char buf[buflen];
873
+    struct iovec v[2] = {{NULL, 0}, {"G", 1}};
874
+    struct lrkp_node_conf *lnconf = NULL;
875
+
876
+    memset(buf, 0, buflen);
877
+    memcpy(buf, send_lrkp_command(node, v, 2, 0), buflen);
878
+
879
+//    if (buf == NULL) {
880
+    if (!buf[0]) {
881
+                LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
882
+        return 0;
883
+    }
884
+
885
+    lnconf = (struct lrkp_node_conf *)(buf + v[0].iov_len + v[1].iov_len + 1);
886
+
887
+    if (lnconf == NULL){
888
+                LM_ERR("can't get config of the lrk proxy %s, Disable it right now.\n", node->ln_url.s);
889
+        return 0;
890
+    }
891
+
892
+//    if(startport != 0 && endport != 0){
893
+//        lnconf->start_port = lnconf->current_port = startport;
894
+//        lnconf->end_port = endport;
895
+//    }
896
+
897
+    /*
898
+    if(startport != 0 && endport != 0){
899
+        //lnconf->start_port = lnconf->current_port = startport;
900
+        lnconf->start_port = lnconf->current_port = node->lrkp_n_c->current_port;
901
+        lnconf->end_port = endport;
902
+    }
903
+
904
+    memcpy(node->lrkp_n_c, lnconf, sizeof(struct lrkp_node_conf));
905
+*/
906
+//    node->lrkp_n_c = lnconf;
907
+            LM_INFO("the lrk proxy %s is configured successfully right now.\n", node->ln_url.s);
908
+            LM_INFO("buffer internal:%s\n", node->lrkp_n_c->internal_ip);
909
+            LM_INFO("buffer external:%s\n", node->lrkp_n_c->external_ip);
910
+            LM_INFO("buffer start_port:%d\n", node->lrkp_n_c->start_port);
911
+            LM_INFO("buffer end_port:%d\n", node->lrkp_n_c->end_port);
912
+            LM_INFO("buffer current_port:%d\n", node->lrkp_n_c->current_port);
913
+
914
+    return 1;
915
+}
916
+
849 917
 static int lrkp_set_conntrack_rule(struct lrkproxy_hash_entry *e) {
850 918
     int buflen = 254;
851 919
     char buf[buflen];
... ...
@@ -963,6 +1031,122 @@ static int lrkp_set_conntrack_rule(struct lrkproxy_hash_entry *e) {
963 1031
 
964 1032
 }
965 1033
 
1034
+static int lrkp_remove_conntrack_rule(struct lrkproxy_hash_entry *e) {
1035
+    int buflen = 254;
1036
+    char buf[buflen];
1037
+    int v_len = 0;
1038
+
1039
+    char src_ipv4[20];
1040
+    char src_port[20];
1041
+    char dst_ipv4[20];
1042
+    char dst_port[20];
1043
+    char snat_ipv4[20];
1044
+    char snat_port[20];
1045
+    char dnat_ipv4[20];
1046
+    char dnat_port[20];
1047
+    char timeout[20];
1048
+    char callid[50];
1049
+
1050
+    struct iovec v[] = {
1051
+            {NULL, 0},  /* reserved (cookie) */
1052
+            {"D",  1},   /* command & common options */
1053
+            {NULL, 0},  /* src_ipv4 */
1054
+            {NULL, 0},  /* dst_ipnv4 */
1055
+            {NULL, 0},  /* snat_ipv4 */
1056
+            {NULL, 0},  /* dnat_ipv4 */
1057
+            {NULL, 0},  /* src_port */
1058
+            {NULL, 0},  /* dst_port*/
1059
+            {NULL, 0},  /* snat_port */
1060
+            {NULL, 0},  /* dnat_port*/
1061
+            {NULL, 0},  /* timeout to clear conntrack entry*/
1062
+            {NULL, 0},  /* callid of session */
1063
+    };
1064
+
1065
+    v_len += v[1].iov_len;
1066
+
1067
+    //set src_ipv4 to buffer.
1068
+    sprintf(src_ipv4, " %.*s ", e->src_ipv4.len, e->src_ipv4.s);
1069
+    v[2].iov_base = src_ipv4;
1070
+    v[2].iov_len = strlen(v[2].iov_base);
1071
+    v_len += v[2].iov_len;
1072
+
1073
+    //set dst_ipv4 to buffer.
1074
+    sprintf(dst_ipv4, "%.*s ", e->dst_ipv4.len, e->dst_ipv4.s);
1075
+    v[3].iov_base = dst_ipv4;
1076
+    v[3].iov_len = strlen(v[3].iov_base);
1077
+    v_len += v[3].iov_len;
1078
+
1079
+    //set snat_ipv4 to buffer.
1080
+    sprintf(snat_ipv4, "%.*s ", e->snat_ipv4.len, e->snat_ipv4.s);
1081
+    v[4].iov_base = snat_ipv4;
1082
+    v[4].iov_len = strlen(v[4].iov_base);
1083
+    v_len += v[4].iov_len;
1084
+
1085
+    //set dnat_ipv4 to buffer.
1086
+    sprintf(dnat_ipv4, "%.*s ", e->dnat_ipv4.len, e->dnat_ipv4.s);
1087
+    v[5].iov_base = dnat_ipv4;
1088
+    v[5].iov_len = strlen(v[5].iov_base);
1089
+    v_len += v[5].iov_len;
1090
+
1091
+    //set src_port to buffer.
1092
+    sprintf(src_port, "%.*s ", e->src_port.len, e->src_port.s);
1093
+    v[6].iov_base = src_port;
1094
+    v[6].iov_len = strlen(v[6].iov_base);
1095
+    v_len += v[6].iov_len;
1096
+
1097
+    //set dst_port to buffer.
1098
+    sprintf(dst_port, "%.*s ", e->dst_port.len, e->dst_port.s);
1099
+    v[7].iov_base = dst_port;
1100
+    v[7].iov_len = strlen(v[7].iov_base);
1101
+    v_len += v[7].iov_len;
1102
+
1103
+    //set snat_port to buffer.
1104
+    sprintf(snat_port, "%.*s ", e->snat_port.len, e->snat_port.s);
1105
+    v[8].iov_base = snat_port;
1106
+    v[8].iov_len = strlen(v[8].iov_base);
1107
+    v_len += v[8].iov_len;
1108
+
1109
+    //set dnat_port to buffer.
1110
+    sprintf(dnat_port, "%.*s ", e->dnat_port.len, e->dnat_port.s);
1111
+    v[9].iov_base = dnat_port;
1112
+    v[9].iov_len = strlen(v[9].iov_base);
1113
+    v_len += v[9].iov_len;
1114
+
1115
+    //set timeout to buffer. Set to 60 sec for default.
1116
+    sprintf(timeout, "%d ", 60);
1117
+    v[10].iov_base = timeout;
1118
+    v[10].iov_len = strlen(v[10].iov_base);
1119
+    v_len += v[10].iov_len;
1120
+
1121
+    //set callid to buffer.
1122
+    sprintf(callid, "%.*s ", e->callid.len, e->callid.s);
1123
+    v[11].iov_base = callid;
1124
+    v[11].iov_len = strlen(v[11].iov_base);
1125
+    v_len += v[11].iov_len;
1126
+//    LM_ERR("e->callid.len is:%d right now.\n\n", e->callid.len);
1127
+
1128
+    memset(buf, 0, buflen);
1129
+    memcpy(buf, send_lrkp_command(e->node, v, 12, v_len), buflen);
1130
+//
1131
+
1132
+//    if (buf == NULL) {
1133
+    if (!buf[0]) {
1134
+                LM_ERR("can't ping the lrk proxy %s, Disable it right now.\n", e->node->ln_url.s);
1135
+        return 0;
1136
+    }
1137
+
1138
+    v_len += v[0].iov_len;
1139
+
1140
+
1141
+//    char *resp = buf + v[0].iov_len + v[1].iov_len + v[2].iov_len;
1142
+    char *resp = buf + v_len;
1143
+    if (memcmp(resp, "OK", 2) == 0) {
1144
+                LM_INFO("Recieve OK response from lrk proxy server, Rule remove successfully.\n");
1145
+                LM_DBG("Recieve OK response from lrk proxy server, Rule remove successfully.\n");
1146
+    }
1147
+    return 1;
1148
+
1149
+}
966 1150
 
967 1151
 char *send_lrkp_command(struct lrkp_node *node, struct iovec *v, int vcnt, int more)
968 1152
 {
... ...
@@ -1057,6 +1241,10 @@ char *send_lrkp_command(struct lrkp_node *node, struct iovec *v, int vcnt, int m
1057 1241
                         case 'S':
1058 1242
                             if (len == more + v[0].iov_len + 2)
1059 1243
                                 goto out;
1244
+//                            break;
1245
+                        case 'D':
1246
+                            if (len == more + v[0].iov_len + 2)
1247
+                                goto out;
1060 1248
 //                            break;
1061 1249
                     }
1062 1250
 
... ...
@@ -1107,7 +1295,7 @@ struct lrkp_node *select_lrkp_node(int do_test)
1107 1295
     int was_forced2;
1108 1296
 
1109 1297
     if(!selected_lrkp_set){
1110
-        LM_ERR("script error -no valid set selected\n");
1298
+                LM_ERR("script error -no valid set selected\n");
1111 1299
         return NULL;
1112 1300
     }
1113 1301
     /* Most popular case: 1 proxy, nothing to calculate */
... ...
@@ -1117,7 +1305,8 @@ struct lrkp_node *select_lrkp_node(int do_test)
1117 1305
         if (!node->ln_enable) {
1118 1306
             node->ln_enable = lrkp_test(node);
1119 1307
             if (node->ln_enable) {       //get lrk proxy config if it is enable.
1120
-                lrkp_get_config(node);
1308
+//                lrkp_get_config(node);
1309
+                lrkp_keep_alive(node);
1121 1310
                 return node;
1122 1311
             }
1123 1312
         }
... ...
@@ -1127,16 +1316,19 @@ struct lrkp_node *select_lrkp_node(int do_test)
1127 1316
 
1128 1317
 
1129 1318
     /* Check node is enable and test it again*/
1130
-retry:
1319
+    retry:
1320
+#if 0
1131 1321
     for (node=selected_lrkp_set->ln_first; node!=NULL; node=node->ln_next) {
1132 1322
 
1133 1323
         if (!node->ln_enable) {
1134 1324
             /* Try to enable if it's time to try. */
1135 1325
             node->ln_enable = lrkp_test(node);
1136 1326
             if (node->ln_enable)       //get lrk proxy config if it is enable.
1137
-                lrkp_get_config(node);
1327
+//                lrkp_get_config(node);
1328
+                lrkp_keep_alive(node);
1138 1329
         }
1139 1330
     }
1331
+#endif
1140 1332
 
1141 1333
     if (lrkp_algorithm == LRK_LINER) {
1142 1334
         was_forced = 0;
... ...
@@ -1153,7 +1345,8 @@ retry:
1153 1345
             /* Try to enable if it's time to try. */
1154 1346
             node->ln_enable = lrkp_test(node);
1155 1347
             if (node->ln_enable)       //get lrk proxy config if it is enable.
1156
-                lrkp_get_config(node);
1348
+//                lrkp_get_config(node);
1349
+                lrkp_keep_alive(node);
1157 1350
         }
1158 1351
 
1159 1352
 
... ...
@@ -1161,12 +1354,12 @@ retry:
1161 1354
     }
1162 1355
     else if(lrkp_algorithm == LRK_RR) {
1163 1356
         was_forced2 = 0;
1164
-retry3:
1357
+        retry3:
1165 1358
         if (!selected_lrkp_node) {
1166 1359
             selected_lrkp_node = selected_lrkp_set->ln_first;
1167 1360
             node = selected_lrkp_set->ln_first;
1168 1361
             if(node->ln_enable)
1169
-                    goto found;
1362
+                goto found;
1170 1363
 //            was_forced2 = 1;
1171 1364
         }
1172 1365
         for (node = selected_lrkp_node->ln_next; node != NULL; node = node->ln_next)
... ...
@@ -1185,6 +1378,15 @@ retry3:
1185 1378
         was_forced2 = 1;
1186 1379
         selected_lrkp_node = NULL;
1187 1380
 
1381
+        //trying to enable all lrkproxy and check again.
1382
+        for (node = selected_lrkp_set->ln_first; node != NULL; node = node->ln_next) {
1383
+            /* Try to enable if it's time to try. */
1384
+            node->ln_enable = lrkp_test(node);
1385
+            if (node->ln_enable)       //get lrk proxy config if it is enable.
1386
+//                lrkp_get_config(node);
1387
+                lrkp_keep_alive(node);
1388
+        }
1389
+
1188 1390
         goto retry3;
1189 1391
     }
1190 1392
 
... ...
@@ -1254,15 +1456,21 @@ static int change_media_sdp(sip_msg_t *msg, struct lrkproxy_hash_entry *e, const
1254 1456
         } else if (strstr(flags, "ie")) {
1255 1457
             ip_selected = e->node->lrkp_n_c->external_ip; //lrk_node->external_ip;
1256 1458
         } else {
1257
-            LM_INFO("unknown flags, use internal_ip\n");
1459
+                    LM_INFO("unknown flags, use internal_ip\n");
1258 1460
             ip_selected = e->node->lrkp_n_c->internal_ip;
1259 1461
         }
1260 1462
     }
1261 1463
     else {
1262
-        LM_INFO("no flags set, use internal_ip\n");
1464
+                LM_INFO("no flags set, use internal_ip\n");
1465
+        ip_selected = e->node->lrkp_n_c->internal_ip;
1466
+    }
1467
+
1468
+    if(behind_nat){
1469
+        LM_INFO("lrkproxy is behind nat, use internal_ip\n");
1263 1470
         ip_selected = e->node->lrkp_n_c->internal_ip;
1264 1471
     }
1265 1472
 
1473
+
1266 1474
     if (op == OP_OFFER) {
1267 1475
         e->dst_ipv4.s = ip_selected;
1268 1476
         e->dst_ipv4.len = strlen(e->dst_ipv4.s);
... ...
@@ -1313,7 +1521,10 @@ static int change_media_sdp(sip_msg_t *msg, struct lrkproxy_hash_entry *e, const
1313 1521
         len = (int) (sdp_param_end - sdp_param_start);
1314 1522
         if ((int) (start_sdp_o - off) == 0) {
1315 1523
             memset(sdp_new_o, 0, 128);
1316
-            snprintf(sdp_new_o, 128, "o=lrkproxy %s %s IN IP4 %s\r", SUP_CPROTOVER, REQ_CPROTOVER, ip_selected);
1524
+            time_t seconds;
1525
+            time(&seconds);
1526
+//            snprintf(sdp_new_o, 128, "o=lrkproxy %s %s IN IP4 %s\r", SUP_CPROTOVER, REQ_CPROTOVER, ip_selected);
1527
+            snprintf(sdp_new_o, 128, "o=lrkproxy %ld %ld IN IP4 %s\r", seconds, seconds, ip_selected);
1317 1528
             strncat(newbody.s, sdp_new_o, strlen(sdp_new_o));
1318 1529
             off += len + 1;
1319 1530
             continue;
... ...
@@ -1377,37 +1588,37 @@ static int change_media_sdp(sip_msg_t *msg, struct lrkproxy_hash_entry *e, const
1377 1588
 
1378 1589
 /* This function assumes p points to a line of requested type. */
1379 1590
 
1380
-	static int
1591
+static int
1381 1592
 set_lrkproxy_set_f(struct sip_msg * msg, char * str1, char * str2)
1382 1593
 {
1383
-	lrkp_set_link_t *lrkl;
1384
-	pv_value_t val;
1385
-
1386
-	lrkl = (lrkp_set_link_t*)str1;
1387
-
1388
-	current_msg_id = 0;
1389
-	selected_lrkp_set = 0;
1390
-
1391
-	if(lrkl->rset != NULL) {
1392
-		current_msg_id = msg->id;
1393
-		selected_lrkp_set = lrkl->rset;
1394
-	} else {
1395
-		if(pv_get_spec_value(msg, lrkl->rpv, &val)<0) {
1396
-			LM_ERR("cannot evaluate pv param\n");
1397
-			return -1;
1398
-		}
1399
-		if(!(val.flags & PV_VAL_INT)) {
1400
-			LM_ERR("pv param must hold an integer value\n");
1401
-			return -1;
1402
-		}
1403
-		selected_lrkp_set = select_lrkp_set(val.ri);
1404
-		if(selected_lrkp_set==NULL) {
1405
-			LM_ERR("could not locate lrkproxy set %d\n", val.ri);
1406
-			return -1;
1407
-		}
1408
-		current_msg_id = msg->id;
1409
-	}
1410
-	return 1;
1594
+    lrkp_set_link_t *lrkl;
1595
+    pv_value_t val;
1596
+
1597
+    lrkl = (lrkp_set_link_t*)str1;
1598
+
1599
+    current_msg_id = 0;
1600
+    selected_lrkp_set = 0;
1601
+
1602
+    if(lrkl->rset != NULL) {
1603
+        current_msg_id = msg->id;
1604
+        selected_lrkp_set = lrkl->rset;
1605
+    } else {
1606
+        if(pv_get_spec_value(msg, lrkl->rpv, &val)<0) {
1607
+                    LM_ERR("cannot evaluate pv param\n");
1608
+            return -1;
1609
+        }
1610
+        if(!(val.flags & PV_VAL_INT)) {
1611
+                    LM_ERR("pv param must hold an integer value\n");
1612
+            return -1;
1613
+        }
1614
+        selected_lrkp_set = select_lrkp_set(val.ri);
1615
+        if(selected_lrkp_set==NULL) {
1616
+                    LM_ERR("could not locate lrkproxy set %d\n", val.ri);
1617
+            return -1;
1618
+        }
1619
+        current_msg_id = msg->id;
1620
+    }
1621
+    return 1;
1411 1622
 }
1412 1623
 
1413 1624
 static int
... ...
@@ -1430,9 +1641,12 @@ lrkproxy_manage(struct sip_msg *msg, char *flags, char *ip)
1430 1641
           || method==METHOD_BYE || method==METHOD_UPDATE))
1431 1642
         return -1;
1432 1643
 
1433
-    if (method==METHOD_CANCEL || method==METHOD_BYE)
1644
+
1645
+    if (method==METHOD_CANCEL || method==METHOD_BYE) {
1434 1646
         return lrkproxy_unforce(msg, flags, OP_DELETE, 1);
1435 1647
 
1648
+    }
1649
+
1436 1650
     if (msg->msg_flags & FL_SDP_BODY)
1437 1651
         nosdp = 0;
1438 1652
     else
... ...
@@ -1661,7 +1875,7 @@ static int lrkproxy_force(struct sip_msg *msg, const char *flags, enum lrk_opera
1661 1875
         entry = lrkproxy_hash_table_lookup(call_id, viabranch);
1662 1876
         if (!entry){
1663 1877
                     LM_ERR("No found entry in hash table\n");
1664
-                    //todo...
1878
+            //todo...
1665 1879
             return 0;
1666 1880
         }
1667 1881
 
... ...
@@ -1706,18 +1920,18 @@ static int lrkproxy_force(struct sip_msg *msg, const char *flags, enum lrk_opera
1706 1920
             change_media_sdp(msg, entry, NULL, op);
1707 1921
 
1708 1922
 
1709
-        LM_INFO("selected node: %s\n",entry->node->ln_url.s);
1710
-        LM_INFO("call_is: %.*s\n",entry->callid.len, entry->callid.s);
1711
-        LM_INFO("viabranch: %.*s\n",entry->viabranch.len, entry->viabranch.s);
1712
-        LM_INFO("src_ipv4: %.*s\n",entry->src_ipv4.len, entry->src_ipv4.s);
1713
-        LM_INFO("src_port: %.*s\n",entry->src_port.len, entry->src_port.s);
1714
-        LM_INFO("dst_ipv4: %.*s\n",entry->dst_ipv4.len, entry->dst_ipv4.s);
1715
-        LM_INFO("dst_port: %.*s\n",entry->dst_port.len, entry->dst_port.s);
1923
+                LM_INFO("selected node: %s\n",entry->node->ln_url.s);
1924
+                LM_INFO("call_is: %.*s\n",entry->callid.len, entry->callid.s);
1925
+                LM_INFO("viabranch: %.*s\n",entry->viabranch.len, entry->viabranch.s);
1926
+                LM_INFO("src_ipv4: %.*s\n",entry->src_ipv4.len, entry->src_ipv4.s);
1927
+                LM_INFO("src_port: %.*s\n",entry->src_port.len, entry->src_port.s);
1928
+                LM_INFO("dst_ipv4: %.*s\n",entry->dst_ipv4.len, entry->dst_ipv4.s);
1929
+                LM_INFO("dst_port: %.*s\n",entry->dst_port.len, entry->dst_port.s);
1716 1930
 
1717
-        LM_INFO("dnat_ipv4: %.*s\n",entry->dnat_ipv4.len, entry->dnat_ipv4.s);
1718
-        LM_INFO("dnat_port: %.*s\n",entry->dnat_port.len, entry->dnat_port.s);
1719
-        LM_INFO("snat_ipv4: %.*s\n",entry->snat_ipv4.len, entry->snat_ipv4.s);
1720
-        LM_INFO("snat_port: %.*s\n",entry->snat_port.len, entry->snat_port.s);
1931
+                LM_INFO("dnat_ipv4: %.*s\n",entry->dnat_ipv4.len, entry->dnat_ipv4.s);
1932
+                LM_INFO("dnat_port: %.*s\n",entry->dnat_port.len, entry->dnat_port.s);
1933
+                LM_INFO("snat_ipv4: %.*s\n",entry->snat_ipv4.len, entry->snat_ipv4.s);
1934
+                LM_INFO("snat_port: %.*s\n",entry->snat_port.len, entry->snat_port.s);
1721 1935
 
1722 1936
 
1723 1937
         lrkp_set_conntrack_rule(entry);
... ...
@@ -1727,8 +1941,9 @@ static int lrkproxy_force(struct sip_msg *msg, const char *flags, enum lrk_opera
1727 1941
 }
1728 1942
 
1729 1943
 static int lrkproxy_unforce(struct sip_msg *msg, const char *flags, enum lrk_operation op, int more){
1730
-//            LM_INFO ("Here is lrkproxy_unforce\n");
1944
+    //            LM_INFO ("Here is lrkproxy_unforce\n");
1731 1945
 //    struct lrkproxy_hash_entry *entry = NULL;
1946
+    struct lrkproxy_hash_entry *entry = NULL;
1732 1947
     str viabranch = STR_NULL;
1733 1948
     str call_id;
1734 1949
     int via_id;
... ...
@@ -1737,7 +1952,6 @@ static int lrkproxy_unforce(struct sip_msg *msg, const char *flags, enum lrk_ope
1737 1952
                 LM_ERR("can't get Call-Id field\n");
1738 1953
         return -1;
1739 1954
     }
1740
-
1741 1955
     /*We have to choice VIA id,
1742 1956
      * for SIP_REQUEST we use VIA1 and for SIP_REPLY we use VIA2 */
1743 1957
     via_id = more;
... ...
@@ -1747,20 +1961,45 @@ static int lrkproxy_unforce(struct sip_msg *msg, const char *flags, enum lrk_ope
1747 1961
         return -1;
1748 1962
     }
1749 1963
 
1964
+    //check if entry exist try to send hangup command to Kernel.
1965
+    entry = lrkproxy_hash_table_lookup(call_id, viabranch);
1966
+    if (!entry) {
1967
+                LM_INFO("lrkproxy_unforce=========>not found in\n");
1968
+        return -1;
1969
+    }else
1970
+                LM_INFO("lrkproxy_unforce=========>found in\n");
1971
+
1972
+
1973
+    //=========================================================================MESPIO NEW
1974
+            LM_INFO("selected node: %s\n",entry->node->ln_url.s);
1975
+            LM_INFO("call_is: %.*s\n",entry->callid.len, entry->callid.s);
1976
+            LM_INFO("viabranch: %.*s\n",entry->viabranch.len, entry->viabranch.s);
1977
+            LM_INFO("src_ipv4: %.*s\n",entry->src_ipv4.len, entry->src_ipv4.s);
1978
+            LM_INFO("src_port: %.*s\n",entry->src_port.len, entry->src_port.s);
1979
+            LM_INFO("dst_ipv4: %.*s\n",entry->dst_ipv4.len, entry->dst_ipv4.s);
1980
+            LM_INFO("dst_port: %.*s\n",entry->dst_port.len, entry->dst_port.s);
1981
+
1982
+            LM_INFO("dnat_ipv4: %.*s\n",entry->dnat_ipv4.len, entry->dnat_ipv4.s);
1983
+            LM_INFO("dnat_port: %.*s\n",entry->dnat_port.len, entry->dnat_port.s);
1984
+            LM_INFO("snat_ipv4: %.*s\n",entry->snat_ipv4.len, entry->snat_ipv4.s);
1985
+            LM_INFO("snat_port: %.*s\n",entry->snat_port.len, entry->snat_port.s);
1986
+    //=========================================================================MESPIO NEW
1987
+    lrkp_remove_conntrack_rule(entry);
1750 1988
     if (op == OP_DELETE) {
1751 1989
         /* Delete the key<->value from the hashtable */
1752 1990
         if (!lrkproxy_hash_table_remove(call_id, viabranch, op)) {
1753
-                    LM_ERR("lrkproxy hash table failed to remove entry for callen=%d callid=%.*s viabranch=%.*s\n",
1991
+                    LM_INFO("lrkproxy hash table failed to remove entry for callen=%d callid=%.*s viabranch=%.*s\n",
1754 1992
                            call_id.len, call_id.len, call_id.s,
1755 1993
                            viabranch.len, viabranch.s);
1756 1994
         } else {
1757
-                    LM_DBG("lrkproxy hash table remove entry for callen=%d callid=%.*s viabranch=%.*s\n",
1995
+                    LM_INFO("lrkproxy hash table remove entry for callen=%d callid=%.*s viabranch=%.*s\n",
1758 1996
                            call_id.len, call_id.len, call_id.s,
1759 1997
                            viabranch.len, viabranch.s);
1760 1998
         }
1761 1999
     }
1762
-    LM_INFO("lrkproxy hash table remove entry for callen=%d callid=%.*s viabranch=%.*s successfully\n",
1763
-            call_id.len, call_id.len, call_id.s,
1764
-            viabranch.len, viabranch.s);
2000
+            LM_INFO("lrkproxy hash table remove entry for callen=%d callid=%.*s viabranch=%.*s successfully\n",
2001
+                    call_id.len, call_id.len, call_id.s,
2002
+                    viabranch.len, viabranch.s);
2003
+
1765 2004
     return 1;
1766 2005
 }
... ...
@@ -354,6 +354,12 @@ int lrkproxy_hash_table_remove(str callid, str viabranch, enum lrk_operation op)
354 354
     entry = lrkproxy_hash_table->row_entry_list[hash_index];
355 355
     last_entry = entry;
356 356
 
357
+    if (!entry)
358
+                LM_INFO("============>entry is null\n");
359
+    else
360
+                LM_INFO("============>entry is not null\n");
361
+
362
+
357 363
     // lock
358 364
     if (lrkproxy_hash_table->row_locks[hash_index]) {
359 365
         lock_get(lrkproxy_hash_table->row_locks[hash_index]);
... ...
@@ -363,12 +369,19 @@ int lrkproxy_hash_table_remove(str callid, str viabranch, enum lrk_operation op)
363 369
     }
364 370
 
365 371
     while (entry) {
372
+                LM_INFO("remove============>current_callid=%.*s, entry_callid=%.*s\n",
373
+                        callid.len, callid.s,
374
+                        entry->callid.len, entry->callid.s);
375
+
376
+                LM_INFO("remove============>current_viabranch=%.*s, entry_viabranch=%.*s\n",
377
+                        viabranch.len, viabranch.s,
378
+                        entry->viabranch.len, entry->viabranch.s);
366 379
         // if callid found, delete entry
367 380
         if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) ||
368 381
             (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE) ||
369 382
             str_equal(entry->callid, callid)){
370
-            //if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) ||
371
-            //    (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE)) {
383
+//            if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) ||
384
+//                (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE)) {
372 385
             // set pointers; exclude entry
373 386
 
374 387
             // set pointers; exclude entry
... ...
@@ -424,7 +437,8 @@ int lrkproxy_hash_table_remove(str callid, str viabranch, enum lrk_operation op)
424 437
 //struct lrkp_node *lrkproxy_hash_table_lookup(str callid, str viabranch, enum lrk_operation op) {
425 438
 //struct lrkproxy_hash_entry *lrkproxy_hash_table_lookup(str callid, str viabranch, enum lrk_operation op) {
426 439
 struct lrkproxy_hash_entry *lrkproxy_hash_table_lookup(str callid, str viabranch) {
427
-    struct lrkproxy_hash_entry *entry, *last_entry;
440
+    struct lrkproxy_hash_entry *entry=NULL;
441
+    struct lrkproxy_hash_entry *last_entry = NULL;
428 442
     unsigned int hash_index;
429 443
 //    struct lrkp_node *node;
430 444
 
... ...
@@ -439,6 +453,7 @@ struct lrkproxy_hash_entry *lrkproxy_hash_table_lookup(str callid, str viabranch
439 453
     entry = lrkproxy_hash_table->row_entry_list[hash_index];
440 454
     last_entry = entry;
441 455
 
456
+
442 457
     // lock
443 458
     if (lrkproxy_hash_table->row_locks[hash_index]) {
444 459
         lock_get(lrkproxy_hash_table->row_locks[hash_index]);
... ...
@@ -447,14 +462,18 @@ struct lrkproxy_hash_entry *lrkproxy_hash_table_lookup(str callid, str viabranch
447 462
         return 0;
448 463
     }
449 464
 
465
+
450 466
     while (entry) {
467
+
451 468
         // if callid found, return entry
452
-//        if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) ||
453
-//            (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE)) {
454
-        if (str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) {
469
+        if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) ||
470
+            (str_equal(entry->callid, callid) && viabranch.len == 0) ||
471
+            str_equal(entry->callid, callid)){
455 472
 //            node = entry->node;
456 473
             // unlock
474
+
457 475
             lock_release(lrkproxy_hash_table->row_locks[hash_index]);
476
+
458 477
             return entry;
459 478
 //            return node;
460 479
         }