public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Change IP during run-time
@ 2004-03-11 15:20 Daniel Lidsten
  2004-03-11 15:38 ` sebastien Couret
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Lidsten @ 2004-03-11 15:20 UTC (permalink / raw)
  To: ecos-discuss

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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ECOS] Change IP during run-time
  2004-03-11 15:20 [ECOS] Change IP during run-time Daniel Lidsten
@ 2004-03-11 15:38 ` sebastien Couret
  2004-04-14 15:14   ` Richard Rauch
  0 siblings, 1 reply; 5+ messages in thread
From: sebastien Couret @ 2004-03-11 15:38 UTC (permalink / raw)
  To: Daniel Lidsten, ecos-discuss

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [ECOS] Change IP during run-time
  2004-03-11 15:38 ` sebastien Couret
@ 2004-04-14 15:14   ` Richard Rauch
  2004-04-14 16:18     ` [ECOS] Change IP during run-time (netmask change ...) sebastien Couret
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Rauch @ 2004-04-14 15:14 UTC (permalink / raw)
  To: ecos-discuss

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ECOS] Change IP during run-time (netmask change ...)
  2004-04-14 15:14   ` Richard Rauch
@ 2004-04-14 16:18     ` sebastien Couret
  2004-04-15  9:35       ` Richard Rauch
  0 siblings, 1 reply; 5+ messages in thread
From: sebastien Couret @ 2004-04-14 16:18 UTC (permalink / raw)
  To: Richard Rauch; +Cc: ecos-discuss

On Wednesday 14 April 2004 16:26, Richard Rauch wrote:
> 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
>

It's almost the same for changing netmask, but you got to use a 
SIOCSIFNETMASK call to ioctl instead of SIOCSIFADDR

Here is an example procedure I wrote for doing such thing
Parameter interface is a string like "eth0" and and adresse is a sub-netmask 
such "255.255.255.0".
It returns 0 if all is right.

Sorry, comments are in French but I hope it helps.
 

int IP_SetMask(const char* interface,const char * adresse)
{
 int test_sock=0;
 struct sockaddr_in* addr=NULL;
 struct sockaddr * addrp=NULL;
 struct ifreq ifr;

 memset( &ifr, 0, sizeof( struct ifreq ) );
 addrp = (struct sockaddr *)&(ifr.ifr_addr);
 addr= (struct sockaddr_in *)addrp;
 memset(addr, 0, sizeof( struct sockaddr) );
 addr->sin_len=sizeof(struct sockaddr_in);
 addr->sin_family=AF_INET;
 addr->sin_addr.s_addr=inet_addr(adresse);

 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  diag_printf("Impossible d'obtenir la socket :%s",strerror(errno));
  return (-1);
 }

 strncpy( ifr.ifr_name,interface,IFNAMSIZ);
 if( ioctl( test_sock, SIOCSIFNETMASK, &ifr ) == -1 )
 {
  diag_printf("Impossible de fixer le masque de sous-réseau de '%s' à '%s' 
car '%s'",interface,adresse,strerror(errno));
  close(test_sock);
  return (-1);
 }
 diag_printf("Masque de sous-réseau de '%s' fixé à '%s'",interface,adresse);
 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [ECOS] Change IP during run-time (netmask change ...)
  2004-04-14 16:18     ` [ECOS] Change IP during run-time (netmask change ...) sebastien Couret
@ 2004-04-15  9:35       ` Richard Rauch
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Rauch @ 2004-04-15  9:35 UTC (permalink / raw)
  To: ecos-discuss

thanks for your reply.
When debugging the IP_SetMask(), I can see that the   s u b n e t m a s k
is changed but not the   n e t m a s k  . Why?
This behaviour can be seen as well with the printouts:

Network stack using 65536 bytes for misc space
                    65536 bytes for mbufs
                    131072 bytes for mbuf clusters
[cyg_net_init] Init: mbinit(0x00000000)
[cyg_net_init] Init: cyg_net_init_devs(0x00000000)
Init device 'emac_eth0'
got MAC from EEPROM: 00:20:d5:77:00:15
[cyg_net_init] Init: loopattach(0x00000000)
[cyg_net_init] Init: ifinit(0x00000000)
IFP: 0x20041530, next: 0x201900a0
IFP: 0x201900a0, next: 0x00000000
[cyg_net_init] Init: domaininit(0x00000000)
[cyg_net_init] Init: cyg_net_add_domain(0x20041f8c)
New domain internet at 0x00000000
[cyg_net_init] Init: cyg_net_add_domain(0x200419b4)
New domain route at 0x00000000
[cyg_net_init] Init: cyg_route_init(0x00000000)
[cyg_net_init] Done
BOOTP[eth0] op: REPLY
       htype: Ethernet
        hlen: 6
        hops: 0
         xid: 0x0
        secs: 0
       flags: 0x0
       hw_addr: 00:20:d5:77:00:15
     client IP: 172.16.129.217
         my IP: 172.16.129.217
     server IP: 0.0.0.0
    gateway IP: 0.0.0.0
  options:
        subnet mask: 255.255.224.0
       IP broadcast: 0.0.0.0
            gateway: 0.0.0.0

----------- 1 ------------
Routing tables
Destination     Gateway         Mask            Flags    Interface
127.0.0.0       127.0.0.1       255.0.0.0       UG       lo0
172.16.128.0    172.16.128.0    255.255.224.0   U        eth0
Interface statistics
eth0    IP: 172.16.129.217, Broadcast: 0.0.0.0, Netmask: 255.255.224.0
        UP BROADCAST RUNNING MULTICAST MTU: 1500, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0
lo0     IP: 127.0.0.1, Broadcast: 127.0.0.1, Netmask: 255.0.0.0
        UP LOOPBACK RUNNING MULTICAST MTU: 16384, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0
IP address of 'eth0' changed to '172.16.129.222'             <----- so with
IP_SetIP("eth0","172.16.129.222") we were successfull

----------- 2 ------------
Routing tables
Destination     Gateway         Mask            Flags    Interface
127.0.0.0       127.0.0.1       255.0.0.0       UG       lo0
172.16.0.0      172.16.0.0      255.255.0.0     U        eth0
Interface statistics
eth0    IP: 172.16.129.222, Broadcast: 172.16.255.255, Netmask: 255.255.0.0
        UP BROADCAST RUNNING MULTICAST MTU: 1500, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0
lo0     IP: 127.0.0.1, Broadcast: 127.0.0.1, Netmask: 255.0.0.0
        UP LOOPBACK RUNNING MULTICAST MTU: 16384, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0
netmask of 'eth0' changed to '255.255.224.0'          <------ so with
IP_SetMask("eth0","255.255.224.0") we were successfull

----------- 3 ------------
Routing tables
Destination     Gateway         Mask            Flags    Interface
127.0.0.0       127.0.0.1       255.0.0.0       UG       lo0
172.16.0.0      172.16.0.0      255.255.0.0     U        eth0
Interface statistics
eth0    IP: 172.16.129.222, Broadcast: 172.16.255.255, Netmask: 255.255.0.0
<------ why isn't there the new netmask?
        UP BROADCAST RUNNING MULTICAST MTU: 1500, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0
lo0     IP: 127.0.0.1, Broadcast: 127.0.0.1, Netmask: 255.0.0.0
        UP LOOPBACK RUNNING MULTICAST MTU: 16384, Metric: 0
        Rx - Packets: 0, Bytes: 0, Tx - Packets: 0, Bytes: 0

Best regards

Richard

-----Original Message-----
From: sebastien Couret [mailto:sebastien.couret@elios-informatique.fr]
Sent: Mittwoch, 14. April 2004 17:56
To: Richard Rauch
Cc: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] Change IP during run-time (netmask change ...)


On Wednesday 14 April 2004 16:26, Richard Rauch wrote:
> 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
>

It's almost the same for changing netmask, but you got to use a
SIOCSIFNETMASK call to ioctl instead of SIOCSIFADDR

Here is an example procedure I wrote for doing such thing
Parameter interface is a string like "eth0" and and adresse is a sub-netmask
such "255.255.255.0".
It returns 0 if all is right.

Sorry, comments are in French but I hope it helps.


int IP_SetMask(const char* interface,const char * adresse)
{
 int test_sock=0;
 struct sockaddr_in* addr=NULL;
 struct sockaddr * addrp=NULL;
 struct ifreq ifr;

 memset( &ifr, 0, sizeof( struct ifreq ) );
 addrp = (struct sockaddr *)&(ifr.ifr_addr);
 addr= (struct sockaddr_in *)addrp;
 memset(addr, 0, sizeof( struct sockaddr) );
 addr->sin_len=sizeof(struct sockaddr_in);
 addr->sin_family=AF_INET;
 addr->sin_addr.s_addr=inet_addr(adresse);

 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  diag_printf("Impossible d'obtenir la socket :%s",strerror(errno));
  return (-1);
 }

 strncpy( ifr.ifr_name,interface,IFNAMSIZ);
 if( ioctl( test_sock, SIOCSIFNETMASK, &ifr ) == -1 )
 {
  diag_printf("Impossible de fixer le masque de sous-réseau de '%s' à '%s'
car '%s'",interface,adresse,strerror(errno));
  close(test_sock);
  return (-1);
 }
 diag_printf("Masque de sous-réseau de '%s' fixé à '%s'",interface,adresse);
 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-04-15  8:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-11 15:20 [ECOS] Change IP during run-time Daniel Lidsten
2004-03-11 15:38 ` sebastien Couret
2004-04-14 15:14   ` Richard Rauch
2004-04-14 16:18     ` [ECOS] Change IP during run-time (netmask change ...) sebastien Couret
2004-04-15  9:35       ` Richard Rauch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).