hg_git hg_git - 3 months ago 19
HTTP Question

sending IP address of machine to server

I need to send a simple request to one of my server on defined intervals, let's say one every 2 seconds, in order to tell my server my machine's IP address(since I've dynamic one). I'm currently doing it in a while loop with a delay for

call for curl command with
--silent
option and redirecting rest to
/dev/null
. Something like this -

curl -s 'http://example.com' > /dev/null


The server currently parses the request and finds out the required IP address from it. Is there any other way to do this?

Answer

Another alternative would be sending a simple UDP datagram packet. The server can obtain the sender's address, upon receipt, equally well.

That requires a little bit less overhead than establishing a TCP connection. Of course, UDP offers no guarantee of deliverability, and an occasional UDP datagram would be lost; but since you're pinging the server regularly that should be fine. More importantly, however, is that a UDP sender's IP address is trivially forged. Whether or not this is an issue for your application is something that only you can determine.

If you're going to stick with TCP, one thing you can do is to establish a socket connection yourself. Let's examine what executing system(), just for the purpose of having curl do a dummy connection, involves:

  1. forking a new child process.

  2. the child process executing /bin/bash, passing it the command to parse.

  3. Bash reading $HOME/.bashrc, and executing any commands in there. If your shell is something other than bash, the shell will have its own equivalent of a startup script to execute.

  4. The shell forking a new child process.

  5. The child process executing curl.

  6. The loader finding all of the libraries that curl requires, and opening them.

  7. curl now executes. Only then, after all this work, there will be code running to open a socket and attempt to connect to the remote host.

Steps 1 through 6 can be trivially skipped, simply by creating a socket, and connecting, yourself. It's not rocket science. socket(), connect(), close(), that's it. This task does not require HTTP. The server process only needs to socket(), bind(), listen(), and accept(), and then obtain the IP address from the connection. Done.