Skip to content

Commit

Permalink
add option for multicast_ttl value
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-jmattson committed Oct 22, 2024
1 parent d57bd8a commit 4b06524
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ PyTAK has the following built-in configuration parameters:

For systems with multiple IP network interfaces, specifies which IP interface to use for the multicast group.

* **`PYTAK_MULTICAST_TTL`**
* Default: `1`

For clients that are more than one hop away from the TAK broadcast network, specifies the time-to-live (TTL) of multicast packets. This is helpful when the client is hosted in a virtual machine or container with an overlay network.

## CoT Event Attributes

Expand Down
14 changes: 8 additions & 6 deletions pytak/client_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ async def protocol_factory( # NOQA pylint: disable=too-many-locals,too-many-bra
),
0,
)
reader, writer = await pytak.create_udp_client(cot_url, local_addr)
multicast_ttl = config.get("PYTAK_MULTICAST_TTL", 1)
reader, writer = await pytak.create_udp_client(cot_url, local_addr, multicast_ttl)

# LOG
elif "log" in scheme:
Expand All @@ -130,7 +131,7 @@ async def protocol_factory( # NOQA pylint: disable=too-many-locals,too-many-bra


async def create_udp_client(
url: ParseResult, local_addr=None
url: ParseResult, local_addr=None, multicast_ttl=1
) -> Tuple[Union[DatagramClient, None], DatagramClient]:
"""Create an AsyncIO UDP network client for Unicast, Broadcast & Multicast.
Expand Down Expand Up @@ -170,6 +171,11 @@ async def create_udp_client(
if is_broadcast:
writer.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

if is_multicast:
writer.socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack("b", multicast_ttl)
)

if is_write_only:
return reader, writer

Expand Down Expand Up @@ -207,11 +213,7 @@ async def create_udp_client(
)
group = int(ipaddress.IPv4Address(host))
mreq = struct.pack("!LL", group, ip)

reader.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
reader.socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack("b", 1)
)

return reader, writer

Expand Down

0 comments on commit 4b06524

Please sign in to comment.