Client Mongoose C app to query LAN DNS? - c

I'm experimenting with Cesanta's Mongoose server/"client" networking library in C. This is in Windows 7 x64 using Pelle's C. I'm trying to make a LAN-capable text messaging app on port 90 as two dialog-as-main executables (server and client) to support 20 connections to the server. FWIW, I'm using a fast timer to call mg_mgr_poll(&mgr, 0) which retains UI responsiveness and uses very little CPU.
Cesanta gives good examples of server communication setup and that works flawlessly. I can also get a client to connect to the server, but the server's IP address must be used in mg_connect().
Since the server's hostname is public to the LAN, I was hoping to ask the LAN for this host's IP. I had this working in VB awhile ago:
Dim host As New IPHostEntry
host = Dns.GetHostEntry(hostName)
Return host.AddressList.GetValue(0)
However as the VB project got bigger, VB got stranger and much more difficult, so I migrated to good old C. (Perhaps it was falling back to a NetBIOS name query, dunno.) No, I'd rather not migrate again to C# or C++/VS.
The Cesanta documentation on DNS resolution is quite vague. Code like mg_connect(&mgr, "MyServerPC:90", ev_handler); simply crashes, despite mg_connect stating it will try and resolve hostnames.
Can Mongoose resolve a local (LAN) hostname, and how? Or is this futile, and should everything be done manually in Winsock?

i have the same issue as you.
First i think that the mg_connect should not crashes, only if you try to do something with the returned connection which is null.
Second, mongoose try to resolve the address using DNS from specific location at the registry and it's take the first address he can find.
The location is :SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces
,check here in each of the interfaces.
i think there is a bug here (i am going to open an issues for it and also try to fix it and created a pull request).
Little update:
i have opend an issue and create a pull request about this issue.
https://github.com/cesanta/mongoose/issues/832
https://github.com/cesanta/mongoose/pull/833
Mongoose check each interface for the Value of NameServer and DhcpNameServer ,and return the first one (which is not empty) it can find but if it found an empty value for NameServer it doesn't check DhcpNameServer(and therefore miss the correct address) which will cause an error and direct the name resolving to a default server(i think it's google DNS -8.8.8.8).
You can fix this by going to the registry and adding the value of your DNS address to the registry key-NameServer
this is a comment from mongoose code:
/*
* See https://github.com/cesanta/mongoose/issues/176
* The value taken from the registry can be empty, a single
* IP address, or multiple IP addresses separated by comma.
* If it's empty, check the next interface.
* If it's multiple IP addresses, take the first one.
*/

Related

Discover and connect to the devices running the same app around globally without a server

I want to create a mobile app which can connect to devices having the same app installed without having a server in between. The devices should act as server and client and discover nodes similar to them , like how it happens in block chain?i firstly want to know the discovery protocol that how these devices will discover each other . I have seen the samples of sockets , TCP connection but they know the ip address before , in my case we donot know the ip address , have to discover similar nodes also with security like cryptography happens in block chain so , random device don't mess up with my chain
I am starting to learn about it, and one of the first things that came to my mind is RPC, because is how Bitcoin works: link, and you can check also de P2P section in the link.
I will follow this and update my answer with the things I'll find.
Good luck.

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.

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. :)

Find machine name and IP address in OOB SL5 app

How can I obtain the client machine name and IP address when running my Silverlight 5 application out-of-browser (i.e. installed on the local machine and NOT running via the web)?
It looks like there's no way to gather this information using just Silverlight. You'd have to do something like this. The short version is you use an AutomationFactory to create an unmanaged object which contains that information (in this case, a WMI Win32_NetworkAdapterConfiguration object). That means this probably won't work on a Mac. If you need something truly cross platform, it seems like the way is to create a really simple web service that simply returns the requestor's IP and hostname.
I dont know what is your exact requirement .We had similar requirement some time back to get the local ip address to call a local service which is supposed to be present in client machine.
In that case we just built the local service url by hard coding the loopback address.ie localhost.

Resources