Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate network conditions with a C/C++ Socket

I'm looking for a way to add network emulation to a socket.

The basic solution would be some way to add bandwidth limitation to a connection.

The ideal solution for me would:

  • Support advanced network properties (latency, packet-loss)
  • Open-source
  • Have a similar API as standard sockets (or wraps around them)
  • Work on both Windows and Linux
  • Support IPv4 and IPv6

I saw a few options that work on the system level, or even as proxy (Dummynet, WANem, neten, etc.), but that won't work for me, because I want to be able to emulate each socket manually (for example, open one socket with modem emulation and one with 3G emulation. Basically I want to know how these tools do it.

EDIT: I need to embed this functionality in my own product, therefore using an extra box or a third-party tool that needs manual configuration is not acceptable. I want to write code that does the same thing as those tools do, and my question is how to do it.

Epilogue: In hindsight, my question was a bit misleading. Apparently, there is no way to do what I wanted directly on the socket. There are two options:

Add delays to send/receive operation (Based on @PaulCoccoli's answer):
by adding a delay before sending and receiving, you can get a very crude network simulation (constant delay for latency, delay sending, as to not send more than X bytes per second, for bandwidth).
Paul's answer and comment were great inspiration for me, so I award him the bounty.

Add the network simulation logic as a proxy (Based on @m0she and others answer):
Either send the request through the proxy, or use the proxy to intercept the requests, then add the desired simulation. However, it makes more sense to use a ready solution instead of writing your own proxy implementation - from what I've seen Dummynet is probably the best choice (this is what webpagetest.org does). Other options are in the answers below, I'll also add DonsProxy
This is the better way to do it, so I'm accepting this answer.

like image 655
Yasei No Umi Avatar asked Jun 27 '12 09:06

Yasei No Umi


2 Answers

You can compile a proxy into your software that would do that. It can be some implementation of full fledged socks proxy (like this) or probably better, something simpler that would only serve your purpose (and doesn't require prefixing your communication with the destination and other socks overhead). That code could run as a separate process or a thread within your process.

Adding throttling to a proxy shouldn't be too hard. You can:

  • delay forwarding of data if it passes some bandwidth limit
  • add latency by adding timer before read/write operations on buffers.
  • If you're working with connection based protocol (like TCP), it would be senseless to drop packets, but with a datagram based protocol (UDP) it would also be simple to implement.

The connection creation API would be a bit different from normal posix/winsock (unless you do some macro or other magic), but everything else (send/recv/select/close/etc..) is the same.

like image 160
m0she Avatar answered Oct 04 '22 16:10

m0she


If you're building this into your product, then you should implement a layer of abstraction over the sockets API so you can select your own implementation at run time. Alternatively, you can implement wrappers of each socket function and select whether to call your own version or the system's version.

As for adding latency, you could have your implementation of the sockets API spin off a thread. In that thread, have a priority queue ordered by time (i.e. this background thread does a very basic discrete event simulation). Each "packet" you send or receive could be enqueued along with a delivery time. Each delivery time should have some amount of delay added. I would use some kind of random number generator with a Gaussian distribution.

The background thread would also have to simulate the other side of the connection, though it sounds like you may have already implemented that part?

like image 39
Paul Coccoli Avatar answered Oct 04 '22 16:10

Paul Coccoli