Commit 4fd98488 authored by Stefan van den Oord's avatar Stefan van den Oord

`pod update` -> NetUtils 2.0.1

parent b492a569
......@@ -10,7 +10,7 @@ target 'Pulse' do
pod 'Base32', '~> 1.0.2'
pod 'IGDigest', '~> 1.1.0'
pod 'OpenSSL-for-iOS', '~> 1.0.2.d.0'
pod 'NetUtils', '~> 1.0'
pod 'NetUtils', '~> 2.0'
end
target 'PulseTests' do
......
......@@ -9,7 +9,7 @@ PODS:
- CocoaAsyncSocket/RunLoop (7.4.3)
- IGDigest (1.1.3)
- lz4 (128)
- NetUtils (1.3.2)
- NetUtils (2.0.1)
- Nimble (3.2.0)
- OpenSSL-for-iOS (1.0.2.d.1)
- Quick (0.9.2)
......@@ -20,7 +20,7 @@ DEPENDENCIES:
- CocoaAsyncSocket (~> 7.4)
- IGDigest (~> 1.1.0)
- lz4 (~> 128)
- NetUtils (~> 1.0)
- NetUtils (~> 2.0)
- Nimble (~> 3.0)
- OpenSSL-for-iOS (~> 1.0.2.d.0)
- Quick (~> 0.6)
......@@ -31,12 +31,12 @@ SPEC CHECKSUMS:
CocoaAsyncSocket: a18c75dca4b08723628a0bacca6e94803d90be91
IGDigest: b8e27edd36468bc87951645b841531229256019a
lz4: 5e066195ca751d552becaa026f8fe213ff738adf
NetUtils: 0e2985b5cbd56824b9304a26aea4784f0927f23c
NetUtils: 5b2e30831df2e7db75962837324187a01a1c0c53
Nimble: 703854335d181df169bbca9c97117b5cf8c47c1d
OpenSSL-for-iOS: d7a30add88a1969b052135adbe48452af231ed32
Quick: 18d057bc66451eedd5d1c8dc99ba2a5db6e60226
SwiftBytes: 262e08479bc103bbb4eccd296e8b8ffb2102f7c3
PODFILE CHECKSUM: 0460f65188106d0e50e4bfb60dd99c049697f620
PODFILE CHECKSUM: 887631484d1d58efde67c525aa71dbc8fec7dc2c
COCOAPODS: 1.0.0.beta.8
......@@ -9,7 +9,7 @@ PODS:
- CocoaAsyncSocket/RunLoop (7.4.3)
- IGDigest (1.1.3)
- lz4 (128)
- NetUtils (1.3.2)
- NetUtils (2.0.1)
- Nimble (3.2.0)
- OpenSSL-for-iOS (1.0.2.d.1)
- Quick (0.9.2)
......@@ -20,7 +20,7 @@ DEPENDENCIES:
- CocoaAsyncSocket (~> 7.4)
- IGDigest (~> 1.1.0)
- lz4 (~> 128)
- NetUtils (~> 1.0)
- NetUtils (~> 2.0)
- Nimble (~> 3.0)
- OpenSSL-for-iOS (~> 1.0.2.d.0)
- Quick (~> 0.6)
......@@ -31,12 +31,12 @@ SPEC CHECKSUMS:
CocoaAsyncSocket: a18c75dca4b08723628a0bacca6e94803d90be91
IGDigest: b8e27edd36468bc87951645b841531229256019a
lz4: 5e066195ca751d552becaa026f8fe213ff738adf
NetUtils: 0e2985b5cbd56824b9304a26aea4784f0927f23c
NetUtils: 5b2e30831df2e7db75962837324187a01a1c0c53
Nimble: 703854335d181df169bbca9c97117b5cf8c47c1d
OpenSSL-for-iOS: d7a30add88a1969b052135adbe48452af231ed32
Quick: 18d057bc66451eedd5d1c8dc99ba2a5db6e60226
SwiftBytes: 262e08479bc103bbb4eccd296e8b8ffb2102f7c3
PODFILE CHECKSUM: 0460f65188106d0e50e4bfb60dd99c049697f620
PODFILE CHECKSUM: 887631484d1d58efde67c525aa71dbc8fec7dc2c
COCOAPODS: 1.0.0.beta.8
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
The MIT License (MIT) Copyright (c) 2015 Stefan van den Oord
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
......@@ -3,10 +3,28 @@
import Foundation
import ifaddrs
/**
* This class represents a network interface in your system. For example, `en0` with a certain IP address.
* It is a wrapper around the `getifaddrs` system call.
*
* Typical use of this class is to first call `Interface.allInterfaces()` and then use the properties of the interface(s) that you need.
*
* - See: `/usr/include/ifaddrs.h`
*/
public class Interface : CustomStringConvertible, CustomDebugStringConvertible {
/// The network interface family (IPv4 or IPv6).
public enum Family : Int {
case ipv4, ipv6, other
/// IPv4.
case ipv4
/// IPv6.
case ipv6
/// Used in case of errors.
case other
/// String representation of the address family.
public func toString() -> String {
switch (self) {
case .ipv4: return "IPv4"
......@@ -16,16 +34,24 @@ public class Interface : CustomStringConvertible, CustomDebugStringConvertible {
}
}
/**
* Returns all network interfaces in your system. If you have an interface name (e.g. `en0`) that has
* multiple IP addresses (e.g. one IPv4 address and a few IPv6 addresses), then they will be returned
* as separate instances of Interface.
* - Returns: An array containing all network interfaces in your system.
*/
public static func allInterfaces() -> [Interface] {
var interfaces : [Interface] = []
var ifaddrsPtr = UnsafeMutablePointer<ifaddrs>()
var ifaddrsPtr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddrsPtr) == 0 {
for (var ifaddrPtr = ifaddrsPtr; ifaddrPtr != nil; ifaddrPtr = ifaddrPtr.memory.ifa_next) {
var ifaddrPtr = ifaddrsPtr
while ifaddrPtr != nil {
let addr = ifaddrPtr.memory.ifa_addr.memory
if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
interfaces.append(Interface(data: ifaddrPtr.memory))
}
ifaddrPtr = ifaddrPtr.memory.ifa_next
}
freeifaddrs(ifaddrsPtr)
}
......@@ -34,13 +60,17 @@ public class Interface : CustomStringConvertible, CustomDebugStringConvertible {
}
/**
* Returns a new Interface instance that does not represent a real network interface, but can be used for (unit) testing.
* Returns a new Interface instance that does not represent a real network interface, but can be used for (unit) testing.
* - Returns: An instance of Interface that does *not* represent a real network interface.
*/
public static func createTestDummy(name:String, family:Family, address:String, multicastSupported:Bool, broadcastAddress:String?) -> Interface
{
return Interface(name: name, family: family, address: address, netmask: nil, running: true, up: true, loopback: false, multicastSupported: multicastSupported, broadcastAddress: broadcastAddress)
}
/**
* Initialize a new Interface with the given properties.
*/
public init(name:String, family:Family, address:String?, netmask:String?, running:Bool, up:Bool, loopback:Bool, multicastSupported:Bool, broadcastAddress:String?) {
self.name = name
self.family = family
......@@ -121,12 +151,11 @@ public class Interface : CustomStringConvertible, CustomDebugStringConvertible {
let s = String.fromCString(conversion)
return s
}
public func getName() -> String { return name }
public func getFamily() -> Family { return family }
public func getAddress() -> String? { return address }
public func getAddressBytes() -> [UInt8]? {
/**
* Creates the network format representation of the interface's IP address. Wraps `inet_pton`.
*/
public var addressBytes: [UInt8]? {
guard let addr = address else { return nil }
let af:Int32
......@@ -145,32 +174,50 @@ public class Interface : CustomStringConvertible, CustomDebugStringConvertible {
let result = inet_pton(af, addr, &bytes)
return ( result == 1 ) ? bytes : nil
}
public func getNetmask() -> String? { return netmask }
public func getBroadcastAddress() -> String? { return broadcastAddress }
public func isRunning() -> Bool { return running }
public func isUp() -> Bool { return up }
public func isLoopback() -> Bool { return loopback }
public func supportsMulticast() -> Bool { return multicastSupported }
/// `IFF_RUNNING` flag of `ifaddrs->ifa_flags`.
public var isRunning: Bool { return running }
/// `IFF_UP` flag of `ifaddrs->ifa_flags`.
public var isUp: Bool { return up }
/// `IFF_LOOPBACK` flag of `ifaddrs->ifa_flags`.
public var isLoopback: Bool { return loopback }
/// `IFF_MULTICAST` flag of `ifaddrs->ifa_flags`.
public var supportsMulticast: Bool { return multicastSupported }
private let name : String
private let family : Family
private let address : String?
private let netmask : String?
private let broadcastAddress : String?
/// Field `ifaddrs->ifa_name`.
public let name : String
/// Field `ifaddrs->ifa_addr->sa_family`.
public let family : Family
/// Extracted from `ifaddrs->ifa_addr`, supports both IPv4 and IPv6.
public let address : String?
/// Extracted from `ifaddrs->ifa_netmask`, supports both IPv4 and IPv6.
public let netmask : String?
/// Extracted from `ifaddrs->ifa_dstaddr`. Not applicable for IPv6.
public let broadcastAddress : String?
private let running : Bool
private let up : Bool
private let loopback : Bool
private let multicastSupported : Bool
public var description: String { get { return getName() } }
public var debugDescription: String { get {
var s = "Interface name:\(getName()) family:\(getFamily())"
if let ip = getAddress() {
/// Returns the interface name.
public var description: String { return name }
/// Returns a string containing a few properties of the Interface.
public var debugDescription: String {
var s = "Interface name:\(name) family:\(family)"
if let ip = address {
s += " ip:\(ip)"
}
s += isUp() ? " (up)" : " (down)"
s += isRunning() ? " (running)" : "(not running)"
s += isUp ? " (up)" : " (down)"
s += isRunning ? " (running)" : "(not running)"
return s
} }
}
}
# NetUtils for Swift [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
# NetUtils for Swift
![](https://img.shields.io/cocoapods/v/NetUtils.svg)
![](https://img.shields.io/cocoapods/l/NetUtils.svg)
![](https://img.shields.io/cocoapods/p/NetUtils.svg)
![](https://img.shields.io/cocoapods/metrics/doc-percent/NetUtils.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
Swift library that simplifies getting information about your network interfaces and their properties, both for iOS and OS X.
This library is a wrapper around the BSD APIs like getifaddrs, to make it easy to use them from Swift.
This library is a wrapper around the BSD APIs like `getifaddrs`, to make it easy to use them from Swift.
Recommended way of integrating this library is using CocoaPods: https://cocoapods.org/pods/NetUtils.
## Background Info
Some system APIs you can simply use from Swift, but others you cannot. The difference is that some are made available
as a module, and some are not. The APIs around network interfaces are not exposed as a module, which means that you
cannot use them from Swift without defining a module for them yourself. I documented this problem extensively over
here: https://ind.ie/labs/blog/using-system-headers-in-swift.
## Usage
This module contains only one class: `Interface`. This class represents a network interface. It has a static method
to list all interfaces, somewhat surprisingly called `allInterfaces()`. This will return an array of `Interface`
objects, which contain properties like their IP address, family, whether they are up and running, etc.
Please note that both IPv4 and IPv6 interfaces are supported.
## Development Info
Note for developers: `pod lib lint` will fail, because the prepare command is not run. After you push the tag
`pod spec lint` should pass.
## License
This project is released under the [MIT license](LICENSE.txt).
......@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.2</string>
<string>2.0.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
......
......@@ -36,32 +36,26 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## NetUtils
This is free and unencumbered software released into the public domain.
The MIT License (MIT) Copyright (c) 2015 Stefan van den Oord
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## OpenSSL-for-iOS
......
......@@ -55,32 +55,26 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
</dict>
<dict>
<key>FooterText</key>
<string>This is free and unencumbered software released into the public domain.
<string>The MIT License (MIT) Copyright (c) 2015 Stefan van den Oord
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to &lt;http://unlicense.org&gt;
</string>
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
<key>Title</key>
<string>NetUtils</string>
<key>Type</key>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment