go-diameter icon indicating copy to clipboard operation
go-diameter copied to clipboard

Added option to loosen message parsing to allow AVPs with empty/invalid payloads while defaulting to strict parsing

Open Yoone opened this issue 5 years ago • 0 comments

I am working with equipments that can occasionally send slightly invalid messages, containing empty AVPs (the length is valid, but the "data" is empty).

This change aims to address this issue by adding an optional parameter in dictionaries to set whether the parsing of incoming messages should be strict or not (it is strict by default).

TODO

  • [ ] There is a missing change needed for this PR to work with #146: the latter introduces a new function called dict.NewParserFromLibrary and it needs to be modified to set p.Strict = true just like dict.NewParser does.

Example usage

dictionary, err := dict.NewParserFromLibrary(
    library.Base,
    library.CreditControl,
    library.TgppRoRf,
)
if err != nil {
    panic(err)
}
dictionary.Strict = false // Does not return an error when one or more incoming AVPs are invalid or empty

// declare stateMachine

if err := diam.ListenAndServeNetwork("tcp", "127.0.0.1", stateMachine, dictionary); err != nil {
    panic(err)
}

How to get the parsing error?

When Parser.Strict is set to false, the parsing error is accessible like so:

func myHandler(conn diam.Conn, msg *diam.Message) {
    if msg.DecodeErr != nil {
        // some custom logic, like logging the error
    }
}

Example error (shows the AVP stack, down the actual error, and it contains all the invalid AVPs, not just the first one):

Failed to decode one or more AVPs: {Service-Information(873): Grouped{SMS-Information(2000): Grouped{Recipient-SCCP-Address(2010): Not enough data to make an Address from byte[0] = []}}}

Yoone avatar Nov 20 '20 00:11 Yoone