Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get local IP address #209

Open
grill2010 opened this issue Feb 23, 2022 · 1 comment
Open

Get local IP address #209

grill2010 opened this issue Feb 23, 2022 · 1 comment

Comments

@grill2010
Copy link

Is is somehow possible with this library to get the local IP address to which the socket is currently bound? E.g. I have following Android Java code which I need to translate to Swift

private static InetAddress getOutboundAddress(SocketAddress remoteAddress) {
        InetAddress localAddress = null;
        try {
            DatagramSocket sock = new DatagramSocket();
            // connect is needed to bind the socket and retrieve the local address later (it would return 0.0.0.0 otherwise)
            sock.connect(remoteAddress);
            localAddress = sock.getLocalAddress();

            sock.disconnect();
            sock.close();
        } catch (Exception e) {
            // ignore
        }

        return localAddress;
}

is there a way which is similar to localAddress = sock.getLocalAddress();?

@grill2010
Copy link
Author

grill2010 commented Feb 24, 2022

I think I found a way. I don't think there is another way of how you can get the local bound IP address, if there is please let me know. This is the solution I use currently

private static func getOutboundAddress(hostname: String, port: Int32) -> String {
        var tmpDatagramSocket: Socket?
        defer {
            tmpDatagramSocket?.close()
        }
        
        var result: String = ""
        do {
            tmpDatagramSocket = try Socket.create(family: .inet, type: .datagram, proto: .udp)
            try tmpDatagramSocket!.connect(to: hostname, port: port)
            
            var addr_in = sockaddr_in()
            addr_in.sin_len = UInt8(MemoryLayout.size(ofValue: addr_in))
            addr_in.sin_family = sa_family_t(AF_INET)
            var len = socklen_t(addr_in.sin_len)
            
            _ = withUnsafeMutablePointer(to: &addr_in, {
                $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
                    getsockname(tmpDatagramSocket!.socketfd, $0, &len)
                }
            })
            
            let address = Socket.Address.ipv4(addr_in)
            if let hostNameAndPort = Socket.hostnameAndPort(from: address) {
                result = hostNameAndPort.hostname
            }
        } catch {
            // ignore
        }
        return result
}

Maybe this could be added to the library in some way so that a bound address can be accessed easier?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant