View topic - FILE EXISTS error on setting IPV4 address using ioctl

Page 1 of 1

FILE EXISTS error on setting IPV4 address using ioctl

PostPosted: Thu Dec 06, 2018 1:40 pm
by parthiban56
I am getting a File exists error on using the SIOCSIFADDR ioctl command to set IP address ,but the IP address is changed
to the requested value. Why the error occurs?

Code snippet:

bool setIpv4Address(std::string interfaceName,std::string ipAddr)
{
struct sockaddr_in sin;
int sockFd;
std::string Ip;
struct ifreq ifr;
bool retStatus;


memset(&ifr, 0x00, sizeof(ifr));
memset(&sin, 0x00, sizeof(sin));
retStatus = true;

sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
/// check valid ip
if (inet_aton(&ipAddr[0], &sin.sin_addr) == 0)
{
retStatus = false;
}
else
{
/// Create socket for INET and update the address information.
memset(&ifr, 0x00, sizeof(ifr));
strcpy(ifr.ifr_name, &interfaceName[0]);
memcpy(&ifr.ifr_addr, &sin, sizeof(ifr.ifr_addr));
if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
{

/// Set IP address using SIOCSIFADDR ioctl flag.
if (ioctl(sockFd, SIOCSIFADDR, &ifr) != -1)
{
/// Set Interface Up
/// Get ifreq.flags
ioctl(sockFd, SIOCGIFFLAGS, &ifr);

ifr.ifr_flags |= (IFF_UP| IFF_RUNNING);

if(ioctl(sockFd, SIOCSIFFLAGS, &ifr) == -1)
{
perror("Interface link status change failed");
retStatus = false;
}
}
else
{
perror("setIpv4Address Setting IPV4 address Error :");
retStatus = false;
}
}
else
{
perror("Socket creation failed ");
retStatus = false;
}
close (sockFd);
}

return retStatus;
}