Getting all IPv6 addresses on a Linux server - c++

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.

Related

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).

Easy Method to Verify IP address has been issued

I'm writing a C++ program on a linux box (DHCP client) that depends on the connectivity of the network. I need to pragmatically verify that my system has a IP address. I know this is a general and open ended question, so any quick and dirty solution will work for me, but ideally I would like to check/read a system file to ensure the DHCP client has received an IP address from the DHCP sever.
Thanks in advance.
Just try any operation that requires an IP address and that should work if there is one. A DNS lookup comes to mind.
Maybe have a look at the source of say ifconfig, since that gets the ip address if one is assigned. A quick strace suggests that it might be an ioctl such as
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("<my ip>")}})
and a grep of /proc/net for my IP address suggests that if you know how to parse /proc/net/fib_trie you might be able to get it from there.

Socket application does not work over the internet

I've got a problem in socket programming. I'm currently writing a simple server/client application which asks connecting clients to answer a survey (I'm following the exercises in the book: TCP/IP Sockets in C: Practical guide for Programmers). It works fine in my local network (using localhost to connect), but I can't make it work over the internet.
Since I changed some of the code found in the book, I tried to compile the original source code from the book but it still doesn't work. I assume this is not a code problem but a network problem.
I did some research and so I turned off my firewall, I forwarded the port I'm using (12543) in my router but it still doesn't work... I've got a remote windows server running Windows 7 for testing: when I run my server on it and try to connect from my computer it fails, when I try to run my server from my computer and try to connect from the remote windows server: it fails again. Even when I run my server on my computer and try to connect with the client from the same computer using my private IPv4 address 192.168.x.x or my public one, it fails ! Oh, and there is no firewall running on the windows server.
I really don't know what to do now... I can ping my windows server from my computer, I can ping my computer from my windows server, but it's impossible to connect to my application.
The source code can be found here: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/textcode.html (SurveyServer2.cpp, SurveyClient2.cpp, SurveyCommon.h), but I don't think it's a code problem.
Please tell me if it's not clear enough. And excuse me if I did some grammatical errors, I'm french!
Any help would be appreciated, thanks!
EDIT : Ok, I know what's going on: it's a compatibility issue between IPv4 and IPv6! It's not properly working yet, but I now know what to fix. Thanks everybody :)
SECOND EDIT : Well, I think I finally understood. I was binding my server to a IPv6 address, but the host my server is running on only have a public IPv4 address. When I tried to connect, the DNS resolution only returned a IPv4 address so I was unable to connect to my server. I told to getaddrinfo to return only IPv4 addresses, so now it binds on a IPv4 address and it works fine.
There is surely a way to add a IPv6 address to my host but I don't think I will need it, it works okay right now, I hope this doesn't cause any trouble.
The code makes usage of getaddrinfo and need a hostname not a IP address.
It does sound like you have a connectivity issue or a DNS issue.
Ensure the name resolution is working:
Enter 'ping server_NAME' on the command line of the client machine.
If it can't find an IP address for the server name that could be your issue.
Ensure connectivity:
On the server command line:
enter 'netstat -an -p TCP'. This will show a list of all programs listening and their port numbers. You should see your server listening on port 12543.
On the machine where you are running your client program:
Use telnet to see if you can get through to the server. You can set the port number telnet uses with a command line option. Usually something like 'telnet -p 12543 server-ip'. If it says 'connection refused' then there's a connectivity issue (a firewall/etc). If it opens a connection you will get no error message and you will be able to type text to be sent to the server. You really only care if telnet was able to establish a tcp connection here.
If Telnet does connect then your issue is communication between the programs and not a network issue.

A Reliable way to Identify a computer by its ip address

I have a network of computers that they will connect to the a server with DHCP, so I don't know what Ip address a computer will get when I connects to the server. If 192.168.0.39 for example is connected to the server can I identify the real computer behinde this ip address? ( I can install an external application on each client in order to send some data to server for example mac address or so... )
If you are responsible for the DHCP server, you can configure it to hand out a specific IP to a specific MAC. Having done that, you can be reasonably confident of that mapping -- it is possible to spoof MACs, so if you are worried about security, you'll need a much more heavy duty approach. If this is a casual application where the risk of that is low, you configure your DHCP server to hand out IPs based on MACs and then make use of those mappings in your application.
You might not even need the IP address. On an Ethernet network, all communication from a computer, whether it's IPv4, IPv6, or even IPX will be labelled with a MAC address that's stable over time and unique per network card.

is ipv6 backward compatible with ipv4?

I've got a little udp example program written using ipv4. If I alter the code to ipv6 would I still be able to communicate with anyone using the listener with an ipv4 address? I was looking at porting examples at
http://ou800doc.caldera.com/en/SDK_netapi/sockC.PortIPv4appIPv6.html
I'm not sure if simply altering the code would ensure that it worked or if I'd have to write it in duel-stack mode.
Yes and no... IPv6 does contain completely different addressing, so you'll have to recode your app to use the alternative headers and structure sizes.
However, the IPv4 address range is available within IPv6, the syntax is to add two colons before the standard address (eg ::10.11.12.13). You can also embed IPv4 addresses within IPv6 packets.
Not without the assistance of an IPv4/IPv6 gateway in the network, and even then communication will be limited by the typical problems introduced by network address translating gateways. The traditional advice for programmers facing decisions like this is to recommend supporting both IPv4 and IPv6 at the same time.
IPv4 and IPv6 are inherently incompatible with each other.
A few basic reasons:
the address space is completely different (IPv6 has 128 bit addresses, IPv4 has 32 bit addresses)
the protocol header of IPv6 looks nothing like the protocol header of IPv4. if you try to parse an IPv6 packet as IPv4 you'll get nonsense.
The obvious result of these is that if you open an IPv6 socket you can't listen to it using an IPv4 socket.