Esempio n. 1
0
func (table *BpfTable) keyToBytes(keyStr string) ([]byte, error) {
	mod := table.module.p
	key_size := C.bpf_table_key_size_id(mod, table.id)
	key := make([]byte, key_size)
	keyP := unsafe.Pointer(&key[0])
	keyCS := C.CString(keyStr)
	defer C.free(unsafe.Pointer(keyCS))
	r := C.bpf_table_key_sscanf(mod, table.id, keyCS, keyP)
	if r != 0 {
		//Warn.Printf("key type is \"%s\"\n", C.GoString(C.bpf_table_key_desc_id(mod, table.id)))
		return nil, fmt.Errorf("error scanning key (%v) from string", keyStr)
	}
	return key, nil
}
Esempio n. 2
0
func (table *BpfTable) keyToBytes(keyX interface{}) ([]byte, error) {
	mod := table.module.p
	key_size := C.bpf_table_key_size_id(mod, table.id)
	keyStr, ok := keyX.(string)
	if !ok {
		return nil, fmt.Errorf("table key must be a string")
	}
	key := make([]byte, key_size)
	keyP := unsafe.Pointer(&key[0])
	keyCS := C.CString(keyStr)
	defer C.free(unsafe.Pointer(keyCS))
	r := C.bpf_table_key_sscanf(mod, table.id, keyCS, keyP)
	if r != 0 {
		return nil, fmt.Errorf("error scanning key from string")
	}
	return key, nil
}