[ECOS] Change IP during run-time (netmask change ...)
sebastien Couret
sebastien.couret@elios-informatique.fr
Wed Apr 14 16:18:00 GMT 2004
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
More information about the Ecos-discuss
mailing list