Skip to content

Add socket module - #5

Merged
Wescoeur merged 1 commit into
develfrom
ran-socket
Oct 2, 2026
Merged

Wescoeur merged 1 commit into
develfrom
ran-socket

Conversation

@Wescoeur

@Wescoeur Wescoeur commented Jun 22, 2026 •

Copy link
Copy Markdown
Member

Must be merged after #20.

@Wescoeur
Wescoeur requested a review from a team June 22, 2026 12:54
@Wescoeur Wescoeur changed the title feat(core): add socket module Add socket module Jun 22, 2026

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One non-blocking suggestion.

Comment thread src/xcp_storage/network/socket.py Outdated
Comment on lines +34 to +44
_FAMILY_TO_STR = {
socket.AF_INET: "IPv4",
socket.AF_INET6: "IPv6",
socket.AF_UNIX: "Unix"
}

_IP_VERSION_TO_FAMILY = {
4: socket.AF_INET,
6: socket.AF_INET6,
0: socket.AF_UNSPEC
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using enums.

Either a single one by doing a complex enum class:

class AddressFamily(Enum):
    # (member name) = (family, ip_version, display_str)
    IPV4 = (socket.AF_INET, 4, "IPv4")
    IPV6 = (socket.AF_INET6, 6, "IPv6")
    UNIX = (socket.AF_UNIX, None, "Unix")
    UNSPEC = (socket.AF_UNSPEC, 0, "Unknown")

    def __init__(self, family: socket.AddressFamily, ip_version: int, display_str: str):
        self.family = family
        self.ip_version = ip_version
        self.display_str = display_str

    # Build the map on lookup
    @classmethod
    def _missing_(cls: type['AddressFamily'], value: Union[socket.AddressFamily, int, str, None]) -> 'AddressFamily':
        if not hasattr(cls, '_lookup_map'):
            cls._lookup_map: Dict[Union[socket.AddressFamily, int, str], AddressFamily] = {}
            for member in cls:
                cls._lookup_map[str(member.family)] = member
                cls._lookup_map[member.display_str] = member
                if member.ip_version is None:
                    cls._lookup_map[str(member.ip_version)] = member
                else:
                    cls._lookup_map[member.ip_version] = member

        # cast to str to prevent collision between family (which is an int) and ip_version
        if value is None or isinstance(value, socket.AddressFamily):
            value = str(value)
        return cls._lookup_map.get(value, cls.UNSPEC)

Usage:

def get_socket_family_str(sock: socket.socket) -> str:
    return AddressFamily(sock.family).display_str

[...]

    family = AddressFamily(ip_version).family

Or using two enums:

class AddressFamily(Enum):
    IPv4 = socket.AF_INET
    IPv6 = socket.AF_INET6
    Unix = socket.AF_UNIX


class IPVersion(Enum):
    IPv4 = 4
    IPv6 = 6
    UNSPEC = 0

    @property
    def family(self) -> int:
        match self:
            case IPVersion.IPv4: return socket.AF_INET
            case IPVersion.IPv6: return socket.AF_INET6
            case IPVersion.UNSPEC: return socket.AF_UNSPEC

Usage:

def get_socket_family_str(sock: socket.socket) -> str:
    return AddressFamily(sock.family).name

[...]

    family = IPVersion(ip_version).family

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like you're suggesting a new API (which, unfortunately, I have no use for) for the code I haven't pushed yet. I'm open to a new contribution to this module, but in that case, additional tests would be required.

For the short to medium term, I simply need functions that allow me to construct an IP address or display information from a socket.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, no problem. Just to be clear the intent was to push the usage of enums as early as possible in the project when they could be fit.

But indeed this is something than can be sorted out in the longer term if there's a need.

@Millefeuille42
Millefeuille42 requested a review from a team June 24, 2026 13:53
Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py
@Wescoeur
Wescoeur force-pushed the ran-socket branch 2 times, most recently from 624e99f to 75220ca Compare June 30, 2026 09:30
@Wescoeur
Wescoeur requested review from a team and Millefeuille42 June 30, 2026 09:58
Base automatically changed from ran-service to devel September 30, 2026 09:59
@Wescoeur
Wescoeur force-pushed the ran-socket branch 2 times, most recently from 7d96833 to 45117bb Compare September 30, 2026 18:40
@Wescoeur
Wescoeur changed the base branch from devel to ran-robustify-iptables September 30, 2026 18:41
@Wescoeur
Wescoeur force-pushed the ran-socket branch 3 times, most recently from af38028 to e4fad85 Compare September 30, 2026 22:32

@Kuruyia Kuruyia left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really clean!

Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py
Comment thread src/xcp_storage/network/socket.py
Comment thread src/xcp_storage/network/socket.py Outdated
Comment thread src/xcp_storage/network/socket.py
Comment thread src/xcp_storage/network/socket.py
Comment thread src/xcp_storage/network/socket.py Outdated
@Wescoeur
Wescoeur force-pushed the ran-robustify-iptables branch 4 times, most recently from 16c6e30 to aac27b1 Compare October 2, 2026 10:19
@Wescoeur
Wescoeur force-pushed the ran-socket branch 3 times, most recently from 6b8de28 to bbf7146 Compare October 2, 2026 14:25
Signed-off-by: Ronan Abhamon <ronan.abhamon@vates.tech>
@Wescoeur
Wescoeur changed the base branch from ran-robustify-iptables to devel October 2, 2026 16:35
@Wescoeur
Wescoeur merged commit 2c8cc00 into devel Oct 2, 2026
15 checks passed
@Wescoeur
Wescoeur deleted the ran-socket branch October 2, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants