gpt4 book ai didi

ios - 如何快速获取本地和子网掩码IP地址

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:03 24 4
gpt4 key购买 nike

我需要使用 Swift 代码获取本地 IP 地址和子网掩码。

请帮帮我。我有一些代码在 Objective-C 中工作。如何在 Swift 中获取等效代码?

下面我添加了 Objective-C 代码:

+(NSDictionary *)getLocalIPAddress{ 
NSDictionary *dictIPDetails;
NSString *address = @"error";
NSString *netmask = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);

if (success == 0)
{
temp_addr = interfaces;

while(temp_addr != NULL)
{
// check if interface is en0 which is the wifi connection on the iPhone
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
{
address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
netmask = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr));
dictIPDetails = @{LOCAL_IP_ADDR:address,SUBNET_MASK:netmask};
}
}

temp_addr = temp_addr->ifa_next;
}
}

freeifaddrs(interfaces);

return dictIPDetails;
}

最佳答案

我修改了 here 的答案包括网络掩码和 ip。

使用下面的代码你可以做这样的事情:

  let ip = getIFAddresses().last!.ip
let netmask = getIFAddresses().last!.netmask

希望这对您有所帮助。

  struct NetInfo {
let ip: String
let netmask: String
}

// Get the local ip addresses used by this node
func getIFAddresses() -> [NetInfo] {
var addresses = [NetInfo]()

// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddr) == 0 {

// For each interface ...
for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
let flags = Int32(ptr.memory.ifa_flags)
var addr = ptr.memory.ifa_addr.memory

// Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {

// Convert interface address to a human readable string:
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
if let address = String.fromCString(hostname) {

var net = ptr.memory.ifa_netmask.memory
var netmaskName = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
getnameinfo(&net, socklen_t(net.sa_len), &netmaskName, socklen_t(netmaskName.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0
if let netmask = String.fromCString(netmaskName) {
addresses.append(NetInfo(ip: address, netmask: netmask))
}
}
}
}
}
}
freeifaddrs(ifaddr)
}
return addresses
}

如果您需要更多功能,我有这个版本的 NetInfo,它还会计算您的网络和广播地址。

struct NetInfo {
// IP Address
let ip: String

// Netmask Address
let netmask: String

// CIDR: Classless Inter-Domain Routing
var cidr: Int {
var cidr = 0
for number in binaryRepresentation(netmask) {
let numberOfOnes = number.componentsSeparatedByString("1").count - 1
cidr += numberOfOnes
}
return cidr
}

// Network Address
var network: String {
return bitwise(&, net1: ip, net2: netmask)
}

// Broadcast Address
var broadcast: String {
let inverted_netmask = bitwise(~, net1: netmask)
let broadcast = bitwise(|, net1: network, net2: inverted_netmask)
return broadcast
}

private func binaryRepresentation(s: String) -> [String] {
var result: [String] = []
for numbers in (split(s) {$0 == "."}) {
if let intNumber = numbers.toInt() {
if let binary = String(intNumber, radix: 2).toInt() {
result.append(NSString(format: "%08d", binary) as String)
}
}
}
return result
}

private func bitwise(op: (UInt8,UInt8) -> UInt8, net1: String, net2: String) -> String {
let net1numbers = toInts(net1)
let net2numbers = toInts(net2)
var result = ""
for i in 0..<net1numbers.count {
result += "\(op(net1numbers[i],net2numbers[i]))"
if i < (net1numbers.count-1) {
result += "."
}
}
return result
}

private func bitwise(op: UInt8 -> UInt8, net1: String) -> String {
let net1numbers = toInts(net1)
var result = ""
for i in 0..<net1numbers.count {
result += "\(op(net1numbers[i]))"
if i < (net1numbers.count-1) {
result += "."
}
}
return result
}

private func toInts(networkString: String) -> [UInt8] {
return (split(networkString){$0 == "."}).map{UInt8($0.toInt()!)}
}
}

关于ios - 如何快速获取本地和子网掩码IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com