-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Cannot Send to a broadcastable socket on iOS 17 device #106588
Comments
Update, I was mistaken about one of my phones: |
@amp64 could you please share what exact version of .NET 8 are using ( I tried you repro locally with latest .NET 8 servicing release (8.0.8), iPhone 11 with iOS 17.5.1 and the code worked correctly (i.e., it reported "Bound ok") |
Dumb question but how do I determine the exact version of dotnet on my iPhone? dotnet --version on my PC says 8.0.6, my PC has the 8.0.302 NET SDK from VS installed. I am using Avalonia 11.1.2 to x-target iOS. In the logs the closest I see to a version is Microsoft.iOS.Sdk/17.2.8053 In Solution Explorer if I click under Frameworks I see 8.0.624.26715, so it seems like I need to update this. |
Sorry I have done a poor job with the repro here. I was thinking the error was from the Bind, but its actually in the subsequent SendTo. Add this:
|
Not a dumb question at all. Running |
No problem, could you please update the repro example and I'll check it out. |
I updated the description and the repro to improve usefulness and accuracy. |
I'm marking these as 10.0 - as the priority is not that high. Once we figure out the real issue we can consider backporting to 9 as necessary. |
Can confirm that the new repro reproduces inside |
Any fix in sight or this? Is it fixed in .net 9? Sucks to be dealing with this. |
@Spoxeman there haven't been any progress on this issue yet. I just tried .NET 9 iOS sample app and it still fails on iOS 17.5.1 device but seems like it is working fine on iOS 18.2 simulator (don't have device to confirm). |
@matouskozak it looks like it’s definitely still broken on physical device too. I did a lot of testing the other day. What I know for sure is if you build the app using an older version of Xcode like 15.x the issue disappears. Problem with this is by April 24 Apple will reject any apps built with older versions of Xcode so where do we go from there ? |
@rolfbjarne are you aware of any Xcode related changes that would be causing this regression on our side? |
Yes, this is a change in iOS 16+. Please read dotnet/macios#21814, where the customer talks to Apple engineers about this exact problem in https://developer.apple.com/forums//thread/771139. One thing that complicates diagnosing, is that broadcasting/multicasting requires a specific entitlement (https://developer.apple.com/documentation/bundleresources/entitlements/com.apple.developer.networking.multicast?language=objc) - which requires permission from Apple before you get it. I've kind of recently looked into this, and from a managed perspective there are a couple of annoyances:
I wasn't able to figure out what the problem was though. |
I got this to work, and this is required:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.multicast</key>
<true />
</dict>
</plist>
<key>NSLocalNetworkUsageDescription</key>
<string>Hello local network!</string>
var sendbuf = new byte [16];
using var udpClient = new UdpClient (new IPEndPoint(IPAddress.Any, 12345));
udpClient.Send (sendbuf, "255.255.255.255", 12345);
Console.WriteLine ($"UDP message sent"); |
Here's the test project I used: and I run it like this:
hitting the button that shows up, yields this in the console:
You'll have to update a few things first in the sample though:
|
You have got to be kidding me! NSLocalNetworkUsageDescription @rolfbjarne just curious, was there particular documentation you reviewed to find this out? |
Heh yeah, I read a lot of documentation from Apple. I started here: https://developer.apple.com/forums//thread/771139 and then followed pretty much every link that looked remotely relevant:
and a bunch more. The last one has this tiny bit of relevant info:
but since it didn't sound all that important, I ignored it at first since I was just doing a simple debug app, especially because I got the privacy prompt once... but never again, no matter what I did. Eventually (after quite a few hours of scratching my head, and trying different things, with multiple phones, almost wiped a phone...) I tried adding the usage description. The privacy prompt promptly showed up, and everything worked like a charm from then on! |
I guess we can close this then, since it's working for you now? |
I have no objections to closing it. Thanks for your help.
|
My app has the changes quoted by @rolfbjarne and the MC entitlement but, when I opened this bug, they did not help. However my Mac is dead right now so I cannot verify this as I cannot build any iOS code. I am nervous about closing this code without someone else verifying this flag is not in fact required on a broadcast socket. @rolfbjarne if you change multicast to true in my repro, does that work for you also? |
I added this code to my test app: static void TestBroadcast(bool multicast)
{
try
{
IPAddress localIP;
using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, 0)) {
socket.Connect ("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address;
}
using (var socket = new Socket (localIP.AddressFamily, SocketType.Dgram, ProtocolType.Udp)) {
if (!multicast)
socket.EnableBroadcast = true;
socket.ExclusiveAddressUse = false;
var dest = new IPEndPoint (IPAddress.Broadcast, 1900);
socket.Bind (new IPEndPoint (localIP, 0));
Console.WriteLine ($"Test-Bound ok to {localIP}");
var bytes = new ArraySegment<byte> (new byte[1]);
socket.SendTo (bytes, SocketFlags.None, dest);
Console.WriteLine ($"TestBroadcast({multicast}): Success!");
}
} catch (Exception ex) {
Console.WriteLine ("TestBroadcast Exception: " + ex);
}
} and called it like this: TestBroadcast (false);
TestBroadcast (true); and it printed this:
This doesn't look like the other error though, so it might be something wrong with the sample code. |
Description
If you try and Send a Broadcast to a socket it will fail on iOS 17 devices with the error "no route to host". I believe this is due to the flag IP_BOUND_IF missing, described here: https://developer.apple.com/forums/thread/658518?answerId=631476022#631476022
Reproduction Steps
Expected behavior
The Send to work
Actual behavior
"No route to host" exception.
Regression?
Unknown. This does work on iOS 15 devices, and iOS 17 simulators.
Known Workarounds
No response
Configuration
iPhone SE 3, iOS 17.5.1, .NET 8 (and 7)
Other information
No response
The text was updated successfully, but these errors were encountered: