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" ) 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 uint8 = 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 ¶
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 map[string]Record (TypeStruct) response, _ := records[0].String() fmt.Printf("response = %s", response)
Output:
Example (MapResponse) ¶
// 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 map // with at least "total" and "current" keys avpMap, _ := records[0].Map() total, _ := avpMap["total"].Int() current, _ := avpMap["current"].Int() fmt.Printf("total = %d\ncurrent = %d\n", total, current, )
Output:
Example (MapResponseScan) ¶
// 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 map // with at least "total" and "current" keys var avpMap map[string]Record if err = records[0].Scan(&avpMap); err != nil { panic(err) } total, _ := avpMap["total"].Int() current, _ := avpMap["current"].Int() fmt.Printf("total = %d\ncurrent = %d\n", total, current, )
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 map[string]Record (TypeStruct) var response string if err = records[0].Scan(&response); err != nil { panic(err) } fmt.Printf("response = %s", response)
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 ¶
type Record struct { Type uint8 Value interface{} // contains filtered or unexported fields }
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 ¶
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 nil, 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.