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

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.

Related

Create a web server with nodejs/http-parser

I want to create a small web server in c++ with http-parser from here nodejs/http-parser, I downloaded and I compiled what should I do next?
First of all, you need a way to communicate using TCP/IP in order to implement the HTTP server; depending on your case, you may also need to use other protocols, like UDP (for instance, if you need to resolve a domain name, you will typically use DNS) or ICMP.
If your application is running by an Operating System, take a look at its documentation about the networking facilities it can provide you. For instance, many operating systems provides a POSIX sockets API that will help you dealing with network devices and protocols.
If you prefer, you can use one of the networking libraries already written, provided they support your environment. They can help you with a useful abstraction of the network, so you can concentrate better on your application.
On the other hand, if your application will not be running by an Operating System, the first thing to do is to find some internet protocol suite that you will be able to use in your particular environment.
If you don't find one, you obviously need to implement everything by yourself.

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

Banning MAC address from accessing certain port - 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.

Linux ioctl -> how to tell if current IP was obtained by dhcp

I'm fiddling with the sockets ioctl's to get the current interfaces setup and I can already get the IP, interface name, netmask and check if the interface is up or down, (I just do IOCTl to SIOCGIFCONF, SIOCGIFNETMASK and SIOCGIFFLAGS).
I am looking for a way to tell if my current IP address was obtained through dhcp or if it was static.
I can check /etc/network/interfaces for everything I want, but I'm looking for a way to do it programmaticly (does this word exist?).
Does anyone have any insight into this?
One more thing, I'm working on Linux (for now).
Cheers
With the wide variety of DHCP clients on Linux -- pump, dhcpcd, dhclient, udhcpc, and quite possibly others that I do not know of -- this isn't possible in a general sense.
However, if you are targeting a specific distribution -- say, "default install of Ubuntu" -- then you can investigate solutions such as Stefan's. Note that all four of the DHCP clients listed here can be installed on Ubuntu and can replace the default DHCP client, dhclient.
If you're running Ubuntu, the leases are stored in /var/lib/dhcp3/dhclient-[interface_name].lease, maybe that's a start.
I don't think its possible to tell via a kernel interface (ioctl) if an IP address was allocated via DHCP as in most distributions DHCP is a userland app that just configures the kernel with data provided by a remote source as if the user had done it manually. In fact, if you look at the ISC dhclient it just passes the data received from the DHCP server to simple shell scripts that do ifconfig, route and various other commands that you could type as a user.
So you'll probably have to look at methods that are specific to your DHCP client and distribution as suggested by Stefan.

Get the ip addresses of an interface

Using the output of "ipconfig /all" i can get what I need but I want a more reliable technique to get ip of an interface (practically the interface has to be identified by it's name) in case of ipconfig was corrupted or was not available for any reason.
I'm not sure what you mean by "server and client", but if you're looking for an API that tells you the IP address(es) associated with the local machine's network interfaces, check out getifaddrs() (for Linux/MacOSX/POSIX) and GetAdaptersAddresses() (for Windows). If you want a usage example for those calls, have a look at the GetNetworkInterfaceInfos() function in this file.
(If you wanted to get the IP address of a client connected to the local machine, call getpeername() on the socket that is associated with that client's connection)
Since you mentioned ipconfig, I assume you want to do this on windows.
Windows has a set of IP Helper apis designed for retrieval and modify networking settings.
And here is a simple sample: http://msdn.microsoft.com/en-us/library/aa366298(VS.85).aspx