Can I use a Bonjour name to specify a NWEndpoint?

Hi,

I am using Network.framework and I have issues resolving Bonjour addresses...


Is it possible to:

let host:NWEndpoint.Host = "somehost.local"

let port:NWEndpoint.Port = NWEndpoint.Port(MyPort)

nwconnection = NWConnection(host: host, port: port, using: .tcp)


The problem is that my connection state handler remains in waiting.


When I specify the IP address it connects right away - no problem. When I try to connect to a web server on the same machine using Safari, Safari connects right away.


Do I need to resolve the IP address of the host using the Bonjour framework before creating a NWEndpoint?

What's the best practice establishing a NWNetwork connection to a Bonjour address?


Thanks for the support!


Best,

Michael

Replies

Do I need to resolve the IP address of the host using the Bonjour framework before creating a

NWEndpoint
?

Oh gosh no. Doing manual DNS resolution is never the right answer [1].

You have a couple of options here:

  • If you’re starting with a Bonjour service, use the

    .service(…)
    case of the
    NWEndpoint
    enum.
    let conn = NWConnection(to: .service(name: "Fluffy", type: "_ssh._tcp.", domain: "local.", interface: nil), using: .tcp)

    .

  • If you’re starting with a local DNS name, use the

    init(host:port:using:)
    initialiser.
    let conn = NWConnection(host: "fluffy.local.", port: 22, using: .tcp)

    .

I just tried both of these here in my office and they’re working for me.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] OK, I’m sure there are cases where it’s the right answer, but they’re exceedingly rare.

Hi eskimo,

so finally I printed the debug description on the nwconnection during the wait stage of the state handler - and I got:

Optional([C1 MINI-20001.local:5055 tcp, indefinite, path unsatisfied (Path was denied by NECP policy), interface: pdp_ip0, ipv4])


I found a thread that pointed out this could be due to connection settings. I checked and enabled Cellular networking for my app (which will never connect via cellular) - and suddenly everything works. Switching cellular off stops the connection from connecting again?


Can you explain what is happening here?


-michael



EDIT:

I have a suspicion...


Following situation:

- The host I am connecting to is a hotspot.

- Since the device is never intended to be router (it simply provides its own network to connect to it) the DHCP of the camera does not supply a default route and when I connect to the network the connection shows "No internet connection"

- Could it be that the Bonjour lookup works through DNS - which may be blocked as the app does not seem to have internet access (due to the missing Cellular connection)?


This is a special case I guess, but the behavior is really kinda counter intuitive?


EDIT2:

Further testing:

- If the WIFI connection provides a default router (which does not exist and is shutting down internet connection via cellular all together) it seems the Cellular setting of my App does not matter and Bonjour resolves and the connection is being established.

- If the WIFI connection does not provide a default route and Cellular is turned off for the App it does not resolve the Bonjour name and the NWConnection remains in waiting.

- If the WIFI connection provides no default route, cellular is turned off for the App but Cellular is disabled for the device all together (flight mode with Wifi enabled), Bonjour names get resolved and the nwconnection gets established.

- Connecting via IP Address works always without any issues.

Path was denied by NECP policy

FYI, NECP is the subsystem that enforces network interface access control policy. For example, if you go into Settings > Mobile Data and disable a specific app’s access to WWAN, the actual work to prevent that access is done by NECP.

With regards your other findings, I think that’s eminently bugworthy. I don’t see any reason that NECP should be preventing the successful resolution of, and connection to, a

local.
DNS name, regardless of how wonky the local network environment is.

When filing your bug, do the following:

  1. Set up things to reproduce the problem.

  2. Make a note of the time.

  3. Reproduce the problem.

  4. Trigger a sysdiagnose log, per the instructions on our Bug Reporting > Profiles and Logs page.

  5. Attach that sysdiagnose log to your bug report, making sure to mention the time from step 2.

Please post your bug number, just for the record.

Regarding workarounds, the first thing I’d try is to explicitly prohibit WWAN using the code below:

let params = NWParameters.tcp
params.prohibitedInterfaceTypes = [.cellular]
let conn = NWConnection(host: "fluffy.local.", port: 22, using: params)

Does that work?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,

prohibiting cellular for the connection did not fix the issue.


I had a quick look at the sysdiagnose page, but it seems to lack a category for Network.framework - I only see CFNetwork there. Not sure which procedure I should follow?


I also created a simple App with a singluar button and did not have that issue - but it also did not show in my Cellular config. What makes an app apear in the cellular config? Maybe there is something wrong in my plist?


-michael

prohibiting cellular for the connection did not fix the issue.

Bummer.

I had a quick look at the sysdiagnose page, but it seems to lack a category for Network.framework - I only see CFNetwork there. Not sure which procedure I should follow?

In the absence of specific guidance, follow the instructions labelled sysdiagnose for ***, where *** is your platform.

I also created a simple App with a singluar button and did not have that issue - but it also did not show in my Cellular config. What makes an app apear in the cellular config?

The first time the app makes a connection that’s routed over WWAN it becomes known to NECP and starts showing up in Settings > Mobile Data.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"