mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added new dataconvertor: *ip2hex
This commit is contained in:
committed by
Dan Christian Bogos
parent
73bfa6bdcf
commit
241272ca31
@@ -6,6 +6,8 @@ cgrates (0.10) UNRELEASED; urgency=medium
|
||||
* [SessionS] Added check for missing CGRevent
|
||||
* [DiameterAgent] Using String function from diam.Message instead of
|
||||
ToJSON for request String method
|
||||
* [DiameterAgent] Updated 3gp_vendor dictionary
|
||||
* [Templates] Added new dataconvertor: *ip2hex
|
||||
|
||||
[ Voivozeanu Teofil ]
|
||||
* [AgentRequest] Add support for *group type and correctly overwrite
|
||||
|
||||
@@ -2061,7 +2061,7 @@
|
||||
<data type="UTF8String" />
|
||||
</avp>
|
||||
<avp name="3GPP-SGSN-Address" code="6" must="V,M" may="P" must-not="-" may-encrypt="N" vendor-id="10415">
|
||||
<data type="OctetString" />
|
||||
<data type="IPv4" />
|
||||
</avp>
|
||||
<avp name="3GPP-GGSN-Address" code="7" must="V,M" may="P" must-not="-" may-encrypt="N" vendor-id="10415">
|
||||
<data type="OctetString" />
|
||||
|
||||
@@ -586,6 +586,7 @@ const (
|
||||
ResourceUsage = "ResourceUsage"
|
||||
MetaDuration = "*duration"
|
||||
MetaLibPhoneNumber = "*libphonenumber"
|
||||
MetaIP2Hex = "*ip2hex"
|
||||
MetaReload = "*reload"
|
||||
MetaLoad = "*load"
|
||||
MetaRemove = "*remove"
|
||||
|
||||
@@ -19,7 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -71,6 +73,8 @@ func NewDataConverter(params string) (conv DataConverter, err error) {
|
||||
return NewDivideConverter(params[len(MetaDivide)+1:])
|
||||
case params == MetaDuration:
|
||||
return NewDurationConverter("")
|
||||
case params == MetaIP2Hex:
|
||||
return &IP2HexConvertor{}, nil
|
||||
case strings.HasPrefix(params, MetaLibPhoneNumber):
|
||||
if len(params) == len(MetaLibPhoneNumber) {
|
||||
return NewPhoneNumberConverter("")
|
||||
@@ -273,3 +277,25 @@ func (lc *PhoneNumberConverter) Convert(in interface{}) (out interface{}, err er
|
||||
}
|
||||
return phonenumbers.Format(num, lc.Format), nil
|
||||
}
|
||||
|
||||
// HexConvertor will round floats
|
||||
type IP2HexConvertor struct{}
|
||||
|
||||
func (_ *IP2HexConvertor) Convert(in interface{}) (out interface{}, err error) {
|
||||
var ip net.IP
|
||||
switch val := in.(type) {
|
||||
case string:
|
||||
ip = net.ParseIP(val)
|
||||
case net.IP:
|
||||
ip = val
|
||||
default:
|
||||
src := IfaceAsString(in)
|
||||
ip = net.ParseIP(src)
|
||||
}
|
||||
|
||||
hx := hex.EncodeToString([]byte(ip))
|
||||
if len(hx) < 8 {
|
||||
return hx, nil
|
||||
}
|
||||
return "0x" + string([]byte(hx)[len(hx)-8:]), nil
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package utils
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -611,3 +612,20 @@ func TestPhoneNumberConverter(t *testing.T) {
|
||||
t.Errorf("expecting: %+v, received: %+v", "+496502530000", phoneNumberConverted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexConvertor(t *testing.T) {
|
||||
hx := IP2HexConvertor{}
|
||||
val := "127.0.0.1"
|
||||
expected := "0x7f000001"
|
||||
if rpl, err := hx.Convert(val); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rpl) {
|
||||
t.Errorf("expecting: %+v, received: %+v", expected, rpl)
|
||||
}
|
||||
val2 := net.ParseIP("127.0.0.1")
|
||||
if rpl, err := hx.Convert(val2); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rpl) {
|
||||
t.Errorf("expecting: %+v, received: %+v", expected, rpl)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user