Skip to content

Commit a026dfa

Browse files
committed
encoding according to defined type in dictionary
1 parent 3eaf84d commit a026dfa

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/corbamico/ccg-go
22

3-
go 1.13
3+
go 1.15
44

55
require (
66
github.com/fiorix/go-diameter/v4 v4.0.1
7-
github.com/google/gopacket v1.1.17
7+
github.com/google/gopacket v1.1.18
88
)

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
4-
github.com/fiorix/go-diameter v3.0.2+incompatible h1:PLk2NkqMYILlEdeiz0hJVB3xcuuL+Gc9iCaEgjfbs5w=
54
github.com/fiorix/go-diameter/v4 v4.0.1 h1:IEjXnzKDej/a1Z703XGfFbgvengXIBLuWLvAGWv+ZK8=
65
github.com/fiorix/go-diameter/v4 v4.0.1/go.mod h1:Qx/+pf+c9sBUHWq1d7EH3bkdwN8U0mUpdy9BieDw6UQ=
76
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -10,6 +9,8 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
109
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
1110
github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY=
1211
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
12+
github.com/google/gopacket v1.1.18 h1:lum7VRA9kdlvBi7/v2p7/zcbkduHaCH/SVVyurs7OpY=
13+
github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
1314
github.com/ishidawataru/sctp v0.0.0-20190922091402-408ec287e38c h1:PwVcPU2rqkJIG0Lz/UGbGcbfi/HhEbOIId+w4xkbGHQ=
1415
github.com/ishidawataru/sctp v0.0.0-20190922091402-408ec287e38c/go.mod h1:co9pwDoBCm1kGxawmb4sPq0cSIOOWNPT4KnHotMP1Zg=
1516
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

internal/json2diam.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ func isJSON(s []byte) bool {
149149
func encode(key string, value interface{}, appid uint32) (avp *diam.AVP, err error) {
150150
var avpcode int
151151
var subAVP *diam.AVP
152-
//var dictAVP *dict.AVP
153152
var matched bool
153+
var foundAVP bool
154+
154155
vendoridInt := int(0)
155156
dictAVP := &dict.AVP{Code: 0, VendorID: 0}
156157

@@ -180,14 +181,28 @@ func encode(key string, value interface{}, appid uint32) (avp *diam.AVP, err err
180181
if dictAVP, err = dict.Default.FindAVP(appid, key); err != nil {
181182
return nil, err
182183
}
184+
foundAVP = true
183185
}
184186

185187
//2. handle value
186-
//TODO, should be handle value according dictAVP found in dictionary
188+
//should be handle value according dictAVP found in dictionary
189+
//2020-08-20, Handle foundAVP
187190
switch value.(type) {
188191
case float64:
192+
if foundAVP {
193+
if avp := newAVP(dictAVP.Code, dictAVP.VendorID, value, dictAVP.Data.Type); avp != nil {
194+
return avp, nil
195+
}
196+
return nil, errors.New("Encode Error, Mismatch Type with defined in Dictionary")
197+
}
189198
return diam.NewAVP(dictAVP.Code, 0, dictAVP.VendorID, datatype.Unsigned32(value.(float64))), nil
190199
case string:
200+
if foundAVP {
201+
if avp := newAVP(dictAVP.Code, dictAVP.VendorID, value, dictAVP.Data.Type); avp != nil {
202+
return avp, nil
203+
}
204+
return nil, errors.New("Encode Error, Mismatch Type with defined in Dictionary")
205+
}
191206
return diam.NewAVP(dictAVP.Code, 0, dictAVP.VendorID, datatype.UTF8String(value.(string))), nil
192207
case map[string]interface{}:
193208
grouped := &diam.GroupedAVP{
@@ -204,3 +219,43 @@ func encode(key string, value interface{}, appid uint32) (avp *diam.AVP, err err
204219
return nil, errors.New("Unknown Data Type")
205220
}
206221
}
222+
223+
func newAVP(avpcode uint32, vendorid uint32, value interface{}, typeid datatype.TypeID) *diam.AVP {
224+
switch typeid {
225+
case datatype.UnknownType:
226+
case datatype.AddressType:
227+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Address(value.(string)))
228+
case datatype.DiameterIdentityType:
229+
return diam.NewAVP(avpcode, 0, vendorid, datatype.DiameterIdentity(value.(string)))
230+
case datatype.DiameterURIType:
231+
return diam.NewAVP(avpcode, 0, vendorid, datatype.DiameterURI(value.(string)))
232+
case datatype.EnumeratedType:
233+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Enumerated(value.(float64)))
234+
case datatype.Float32Type:
235+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Float32(value.(float64)))
236+
case datatype.Float64Type:
237+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Float64(value.(float64)))
238+
case datatype.GroupedType:
239+
case datatype.IPFilterRuleType:
240+
return diam.NewAVP(avpcode, 0, vendorid, datatype.IPFilterRule(value.(string)))
241+
case datatype.IPv4Type:
242+
return diam.NewAVP(avpcode, 0, vendorid, datatype.IPv4(value.(string)))
243+
case datatype.Integer32Type:
244+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Integer32(value.(float64)))
245+
case datatype.Integer64Type:
246+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Integer64(value.(float64)))
247+
case datatype.OctetStringType:
248+
return diam.NewAVP(avpcode, 0, vendorid, datatype.OctetString(value.(string)))
249+
case datatype.TimeType:
250+
if t, err := time.Parse(time.RFC3339, value.(string)); err == nil {
251+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Time(t))
252+
}
253+
case datatype.UTF8StringType:
254+
return diam.NewAVP(avpcode, 0, vendorid, datatype.UTF8String(value.(string)))
255+
case datatype.Unsigned32Type:
256+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Unsigned32(value.(float64)))
257+
case datatype.Unsigned64Type:
258+
return diam.NewAVP(avpcode, 0, vendorid, datatype.Unsigned64(value.(float64)))
259+
}
260+
return nil
261+
}

0 commit comments

Comments
 (0)