mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package libsignal
|
|
|
|
/*
|
|
#include <signal_protocol.h>
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
// Address identifies a Signal Protocol session peer: a logical recipient
|
|
// name (for OMEMO, a bare JID) plus a numeric device ID.
|
|
type Address struct {
|
|
Name string
|
|
DeviceID uint32
|
|
}
|
|
|
|
// withCAddress converts addr to a C signal_protocol_address and invokes fn
|
|
// with a pointer to it. The pointer - and the memory its name field borrows
|
|
// from addr.Name - is only valid for the duration of fn; it must not be
|
|
// retained past fn's return.
|
|
func withCAddress(addr Address, fn func(*C.signal_protocol_address)) {
|
|
nameBytes := []byte(addr.Name)
|
|
var namePtr *C.char
|
|
if len(nameBytes) > 0 {
|
|
namePtr = (*C.char)(unsafe.Pointer(&nameBytes[0]))
|
|
}
|
|
cAddr := C.signal_protocol_address{
|
|
name: namePtr,
|
|
name_len: C.size_t(len(nameBytes)),
|
|
device_id: C.int32_t(addr.DeviceID),
|
|
}
|
|
fn(&cAddr)
|
|
runtime.KeepAlive(nameBytes)
|
|
}
|
|
|
|
// addressFromC copies a C signal_protocol_address into a Go Address.
|
|
func addressFromC(addr *C.signal_protocol_address) Address {
|
|
return Address{
|
|
Name: C.GoStringN(addr.name, C.int(addr.name_len)),
|
|
DeviceID: uint32(addr.device_id),
|
|
}
|
|
}
|