Exemple #1
0
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)
Exemple #2
0
Fichier : nl.py Projet : 0x90/libnl
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)
Exemple #3
0
def test_error_nle_bad_sock():
    """Test for NLE_BAD_SOCK."""
    sk = nl_socket_alloc()
    msg = nlmsg_alloc_simple(0, 0)
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    nl_complete_msg(sk, msg)

    message = 'Hello World!\n'
    iov = bytes(message.encode('ascii'))
    hdr = msghdr(msg_iov=iov)

    assert -3 == nl_sendmsg(sk, msg, hdr)
Exemple #4
0
def test_default(tcp_server):
    r"""C code to test against.

    // gcc a.c $(pkg-config --cflags --libs libnl-genl-3.0)
    // (nc -l 2000 &); sleep 0.1; ./a.out
    #include <netlink/msg.h>
    struct nl_sock {
        struct sockaddr_nl s_local; struct sockaddr_nl s_peer; int s_fd; int s_proto; unsigned int s_seq_next;
        unsigned int s_seq_expect; int s_flags; struct nl_cb *s_cb; size_t s_bufsize;
    };
    int main() {
        struct sockaddr_in sin = { .sin_port = htons(2000), .sin_family = AF_INET, };
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        connect(fd, (struct sockaddr *) &sin, sizeof(sin));

        struct nl_sock *sk = nl_socket_alloc();
        struct nl_msg *msg = nlmsg_alloc_simple(0, 0);
        nl_connect(sk, NETLINK_ROUTE);
        sk->s_fd = fd;
        sk->s_local.nl_pid = 0;
        nl_complete_msg(sk, msg);

        char message[] = "Hello World!\n";
        struct iovec iov = { .iov_base = message, .iov_len = sizeof(message), };
        struct msghdr hdr = { .msg_iov = &iov, .msg_iovlen = 1, };

        int ret = nl_sendmsg(sk, msg, &hdr);
        printf("Bytes: %d\n", ret);  // 14
        return 0;
    }
    // Expected bash output:
    // Hello World!
    // Bytes: 14
    """
    sk = nl_socket_alloc()
    msg = nlmsg_alloc_simple(0, 0)
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    sk.socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.socket_instance.connect(tcp_server.server.server_address)
    sk.s_local.nl_pid = 0
    nl_complete_msg(sk, msg)

    message = 'Hello World!\n\0'
    iov = bytes(message.encode('ascii'))
    hdr = msghdr(msg_iov=iov)

    assert 14 == nl_sendmsg(sk, msg, hdr)
    assert [iov] == tcp_server.data
    nl_socket_free(sk)
Exemple #5
0
def test_socket(tcp_server):
    r"""C code to test against.

    // gcc a.c $(pkg-config --cflags --libs libnl-genl-3.0) && (nc -l 2000 |base64 &) && sleep 0.1 && ./a.out
    #include <netlink/msg.h>
    struct nl_sock {
        struct sockaddr_nl s_local; struct sockaddr_nl s_peer; int s_fd; int s_proto; unsigned int s_seq_next;
        unsigned int s_seq_expect; int s_flags; struct nl_cb *s_cb; size_t s_bufsize;
    };
    int main() {
        struct nl_msg *msg = nlmsg_alloc();
        struct nlmsghdr *nlh = nlmsg_hdr(msg);
        printf("%d == nlh->nlmsg_len\n", nlh->nlmsg_len);
        printf("%d == nlh->nlmsg_type\n", nlh->nlmsg_type);
        printf("%d == nlh->nlmsg_flags\n", nlh->nlmsg_flags);
        printf("%d == nlh->nlmsg_seq\n", nlh->nlmsg_seq);
        printf("%d == nlh->nlmsg_pid\n", nlh->nlmsg_pid);

        struct sockaddr_in sin = { .sin_port = htons(2000), .sin_family = AF_INET, };
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        connect(fd, (struct sockaddr *) &sin, sizeof(sin));

        struct nl_sock *sk = nl_socket_alloc();
        nl_connect(sk, NETLINK_ROUTE);
        sk->s_fd = fd;
        sk->s_local.nl_pid = 0;
        sk->s_seq_next = 0;
        nl_complete_msg(sk, msg);
        printf("%d == nlh->nlmsg_seq\n", nlh->nlmsg_seq);

        struct iovec iov = { .iov_base = nlh, .iov_len = nlh->nlmsg_len };
        struct msghdr hdr = { .msg_iov = &iov, .msg_iovlen = 1, };

        int ret = nl_sendmsg(sk, msg, &hdr);
        printf("Bytes: %d\n", ret);
        return 0;
    }
    // Expected bash output:
    // 16 == nlh->nlmsg_len
    // 0 == nlh->nlmsg_type
    // 0 == nlh->nlmsg_flags
    // 0 == nlh->nlmsg_seq
    // 0 == nlh->nlmsg_pid
    // 0 == nlh->nlmsg_seq
    // Bytes: 16
    // EAAAAAAABQAAAAAAAAAAAA==
    """
    msg = nlmsg_alloc()
    nlh = nlmsg_hdr(msg)
    assert 16 == nlh.nlmsg_len
    assert 0 == nlh.nlmsg_type
    assert 0 == nlh.nlmsg_flags
    assert 0 == nlh.nlmsg_seq
    assert 0 == nlh.nlmsg_pid

    sk = nl_socket_alloc()
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    sk.socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.socket_instance.connect(tcp_server.server.server_address)
    sk.s_local.nl_pid = 0
    sk.s_seq_next = 0
    nl_complete_msg(sk, msg)
    assert 0 == nlh.nlmsg_seq
    nlh.nlmsg_pid = 0  # sk.s_local.nl_pid is read-only in Python.

    iov = nlh.bytearray[:nlh.nlmsg_len]
    hdr = msghdr(msg_iov=iov)

    assert 16 == nl_sendmsg(sk, msg, hdr)
    assert 1 == len(tcp_server.data)
    assert b'EAAAAAAABQAAAAAAAAAAAA==' == base64.b64encode(buffer(iov))
    assert b'EAAAAAAABQAAAAAAAAAAAA==' == base64.b64encode(buffer(tcp_server.data[0]))
    nl_socket_free(sk)
Exemple #6
0
def test_socket(tcp_server):
    r"""C code to test against.

    // gcc a.c $(pkg-config --cflags --libs libnl-genl-3.0) && (nc -l 2000 |base64 &) && sleep 0.1 && ./a.out
    #include <netlink/msg.h>
    struct nl_sock {
        struct sockaddr_nl s_local; struct sockaddr_nl s_peer; int s_fd; int s_proto; unsigned int s_seq_next;
        unsigned int s_seq_expect; int s_flags; struct nl_cb *s_cb; size_t s_bufsize;
    };
    int main() {
        struct nl_msg *msg = nlmsg_alloc();
        printf("%d == nla_put_u32()\n", nla_put_u32(msg, 4, 8));
        struct nlmsghdr *nlh = nlmsg_hdr(msg);
        struct nlattr *attr = nlmsg_find_attr(nlh, 0, 4);
        printf("%d == nla_type(attr)\n", nla_type(attr));
        printf("%d == nla_get_u32(attr)\n", nla_get_u32(attr));
        printf("%d == attr->nla_len\n", attr->nla_len);

        struct sockaddr_in sin = { .sin_port = htons(2000), .sin_family = AF_INET, };
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        connect(fd, (struct sockaddr *) &sin, sizeof(sin));

        struct nl_sock *sk = nl_socket_alloc();
        nl_connect(sk, NETLINK_ROUTE);
        sk->s_fd = fd;
        sk->s_local.nl_pid = 0;
        nl_complete_msg(sk, msg);

        struct iovec iov = { .iov_base = attr, .iov_len = attr->nla_len };
        struct msghdr hdr = { .msg_iov = &iov, .msg_iovlen = 1, };

        int ret = nl_sendmsg(sk, msg, &hdr);
        printf("Bytes: %d\n", ret);  // 14
        return 0;
    }
    // Expected bash output:
    // 0 == nla_put_u32()
    // 4 == nla_type(attr)
    // 8 == nla_get_u32(attr)
    // 8 == attr->nla_len
    // Bytes: 8
    // CAAEAAgAAAA=
    """
    msg = nlmsg_alloc()
    assert 0 == libnl.attr.nla_put_u32(msg, 4, 8)
    nlh = nlmsg_hdr(msg)
    attr = nlmsg_find_attr(nlh, 0, 4)
    assert 4 == libnl.attr.nla_type(attr)
    assert 8 == libnl.attr.nla_get_u32(attr)
    assert 8 == attr.nla_len

    sk = nl_socket_alloc()
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    sk.socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.socket_instance.connect(tcp_server.server.server_address)
    sk.s_local.nl_pid = 0
    nl_complete_msg(sk, msg)

    iov = attr.bytearray[:attr.nla_len]
    hdr = msghdr(msg_iov=iov)

    assert 8 == nl_sendmsg(sk, msg, hdr)
    assert 1 == len(tcp_server.data)
    assert b'CAAEAAgAAAA=' == base64.b64encode(buffer(iov))
    assert b'CAAEAAgAAAA=' == base64.b64encode(buffer(tcp_server.data[0]))
    nl_socket_free(sk)
Exemple #7
0
def test_socket(tcp_server):
    r"""C code to test against.

    // gcc a.c $(pkg-config --cflags --libs libnl-genl-3.0) && (nc -l 2000 |base64 &) && sleep 0.1 && ./a.out
    #include <netlink/msg.h>
    struct nl_sock {
        struct sockaddr_nl s_local; struct sockaddr_nl s_peer; int s_fd; int s_proto; unsigned int s_seq_next;
        unsigned int s_seq_expect; int s_flags; struct nl_cb *s_cb; size_t s_bufsize;
    };
    int main() {
        struct nl_msg *msg = nlmsg_alloc();
        printf("%d == nla_put_u32()\n", nla_put_u32(msg, 4, 8));
        struct nlmsghdr *nlh = nlmsg_hdr(msg);
        struct nlattr *attr = nlmsg_find_attr(nlh, 0, 4);
        printf("%d == nla_type(attr)\n", nla_type(attr));
        printf("%d == nla_get_u32(attr)\n", nla_get_u32(attr));
        printf("%d == attr->nla_len\n", attr->nla_len);

        struct sockaddr_in sin = { .sin_port = htons(2000), .sin_family = AF_INET, };
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        connect(fd, (struct sockaddr *) &sin, sizeof(sin));

        struct nl_sock *sk = nl_socket_alloc();
        nl_connect(sk, NETLINK_ROUTE);
        sk->s_fd = fd;
        sk->s_local.nl_pid = 0;
        nl_complete_msg(sk, msg);

        struct iovec iov = { .iov_base = attr, .iov_len = attr->nla_len };
        struct msghdr hdr = { .msg_iov = &iov, .msg_iovlen = 1, };

        int ret = nl_sendmsg(sk, msg, &hdr);
        printf("Bytes: %d\n", ret);  // 14
        return 0;
    }
    // Expected bash output:
    // 0 == nla_put_u32()
    // 4 == nla_type(attr)
    // 8 == nla_get_u32(attr)
    // 8 == attr->nla_len
    // Bytes: 8
    // CAAEAAgAAAA=
    """
    msg = nlmsg_alloc()
    assert 0 == libnl.attr.nla_put_u32(msg, 4, 8)
    nlh = nlmsg_hdr(msg)
    attr = nlmsg_find_attr(nlh, 0, 4)
    assert 4 == libnl.attr.nla_type(attr)
    assert 8 == libnl.attr.nla_get_u32(attr)
    assert 8 == attr.nla_len

    sk = nl_socket_alloc()
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    sk.socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.socket_instance.connect(tcp_server.server.server_address)
    sk.s_local.nl_pid = 0
    nl_complete_msg(sk, msg)

    iov = attr.bytearray[: attr.nla_len]
    hdr = msghdr(msg_iov=iov)

    assert 8 == nl_sendmsg(sk, msg, hdr)
    assert 1 == len(tcp_server.data)
    assert b"CAAEAAgAAAA=" == base64.b64encode(buffer(iov))
    assert b"CAAEAAgAAAA=" == base64.b64encode(buffer(tcp_server.data[0]))
    nl_socket_free(sk)
def test_socket(tcp_server):
    r"""C code to test against.

    // gcc a.c $(pkg-config --cflags --libs libnl-genl-3.0) && (nc -l 2000 |base64 &) && sleep 0.1 && ./a.out
    #include <netlink/msg.h>
    struct nl_sock {
        struct sockaddr_nl s_local; struct sockaddr_nl s_peer; int s_fd; int s_proto; unsigned int s_seq_next;
        unsigned int s_seq_expect; int s_flags; struct nl_cb *s_cb; size_t s_bufsize;
    };
    int main() {
        struct nl_msg *msg = nlmsg_alloc();
        struct nlmsghdr *nlh = nlmsg_hdr(msg);
        printf("%d == nlh->nlmsg_len\n", nlh->nlmsg_len);
        printf("%d == nlh->nlmsg_type\n", nlh->nlmsg_type);
        printf("%d == nlh->nlmsg_flags\n", nlh->nlmsg_flags);
        printf("%d == nlh->nlmsg_seq\n", nlh->nlmsg_seq);
        printf("%d == nlh->nlmsg_pid\n", nlh->nlmsg_pid);

        struct sockaddr_in sin = { .sin_port = htons(2000), .sin_family = AF_INET, };
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        connect(fd, (struct sockaddr *) &sin, sizeof(sin));

        struct nl_sock *sk = nl_socket_alloc();
        nl_connect(sk, NETLINK_ROUTE);
        sk->s_fd = fd;
        sk->s_local.nl_pid = 0;
        sk->s_seq_next = 0;
        nl_complete_msg(sk, msg);
        printf("%d == nlh->nlmsg_seq\n", nlh->nlmsg_seq);

        struct iovec iov = { .iov_base = nlh, .iov_len = nlh->nlmsg_len };
        struct msghdr hdr = { .msg_iov = &iov, .msg_iovlen = 1, };

        int ret = nl_sendmsg(sk, msg, &hdr);
        printf("Bytes: %d\n", ret);
        return 0;
    }
    // Expected bash output:
    // 16 == nlh->nlmsg_len
    // 0 == nlh->nlmsg_type
    // 0 == nlh->nlmsg_flags
    // 0 == nlh->nlmsg_seq
    // 0 == nlh->nlmsg_pid
    // 0 == nlh->nlmsg_seq
    // Bytes: 16
    // EAAAAAAABQAAAAAAAAAAAA==
    """
    msg = nlmsg_alloc()
    nlh = nlmsg_hdr(msg)
    assert 16 == nlh.nlmsg_len
    assert 0 == nlh.nlmsg_type
    assert 0 == nlh.nlmsg_flags
    assert 0 == nlh.nlmsg_seq
    assert 0 == nlh.nlmsg_pid

    sk = nl_socket_alloc()
    nl_connect(sk, NETLINK_ROUTE)
    sk.socket_instance.close()
    sk.socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.socket_instance.connect(tcp_server.server.server_address)
    sk.s_local.nl_pid = 0
    sk.s_seq_next = 0
    nl_complete_msg(sk, msg)
    assert 0 == nlh.nlmsg_seq
    nlh.nlmsg_pid = 0  # sk.s_local.nl_pid is read-only in Python.

    iov = nlh.bytearray[:nlh.nlmsg_len]
    hdr = msghdr(msg_iov=iov)

    assert 16 == nl_sendmsg(sk, msg, hdr)
    assert 1 == len(tcp_server.data)
    assert b'EAAAAAAABQAAAAAAAAAAAA==' == base64.b64encode(buffer(iov))
    assert b'EAAAAAAABQAAAAAAAAAAAA==' == base64.b64encode(
        buffer(tcp_server.data[0]))
    nl_socket_free(sk)