Simulating a bad internet connection - router

I'm developing an embedded device which has access to the internet through LAN.
I'm in the testing phase now, and I would like to test how the device performs when the connection to the internet is poor.
Currently, the device is connected to a router through a hub, which I use to monitor the packets with Wireshark.
What's the best way to throttle down the internet speed of the device to mimic a scenario that may happen?
Can I do it through a PC? Do I need access to the router? If so, is it possible to limit the speed of each IP in the router interface?
Actually, a friend suggested that I will purchase a usb2lan ethernet card, and to bridge the PC lan connection to the embedded device, and then using a software QoS limiter. do you think it will work ?

If you have a Mac handy, Macs have kernel facility called dummynet built in, which you control through ipfw. It allows you to simulate a slow connection, randomly drop packets with certain probabilities, and more.
The same facility exists in Linux and other OSes.
From the dummynet homepage:
As of Feb.2010 we have released the third major version of dummynet,
which now runs on all main platforms: FreeBSD, Mac OS X as part of the
native distributions, and you can find Linux, OpenWRT and Windows
versions here.
It can do a lot for you:
limit the total incoming TCP traffic to 2Mbit/s, and UDP to 300Kbit/s
ipfw add pipe 2 in proto tcp
ipfw add pipe 3 in proto udp
ipfw pipe 2 config bw 2Mbit/s
ipfw pipe 3 config bw 300Kbit/s
limit incoming traffic to 300Kbit/s for each host on network 10.1.2.0/24.
ipfw add pipe 4 src-ip 10.1.2.0/24 in
ipfw pipe 4 config bw 300Kbit/s queue 20 mask dst-ip 0x000000ff
simulate an ADSL link to the moon:
ipfw add pipe 3 out
ipfw add pipe 4 in
ipfw pipe 3 config bw 128Kbit/s queue 10 delay 1000ms
ipfw pipe 4 config bw 640Kbit/s queue 30 delay 1000ms

You can try using "Fiddler"
You have options to simulate bad network (like old modems 33k or 56k)
You need to plug in your device to your PC and turn on the proxy (every request will be transfered through Fiddler)
Then you could test your code with a bad network then see what happening ;)

You may use (almost) any linux distribution from liveCd (like ubuntu, mandriva, others) to run it on any hardware you want, and use the kernel-based tool netem, aka "Network Emulation" with the "iproute2" package tools. It lets you control delays, package loss, corruption, duplication and other possible problems of wide area network.
There is list of most used simulations, like:
Rate control using Token Bucket Filter:
tc qdisc add dev eth0 root handle 1:0 netem delay 100ms
tc qdisc add dev eth0 parent 1:1 handle 10: tbf rate 256kbit buffer 1600 limit 3000
Delay all packets by fixed time: tc qdisc add dev eth0 root netem delay 100ms
Package loss: tc qdisc change dev eth0 root netem loss 0.1%

This is another OSX solution, have a look at the Network Link Conditioner.
The following quote is ripped from the following blog (All credit for the below should go to Matt Gemmell):
http://mattgemmell.com/2011/07/25/network-link-conditioner-in-lion/
Get the Hardware IO Tools for Xcode. To do this, go into the Xcode
menu, then choose “Open Developer Tool” and finally “More Developer
Tools…”. You’ll be taken to Apple’s developer downloads site; you
should download the “Hardware IO Tools for Xcode”.
The resulting disk image will contain (amongst other things) a
preference pane for System Preferences, called “Network Link
Conditioner”. Double-click the prefpane file and authenticate to allow
it to be installed. You’ll then see the pane in System Preferences.
You can choose from various different types of network conditions
using the Profile popup menu.
You can also add profiles of your own by clicking Manage Profiles,
either from scratch or via duplicating an existing profile.
Extremely handy for simulating less than optimal network conditions
while testing an app. Just don’t forget to switch it off afterwards!

Related

UDP packets received only in promiscuous mode

I am generating UDP packets on a 100 multicast groups on one VM Ubuntu 16.04 machine and subscribe to those groups on the other VM Ubuntu 16.04 machine. Both are on a HP server run by Hyper-V manager. The problem is that my application only receives 2 out of 100 groups. However, when Wireshark is capturing, the application starts receiving all messages.
I found several other similar questions like this one, where it explains that because Wireshark is running in promiscuous mode, it allows all packets to get through (through what?), and this explains why my application starts "seeing" them too. Thus, changing the Ethernet interface configuration to promiscuous mode allows the application to receive all the messages without running the Wireshark.
But what is the problem with the other packets that are not normally received? I tried to cross-verify the hex-dump of the "good" and "bad" messages and they don't seem to be different. The check sums for on the IP and UDP levels are correct. What else could be the problem?
Multicast ip range 239.1.4.1-100
Destination port 50003
Source port range ~33000 - 60900
firewall is disabled
EDIT:
It looks like when the application is subscribed to only 8 multicast groups, it works fine, however, if subscribed to more than 8, it receives only 2 (if they end on .7 or .8) or none, as described above. So, I would assume that the packets are correct. Could the problem be in the network settings? Or the application itself - need to find the bug in the script I did not write.
EDIT2:
I installed the ISO image on the other machine (Virtual box instead of HP Windows Server) and it works as it should. Thus, I assume my application works fine and all the ubuntu OS configurations are correct. Now I put all the blame on the Virtual Manager/settings. Any ideas?
It sounds as if you didn't tell the kernel about them.
See http://tldp.org/HOWTO/Multicast-HOWTO-6.html
You have to use setsockopt with IP_ADD_MEMBERSHIP. And be sure to use the correct values for your local interfaces.

Passive WiFi detection system using WiFi router

As part of my project requirement I want to make a system which will detect all the WiFi devices in my router range either its connected or not, I did some research on it then I found something like wireshark ,kismate etc I just tried the wireshark by making my Mac machine's WiFi as an adhoc network and its all fine I am able to list all the WiFi devices in wireshark, now I want to make a real-time system based on a real WiFi router I don't know how I will configure my router using my PC and how I will monitor the router from my PC , one more thing if I am using this wireshark how I will use this data for my requirement. If any one worked with similar scenarios please help me..thanks in advance
To do that you will need more than the usual API that you have on commercial WiFi routers (by that I mean a full SSH access). I would:
flash my router with OpenWRT (you can search for your router on this page for detailed instructions)
Install the aircrack-ng suite on the flashed router with
opkg update
opkg install aircrack-ng
Put my WiFi card in monitor mode and run the airodump service:
airmon-ng start wlan0 #Put your NIC in monitor mode
airodump-ng mon0 #Sniff surrounding packets
You don't necessarily have to install aircrack-ng, you can just put your card in monitor mode using command line (look at the documentation for your WiFi driver) and then run tcpdump (command line equivalent to wireshark) but aircrack works very well and has a nice format.
Also, I should warn you that you can brick your router by flashing it. I never had such a problem when flashing router mentioned on the OpenWRT wiki and there are (most of the times) ways to restore a bricked router depending on the brand but I am not responsible if you break it ;)

Shut down/stop TCP but keep other protocols up?

We make a line of industrial manufacturing products which are controlled from software running on a PC. The PC software, in turn, can be controlled from Android devices via a TCP connection, so out on the factory floor workers can use these Android devices as a remote controls for the manufacturing process.
One problem we have is that in congested networks, like at trade shows where we show these products off, the TCP connections between the Android and the PC often gets dropped. We've just written software to detect dropped TCP connections from the Android and send a message to the PC via UDP that this has occurred, but we need to test it.
To aid in testing this I want a way to shut down or break TCP on the PC so I can simulate this condition. I'd like to do this either as a simple C#/.Net program or, better yet, via a "DOS" command or something similar. Any suggestions on how to do this?

Capture HTTP request packets from my iPhone

I want to monitor the HTTP traffic sent/received from my iPhone. The iphone is connected to the Internet via my wifi router.
I want to capture packets from my windows 7 station.
Thanks for your help.
You have a few options here:
If your wireless router has a port mirroring or port spanning feature, turn it on and point it at your workstation's IP. Use Wireshark on your workstation to look at the packets arriving on the interface assigned to that IP.
If your workstation has a wireless card, get Connectify for Windows 7 (turns wireless card into Wifi Hotspot). Connect iPhone through Windows 7 wireless, and workstation through ethernet to the internet. Your workstation will effectively act as a router for your iPhone and you will be able to record iPhone's packets passing through it.
Get an ethernet hub (make sure it is not a switch, you won't see all packets on every interface with a switch), and connect your workstation, wifi router and internet to it.
Get a switch with port mirroring feature, configure port mirroring to forward a copy of all packets to your workstation.
Another option that I wish someone would have mentioned to me is pfSense. This is an operating system based on BSD made to serve as a firewall. Top of the line routers have, say 400 Mhz of processing speed, and unimpressive amounts of ram. The lowest-end computer you'll find these days has better specs than that, and of course, it's upgradeable. You don't have to bother with those terrible Cisco licenses (no DHCP with no license, 20 DHCP users at one license level, 100 users at an higher lever? Ludicrous), etc. Best of all, you have "root' access to the system, so you can run whatever you want on it (including wireshark, say)!!
Make sure you have two sufficiently fast ethernet cards. You'll set your wireless router to not do NAT (because pfSense will be doing that), then you can get to work setting up your VPN server, etc. without thinking about cisco licensing, etc.

How to test low bandwidth conditions on the iPhone

I have an application that does a lot of binary data loading. I've encountered scenarios where an unstable 3G connection may intermittently cut out during the loading of some of this binary data, causing issues.
Is there any way, using the simulator or otherwise, to test for low-bandwidth/unstable connection scenarios?
I seem to recall Adobe Flash having bandwidth simulators in their test suite.
You can accomplish this by creating a wireless network on your Mac using Internet Sharing, degrading that interface using firewall rules, and connecting your iPhone to that network. This will actually work to debug any device that connects to a wireless network.
Using this technique, you can simulate extremely meager, lossy, or latent networks.
I use this technique instead of Apple's official Network Link Conditioner for a couple reasons:
Throttling can be applied only to specific connected devices, rather than affecting your development machine's network connection.
It can be scripted to simulate rapidly changing or "bursty" networks.
Unlike the Network Link Conditioner built into iOS, you can change the settings while your application remains in the foreground.
This uses ipfw's dummeynet feature. ipfw is technically deprecated in modern versions of OS X, but it still works fine. Unfortunately, pf (the replacement) doesn't yet support arbitrary packet delays. I'll update this answer if something changes.
Creating an awful Wi-Fi network
Plug into Ethernet if you aren't already.
Enable Internet Sharing in the Sharing pane of System Preferences. Choose to "Share your connection from: Ethernet" and check "Wi-Fi".
Get your phone connected to the network you just created and make sure you can browse the web.
Tell Mac OS's built-in firewall (ipfw) to ensure packets that have latency applied (pass through the "dummynet" in ipfw parlance) are still routed through the normal rules. This allows Internet Sharing to continue working:
phil#Nebula ~$ sudo sysctl -w net.inet.ip.fw.one_pass=0
net.inet.ip.fw.one_pass: 0 -> 0
Configure the low-quality pipe through which your iPhone's traffic will pass (14Kb/s throughput with 1% packet loss):
phil#Nebula ~$ sudo ipfw pipe 1 config bw 14KB/s
phil#Nebula ~$ sudo ipfw pipe 1 config plr 0.01
The next step varies depending on whether you're on Mountain Lion or below, or Mavericks.
For Mountain Lion (10.8) or below:
Route packets into the pipe, but only for traffic over your AirPort interface:
phil#Nebula ~$ sudo ipfw add 10 pipe 1 ip from any to any via en1
00010 pipe 1 ip from any to any via en1
Important: If you're using an Air or new MacBook Pro without a physical ethernet port, your AirPort interface will likely be called en0. Replace en1 with en0 above if that's the case.
For Mavericks (10.9):
Check the output of ifconfig and look for the bridge interface created by Internet Sharing:
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether xx:xx:xx:xx:xx:xx
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en4 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 6 priority 0 path cost 0
nd6 options=1<PERFORMNUD>
media: <unknown type>
status: inactive
bridge100: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=3<RXCSUM,TXCSUM>
ether xx:xx:xx:xx:xx:xx
inet 192.168.2.1 netmask 0xffffff00 broadcast 192.168.2.255
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 5 priority 0 path cost 0
media: autoselect
status: active
You want the bridge interface that has an IP address; in most cases, it will be bridge100.
Route packets into the pipe, but only for traffic over the bridge interface:
phil#Nebula ~$ sudo ipfw add 10 pipe 1 ip from any to any via bridge100
00010 pipe 1 ip from any to any via bridge100
Change bridge100 if it has a different name on your system.
Simulating a changing network
You can change the values 14KB/s and 0.01 in step 5 above to simulate different types of networks. You can also specify config delay 1000 to introduce a 1000ms delay. See the manpage for more options.
You can continue to reconfigure the pipe after adding the rule for it. For instance, to simulate nearing the edge of cellular coverage, issue this command while your app is running and connected (95% packet loss):
phil#Nebula ~$ sudo ipfw pipe 1 config plr 0.95
There is no need to run sudo ipfw add 10 … again after reconfiguring the pipe. You can script these changes to simulate an extremely dynamic network environment.
Cleaning up
You can issue sudo ipfw delete 10 to put everything back to normal, or just reboot.
Here is a great script I've used on OS X to throttle connection speed, or just turn it off, for any domain you want. I wish I could remember where I got it from to give credit.
Save the code to a file on your machine and name it "throttling". Then to run, just enter the below in terminal, and select from one of these speeds: [full|fast|medium|slow|wwdc|off].
"./throttling medium"
If you have the script set up to throttle localhost:3000 and stackoverflow.com, then loading up a page from either of those domains in your browser (or iphone simulator or whatever) will respond slower and load files slower. It's been really great for testing iphone connectivity bugs.
http://gist.github.com/499177
You could test a number of things if you turn 3G off and connect to wifi.
Log into your router and rate limit the mac address of your iphone. (to test slow connections)
Kill the power to the wifi when in the middle of downloading data
Reboot the wifi router when downloading so the phone has connection, loses it, and gets it again. ( to test different scenarios )
Happy Coding!
I read of someone testing with their iPhone connected by USB cable and the phone wrapped in aluminum foil to get the cellular signal reduced. You can turn off WiFi and 3G and just have Edge and then attenuate it with foil. Sounds crude but...
You could also use an iPhone 4 and hold in your hand to short the two antennas together ;-)
Chrome 38 includes network emulation in the device emulation settings. You can select from Offline, GPRS, EDGE, 3G, DSL and WiFi. Also emulates increased latency.
It's not as accurate as testing on a real device but it's much quicker to set up.
Well, low bandwidth is simple: turn off Wifi, turn off 3G. Cutting connection is also simple: turn on wifi, get connected, power off your wifi access point.
I guess you could connect to a Mac (if you had one) setup as a WiFi base station (ad-hoc network). Then on the Mac set up a "dummynet" bandwidth limit and/or high packet loss filter.
That's what I do to simulate the slower higher-latency DSL lines I am making software to setup for Macs.
On the iPhone you can't. One way would be to share your computer WIFI connection to your iPhone but to slow it down using a special application.
This blog post describes some approaches for that:
http://blog.aptivate.org/2010/01/23/make-sure-your-apps-work-in-the-field/