Browse code

sip: moved resolve_targets() to resolver

Raphael Coeffic authored on 19/11/2013 12:31:01
Showing 7 changed files
... ...
@@ -43,6 +43,7 @@
43 43
 #include "AmSessionContainer.h"
44 44
 #include "Am100rel.h"
45 45
 #include "sip/transport.h"
46
+#include "sip/resolver.h"
46 47
 #include "sip/ip_util.h"
47 48
 #include "sip/sip_timers.h"
48 49
 #include "sip/raw_sender.h"
... ...
@@ -91,7 +92,6 @@ bool         AmConfig::ForceSymmetricRtp       = false;
91 92
 bool         AmConfig::SipNATHandling          = false;
92 93
 bool         AmConfig::UseRawSockets           = false;
93 94
 bool         AmConfig::IgnoreNotifyLowerCSeq   = false;
94
-bool         AmConfig::DisableDNSSRV           = false;
95 95
 string       AmConfig::Signature               = "";
96 96
 unsigned int AmConfig::MaxForwards             = MAX_FORWARDS;
97 97
 bool	     AmConfig::SingleCodecInOK	       = false;
... ...
@@ -386,7 +386,7 @@ int AmConfig::readConfiguration()
386 386
   }
387 387
 
388 388
   if(cfg.hasParameter("disable_dns_srv")) {
389
-    DisableDNSSRV = (cfg.getParameter("disable_dns_srv") == "yes");
389
+    _resolver::disable_srv = (cfg.getParameter("disable_dns_srv") == "yes");
390 390
   }
391 391
   
392 392
 
... ...
@@ -196,8 +196,6 @@ struct AmConfig
196 196
   static bool UseRawSockets;
197 197
   /** Ignore Low CSeq on NOTIFY  - for RFC 3265 instead of 5057 */
198 198
   static bool IgnoreNotifyLowerCSeq;
199
-  /** skip DNS SRV lookup for resolving destination address*/
200
-  static bool DisableDNSSRV;
201 199
   /** Server/User-Agent header (optional) */
202 200
   static string Signature;
203 201
   /** Value of Max-Forward header field for new requests */
... ...
@@ -183,6 +183,11 @@ inline int lower_cmp_n(const char* l, int llen, const char* r, int rlen)
183 183
     return lower_cmp(l,r,rlen);
184 184
 }
185 185
 
186
+inline int lower_cmp_n(const cstring& l, const cstring& r)
187
+{
188
+    return lower_cmp_n(l.s,l.len,r.s,r.len);
189
+}
190
+
186 191
 int parse_sip_version(const char* beg, int len);
187 192
 
188 193
 /** 
... ...
@@ -31,7 +31,10 @@
31 31
 #include "hash.h"
32 32
 
33 33
 #include "parse_dns.h"
34
+#include "parse_common.h"
34 35
 #include "ip_util.h"
36
+#include "trans_layer.h"
37
+#include "tr_blacklist.h"
35 38
 #include "wheeltimer.h"
36 39
 
37 40
 #include "AmUtils.h"
... ...
@@ -797,6 +800,8 @@ void sip_target_set::debug()
797 800
     }
798 801
 }
799 802
 
803
+bool _resolver::disable_srv = false;
804
+
800 805
 _resolver::_resolver()
801 806
     : cache(DNS_CACHE_SIZE)
802 807
 {
... ...
@@ -950,7 +955,8 @@ int _resolver::str2ip(const char* name,
950 955
 	    return 1;
951 956
 	}
952 957
 	else if(ret < 0) {
953
-	    ERROR("while trying to detect an IPv4 address '%s': %s",name,strerror(errno));
958
+	    ERROR("while trying to detect an IPv4 address '%s': %s",
959
+		  name,strerror(errno));
954 960
 	    return ret;
955 961
 	}
956 962
     }
... ...
@@ -962,7 +968,8 @@ int _resolver::str2ip(const char* name,
962 968
 	    return 1;
963 969
 	}
964 970
 	else if(ret < 0) {
965
-	    ERROR("while trying to detect an IPv6 address '%s': %s",name,strerror(errno));
971
+	    ERROR("while trying to detect an IPv6 address '%s': %s",
972
+		  name,strerror(errno));
966 973
 	    return ret;
967 974
 	}
968 975
     }
... ...
@@ -970,6 +977,113 @@ int _resolver::str2ip(const char* name,
970 977
     return 0;
971 978
 }
972 979
 
980
+int _resolver::set_destination_ip(const cstring& next_hop,
981
+				  unsigned short next_port,
982
+				  const cstring& next_trsp,
983
+				  sockaddr_storage* remote_ip,
984
+				  dns_handle* h_dns)
985
+{
986
+
987
+    string nh = c2stlstr(next_hop);
988
+
989
+    DBG("checking whether '%s' is IP address...\n", nh.c_str());
990
+    if (am_inet_pton(nh.c_str(), remote_ip) != 1) {
991
+
992
+	// nh does NOT contain a valid IP address
993
+    
994
+	if(!next_port) {
995
+	    // no explicit port specified,
996
+	    // try SRV first
997
+	    if (disable_srv) {
998
+		DBG("no port specified, but DNS SRV disabled (skipping).\n");
999
+	    } else {
1000
+		string srv_name = "_sip._";
1001
+		if(!next_trsp.len || !lower_cmp_n(next_trsp,"udp")){
1002
+		    srv_name += "udp";
1003
+		}
1004
+		else if(!lower_cmp_n(next_trsp,"tcp")) {
1005
+		    srv_name += "tcp";
1006
+		}
1007
+		else {
1008
+		    DBG("unsupported transport: skip SRV lookup");
1009
+		    goto no_SRV;
1010
+		}
1011
+
1012
+		srv_name += "." + nh;
1013
+
1014
+		DBG("no port specified, looking up SRV '%s'...\n",
1015
+		    srv_name.c_str());
1016
+
1017
+		if(!resolver::instance()->resolve_name(srv_name.c_str(),
1018
+						       h_dns,remote_ip,
1019
+						       IPv4,dns_r_srv)){
1020
+		    return 0;
1021
+		}
1022
+
1023
+		DBG("no SRV record for %s",srv_name.c_str());
1024
+	    }
1025
+	}
1026
+
1027
+    no_SRV:
1028
+	memset(remote_ip,0,sizeof(sockaddr_storage));
1029
+	int err = resolver::instance()->resolve_name(nh.c_str(),
1030
+						     h_dns,remote_ip,
1031
+						     IPv4);
1032
+	if(err < 0){
1033
+	    ERROR("Unresolvable Request URI domain\n");
1034
+	    return -478;
1035
+	}
1036
+    }
1037
+    else {
1038
+	am_set_port(remote_ip,next_port);
1039
+    }
1040
+
1041
+    if(!am_get_port(remote_ip)) {
1042
+	if(!next_port) next_port = 5060;
1043
+	am_set_port(remote_ip,next_port);
1044
+    }
1045
+
1046
+    DBG("set destination to %s:%u\n",
1047
+	nh.c_str(), am_get_port(remote_ip));
1048
+    
1049
+    return 0;
1050
+}
1051
+
1052
+int _resolver::resolve_targets(const list<sip_destination>& dest_list,
1053
+			       sip_target_set* targets)
1054
+{
1055
+    for(list<sip_destination>::const_iterator it = dest_list.begin();
1056
+	it != dest_list.end(); it++) {
1057
+	
1058
+	sip_target t;
1059
+	dns_handle h_dns;
1060
+
1061
+	DBG("sip_destination: %.*s:%u/%.*s",
1062
+	    it->host.len,it->host.s,
1063
+	    it->port,
1064
+	    it->trsp.len,it->trsp.s);
1065
+
1066
+	if(set_destination_ip(it->host,it->port,it->trsp,&t.ss,&h_dns) != 0) {
1067
+	    ERROR("Unresolvable destination");
1068
+	    return -478;
1069
+	}
1070
+	if(it->trsp.len && (it->trsp.len <= SIP_TRSP_SIZE_MAX)) {
1071
+	    memcpy(t.trsp,it->trsp.s,it->trsp.len);
1072
+	    t.trsp[it->trsp.len] = '\0';
1073
+	}
1074
+	else {
1075
+	    t.trsp[0] = '\0';
1076
+	}
1077
+
1078
+	do {
1079
+	    targets->dest_list.push_back(t);
1080
+
1081
+	} while(h_dns.next_ip(&t.ss) == 0);
1082
+    }
1083
+
1084
+    return 0;
1085
+}
1086
+
973 1087
 void _resolver::run()
974 1088
 {
975 1089
     struct timespec tick,rem;
... ...
@@ -242,6 +242,9 @@ class _resolver
242 242
     : AmThread
243 243
 {
244 244
 public:
245
+    // disable SRV lookups
246
+    static bool disable_srv;
247
+
245 248
     int resolve_name(const char* name, 
246 249
 		     dns_handle* h,
247 250
 		     sockaddr_storage* sa,
... ...
@@ -254,10 +257,24 @@ public:
254 257
 
255 258
     int query_dns(const char* name, dns_entry_map& entry_map, dns_rr_type t);
256 259
 
260
+    /**
261
+     * Transforms all elements of a destination list into
262
+     * a target set, thus resolving all DNS names and
263
+     * converting IPs into a sockaddr_storage.
264
+     */
265
+    int resolve_targets(const list<sip_destination>& dest_list,
266
+			sip_target_set* targets);
267
+
257 268
 protected:
258 269
     _resolver();
259 270
     ~_resolver();
260 271
 
272
+    int set_destination_ip(const cstring& next_hop,
273
+			   unsigned short next_port,
274
+			   const cstring& next_trsp,
275
+			   sockaddr_storage* remote_ip,
276
+			   dns_handle* h_dns);
277
+
261 278
     void run();
262 279
     void on_stop() {}
263 280
 
... ...
@@ -879,100 +879,6 @@ int _trans_layer::set_next_hop(sip_msg* msg,
879 879
     return 0;
880 880
 }
881 881
 
882
-
883
-int _trans_layer::set_destination_ip(const cstring* next_hop,
884
-				     unsigned short next_port,
885
-				     sockaddr_storage* remote_ip,
886
-				     dns_handle* h_dns)
887
-{
888
-
889
-    string nh = c2stlstr(*next_hop);
890
-
891
-    DBG("checking whether '%s' is IP address...\n", nh.c_str());
892
-    if (am_inet_pton(nh.c_str(), remote_ip) != 1) {
893
-
894
-	// nh does NOT contain a valid IP address
895
-    
896
-	if(!next_port){
897
-	    // no explicit port specified,
898
-	    // try SRV first
899
-	    if (AmConfig::DisableDNSSRV) {
900
-		DBG("no port specified, but DNS SRV disabled (skipping).\n");
901
-	    } else {
902
-		string srv_name = "_sip._udp." + nh;
903
-
904
-		DBG("no port specified, looking up SRV '%s'...\n",
905
-		    srv_name.c_str());
906
-
907
-		if(!resolver::instance()->resolve_name(srv_name.c_str(),
908
-						       h_dns,remote_ip,IPv4,
909
-						       dns_r_srv)){
910
-		    return 0;
911
-		}
912
-
913
-		DBG("no SRV record for %s",srv_name.c_str());
914
-	    }
915
-	}
916
-
917
-	memset(remote_ip,0,sizeof(sockaddr_storage));
918
-	int err = resolver::instance()->resolve_name(nh.c_str(),
919
-						     h_dns,remote_ip,
920
-						     IPv4);
921
-	if(err < 0){
922
-	    ERROR("Unresolvable Request URI domain\n");
923
-	    return -478;
924
-	}
925
-    }
926
-    else {
927
-	am_set_port(remote_ip,next_port);
928
-    }
929
-
930
-    if(!am_get_port(remote_ip)) {
931
-	if(!next_port) next_port = 5060;
932
-	am_set_port(remote_ip,next_port);
933
-    }
934
-
935
-    DBG("set destination to %s:%u\n",
936
-	nh.c_str(), am_get_port(remote_ip));
937
-    
938
-    return 0;
939
-}
940
-
941
-int _trans_layer::resolve_targets(const list<sip_destination>& dest_list,
942
-				  sip_target_set* targets)
943
-{
944
-    for(list<sip_destination>::const_iterator it = dest_list.begin();
945
-	it != dest_list.end(); it++) {
946
-	
947
-	sip_target t;
948
-	dns_handle h_dns;
949
-
950
-	DBG("sip_destination: %.*s:%u/%.*s",
951
-	    it->host.len,it->host.s,
952
-	    it->port,
953
-	    it->trsp.len,it->trsp.s);
954
-
955
-	if(set_destination_ip(&it->host,it->port,&t.ss,&h_dns) != 0) {
956
-	    ERROR("Unresolvable destination");
957
-	    return -478;
958
-	}
959
-	if(it->trsp.len && (it->trsp.len <= SIP_TRSP_SIZE_MAX)) {
960
-	    memcpy(t.trsp,it->trsp.s,it->trsp.len);
961
-	    t.trsp[it->trsp.len] = '\0';
962
-	}
963
-	else {
964
-	    t.trsp[0] = '\0';
965
-	}
966
-
967
-	do {
968
-	    targets->dest_list.push_back(t);
969
-
970
-	} while(h_dns.next_ip(&t.ss) == 0);
971
-    }
972
-
973
-    return 0;
974
-}
975
-
976 882
 static void set_err_reply_from_req(sip_msg* err, sip_msg* req,
977 883
 				   int code, const char* reason)
978 884
 {
... ...
@@ -253,18 +253,7 @@ protected:
253 253
 		     unsigned short* next_port, cstring* next_trsp);
254 254
 
255 255
     /**
256
-     * Transforms all elements of a destination list into
257
-     * a target set, thus resolving all DNS names and
258
-     * converting IPs into a sockaddr_storage.
259 256
      */
260
-    int resolve_targets(const list<sip_destination>& dest_list,
261
-			sip_target_set* targets);
262
-
263
-    /**
264
-     * Fills msg->remote_ip according to next_hop and next_port.
265
-     */
266
-    int set_destination_ip(const cstring* next_hop, unsigned short next_port,
267
-			   sockaddr_storage* remote_ip, dns_handle* h_dns);
268 257
 
269 258
     sip_trans* copy_uac_trans(sip_trans* tr);
270 259