Browse code

Merge branch '1.6-dev' into tcp-trsp

Raphael Coeffic authored on 19/12/2013 14:57:04
Showing 2 changed files
... ...
@@ -48,9 +48,11 @@
48 48
 #include <arpa/nameser.h> 
49 49
 
50 50
 #include <list>
51
+#include <utility>
51 52
 #include <algorithm>
52 53
 
53 54
 using std::pair;
55
+using std::make_pair;
54 56
 using std::list;
55 57
 
56 58
 #include "log.h"
... ...
@@ -586,7 +588,7 @@ int rr_to_dns_entry(dns_record* rr, dns_section_type t,
586 588
 	    // unsupported record type
587 589
 	    return 0;
588 590
 	}
589
-	h->entry_map[name] = dns_e;
591
+	h->entry_map.insert(name,dns_e);
590 592
     }
591 593
     else {
592 594
 	dns_e = it->second;
... ...
@@ -800,6 +802,45 @@ void sip_target_set::debug()
800 802
     }
801 803
 }
802 804
 
805
+dns_entry_map::dns_entry_map()
806
+    : map<string,dns_entry*>()
807
+{
808
+}
809
+
810
+dns_entry_map::~dns_entry_map()
811
+{
812
+    for(iterator it = begin(); it != end(); ++it) {
813
+	dec_ref(it->second);
814
+    }
815
+}
816
+
817
+std::pair<dns_entry_map::iterator, bool>
818
+dns_entry_map::insert(const dns_entry_map::value_type& x)
819
+{
820
+    return dns_entry_map_base::insert(x);
821
+}
822
+
823
+bool dns_entry_map::insert(const string& key, dns_entry* e)
824
+{
825
+    std::pair<iterator, bool> res =
826
+    	insert(make_pair<const key_type&,mapped_type>(key,e));
827
+
828
+    if(res.second) {
829
+	inc_ref(e);
830
+	return true;
831
+    }
832
+
833
+    return false;
834
+}
835
+
836
+dns_entry* dns_entry_map::fetch(const key_type& key)
837
+{
838
+    iterator it = find(key);
839
+    if(it != end())
840
+	return it->second;
841
+    return NULL;
842
+}
843
+
803 844
 bool _resolver::disable_srv = false;
804 845
 
805 846
 _resolver::_resolver()
... ...
@@ -842,18 +883,10 @@ int _resolver::query_dns(const char* name, dns_entry_map& entry_map, dns_rr_type
842 883
 	it != h.entry_map.end(); it++) {
843 884
 
844 885
 	dns_entry* e = it->second;
845
-	if(!e) continue;
846
-
847
-	if((e)->ip_vec.empty()){
848
-	    delete e;
849
-	    e = NULL;
850
-	    continue;
851
-	}
886
+	if(!e || e->ip_vec.empty()) continue;
852 887
 
853 888
 	e->init();
854
-	//inc_ref(e);
855
-
856
-	entry_map[it->first] = e;
889
+	entry_map.insert(it->first,e);
857 890
     }
858 891
 
859 892
     return 0;
... ...
@@ -903,45 +936,28 @@ int _resolver::resolve_name(const char* name,
903 936
 	return -1;
904 937
     }
905 938
 
906
-    int res = -1;
907 939
     for(dns_entry_map::iterator it = entry_map.begin();
908 940
 	it != entry_map.end(); it++) {
909 941
 
910
-	dns_entry*& e = it->second;
911
-	if(e) {
912
-
913
-	    b = cache.get_bucket(hashlittle(it->first.c_str(),
914
-					    it->first.length(),0));
915
-	    // cache the new record
916
-	    if(!b->insert(it->first,e)) {
917
-		// record already cached
918
-
919
-		// this should allow us to avoid some race conditions
920
-		// ex: two simultanous exact same DNS queries
921
-		// -> one will be cached, the other gets destroyed after use
922
-		if(it->first != name) {
923
-		    delete e;
924
-		    e = NULL;
925
-		}
926
-		continue;
927
-	    }
928
-	    
929
-	    DBG("inserted: '%s' -> %s",
930
-		it->first.c_str(),
931
-		e->to_str().c_str());
942
+	if(!it->second) continue;
943
+
944
+	b = cache.get_bucket(hashlittle(it->first.c_str(),
945
+					it->first.length(),0));
946
+	// cache the new record
947
+	if(b->insert(it->first,it->second)) {
948
+	    // cache insert successful
949
+	    DBG("new DNS cache entry: '%s' -> %s",
950
+		it->first.c_str(), it->second->to_str().c_str());
932 951
 	}
933 952
     }
934 953
 
935
-    dns_entry_map::iterator it = entry_map.find(name);
936
-    if(it != entry_map.end()) {
937
-	dns_entry* e = it->second;
938
-	if(e) {
939
-	    // now we should have a valid IP
940
-	    res = e->next_ip(h,sa);
941
-	}
954
+    e = entry_map.fetch(name);
955
+    if(e) {
956
+	// now we should have a valid IP
957
+	return e->next_ip(h,sa);
942 958
     }
943 959
 
944
-    return res;
960
+    return -1;
945 961
 }
946 962
 
947 963
 int _resolver::str2ip(const char* name,
... ...
@@ -1126,6 +1142,7 @@ void _resolver::run()
1126 1142
     }
1127 1143
 }
1128 1144
 
1145
+
1129 1146
 /** EMACS **
1130 1147
  * Local variables:
1131 1148
  * mode: c++
... ...
@@ -37,8 +37,10 @@
37 37
 
38 38
 #include <string>
39 39
 #include <vector>
40
+#include <map>
40 41
 using std::string;
41 42
 using std::vector;
43
+using std::map;
42 44
 
43 45
 #include <netinet/in.h>
44 46
 
... ...
@@ -236,7 +238,23 @@ private:
236 238
     sip_target_set(const sip_target_set&) {}
237 239
 };
238 240
 
239
-typedef map<string,dns_entry*> dns_entry_map;
241
+typedef map<string,dns_entry*> dns_entry_map_base;
242
+
243
+class dns_entry_map
244
+     : public dns_entry_map_base
245
+{
246
+public:
247
+    dns_entry_map();
248
+    ~dns_entry_map();
249
+
250
+    bool insert(const key_type& key, mapped_type e);
251
+    dns_entry* fetch(const key_type& key);
252
+
253
+private:
254
+    // forbid some inherited methods
255
+    mapped_type& operator[](const key_type& k);
256
+    std::pair<iterator, bool> insert(const value_type& x);
257
+};
240 258
 
241 259
 class _resolver
242 260
     : AmThread