Linux C API for getting remote server's hostname? - c

Is there a way(a C API?), using which I can get the hostname of a remote server. Something like gethostname() but having an IP address as an argument.
I know about getnameinfo() and getaddrinfo(), however I don't want the hostname used in the DNS server. I want the hostname which you get when you use the hostname command in linux. I have a feeling that it might be impossible to do without knowing the login credentials of that remote server but I'm not sure about it.

Although you can query DNS for hostnames, there's no standard protocol to ask a machine (an interface, really) what it calls itself (if it does even have a name for itself - that's not mandatory).
You'd need to implement and deploy a simple server program onto all the hosts you're interested in (it could be something as simple as adding a line to /etc/inetd.conf to run /bin/hostname if it's a Unix-like system), and a client library to access it.

Related

What is getservbyname() — do I understand it right?

I don't really know what to write in the first parameter of getservbyname().
I read that if I give the name and protocol such as TCP I get a struct back with information to the server, but what should I write in there?
So, for example, getservbyname("www.google.com", "tcp")
This is wrong but what needs to be in the first parameter to get, for example, the port from Google or other websites or am I understanding this absolutely wrong?
getservbyname looks up service names from this list and gives you back port numbers. "http" is an example of a service name that you can look up in this table.
www.google.com is a host name, not a service name. gethostbyname looks up host names and gives you IPv4 addresses; to connect to Google's web server you need its address as well as the service's port number. Nowadays it is usually better to do both lookups at once, using getaddrinfo, which also seamlessly handles IPv6.
"www.google.com" is a hostname, not a service. getservbyname() is just a fancy way to read information from the /etc/services file on your local machine, so the first argument to the function might be something like "telnet" or "ftp". i.e. it is used to find out what port a particular service is expected to be running on, on your local machine.
If you want to get information about a hostname (e.g. its IP address), you can get that via a different API call like gethostbyname(), or (for a more modern/flexible implementation) getaddrinfo().

RPC windows get client IP address

I have read loads of Microsoft documentation regarding RPC programming and still do not figure out how do we get from RPC server the IP address of the connecting client.
I am sure there is a simple way to get client IP address from server when connecting, but do not know how to do this :/
Thanks for helping, a simple pointer to a documentation would be great.
No - there is no documented way to accomplish this. Windows RPC by design abstracts the network transport (and associated metadata like network addresses) from it's clients.
If you really need something like this, you could bake it into your interface (e.g. implement a Connect() method where your client provides it's IP address that you could stash in a Context Handle). This assumes of course, that you can trust your clients to provide valid IP addresses...
It should be possible using RpcBindingServerFromClient. Quoting documentation for RpcBindingServerFromClient:
To query a client's address, an application starts by calling the RpcBindingServerFromClient function to obtain a partially bound server binding handle. The server binding handle can be used to obtain a string binding by invoking RpcBindingToStringBinding. The server can then call RpcStringBindingParse to extract the client's network address from the string binding.
UPDATE 16/05/2017: There is also undocumented function I_RpcServerInqRemoteConnAddress() that most likely return client IP address. But I didn't try it yet.

How can I connect two sockets on different machines?

I'm sure this seems like a simple question but I can't seem to find the answer anywhere. I've been playing with basic client/server code in C, basically the same stuff that's posted on any intro socket programming guide, and no matter how I vary the few lines of code I can't connect two machines over the internet. I can connect locally with 127.0.0.1 and all the examples work fine, but I want to be able to connect two separate hosts.
I've tried using IP addresses - ifconfig and curl ifconfig.me return different IP's for every machine or remote box I have access to and I've been told this means they're all behind NAT which means I can't connect with an explicit IP.
I've tried using username#hostname with gethostbyname() - I've been using whoami and hostname on the serving machine to determine the hostname the client should use, however the DNS always returns an error. How should I determine the hostname I should supply to the client so that I can connect?
Other than that I'm not sure what the issue could be because I'm using the same socket code posted all over the web and I've checked every line many times against several different examples. This has been frustrating me for a long time, any help would be much appreciated.

Using gethostbyname() function on an external device

I'm using the client server program in C language on a Linux OS. In a first step, I programmed the code on the same machine using "localhost". Therefore, for the client to get the server information, I would use
server = gethostbyname("localhost")
since both the client and the server are running on the same machine.
However, now I would like to connect to an external device (using ssh). The device's username is of the following type
user#foxboard1
Although, I am not sure how to use the gethostbyname() function in this instance. I have tried
server = gethostbyname("user#foxboard1")
but that doesn't seem to work.
Any help would be appreciated!
P.s, I cannot copy and paste the code since it's on a different machine
EDIT
Instead of retreiving information about an external device, I would like to connect to the device using its IP address (an ssh connection through the connect() function.
gethostbyname() and its replacement getaddrinfo() are supposed to transform a host name or strin representation of an IP address into a "usable" structure.
This works very well on things like localhost, www.google.com, 1.2.3.4 etc.
But if you have a username embedded in the string, you'll have to split them up.
Of course, you'll have to become aware what kind of connection you want to establish and what the username is useful for.
If, as it seems, you want to do a SSH connection, it is probably best to use popen() and alike functions to start an external ssh process.

How to check if an IP address corresponds to localhost in C

In C on linux, is there a canonical way to check that an IP address corresponds to localhost?
That is, I'm looking for a function is_localhost such that if my computer has an external IP of "1.2.3.4", then calling is_localhost on any of "localhost", "127.0.0.1" or "1.2.3.4" should return true, and any other IP will return false.
On a side note, how difficult is it to spoof this information - does checking that the host is localhost in this way guarantee that the request actually came from this computer?
For context, I'm writing a management interface for a server. I'd like to make the read-only management bits, like viewing a list of connections, available over the network, but for anything dangerous, like manually killing a connection, you should be doing it by running a script on the server itself.
Thanks!
I think you may be trying to solve your problem in the wrong way - If you want to restrict access to your remotely accessible application by checking if its the local host or not then checking the IP address would be a very bad way to do it. A PC can have any number of easily configurable network interfaces with IP addresses of your own choosing. So it would be very easy to work around.
You may want to look into adding some basic authentication or simply don't allow certain functions to be run remotely. There would be many ways to achieve this, but I think the scope of the question ends here with -- Don't rely on checking for the IP address. :)

Resources