On Linux machine i would like to write a program to change the timeout value of DHCP connection (How long the DHCP client can wait before giving up).
i went through the https://askubuntu.com/questions/203157/timeout-in-a-connection-to-a-dhcp-server but this provides the change of configuration file manually.
Please provide some help
The DHCP client settings are stored in an ordinary text file, dhclient.conf. There's a timeout <time>; option that you want to add or change.
Related
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.
I am working on a project need to send periodic alive message to https server.
Because of security issue, we need to use minimal number of ports (blocking unused ports as many as we can).
I am using c++ libcurl easy interface to send https request in linux.
I have tried to use the same curl handler object (CURL object) and set CURLOPT_LOCALPORT to a port number. The first request is ok. But in the second, libcurl verbose mode said address already in use.
However, when I comment out the port set through CURLOPT_LOCALPORT, it works on second connection also, and by setting VERBOSE to 1, I can see "Re-using existing connection" print out, which is missing in version setting up local port.
And I check with linux netstat, find out that it is using the same port.
I cannot figure out why setting up local port will make it failed.
And also, I have tried to close the connection using curl_easy_cleanup, but due to tcp time_wait state, we cannot reuse the port in a while, that's not what I want.
Could anyone provide a solution or suggestion to us? Thanks a lot.
Edit
My reason using one port is not to keep opening and closing connection too much.
Because of the security issue ...
There is no security issue. You need to get over this phobia about using multiple local outbound ports. There is zero security benefit in using fewer, or in constraining them in any way.
I am trying to write an application in c++ using winsock.
I need to handle a case where default port used by the app is not open to use then what is fallback mechanism?
Server or client?
In the server, if the requested port is already taken, you log an error and stop. This is why ports are often stored in configuration.
For the client, if you cannot connect to the specified port, you log an error too. Its not much different from not being able to connect to any other network resource, like a URL in your browser.
That completely depends on you.
There is a mechanism called port knocking: The application just tries a range of ports until it finds one it can bind to. Obviously server AND client have to do this to find each other if the default port did not work out.
You could also just display an error message and let the user decide on how to deal with this.
Or you specify a fallback port in a config file...
Just to give you an idea.
In order to keep my VPN connection active, i wrote this little applescript:
tell application "System Events"
tell network preferences
connect service "VPNServiceNameIConfigured"
end tell
end tell
This script works fine!
I wrote myself a lauchdeamon .plist to call the script on StartUp, WakeUp and every 5 seconds. This means, that every time my vpnconnection breaks, it will be automatically reconnected (if possible) every 5 seconds.
This part works fine but i want to improve it a little. I want to use a if-case like
if network preferences service "VPNServiceNameIConfigured" is not connected
connect it
else do nothing
Is there a way to do that? If so i am very happy about an example or good documentation using applescript for handling system events.
Thank you!
The place to look for that information is in the Dictionary for System Events. You can open any dictionary using “Open Dictionary…" in AppleScript Editor’s File menu.
You don’t give enough information to write exact code; for example, does your VPNServiceNameIConfigured service contain any configurations?
If you can get a configuration, you should be able to check the “connected” of that configuration. Something like:
if connected of current configuration of service VPNServiceNameIConfigured is false then
connect it
end if
Depending on your setup, you might also be able to check the “active” boolean of service VPNServiceNameIConfigured. Here’s a simple test script that works on my setup to check that my WiFi is active:
tell application "System Events"
tell network preferences
set myConnection to location "Automatic"
--get name of every service of myConnection
set myService to service "Wi-FI" of myConnection
--get properties of myConnection
if active of myService is false then
display dialog "Need to reconnect"
end if
end tell
end tell
The “connected” boolean is only available on a configuration, however, and that may be your more reliable option, if your service contains a configuration.
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.