Changing IP address at runtime - linux

I am creating a tcp connection using the function socket(), bind(), and then listen().
Our customers would like to be able to define an IP address of the server at runtime. Is there a way of changing the IP at runtime or must it be done in the BIOS?
Thanks for any tips

I've changed the IP address using ifAddrSet(..) many times. Usually I call this function from within the startup script before my application is running so I have no idea how calling this function affects already connected sockets.
But have a look at the functions provided by ifLib.h. I'm sure you'll find something that suits your needs (ifAddrAdd(..) looks promising).

I am not sure what you mean by defining 'IP address of the server at runtime?'. Obviously for a given socket it's IP address cannot be changed. It's an endpoint of a connection, it cannot be changed run time. If you just want to assign multiple IP addresses to a host that's possible.
In general - you can add as many IP addresses as you want to your machine (ok not exactly) but certainly a hundred or so (ie. statically allocated). That's not the problem (management of that is a nightmare, but sure not impossible). The problem is how those IP addresses are reached, that is not in your control, that depends upon the settings on client especially the routing entries. eg. you could use all of the IP addresses in a Subnet (say 10.1.2/24).
Not recommended - but possible.
Once you have those IP addresses - you bind on the port and address as INADDR_ANY, which says accept connection on 'any ' of the local addresses. On which address the connection was made to can be determined on server using getsockname.

Related

Two hostnames sharing the same IP

I noticed that one of Google's mail servers (alt4.aspmx.l.google.com) points to 74.125.200.26, but when I do a reverse DNS lookup on that IP I see that the hostname associated with it is sa-in-f26.1e100.net. My limited understanding of DNS is that when you have a situation like that, one hostname is an alias of the other, but that's not the case here.
My initial goal was making a Python program that given an IP address and a hostname, returns a boolean answer indicating whether the IP belongs to a mail server of that domain. The algorithm I implemented used dig to search all mail servers of a domain and then tried to match any of them to the hostname associated with the given IP (which I found using dig -x). My program fails with the case I mentioned before. What am I missing?
Sorry for my bad english. Thanks!
Many services can run on one server/ipaddress, and many hostnames can resolve to one IP address. In the other direction, one ip address will most often resolve to only one hostname (if it has PTR record at all), and the name will very often be something generic like ip-xx-yy-zz-qq.networkcarrier.net (so unrelated to any of the services that are legitimately running on that server).
Depending on the purpose of your check, perhaps you can just test if the hostname A record points to the required IP address (because your initial requirement is flawed: ip addresses do not belong to domains, they belong to network providers).
(Still, for some purposes, most notably as anti spam measure, there is a use case for checking if ip address resolves to some particular hostname.)

How to write a Node.js server if my pc's ip address changes daily

I am owning a Windows PC. I have written a Web application that runs fine. But the problem is my PC's IP changes periodically. I want that application to be accessible in the network (from other pc's) without changing client side code . My client side code is in angular js.
The web server of your web application cannot bind to the correct IP if it doesn't know it in advance.
You can make your IP address static by following these steps:
In Windows, go to the Network and Sharing center
In the left pane, choose "Change adapter settings"
Right click your ethernet or wifi connection (the one connected to your router) and choose Properties
Double click Internet Protocol Version 4 (TCP / IPv4)
As an IP address, choose the one your computer already has (or try another IP address starting with the first same 3 numbers, i.e. xxx.xxx.xxx.yyy)
The default gateway is the IP address of your router
The subnet mask is typically 255.255.255.0
For the DNS server choose Google's 8.8.8.8 and/or your router's IP address
You have several choices depending upon your configuration.
If this is the public dynamic IP you get from your ISP, you can go to your ISP (whoever you get internet service from) and upgrade your service to a static IP address so it won't change.
If this is the public dynamic IP you get from your ISP, you can use a dynamic DNS service to attach a hostname to your IP address and then use that hostname to access the server. The dynamic DDNS app you put on your server will keep the DNS updated whenever your dynamic IP address changes.
If this is just a local IP address on your LAN (a 192.x.x.x or 10.x.x.x address), then you can pick an IP address that your router supports, but is above the range being used for DHCP and set that computer to use that IP address and not DHCP. In Windows, you can go to the networking configuration and choose the IP address instead of using DHCP. Then, the IP address won't ever change. It is important that you pick an address outside the range used for DHCP to avoid any conflicts. On my own LAN where the router is allocating addresses like 192.168.0.x, I manually assign addresses like 192.168.1.250.
This is something that your router handles through a protocol known as DHCP. Basically, the answer is to keep your IP from changing.
Many routers allow you to reserve an IP for certain MAC addresses. I would recommend that you access your router over your local network and work with the GUI it provides to try to configure this... if you're successful, then your IP will no longer change, and problem solved :-) If it's not intuitive, then of course refer to the documentation for your router.

give local node server a name rather then ip address

This might be a stupid question, but i run a business in a strip mall and internet is included (because of this I have no access to the router). the router assigns dynamic ip address and when I set a static ip I lose connection.
is there a way to change the address to my node app from "http:/ /[my-server-ip]:3000" to something like "http:/ /[business-name]:3000". (/ / should be // but stack wont let me post with dead links)
the main reason for this is I have multiple tablets and devices that use this system however recently after they made some changes to the routers setting my computer will be assigned two or three ip addresses a day (I notified the landlord he says its normal). and everytime my ip address changes I lose connection to my app.
is there a possible fix that doesnt require me to host on a web server or enable port forwarding?
perhaps I should reword the question. How do I go about setting up local dns zones from mac? (I know this can be achieved with most linux distros)
No you can't.
Your server IP will always change as the router gives you a new IP address, so change the hosts will not work unless every time the ip on the server changes, you change your ip as well on the devices.
You may try to create an account on dns sites like http://www.noip.com/. They will give you an "external" internet address like mybusiness.noip.com and then you will point your programs on tablets and cell phones to this address.
You will then install a program on your server that will tell noip what the new address is maintaining the same mybusiness.noip.com on them.

Does accept function return error(-1) if TCP server goes out of network

Does the accept function returns error(-1) if the Ethernet interface it is attached goes out of network?if not how does the application(TCP server) will know that its interface is not active any more ??
I am using one thread for accepting the connection and not using any "select" statement for doing so.Directly calling accept() function but somehow it is not retuning error if I remove the IP address from Ethernet interface.
using C and working environment is linux.
Usually you don't bind your server socket to a specific IP address (you use INADDR_ANY). So even if you remove your ethernet IP address, you could still contact your server using the loopback interface. Or some other interface with an IP address.
If you want to make sure that your server is reachable from the net, checking the interface status does not get you much. It's just a single hop on a long path through the network. You'd need a testing client somewhere else to check the reachability of your server.

Same server, same program but started once using one network card and after with another

I have a Linux server with multiple ips (so, multiple eth0, eth0:0, eth0:1 etc).
The script I'm trying to start is a php CLI script which is downloading stuff from an another server API, and I would like to change the IP based on different parameters. Once the script is started, I don't need anymore to change the ip OF THAT SPECIFIC script until his end.
Do you have any clue if it is possible to achieve it?
My other solution was to install Xen or OpenVZ and create N different VPS per each IP, but as you can see is definitely a PITA :-)
You don't specify how you connect to the other server, but with sockets you can try socket_bind.
EDIT:
With curl you can try curl_setopt.
CURLOPT_INTERFACE The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.
I know how to do it in C - you use bind() on your socket before you call connect(), and you bind to the IP address assigned to the desired interface, passing 0 for port. I don't know how to do it in PHP.

Resources