From 50d5d3a3ba0f32b13149dc8071b2e09e450dd622 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 21 Dec 2023 14:47:13 -0800 Subject: [PATCH] add opposite endian ipv6 addresses for s390x and other Big Endian architecture support --- jc/parsers/proc_net_tcp.py | 82 ++++++++++++++++--------- tests/fixtures/linux-proc/net_tcp6.json | 2 +- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/jc/parsers/proc_net_tcp.py b/jc/parsers/proc_net_tcp.py index 99c3ddac..6b504f7e 100644 --- a/jc/parsers/proc_net_tcp.py +++ b/jc/parsers/proc_net_tcp.py @@ -37,30 +37,37 @@ https://github.com/torvalds/linux/blob/master/net/ipv6/tcp_ipv6.c [ { - "entry": integer, - "local_address": string, - "local_port": integer, - "remote_address": string, - "remote_port": integer, - "state": string, - "tx_queue": string, - "rx_queue": string, - "timer_active": integer, - "jiffies_until_timer_expires": string, - "unrecovered_rto_timeouts": string, - "uid": integer, - "unanswered_0_window_probes": integer, - "inode": integer, - "sock_ref_count": integer, - "sock_mem_loc": string, - "retransmit_timeout": integer, - "soft_clock_tick": integer, - "ack_quick_pingpong": integer, - "sending_congestion_window": integer, - "slow_start_size_threshold": integer + "entry": integer, + "local_address": string, + "local_port": integer, + "remote_address": string, + "remote_port": integer, + "state": string, + "tx_queue": string, + "rx_queue": string, + "timer_active": integer, + "jiffies_until_timer_expires": string, + "unrecovered_rto_timeouts": string, + "uid": integer, + "unanswered_0_window_probes": integer, + "inode": integer, + "sock_ref_count": integer, + "sock_mem_loc": string, + "retransmit_timeout": integer, + "soft_clock_tick": integer, + "ack_quick_pingpong": integer, + "sending_congestion_window": integer, + "slow_start_size_threshold": integer, + "opposite_endian_local_address": string, [0] + "opposite_endian_remote_address": string [0] } ] + [0] For IPv6 output originating from an opposite endian architecture + (e.g. s390x vs. x64) You should not need to use this to process + output originating from the same machine running `jc` or from a + machine with the same endianness. + Examples: $ cat /proc/net/tcp | jc --proc -p @@ -155,16 +162,16 @@ Examples: ... ] """ -import binascii import socket import struct +import ipaddress from typing import List, Dict import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = '`/proc/net/tcp` and `/proc/net/tcp6` file parser' author = 'Alvin Solomon' author_email = 'alvinms01@gmail.com' @@ -181,10 +188,15 @@ def hex_to_ip(hexaddr: str) -> str: addr_long = int(hexaddr, 16) return socket.inet_ntop(socket.AF_INET, struct.pack("IIII', addr) - addr_bytes = struct.pack('@IIII', *addr_tup) - return socket.inet_ntop(socket.AF_INET6, addr_bytes) + newaddr = '' + for chunk in range(0, 32, 8): + chunk_a = hexaddr[chunk + 6:chunk + 8] + chunk_b = hexaddr[chunk + 4:chunk + 6] + chunk_c = hexaddr[chunk + 2:chunk + 4] + chunk_d = hexaddr[chunk + 0:chunk + 2] + newaddr = newaddr + chunk_a + chunk_b + chunk_c + chunk_d + full_addr = ':'.join(newaddr[i:i + 4] for i in range(0, 32, 4)) + return ipaddress.IPv6Address(full_addr).compressed return '' @@ -210,11 +222,23 @@ def _process(proc_data: List[Dict]) -> List[Dict]: for entry in proc_data: if 'local_address' in entry: - entry['local_address'] = hex_to_ip(entry['local_address']) + local_addr = entry['local_address'] + remote_addr = entry['remote_address'] + + entry['local_address'] = hex_to_ip(local_addr) entry['local_port'] = int(entry['local_port'], 16) - entry['remote_address'] = hex_to_ip(entry['remote_address']) + entry['remote_address'] = hex_to_ip(remote_addr) entry['remote_port'] = int(entry['remote_port'], 16) + if len(local_addr) == 32: + opp_endian_local_addr = ':'.join(local_addr[i:i + 4] for i in range(0, 32, 4)) + opp_endian_local_addr = ipaddress.IPv6Address(opp_endian_local_addr).compressed + opp_endian_remote_addr = ':'.join(remote_addr[i:i + 4] for i in range(0, 32, 4)) + opp_endian_remote_addr = ipaddress.IPv6Address(opp_endian_remote_addr).compressed + + entry['opposite_endian_local_address'] = opp_endian_local_addr + entry['opposite_endian_remote_address'] = opp_endian_remote_addr + for item in int_list: if item in entry: entry[item] = jc.utils.convert_to_int(entry[item]) diff --git a/tests/fixtures/linux-proc/net_tcp6.json b/tests/fixtures/linux-proc/net_tcp6.json index 291e9cf2..767a46ad 100644 --- a/tests/fixtures/linux-proc/net_tcp6.json +++ b/tests/fixtures/linux-proc/net_tcp6.json @@ -1 +1 @@ -[{"entry":"0","local_address":"::","local_port":22,"remote_address":"::","remote_port":0,"state":"0A","tx_queue":"00000000","rx_queue":"00000000","timer_active":0,"jiffies_until_timer_expires":"00000000","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":23556,"sock_ref_count":1,"sock_mem_loc":"ffff8c7a0dee0000","retransmit_timeout":100,"soft_clock_tick":0,"ack_quick_pingpong":0,"sending_congestion_window":10,"slow_start_size_threshold":0},{"entry":"1","local_address":"2601:646:9e01:f100::3a4c","local_port":59136,"remote_address":"2a03:2880:f131:83:face:b00c:0:25de","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003D1","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d000"},{"entry":"2","local_address":"2601:646:9e01:f100::3a4c","local_port":59878,"remote_address":"2001:4998:44:3507::8000","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003AD","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d440"},{"entry":"3","local_address":"2601:646:9e01:f100::3a4c","local_port":38370,"remote_address":"2600:1406:3a00:18f::c3a","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"00000400","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76daa0"},{"entry":"4","local_address":"2601:646:9e01:f100::3a4c","local_port":45028,"remote_address":"2001:559:19:6091::720","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003EC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d550"},{"entry":"5","local_address":"2601:646:9e01:f100::3a4c","local_port":40216,"remote_address":"2607:f8b0:4005:801::2004","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"00000396","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76dcc0"},{"entry":"6","local_address":"2601:646:9e01:f100::3a4c","local_port":60938,"remote_address":"2001:559:19:608f::b33","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003FC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d110"},{"entry":"7","local_address":"2601:646:9e01:f100::3a4c","local_port":32936,"remote_address":"2001:559:19:c000::17c5:3313","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003FC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d330"}] +[{"entry":"0","local_address":"::","local_port":22,"remote_address":"::","remote_port":0,"state":"0A","tx_queue":"00000000","rx_queue":"00000000","timer_active":0,"jiffies_until_timer_expires":"00000000","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":23556,"sock_ref_count":1,"sock_mem_loc":"ffff8c7a0dee0000","retransmit_timeout":100,"soft_clock_tick":0,"ack_quick_pingpong":0,"sending_congestion_window":10,"slow_start_size_threshold":0,"opposite_endian_local_address":"::","opposite_endian_remote_address":"::"},{"entry":"1","local_address":"2601:646:9e01:f100::3a4c","local_port":59136,"remote_address":"2a03:2880:f131:83:face:b00c:0:25de","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003D1","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d000","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"8028:32a:8300:31f1:cb0:cefa:de25:0"},{"entry":"2","local_address":"2601:646:9e01:f100::3a4c","local_port":59878,"remote_address":"2001:4998:44:3507::8000","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003AD","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d440","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"9849:120:735:4400::80:0"},{"entry":"3","local_address":"2601:646:9e01:f100::3a4c","local_port":38370,"remote_address":"2600:1406:3a00:18f::c3a","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"00000400","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76daa0","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"614:26:8f01:3a::3a0c:0"},{"entry":"4","local_address":"2601:646:9e01:f100::3a4c","local_port":45028,"remote_address":"2001:559:19:6091::720","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003EC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d550","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"5905:120:9160:1900::2007:0"},{"entry":"5","local_address":"2601:646:9e01:f100::3a4c","local_port":40216,"remote_address":"2607:f8b0:4005:801::2004","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"00000396","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76dcc0","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"b0f8:726:108:540::420:0"},{"entry":"6","local_address":"2601:646:9e01:f100::3a4c","local_port":60938,"remote_address":"2001:559:19:608f::b33","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003FC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d110","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"5905:120:8f60:1900::330b:0"},{"entry":"7","local_address":"2601:646:9e01:f100::3a4c","local_port":32936,"remote_address":"2001:559:19:c000::17c5:3313","remote_port":80,"state":"06","tx_queue":"00000000","rx_queue":"00000000","timer_active":3,"jiffies_until_timer_expires":"000003FC","unrecovered_rto_timeouts":"00000000","uid":0,"unanswered_0_window_probes":0,"inode":0,"sock_ref_count":3,"sock_mem_loc":"ffff8c7a1c76d330","opposite_endian_local_address":"4606:126:f1:19e::4c3a:0","opposite_endian_remote_address":"5905:120:c0:1900::1333:c517"}]