NWMulticastGroup crash when socket address already in use

iOS 14 introduces multicast support in the Network framework in the form of NWMulticastGroup.

Following Apple's guide ( https://developer.apple.com/news/?id=0oi77447 ) I am trying to join the multicast group for SSDP using the following code:

Code Block swift
let host = NWEndpoint.Host(ipFamily.ssdpGroupName)
let multicastGroup = try NWMulticastGroup(for: [.hostPort(host: host, port: .ssdp)])
let ssdpGroup = NWConnectionGroup(with: multicastGroup, using: .udp)
ssdpGroup.stateUpdateHandler = { newState in
Logger.info("Reached new state: \(newState)")
}
ssdpGroup.setReceiveHandler(maximumMessageSize: 16384, rejectOversizedMessages: false, handler: {[weak self] (message, content, isComplete) in
Logger.info("SSDP message received, do something here...")
})
ssdpGroup.start(queue: delegateQueue)

However, it seems that the port is already in use by another application.

Now I have the following issues:
  1. It reaches the .ready state and then crashes, instead of reaching the error state...

  2. Is there a way to configure the socket to allow socket reuse? This is important to prevent clashes with e.g. the Spotify app.

  3. Is it possible to send a message to the multicast group from a different port? (destination port and source port are different)

This is what we see in Xcode (where 192.168.1.165 is the IP address of the device that we are trying to discover via SSDP):
Code Block lang-none
2020-08-18 16:36:44.804834+0200 DemoApp[632:112993] [] nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW <some-UUID> [48: Address already in use]
2020-08-18 16:36:44.806020+0200 DemoApp[632:112993] [connection] nw_endpoint_flow_setup_channel [C8 192.168.1.165:1900 initial channel-flow (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] failed to request add nexus flow
2020-08-18 16:36:44.808119+0200 DemoApp[632:112993] [connection] nw_endpoint_handler_create_from_protocol_listener [C8 192.168.1.165:1900 failed channel-flow (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] nw_endpoint_flow_pre_attach_protocols
2020-08-18 16:36:44.808930+0200 DemoApp[632:112993] [connection] nw_connection_create_from_protocol_listener_on_nw_queue [C8] Failed to create connection from listener
2020-08-18 16:36:44.809410+0200 DemoApp[632:112993] [] nw_ip_channel_inbox_handle_new_flow nw_connection_create_from_protocol_listener_on_nw_queue failed

And this is the crash (Thread 10: EXC_BAD_ACCESS (code=1, address=0x10)) stack trace that we see after a while:
Code Block lang-none
libnetwork.dylib`__57-[NWConcrete_nw_listener handleInbound:addProtocolInbox:]_block_invoke:
    0x19e1530c8 <+0>:  ldp    x8, x1, [x0, #0x20]
    0x19e1530cc <+4>:  mov    x2, x8
->  0x19e1530d0 <+8>:  ldr    x3, [x2, #0x10]!
    0x19e1530d4 <+12>: mov    x0, x8
    0x19e1530d8 <+16>: braa   x3, x2

Is this an iOS 14 Beta 4 issue? Or are we doing something wrong?


We had the crash with iOS 14 Beta 4 and Xcode 12 Beta 4, but can't reproduce it with iOS 14 Beta 5 and Xcode 12 Beta 5 (released a few hours ago) so far.

However, SSDP is still failing for us due to the address already being in use ([48: Address already in use]) with Beta 5.

However, SSDP is still failing for us due to the address already being
in use ([48: Address already in use]) with Beta 5.

What port number are you using for SSDP?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
NWMulticastGroup crash when socket address already in use
 
 
Q