Results 1 to 5 of 5
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 ...
- 05-11-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 17
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!
=== TEST CODE === ======================================
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#define BUFLEN 512
#define PORT 9930
#define NPACK 10
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, r, slen=sizeof(si_other);
int nfds;
fd_set rfds;
char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 )
return(-1);
memset((void*)&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(0x0A00020F);
if ( bind(s, (const struct sockaddr*)&si_me, sizeof(si_me)) == -1 )
{
printf("* fail to bind()\n");
return(-1);
}
FD_ZERO(&rfds);
FD_SET(s, &rfds);
nfds = s + 1;
r = select(nfds, &rfds, (fd_set*)0, &rfds, (struct timeval*)0);
if ( r < 0 )
{
printf("* select() error: %d, (errno=%d.%s)\n", r, errno, strerror(errno));
return (-2);
}
if (recvfrom(s, (void*)buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen)==-1)
{
printf("- Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
}
close(s);
return (0);
}
- 05-11-2008 #2
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.
- 05-12-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 17
Dear wje_lf,
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?
Best Regards,
Rike_Lin.
- 05-12-2008 #4
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.
- 05-12-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 17
Dear Wje_lf,
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.
Best Regards,
Rike_Lin.


Reply With Quote
