Ramesh Guest
|
Posted: Fri Oct 17, 2008 6:21 am Post subject: ioctl error while trying to set subnet mask on an interface |
|
|
Hello,
I am trying to apply a subnet mask of 255.255.255.0 on an interface
"eth1" - I am using 2.6.25.4-10.fc8 with gcc gcc version 4.1.2 .
My ioctl to clear the IP on the interface & apply a new IP works fine.
However an attempt to set the subnet mask fails, could someone please
point out whats wrong with my code?
Thanks
Ramesh
--
int SetNewMask(const char *pIface, const char *Mask) {
int sock=0;
struct sockaddr_in* mask=NULL;
struct ifreq ifr;
sock = socket( PF_INET, SOCK_DGRAM, 0 );
if(!sock)
{
fprintf(stderr,"Cannot obtain socket\n");
return -1;
}
memset(&ifr,0,sizeof( struct ifreq ) );
strncpy(ifr.ifr_name,pIface,IFNAMSIZ);
//mask = (struct sockaddr_in *)&(ifr.ifr_netmask);
mask = (struct sockaddr_in *)&(ifr.ifr_addr);
mask->sin_family=AF_INET;
mask->sin_addr.s_addr=inet_addr(Mask);
if( ioctl( sock, SIOCGIFNETMASK, &ifr ) != 0 )
{
printf("%s:%d Cannot set subnet mask of %s to %s
because %s\n",
__FUNCTION__, __LINE__,
pIface,Mask,strerror(errno));
close(sock);
return -1;
}
close(sock);
return(0);
}
main() {
char IfName[IFNAMSIZ] = { "eth1" };;
char IpAddr[16] = { "172.16.2.48" };
char ZeroIP[16] = { "0.0.0.0" };
char SnMask[16] = { "255.255.255.0"};
long nIpAddr = 0;
in_addr_t inAddr = inet_addr((const char *)IpAddr);
in_addr_t inSmask = inet_addr((const char *)SnMask);
printf("Eth Interface : %s\n", IfName);
SetNewIp((const char *)IfName, (const char *)ZeroIP);
SetNewMask((const char *)IfName, (const char*)ZeroIP);
SetNewMask((const char *)IfName, (const char *)SnMask);
SetNewIp((const char *)IfName, (const char *)IpAddr);
nIpAddr = GetIpAddress(IfName);
printf("Ip Address of %s : %8X", IfName, ntohl(nIpAddr));
} |
|
Gil Hamilton Guest
|
Posted: Fri Oct 17, 2008 6:14 pm Post subject: Re: ioctl error while trying to set subnet mask on an interf |
|
|
Ramesh <rramesh1@gmail.com> wrote in news:c798c357-55e5-4d0a-894c-
78fcf58517f0@v15g2000hsa.googlegroups.com:
| Quote: | My ioctl to clear the IP on the interface & apply a new IP works fine.
However an attempt to set the subnet mask fails, could someone please
point out whats wrong with my code?
int SetNewMask(const char *pIface, const char *Mask) {
if( ioctl( sock, SIOCGIFNETMASK, &ifr ) != 0 )
|
Maybe that should be 'SIOCSIFNETMASK' instead?
GH |
|