[go: up one dir, main page]

Skip to content

Commit

Permalink
fix network byte order
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhart committed Jun 3, 2023
1 parent 2928da8 commit a9f6a78
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ tasks:
status:
- '[ -n "$(find ./venv/lib/*/site-packages -type d -name "pygmp-*.dist-info" -print -quit)" ]'

test:
desc: Run pytests.
deps: [ install ]
cmds:
- sudo {{.USER_WORKING_DIR}}/venv/bin/python3 -m pytest -s {{.CLI_ARGS}}

setup-network:
desc: Setup the network namespaces for testing.
cmds:
Expand Down
4 changes: 2 additions & 2 deletions pygmp/daemons/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


def main(sock, args, app):
config = load_config("/home/jack/Documents/projects/pygmp/tests/simple.ini") # TODO - cleanup
config = load_config("/home/jack/Documents/projects/pygmp/tests/simple_confs/basic1.ini") # TODO - move
kernel.flush(sock)
kernel.disable_pim(sock)
kernel.enable_mrt(sock)
Expand Down Expand Up @@ -205,7 +205,7 @@ def add(self, mroute: MRoute):
if str(mroute.source) == ANY_ADDR:
vifi = self.vif_manager.vifi(mroute.from_)
if self._dynamic_mroutes.get(vifi):
self._dynamic_mroutes[vifi][self._dynamic_mroutes[vifi].index(mroute)] = mroute
self._dynamic_mroutes[vifi][self._dynamic_mroutes[vifi].index(mroute)] = mroute # FIXME throws ValueError
else:
self._dynamic_mroutes[vifi] = [mroute]
else:
Expand Down
2 changes: 2 additions & 0 deletions pygmp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import annotations
import hashlib
import socket
import sys
from ipaddress import ip_address, IPv4Address, IPv6Address


Expand Down Expand Up @@ -49,6 +50,7 @@ def host_hex_to_ip(hex_val: str) -> IPv4Address | IPv6Address:
"""Convert a hex string in network byte order (big-endian) to IP address object."""
# Convert hex string to bytes
net_order = bytes.fromhex(hex_val)
net_order = net_order if sys.byteorder == 'big' else net_order[::-1]

if len(net_order) == 4: # IPv4 address
return ip_address(socket.inet_ntop(socket.AF_INET, net_order))
Expand Down
1 change: 0 additions & 1 deletion tests/simple.ini → tests/simple_confs/basic1.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ from = a1
source = 192.168.2.1
group = 224.0.1.0
to = a2,a3=2

14 changes: 10 additions & 4 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest

from pathlib import Path
from pygmp import kernel
from pygmp.daemons import config, simple

Expand All @@ -10,9 +10,9 @@ def igmp_sock():
yield sock


@pytest.fixture
def example_config(): # FIXME
return config.load_config("/home/jack/Documents/projects/pygmp/tests/simple.ini")
@pytest.fixture(params=list(Path("/home/jack/Documents/projects/pygmp/tests/simple_confs").glob("*.ini")))
def example_config(request): # FIXME
return config.load_config(str(request.param))


@pytest.fixture
Expand Down Expand Up @@ -67,6 +67,7 @@ def test_vifmanager_vifi(vif_manager):
vif_manager.vifi("a4")



def test_mfcmanager_init(mfc_manager):
assert len(mfc_manager.static_mfc()) == 1
assert len(mfc_manager.dynamic_mfc()) == 1
Expand All @@ -84,3 +85,8 @@ def test_mfcmanager_remove(mfc_manager, example_config):
mfc_manager.remove(example_config.mroute[1])
assert len(mfc_manager.static_mfc()) == 0
assert len(mfc_manager.dynamic_mfc()) == 0


def test_print(mfc_manager, example_config):
print(mfc_manager.static_mfc())
print(mfc_manager.dynamic_mfc())

0 comments on commit a9f6a78

Please sign in to comment.