Home > Blockchain >  Cocoa: How to get broadcast IP address in Swift?
Cocoa: How to get broadcast IP address in Swift?

Time:10-24

Hi I am able to get local network IP address using this solution. I tried to use this solution to get broadcast IP address by using this Objective-C solution.

static func getBroadCastAddress() -> String? {
        var address: String?
            var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
            if getifaddrs(&ifaddr) == 0 {
                var ptr = ifaddr
                while ptr != nil {
                    defer { ptr = ptr?.pointee.ifa_next }

                    guard let interface = ptr?.pointee else { return "" }
                    let addrFamily = interface.ifa_addr.pointee.sa_family
                    if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
 
                        let name: String = String(cString: (interface.ifa_name))
                        if  name == "en0" || name == "en2" || name == "en3" || name == "en4" || name == "pdp_ip0" || name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3" {
                            var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                            getnameinfo(interface.ifa_addr, socklen_t((interface.ifa_addr.pointee.sa_len)), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
                            
//                            inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)
                             
                            if let sinAddress = (interface.ifa_dstaddr as? sockaddr_in)?.sin_addr{ //=> It's always fail
                                
                                
                                address = String(utf8String: inet_ntoa(sinAddress))
                            }
                            
                        }
                    }
                }
                freeifaddrs(ifaddr)
            }
            return address ?? ""
    }

The problem is that I can't get sin_addr from interface like inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr) in objective c code.

Can anyone help me to get broadcast IP address in Cocoa using Swift? Thank you for your time.

CodePudding user response:

let acceptedInterfaces = ["en0", "en2", "en3", "en4", "pdp_ip0", "pdp_ip1", "pdp_ip2", "pdp_ip3"]
func getBroadCastAddress() {
    var interfaceAddresses : [String : String] = [:]
    var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil

    if getifaddrs(&ifaddr) == 0 {
        defer { freeifaddrs(ifaddr) }

        var currentInterface = ifaddr
        while currentInterface != nil {
            defer { currentInterface = currentInterface!.pointee.ifa_next }

            let interface = currentInterface!.pointee
            let addrFamily = interface.ifa_addr.pointee.sa_family
            guard addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) else { continue }

            let interfaceName = String(cString: interface.ifa_name)
            if acceptedInterfaces.contains(interfaceName) {
                currentInterface!.withMemoryRebound(to: sockaddr_in.self, capacity: 1) {
                    sockaddr in

                    if let addressCString = inet_ntoa(sockaddr.pointee.sin_addr) {
                        interfaceAddresses[interfaceName] = String(cString: addressCString)
                    }
                }
            }
        }
    }

    debugPrint(interfaceAddresses)
}

it prints:

["en0": "194.127.0.0"]
  • Related