[ECOS] Change IP during run-time

Richard Rauch Richard.Rauch@vipa.de
Wed Apr 14 15:14:00 GMT 2004


Hi,

we were also interested on this information. we implemented as described and
it works,
but with this solution it is not possible to change the network mask, too.
How we have to do this?

Best Regards

Richard

-----Original Message-----
From: ecos-discuss-owner@ecos.sourceware.org
[mailto:ecos-discuss-owner@ecos.sourceware.org]On Behalf Of sebastien
Couret
Sent: Donnerstag, 11. März 2004 16:41
To: Daniel Lidsten; ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] Change IP during run-time


On Thursday 11 March 2004 16:20, Daniel Lidsten wrote:
> Hi,
>
> I want to change the IP address of my "eth0" interface during run-time.
> Is that possible? I tried to do it using ioctl but encountered some
> problems:
>
> 1. First i tried to just change the current IP with the below calls but
> the old IP address still answers ping calls toghether with the new IP
> address. I could successfull ping two addresses. That's not goos since i
> want to old address to stop responding.
>
> struct sockaddr_in *inaddr = (struct sockaddr_in *)&ifreq.ifr_addr;
> inet_aton("111.22.33.444", &inaddr->sin_addr  );
> ioctl( sock, SIOCSIFADDR, &ifreq );
>
>
> 2. The next thing to try was to delete the interface and then
> change/create the new one:
>
> if (ioctl(sock, SIOCDIFADDR, &ifreq)) { /* delete IF addr */
> inet_aton("111.22.33.444", &inaddr->sin_addr  );
> ioctl( sock, SIOCSIFADDR, &ifreq );
>
> With this code i only get ping responses from the new IP address
> (good...) but when creating a new socket and sending out data then the
> package source (i logg all ethernet traffic) are set to the OLD IP
> address.
>
> Any ideas how to change IP address of the board during run-time?
>
> Regards, Daniel Lidsten

FreeBSD stack can managing several IP interface on the same physical device.
Using only SIOCSIFADDR is creating a new one.
Have a look at show_network_tables((pr_fun*)diag_printf); (declare it as
extern before using, I haven't found it in an header file ... : extern int
show_network_tables(pr_fun *pr);) to see that ...

For the last point '
>but when creating a new socket and sending out data then the
> package source (i logg all ethernet traffic) are set to the OLD IP
> address.'

I have not check it fully.


Anyway, here is a code I wrote for doing such think (sorry comments are in
French ... ;o) )

interface is the name of the interface a.ka "eth0" for example, adresse is
the IP adress to set "192.168.0.76" for example.

//--------------------------------------------------------------------------
------------------
// Fixe l'adresse IP de l'interface en paramétre
// Remarque : le masque et l'adresse de broadcast sont automatiquement
changés
// Retourne -1 en cas d'erreur, 0 sinon
//--------------------------------------------------------------------------
------------------
int IP_SetIP(const char* interface,const char * adresse)
{
 int test_sock=0;
 struct sockaddr_in* addr=NULL;
 struct ifreq ifr;


 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  printf("Impossible d'obtenir la socket");
  return (-1);
 }

 memset(&ifr,0,sizeof( struct ifreq ) );
 strncpy(ifr.ifr_name,interface,IFNAMSIZ);

 if( ioctl( test_sock, SIOCGIFADDR, &ifr ) == -1 )
 {
  printf("Impossible d'obtenir l'adresse IP de '%s' car
'%s'",interface,strerror(errno));
 }
 else
 {
  if( ioctl( test_sock, SIOCDIFADDR, &ifr ) != 0 )
  {
   printf("Impossible de supprimer l'adresse IP de '%s' car
'%s'",interface,strerror(errno));
  }
 }
 memset( &ifr, 0, sizeof( struct ifreq ) );
 addr= (struct sockaddr_in *)&(ifr.ifr_addr);
 memset(addr, 0, sizeof( struct sockaddr_in) );
 addr->sin_len=sizeof(struct sockaddr_in);
 addr->sin_family=AF_INET;
 addr->sin_addr.s_addr=inet_addr(adresse);
 strncpy(ifr.ifr_name,interface,IFNAMSIZ);

 if( ioctl( test_sock, SIOCSIFADDR, &ifr ) != 0 )
 {
  printf("Impossible de fixer l'adresse IP de '%s' à '%s' car
'%s'",interface,adresse,strerror(errno));
  close(test_sock);
  return (-1);
 }
 else
  printf("Adresse IP de '%s' fixée à
'%s'",interface,inet_ntoa(addr->sin_addr));
 close(test_sock);
 return(0);
}

--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss



More information about the Ecos-discuss mailing list