From d48d6e15c703b45c67881a4bab3be17a904a56ec Mon Sep 17 00:00:00 2001
From: David Evans <d.evans@apple.com>
Date: Mon, 7 Jun 2021 16:11:38 +0100
Subject: [PATCH] Fix endianess

---
 Sources/NIOSOCKS/Messages/ClientRequest.swift      | 6 +++---
 Tests/NIOSOCKSTests/ClientRequest+Tests.swift      | 8 ++++----
 Tests/NIOSOCKSTests/SocksClientHandler+Tests.swift | 6 +++---
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/Sources/NIOSOCKS/Messages/ClientRequest.swift b/Sources/NIOSOCKS/Messages/ClientRequest.swift
index adf9fd7..f689e52 100644
--- a/Sources/NIOSOCKS/Messages/ClientRequest.swift
+++ b/Sources/NIOSOCKS/Messages/ClientRequest.swift
@@ -166,12 +166,12 @@ extension ByteBuffer {
         switch type {
         case .address(.v4(let address)):
             return self.writeInteger(UInt8(1))
-                + self.writeInteger(address.address.sin_addr.s_addr, endianness: .little)
-                + self.writeInteger(address.address.sin_port, endianness: .little)
+                + self.writeInteger(address.address.sin_addr.s_addr)
+                + self.writeInteger(address.address.sin_port)
         case .address(.v6(let address)):
             return self.writeInteger(UInt8(4))
                 + self.writeIPv6Address(address.address)
-                + self.writeInteger(address.address.sin6_port, endianness: .little)
+                + self.writeInteger(address.address.sin6_port)
         case .address(.unixDomainSocket):
             // enforced in the channel initalisers.
             fatalError("UNIX domain sockets are not supported")
diff --git a/Tests/NIOSOCKSTests/ClientRequest+Tests.swift b/Tests/NIOSOCKSTests/ClientRequest+Tests.swift
index fc646e5..efdb38e 100644
--- a/Tests/NIOSOCKSTests/ClientRequest+Tests.swift
+++ b/Tests/NIOSOCKSTests/ClientRequest+Tests.swift
@@ -29,7 +29,7 @@ extension ClientRequestTests {
         XCTAssertEqual(buffer.writeClientRequest(req), 10)
         XCTAssertEqual(buffer.readableBytes, 10)
         XCTAssertEqual(buffer.readBytes(length: 10)!,
-                       [0x05, 0x01, 0x00, 0x01, 0xC0, 0xA8, 0x01, 0x01, 0x00, 0x50])
+                       [0x05, 0x01, 0x00, 1, 1, 1, 168, 192, 0x50, 0])
     }
     
 }
@@ -58,13 +58,13 @@ extension ClientRequestTests {
     func testWriteAddressType(){
         var ipv4 = ByteBuffer()
         XCTAssertEqual(ipv4.writeAddressType(.address(try! .init(ipAddress: "192.168.1.1", port: 80))), 7)
-        XCTAssertEqual(ipv4.readBytes(length: 5)!, [1, 192, 168, 1, 1])
-        XCTAssertEqual(ipv4.readInteger(as: UInt16.self)!, 80)
+        XCTAssertEqual(ipv4.readBytes(length: 5)!, [1, 1, 1, 168, 192])
+        XCTAssertEqual(ipv4.readInteger(as: UInt16.self)!, 0x5000)
         
         var ipv6 = ByteBuffer()
         XCTAssertEqual(ipv6.writeAddressType(.address(try! .init(ipAddress: "0001:0002:0003:0004:0005:0006:0007:0008", port: 80))), 19)
         XCTAssertEqual(ipv6.readBytes(length: 17)!, [4, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8])
-        XCTAssertEqual(ipv6.readInteger(as: UInt16.self)!, 80)
+        XCTAssertEqual(ipv6.readInteger(as: UInt16.self)!, 0x5000)
     }
     
 }
diff --git a/Tests/NIOSOCKSTests/SocksClientHandler+Tests.swift b/Tests/NIOSOCKSTests/SocksClientHandler+Tests.swift
index d476687..0c9105d 100644
--- a/Tests/NIOSOCKSTests/SocksClientHandler+Tests.swift
+++ b/Tests/NIOSOCKSTests/SocksClientHandler+Tests.swift
@@ -59,7 +59,7 @@ class SocksClientHandlerTests: XCTestCase {
         self.writeInbound([0x05, 0x00])
         
         // client sends the request
-        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 192, 168, 1, 1, 0x00, 0x50])
+        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 1, 1, 168, 192, 0x50, 0x00])
         
         // server replies yay or nay
         self.writeInbound([0x05, 0x00, 0x00, 0x01, 192, 168, 1, 1, 0x00, 0x50])
@@ -80,7 +80,7 @@ class SocksClientHandlerTests: XCTestCase {
         self.writeInbound([0x05])
         self.assertOutputBuffer([])
         self.writeInbound([0x00])
-        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 192, 168, 1, 1, 0x00, 0x50])
+        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 1, 1, 168, 192, 0x50, 0x00])
         
         // drip feed server response
         self.writeInbound([0x05, 0x00, 0x00, 0x01])
@@ -142,7 +142,7 @@ class SocksClientHandlerTests: XCTestCase {
         // start handshake, send request
         self.assertOutputBuffer([0x05, 0x01, 0x00])
         self.writeInbound([0x05, 0x00])
-        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 192, 168, 1, 1, 0x00, 0x50])
+        self.assertOutputBuffer([0x05, 0x01, 0x00, 0x01, 1, 1, 168, 192, 0x50, 0x00])
         
         // server replies with an error
         let promise = self.channel.eventLoop.makePromise(of: Void.self)