Skip to content

Go implementation of the Kamailio BINRPC & JSONRPC protocol for invoking RPC functions

License

Notifications You must be signed in to change notification settings

voiplens/go-kamailio

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-kamailio

Go Report Card CI GoDoc License

Zero dependencies Go implementation of Kamailio BINRPC protocol for invoking RPC functions.

This library works with any Kamailio version.

go.voiplens.io/kamailio requires at least Go 1.21.

Usage

import binrpc "go.voiplens.io/kamailio/binrpc"

Full Example

package main

import (
	"fmt"
	"net"

	binrpc "go.voiplens.io/kamailio/binrpc"
)

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 double string and structs are implemented. Other types will return an error.

Contributing

Contributions are welcome.

License

This library is distributed under the MIT license.

About

Go implementation of the Kamailio BINRPC & JSONRPC protocol for invoking RPC functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%