mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
DNSAgent - parsing NAPTR and E164 from within
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
42
agents/libdns.go
Normal file
42
agents/libdns.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
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
|
||||
}
|
||||
29
agents/libdns_test.go
Normal file
29
agents/libdns_test.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user