-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcgopy.py
42 lines (34 loc) · 1.28 KB
/
cgopy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
__all__ = [ 'libgobgp', '_PATTRS_CAP', '_AF_NAME', 'Buf', 'Path', 'unpack_buf', 'protobuf_obj_attrs', ]
from ctypes import *
from struct import *
import os
# load C shared library
libgobgp = cdll.LoadLibrary(os.environ["GOPATH"] +"/src/github.com/osrg/gobgp/gobgp/lib/libgobgp.so")
# constants
_PATTRS_CAP = 32
_AF_NAME = dict([(4, "ipv4-unicast"), (6, "ipv6-unicast"), ])
# structs
class Buf(Structure):
_fields_ = [("value", POINTER(c_char)),
("len", c_int),
]
class Path(Structure):
_fields_ = [("nlri", Buf),
("path_attributes", POINTER(POINTER(Buf) * _PATTRS_CAP)),
("path_attributes_len", c_int),
("path_attributes_cap", c_int),
]
# argument and return type definitions
libgobgp.serialize_path.restype = POINTER(Path)
libgobgp.serialize_path.argtypes = (c_int, c_char_p)
libgobgp.decode_path.restype = c_char_p
libgobgp.decode_path.argtypes = (POINTER(Path), )
def unpack_buf(buf):
# unpack Buf obj
return unpack(str(buf.len)+"s", buf.value[:buf.len])[0]
def protobuf_obj_attrs(o):
# return a list of protobuf object attribute names
slice_ind = -1 * len('_FIELD_NUMBER')
return [ attr[:slice_ind].lower() for attr in dir(o) if attr.endswith('_FIELD_NUMBER') ]
if __name__ == '__main__':
pass