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.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > How to detect the IP change of a interface address in the socket programming?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 05-11-2008   #1 (permalink)
Just 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);
}
rike_lin is offline  

Reply With Quote
Old 05-11-2008   #2 (permalink)
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 1,192
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)
Just 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.
rike_lin is offline   Reply With Quote
Old 05-12-2008   #4 (permalink)
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 1,192
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)
Just 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.
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

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

Free Magazines
Free Network Mapping Tool for Microsoft® Office Visio® Professional 2007 Users
Don't map your network by hand – let LANsurveyor Express for Microsoft Visio Professional 2007 automatically create network diagrams for you.
subscribe
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say.
subscribe

Safe, Secure Backup


All times are GMT. The time now is 01:40 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2