-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSMBServer.swift
52 lines (43 loc) · 1.34 KB
/
SMBServer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// SMBServer.swift
// SMBClient
//
// Created by Seth Faxon on 9/25/17.
// Copyright © 2017 Filmic. All rights reserved.
//
import Foundation
public struct SMBServer {
public let hostname: String
public let ipAddress: UInt32
public init(hostname: String, ipAddress: UInt32) {
self.hostname = hostname
self.ipAddress = ipAddress
}
// fails initiation if ipAddress lookup fails
public init?(hostname: String) {
self.hostname = hostname
let ns = NetBIOSNameService()
if let addr = ns.resolveIPAddress(forName: self.hostname, ofType: .fileServer) {
self.ipAddress = addr
} else {
return nil
}
}
public var ipAddressString: String {
var bytes = [UInt32]()
bytes.append((self.ipAddress >> 24) & 0xFF)
bytes.append((self.ipAddress >> 16) & 0xFF)
bytes.append((self.ipAddress >> 8) & 0xFF)
bytes.append(self.ipAddress & 0xFF)
return "\(bytes[0]).\(bytes[1]).\(bytes[2]).\(bytes[3])"
}
}
extension SMBServer: CustomStringConvertible {
public var description: String {
return "\(hostname) - \(ipAddressString)"
}
}
extension SMBServer: Equatable { }
public func == (lhs: SMBServer, rhs: SMBServer) -> Bool {
return lhs.hostname == rhs.hostname && lhs.ipAddress == rhs.ipAddress
}