[ECOS] IP adress Get and Set (SIOCSIFADDR problem ? )

sebastien Couret sebastien.couret@elios-informatique.fr
Fri Jan 16 17:05:00 GMT 2004


Hello,

I'm using FreeBSd stack over eCOS , last CVS revision.
It seems that IP adress cannot be changed while the application is running 
... 

I'm using ioctl to get and set IP adress of network interfaces as shown in 
the following code :

<
#include <network.h>
#include <arpa/inet.h> // for using inet_addr

//////////////////////////////////////////////
// Set IP adress of interface to value adress
//////////////////////////////////////////////
int SetIP(const char* interface,const char * adress)
{
 int test_sock=0;
 struct sockaddr_in* addr=NULL;
 struct ifreq ifr;

 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(adress);

 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  diag_printf("Cannot obtain socket :%s\n",strerror(errno));
  return (-1);
 }

 strncpy( ifr.ifr_name,interface,IFNAMSIZ);
 if( ioctl( test_sock, SIOCSIFADDR, &ifr ) != 0 )
 {
  diag_printf("Cannot set IP address of interface '%s' to 
'%s':%s\n",interface,adress,strerror(errno));
  close(test_sock);
  return (-1);
 }
 else diag_printf("IP address of '%s' set to '%s'\n",interface,adress);
 close(test_sock);
 return(0);
}

//////////////////////////////////////////////
// Display IP address of interface
//////////////////////////////////////////////
int GetIP(const char* interface)
{
 int test_sock=0;
 struct sockaddr_in *addrp=NULL;
 struct ifreq ifr;

 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  diag_printf("Cannot obtain socket :%s\n",strerror(errno));
  return (-1);
 }

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

 if( ioctl( test_sock, SIOCGIFADDR, &ifr ) == -1 )
 {
  diag_printf("Cannot obtain IP address of '%s' 
:%s\n",interface,strerror(errno));
  close(test_sock);
  return (-1);
 }
 close(test_sock);

 addrp = (struct sockaddr_in *)&(ifr.ifr_addr);
 diag_printf("IP Adress of '%s' is 
'%s'\n",interface,inet_ntoa(addrp->sin_addr));
 return(0);
}

//////////////////////////////////////////////
//////////////////////////////////////////////
int main(int argc,char** argv)
{
 init_all_network_interfaces();
 GetIP("eth0");
 SetIP("eth0","1.2.3.4");
 GetIP("eth0");
 return(0);
}
>

So ioctl calls always return a no error value, but IP adress stays the same 
as shown by the result gaved by the Linux Synthetic Target :

<
IP Adress of 'eth0' is '10.0.0.254' 
IP address of 'eth0' set to '1.2.3.4' 
IP Adress of 'eth0' is '10.0.0.254'
>
 
I have proposed a change in the in.c code. (commenting out lines 263-268 do 
the trick ...)
But I agree, that it's not fair to change FreeBSD code, may be i'm forgetting 
something while calling SetIP but i dunno what ... 

Someone got a clue ?
Thanks for your support.

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



More information about the Ecos-discuss mailing list