Get ip address of computer on network c++ - c++

I am trying to make a C++ program in Windows that will output all of the ip addresses on the network to a text file but I have not been able to find anything useful online for other computers on the network. I figured out how to query the registry and find the local ip but I dont even know where to start to find other ip addresses. I don't want a copy-paste cookie cutter code because I want to learn how it works so if I could just get a link or a book that will guide me in the right direction that would be great.

For what purpose do you want to gain these IPs anyway?
I suspect what you're looking for is a way to obtain all possible IPs in a given subnet, which can be done quite trivially for say, IPv4. You simply take the IP address of your machine or router, and apply the subnet mask to it using bitwise and. (eg, 192.168.1.20 & 255.255.255.0 = 192.168.1.0, called the base, or network address). The valid hosts in this network are basically all of the bits which are zero in the subnet mask (255 in the previous example). This means the range 192.168.1.0 - 192.168.1.255 (exclusive, as the network address and broadcast address are not valid IP addresses of hosts in the network) are valid IP addresses of hosts.
There's no truly reliable way for any machine on the network other than the router to obtain specific information as to which of these addresses are in use though, as the interaction between machines on a network is meant to be voluntary, not implicit. Most machines however are generally running some kind of protocol which enables them to discover each other for purposes like printer and file sharing.
You can attempt to ping each of the possible host addresses in the network, or port scan, or talk to them with some protocol they might know. Alternatively you can use the broadcast address with a known protocol to ask all machines on the network to report back to you, and if they're running some software which talks that protocol, they'll respond with their own addresses.

Related

What happens when a private IPv4 is assigned to multiple network interfaces?

I am reading this AWS networking doc. It mentions that a secondary IPv4 can be reassigned to another network interface. I have the following questions regarding this behavior:
Will the traffic for the same IPv4 ever need to differentiate these network interfaces this same IPv4 assigned to?
Why doesn't IPv6 support this reassignment of network interface?
What defines an IPv4 to be primary or secondary?
Are those behavior mostly standard or something specific to AWS?
I am not sure if my questions even make sense. My understanding is that a traffic is targeting an IP not a network interface, but the traffic has to go through network interface first which sounds like just a grouping of IPs. Anyway, thank you in advance!

IPv6 Network is unreachable (os error 101)

Trying to create tcp socket connection between via an Ipv6 address, I get Network is unreachable (os error 101)
As a binding local address is used fe80::850***.
Probably it's because of fe80*** is local ipv6 address generated by OS. Is there a way to provide correct configuration for system to make a call via IPv6?
Short answer:
Your IPv6 connectivity is not setup correctly. This is most probably no configuration problem on your machine, but on the gateway router.
Long answer:
You get a "Network is unreachable" error because your operating system does not know how to reach the destination address. Usually it maintains a table called the routing table, where it looks up which network path leads to which ip subnet. It seems that in your case, there are several things missing.
Your interface probably has no configured non-link-local (fe80::/64) address
There is no routing table entry for the destination address range
In the IPv4 world these missing things are usually resolved by a DHCP server. In the IPv6 world, there are two possible configuration options. Either DHCPv6 is used or the IPv6 Stateless Address Autoconfiguration is used. It seems that neither is setup in your network. Note that correctly setting up one of these requires you to have administrative access to your gateway router.
As it required quite long time to find the main reason of the problem, will try to share the solution/investigation steps:
To make sure ipv6 is not configured properly just run 'ping6 ipv6.google.com'. It should return some error message like 'connect: Network is unreachable' or just stuck.
AWS ec2 instances by default are not assigned ipv6 global routable addresses(only ipv4 is assigned). Ipv6 addresses like 'fe80:*' are local routable and can't be used for global requests. To check the list of ip addresses, check eth0 config /sbin/ifconfig. Btw, AWS provides interface to generate global routable ipv6 address for ec2 instance (just check vpc/ec2 console pages - https://docs.aws.amazon.com/vpc/latest/userguide/vpc-migrate-ipv6.html)
Check security group of ec2 instance and make sure that in route table ipv6 traffic is allowed.Specifically inbound and outbound rules should include '::/0' for ipv6(or one of them based on the need).
Try 'ping6 ipv6.google.com' again
Run the following command to check whether IPv6 is enabled:
ip addr
If only an IPv4 address is displayed, IPv6 is disabled. Enable it by referring to this tutorial.
If a link-local address (starting with fe80) is displayed, IPv6 is enabled but dynamic assignment of IPv6 addresses is not enabled.
If an IPv6 address other than fe80 is displayed, IPv6 is enabled and an IPv6 address has been assigned.

TCP Chat not working in different networks

I coded a TCP/IP Chat Server/Client in C/C++ that works perfectly in my local network and with several clients connected to it. It does not work when the Server is in a different network to the client. I coded time ago the same tool in Python and the same happened.
Is there any way of solving this? Without port forwarding, that is the most common solution.
I could only find this: Android server concept confusion. How to connect TCP chat app on different networks but I could not understand it.
NOTE: I have just find a sample program (server) that works when it is on a different network; http://www.codeproject.com/Articles/1891/Beginning-Winsock-Programming-Simple-TCP-server
You have three choices:
1) NAT penetration. Both devices simultaneously attempt to exchange data with each other, tricking each person's router into thinking that it's replying. For this to work, you each need each other's public IP address and if you need any features from TCP, you need to implement them yourself.
2) Public rendezvous server. Both devices automatically connect to a server on an unNATed network. The server knows the public IP address of every client that connects to it, so it can route your data to the client with the correct public IP.
3) Some combination of 1 and 2. Here, a public server is used to facilitate NAT penetration and eliminate the need for manual coordination. Your friend registers with the public server and the public server tells him your public IP address and facilitates NAT penetration.
In local network addresses of devices doesn't change offently. Yo can reach the computer by only defining the local address. On the other hand, if you want to communicate with a device in different network yoou should know its public IP and should define a routing to the port.
If you have a server with static IP or known IP you can reach it. You can store the IP addresses of clients dynamically in this server. Write a program that inform the IP of host computer to the server. In that way, you can store the IP addresses even they change oftenly.

Pass along client ip address

So I have a system using ZeroMQ as the message controller and because it doesn't allow you to find information about a clients address that is connecting or sending messages I need to pass in self identifying information containing port and ip address of the node. However I'm not sure the best way of doing this.
I'm stuck because if they connect using an internal ip address for the network I would obviously need to use that ip address but if its going over WAN then I need the public ip address. What is the best way of communicating the ip address of the machine without knowing whether or not you are connecting to a local machine or a machine over WAN

Setting Linux IP with C/C++

I am trying to find a way that I can effectively change the IP and netmask of the computer that my programs are running on. I have to be able to maintain communications with a "box" that the user has access to and can change the IP of that box (but not that of the Linux host). Luckily the box broadcasts its IP over a multicast address. I am able to read that address and communicate to whatever IP the box is broadcasting. However, my problem is that when the box changes IPs outside of the subnet of the Linux host, I lose connectivity (as I would expect to happen).
As a result, I need a quick and dirty way to to change the IP and netmask of the Linux host when it detects that the IP being broadcast is outside it's subnet. I am aware that I can modify/edit the /etc/networking/interfaces file, but I was hoping to have something "live" where I wouldn't need to restart the networking interface or Linux host. Additionally, I would like to maintain all sockets that I have currently open during the change. My main issue is that I have a slew of connections run by other applications that cannot be corrupted as a result of the IP changeover. Also, if possible, I'd like to avoid having to redo having to run the multicast route add command for Linux after the host IP changeover.
Is there anything that I can do to satisfy these requirements? Thanks.
You don't want to change the IP address of the interface, you want to add an additional IP address and subnet to the interface. Search your Linux system documentation for IP Alias functionality.
Here's the HOWTO.