Banning MAC address from accessing certain port - C++ - c++

I want to stop someone with a certain MAC address from accessing a certain port on my server, I'm using this as a sort of hardware ban for a private server a friend of mine runs.
I am looking to do this in C++, and would like to know what I would need to research in order to do it. The server runs Windows.
Also, how would I find out the MAC address of the person accessing? Thankyou.

Filtering on MAC addresses is only useful if the server and client are on the same LAN.
The server will see the MAC address of the nearest upstream router, not the client's
MAC address.

Application-level sockets do not allow for MAC filtering. The only way to get the MAC is to have direct access to the TCP/IP headers themselves, which sockets do not provide access to. Unless you use a low-level intercept driver, like WinPCap, then you are just better off putting the server behind a real hardware firewall/router and let it do the MAC filtering for you.

While I can't answer your question, MAC addresses now tend to be set in software, so can be changed pretty easily.

Related

How to display names and IP addresses of devices connected to a network using C++

I am making a program where I have to use TCP/IP connection to transmit and receive data between devices. I would like to be able to detect the names and IP addresses of all devices that are using the network(The device running the program is also on the network). I am using C++ on windows, I looked through the Windows Native Wifi API but couldn't find anything. Is there an efficient way to get the names and IP addresses of all devices connected to the network?
There are a few competing techniques, such as Apple Bonjour. That's pretty much a clue that there is no single way that gives you all names of all devices - this is not standard IP-level functionality.

How to get IP adress with WINSOCK ? c++

I'm not familiar with networks and etc so I dont get how to use it, so if anyone can help me how to get ip adress of my computer with it and I also have to get MAC adress too.
WinSock is a Windows-specific API. Most socket APIs in general do not provide information about local IP/MAC addresses. You might be able to use getaddrinfo() to query the IP addresses for localhost, but that would be implementation-specific whether it works or not, and it would not include MAC addresses anyway.
On Windows, the correct way to get the IP and MAC addresses for the local machine is to use GetAdaptersInfo() or GetAdaptersAddresses() to enumerate the local NICs.
On POSIX-based platforms, including OSX, you can use getifaddrs() to get the local IP addresses (family AF_INET/6) and MAC addresses (family AF_LINK).

Windows WiFi network devices

I'm creating a WiFi program for Windows, I'm new to network programming.
I'm using the Native Wifi API to get information about a network but now I want information about the other devices that are connected to a network.
Does anybody know what I should learn to accomplish this? Do I need to use winsock?
You can do this via UPnP (assuming your AP supports UPnP, but most do).
You'd connect to the WLANConfiguration service of your UPnP access point, and read the TotalAssociations to get the number of associated devices, and the AssociatedDeviceMACAddress and/or AssociatedDeviceIPAddress variables to get the addresses of the associated devices. The latter might give you IPv4 or IPv6 addresses, or it might give you host names.
The TotalAssociations variable is "evented", which means you can have the access point tell you want the number of associated devices changes, and re-enumerate their addresses when that happens.
Microsoft also provides a UPnP API that may be helpful (though I've never used it personally, so I can't say much more about it).
References
UPnP Architecture specification
WLAN Configuration Service specification

C/C++ detect network type

I need to write a win32 c/c++ application which will be able to determine whether the PC it's running on is connected to one of 2 networks. The first network is the company LAN (which has no internet connection) and the second network is a standalone switch with a single PC connected to it (the PC that the program is running on).
I'm pretty new to network programming but so far I have tried testing to see if a network drive which is held on our LAN can be mapped. This works fine if the PC is connected to the LAN, the drive mapping succeeds so so LAN detection is successful. However, if the PC is connected to the switch, this results in a VERY long timeout which is not a suitable as it will delay the program so much as to make it unusable.
Does anyone have any alternative suggestions?
I'm using c/c++ in VS 6.0
[Update]
Whilst trying a few different ideas and looking at some of the suggestions below I thought I should update with some additional information as many (if not all) of the suggestions I don't think will work.
(1) The aforementioned LAN has no external connections at all, it is completely isolated so no resolving of external DNS or pinging websites is possible.
(2) Hostname, MAC address, IP, Default Gateway, Subnet etc etc (basically everything you see in ipconfig -all) are all manually configured (not dynamic from the router) so checking any of these settings will return the same whether connected to the LAN or the switch.
(3) Due to point (2), any attempts to communicate with the switch seem to be unsuccessful, in fact almost all networking commands (ping, arp etc) seem to fail - I think due to the machine trying to connect to the LAN when it isn't there :-(
One thing I have found which works is pinging the default gateway IP which times out when connected to the switch. This is sort of ok as I can reduce the timeout of ping so it doesn't just hang for ages but it feels like a bit of a hack and I would certainly appreciate any better solutions.
Thanks
As far as TCP/IP is concerned there is no such thing as a LAN on WAN. There are a set of non-internet routable addresses like 192.168.x.x and 10.x.x.x but these are sometimes used by ISP short of IP addresses.
You best bet is to use Asynchronous APIs when making TCP/IP connections. WIN32 defines a whole buch of OVERLAPPED APIs for this purpose. This will prevent your application from grinding to a halt while waiting for a remote connection.
Alternatively put the socket stuff into another thread and then only notify the UI when the operation is done.
I would first try to differentiate between the two using information available locally--that is, from your computer. Does the output of ipconfig /all differ depending on which network you're connected to? If so, exploit that difference if you can.
Is it possible to get the MAC address of the standalone switch? Of the switch that controls the company LAN? That would be a sure way to tell. Unless somebody cloned the MAC address.
If you try using the existence or non-existence of some network service to determine which network you're connected to, you can never be sure. For example, if you failed to map that network drive, all you know is that the network drive isn't available. You can't say for certain that you're not connected to the company LAN. Same is true if you use ping. Lack of response from a particular machine means only that the machine didn't respond.
Various things you can look at for differentiation:
DNS domain name (GetComputerNameEx)
MAC address of gateway (ping it, then GetIpNetTable)
Routing table(do you have a gateway and default route on the company LAN)
WNet discovered network resources (WNetOpenEnum, WNetEnumResource)
Ability to resolve external hostnames (try a 5-10 names like www.google.com, www.microsoft.com and so on, if one resolves you should have internet)
You'll have to decide how many indicators are "enough" to decide you're on one or the other LAN though if tests fail. Then keep retrying until you have a definite result.
http://msdn.microsoft.com/en-us/library/aa366071%28v=VS.85%29.aspx has a lot of network related functions that you can experiment with to create further indicators.

Portable C++ approach to listing all adapter IPv4 addresses?

I want to generate the list of IP addresses on the local machine using C++. I looked at boost and it doesn't seem to have any function to do so.
I need this because I want to see if the host name/IP address entered by the user is for the local machine.
I want to see if the host name/IP address entered by the user is for the local machine.
In general, you cannot do that. There could be any number of host names registered for the local machine. You could try to open a socket and see if ends up at yourself though.
If you are looking for portable solution, try ACE library. This library provide cross-platform functionality for network applications development.