We are trying to create a local HTTP server on iOS that receives a redirect request from a remote server. The request is processed by the local server and an HTTP response is returned. The local server runs on a port 9078 and our requirement is bind the local server to the loopback address 127.50.100.1. But when trying to bind the server to an address other than 127.0.0.1, it isn't working as expected and it throws an error something like address can't be used. Is there any way that we could bind the local server to a loopback address that's other than localhost / 127.0.0.1?
We tried working with different HTTP server libraries like Vapor, CocoaHTTP, SwiftNIO, GCD Webserver and none of them seem to support this behavior. Any leads on this would be very helpful. TIA!
the application server will always redirect to this loopback address
I hate to be the bearer of bad news but you’re going to have to change that code. Consider this snippet:
int sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock >= 0);
struct sockaddr_in addr = {};
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
addr.sin_addr.s_addr = htonl(0x7f000001);
NSLog(@"will bind");
BOOL success = bind(sock, (const struct sockaddr *) &addr, sizeof(addr)) >= 0;
if (success) {
NSLog(@"did bind");
} else {
NSLog(@"did not bind, error: %d", errno);
}
success = close(sock) >= 0;
assert(success);
It prints [1]:
will bind
did bind
If you change the 0x7f000001 to 0x7f000002, so you’re binding to 127.0.0.2, it prints:
will bind
did not bind, error: 49
where 49 is EADDRNOTAVAIL.
iOS simply does not support this.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] I’m testing on iOS 16.3.1 but I believe that this isn’t new behaviour.