resolver.h

00001 /*
00002  * $Id: resolver.h 1460 2009-07-08 12:50:39Z rco $
00003  *
00004  * Copyright (C) 2007 Raphael Coeffic
00005  *
00006  * This file is part of SEMS, a free SIP media server.
00007  *
00008  * SEMS is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version. This program is released under
00012  * the GPL with the additional exemption that compiling, linking,
00013  * and/or using OpenSSL is allowed.
00014  *
00015  * For a license to use the SEMS software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * SEMS is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  */
00029 #ifndef _resolver_h_
00030 #define _resolver_h_
00031 
00032 #include "singleton.h"
00033 #include "hash_table.h"
00034 #include "atomic_types.h"
00035 #include "parse_dns.h"
00036 #include "parse_next_hop.h"
00037 
00038 #include <string>
00039 #include <vector>
00040 using std::string;
00041 using std::vector;
00042 
00043 #include <netinet/in.h>
00044 
00045 #define DNS_CACHE_SIZE 128
00046 
00047 enum address_type {
00048 
00049     IPnone=0,
00050     IPv4=1,
00051     IPv6=2
00052 };
00053 
00054 enum proto_type {
00055     
00056     TCP=1,
00057     UDP=2
00058 };
00059 
00060 struct dns_handle;
00061 
00062 struct dns_base_entry
00063 {
00064     long int expire;
00065 
00066     dns_base_entry()
00067         :expire(0)
00068     {}
00069 
00070     virtual ~dns_base_entry() {}
00071 };
00072 
00073 class dns_entry
00074     : public atomic_ref_cnt,
00075       public dns_base_entry
00076 {
00077     virtual dns_base_entry* get_rr(dns_record* rr, u_char* begin, u_char* end)=0;
00078 
00079 public:
00080     vector<dns_base_entry*> ip_vec;
00081 
00082     static dns_entry* make_entry(ns_type t);
00083 
00084     dns_entry();
00085     virtual ~dns_entry();
00086     virtual void init()=0;
00087     virtual void add_rr(dns_record* rr, u_char* begin, u_char* end, long now);
00088     virtual int next_ip(dns_handle* h, sockaddr_storage* sa)=0;
00089 };
00090 
00091 typedef ht_map_bucket<string,dns_entry> dns_bucket_base;
00092 
00093 class dns_bucket
00094     : protected dns_bucket_base
00095 {
00096     friend class _resolver;
00097 public:
00098     dns_bucket(unsigned long id);
00099     bool insert(const string& name, dns_entry* e);
00100     bool remove(const string& name);
00101     dns_entry* find(const string& name);
00102 };
00103 
00104 typedef hash_table<dns_bucket> dns_cache;
00105 
00106 struct ip_entry
00107     : public dns_base_entry
00108 {
00109     address_type  type;
00110 
00111     union {
00112         in_addr       addr;
00113         in6_addr      addr6;
00114     };
00115 
00116     virtual void to_sa(sockaddr_storage* sa);
00117 };
00118 
00119 struct ip_port_entry
00120     : public ip_entry
00121 {
00122     unsigned short port;
00123 
00124     virtual void to_sa(sockaddr_storage* sa);
00125 };
00126 
00127 class dns_ip_entry
00128     : public dns_entry
00129 {
00130 public:
00131     dns_ip_entry()
00132         : dns_entry()
00133     {}
00134 
00135     void init(){};
00136     dns_base_entry* get_rr(dns_record* rr, u_char* begin, u_char* end);
00137     int next_ip(dns_handle* h, sockaddr_storage* sa);
00138 
00139     int fill_ip_list(const list<host_port>& ip_list);
00140 };
00141 
00142 class dns_srv_entry;
00143 
00144 struct dns_handle
00145 {
00146     dns_handle();
00147     ~dns_handle();
00148 
00149     bool valid();
00150     bool eoip();
00151 
00152     int next_ip(sockaddr_storage* sa);
00153     const dns_handle& operator = (const dns_handle& rh);
00154 
00155 private:
00156     friend class _resolver;
00157     friend class dns_entry;
00158     friend class dns_srv_entry;
00159     friend class dns_ip_entry;
00160 
00161     dns_srv_entry* srv_e;
00162     int            srv_n;
00163     unsigned int   srv_used;
00164     unsigned short port;
00165 
00166     dns_ip_entry*  ip_e;
00167     int            ip_n;
00168 };
00169 
00170 class _resolver
00171     : AmThread
00172 {
00173 public:
00174     int resolve_name(const char* name, 
00175                      dns_handle* h,
00176                      sockaddr_storage* sa,
00177                      const address_type types);
00178 
00179 protected:
00180     _resolver();
00181     ~_resolver();
00182 
00183     int query_dns(const char* name,
00184                   dns_entry** e,
00185                   long now);
00186 
00187     void run();
00188     void on_stop() {}
00189 
00190 private:
00191     dns_cache cache;
00192 };
00193 
00194 typedef singleton<_resolver> resolver;
00195 
00197 int str2ip(const char* name,
00198            sockaddr_storage* sa,
00199            const address_type types);
00200 
00201 #endif
00202 
Home |  Recent changes |  Search |  Glossary |  Sitemap |  Login