Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for multicast_ttl value #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading