-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetUtilsXX.swift
157 lines (148 loc) · 5.83 KB
/
NetUtilsXX.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// NetUtils.swift
// OurglassAppSwift
//
// Created by Alyssa Torres on 3/10/16.
// Copyright © 2016 App Delegates. All rights reserved.
//
import Foundation
import SystemConfiguration.CaptiveNetwork
import PromiseKit
class NetUtilsXX : NSObject, StreamDelegate {
// MARK: PING
// var sema: dispatch_semaphore_t!
// var isError = false
// var response: AnyObject!
//
// func ping(address: String) -> Promise<AnyObject>! {
// let simpPing = SimplePing(hostName: address)
// simpPing.delegate = self
// simpPing.start()
//
// xcglog.debug("Starting ping")
//
// return Promise<AnyObject> { fulfill, reject in
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// self.sema = dispatch_semaphore_create(0)
// dispatch_semaphore_wait(self.sema, DISPATCH_TIME_FOREVER)
// if self.isError {
// reject(self.response as! ErrorType)
// } else {
// fulfill(self.response)
// }
// }
// }
// }
//
// // Stop and signal promise
// func stopPing(pinger: SimplePing, isError: Bool, response: AnyObject) {
// self.isError = isError
// self.response = response
// pinger.stop()
// dispatch_semaphore_signal(sema)
// }
//
// // Send ping immediately
// func simplePing(pinger: SimplePing, didStartWithAddress address: NSData) {
// xcglog.debug("Sent ping with data")
// pinger.sendPingWithData(nil)
// }
//
// // Failed so reject promise
// func simplePing(pinger: SimplePing, didFailWithError error: NSError) {
// self.isError = true
// self.response = error.description
// xcglog.debug(String("Ping failed with error: %@", error.description))
// stopPing(pinger, isError: true, response: error.description)
// }
//
// // Failed so reject promise
// func simplePing(pinger: SimplePing, didFailToSendPacket packet: NSData, sequenceNumber: UInt16, error: NSError) {
// xcglog.debug(String("Ping packed failed to send with error: %@", error.description))
// stopPing(pinger, isError: true, response: error.description)
// }
//
// // Got data back so fulfill promise
// func simplePing(pinger: SimplePing, didReceivePingResponsePacket packet: NSData, sequenceNumber: UInt16) {
// xcglog.debug(String("Received ping response with data: %@", packet))
// stopPing(pinger, isError: false, response: packet)
// }
//
// MARK: WIFI ADDRESS
// Get mobile device IP and netmask of connected Wi-Fi
// static func getWifiInfo() -> [String:String]? {
// var address : String!
// var netmask : String!
// var ifaddr : UnsafeMutablePointer<ifaddrs>? = nil
//
// if getifaddrs(&ifaddr) == 0 {
// var ptr = ifaddr
// while ptr != nil {
// let interface = ptr?.pointee
// if interface?.ifa_addr.pointee.sa_family == UInt8(AF_INET) {
// if let name = String.fromCString((interface?.ifa_name)!), name == "en0" {
// var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
// getnameinfo(&interface!.ifa_addr.memory, socklen_t(interface.ifa_addr.memory.sa_len), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST)
// address = String(cString: hostname)
// getnameinfo(&interface.ifa_netmask.memory, socklen_t(interface.ifa_netmask.memory.sa_len), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST)
// netmask = String(cString: hostname)
// }
// }
//
// ptr = ptr.memory.ifa_next
// }
// freeifaddrs(ifaddr)
// }
// if address == nil {
// address = "Not connected to Wi-Fi"
// }
// if netmask == nil {
// netmask = "Not connected to Wi-Fi"
// }
// var base: String!
// let baseComps = address.components(separatedBy: ".")
// if baseComps.count == 4 {
// base = baseComps[3]
// }
// if base == nil {
// base = "Not connected to Wi-Fi"
// }
// return ["ip": address, "subnet": netmask, "base": base]
// }
//
// // Get name (SSID) of connected Wi-Fi
// static func getWifiSSID() -> String? {
// #if arch(i386) || arch(x86_64)
// return "SimulatedSSID"
// #endif
// var currentSSID = ""
// if let interfaces:CFArray? = CNCopySupportedInterfaces() {
// for i in 0..<CFArrayGetCount(interfaces){
// let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
// let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
// let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
// if unsafeInterfaceData != nil {
// let interfaceData = unsafeInterfaceData! as Dictionary!
// currentSSID = interfaceData["SSID"] as! String
// }
// }
// }
// return currentSSID
// }
//
// // Get IP address of OPIE from data
// static func getIPAddress(_ address: Data) -> String? {
// var ipAddress : String?
// var sa = sockaddr()
//
// (address as NSData).getBytes(&sa, length: MemoryLayout<sockaddr>.size)
//
// if Int32(sa.sa_family) == AF_INET {
// var ip4 = sockaddr_in()
// (address as NSData).getBytes(&ip4, length: MemoryLayout<sockaddr_in>.size)
// ipAddress = String(format: "%s", inet_ntoa(ip4.sin_addr))
// }
//
// return ipAddress
// }
}