fiorix / go-diameter

Diameter stack and Base Protocol (RFC 6733) for the Go programming language
Other
252 stars 143 forks source link

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

Open Yoone opened 3 years ago

Yoone commented 3 years ago

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

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] = []}}}