-
-
Notifications
You must be signed in to change notification settings - Fork 665
[WIP] Added IPv6 support in the SRT protocol. #1842
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ final actor SRTSocket { | |
|
|
||
| enum Error: Swift.Error { | ||
| case notConnected | ||
| case invalidArgument(_ message: String) | ||
| case rejected(_ reason: SRTRejectReason) | ||
| case illegalState(_ message: String) | ||
| } | ||
|
|
@@ -128,28 +129,42 @@ final actor SRTSocket { | |
| let status: Int32 = try { | ||
| switch url.mode { | ||
| case .caller: | ||
| guard var remote = url.remote else { | ||
| return SRT_ERROR | ||
| guard let remote = url.remote else { | ||
| throw Error.invalidArgument("missing remote url") | ||
| } | ||
| return try remote.resolve(AI_ADDRCONFIG) { name, length in | ||
| srt_connect(socket, name, length) | ||
| } | ||
| var remoteaddr = remote.makeSockaddr() | ||
| return srt_connect(socket, &remoteaddr, Int32(remote.size)) | ||
| case .listener: | ||
| guard var local = url.local else { | ||
| return SRT_ERROR | ||
| guard let local = url.local else { | ||
| throw Error.invalidArgument("missing local url") | ||
| } | ||
| var localaddr = local.makeSockaddr() | ||
| let status = srt_bind(socket, &localaddr, Int32(local.size)) | ||
| guard status != SRT_ERROR else { | ||
| throw makeSocketError() | ||
| let _: Int32 = try local.resolve(AI_PASSIVE) { name, length in | ||
| let status = srt_bind(socket, name, length) | ||
| if status == SRT_ERROR { | ||
| return nil | ||
| } else { | ||
| return status | ||
| } | ||
| } | ||
| return srt_listen(socket, 1) | ||
| case .rendezvous: | ||
| guard var remote = url.remote, var local = url.local else { | ||
| return SRT_ERROR | ||
| guard let remote = url.remote else { | ||
| throw Error.invalidArgument("missing remote url") | ||
| } | ||
| guard let local = url.local else { | ||
| throw Error.invalidArgument("missing local url") | ||
| } | ||
| return try remote.resolve(AI_PASSIVE | AI_ADDRCONFIG) { remotename, remotelen in | ||
| return try local.resolve(AI_PASSIVE | AI_ADDRCONFIG) { localname, locallen in | ||
| let status = srt_rendezvous(socket, localname, locallen, remotename, remotelen) | ||
|
Comment on lines
+159
to
+160
|
||
| if status == SRT_ERROR { | ||
| return nil | ||
| } else { | ||
| return status | ||
| } | ||
| } | ||
| } | ||
| var remoteaddr = remote.makeSockaddr() | ||
| var localaddr = local.makeSockaddr() | ||
| return srt_rendezvous(socket, &remoteaddr, Int32(remote.size), &localaddr, Int32(local.size)) | ||
| } | ||
| }() | ||
| guard status != SRT_ERROR else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||
| import Foundation | ||||||||||||||||||||||||||||||||||||||||
| import libsrt | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| struct AddrInfo { | ||||||||||||||||||||||||||||||||||||||||
| enum Error: Swift.Error { | ||||||||||||||||||||||||||||||||||||||||
| case failedToGetaddrinfo(_ code: Int) | ||||||||||||||||||||||||||||||||||||||||
| case failedToResolve | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let host: String | ||||||||||||||||||||||||||||||||||||||||
| let port: Int | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| @discardableResult | ||||||||||||||||||||||||||||||||||||||||
| func resolve<R>(_ flags: Int32, lambda: (UnsafePointer<sockaddr>, Int32) throws -> R?) throws -> R { | ||||||||||||||||||||||||||||||||||||||||
| var hints = addrinfo( | ||||||||||||||||||||||||||||||||||||||||
| ai_flags: flags, | ||||||||||||||||||||||||||||||||||||||||
| ai_family: AF_UNSPEC, | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+17
|
||||||||||||||||||||||||||||||||||||||||
| @discardableResult | |
| func resolve<R>(_ flags: Int32, lambda: (UnsafePointer<sockaddr>, Int32) throws -> R?) throws -> R { | |
| var hints = addrinfo( | |
| ai_flags: flags, | |
| ai_family: AF_UNSPEC, | |
| /** | |
| Resolves the host and port to a socket address. | |
| - Parameters: | |
| - flags: Flags for address resolution. | |
| - family: Protocol family to use (AF_INET for IPv4, AF_INET6 for IPv6, AF_UNSPEC for both). Defaults to AF_UNSPEC. | |
| - lambda: Closure to process each resolved address. | |
| - Returns: The result of the closure for the first matching address. | |
| - Throws: Error.failedToGetaddrinfo or Error.failedToResolve. | |
| */ | |
| @discardableResult | |
| func resolve<R>(_ flags: Int32, family: Int32 = AF_UNSPEC, lambda: (UnsafePointer<sockaddr>, Int32) throws -> R?) throws -> R { | |
| var hints = addrinfo( | |
| ai_flags: flags, | |
| ai_family: family, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
AI_PASSIVEflag for remote address resolution in rendezvous mode is incorrect.AI_PASSIVEis intended for addresses that will be used withbind(), notconnect()or rendezvous connections. The remote address should useAI_ADDRCONFIGonly, while only the local address should useAI_PASSIVE.