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.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page How to detect the IP change of a interface address in the socket programming?
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-11-2008   #1 (permalink)
rike_lin
Just Joined!
 
Join Date: May 2008
Posts: 11
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);
}
rike_lin is offline   Reply With Quote
Old 05-11-2008   #2 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 874
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.
wje_lf is offline   Reply With Quote
Old 05-12-2008   #3 (permalink)
rike_lin
Just Joined!
 
Join Date: May 2008
Posts: 11
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.
rike_lin is offline   Reply With Quote
Old 05-12-2008   #4 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 874
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.
wje_lf is offline   Reply With Quote
Old 05-12-2008   #5 (permalink)
rike_lin
Just Joined!
 
Join Date: May 2008
Posts: 11
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.
rike_lin is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 06:15 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0