1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add opposite endian ipv6 addresses for s390x and other Big Endian architecture support

This commit is contained in:
Kelly Brazil
2023-12-21 14:47:13 -08:00
parent b14a8980b8
commit 50d5d3a3ba
2 changed files with 54 additions and 30 deletions

View File

@ -37,30 +37,37 @@ https://github.com/torvalds/linux/blob/master/net/ipv6/tcp_ipv6.c
[ [
{ {
"entry": integer, "entry": integer,
"local_address": string, "local_address": string,
"local_port": integer, "local_port": integer,
"remote_address": string, "remote_address": string,
"remote_port": integer, "remote_port": integer,
"state": string, "state": string,
"tx_queue": string, "tx_queue": string,
"rx_queue": string, "rx_queue": string,
"timer_active": integer, "timer_active": integer,
"jiffies_until_timer_expires": string, "jiffies_until_timer_expires": string,
"unrecovered_rto_timeouts": string, "unrecovered_rto_timeouts": string,
"uid": integer, "uid": integer,
"unanswered_0_window_probes": integer, "unanswered_0_window_probes": integer,
"inode": integer, "inode": integer,
"sock_ref_count": integer, "sock_ref_count": integer,
"sock_mem_loc": string, "sock_mem_loc": string,
"retransmit_timeout": integer, "retransmit_timeout": integer,
"soft_clock_tick": integer, "soft_clock_tick": integer,
"ack_quick_pingpong": integer, "ack_quick_pingpong": integer,
"sending_congestion_window": integer, "sending_congestion_window": integer,
"slow_start_size_threshold": 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: Examples:
$ cat /proc/net/tcp | jc --proc -p $ cat /proc/net/tcp | jc --proc -p
@ -155,16 +162,16 @@ Examples:
... ...
] ]
""" """
import binascii
import socket import socket
import struct import struct
import ipaddress
from typing import List, Dict from typing import List, Dict
import jc.utils import jc.utils
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.0' version = '1.1'
description = '`/proc/net/tcp` and `/proc/net/tcp6` file parser' description = '`/proc/net/tcp` and `/proc/net/tcp6` file parser'
author = 'Alvin Solomon' author = 'Alvin Solomon'
author_email = 'alvinms01@gmail.com' author_email = 'alvinms01@gmail.com'
@ -181,10 +188,15 @@ def hex_to_ip(hexaddr: str) -> str:
addr_long = int(hexaddr, 16) addr_long = int(hexaddr, 16)
return socket.inet_ntop(socket.AF_INET, struct.pack("<L", addr_long)) return socket.inet_ntop(socket.AF_INET, struct.pack("<L", addr_long))
elif len(hexaddr) == 32: elif len(hexaddr) == 32:
addr = binascii.a2b_hex(hexaddr) newaddr = ''
addr_tup = struct.unpack('>IIII', addr) for chunk in range(0, 32, 8):
addr_bytes = struct.pack('@IIII', *addr_tup) chunk_a = hexaddr[chunk + 6:chunk + 8]
return socket.inet_ntop(socket.AF_INET6, addr_bytes) 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 '' return ''
@ -210,11 +222,23 @@ def _process(proc_data: List[Dict]) -> List[Dict]:
for entry in proc_data: for entry in proc_data:
if 'local_address' in entry: 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['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) 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: for item in int_list:
if item in entry: if item in entry:
entry[item] = jc.utils.convert_to_int(entry[item]) entry[item] = jc.utils.convert_to_int(entry[item])

View File

@ -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"}]