Cast or convert Network.NWInterface to nw_interface_t

From the documentation to NEAppProxyFlow class

   /**
    * @property networkInterface
    * @discussion An nw_interface_t containing information about the network interface used by the flow. If the flow's data is transported using a different interface, this property
    *  should be set to that interface.
    */
  @available(macOS 10.15.4, *)
  @NSCopying open var networkInterface: nw_interface_t?

How to create an instance of nw_interface_t given I have an instance of Network.NWInterface class? There are no methods like nw_interface_create_with_name or something like that. Force cast also fails.

Typically you are not constructing an interface here, but rather checking the interface to see if it is bound. For example:

if let interface = flow.networkInterface {
   os_log(.debug, log: self.log, "(handleNewFlow) NEAppProxyFlow, flow: 0x%zx - with interface %{public}@ isBound: %d", flow.osLogID, interface.description, flow.isBound)
}

// (handleNewFlow)NEAppProxyFlow, flow: 0x7f9df7e068c0 - with interface en0 isBound: 1

Now, if this is not your use-case, why are you trying to construct and set an interface here?

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

I have been writing a router suing system network extension(NEAppProxyProvider). Its task is to send traffic to different network interfaces based on bundle id.

From what I understood there is no way to convert between nw_interface_t  and Network.NWInterface types and I should use

  • nw_path_monitor
  • nw_path_enumerate_interfaces

to get an instance of nw_interface_t and search its name in NWInterface list received by

  • NWPathMonitor
  • path.availableInterfaces
Accepted Answer

From what I understood there is no way to convert between nw_interface_t and Network.NWInterface types

You are correct. You cannot create a new NWInterface or construct one from an existing physical interface name, e.g., using something like en0.

I would open an enhancement request here for an API do so. Please respond back with the Feedback ID.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Thanks, Matt I have opened an enhancement request. Feedback ID: FB9386412

I have opened an enhancement request. Feedback ID: FB9386412

Thank you, I can see that your bug landed in the right hands.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Cast or convert Network.NWInterface to nw_interface_t
 
 
Q