# README
go-kamailio-binrpc
Go implementation of Kamailio BINRPC protocol for invoking RPC functions.
This library works with any Kamailio version.
go-kamailio-binrpc has been tested with Go 1.11, but should work with previous versions.
Usage
import binrpc "github.com/florentchauveau/go-kamailio-binrpc/v2"
Full Example
package main
import (
"fmt"
"net"
binrpc "github.com/florentchauveau/go-kamailio-binrpc/v2"
)
func main() {
// establish connection to Kamailio server
conn, err := net.Dial("tcp", "localhost:2049")
if err != nil {
panic(err)
}
// WritePacket returns the cookie generated
cookie, err := binrpc.WritePacket(conn, "tm.stats")
// for commands that require args, add them as function args
// like this: binrpc.WritePacket(conn, "stats.fetch", "all")
if err != nil {
panic(err)
}
// the cookie is passed again for verification
// we receive records in response
records, err := binrpc.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,
)
}
}
Kamailio Config
The ctl
module must be loaded:
loadmodule "ctl.so"
If you are using kamcmd
(and you most probably are), the module is already loaded.
In order to connect remotely, you must listen on TCP or UDP (defaults to local unix socket):
modparam("ctl", "binrpc", "tcp:2049")
WARNING: this will open your Kamailio to the world. Make sure you have a firewall in place, or listen on an internal interface.
Limits
For now, only int, string and structs are implemented. Other types will return an error.
Contributing
Contributions are welcome.
License
This library is distributed under the MIT license.
# Functions
BigCookie a workaround to get a big value Return a big value.
CreateRecord is a low level function that creates a Record from value v and fills the Type property automatically.
ReadHeader is a low level function that reads from r and returns a Header.
ReadPacket reads from r and returns records, or an error if one occurred.
ReadRecord is a low level function that reads from r and returns a Record or an error if one occurred.
WritePacket creates a BINRPC packet (header and payload) containing values v, and writes it to w.
# Constants
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
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.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
BinRPCMagic is a magic value at the start of every BINRPC packet.
# Structs
Header is a struct containing values needed for parsing the payload and replying.
Record represents a BINRPC type+size, and Go value.
StructItem represents an item in a BINRPC struct.