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

Fix: UDP server bind issue after PR#161 #164

Merged
merged 3 commits into from
Nov 22, 2022
Merged
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
28 changes: 21 additions & 7 deletions iso15118/secc/transport/udp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import socket
import struct
from asyncio import DatagramTransport
from sys import platform
from typing import Optional, Tuple

from iso15118.shared.messages.v2gtp import V2GTPMessage
Expand Down Expand Up @@ -57,13 +58,26 @@ async def __init__.
# Socket type (datagram, determines transport layer protocol UDP)
sock = socket.socket(family=socket.AF_INET6, type=socket.SOCK_DGRAM)

# Allows address to be reused
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind the socket to the predefined port for receiving
# UDP packets (SDP requests)
full_ipv6_address = await get_link_local_full_addr(SDP_SERVER_PORT, iface)
sock.bind(full_ipv6_address)
# Block binding to this socket+interface combination from now.
# Ref: https://www.man7.org/linux/man-pages/man7/socket.7.html
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)

# Bind the socket to the predefined port on specified interface for receiving
# UDP packets (SDP requests). This is done differently on Mac and Linux.
# Reference:
# https://djangocas.dev/blog/linux/linux-SO_BINDTODEVICE-and-mac-IP_BOUND_IF-to-bind-socket-to-a-network-interface/ # noqa
# https://linux.die.net/man/7/socket
# https://stackoverflow.com/questions/20616029/os-x-equivalent-of-so-bindtodevice # noqa
if platform == "darwin":
full_ipv6_address = await get_link_local_full_addr(SDP_SERVER_PORT, iface)
sock.bind(full_ipv6_address)
else:
sock.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
(iface + "\0").encode("ascii"),
)
sock.bind("", SDP_SERVER_PORT)

# After the regular socket is created and bound to a port, it can be
# added to the multicast group by using setsockopt() to set the
Expand Down