Skip to content

Commit

Permalink
Code clenup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 1, 2024
1 parent 07e498f commit 6f52416
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pytcp/lib/net_addr/ip4_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,5 @@ def multicast_mac(self) -> MacAddress:
assert self.is_multicast

return MacAddress(
int(MacAddress(0x01005E000000)) | self._address & 0x7FFFFF
int(MacAddress(0x0100_5E00_0000)) | self._address & 0x0000_007F_FFFF
)
5 changes: 3 additions & 2 deletions pytcp/lib/net_addr/ip6_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ def solicited_node_multicast(self) -> Ip6Address:
"""

return Ip6Address(
self._address & 0xFFFFFF | int(Ip6Address("ff02::1:ff00:0"))
self._address & 0x0000_0000_0000_0000_0000_0000_00FF_FFFF
| int(Ip6Address("ff02::1:ff00:0"))
)

@property
Expand All @@ -242,7 +243,7 @@ def multicast_mac(self) -> MacAddress:

assert self.is_multicast
return MacAddress(
int(MacAddress(0x333300000000)) | self._address & 0xFFFFFFFF
int(MacAddress(0x3333_0000_0000)) | self._address & 0x0000_FFFF_FFFF
)

@property
Expand Down
27 changes: 14 additions & 13 deletions pytcp/lib/net_addr/mac_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,51 +145,52 @@ def is_unspecified(self) -> bool:
Check if address is unspecified.
"""

return self._address == 0
return self._address == 0x0000_0000_0000

@property
def is_unicast(self) -> bool:
"""
Check if address is unicast.
"""

return (
self._address != 0 # unspecified
and self._address
not in range(1101088686080, 1101105463296) # IPv4 multicast
and self._address
not in range(56294136348672, 56298431315968) # IPv6 multicast
and self._address != 281474976710655 # broadcst
)
return (self._address & 0x0100_0000_0000) == 0x0000_0000_0000

@property
def is_multicast(self) -> bool:
"""
Check if address is multicast.
"""

return (self._address & 0x0100_0000_0000) == 0x0100_0000_0000

@property
def is_multicast_ip4(self) -> bool:
"""
Check if address is a MAC for IPv4 multicast.
"""

return self._address in range(1101088686080, 1101105463296)
return (self._address & 0xFFFF_FF00_0000) == 0x0100_5E00_0000

@property
def is_multicast_ip6(self) -> bool:
"""
Check if address is a MAC for IPv6 multicast.
"""

return self._address in range(56294136348672, 56298431315968)
return (self._address & 0xFFFF_0000_0000) == 0x3333_0000_0000

@property
def is_multicast_ip6_solicited_node(self) -> bool:
"""
Check if address is a MAC for IPv6 solicited node multicast.
"""

return self._address in range(56298414538752, 56298431315968)
return (self._address & 0xFFFF_FF00_0000) == 0x3333_FF00_0000

@property
def is_broadcast(self) -> bool:
"""
Check if address is a broadcast MAC.
"""

return self._address == 281474976710655
return self._address == 0xFFFF_FFFF_FFFF
6 changes: 5 additions & 1 deletion tests__legacy/unit/test__lib__mac_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class MacSample:
is_broadcast: bool = False

self.mac_samples = [
MacSample(MacAddress("00:00:00:00:00:00"), is_unspecified=True),
MacSample(
MacAddress("00:00:00:00:00:00"),
is_unspecified=True,
is_unicast=True,
),
MacSample(MacAddress("02:03:04:aa:bb:cc"), is_unicast=True),
MacSample(MacAddress("01:00:5e:01:02:03"), is_multicast_ip4=True),
MacSample(MacAddress("33:33:00:01:02:03"), is_multicast_ip6=True),
Expand Down

0 comments on commit 6f52416

Please sign in to comment.