How to get IP adress with WINSOCK ? c++ - 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).

Related

How could I convert from IP address to MAC address

I want to store all the MAC the mac address which has access to my server.
All I know are only the IP addresses. All the machines are under unique gateway.
Could I got the MAC address from their IP address?
MAC addresses are not part of any protocol that gets routed, you will never get the MAC address of a machine that is on the other side of a router or switch.
They are are the addresses of the physical ports, not of machines (which are what you're taling to using IP).
If server and client are on the same network, you will have to use ARP. This protocol is designed to get the MAC address to a given IP address.
As soon as there is something like a router between client and server, ARP will only reveal the MAC address of the router since this is the target for your ethernet packets.
Simple answer is, no You Cannot get MAC Address from an IP Address for clients connecting to your server unless both the machines are on same Physical network
It is not possible to get MAC address from IP address. MAC address is a physical address of network device, and IP address is not.
IP address is not bound to one device in general.
I suggest you go through this
the post says
An IP address is usually assigned by the network administrator or internet
service provider, you are either provided a static one at the beginning or
given a dynamic one every time you connect to the network.
This is not true with MAC addresses as it is already embedded on the device
or the network card during manufacturing. It is supposed to be permanent and could
not be changed by anyone as it was meant to identify a specific network interface
card no matter where it is in the world.
There are however ways to change mac addess, but i don't think it is possible to get mac address using IP- Address

Getting all IPv6 addresses on a Linux server

I'd like to find all IPv6 addresses on all interfaces on a Linux server in a C++ program. I'm looking for the equivalent to GetAdaptersAddresses() on Windows.
I want to do the same as ip -6 addr show does. How ip do this ? strace didn't really enlighten me.

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.

get global ip address

How can I get ( in C++ ) the global IP address of my computer(windows XP)?
You can't.
You can determine the IP addresses on the various interfaces, and there may be more than one. These could be local area network IPs (10.0.0.0/8, 192.168.0.0/16, etc.), or they might be internet routable.
You seem to be asking "if I have 192.168.0.3, how do I get my Internet IP?" There is not function call to do this: such an IP might exist, it might not exist, there might even be more than one.
The closest you can get is to have a known computer on the Internet tell you: connect to some other machine, and ask them to send back what they think your IP address is. There are a few websites out there for this, some might even have APIs to do this.
I feel like some home-routers might be able to tell you through uPnP too, but again, this will not cover all possible cases.
On windows you need to get the local host name and then pass that to the gethostbyname function in winsock2 which returns the associated IP addresses.
Example: http://tangentsoft.net/wskfaq/examples/ipaddr.html
A stack overflow answer for Linux:
Get the IP address of the machine
You will have to make a request to a website like http://whatismyipaddress.com/ and extract the string in it that shows your IP.
Use this plain text IP url: get ip

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