Documentation ¶
Overview ¶
Package binrpc implements the BINRPC protocol of Kamailio for invoking RPC functions. This package lets you talk to a Kamailio instance from Go code.
The ctl module must be loaded: https://www.kamailio.org/docs/modules/stable/modules/ctl.html
The BINRPC protocol is described in "src/modules/ctl/binrpc.h": https://github.com/kamailio/kamailio/blob/master/src/modules/ctl/binrpc.h
Limits ¶
The current implementation handles only int, string, and structs containing int or string values. Other types will return an error.
Usage ¶
High level functions:
- WritePacket to call an RPC function (a string like "tm.stats")
- ReadPacket to read the response
package main import ( "fmt" "net" binrpc "github.com/florentchauveau/go-kamailio-binrpc/v3" ) func main() { conn, err := net.Dial("tcp", "localhost:2049") if err != nil { panic(err) } cookie, err := binrpc.WritePacket(conn, "tm.stats") if err != nil { panic(err) } records, err := binrpc.ReadPacket(conn, cookie) if err != nil { panic(err) } fmt.Printf("records = %v", records) }
Index ¶
Examples ¶
Constants ¶
const ( BinRPCMagic uint8 = 0xA BinRPCVersion uint8 = 0x1 TypeInt uint8 = 0x0 TypeString uint8 = 0x1 TypeDouble uint8 = 0x2 TypeStruct uint8 = 0x3 TypeArray uint8 = 0x4 TypeAVP uint8 = 0x5 TypeBytes uint8 = 0x6 // the totalLength cannot be larger than 4 bytes // because we have 2 bits to write its "length-1" // so "4" is the largest length that we can write MaxSizeOfLength = 4 )
BinRPCMagic is a magic value at the start of every BINRPC packet. BinRPCVersion is the version implemented (currently 1).
Variables ¶
This section is empty.
Functions ¶
func WritePacket ¶
func WritePacket[T ValidTypes](w io.Writer, values ...T) (uint32, error)
WritePacket creates a BINRPC packet (header and payload) containing values v, and writes it to w. It returns the cookie generated, or an error if one occurred.
Example ¶
// establish connection to Kamailio server conn, err := net.Dial("tcp", "localhost:2049") if err != nil { panic(err) } cookie, err := WritePacket(conn, "core.echo", "bonjours") if err != nil { panic(err) } records, err := ReadPacket(conn, cookie) if err != nil { panic(err) } // based on records[0].Type, records[0].Value is either: // an int (TypeInt) // a string (TypeString) // a []StructItem (TypeStruct) response, _ := records[0].String() fmt.Printf("response = %s", response)
Output:
Example (Scan) ¶
// establish connection to Kamailio server conn, err := net.Dial("tcp", "localhost:2049") if err != nil { panic(err) } cookie, err := WritePacket(conn, "core.echo", "bonjours") if err != nil { panic(err) } records, err := ReadPacket(conn, cookie) if err != nil { panic(err) } // based on records[0].Type, records[0].Value is either: // an int (TypeInt) // a string (TypeString) // a []StructItem (TypeStruct) var response string if err = records[0].Scan(&response); err != nil { panic(err) } fmt.Printf("response = %s", response)
Output:
Example (StructResponse) ¶
// establish connection to Kamailio server conn, err := net.Dial("tcp", "localhost:2049") if err != nil { panic(err) } // WritePacket returns the cookie generated cookie, err := WritePacket(conn, "tm.stats") if err != nil { panic(err) } // the cookie is passed again for verification // we receive records in response records, err := ReadPacket(conn, cookie) if err != nil { panic(err) } // "tm.stats" returns one record that is a struct // and all items are int values items, _ := records[0].StructItems() for _, item := range items { value, _ := item.Value.Int() fmt.Printf("%s = %d\n", item.Key, value, ) }
Output:
Example (StructResponseScan) ¶
// establish connection to Kamailio server conn, err := net.Dial("tcp", "localhost:2049") if err != nil { panic(err) } // WritePacket returns the cookie generated cookie, err := WritePacket(conn, "tm.stats") if err != nil { panic(err) } // the cookie is passed again for verification // we receive records in response records, err := ReadPacket(conn, cookie) if err != nil { panic(err) } // "tm.stats" returns one record that is a struct // and all items are int values var items []StructItem if err = records[0].Scan(&items); err != nil { panic(err) } for _, item := range items { value, _ := item.Value.Int() fmt.Printf("%s = %d\n", item.Key, value, ) }
Output:
Types ¶
type Header ¶
Header is a struct containing values needed for parsing the payload and replying. It is not a binary representation of the actual header.
type Record ¶
Record represents a BINRPC type+size, and Go value. It is not a binary representation of a record. Type is the BINRPC type.
func CreateRecord ¶
func CreateRecord[T ValidTypes](v T) (*Record, error)
CreateRecord is a low level function that creates a Record from value v and fills the Type property automatically.
func ReadPacket ¶
ReadPacket reads from r and returns records, or an error if one occurred. If expectedCookie is not zero, it verifies the cookie.
func ReadRecord ¶
ReadRecord is a low level function that reads from r and returns a Record or an error if one occurred.
func (Record) Double ¶ added in v3.1.0
Double returns the double value as a float64, or an error if the type is not a double
func (*Record) Scan ¶
Scan copies the value in the Record into the values pointed at by dest. Valid dest type are *int, *string, and *[]StructItem
func (*Record) StructItems ¶
func (record *Record) StructItems() ([]StructItem, error)
StructItems returns items for a struct value, or an error if not a struct.
type StructItem ¶
StructItem represents an item in a BINRPC struct. Because BINRPC structs may contain the same key multiple times, structs are handled with arrays of StructItem.
type ValidTypes ¶ added in v3.2.0
ValidTypes is an interface of types that can be used in a Record.