diff --git a/agents/dnsagent.go b/agents/dnsagent.go index e2e34c9e5..1ca0c9172 100644 --- a/agents/dnsagent.go +++ b/agents/dnsagent.go @@ -20,7 +20,6 @@ package agents import ( "fmt" - "net" "strings" "github.com/cgrates/cgrates/config" @@ -73,7 +72,7 @@ func (da *DNSAgent) ListenAndServe() error { // requests are reaching here asynchronously func (da *DNSAgent) handleMessage(w dns.ResponseWriter, req *dns.Msg) { fmt.Printf("got message: %+v\n", req) - rply := new(dns.Msg) + /*rply := new(dns.Msg) rply.SetReply(req) switch req.Question[0].Qtype { case dns.TypeA: @@ -90,7 +89,17 @@ func (da *DNSAgent) handleMessage(w dns.ResponseWriter, req *dns.Msg) { ) } } - - fmt.Printf("send reply message: %+v\n", rply) w.WriteMsg(rply) + */ + reqVars := make(map[string]interface{}) + reqVars[QueryType] = dns.TypeToString[req.Question[0].Qtype] + if req.Question[0].Qtype == dns.TypeNAPTR { + e164, err := e164FromNAPTR(req.Question[0].Name) + if err != nil { + utils.Logger.Warning( + fmt.Sprintf("<%s> decoding NAPTR query: <%s>, err: %s", + utils.DNSAgent, req.Question[0].Name, err.Error())) + } + reqVars[E164Address] = e164 + } } diff --git a/agents/libdns.go b/agents/libdns.go new file mode 100644 index 000000000..b2f5a95b1 --- /dev/null +++ b/agents/libdns.go @@ -0,0 +1,42 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package agents + +import ( + "errors" + "strings" + + "github.com/cgrates/cgrates/utils" +) + +const ( + QueryType = "QueryType" + E164Address = "E164" +) + +// e164FromNAPTR extracts the E164 address out of a NAPTR name record +func e164FromNAPTR(name string) (e164 string, err error) { + i := strings.Index(name, ".e164.arpa") + if i == -1 { + return "", errors.New("unknown format") + } + e164 = utils.ReverseString( + strings.Replace(name[:i], ".", "", -1)) + return +} diff --git a/agents/libdns_test.go b/agents/libdns_test.go new file mode 100644 index 000000000..bef6861e3 --- /dev/null +++ b/agents/libdns_test.go @@ -0,0 +1,29 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package agents + +import "testing" + +func TestE164FromNAPTR(t *testing.T) { + if e164, err := e164FromNAPTR("8.7.6.5.4.3.2.1.0.1.6.e164.arpa."); err != nil { + t.Error(err) + } else if e164 != "61012345678" { + t.Errorf("received: <%s>", e164) + } +} diff --git a/utils/coreutils.go b/utils/coreutils.go index bb4760e75..c504c869f 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -937,3 +937,11 @@ type CachedRPCResponse struct { Result interface{} Error error } + +func ReverseString(s string) string { + r := []rune(s) + for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { + r[i], r[j] = r[j], r[i] + } + return string(r) +}