Have TCP client search for TCP Server - c

Background
I am using a SparkCore wireless arduino board to connect to a local Node.js server. The server includes a local intranet TCP server that a TCP client programmed onto the SparkCore connects to.
Problem
If I run the server on a different network, the server has a different local IP address. When I do this, I have to reprogram the SparkCore arduino to tell it the new local IP address of the server to connect its TCP client to. This is not ideal for a variety of reasons.
Question
Is there a way to have the client dynamically search for the TCP server or alternatively have the server broadcast to TCP clients in a way that would inform the client of the local IP address to use for the server without initially hardcoding it? I would love to do this in way that did not involve iterating through a bunch of IPs on a specific port to see if a connection is made. That being said, if that's the only way to do this, then so be it.

How is the arduino booting? If it's booting using DHCP, one method would be to provide a customer DHCP option that provided the address of the node.js server. ntp, for instance, can configure itself in a similar way. This has the advantage that the arduino need not be on the same local subnet as the node.js server.
An alternative (slightly disgusting) would be to use an A record within your domain (let's say nodejs.example.com. Configure the local DNS recursive server to explicitly return this value (I am presuming you might have lots of different deployments with lots of different nodejs servers).
A third possibility would be to send out some form of discovery packet, either by broadcast, or better by multicast UDP. Assuming it's on the same LAN, the nodejs server could then reply. Clearly you might need to concern yourself with a rogue server impersonating your nodejs server, and therefore might need to add some security (e.g. use a shared secret, send a random nonce plus the nonce hashed with the shared secret to the server, the server checks the hash, and replies with the answer, the nonce, plus the answer hashed with the shared secret and the nonce, each of which the client then checks).

Related

Can server know the hostname of client in socket programming C

In socket programming communication between client and server.
when the client sends a message to the server.
Can the server know or determine the hostname of the client?
Does it has to be passed manually by the client?
The normal way to pass message to server from client is write(fd, clientMessage, sizeof(clientMessage));.
Suppose there is some header information to be shared also like say hostname.
Does that has to be passed like this only? Or there is some better alternative?
Can the server know or determine the hostname of the client?
No. The server has only the clients IP address. While it can try a reverse DNS lookup the information returned by this are unreliable, i.e. there might be no DNS PTR record setup in the first place or the PTR record might just claim an arbitrary mapping between IP and hostname, like claiming to be google.com.
Does it has to be passed manually by the client?
There is no automatic way in TCP to do this. But like with a PTR record - the information is not reliable since the client can claim to be anything.
The normal way to pass message to server from client is write(fd, clientMessage, sizeof(clientMessage));. ... Does that has to be passed like this only ...
It does not matter how it gets passed. All what matters is that client and server agree on the same way, so that the server can actually extract the information from the data.

Can a server directly connect two sockets that it is connected to?

Say I have a public server on the Internet, written in C, that is connected to two independent clients. Both clients initiated the connection. One client is (e.g.) an iOS app, the other is a native Windows app.
The purpose of my server is to allow these clients to send text messages to each other. Client 1 sends a message, the server receives it, and then forwards it onto Client 2. The same thing happens in reverse when Client 2 sends a message.
This feels inefficient. What I would really like is for both of these connections to contact the server and then for the server to connect these two clients directly to each other - after which my server can forget both clients as they are responsible for communicating with each other. My server is then free to connect up other clients in the same way.
My question is: is this even possible (with TCP and/or UDP)? Neither client necessarily has a public IP address, which is why they have to initiate the connection. Once the connection is established however, my server knows the connection address of both clients. Is there a way to connect them together? A syscall that can do this sort of thing, perhaps?
No, you can not join two existing TCP connections to one.
There are 2 options:
1) Keep your server in the middle as it already is. It should not be problem, if you don't have thousands of clients under one server.
2) Server could send control messages to clients, and order those open new TCP connection between each other. Server should also make decision which of the two clients must be initiator of the new TCP connection.
Option 2 is problematic, because client may have firewall rule that prevents in coming connection to the wanted port. Also NAT would cause problems.

Assigning static/same IP address to the Server everytime it logs in

I am working on udp server/client applicataion. Since for communicating with the server, all the clients must know the ip address and port number of the server. For this purpose, I have hard coded the ip and port number of my server to the clients so that everytime, the client connects to same ip and port number. (found the ip address of the server machine using ipconfig command.)
But now, the problem is that I am working on DHCP network, and there is a chance that everytime sever machine is restarted, a new ip address may be assigned to it (different from the ip address known by the clients at which they will connect.)
So, I always want the ip address hard coded at client side to be assigned to the server machine, everytime it logs in.
Is there any way to do it? I have no idea about it. Searched internet but couldn't find anything relevant.
Looking forward to help :(
Assuming that your clients are local to the server, why not abandon the hard-coded server IP address, and borrow a page from DHCP and use some kind of service discovery method:
Your clients broadcast "where is the server" message when they first come online. The server responds with "I am at IP address X.X.X.X"
When the server comes up, it broadcasts "Server is now at IP address Y.Y.Y.Y" so that if the server crashed, the clients start using the new server.
Presuming you are working on a LAN, that's how I'd do it.
Presuming your DHCP server is configurable enough:
Assign a static map MAC address/IP address in the dhcp server, so
that the same machine always get the same IP (just for the server,
not for every client).
Most entry level all in one devices with DHCP have this functionality, if not it should be quite cheap to buy a new one that has it.
If your DHCP server is a real computer, you can surely configure it to do so.
Additionally you might want to tell your clients to use a local DNS and in this local DNS server define a name for your server, so you won't have to hardcode an IP address in your clients. But the address should be located in some configuration file rather than hardcoded in any case.
I have used dnsmasq to serve as both DNS server with local names, and as DHCP server, giving the servers always the same address and pointing all the DNS requests towards itself.
This questions could be useful to find a windows alternative for dnsmasq: https://stackoverflow.com/questions/7709744/is-there-something-like-dnsmasq-for-windows
By adding a reservation field in the DHCP server we can attain this. If you are using Windows DHCP server, there is a section named 'Reservations', there we can give the MAC address of your pc and the desired IP address. Then the server will provide the mentioned IP for you.
With the narrow focus of a developer a DHCP reservation might be the logical step. But using a nameserver is far better. If the network itself changes or maybe the server is moved to another subnet or maybe even into anoher zone, using an IP address from a DHCP reservation fails, because the server's address changes.
You don't have any of these problems if you use a nameserver. That is what DNS is meant to be doing. Think of it as a "serviceprovider finding service" that detaches your service from the host it is running on.
And, like already suggested, you should never hardcode an IP address or DNS name or anything else that might change (even if you think it will not change) unless it is a design goal that things aren't working anymore if something changes (=not configurable).

Access front end SQL back end secure connection

I need to have an Access front end and SQL 2005 back end for a client. I would like to require them to have a static IP and I'll open a port for that IP. But they also want to be mobile, so their IP will change when they aren't in their office.
Is there another way to secure the connection?
Thanks.
I much don't see any problem or issue with this question. As a note, one could keep in mind with your requested setup the IP address of the mobile devices does not and should not matter.
What matters for the most part is that device can connect to the IP address of the SQL server in question (and that address is not likely to change – or at least not change often).
so their IP will change when they aren't in their office.
This question makes more sense. The internal IP address you use to connect to SQL server can be an unc path name to the server (with SQL on it). However, often just using the IP address of the server on your local network also works. So a typical address would be 192.168.1.100.
So, your Access front end can thus link to the above IP address (I assume you have some SQL re-link code on startup).
When you step outside of the office and that local network, then of course you have several choices.
You could as you suggest open up a port on the SQL serving to allow incoming connections. This is likely not such a great idea. It likely better to setup some type of VPN from that device back to your office network. Once you have that VPN connection setup, then the front end that linked to the fixed internal ip address should continue to work (and you can use the SAME internal address).
However, without a VPN, then you can certainly have the Access front end "re-link" to the external exposed IP address of your office network. This would require that you open a port and forward a port on your router to the machine running SQL server. And you would have to open the default port for sql server. The default is 1433 – but as noted, it is NOT such a great idea to open up your SQL server to the wild internet. Last time I did this, within a FEW minutes there was automated "bots" on the internet already attempting to logon (and the robot was testing all of the most common sql logons + most common passwords). So, this is high risk adventure to open up sql server ports this way.
So, the best approach here is some type of SSL tunnel, or a VPN to your office network, and the bonus part is you likely NOT have to re-link the front end to a different IP address since once that "tunnel" is setup to your office, then the result of the network setup, including the use of printers etc. from your laptop should thus work with any place you connect to the internet.

Search for and connect to a local server (C-programming)

I'm doing some socket programming in C where I have one server and many clients on the local network. I want the client to find (search for) the server on the network by itself (i.e.not having to specify the server's address) and connect to it. The protocol between the clients and server should be TCP.
I've been searching the web for some time to find a good solution to this, but haven't found one yet.
Any suggestion to how I would tackle this problem would be greatly appreciated.
An IP and port number is necessary for a client to connect to the game server. However, if the IP address of the server is not available, a 'compromise' could be reached by:
Using a hostname to connect to the server, you can use gethostbyname() to resolve a hostname and obtain the IP address. This should solve the issue if the server's IP is not known or is not static.
Having a process running on a known IP which can give you a list of active servers.
Having the clients scan a particular range of IP addresses instead of trying to connect to a single IP (not a very good idea, but should be doable on a LAN).
Havinh your server broadcast packets (say UDP datagrams) at fixed intervals to all hosts on the current subnet (again not really a good idea, will lead to unnecessary network traffic).
A hybrid approach with more than one of the above could also employed, for example, try connecting via a hostname and if that fails, fallback to connecting to a known IP with list of active servers etc.
If you have control over the network layout and such, I would use a solution involving DHCP and DNS.
Basically, you want to connect your DHCP server to your DNS server so that it would automatically create entries for new computers on the network. This is a feature that most DHCP servers and DNS servers support, including BIND and named and Microsoft's solution.
Then you'll set the server hostname to some known value, and have the clients find the server using DNS. That is, gethostbyname() would work properly so you could use the first bullet point offered in Bhargav's answer.

Resources