Skip to content

Commit

Permalink
Ability to load additional diameter dictionary definition (#2)
Browse files Browse the repository at this point in the history
* add dict_generator cli
* add dict.load(dictionary)
  • Loading branch information
lwlee2608 authored Nov 12, 2023
1 parent 31d73c5 commit 3b540b8
Show file tree
Hide file tree
Showing 6 changed files with 918 additions and 27 deletions.
87 changes: 87 additions & 0 deletions cmd/dict_generator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"flag"
"fmt"
"io"
"os"
"strings"

"github.com/fiorix/go-diameter/v4/diam/dict"
log "github.com/sirupsen/logrus"
)

func main() {
var dictionary string
var output string

flag.StringVar(&dictionary, "dictionary", "dictionary.xml", "Dictionary")
flag.StringVar(&output, "output", "const.js", "Output file")
flag.Parse()

file, err := os.Open(dictionary)
if err != nil {
log.Fatalf("Error opening dictioanry: %s\n", err)
}
defer file.Close()

parser := dict.Default
parser.Load(file)
if err != nil {
log.Fatalf("Error parsing dictioanry: %s\n", err)
}

w, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.Fatalf("Error opening file: %s\n", err)
}
defer w.Close()

PrintFlags(w)
PrintAvpCode(w, parser)
PrintVendorId(w, parser)
}

func PrintFlags(w io.Writer) {
fmt.Fprintf(w, "export const flags = {\n")
fmt.Fprintf(w, " Vbit: 0x80,\n")
fmt.Fprintf(w, " Mbit: 0x40,\n")
fmt.Fprintf(w, " Pbit: 0x20,\n")
fmt.Fprintf(w, "}\n")
fmt.Fprintf(w, "\n")
}

func PrintAvpCode(w io.Writer, parser *dict.Parser) {
fmt.Fprintf(w, "export const avpCode = {\n")
for _, app := range parser.Apps() {
fmt.Fprintf(w, " // %s\n", app.Name)
for _, avp := range app.AVP {
name := strings.ReplaceAll(avp.Name, "-", "")
fmt.Fprintf(w, " %s: %d,\n", name, avp.Code)
}
}
fmt.Fprintf(w, "}\n")
fmt.Fprintf(w, "\n")

}

func PrintVendorId(w io.Writer, parser *dict.Parser) {
vendorIds := make(map[uint32]struct{})
exists := struct{}{}

fmt.Fprintf(w, "export const vendorId = {\n")
for _, app := range parser.Apps() {
for _, vendor := range app.Vendor {
// Remove duplicate vendorId
_, found := vendorIds[vendor.ID]
if found {
continue
}
vendorIds[vendor.ID] = exists

fmt.Fprintf(w, " %s: %d,\n", vendor.Name, vendor.ID)
}
}
fmt.Fprintf(w, "}\n")
fmt.Fprintf(w, "\n")
}
22 changes: 20 additions & 2 deletions diameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package diameter
import (
"errors"
"net"
"os"
"time"

"github.com/fiorix/go-diameter/diam/avp"
Expand Down Expand Up @@ -31,7 +32,9 @@ type DataType struct{}

type AVP struct{}

func (*Diameter) NewClient() (*DiameterClient, error) {
type Dict struct{}

func (*Diameter) XClient() (*DiameterClient, error) {

// TODO make all this configurable later
cfg := &sm.Settings{
Expand Down Expand Up @@ -118,7 +121,7 @@ func (d *Diameter) Send(client *DiameterClient, msg *DiameterMessage) (uint32, e

// Wait for CCA
resp := <-client.hopIds[hopByHopID]
//log.Infof("Received CCA \n%s", resp)
log.Infof("Received CCA \n%s", resp)

delete(client.hopIds, hopByHopID)

Expand Down Expand Up @@ -228,7 +231,22 @@ func (a *AVP) XNew(code uint32, vendor uint32, flags uint8, data datatype.Type)
return diam.NewAVP(code, flags, vendor, data)
}

func (*Dict) Load(dictionary string) error {
file, err := os.Open(dictionary)
if err != nil {
return err
}
defer file.Close()

dict.Default.Load(file)
if err != nil {
return err
}
return nil
}

func init() {
modules.Register("k6/x/diameter", &Diameter{})
modules.Register("k6/x/diameter/avp", &AVP{})
modules.Register("k6/x/diameter/dict", &Dict{})
}
9 changes: 9 additions & 0 deletions dict/dictionary.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<diameter>
<application id="4" type="auth" name="Extra">
<vendor name="Matrixxsoftware" id="35838"/>
<avp name="Event-Name-Code" code="13001" vendor-id="35838">
<data type="UTF8String"/>
</avp>
</application>
</diameter>
23 changes: 0 additions & 23 deletions example/diam.js

This file was deleted.

Loading

0 comments on commit 3b540b8

Please sign in to comment.