diam

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2014 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Diameter Base Protocol for the Go programming language. See RFC 6733.

Index

Constants

This section is empty.

Variables

View Source
var DefaultServeMux = NewServeMux()

DefaultServeMux is the default ServeMux used by Serve.

View Source
var ErrHandlerTimeout = errors.New("diam: Handler timeout")

ErrHandlerTimeout is returned on MessageWriter Write calls in handlers which have timed out.

View Source
var ErrInvalidAvpHdr = errors.New("Invalid AVP header size: probably a bad dict")
View Source
var ErrNoParentMessage = errors.New("This AVP has no parent message")

Functions

func ErrorReports

func ErrorReports() chan ErrorReport

ErrorReport returns the ErrorReport channel of the DefaultServeMux.

func Handle

func Handle(cmd string, handler Handler)

Handle registers the handler for the given pattern in the DefaultServeMux.

func HandleFunc

func HandleFunc(cmd string, handler func(Conn, *Message))

HandleFunc registers the handler function for the given command in the DefaultServeMux.

func ListenAndServe

func ListenAndServe(addr string, handler Handler, dict *dict.Parser) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections.

If handler is nil, diam.DefaultServeMux is used.

If dict is nil, dict.Default is used.

func ListenAndServeTLS

func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler, dict *dict.Parser) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects SSL connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.

One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.

func Serve

func Serve(l net.Listener, handler Handler) error

Serve accepts incoming diameter connections on the listener l, creating a new service goroutine for each. The service goroutines read messages and then call handler to reply to them. Handler is typically nil, in which case the DefaultServeMux is used.

Types

type AVP

type AVP struct {
	Code     uint32
	Flags    uint8
	Length   uint32
	VendorId uint32
	// contains filtered or unexported fields
}

AVP represents an AVP header and data.

func ReadAVP

func ReadAVP(m *Message, r io.Reader) (uint32, *AVP, error)

ReadAVP reads an AVP from the connection and returns the total number of padding bytes read and the parsed AVP, or an error.

Total AVP bytes is avp.Length + padding.

For details on AVP length and padding see http://tools.ietf.org/html/rfc6733#section-4

A pointer to the parent Message is required, otherwise it panics.

func (*AVP) Body

func (avp *AVP) Body() Codec

Body returns the internal AVP body.

func (*AVP) Data

func (avp *AVP) Data() avpdata.Generic

Data returns internal AVP body data. It's a short for AVP.Body().Data().

func (*AVP) Pack

func (avp *AVP) Pack() []byte

Pack returns an AVP in binary form so it can be attached to a Message before sent to a connection.

func (*AVP) Set

func (avp *AVP) Set(data interface{}) error

Set updates the internal AVP data (the body) with a new value.

func (*AVP) String

func (avp *AVP) String() string

String returns the AVP in human readable format.

The AVP name is "guessed" by scanning the list of available AVPs in the dictionary that was used to build this AVP. It might return the wrong AVP name if the same code is used by different dictionaries in different applications, with a different name - yet, very unlikely.

type CloseNotifier

type CloseNotifier interface {
	// CloseNotify returns a channel that receives a single value
	// when the client connection has gone away.
	CloseNotify() <-chan bool
}

The CloseNotifier interface is implemented by Conns which allow detecting when the underlying connection has gone away.

This mechanism can be used to detect if the client has disconnected.

type Codec

type Codec interface {
	// Decode binary data from the network and store as internal AVP data.
	Put([]byte)

	// Encode internal AVP data to binary and return it.
	Bytes() []byte

	// Returns internal AVP data.
	Data() avpdata.Generic

	// Length of the internal data without padding.
	// Might be diffent from len(Bytes()).
	Length() uint32

	// String returns the internal AVP data in human readable format.
	String() string
}

Codec provides an interface for converting data from network bytes to Go native and vice-versa.

This interface is used to encode and decode AVP data such as OctetString, and is implemented by all data types in the avpdata sub package.

type Conn

type Conn interface {
	Write(*Message) (int, error) // Writes a msg to the connection
	Close()                      // Close the connection
	LocalAddr() net.Addr         // Local IP
	RemoteAddr() net.Addr        // Remote IP
	TLS() *tls.ConnectionState   // or nil when not using TLS
}

A Conn interface is used by a handler to send diameter messages.

func Dial

func Dial(addr string, handler Handler, dict *dict.Parser) (Conn, error)

Dial connects to the peer pointed to by addr and returns the Conn that can be used to send diameter messages. Incoming messages are handled by the handler, which is tipically nil and DefaultServeMux is used. If dict is nil, dict.Default is used.

func DialTLS

func DialTLS(addr, certFile, keyFile string, handler Handler, dict *dict.Parser) (Conn, error)

DialTLS is the same as Dial, but for TLS.

type ErrorReport

type ErrorReport struct {
	Message *Message
	Error   error
}

ErrorReport is sent out of the server in case it fails to read messages because of a bad dictionary or network errors.

type Grouped

type Grouped struct {
	Message *Message // Parent Message of this group.
	AVP     []*AVP   // Group AVPs
	Buffer  []byte   // len(Buffer) might be bigger than length below due to padding.
	// contains filtered or unexported fields
}

Grouped Diameter Type

func NewGroup

func NewGroup(m *Message) *Grouped

NewGroup allocates a new Grouped AVP. Same as &Grouped{Message: m}

func (*Grouped) Add

func (gr *Grouped) Add(avp *AVP)

Add adds an AVP to the group.

func (*Grouped) Bytes

func (gr *Grouped) Bytes() []byte

Bytes implement the Codec interface. Bytes are always returned from internal Buffer cache.

func (*Grouped) Data

func (gr *Grouped) Data() avpdata.Generic

Data implements the Data interface.

func (*Grouped) Length

func (gr *Grouped) Length() uint32

Length implements the Codec interface.

func (*Grouped) NewAVP

func (gr *Grouped) NewAVP(code interface{}, flags uint8, vendor uint32, data avpdata.Generic) (*AVP, error)

NewAVP allocates an AVP and appends to the group. @code can be either the AVP code (int, uint32) or name (string).

func (*Grouped) Put

func (gr *Grouped) Put(d []byte)

Put implements the Codec interface. Does nothing for Grouped types.

func (*Grouped) String

func (gr *Grouped) String() string

String returns a human readable version of the AVP.

type Handler

type Handler interface {
	ServeDiam(Conn, *Message)
	ErrorReports() chan ErrorReport
}

Objects implementing the Handler interface can be registered to serve particular messages like CER, DWR.

ServeDiam should write messages to the Conn and then return. Returning signals that the request is finished and that the server can move on to the next request on the connection.

func TimeoutHandler

func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler

TimeoutHandler returns a Handler that runs h with the given time limit.

The new Handler calls h.ServeDiam to handle each request, but if a call runs for longer than its time limit, the connection is closed. After such a timeout its MessageWriter will return ErrHandlerTimeout.

type HandlerFunc

type HandlerFunc func(Conn, *Message)

The HandlerFunc type is an adapter to allow the use of ordinary functions as diameter handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) ErrorReports

func (f HandlerFunc) ErrorReports() chan ErrorReport

ErrorReports calls f.ErrorReports()

func (HandlerFunc) ServeDiam

func (f HandlerFunc) ServeDiam(c Conn, m *Message)

ServeDiam calls f(c, m).

type Header struct {
	Version          uint8
	RawMessageLength [3]uint8
	CommandFlags     uint8
	RawCommandCode   [3]uint8
	ApplicationId    uint32
	HopByHopId       uint32
	EndToEndId       uint32
}

func (*Header) CommandCode

func (hdr *Header) CommandCode() uint32

CommandCode returns RawCommandCode as int.

func (*Header) MessageLength

func (hdr *Header) MessageLength() uint32

MessageLength helper function returns RawMessageLength as int.

func (*Header) SetMessageLength

func (hdr *Header) SetMessageLength(length uint32)

UpdateLength updates RawMessageLength from an int.

type Message

type Message struct {
	Header *Header
	AVP    []*AVP
	Dict   *dict.Parser // Dictionary associated with this Message
}

Message represents a diameter message.

func NewMessage

func NewMessage(cmd uint32, flags uint8, appid, hopbyhop, endtoend uint32, d *dict.Parser) *Message

NewMessage allocates a new Message object. Used for building messages that will be sent to a connection later.

Arguments hopbyhop and endtoend are optional. If set to 0, random values are used.

Dictionary d is used for encoding the AVPs added to the message. If set to nil, static dict.Default is used.

TODO: Support command short names like CER, CEA.

func ReadMessage

func ReadMessage(r io.Reader, d *dict.Parser) (*Message, error)

ReadMessage reads a diameter message from the connection.

Dictionary d is used to parse message's AVPs. If set to nil, the static dict.Default is used.

Using the wrong dictionary might result in errors.

func (*Message) Add

func (m *Message) Add(avp *AVP)

Add adds an AVP to the Message and set its parent Message to the current.

func (*Message) Answer

func (m *Message) Answer(resultCode uint32) *Message

Answer creates an answer for the current Message with an embedded Result-Code AVP.

func (*Message) FindAVP

func (m *Message) FindAVP(code interface{}) (*AVP, error)

FindAVP looks for an AVP in the message body, and return it. code can be either the AVP code (int, uint32) or name (string).

Example:

avp, err := m.FindAVP(264)
avp, err := m.FindAVP("Origin-Host")

func (*Message) NewAVP

func (m *Message) NewAVP(code interface{}, flags uint8, vendor uint32, data avpdata.Generic) (*AVP, error)

NewAVP allocates and returns a new AVP. @code can be either the AVP code (int, uint32) or name (string).

func (*Message) Pack

func (m *Message) Pack() []byte

Pack returns the Message in binary form to be sent to a connection.

func (*Message) PrettyPrint

func (m *Message) PrettyPrint()

PrettyPrint prints the message in a human readable format.

func (*Message) String

func (m *Message) String() string

String returns a human readable version of the Message header.

type ServeMux

type ServeMux struct {
	// contains filtered or unexported fields
}

ServeMux is a diameter message multiplexer. It matches the command from the incoming message against a list of registered commands and calls the handler.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux.

func (*ServeMux) ErrorReports

func (mux *ServeMux) ErrorReports() chan ErrorReport

ErrorReports returns the ErrorReport channel of the handler.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(cmd string, handler Handler)

Handle registers the handler for the given code. If a handler already exists for code, Handle panics.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(cmd string, handler func(Conn, *Message))

HandleFunc registers the handler function for the given command. Special cmd "ALL" may be used as a catch all.

func (*ServeMux) ServeDiam

func (mux *ServeMux) ServeDiam(c Conn, m *Message)

ServeDiam dispatches the request to the handler whose code match the incoming message, or close the connection if no handler is found.

type Server

type Server struct {
	Addr         string        // TCP address to listen on, ":3868" if empty
	Handler      Handler       // handler to invoke, diam.DefaultServeMux if nil
	Dict         *dict.Parser  // diameter dictionaries for this server
	ReadTimeout  time.Duration // maximum duration before timing out read of the request
	WriteTimeout time.Duration // maximum duration before timing out write of the response
	TLSConfig    *tls.Config   // optional TLS config, used by ListenAndServeTLS
}

A Server defines parameters for running a diameter server.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":3868" is used.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming TLS connections.

Filenames containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.

If srv.Addr is blank, ":3868" is used.

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them.

Directories

Path Synopsis
Diameter data types.
Diameter data types.
Diameter dictionary parser.
Diameter dictionary parser.
Collection of internal utilities.
Collection of internal utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL