Python socket.AF_NETLINK() Examples

The following are code examples for showing how to use socket.AF_NETLINK(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pytest-recording   Author: kiwicom   File: test_blocking_network.py    MIT License 6 votes vote down vote up
def test_other_socket(testdir):
    # When not AF_UNIX, AF_INET or AF_INET6 socket is used
    testdir.makepyfile(
        """
from socket import socket, AF_NETLINK, SOCK_RAW
import pytest

def call():
    s = socket(AF_NETLINK, SOCK_RAW)
    try:
        return s.connect((0, 0))
    finally:
        s.close()

@pytest.mark.block_network(allowed_hosts=["./allowed_socket", "127.0.0.1", "0"])
def test_blocked():
    with pytest.raises(RuntimeError, match=r"^Network is disabled$"):
        call()
    """
    )
    # Then socket.socket.connect call is blocked, even if resource name is in the allowed list
    result = testdir.runpytest()
    result.assert_outcomes(passed=1) 
Example 2
Project: neutron-qos   Author: eayunstack   File: htb.py    Apache License 2.0 5 votes vote down vote up
def __enter__(self):
        super(RTNetLink, self).__enter__()
        self._sock = socket.socket(
            socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)
        self._sock.bind((self._pid, 0))
        self._seq = int(time.time())
        self._sfd = os.fdopen(self._sock.fileno(), 'w+b')
        return self 
Example 3
Project: libnl   Author: Robpol86   File: nl.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def nl_send_iovec(sk, msg, iov, _):
    """Transmit Netlink message.

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/nl.c#L342

    This function is identical to nl_send().

    This function triggers the `NL_CB_MSG_OUT` callback.

    Positional arguments:
    sk -- Netlink socket (nl_sock class instance).
    msg -- Netlink message (nl_msg class instance).
    iov -- data payload to be sent (bytearray).

    Returns:
    Number of bytes sent on success or a negative error code.
    """
    hdr = msghdr(msg_name=sk.s_peer, msg_iov=iov)

    # Overwrite destination if specified in the message itself, defaults to the peer address of the socket.
    dst = nlmsg_get_dst(msg)
    if dst.nl_family == socket.AF_NETLINK:
        hdr.msg_name = dst

    # Add credentials if present.
    creds = nlmsg_get_creds(msg)
    if creds:
        raise NotImplementedError  # TODO https://github.com/Robpol86/libnl/issues/2

    return nl_sendmsg(sk, msg, hdr) 
Example 4
Project: darkc0de-old-stuff   Author: tuwid   File: socketcall_call.py    GNU General Public License v3.0 5 votes vote down vote up
def createText(self):
        syscall = self.function.name
        name = self.name
        argtype = self.type
        value = self.value
        if syscall == "socket":
            if name == "family":
                return SOCKET_FAMILY.get(value, value)
            if name == "type":
                return SOCKET_TYPE.get(value, value)
            if name == "protocol":
                return SOCKET_PROTOCOL.get(value, value)
        if syscall == "setsockopt":
            if name == "level":
                return SETSOCKOPT_LEVEL.get(value, value)
            if name == "optname":
                return SETSOCKOPT_OPTNAME.get(value, value)
            if name == "optval":
                optlen = self.function["optlen"].value
                if optlen == 4:
                    text = self.function.process.readStruct(self.value, c_int)
                    return self.formatPointer("<%s>" % text, self.value)
        if argtype == "struct sockaddr*":
            address = self.value
            addr = self.function.process.readStruct(address, sockaddr)
            family = addr.family
            if family == AF_INET:
                return self.readStruct(self.value, sockaddr_in)
            elif family == AF_FILE:
                return self.readStruct(self.value, sockaddr_un)
            elif family == AF_NETLINK:
                return self.readStruct(self.value, sockaddr_nl)
            else:
                family = SOCKET_FAMILY.get(family, family)
                return self.formatPointer("<sockaddr family=%s>" % family, address)
        return None 
Example 5
Project: libnl   Author: Robpol86   File: nl.py    GNU Lesser General Public License v2.1 4 votes vote down vote up
def nl_connect(sk, protocol):
    """Create file descriptor and bind socket.

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/nl.c#L96

    Creates a new Netlink socket using `socket.socket()` and binds the socket to the protocol and local port specified
    in the `sk` socket object (if any). Fails if the socket is already connected.

    Positional arguments:
    sk -- Netlink socket (nl_sock class instance).
    protocol -- Netlink protocol to use (integer).

    Returns:
    0 on success or a negative error code.
    """
    flags = getattr(socket, 'SOCK_CLOEXEC', 0)
    if sk.s_fd != -1:
        return -NLE_BAD_SOCK
    try:
        sk.socket_instance = socket.socket(getattr(socket, 'AF_NETLINK', -1), socket.SOCK_RAW | flags, protocol)
    except OSError as exc:
        return -nl_syserr2nlerr(exc.errno)

    if not sk.s_flags & NL_SOCK_BUFSIZE_SET:
        err = nl_socket_set_buffer_size(sk, 0, 0)
        if err < 0:
            sk.socket_instance.close()
            return err

    try:
        sk.socket_instance.bind((sk.s_local.nl_pid, sk.s_local.nl_groups))
    except OSError as exc:
        sk.socket_instance.close()
        return -nl_syserr2nlerr(exc.errno)
    sk.s_local.nl_pid = sk.socket_instance.getsockname()[0]

    if sk.s_local.nl_family != socket.AF_NETLINK:
        sk.socket_instance.close()
        return -NLE_AF_NOSUPPORT

    sk.s_proto = protocol
    return 0