0

I'm reading data over netlink socket. One of the TLVs contain IPv4 address as a sequence of 4 bytes, e.g. 0x01 0x02 0x03 0x0b for address 1.2.3.11 :

import socket
...
s = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)
s.bind((os.getpid(), RTMGRP_IPV4_IFADDR))

while True:
   data = s.recv(65535)
   msg_len, msg_type, flags, seq, pid = struct.unpack("=LHHLL", data[:16])
   ...

   # skip all headers and get to TLVs
   data = data[24:]
   remaining = msg_len - 24

   while remaining:
     rta_len, rta_type = struct.unpack("=HH", data[:4])
     ...
     rta_data = data[4:rta_len]

     ...
     if rta_type == IFA_ADDRESS:
         # rta_data[:4] contains IP address
     ...

I need to read rta_data[:4] in such way that I get a string which I can pass on further. I tried to play with encode() or decode() string methods, but with no luck. I guess I should be walking through rta_data and convert each byte into string and put dots in between? I wonder if there is a one-liner for this.

Mark
  • 6,052
  • 8
  • 61
  • 129

0 Answers0