-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpl.py
115 lines (90 loc) · 2.37 KB
/
vpl.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
from ctypes import *
import os
from fcntl import ioctl
import sys, getopt
import time
ETH_ALEN = 6
path = "/dev/flow_valve"
IOCTL_SYNC = 2148038145
IOCTL_SETVALVE = 1074296322
VALVE_STARTPROTECT = 1074296323
VALVE_RENEWPROTECT = 1074296324
VALVE_ENDPROTECT = 30213
class timespec(Structure):
_fields_ = [
('tv_sec', c_longlong),
('tv_ns', c_long)
]
class flow_tsf(Structure):
_fields_ = [
('ts_beacon', c_uint64),
('ts_kernel', c_int64),
('ts_system', timespec)
]
def send(self):
return create_string_buffer(self)[:]
def receive(self, bytes):
fit = min(len(bytes), sizeof(self))
memmove(addressof(self), bytes, fit)
class struct_plan(Structure):
_fields_ = [
('q_minus_delta_b', c_longlong),
('q_plus_delta_b', c_longlong)
]
def send(self):
return create_string_buffer(self)[:]
def receive(self, bytes):
fit = min(len(bytes), sizeof(self))
memmove(addressof(self), bytes, fit)
def time_sync():
tsf = flow_tsf()
b = create_string_buffer(sizeof(tsf))
fd = os.open(path, os.O_RDWR)
ioctl(fd, IOCTL_SYNC, b)
os.close(fd)
tsf.receive(b)
return tsf.ts_beacon * 1e-6, tsf.ts_kernel * 1e-9
def valve_control(control):
if (type(control)!= list or len(control) != 4):
print("control type wrong")
return 0
control = (c_int * 4)(*control)
fd = os.open(path, os.O_RDWR)
ret = ioctl(fd, IOCTL_SETVALVE, control)
os.close(fd)
if ret == 0:
return 0
else:
return -1
def valve_startprotect(t):
if (type(t) != int):
raise RuntimeError('parameter must be integer time')
c_t = c_int64(t)
fd = os.open(path, os.O_RDWR)
ret = ioctl(fd, VALVE_STARTPROTECT, c_t)
os.close(fd)
if ret != 0:
raise RuntimeError('failed to inform')
def valve_renewprotect(t):
if (type(t) != int):
print("parameter is not time")
return 0
c_t = c_int64(t)
fd = os.open(path, os.O_RDWR)
ret = ioctl(fd, VALVE_RENEWPROTECT, c_t)
os.close(fd)
if ret == 0:
return 0
else:
return -1
def valve_endprotect():
fd = os.open(path, os.O_RDWR)
ret = ioctl(fd, VALVE_ENDPROTECT)
os.close(fd)
if ret == 0:
return 0
else:
return -1
if __name__ == "__main__":
valve_endprotect()