Skip to content

Commit

Permalink
Allow specifying 5353 or 0 as a port
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Mar 10, 2025
1 parent fa31ac6 commit 2477f05
Showing 1 changed file with 3 additions and 31 deletions.
34 changes: 3 additions & 31 deletions Sources/DNSClient/DNSClient+Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ extension DNSClient {
/// Creates a multicast DNS client. This client will join the multicast group and listen for responses. It will also send queries to the multicast group.
/// - parameters:
/// - group: EventLoops to use
public static func connectMulticastClient(
public static func connectMulticast(
on group: EventLoopGroup,
port: Int = 5353,
queryTimeout: TimeAmount = .seconds(5),
onMulticastMessage: @escaping HandleMulticastMessage = { _ in nil }
) async throws -> DNSClient {
Expand All @@ -57,7 +58,7 @@ extension DNSClient {

let ipv4 = address.protocol.rawValue == PF_INET

return try await bootstrap.bind(host: ipv4 ? "0.0.0.0" : "::", port: 5353).map { channel in
return try await bootstrap.bind(host: ipv4 ? "0.0.0.0" : "::", port: port).map { channel in
let client = DNSClient(
channel: channel,
address: address,
Expand All @@ -70,35 +71,6 @@ extension DNSClient {
return client
}.get()
}

/// Creates a multicast DNS client. This client will join the multicast group and listen for responses. It will also send queries to the multicast group.
/// - parameters:
/// - group: EventLoops to use
public static func connectMulticast(
on group: EventLoopGroup,
queryTimeout: TimeAmount = .seconds(5),
onMulticastMessage: @escaping HandleMulticastMessage
) -> EventLoopFuture<DNSClient> {
do {
let address = try SocketAddress(ipAddress: "224.0.0.251", port: 5353)

return connect(
on: group,
localPort: 5353,
config: [address]
).flatMap { client in
let channel = client.channel as! MulticastChannel
client.isMulticast = true
client.timeout = queryTimeout
client.dnsDecoder.handleMulticast.withLockedValue { handler in
handler = onMulticastMessage
}
return channel.joinGroup(address).map { client }
}
} catch {
return group.next().makeFailedFuture(UnableToParseConfig())
}
}

/// Connect to the dns server using TCP
///
Expand Down

0 comments on commit 2477f05

Please sign in to comment.