For a while now I’ve been writing my own network monitoring app for Android, called InterneTakip. It shows live speed, daily usage and ping in the notification bar. There’s also an optional “connection log” layer you can switch on: which app talked to which address, on which port, and how many bytes.

For that last part I use VpnService. But this isn’t a real VPN going out to some remote server. The phone’s traffic lands on a local tun interface, I count the connections there, and I forward each one to its actual destination over normal sockets. I didn’t write the part that turns raw IP packets back into TCP/UDP connections from scratch. I use zdtun, a small C library that runs in user space (LGPL-3.0, linked as a separate .so). On top of it I added a thin JNI wrapper and a hand-written parser that reads DNS responses and the server name in TLS (SNI).

This post is about how I caught that engine quietly killing idle connections. Every measurement was done on a real device: a Poco X7 Pro running Android 16.

The suspicion came from the source code first

Before bringing zdtun into the project, I read its code. Two constants caught my eye:

#define TCP_TIMEOUT_SEC 60
// on linux, the maximum open files limit is 1024
#define MAX_NUM_SOCKETS 128

These are used inside zdtun_purge_expired. Every time the function is called, it destroys any TCP connection that hasn’t seen traffic for 60 seconds. And if the number of open sockets has hit 128, it sorts connections by timestamp and closes them, oldest first, until it gets down to 96. My loop calls this function once a second.

On a desktop, for short-lived connections, these values might be reasonable. On a phone, though, lots of apps open a connection and then let it sit there for ages without sending anything: notification channels, messaging, background sync and so on. If the tunnel forgets a connection like that, the app gets hit with a disconnect and has to reconnect.

But inferring “this might cause trouble” from the code is one thing, and seeing it on a device is another. So I wrote it down in my assumptions log: “zdtun’s socket limit and idle timeout are good enough for everyday use.” And then I tried to disprove it.

Who’s closing it? That had to be pinned down first

When a TCP connection drops, there are plenty of suspects. The server might have closed it on its own timeout, the modem or NAT in between might have forgotten the connection, or the tunnel might have cut it. If I just connected to a real website and waited, I couldn’t tell where the drop came from.

So I wrote a small Node server on my local network. One of its ports only accepts the connection, reads whatever comes in and throws it away, and never closes the connection itself. That takes the server off the suspect list right from the start.

The test on the phone side is simple too: open N connections, send nothing for a set amount of time, then try to read from each one and classify the result:

  • if the read times out, the connection is still open,
  • if read() returns -1, the other side closed it cleanly,
  • if you get “connection reset”, someone sent an RST.

I also needed a control group: running the same test with the VPN off. If the connections stay open without the VPN, the network itself is off the suspect list too.

The measurement: 20 connections, 75 seconds

On Wi-Fi, against the local server, with unmodified zdtun, here’s what came out:

  • Of 20 connections left idle for 75 seconds, all 20 were killed with an RST.
  • Of 200 concurrent connections, the oldest 89 were killed with an RST.
  • With the VPN off, every connection stayed open in the same tests.

The server doesn’t close them, and with the VPN off the network doesn’t either. That leaves just one suspect. I wrote “false” next to the assumption. The guess had come from the source code, and now I had a number to back it up.

The patch: don’t change the value, make it changeable

The easiest thing would have been to edit the numbers directly in zdtun.c. Instead, I wrapped the constants in an #ifndef guard. The defaults stay exactly where they were; this is the whole patch:

+#ifndef TCP_TIMEOUT_SEC
 #define TCP_TIMEOUT_SEC 60
+#endif

I did the same for the UDP timeout, the socket limit and the post-purge target. The real values are set in my own CMakeLists.txt:

set(ZDTUN_LIMITS TCP_TIMEOUT_SEC=7200 UDP_TIMEOUT_SEC=120 MAX_NUM_SOCKETS=512 NUM_SOCKETS_AFTER_PURGE=448)

That way the diff in the third-party file stays a guard of just a few lines, and it reads at a glance as an LGPL patch. And the values live in my own project, right next to a comment explaining why.

By the way, you can’t make the socket limit as big as you like. zdtun uses select(), and on Linux FD_SETSIZE is 1024. My loop also checks right at the start that the tun descriptor is below that limit. 512 sits comfortably under that ceiling.

Verification

I ran the same tests again with the patched engine: all 20 of the 20 idle connections and all 200 of the 200 concurrent connections stayed open. The most sockets seen during the test was 208. When the phone is just sitting idle normally, 10–12 sockets are open. So the new limit is well above the peak in the test, and way above everyday use.

Other things I measured along the way

While choosing and wiring up the engine, there were a few more things I didn’t leave as assumptions; I measured them on the device.

Who owns the connection. Since Android 10, the active VPN app can ask which app a TCP/UDP connection belongs to via getConnectionOwnerUid. On mobile data, a UID was found for 59 out of 59 connections. The query took 1.2 ms on average, 5.4 ms at worst. In the experiment this query ran synchronously inside the loop, and like that, the median setup time for a new TCP connection went up from 17 ms to 27 ms. In the shipped app, the loop reports the connection event to Java and returns right away, and the work happens in a queue. There’s no such API for ICMP, so those connections get logged with owner “unknown”.

My own measurements shouldn’t go into the tunnel. If the app’s own ping measurement went through the tunnel, it would be measuring the tunnel, not the real network. When I opened the ICMP socket without protect(), 1 ICMP connection showed up in the engine; with protect(), none did. In both cases, 5 out of 5 pings got a reply. Also, the app’s own sockets are completely outside the tunnel, and every forwarding socket zdtun opens is protect()ed the moment it’s opened. Otherwise the engine’s own traffic would fall back into the tunnel and create a loop.

MTU. On the local network, upload speed without the tunnel was 496–610 Mbps, while with the tun MTU at 1500 it came out at 136–143, at 9000 it was 330–432, and at 16000 it was 417–600 Mbps. CPU time per round was about 2 s without the tunnel, about 5 s at MTU 1500, and about 2.7 s at 9000. I settled on MTU 9000. Over a 20-round series, download stayed almost the same (median 502 vs 495 Mbps), while upload loses about 20% (423 vs 339 Mbps). Since the connection log is a layer you can switch off, I accepted that cost.

If the engine dies or freezes. A monitoring bug should never cut anyone’s internet. When I killed the process with kill -9, the VPN went down within 0.2 seconds and pings came back immediately. But when I froze the process with SIGSTOP, the VPN stayed up and the internet was cut off; resuming it fixed things. So dying is safe, hanging isn’t. That’s why the loop writes a “heartbeat” timestamp on every round, the service checks it every 5 seconds, and if it’s been silent for more than 15 seconds it shuts the tunnel down.

What I don’t look at

A tool like this can easily turn into something you never wanted, so I set the boundaries up front:

  • Packet contents are never written to disk, to logs or to persistent storage. Only bytes are counted per connection.
  • No TLS interception, no installing CA certificates. I read the server name from the ClientHello in the first TCP data the app sends. Later packets aren’t looked at.
  • For domain names, I look at the name-to-address mapping in DNS responses. I don’t do reverse DNS lookups, because they both leak your history to the outside and can make up domain names.
  • No data ever leaves the device. No analytics, no crash SDK, no cloud backup.

This has a cost too: if Private DNS (DoT/DoH) or ECH is turned on, the domain name may not be visible, and all you’re left with is the IP. I’m not hiding that like it’s a bug; I openly accept it as a known blind spot.

What stuck with me

I didn’t catch this bug through a user complaint, but while reading the source code. Still, what did the real work was the test that could tell the suspects apart. Without a server that never closes and a control group with the VPN off, all I’d have had was the sentence “connections are dropping”.

Here’s my advice to you: a library’s default values are values chosen for someone else’s use case. When you read the code, take a look at the constants. Write down what you suspect as an assumption, set up a test that tries to disprove it, and record the result as a number. A one-minute timeout looks like a tiny detail, right up until it quietly kills every quiet connection.