I am a bit shocked to see that my connector that still worked on iOS 17 doesn't want to connect to a local network host on iOS 18 anymore. To give you some context:
- The code is part of a swift package
- I declared NSLocalNetworkUsageDescription in Info.plist (of the app that loads the swift package), and the location permission dialog also pops up at the first start of the app.
- I tried to tunnel traffic over ngrok and that works, so it's definitely an issue related to LOCAL network addresses
- I tried the same code on iOS 17 without ngrok and it works, so it's definitely also related to the iOS version
This is the code, any ideas are very welcome:
- (int)connectAddr:(struct sockaddr *)remoteAddr {
double startTime = CFAbsoluteTimeGetCurrent();
int remoteSocket = 0;
while (CFAbsoluteTimeGetCurrent() - startTime < 10.0) {
if ((remoteSocket = socket(remoteAddr->sa_family, SOCK_STREAM, 0)) < 0) {
Log(self, @"Could not open socket for injection: %s", strerror(errno));
[InfoMessages.shared onError];
usleep(500000); // wait 0.5 sec before retrying
continue;
}
if (setsockopt(remoteSocket, IPPROTO_TCP, TCP_NODELAY, (void *)&(int){1}, sizeof(int)) < 0) {
Log(self, @"Could not set TCP_NODELAY: %s", strerror(errno));
close(remoteSocket);
[InfoMessages.shared onError];
usleep(500000);
continue;
}
// Log remoteAddr details
if (remoteAddr->sa_family == AF_INET) {
struct sockaddr_in *addr_in = (struct sockaddr_in *)remoteAddr;
Log(self, @"remoteAddr: family=%d, port=%d, addr=%s",
addr_in->sin_family, ntohs(addr_in->sin_port), inet_ntoa(addr_in->sin_addr));
} else {
Log(self, @"remoteAddr: Unsupported family %d", remoteAddr->sa_family);
}
Log(self, @"Try to connect ttt");
if (connect(remoteSocket, remoteAddr, remoteAddr->sa_len) >= 0){
Log(self, @"Connected!");
isConnectedWithHost = true;
[InfoMessages.shared onConnect];
return remoteSocket;
}
Log(self, @"Could not connect: %s", strerror(errno));
close(remoteSocket);
[InfoMessages.shared onError];
usleep(500000);
}
return 0;
}