This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
Re: Change IP during run-time (netmask change ...)
- From: sebastien Couret <sebastien dot couret at elios-informatique dot fr>
- To: "Richard Rauch" <Richard dot Rauch at vipa dot de>
- Cc: <ecos-discuss at sources dot redhat dot com>
- Date: Wed, 14 Apr 2004 17:55:40 +0200
- Subject: Re: [ECOS] Change IP during run-time (netmask change ...)
- Organization: Elios Informatique
- References: <001601c4222c$8aa1c720$408010ac@leo>
- Reply-to: sebastien dot couret at elios-informatique dot fr
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