Beispiel #1
0
def exec_sync(cmds):
    """Execute a command and convert returned values to native string.

    Note that this function should not be used if output data could be
    undecodable bytes.
    """
    retcode, out, err = exec_sync_bytes(cmds)
    return retcode, conversion_util.to_str(out), conversion_util.to_str(err)
Beispiel #2
0
def driver_name(device_name):
    """Returns the driver used by a device.

    Throws IOError ENODEV for non existing devices.
    Throws IOError EOPNOTSUPP for non supported devices, i.g., loopback.
    """
    encoded_name = conversion_util.to_binary(device_name)

    buff = array.array('b', b'\0' * struct.calcsize(DRVINFO_FORMAT))
    cmds = struct.pack('= I', ETHTOOL_GDRVINFO)
    buff[0:len(cmds)] = array.array('b', cmds)  # noqa: E203
    data = struct.pack(IFREQ_FORMAT, encoded_name, *buff.buffer_info())

    with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as sock:
        fcntl.ioctl(sock, SIOCETHTOOL, data)

    (
        cmds,
        driver,
        version,
        fw_version,
        businfo,
        _,
        _,
        n_priv_flags,
        n_stats,
        testinfo_len,
        eedump_len,
        regdump_len,
    ) = struct.unpack(DRVINFO_FORMAT, buff)
    driver_str = conversion_util.to_str(driver)
    return driver_str.rstrip('\0')  # C string end with the leftmost null char
Beispiel #3
0
def nl_object_get_type(obj):
    """Return the object's type.

    @arg obj             object

    @return Name of the object type or None if not recognized
    """
    _nl_object_get_type = _libnl('nl_object_get_type', c_char_p, c_void_p)
    object_type = _nl_object_get_type(obj)
    return conversion_util.to_str(object_type) if object_type else None
Beispiel #4
0
def nl_geterror(error_code):
    """Return error message for an error code.

    @arg error_code      error code

    @return error message
    """
    _nl_geterror = _libnl('nl_geterror', c_char_p, c_int)
    error_message = _nl_geterror(error_code)
    return conversion_util.to_str(error_message)
Beispiel #5
0
def rtnl_link_get_qdisc(link):
    """Return name of queueing discipline of link object.

    @arg link            Link object

    @return Name of qdisc or None if not specified.
    """
    _rtnl_link_get_qdisc = _libnl_route('rtnl_link_get_qdisc', c_char_p,
                                        c_void_p)
    qdisc = _rtnl_link_get_qdisc(link)
    return conversion_util.to_str(qdisc) if qdisc else None
Beispiel #6
0
def rtnl_link_get_name(link):
    """Return name of link object.

    @arg link            Link object

    @return Link name or None if name is not specified
    """
    _rtnl_link_get_name = _libnl_route('rtnl_link_get_name', c_char_p,
                                       c_void_p)
    name = _rtnl_link_get_name(link)
    return conversion_util.to_str(name) if name else None
Beispiel #7
0
def rtnl_link_get_type(link):
    """Return type of link.

    @arg link            Link object

    @return Name of link type or None if not specified.
    """
    _rtnl_link_get_type = _libnl_route('rtnl_link_get_type', c_char_p,
                                       c_void_p)
    link_type = _rtnl_link_get_type(link)
    return conversion_util.to_str(link_type) if link_type else None
Beispiel #8
0
def nl_af2str(family):
    """Convert address family code to string.

    @arg family          Address family code.

    @return Address family represented as string
    """
    _nl_af2str = _libnl('nl_af2str', c_char_p, c_int, c_char_p, c_size_t)
    buf = (c_char * CHARBUFFSIZE)()
    address_family = _nl_af2str(family, buf, sizeof(buf))
    return conversion_util.to_str(address_family)
Beispiel #9
0
def rtnl_addr_flags2str(flags_bitfield):
    """Return string representation of address flags bitfield.

    @arg flags_bitfield  Bitfield of address' flags

    @return String represantion of given flags in format "flag1,flag2,flag3".
    """
    _rtnl_addr_flags2str = _libnl_route('rtnl_addr_flags2str', c_char_p, c_int,
                                        c_char_p, c_size_t)
    buf = (c_char * (CHARBUFFSIZE * 2))()
    flags_str = _rtnl_addr_flags2str(flags_bitfield, buf, sizeof(buf))
    return conversion_util.to_str(flags_str)
Beispiel #10
0
def rtnl_scope2str(scope):
    """Convert address scope code to string.

    @arg scope           Address scope code.

    @return Address scope represented as string
    """
    _rtnl_scope2str = _libnl_route('rtnl_scope2str', c_char_p, c_int, c_char_p,
                                   c_size_t)
    buf = (c_char * CHARBUFFSIZE)()
    address_scope = _rtnl_scope2str(scope, buf, sizeof(buf))
    return conversion_util.to_str(address_scope)
Beispiel #11
0
def nl_addr2str(addr):
    """Convert abstract address object to string.

    @arg addr            Abstract address object.

    @return Address represented as string
    """
    _nl_addr2str = _libnl('nl_addr2str', c_char_p, c_void_p, c_char_p,
                          c_size_t)
    buf = (c_char * HWADDRSIZE)()
    address = _nl_addr2str(addr, buf, sizeof(buf))
    return conversion_util.to_str(address)
Beispiel #12
0
def rtnl_link_operstate2str(operstate_code):
    """Convert operstate code to string.

    @arg operstate_code  Operstate code.

    @return Operstate represented as string
    """
    _rtnl_link_operstate2str = _libnl_route('rtnl_link_operstate2str',
                                            c_char_p, c_int, c_char_p,
                                            c_size_t)
    buf = (c_char * CHARBUFFSIZE)()
    operstate = _rtnl_link_operstate2str(operstate_code, buf, sizeof(buf))
    return conversion_util.to_str(operstate)
Beispiel #13
0
def rtnl_link_i2name(cache, ifindex):
    """Translate interface index to corresponding link name.

    @arg cache           Link cache
    @arg ifindex         Interface index

    Translates the specified interface index to the corresponding link name.

    @return Name of link or None if no match was found.
    """
    _rtnl_link_i2name = _libnl_route('rtnl_link_i2name', c_char_p, c_void_p,
                                     c_int, c_char_p, c_size_t)
    buf = (c_char * CHARBUFFSIZE)()
    name = _rtnl_link_i2name(cache, ifindex, buf, sizeof(buf))
    return conversion_util.to_str(name) if name else None