Skip to content

A Swift DNS library built on top of SwiftNIO; aiming to provide DNS client, resolver and server implementations.

License

Notifications You must be signed in to change notification settings

MahdiBM/swift-dns

Repository files navigation

Unit Tests CI Integration Tests CI Benchamrks CI Swift 6.2+

swift-dns

A Swift DNS library built on top of SwiftNIO; aiming to provide DNS client, resolver and server implementations.

Platform Requirements

  • Requires Swift 6.2.
  • On Apple platforms, requires macOS/iOS 26 etc... as well, to use.
  • You can still depend on this library in packages supporting macOS 15 and lower.
    • But you'll need to guard your usage of this library with @available or #available.
    • Example: @available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *).

Usage

Initialize a DNSClient, then use the query methods:

import DNSClient
import DNSModels

/// Create a `DNSClient`
let client = try DNSClient(
    transport: .default(
        serverAddress: .domain(name: "1.1.1.1", port: 53)
    )
)

try await withThrowingTaskGroup(of: Void.self) { taskGroup in
    taskGroup.addImmediateTask {
        await client.run()  /// !important
    }

    /// You can use the client while the `client.run()` method is not cancelled.

    /// Send the query
    /// `response` will be of type `Message`
    let response = try await client.queryA(
        message: .forQuery(name: "mahdibm.com")
    )

    /// Read the answers
    for answer in response.answers {
        /// `a` will be of type `A`
        let a = try answer.rdata
        /// `ipv4` will be of type `IPv4Address`
        let ipv4 = a.value
        print("Got ipv4 \(ipv4) for domain \(response.queries.first?.name.description ?? "n/a")")
    }

    /// To shutdown the client, cancel its run method, by cancelling the taskGroup.
    taskGroup.cancelAll()
}

You can use different transports if you so desire. The default transport is preferUDPOrUseTCP similar to other DNS clients and resolvers. Currently a TCP-only transport is also supported:

/// Create a `DNSClient` with the TCP transport
let client = try DNSClient(
    transport: .tcp(
        serverAddress: .domain(name: "1.1.1.1", port: 53)
    )
)

Checklist

  • DNS Parsing
    • IDNA support for non-ASCII domain names.
  • DNS client
    • DNS over UDP
    • DNS over TCP
    • DoT (DNS Over TLS)
    • DoH (DNS Over HTTPS)
    • DoQ (DNS Over Quic)
    • MDNS
  • DNS resolver (DNS client but with caching etc...)
  • DNS server
  • DNSSEC

Credits

About

A Swift DNS library built on top of SwiftNIO; aiming to provide DNS client, resolver and server implementations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •