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

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.

Related

How to get the TFTP server address from the DHCP configuration

I need to find out the address of the TFTP server that is specified in the DHCP configuration.
When the PC is started on, the computer receives the IP address via DHCP, then downloads the image from the PXE server. During the download of the distribution, I need to run a utility that accesses the Database to the server from which this image was downloaded (where the TFTP server is running).
In theory, it would be possible to register the necessary address of the TFTP server in the downloadable image of the distribution. But the bottom line is that such a scheme exists in various subnets. And specifying its TFTP server for each subnet is an irrational approach. It would be more convenient to get the address of the TFTP server from the DHCP server, which is listed there as next-server
I found something similar at Busybox.
Is it possible to implement something like this in C/C++ and how can it be done? I don't have any ideas.
Some example of the code of contacting the DHCP server to get an address or parameter. Open a socket and make a request or something like that... At least I haven't found any similar examples on the Internet.
P.S. I will put a dislike and send it to read man, than I will answer the question constructively. Nice!
If you are using the ISC dhcp-client then
man dhclient-script
If you are using some other dhcp client then it might have support for calling scripts too. Or it might store the dhcp lease somewhere in a parseable format, like /var/lib/dhcp/dhclient.leases. You can extract the dhcp options from that.

VMware vCenter Server 5.5 Single Sign-On Install finds wrong ip address for FQDN

I am migrating my vCenter Server 5.5 to a new server (databases have already been moved to a new SQL server and all is OK on existing vCenter Server 5.5 implementation). When I begin the simple install process on the new vCenter Server host the Single Sign-On component presents me with an IP address of 10.10.10.117 as the ip address of the FQDN file01.xxxxxxxxx.com. This is the iSCSI interface address. I need it to use the 10.1.1.17 ip address that is the address of the production NIC that the ESXi 5.5 hosts will be communicating with. I have already changed the binding order of the NIC cards and flushed the DNS cache. I also added file01.xxxxxxxx.com with the proper IP address to the hosts file and also file01 to the hosts file. Still, during the install, 10.10.10.117 is discovered. Thanks in advance! Babak C.
Just to get a quick clarification...are you freshly installing vCenter 5.5? Or are you migrating an existing vCenter server to a new host and using the update utility to upgrade? I am assuming you are doing a fresh install based on your details about the SQL server and SSO. Here is my suggestion, in case it is a fresh install.
We had a similar problem with 5.5 on a new install where the IP address that was discovered during the actual vCenter Server install was that of the public facing NIC which we never use for management traffic (it's for internet access on the vC server, for update manager, etc.)
The strange thing is that there had NEVER been an entry in ANY of our DNS servers for that interface. So, after looking into it a little bit, I started thinking the IP that was returned during install was not a DNS result at all. Rather, it was (most likely) simply gathered from the interfaces on the Server based on binding order (e.g. which NIC has the default gateway.)
In order to save having to uninstall and clean up a major mess if the install completed wrong, we stopped and got in touch with VMware support. They suggested we clear all of the temporary files both in the standard "temporary" folder on windows as well as under /ApplicationData/vmware/xxx, where 'xxx' would be whatever product is giving you trouble and HAS NOT been FULLY INSTALLED* (e.g. you started the install and noticed the incorrect IP, so you terminated the installer and there is metadata and cached files remaining from the partially run install).
Basically, what we had to do, was clear the temporary files and then make sure the NIC Binding Priority was correct (so you should check in Network Adapters|(press-alt)|Advanced Settings. Make sure the correct binding is checked (e.g. if you don't use IPv6 on the private network, clear it) and make sure that the Windows Network is at the top of the priority list on the second pane of the advanced settings. This helps tremendously with SSO by making sure the Windows Network stack is the first queried when you are signing in and SSO must submit a kerberos ticket to the AD DC for validation.
It is possible, that once you delete the partial install files and temporary files and fix the network settings (probably be a good idea to reboot as well), the next time you run the installer you might have success.
I will try to check this post later to see if it helped you at all... or it I just succeeded in making your life even more difficult (which I certainly hope not!) :)
One more thing...prior to initializing the installer, open up a PS session, perform ipconfig /flushdns and then ping the hostname of your vCenter server in order to get it in the DNS cache. You should also perform the following:
nslookup
NS>{your vcenter server IP address}
/* make sure the resulting hostname is correct..this ensures your PTRs and rDNS is working correctly. vCenter HEAVILY relies on accurate reverse DNS configuration...then do the following lookup for forward DNS */
NS>{your vcenter server FQDN}
Hope it helps. Best of luck my friend!
SIETEC

Are there any fast techniques to get all ip addresses in a subnet?

in previous question i try to get all valid ip addresses in a subnet , so i use for loop , but after i try it , i find that my code takes more that 2 minutes to find an all ip addresses in a sub net , and in some cases it's take more than 5 minutes!
My code is writing in C++ under Mac OS ;
but in windows , to do the same thing you only write net view/all command , and it print all ip addresses in a subnet in a moment !
and in MAC os you can use Bonjour service to do such job .
how these techniques work like this speed (Net view /all and Bonjour service ) ?
is there any way to do this job very fast like this ?
if not please tell me if is it there is APi to use Bonjour service directly into my code (C++) in mac os?
EDIT:
i found new idea
i found on apple develop some api called Bonjour API , my be it's help ,but how i can use it i c++ , because i know that mac use opbjectiv-c .
bonjour
net view /all is working on Windows networking level, not on IP level. It will only list machines with Windows networking and name resolution enabled. If you have computers that are not running Windows (or samba) they won't be listed. The same with printers, routers etc.
With IPv4 the best way is to extract the list of IP addresses in the current subnet by examining the IP address and netmask of your computer. Then force an ARP lookup to be done of each IP address. The ARP lookup will always work if the unit is present on the network, even if it is completely locked down (no ports open, not answering to ping).
With IPv6 you are essentially out of luck. The number of available IP addresses in a single subnet (18 446 744 073 709 551 616) is so wast that an exhaustive search is impossible.
You should work asynchronously. You need a function that issues a communication request but returns immediately like IcmpSendEcho2. You will have to create an array of completion events for each call. Each event handle is passed to one IcmpSendEcho2 call.
After that you call WaitForMultipleObjects waiting for ALL events set.

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.

how to know which installed devices has internet connection in WINPCAP?

i have known that winpcap library enabled a person to obtain advanced information about installed devices...
given all the networking devices found in the computer, how will i know which one of this has an internet connection?!
thanks:)
You typically don't care about this. Normally you just ask the network stack to make a connection, and don't worry about details.
However, each "device" will have its own IP address. You can request the network stack to use this IP address when making a connection.
Now, to figure which devices have an intenet connection, iterate over all of them, obtain their IP address, and try to create a connection from that originating IP to a destination IP on the Internet. Typically you already know such a destination IP, e.g. your companies webserver.
GetAdaptersInfo() will give you a list of all network adapters installed in a form that you can then use in GetIfEntry(). This latter function will tell you the operational status of an adapter, so you can at least tell if the adapter is plugged into a live hub/switch/router.
If you are particularly interested in IP connectivity, you could look to see if a default gateway is configured for that interface. Don't forget you might have to support IPv6 as well as IP4 if you try this.
Correlating the network adapters found by GetAdaptersInfo() with those found by pcap_findalldevs() is left as an exercise for the reader. I don't remember the details but it was fairly obvious.