-
Notifications
You must be signed in to change notification settings - Fork 687
Create SocketAddress from packed byte representation #1692
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
Create SocketAddress from packed byte representation #1692
Conversation
…ion. Motivation: A user should be able to create SocketAddress from packed bytes representation. Modifications: I added a new SocketAddress initializer which takes the IP address in ByteBuffer form. I have also added tests that ensure the initializer works properly. Result: We have a new way to initialize a SocketAddress from a byteBuffer.
Can one of the admins verify this patch? |
10 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
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.
Thanks for this patch!
As a quick note, we don't want to construct this from a packed SOCKS-formatted data structure, we'd like to construct it from just the raw address bytes themselves. The SOCKS formatting is necessary for the other use-case, but is not necessary for NIO more generically.
Motivation: We should be able to create SocketAddress from just the raw address bytes and not from packed SOCKS-formatted data structure. Modifications: Remove SocketAddress construction from packed SOCKS-formatted data structure. Result: Construct SocketAddress from just the raw address bytes.
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.
This looks really good! I have two minor notes and then I think this should be good to go.
Please also run the scripts/generate_linux_tests.rb
script to add the extra hooks for testing on Linux.
Sources/NIO/SocketAddresses.swift
Outdated
/// - ipAddress: The IP address, in ByteBuffer form. | ||
/// - port: The target port. | ||
/// - returns: the `SocketAddress` corresponding to this string and port combination. | ||
/// - throws: may throw `SocketAddressError.failedToParseIPByteBuffer` if the IP address cannot be parsed or `SocketAddressError.unsupported` if the ATYP is wrong. |
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.
We aren't using an ATYP
here and can never throw unsupported
. Can we update this throws message?
Sources/NIO/SocketAddresses.swift
Outdated
/// - port: The target port. | ||
/// - returns: the `SocketAddress` corresponding to this string and port combination. | ||
/// - throws: may throw `SocketAddressError.failedToParseIPByteBuffer` if the IP address cannot be parsed or `SocketAddressError.unsupported` if the ATYP is wrong. | ||
public init(ipAddress: ByteBuffer, port: Int) throws { |
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.
One minor nit: can we change the argument to packedIpAddress
? This will give us a slightly more descriptive name, and it'll avoid a type-based overload and replace it with a label-based overload.
Motivation: Make the code more consistent and fix some typos. Modifications: Renamed ipAddress into packedIpAddress. Added extra hooks for testing on Linux. Result: Some typos and refactoring changes.
Sources/NIO/SocketAddresses.swift
Outdated
@@ -38,6 +38,8 @@ public enum SocketAddressError: Error { | |||
case unixDomainSocketPathTooLong | |||
/// Unable to parse a given IP string | |||
case failedToParseIPString(String) | |||
/// Unable to parse a given IP ByteBuffer | |||
case failedToParseIPByteBuffer(ByteBuffer) |
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.
I'm afraid we can't add new cases to enumerations: doing so is a Semver major change. Can we add a new struct
to represent this error instead?
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.
I'm having trouble implementing the struct
.
Could you enlighten me on this point ?
This is what i did :
public enum SocketAddressError: Error {
/// The host is unknown (could not be resolved).
case unknown(host: String, port: Int)
/// The requested `SocketAddress` is not supported.
case unsupported
/// The requested UDS path is too long.
case unixDomainSocketPathTooLong
/// Unable to parse a given IP string
case failedToParseIPString(String)
/// Unable to parse a given IP ByteBuffer
struct failedToParseIPByteBuffer: Error {
let address: ByteBuffer
}
}
// ...
throw SocketAddressError.failedToParseIPByteBuffer(address: packedIpAddress)
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.
extension SocketAddressError {
public struct FailedToParseIPByteBuffer: Error, Hashable {
public var address: ByteBuffer
public init(address: ByteBuffer) {
self.address = address
}
}
}
throw SocketAddressError.FailedToParseIPByteBuffer(address: packedIpAddress)
Motivation: Remove failedToParseIPByteBuffer from SocketAddressError enum as it forces Semver major change. Modifications: Add a new struct to represent the failedToParseIPByteBuffer error. Result: failedToParseIPByteBuffer error extracted into a struct.
@Lukasa Does the code look good to you or do you want me to make other changes ? |
@swift-nio-bot test this please |
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.
Great, this looks really good to me. Well done @ayshiff!
Motivation: A new init was added to `SocketAddress` in apple#1692, but the casing of `packedIpAddress` is incorrect, it should be `packedIPAddress`. This hasn't been released yet so let's fix it while we still can! Modifications: - s/packedIpAddress/packedIPAddress Result: More consistent API
Motivation: A new init was added to `SocketAddress` in #1692, but the casing of `packedIpAddress` is incorrect, it should be `packedIPAddress`. This hasn't been released yet so let's fix it while we still can! Modifications: - s/packedIpAddress/packedIPAddress Result: More consistent API
Closes #1649
Motivation:
A user should be able to create SocketAddress from packed bytes representation.
Modifications:
I added a new SocketAddress initializer which takes the IP address in ByteBuffer form.
I have also added tests that ensure the initializer works properly.
ByteBuffer inputs should be in the SOCKS representation:
As mentioned in #1648.
ATYP address type of following address:
Address field of following length:
Implemented address types:
I don't know if I followed the correct method and if there are scenarios that I haven't covered (for example if we want to initialize the IP address as a packed byte representation but the port as an Integer).
I don't have much knowledge on the subject and I would like to have your opinion on this PR !
Result:
We have a new way to initialize a SocketAddress from a byteBuffer.