Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kishikawakatsumi committed Jul 27, 2024
1 parent 6ff42dd commit 449f8f0
Show file tree
Hide file tree
Showing 12 changed files with 612 additions and 318 deletions.
22 changes: 11 additions & 11 deletions Sources/SMBClient/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ public class Connection {
repeat {
header = reader.read()

switch header.status {
switch NTStatus(header.status) {
case
NTStatus.success,
NTStatus.moreProcessingRequired,
NTStatus.noMoreFiles,
NTStatus.endOfFile:
.success,
.moreProcessingRequired,
.noMoreFiles,
.endOfFile:
response += data
case NTStatus.pending:
case .pending:
if self.buffer.count > 0 {
let transportPacket = DirectTCPPacket(response: self.buffer)
let length = Int(transportPacket.protocolLength)
Expand All @@ -181,12 +181,12 @@ public class Connection {
let reader = ByteReader(data)
let header: Header = reader.read()

switch header.status {
switch NTStatus(header.status) {
case
NTStatus.success,
NTStatus.moreProcessingRequired,
NTStatus.noMoreFiles,
NTStatus.endOfFile:
.success,
.moreProcessingRequired,
.noMoreFiles,
.endOfFile:
response += data
break
default:
Expand Down
4 changes: 2 additions & 2 deletions Sources/SMBClient/FileReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class FileReader {
)

buffer.append(response.buffer)
} while response.header.status != NTStatus.endOfFile && buffer.count < length && offset + UInt64(buffer.count) < fileProxy.size
} while NTStatus(response.header.status) != .endOfFile && buffer.count < length && offset + UInt64(buffer.count) < fileProxy.size

return buffer
}
Expand All @@ -57,7 +57,7 @@ public class FileReader {

buffer.append(response.buffer)
offset = UInt64(buffer.count)
} while response.header.status != NTStatus.endOfFile && buffer.count < fileProxy.size
} while NTStatus(response.header.status) != .endOfFile && buffer.count < fileProxy.size

return buffer
}
Expand Down
130 changes: 0 additions & 130 deletions Sources/SMBClient/Messages/ErrorCodes.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/SMBClient/Messages/ErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct ErrorResponse: Error {

extension ErrorResponse: CustomStringConvertible {
public var description: String {
return ErrorCodes.description(header.status)
return NTStatus(header.status).description
}
}

Expand Down
113 changes: 113 additions & 0 deletions Sources/SMBClient/Messages/Header+CustomDebugStringConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import Foundation

extension Header: CustomDebugStringConvertible {
public var debugDescription: String {
if flags.contains(.serverToRedir) {
return
"""
SMB2 Header
ProtocolId: \(String(format: "0x%08x", protocolId.bigEndian))
Header Length: \(structureSize)
Credit Charge: \(creditCharge)
NT Status: \(NTStatus(status).debugDescription) (\(String(format: "0x%08x", status)))
Command: \(Command.debugDescription(rawValue: command)) (\(command))
Credits granted: \(creditRequestResponse)
Flags: \(flags)
Chain Offset: \(nextCommand)
Message ID: \(String(messageId, radix: 16))
Process Id: \(String(format: "0x%08x", reserved))
Tree Id: \(String(format: "0x%08x", treeId))
Session Id: \(String(format: "0x%016llx", sessionId))
Signature: \(signature.hex)
"""
} else {
return
"""
SMB2 Header
ProtocolId: \(String(format: "0x%08x", protocolId.bigEndian))
Header Length: \(structureSize)
Credit Charge: \(creditCharge)
Channel Sequence: \(String((status & 0xFFFF0000) >> 16, radix: 16))
Reserved: \(String(format: "%04x", status & 0x0000FFFF))
Command: \(Command.debugDescription(rawValue: command)) (\(command))
Credits requested: \(creditRequestResponse)
Flags: \(flags)
Chain Offset: \(nextCommand)
Message ID: \(String(messageId, radix: 16))
Process Id: \(String(format: "0x%08x", reserved))
Tree Id: \(String(format: "0x%08x", treeId))
Session Id: \(String(format: "0x%016llx", sessionId))
Signature: \(signature.hex)
"""
}
}
}

extension Header.Command: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .negotiate: return "Negotiate Protocol"
case .sessionSetup: return "SESSION_SETUP"
case .logoff: return "LOGOFF"
case .treeConnect: return "TREE_CONNECT"
case .treeDisconnect: return "TREE_DISCONNECT"
case .create: return "CREATE"
case .close: return "CLOSE"
case .flush: return "FLUSH"
case .read: return "READ"
case .write: return "WRITE"
case .lock: return "LOCK"
case .ioctl: return "IOCTL"
case .cancel: return "CANCEL"
case .echo: return "ECHO"
case .queryDirectory: return "QUERY_DIRECTORY"
case .changeNotify: return "CHANGE_NOTIFY"
case .queryInfo: return "QUERY_INFO"
case .setInfo: return "SET_INFO"
case .oplockBreak: return "OPLOCK_BREAK"
case .serverToClientNotification: return "SERVER_TO_CLIENT_NOTIFICATION"
}
}

static public func debugDescription(rawValue: UInt16) -> String {
if let command = Header.Command(rawValue: rawValue) {
return command.debugDescription
} else {
return String(format: "0x%04x", rawValue)
}
}
}

extension Header.Flags: CustomDebugStringConvertible {
public var debugDescription: String {
var values: [String] = []

if contains(.serverToRedir) {
values.append("Response")
}
if contains(.asyncCommand) {
values.append("Async command")
}
if contains(.relatedOperations) {
values.append("Chained")
}
if contains(.signed) {
values.append("Signing")
}
if contains(.priorityMask) {
values.append("Priority")
}
if contains(.dfsOperation) {
values.append("DFS operation")
}
if contains(.replayOperation) {
values.append("Replay operation")
}

if values.isEmpty {
return String(format: "0x%08x", rawValue)
} else {
return "\(String(format: "0x%08x", rawValue)) (\(values.joined(separator: ", ")))"
}
}
}
Loading

0 comments on commit 449f8f0

Please sign in to comment.