Send and receive IP packets from iOS

Is there a way to send and receive IP packets from iOS app similar to TCP or UDP. I couldn't find any docs online so any inputs will be very helpful

Answered by DTS Engineer in 721452022

Are you asking about raw IP, that is, not TCP and not UDP?

If so, there’s no way to do that on iOS. The underlying infrastructure is in place but it requires that you run as root. On macOS that’s just an inconvenience, but on iOS it’s a showstopper. iOS apps cannot escalate privileges.

Why do you need raw IP?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

You have all the normal POSIX APIs for TCP and UDP. "Similar to TCP or UDP" is a bit vague, what are you trying to do?

There are also some higher-level but less portable APIs for TCP including NSStream and NSNetService.

Accepted Answer

Are you asking about raw IP, that is, not TCP and not UDP?

If so, there’s no way to do that on iOS. The underlying infrastructure is in place but it requires that you run as root. On macOS that’s just an inconvenience, but on iOS it’s a showstopper. iOS apps cannot escalate privileges.

Why do you need raw IP?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo, Is the restriction just for raw IP packets. What about the TCP and UDP packets which are converted to IP packets. Can we use the existing infrastructure to send those IP packets ?

Can we use the existing infrastructure to send those (TCP and UDP) IP packets ?

Yes.

@endecotp I mean sending TCP as IP packets instead of sending it as TCP packets. Is that possible ?

I mean sending TCP as IP packets instead of sending it as TCP packets.

A TCP packet IS an IP packet.

Do you mean, you want to send a TCP packet but to have control over all the bytes of the TCP header? That requires raw packets and is not possible on iOS.

@endecotp and @eskimo - Sorry if I was not clear. I meant extracting the TCP/UDP packet from the IP packet and sending it. Is there a library in iOS which helps with this ?

I meant extracting the TCP/UDP packet from the IP packet and sending it.

Can you give us some more background as to your high-level goal here? What problem are you trying to solve by doing this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo The high level goal is to route the IP packets from accessory device (which connects to iphone via bluetooth). The accessory will send data as IP packets. Since we can't send raw IP packets from iPhone, wanted to analyse the IP packet received from the accessory, find if it is TCP/UDP and construct the corresponding packet and send it to the destination. Is there any library on the ios side which might help us with this ?

Data sent over an IP network uses IP packets. Only.

If I ignore the (confusing to me) TCP and UDP phrasing you’ve used here, I’d consider an IP tunnel (usually TLS wrapped, maybe using GRE or stunnel, etc) connection, or creating a custom protocol router that extracts the app data and forwards that via TLS (stream) or DTLS (datagram). The former tunnel wraps (encapsulates) the existing IP traffic, the latter router preferably sends only the app data.

The choice of stream or datagram depends on the details of the LAN-local (Bluetooth) data. If the data is status data and periodic updates and some can (rarely) be lost (read: datagram), then UDP or (preferably, secure) DTLS. If some of the data can’t be dropped (read: stream), then TCP or QUIC or (preferably, secure) TLS.

Since we can't send raw IP packets from iPhone, wanted to analyse the IP packet received from the accessory, find if it is TCP/UDP and construct the corresponding packet and send it to the destination.

Just forwarding packets won’t work here. Let’s focus on TCP for the moment. There are two problems with forwarding packets:

  • You can’t do it because there’s no way to send raw IP [1].

  • The packets you get have a source address that’s unique to your accessory, probably some link-local or private IP address. If you forward those as is, the receiver’s reply will never get to you.

So you have to do some sort of rewriting. The traditional option here is to implement NAT, however that’s not feasible on iOS. An alternative is to use your own TCP/IP stack to re-assemble the TCP flow from the packets you get from the device and then proxy that flow using an iOS TCP API like NWConnection. That feasible, but you probably don’t want to write your own TCP/IP stack from scratch.

Is there any library on the ios side which might help us with this ?

There’s nothing in the iOS SDK. There are plenty of third-party libraries that let you slice’n’dice IP packets, including full-scale user space IP stacks, but I don’t have any direct experience so I can’t offer a specific recommendation.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Even on systems where raw IP is available, you typically can’t do this the platform’s TCP stack ‘owns’ the TCP protocol.

The high level goal is to route the IP packets from accessory device (which connects to iphone via bluetooth). The accessory will send data as IP packets. Since we can't send raw IP packets from iPhone, wanted to analyse the IP packet received from the accessory, find if it is TCP/UDP and construct the corresponding packet and send it to the destination.

How are the IP packets encapsulated over Bluetooth? Is it using something like 6LoWPAN?

I believe that, as you said, you will need to analyse the IP packets received from the accessory. But they rather than just forwarding those packets, you will need some more complex logic that acts as a proxy, I.e. when you see a packet with a TCP SYN, you should open a TCP socket and send an encapsulated ACK back to the accessory over bluetooth.

A fully-general proxy would be quite complicated, but if you know that the accessory uses only a certain subset of the possible functionality then it becomes easier.

Are you the manufacturer of the accessory?

Send and receive IP packets from iOS
 
 
Q