0

I am trying to find a way that I can effectively change the IP and netmask of the computer that my programs are running on. I have to be able to maintain communications with a "box" that the user has access to and can change the IP of that box (but not that of the Linux host). Luckily the box broadcasts its IP over a multicast address. I am able to read that address and communicate to whatever IP the box is broadcasting. However, my problem is that when the box changes IPs outside of the subnet of the Linux host, I lose connectivity (as I would expect to happen).

As a result, I need a quick and dirty way to to change the IP and netmask of the Linux host when it detects that the IP being broadcast is outside it's subnet. I am aware that I can modify/edit the /etc/networking/interfaces file, but I was hoping to have something "live" where I wouldn't need to restart the networking interface or Linux host. Additionally, I would like to maintain all sockets that I have currently open during the change. My main issue is that I have a slew of connections run by other applications that cannot be corrupted as a result of the IP changeover. Also, if possible, I'd like to avoid having to redo having to run the multicast route add command for Linux after the host IP changeover.

Is there anything that I can do to satisfy these requirements? Thanks.

9
  • If you want to maintain current connections, it may be best to ensure you have two NICs in the Linux box, one unchanging one for regular traffic and one you can continuously morph to communicate with the elusive box. Commented Jun 12, 2013 at 2:16
  • Unfortunately, the Linux host system in question is an embedded system with only the capacity for one NIC. I don't necessarily want to maintain the connection DURING the changeover, but I would need all sockets to retain connectivity afterwards. I want to avoid having to find a way to send a falg to all my open sockets that they need to reinitialize. Commented Jun 12, 2013 at 2:23
  • 1
    Not sure how much luck you're going to have with that. If your IP address changes, packets destined for your original IP will probably end up in the bitbucket. You may want to consider telling customer you don't support changing of the elusive device IP address - sometimes, non-tech solution are the best, they're certainly easy to implement :-) Commented Jun 12, 2013 at 2:36
  • I've tried to reason with my project lead to no avail. The SOP for the box is to always change the IP, even though I cannot think of a single use-case where this would provide any benefit. I forgot to state that each one of my sockets are transmitting UDP. The only receiving being done is via Multicast, or there are additional handshaking routines that occur. Commented Jun 12, 2013 at 2:42
  • Okay then, how about a proxy box with two NICs. One talking to your embedded box with an unchanging NIC, the other adapting to the elusive box. It just runs forwarding software for the link. I know that's getting complicated but, if they're going to impose these limitations, you may have to think "outside the boxes". Bottom line here, without two NICs, I don't think you can both adapt and maintain current connections. Commented Jun 12, 2013 at 2:45

1 Answer 1

1

You don't want to change the IP address of the interface, you want to add an additional IP address and subnet to the interface. Search your Linux system documentation for IP Alias functionality.

Here's the HOWTO.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, it looks promising. Is there an cleaner implementation in C/C++ than just calling system() on the command line commands?
You can look at iproute2 to figure out how it interfaces to the kernel, but I'm going to guess that system("ip addr add 192.168.1.2/24 brd + dev eth0") will be a lot easier - and more maintainable - in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.