| 1 | /* geobackend.hh |
|---|
| 2 | * Copyright (C) 2004 Mark Bergsma <mark@nedworks.org> |
|---|
| 3 | * This software is licensed under the terms of the GPL, version 2. |
|---|
| 4 | * |
|---|
| 5 | * $Id$ |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #include <vector> |
|---|
| 10 | #include <map> |
|---|
| 11 | #include <pthread.h> |
|---|
| 12 | |
|---|
| 13 | #include <pdns/dnsbackend.hh> |
|---|
| 14 | #include <pdns/logger.hh> |
|---|
| 15 | |
|---|
| 16 | #include "ippreftree.hh" |
|---|
| 17 | |
|---|
| 18 | #include "namespaces.hh" |
|---|
| 19 | |
|---|
| 20 | class GeoRecord { |
|---|
| 21 | public: |
|---|
| 22 | GeoRecord(); |
|---|
| 23 | |
|---|
| 24 | string qname; |
|---|
| 25 | string origin; |
|---|
| 26 | string directorfile; |
|---|
| 27 | map<short, string> dirmap; |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | class GeoBackend : public DNSBackend{ |
|---|
| 31 | public: |
|---|
| 32 | |
|---|
| 33 | GeoBackend(const string &suffix); |
|---|
| 34 | ~GeoBackend(); |
|---|
| 35 | |
|---|
| 36 | virtual void lookup(const QType &qtype, const string &qdomain, DNSPacket *pkt_p=0, int zoneId=-1); |
|---|
| 37 | virtual bool list(const string &target, int domain_id); |
|---|
| 38 | virtual bool get(DNSResourceRecord &r); |
|---|
| 39 | virtual bool getSOA(const string &name, SOAData &soadata, DNSPacket *p=0); |
|---|
| 40 | |
|---|
| 41 | virtual void reload(); |
|---|
| 42 | virtual void rediscover(string *status = 0); |
|---|
| 43 | |
|---|
| 44 | private: |
|---|
| 45 | // Static resources, shared by all instances |
|---|
| 46 | static IPPrefTree *ipt; |
|---|
| 47 | static vector<string> nsRecords; |
|---|
| 48 | static map<string, GeoRecord*> georecords; |
|---|
| 49 | static string soaMasterServer; |
|---|
| 50 | static string soaHostmaster; |
|---|
| 51 | static string zoneName; |
|---|
| 52 | static uint32_t geoTTL; |
|---|
| 53 | static uint32_t nsTTL; |
|---|
| 54 | static time_t lastDiscoverTime; |
|---|
| 55 | const static string logprefix; |
|---|
| 56 | |
|---|
| 57 | bool forceReload; |
|---|
| 58 | |
|---|
| 59 | // Locking |
|---|
| 60 | static bool first; |
|---|
| 61 | static int backendcount; |
|---|
| 62 | static pthread_mutex_t startup_lock; |
|---|
| 63 | static pthread_mutex_t ipt_lock; |
|---|
| 64 | |
|---|
| 65 | vector<DNSResourceRecord*> answers; |
|---|
| 66 | vector<DNSResourceRecord*>::const_iterator i_answers; |
|---|
| 67 | |
|---|
| 68 | void answerGeoRecord(const QType &qtype, const string &qdomain, DNSPacket *p); |
|---|
| 69 | void answerLocalhostRecord(const string &qdomain, DNSPacket *p); |
|---|
| 70 | void queueNSRecords(const string &qname); |
|---|
| 71 | void queueGeoRecords(); |
|---|
| 72 | void fillGeoResourceRecord(const string &qname, const string &target, DNSResourceRecord *rr); |
|---|
| 73 | const inline string resolveTarget(const GeoRecord &gr, short isocode) const; |
|---|
| 74 | |
|---|
| 75 | void loadZoneName(); |
|---|
| 76 | void loadTTLValues(); |
|---|
| 77 | void loadSOAValues(); |
|---|
| 78 | void loadNSRecords(); |
|---|
| 79 | void loadIPLocationMap(); |
|---|
| 80 | void loadGeoRecords(); |
|---|
| 81 | void loadDirectorMaps(const vector<GeoRecord*> &newgrs); |
|---|
| 82 | void loadDirectorMap(GeoRecord &gr); |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | class GeoFactory : public BackendFactory{ |
|---|
| 86 | public: |
|---|
| 87 | GeoFactory() : BackendFactory("geo") {} |
|---|
| 88 | |
|---|
| 89 | void declareArguments(const string &suffix = "") { |
|---|
| 90 | declare(suffix, "zone", "zonename to be served", ""); |
|---|
| 91 | declare(suffix, "soa-values", "values of the SOA master nameserver and hostmaster fields, comma seperated", ""); |
|---|
| 92 | declare(suffix, "ns-records", "targets of the NS records, comma seperated.", ""); |
|---|
| 93 | declare(suffix, "ttl", "TTL value for geo records", "3600"); |
|---|
| 94 | declare(suffix, "ns-ttl", "TTL value for NS records", "86400"); |
|---|
| 95 | declare(suffix, "ip-map-zonefile", "path to the rbldnsd format zonefile", "zz.countries.nerd.dk.rbldnsd"); |
|---|
| 96 | declare(suffix, "maps", "list of paths to director maps or directories containing director map files", ""); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | DNSBackend *make(const string &suffix) { |
|---|
| 100 | return new GeoBackend(suffix); |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | class GeoLoader { |
|---|
| 105 | public: |
|---|
| 106 | GeoLoader() { |
|---|
| 107 | BackendMakers().report(new GeoFactory); |
|---|
| 108 | |
|---|
| 109 | L << Logger::Info << "[GeoBackend] This is the geobackend (" |
|---|
| 110 | __DATE__", "__TIME__" - $Revision: 1.1 $) reporting" << endl; |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | static GeoLoader geoloader; |
|---|