Allocate new Netlink socket. Does not yet actually open a socket.

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/socket.c#L206

    Also has code for generating local port once.
    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/nl.c#L123

    Keyword arguments:
    cb -- custom callback handler.

    Returns:
    Newly allocated Netlink socket (nl_sock class instance) or None.

def nl_socket_alloc(cb=None):
    """Allocate new Netlink socket. Does not yet actually open a socket.

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/socket.c#L206

    Also has code for generating local port once.
    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/nl.c#L123

    Keyword arguments:
    cb -- custom callback handler.

    Returns:
    Newly allocated Netlink socket (nl_sock class instance) or None.
    """
    # Allocate the callback.
    cb = cb or nl_cb_alloc(default_cb)
    if not cb:
        return None

    # Allocate the socket.
    sk = nl_sock()
    sk.s_cb = cb
    sk.s_local.nl_family = getattr(socket, 'AF_NETLINK', -1)
    sk.s_peer.nl_family = getattr(socket, 'AF_NETLINK', -1)
    sk.s_seq_expect = sk.s_seq_next = int(time.time())
    sk.s_flags = NL_OWN_PORT  # The port is 0 (unspecified), meaning NL_OWN_PORT.

    # Generate local port.
    nl_socket_get_local_port(sk)  # I didn't find this in the C source, but during testing I saw this behavior.

    return sk