# Copyright (c) 2022, Dell Inc. or its subsidiaries. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # See the LICENSE file for details. # # This file is part of NVMe STorage Appliance Services (nvme-stas). # # Authors: Martin Belanger '''A collection of IP address and network interface utilities''' import struct import socket import ipaddress RTM_BASE = 16 RTM_GETLINK = 18 RTM_NEWADDR = 20 RTM_GETADDR = 22 NLM_F_REQUEST = 0x01 NLM_F_ROOT = 0x100 NLMSG_DONE = 3 NLMSG_HDRLEN = 16 IFADDRMSG_SZ = 8 IFINFOMSG_SZ = 16 ARPHRD_ETHER = 1 ARPHRD_LOOPBACK = 772 NLMSG_LENGTH = lambda msg_len: msg_len + NLMSG_HDRLEN # pylint: disable=unnecessary-lambda-assignment RTATTR_SZ = 4 RTA_ALIGN = lambda length: ((length + 3) & ~3) # pylint: disable=unnecessary-lambda-assignment IFLA_ADDRESS = 1 IFLA_IFNAME = 3 def _nlmsghdr(nlmsg_type, nlmsg_flags, nlmsg_seq, nlmsg_pid, msg_len: int): '''Implement this C struct: struct nlmsghdr { __u32 nlmsg_len; /* Length of message including header */ __u16 nlmsg_type; /* Message content */ __u16 nlmsg_flags; /* Additional flags */ __u32 nlmsg_seq; /* Sequence number */ __u32 nlmsg_pid; /* Sending process port ID */ }; ''' return struct.pack('= len(nlmsg): nlmsg += sock.recv(8192) nlmsghdr = nlmsg[nlmsg_idx : nlmsg_idx + NLMSG_HDRLEN] nlmsg_len, nlmsg_type, _, _, _ = struct.unpack('= len(nlmsg): nlmsg += sock.recv(8192) nlmsghdr = nlmsg[nlmsg_idx : nlmsg_idx + NLMSG_HDRLEN] nlmsg_len, nlmsg_type, _, _, _ = struct.unpack('