File: netl.py

package info (click to toggle)
pyroute2 0.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,704 kB
  • sloc: python: 50,245; makefile: 280; javascript: 183; ansic: 81; sh: 44; awk: 17
file content (48 lines) | stat: -rwxr-xr-x 1,180 bytes parent folder | download
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
#!/usr/bin/env python3

import traceback
from pyroute2.netlink import NLM_F_REQUEST
from pyroute2.netlink import genlmsg
from pyroute2.netlink.generic import GenericNetlinkSocket


RLINK_CMD_UNSPEC = 0
RLINK_CMD_REQ = 1


class rcmd(genlmsg):
    '''
    Message class that will be used to communicate
    with the kernel module
    '''

    nla_map = (
        ('RLINK_ATTR_UNSPEC', 'none'),
        ('RLINK_ATTR_DATA', 'asciiz'),
        ('RLINK_ATTR_LEN', 'uint32'),
    )


class Rlink(GenericNetlinkSocket):
    def send_data(self, data):
        msg = rcmd()
        msg['cmd'] = RLINK_CMD_REQ
        msg['version'] = 1
        msg['attrs'] = [('RLINK_ATTR_DATA', data)]
        ret = self.nlm_request(msg, self.prid, msg_flags=NLM_F_REQUEST)[0]
        return ret.get_attr('RLINK_ATTR_LEN')


if __name__ == '__main__':
    try:
        # create protocol instance
        rlink = Rlink()
        rlink.bind('EXMPL_GENL', rcmd)
        # request a method
        print(rlink.send_data('x' * 65000))
    except:
        # if there was an error, log it to the console
        traceback.print_exc()
    finally:
        # finally -- release the instance
        rlink.close()