Monitor Internet Connection using C# - c#

I want to monitor my router to see what is currently being downloaded and what application/Software that initiate it, who is the user doing this and download speedthat user occupy
i got all credential needed for both my wireless connection and the router
any head start for sth like that in C#?
the idea

First of all: Do you even know that your router can do this? Most routers I've seen do not have this level of traffic understanding and management.
If your router does, then there is one of two ways that such data is generally made available:
1) Through a HTTP interface (password protected)
or
2) Through an SNMP interface
To retrieve the appropriate HTTP URL, you simply get it as you would any other web page. Try something like: http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
To retrieve the SNMP settings, there are many options; try this question: What SNMP library for .NET makes traps, sets or gets simple?
However, most likely, your router will not actually have this information available.
So, edit: If your router doesn't support side-chaining, and doesn't support SNMP or similar statistics, then you can't do this in the general sense.
You could spin up a Linux box as the gateway for the machines, and use NAT session statistics to monitor this. The way to do this is to have two network interfaces (logical or physical), one of which gets an IP from your modem, and the other which is the default gateway for the wireless network. Turn on IP forwarding and masquerading (NAT) as well as a DHCP server for the wireless network side. Now, you can use iptables to look at active NAT sessions and how much data has been transferred. You can also use packet filters for more specific information.
Also, if you know which machine is doing the downloading, and are running Windows, you can use WMI ("perfmon.exe" to plot this) to see how much data is being transfered on the actual machine.

Related

Best way to configure network devices that have an invalid / non matching network configuration setup?

We're developing some hardware devices in our company that need to communicate through TCP on the network with our application. The devices have some restrictions, such as they do not have a display or a dotmatrix to show any current configuration, such as IP or mac address. Therefore, it is not possible to configure the IP configuration directly on the device.
When they are connected to the network for the first time, there is a high chance, that their current network configuration does not match with the network being connected to.
E.g.
They are configured to use DHCP and the network does not provide any DHCP.
They are configured to use a manual IP, such as 192.168.1.1 with a subnet mask of 255.255.0.0, but the network is set up to use 10.X.X.X as IP range with a network mask of 255.0.0.0
We now want to develop a standalone application, that lists all devices being connected to the network and change the IP configuration to a specific one (matching the network needs).
What needs to be set up on the device (running Linux) in order to be capable of the things posted above?
What needs to be set up on the client side application in order to display the devices and reconfigure them if mismatching? The Application should be a .NET WPF / C# application if possible, admin rights can be aquired if needed.
Are there any restrictions / scenarios under which we are not able to detect those devices or set up the new network settings?
Leaving out any security issues I would use a udp broadcast to 255.255.255.255 which will been received by any devices. When you put all valid subnets into the payload the client will know that it is wrongly configured and has to answer back as broadcast (since it might be in a different subnet and a unicast is impossible without any valid routing tables)
But please keep in mind that this can open security issues since you will publish on scans all valid subnets which might been very interesting for attackers.
If you know e.g. the Mac addresses of all possible device you may could encrypt the UDP payload which can only been encrypted with the right Mac addresses.
This is what DHCP, BOOTP, RARP are for. Using DHCP and friends to do this sort of thing means a device plonked on a network stands the best possible chance of getting a valid config in the first place.
If you MUST have your own app doing the management of the issue of IP config, then perhaps consider using the DHCP protocol in your management/server app.
If your devices must not speak to a random DHCP server then consider having them either ignore responses that don't have certain attributes in the reply (bit inefficient since it may keep asking and may tie up IPs) or use DHCP on different ports... At least you're not reinventing the wheel then.
Using existing protocols has the benefit that network analysers can understand what's going on too, system admins can debug and make intuitive guesses as to problems, etc.

Load Testing TCP with IP spoofing

I'm trying to come up with a way to Load Test the following architecture:
We have an Application Server that recieves data from multiple servers with data-collecting agents on them.
However, our testing environment only has ONE server with an agent, and we wish to emulate multiple (100) connections from the same server outgoing to the Application Server.
I have a wide-open range of IPs I have already assigned to the NIS card on the agent machine, but I am unable to find a way to make each outgoing connection from the machine to the app server use a different IP and thus open a new connection.
I know that web tests on HTTP protocols can use Virtual Users and IP Spoofing tools easily when facing thi sproblem, but since the agent-server connection uses TCP - I cannot find ANY tool that is capable of fulfilling this function.
I also thought about possibly editing each packet by code and replacing it's header with a different IP each time - but this method seems both too convuluted and impractical as it would delay each request for too long to make the load testing relevant.
Any ideas and solutions are welcome!
thanks in advance :)
You can set the IP adress by using Socket.Bind before connecting to the server.
It's described here: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.localendpoint(v=vs.100).aspx
You can use winpcap or its .net wrapper pcap.Net (http://pcapdotnet.codeplex.com/) to get complete control over the IP/TCP packets sent.

LAN based app - How to connect without static IP?

I am developing a LAN-based database application. It involves a central "server" app to house the database, along with many "client" applications that access it.
The "server" will be a simple C#-based HTTP server that responds to GET and POST requests. However, since it is designed to be able to run from any laptop on the network, I am wondering how to establish the connection between clients and the server without knowing the IP address.
I suppose I could ping every IP address from 192.168.0.0 to 192.168.0.255, and then test those that responded to see if any are my server. But I would only do that if there is no better way. Any suggestions?
Many of these types of discovery services run by putting out some kind of beacon on either the subnet broadcast address (for 192.168.0.0/24 it would be 192.168.0.255) or by putting out a beacon on a multicast address.
Multicast is particularly interesting because in a properly configured network, it allows hosts to find the service even across subnets. Routers and switches won't generally forward broadcast packets across subnet boundaries, but multicast packets will.
The beacon would have information in it such as the port the service is running on, what type of service it is, whatever is needed to start using the service.
To head you in the right direction, what you should do is have the database server running on a specified port. Then send out a broadcast to that port from the client (the system needing to connect to the database). When the database server receives this, it will be able to respond to the sender, allowing a handshake to occur.
Of course, you will need to validate the database server's authenticity (to make it secure, unless you aren't worried about that). This can be as simple as having the client display 4 numbers which then need to be typed into the database, so that the database can send the 4 numbers back to the client proving it is the right computer (how the iTunes remote works), or you can use certificates (but that is too complex a topic for me to cover correctly).
After that the two computers will know each others IPs, and you're set!

How to scan for a port waiting for a connection on a network

I am currently working on a little chat utility in C#.
The problem is I can't figure out how to scan the network for a specific port on all machines. I am currently using this method
IPGlobalProperties network = IPGlobalProperties.GetIPGlobalProperties();
IEnumerable<IPEndPoint> connections = network.GetActiveTcpListeners()
.Where(x => x.Port == ConstParams.iPort);
where ConstParams.iPort is the port I want to scan (6910 here).
The problem is that the returned values are only local ports and the "0.0.0.0" ip address...
How can I scan for all open ports (6910) on the current network?
Rather than using port scanning, I suggest you to implement a simple discovery mechanism based on multicast/broadcast communication.
During the startup the application should broadcast/multicast its IP/Port information. All running instances should respond to this message with their IP/Port information. This kind of a discovery mechanism is easy to implement, and it is both faster in runtime and more dynamic than the port scanning approach.
You should consider multicast, but rather than rolling your own, rely on an existing standard with library support, like mDNS:
http://en.wikipedia.org/wiki/Multicast_DNS
Or, since you said C#, using one of its native solutions:
http://msdn.microsoft.com/en-us/library/system.net.peertopeer.aspx
Scanning ports is a poor choice, you will most likely trigger firewalls on machines in the network to display your machine as an attacker. Any Intrusion detection systems on the networks could potentially be triggered as well. It's a lot of overhead for what you need.
I would recommend doing a broadcast using UDP or a multicast to discover other clients
http://www.codeproject.com/Articles/1705/IP-Multicasting-in-C
Another option would be to have a centralized server, either on a web server (php script, asp.net page, etc) or a web service (REST) which the chat client would connect to on start up, POSTing it's listening IP/Port, and then in turn would receive a list of all recently announced IP/Ports of the other clients on the network. You'd probably want some keep alive here, IE: the client would POST to the page every 5 minutes, if an IP does not POST for 10 minutes, it would be removed from the list.
To get the public IP of the machine, you could check out this page:
http://www.whatismyip.com/faq/automation.asp
You'd just need to send a web request to it to retrieve the IP. If you want to get the non 0.0.0.0/127.0.0.1 IP of the local interface, you can check out these posts:
Get local IP address
How do I get the Local Network IP address of a computer programmatically? (C#)
GetIPGlobalProperties only returns info about your local machine (see http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getipglobalproperties.aspx ).
To find out which other machines on the network have that port open, you'd have to iterate through the a range of IPs, attempting to connect on that port. There is no central repository to query on this.
This article describes an approach: http://www.dijksterhuis.org/building-a-simple-portscanner-in-c/

Send data from the server to a GPS device

this is my first question here, after searching and reading through many places I have not choice but to ask.
I have a C# application that receives data from a GPS device and stored in a database, I need now is to send a string that contains commands which the device should be interpreted.
This device connects to the Internet over GPRS, I haven't idea how to send this packet over TCP over GPRS because the IP is dynamic.
I hope any suggestions or ideas on how to solve this.
Best regards.
You should do it the other way around - the device should poll the server for instructions. Just make sure the server is accessible.
As you have specified that you have developed a C# application to read the GPS data through GPRS that means you are running C# application on a public/static IP.
Nwo as soon as you get a request from the GPRS client, you get the DHCP IP address of the remote endpoint too.
Once you have that endpoint and socket open, you can transmit any data to the GPS device back.
Assuming that your GPS device is having some microcontroller to drive the GSM/GPRS modem.
I'm going to make an assumption here that the 'Device' is some kind of mobile phone connected to a standard GSM network?
If it is, then the short version of the story is "Forget it" even if the IP wasn't dynamic, you simply wouldn't be talking to the IP of your device, you'd actually be talking to the IP address of your providers GGSN, and for a standard consumer connection this is going to be where the buck stops.
Now that said, IF you have the budget, and all your devices (Assuming multiple ones) are with the same carrier, then you can approach the carrier and request a dedicated APN (Access point name) essentially what this is , is the mobile network equivalent of a DNS record, or at least similar enough to use that analogy anyway.
When you set up your data connection on your device you may recall having to enter something like 'pp.vodafone.com' or 'INGhub411.o2-uk.inbound' we'll this is your actual APN, and if you have a custom one they your devices can be set up so that the IP the presents itself at the GGSN actually has a static route back to the individual device in the suppliers GSM network.
As a general rule of thumb however on consumer grade connections this is not enabled and hence there is no ingress available to the individual device.
As zmbq says, the ONLY option you have is for the device to keep polling the server on a regular basis, and yes unfortunately that is going to be very unforgiving on the battery.
of course there is one other way of approaching this, and that's to have the device open a socket directly to your server then keep that socket open. Once the socket is open, 2 way communication can be performed across the link, unfortunately your going to also have to write all the code to manage this connection including, but not limited to monitoring the connection to make sure it's still open and re-establishing it if it's not, something which is incredibly important on a mobile device.

Categories