mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Beautity populating host ip addresses in diameter
This commit is contained in:
committed by
Dan Christian Bogos
parent
df5e9bd8be
commit
5910d3a14a
@@ -86,12 +86,11 @@ func (da *DiameterAgent) handlers() diam.Handler {
|
||||
ProductName: datatype.UTF8String(da.cgrCfg.DiameterAgentCfg().ProductName),
|
||||
FirmwareRevision: datatype.Unsigned32(utils.DIAMETER_FIRMWARE_REVISION),
|
||||
}
|
||||
ipPort := strings.Split(da.cgrCfg.DiameterAgentCfg().Listen, utils.InInFieldSep)
|
||||
if ipPort[0] == "" {
|
||||
hosts := disectDiamListen(da.cgrCfg.DiameterAgentCfg().Listen)
|
||||
if len(hosts) == 0 {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<%s> scan for interfaces err: %s",
|
||||
utils.DiameterAgent, err.Error()))
|
||||
utils.Logger.Err(fmt.Sprintf("<%s> error : %v, when quering interfaces for address", utils.DiameterAgent, err))
|
||||
}
|
||||
for _, inter := range interfaces {
|
||||
addrs, err := inter.Addrs()
|
||||
@@ -101,14 +100,13 @@ func (da *DiameterAgent) handlers() diam.Handler {
|
||||
continue
|
||||
}
|
||||
for _, iAddr := range addrs {
|
||||
settings.HostIPAddresses = append(settings.HostIPAddresses, datatype.Address(
|
||||
strings.Split(iAddr.String(), utils.HDR_VAL_SEP)[0])) // address came in form x.y.z.t/24
|
||||
hosts = append(hosts, strings.Split(iAddr.String(), utils.HDR_VAL_SEP)[0]) // address came in form x.y.z.t/24
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, ip := range strings.Split(ipPort[0], utils.HDR_VAL_SEP) {
|
||||
settings.HostIPAddresses = append(settings.HostIPAddresses, datatype.Address(ip))
|
||||
}
|
||||
}
|
||||
settings.HostIPAddresses = make([]datatype.Address, len(hosts))
|
||||
for i, host := range hosts {
|
||||
settings.HostIPAddresses[i] = datatype.Address(host)
|
||||
}
|
||||
|
||||
dSM := sm.New(settings)
|
||||
|
||||
@@ -476,3 +476,16 @@ func diamBareErr(m *diam.Message, resCode uint32) (a *diam.Message) {
|
||||
a.Header.CommandFlags = diam.ErrorFlag
|
||||
return
|
||||
}
|
||||
|
||||
func disectDiamListen(addrs string) (ipAddrs []string) {
|
||||
ipPort := strings.Split(addrs, utils.InInFieldSep)
|
||||
if ipPort[0] == "" {
|
||||
return
|
||||
}
|
||||
ips := strings.Split(ipPort[0], utils.HDR_VAL_SEP)
|
||||
ipAddrs = make([]string, len(ips))
|
||||
for i, ip := range ips {
|
||||
ipAddrs[i] = ip
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -338,3 +338,22 @@ func TestMessageSetAVPsWithPath5(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v \n, received: %+v \n", eMessage, m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestdisectDiamListen(t *testing.T) {
|
||||
expIPs := []string{"192.168.56.203", "192.168.57.203"}
|
||||
rvc := disectDiamListen("192.168.56.203/192.168.57.203:3869")
|
||||
if !reflect.DeepEqual(expIPs, rvc) {
|
||||
t.Errorf("Expecting: %+v \n, received: %+v \n ", expIPs, rvc)
|
||||
}
|
||||
expIPs = []string{"192.168.56.203"}
|
||||
rvc = disectDiamListen("192.168.56.203:3869")
|
||||
if !reflect.DeepEqual(expIPs, rvc) {
|
||||
t.Errorf("Expecting: %+v \n, received: %+v \n ", expIPs, rvc)
|
||||
}
|
||||
expIPs = []string{}
|
||||
rvc = disectDiamListen(":3869")
|
||||
if !reflect.DeepEqual(expIPs, rvc) {
|
||||
t.Errorf("Expecting: %+v \n, received: %+v \n ", expIPs, rvc)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user