Getting the MAC address of the remote host - c#

I just a moment ago saw a request for finding the MAC adress of a remote host. An answer was that the MAC address is always sent as part of the TCP/IP protocol.
How would I go about retrieving this information from an ASP.NET C# application?
See: Reference to sister-post

Any such answer is false. The MAC address of an adapter is only available on the same network segment. Not on the other side of a router.

If your remote device is SNMP-enabled you can query it for its ARP cache. That will have the MAC address in it. See this FAQ entry for more info.

Simple Answer: You can't.
Complex answer: You can, but you need to read the output from "arp -a" command. That might get difficult, depending on how many connections you currently have through your NIC (wireless or wired).

The only way that I can think of to get this from the network itself depends on interfacing with network management software with SNMP or RMON. Currently, I'm using a Java Applet on the client (see my answer to the other question), but it has some limitations. I'm hoping to meet with our network engineers to see if there is a way to get the same information server-side by interrogating the network switches. If such a solution exists, it will likely be specific to whatever network management software you are running.

You can build it into the application itself and have the application send the mac address in any messages that it may already be sending. Other than that, John nailed it.
Edit: Oh, if this is a "web app" and the "client app" is a browser, yeah, no go.

Related

Which local ip connects to a remote ip?

Im sorry if this been asked before, but couldnt find anything about this particular matter.
I try to find out with which of my own ip's my computer use to connect to a remote ip.
I use some kind of socket setup both ways etc, and im sending my ip (and other stuff with xml) to another server so it knows how to contact me.
But how do i figure out which ip i should send to it?
I have the servers ip or host name, but trying with
Dns.GetHostEntry("host").AddressList
But that only gives me the ip of the remote host and not how my computer reach it.
Is my question solveable at all or is this just wishful thinking?
Can you have the remote host capture that data? That end would surely have it.
If you're using UDP then this is exactly what the STUN protocol was designed for. STUN is used in VOIP applications (among other P2P systems) to be able to tell what a specific connection looks like on the internet.
One very reliable .NET implementation that implements STUN is included in the Lumisoft.NET library (source code available here). I've used it myself for to satisfy this specific task for applications ranging from VOIP to P2P VPN alternatives. It is very easy to use and is standards compliant.
NOTE: I am NOT in any way affiliated with Lumisoft, I've merely used their library in several different applications
Ok, quick update that wont help anyone except myself.
But.
Found out that i can send hostname instead of actual ip, will work for now and most cases, and let the DNS do what its suppose to do :)

Two questions about DNS resolution using [System.Net.Dns]: how does it work and what port does it use?

1 - How does it work? This question has come up because I am working with a guy for whom the call to System.Net.Dns.GetHostAddresses does not work. I gave him a sample app that just calls the aforementioned method and displays the IP addresses that are returned. Here is his explanation after using it:
When I try to resolve www.google.com [in the app]
it does not
work on the local workstations,
because our internal DNS do not
resolve external DNS names. [But] If I put
www.google.com in the browser, the
proxy server knows to go to the
external DNS servers for name
resolution and it resolves the name to
the IP address. Even if we put the
proxy server name in the application
it does not work when testing the
resolution.
I have done some packet sniffing on my work computer (which has a proxy server) and my personal computer (which does NOT have a proxy server) and there seem to be no packets sent or received durring the GetHostAddresses call. So, what exactly is that call doing?
2:
What port number is used?
Also asked by the person I am working with. I have no idea about this one. If there are no packets sent then it seems the answer would be there is no port being used, but I am not very savvy when it comes to ports and things like this. If anyone knows how to answer this or even a process for me to find the answer, please let me know.
DNS is a huge subject. Concerning Q1, it depends on how your machine is configured (node type). Googling on this should help you on the general principles.
Here is an answer about the ports: Network Ports Used by DNS
The reason in browser works and in command prompt does not is that browser has been setup to use a proxy and command prompt is not.
In order to see what code is used, use reflector. I have been told off to post any Microsoft code but I looked at the code and it was mainly unsafe and Win32 API calls.
The reason you're not seeing any network traffic when you use the packet sniffer could be that google.com is already in your local client DNS cache. If your browser has resolved it, it'll be in the cache. Try ipconfig /flushdns from the command line, and then use nslookup again to resolve google.com. I think you'll see some network traffic then.

Multiplayer game

Using either XNA OR just the usual c# win forms application, which would be the best way to create the feature that enables text messages to be sent from me to my friend over the internet?
I don't know if this matters or not, but I now have 3 Mobile Broadband internet... so it's like... Wireless internet, does that matter?
It is quite difficult to "reach your friend over the internet", whatever the framework.
Reaching someone's PC requires knowing the IP address of the destination PC. If the PC is behind a router, the PC most likely has a private IP address, thus is unreachable unless the router implements some form of forwarding (in which case a given TCP port is forwarded to a given PC's internal IP address). Then, you need to configure the router to use DDNS so that your code can get at the router's IP address.
So, it is possible (using the classes mentioned by #Bob), but not for the faint of heart.
The way professional programs (like chat clients) do it is to have a central server that relays messages. The code running on your PC and on your friend's PC sends messages to this server instead of directly to each other, so one doesn't need to know the IP address of the other one. But going this route requires some form of hosting for the server PC.
You could look into the Socket class or the simpler TcpClient and TcpListener. Whether XNA or Winforms shouldn't matter. Using XNA on the Windows platform gives you access to the entirety of the .NET framework, including the networking aspects.
I agree with Timores, you can't directly reach the device of your friend and you will need a server for that. Sockets are not something operators allow when you are connected using 3G and not WIFI.
You are welcome to try if you like the Skiller SDK solution: www.skiller-games.com (full disclosure: I am from Skiller), we have distributed servers so you don't need to host anything and besides other tools you can use the built in social layer where your users will be able to make a list of friends and send messages to each other.
Hope that helps.

how can i send data over internet to any specific computer in subnet

I want to know how I can send or recieve data over internet to/from a computer in subnet
(this is specially in context to PPP users bcoz getting static IP is not so much in practice).
I actually want to create an application which can transfer file between 2 specific computer in WAN.
so what are things I need to know about to do the same..(ex. PRESENT IP or MAC ADDRESS etc..)
PROGRAMATICAL EXPLANATION ALTHOUGH PREFFERED,BUT IS NOT NECCESARY...
FTP?
There is a vast torrent of useful results in google, I seriously suggest to google before you ask here.
For instance, have a look at the top result: http://www.devarticles.com/c/a/C-Sharp/Network-Programming-in-C-sharp/
Apart from that, FTP, as suggested by Colin, may be what you're looking for. If you're new to using FTP in C# have a look at http://www.google.com/search?q=c%23+ftp
Put the information on a public IP server, so both computers keep polling if there is new data and send / download that data as needed. A single text file can hold necessary flags such as paths and other info you need.
server just need to be a web server, which means IIS or similar should be installed.
I'm thinking part of your question has to do with one of the computers not having a static IP address. If the two computers, A & B, don't know each others IP address, then an alternative is to use a server. Either A can store the information on the server to be picked up by B, or A can register his current IP address on the server for B to lookup and then connect to A, assuming firewalls don't interfere. Another scheme is that A and B can simultaneously connect to the server, and the server can relay data between the two.
Of course, all of this communication would be done using techniques such as those suggested by mafutrct and Colin.
This question sounds to me like the difficulties of NAT Traversal and trying to establish peer-to-peer connectivity over the Internet. I stumbled on this question researching the best way to set up Internet connectivity to a device that is sitting behind a firewall. I am assuming that the device behind the firewall has to initiate the connection, that a Internet server application (on a Public IP address) to at least manage the initiation of the connection is required, and the Internet server application may also have to act as a relay if the difficulties of NAT is not able to be traversed.
http://en.wikipedia.org/wiki/NAT_traversal
http://en.wikipedia.org/wiki/Peer-to-peer
A good example of this is LogMeIn. Where the application is installed on the computer that needs to be accessible over the Internet, that application communicates with the LogMeIn Internet servers, you can establish a connection to that computer either by initiating through the LogMeIn servers and connecting peer-to-peer over port 80/443 using NAT Traversal (this would be establishing a peer-to-peer connection) or by initiating through the LogMeIn server and the LogMeIn server acting as a relay if peer-to-peer is unable to be established.
Please note; you must have an application (something running) on both ends of the connection on the Internet, there is no other way. Just like FTP that was mentioned earlier (FTP Client, FTP Server)... but in this case it is peer-to-peer (basically you are writing your own Client and Server... or if you have to use a relay... Client--Relay--Server
http://en.wikipedia.org/wiki/LogMeIn
"The service connects the remote desktop and the local computer using SSL over TCP or UDP and utilizing NAT traversal techniques to achieve peer-to-peer connectivity when available."
The part I don't know is can the relay go in both directions; and I am figuring the device behind the firewall must have to constantly manage the open connection to the Internet Server that is the relay. Those are the questions I am wrestling with now.
Also, you may want to check out this post.
C# byte streams through TCP
From that time, till this date I have encounter so many problems with these four kinds of NAT that basically without an external server you cant do anything.
This is because, one computer can never find where the other one is located as suggested in the pic.
Here PC 'A' will never come to know about the port that corresponds to PC 'B' on R2B without an external server.Even more, if somehow u come to know about the ports you are still in a situation that the router wont allow you to access it if the request hasn't been made for your IP.

Blocking Connections By IP

I need to able to block any and all connections to my pc from a specific IP address , i know this is possible with a firewall but i need to do this in c#. Any idea how (need code).
Update :
Its a generic C# app not asp.net , target platform is WinXp till Win7
Need more information... if you're talking socket communication, you can simply close the connection to a client as soon as it connects if the IP address is blocked, or process the Connection Request and evaluate there.
Edit: Simplest way for you would probably just be to interact with Windows Firewall API... here's how:
http://www.shafqatahmed.com/2008/01/controlling-win.html
Your question is unclear but I'll try to answer the best I can, within my understanding.
Do you want to control machines from connecting to any port on your machine? if so, you need to control the built-in windows firewall or find yourself a filter driver you can control. In order to write your own filter driver, you must leave the land of managed code, so I am guessing that's not an option.
To learn how to control the firewall, here's a link:
http://www.shafqatahmed.com/2008/01/controlling-win.html
more on google.
Do you want to control remote machines from connection to a port on your machines that your application owns? You cannot do that either (see #1 above). However you can take action after the connection, and close the connection if you don't like the remote IP (check the remote endpoint's IP).
two caveats with this approach:
It doesn't save you from a DoS attack.
You will need to be careful if you need ipv6 support (you can't just check the IPV4 address in that case)
HTH
A "firewall" in c#?
First you would have to access the network interface on a low level, eg.: http://msdn.microsoft.com/en-us/library/ms817945.aspx
Then you have to parse all incoming packets and maybe discard them.
It's not an easy task and I don't recommend you to write a driver and a firewall in C#, because the .NET Framework will be loaded every time you start your machine.
Also traffic parsing can be tricky... I implemented a router/traffic analyzer in C# some time ago and it took me about one year to gain the experience with network programming to gain the knowledge to do this.

Categories

Resources