runtime: Cannot Send to a broadcastable socket on iOS 17 device

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

        private static async Task TestBroadcastAsync()
        {
            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;
                }

                bool multicast = false;

                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));
                    Debug.WriteLine($"Test-Bound ok to {localIP}");
                    
                    var bytes = new ArraySegment<byte>(new byte[1]);
                    await socket.SendToAsync(bytes, SocketFlags.None, dest);
                }
            } 
            catch (Exception ex)
            {
               Debug.WriteLine("Exception: " + ex.Message);
            }
        }

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

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 24 (15 by maintainers)

Most upvoted comments

@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:

If your app accesses the local network, add the NSLocalNetworkUsageDescription property to its Info.plist to explain its behavior to the user.

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 got this to work, and this is required:

  1. Ask Apple for access to the com.apple.developer.networking.multicast entitlement: https://developer.apple.com/documentation/bundleresources/entitlements/com.apple.developer.networking.multicast?language=objc
  2. Once you have the entitlement, add it to your Entitlements.plist:
<?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>
  1. Add an entry for NSLocalNetworkUsageDescription in your Info.plist (without this it seems the privacy dialog to ask for permission to access the local network never shows up… and you never get permission):
<key>NSLocalNetworkUsageDescription</key>
<string>Hello local network!</string>
  1. Do something on the local network:
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");

Yes, this is a change in iOS 16+.

Please read https://github.com/dotnet/macios/issues/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:

  • IP_BOUND_IF isn’t bound.
  • There’s no way to get the name of a particular interface (this is done by calling if_nametoindex - which the BCL actually calls, and stores the name somewhere, but as far as I could figure out it’s not available using public APIs).

I wasn’t able to figure out what the problem was though.

@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 ?

I updated the description and the repro to improve usefulness and accuracy.

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:

var remoteIP = IPAddress.Parse("239.255.255.250");
var dest = new IPEndPoint(remoteIP, 1900);
await socket.SendToAsync(buff, SocketFlags.None, dest);