Skip to content

Commit

Permalink
feat(src): Add option to set the port for replies to the multicast
Browse files Browse the repository at this point in the history
  • Loading branch information
aleasto committed Sep 26, 2024
1 parent 486e054 commit b085f5a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ below for details.
* `--metadata-timeout TIMEOUT`
Set the timeout for HTTP-based metadata exchange. Default is 2.0 seconds.

* `--reply-port PORT`
Set the source port for outgoing multicast messages, so that replies will
use this as the destination port.
This is useful for firewalls that do not detect incoming unicast replies
to a multicast as part of the flow, so the port needs to be fixed in order
to be allowed manually.

* `-s`, `--shortlog`

Use a shorter logging format that only includes the level and message.
Expand Down
7 changes: 7 additions & 0 deletions man/wsdd.8
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ For IPv6, only link local addresses are actually considered as noted above.
\fB\-\-metadata-timeout\ \fITIMEOUT\fR
Set the timeout for HTTP-based metadata exchange. Default is 2.0 seconds.
.TP
\fB\-\-reply-port\ \fPORT\fR
Set the source port for outgoing multicast messages, so that replies will
use this as the destination port.
This is useful for firewalls that do not detect incoming unicast replies
to a multicast as part of the flow, so the port needs to be fixed in order
to be allowed manually.
.TP
\fB\-s\fR, \fB\-\-shortlog\fR
Use a shorter logging format that only includes the level and message.
This is useful in cases where the logging mechanism, like systemd on Linux,
Expand Down
19 changes: 19 additions & 0 deletions src/wsdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ def init_v6(self) -> None:
self.mc_send_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, args.hoplimit)
self.mc_send_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, idx)

# bind multicast socket to interface address and a user-provided port (or random if unspecified)
# this allows not-so-smart firewalls to whitelist another port to allow incoming replies
try:
self.mc_send_socket.bind((str(self.address), args.reply_port, 0, idx))
except OSError:
logger.error('specified port {} already in use for {}'.format(args.reply_port, str(self.address)))

self.listen_address = (self.address.address_str, WSD_HTTP_PORT, 0, idx)

def init_v4(self) -> None:
Expand Down Expand Up @@ -292,6 +299,13 @@ def init_v4(self) -> None:
self.mc_send_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, struct.pack('B', 0))
self.mc_send_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack('B', args.hoplimit))

# bind multicast socket to interface address and a user-provided port (or random if unspecified)
# this allows not-so-smart firewalls to whitelist another port to allow incoming replies
try:
self.mc_send_socket.bind((self.address.address_str, args.reply_port))
except OSError:
logger.error('specified port {} already in use for {}'.format(args.reply_port, self.address.address_str))

self.listen_address = (self.address.address_str, WSD_HTTP_PORT)

def add_handler(self, socket: socket.socket, handler: INetworkPacketHandler) -> None:
Expand Down Expand Up @@ -1862,6 +1876,11 @@ def parse_args() -> None:
'--metadata-timeout',
help='set timeout for HTTP-based metadata exchange',
default=2.0)
parser.add_argument(
'--reply-port',
help='recieve replies to multicast on this port',
type=int,
default=0)

args = parser.parse_args(sys.argv[1:])

Expand Down

0 comments on commit b085f5a

Please sign in to comment.