1833329 Members
3774 Online
110051 Solutions
New Discussion

Re: socket ioctl help

 
Prakash Achuthan
Occasional Advisor

socket ioctl help

Hi,

I am getting a "reinterpret_cast" warning while compiling the following
C++ program. In the program I am making socket ioctl call to get the
network mask.

Looks like there is no way I can get over this warning, the "ifreq"
structure has no member of type "sockaddr_in". Is there anything wrong
that I am doing here which gives out this warning ?

$ aCC -AA main1.C
Warning 749: "main1.C", line 24 # The cast from 'sockaddr *' to
'sockaddr_in
*' is performed as a 'reinterpret_cast'. This operation is
non-portable
and potentially unsafe.
s_addr = (struct sockaddr_in *)(&ifr.ifr_addr);
^^^^^^^^^^^^^^^^^^^^^^


Program:
--------

#include
using namespace std;

#include
#include
#include
#include
#include
#include

int main(){

struct ifreq ifr;
int skfd;
struct sockaddr_in *s_addr;

// open system socket
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
{
strcpy(ifr.ifr_name, "lan0");
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
{
s_addr = (struct sockaddr_in *)(&ifr.ifr_addr);
cout<<"Network mask is "<sin_addr)< }

close(skfd);
}
}

regards,
1 REPLY 1
Jason Deckard
Occasional Advisor

Re: socket ioctl help

Hi,

Your cast is legal; aCC is just concerned your code may be non-portable. I have three suggestions to make the warning go away:

1) See if aCC prefers you use "reinterpret_cast" over the C-style cast "(struct sockaddr_in *)". It may be that aCC thinks you don't realize it is a reinterpret_cast.

2) See if aCC supports a means of disabling certain warnings, such as through #pragma.

3) Finally, there is always the compiler option to disable all warnings (this has obvious disadvantages). It may be different with aCC, but the switch is usually -w.

For what it's worth, your code compiles without error under g++, even with the -Wall option.

Best of luck to you :)
[Insert humorous and/or inspirational quote here]