Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
How to detect the IP change of a interface address in the socket programming?
Dear all,
There is a socket programming issue as below. Please help to provide your good idea for our reference. Thanks!
In our linux system, there are two network cards with two different IP addresses (for example: 10.1.1.1 and 20.2.2.2). We write a server program to bind a socket with one of above interfaces (10.1.1.1) for receiving the requests from any client. After a period of operation, the IP address of such interface would be chagned from 10.1.1.1 to 30.3.3.3. The original binding for socket would not identify such change and fail to communicate with client. Is there any good solution to detect the change of interface address and then re-bind the socket to new IP address?
In addition, I use the following code to do the tests. The socket is binded to the IP (10.0.2.15) of only-one interface. And, then it is suspended to wait incoming packets by the select() API.
1). The code would be executed successfully when interface's IP is 10.0.2.15.
2). If I change the IP to 10.0.2.16 and run the program again, the code is failed to execute due the failured of binding.
3). After the code is executed, I change the interface's IP. There's no any response for that program. That program is still suspended in the select(().
4). After the code is executed, I make the interface down. There's no any response for that program. That program is still suspended in the select(().
Do you have any good idea to detect the IP change for specific binding? Thanks!
A program which is waiting on a select() cannot detect a change in an interface's IP address by itself.
But you can run another program which checks the IP address every 10 seconds (or whatever interval you choose) and then send a signal to the original program if the interface is no longer up or the IP address changes.
This second program need not even be a C program. It can be as simple as a shell script. In that script, you can wrap a loop around the running of program ifconfig. Parse the data. If the interface is no longer up, or if the IP address has changed, you can have the script put the relevant details in a file and then send a signal to the original C program.
Hope this helps.
__________________ --
Bill
Old age and treachery will overcome youth and skill.
Thanks very much for your reply. It seems to be a good solution to provide the feature of automatic detection to IP address for a set of running problems.
On the other hand, as you saw in the previous message, I bind the specific IP to the socket. If select() funnction cannot check the change of an interface's IP address, what socket API could provide such detection? Is this the limitation of socket programming, or a bug?
There is no function which allows you to wait for an IP address change, if that's what you want. But if you only want a way in C to find out the IP address of a specific network interface card, and are willing to check that periodically, use ioctl(SIOCGIFCONF). If you're going to do that, then run (don't walk) to your nearest bookseller and obtain a book published by Addison-Wesley: UNIX Network Programming, volume 1, by W. Richard Stevens of happy memory. Using SIOCGIFCONF involves a few necessary tricks, and I don't know that they will be documented anywhere else. But if you're doing serious network programming, this book will be useful in many, many ways.
__________________ --
Bill
Old age and treachery will overcome youth and skill.
I don't want to get a function which allows me to wait for an IP address change. Because I already bind a socket to the specific IP address, I expect to get a error when I use such socket to execute some operations on a non-eixsted IP.
BTW, thanks very much for you to share your knowledge. I will go to the library to search that book.
Open Source Security Myths Dispelled Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization subscribe
InformationWeek InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology. subscribe