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

FILE EXISTS error on setting IPV4 address using ioctl

anything that doesn't fit to other groups.

FILE EXISTS error on setting IPV4 address using ioctl

Postby parthiban56 » Thu Dec 06, 2018 1:41 pm

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;
}
parthiban56
New Member
 
Posts: 2
Joined: Tue Dec 04, 2018 6:48 am

Re: FILE EXISTS error on setting IPV4 address using ioctl

Postby nico04 » Thu Dec 06, 2018 2:40 pm

1) Use code tags to make your code readable.

2) Never use this kind of writing
Code: Select all
if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) != -1)

Instead, use
Code: Select all
sockFd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockFd != -1)


3) Add printf() to precisely trace the execution of you program.

4) Report the trace output here.
nico04
Senior Member
 
Posts: 115
Joined: Wed Sep 29, 2010 9:59 am
Location: France


Return to General Programming

Who is online

Users browsing this forum: No registered users and 4 guests