Could you please tell me how to set an IP address with an ioctl() call.
I don’t know how to fill the sockaddr structure.
I tried some UNIX examples but they don’t work, they just clear my
actual IP address.
Thanks,
Alain.
Could you please tell me how to set an IP address with an ioctl() call.
I don’t know how to fill the sockaddr structure.
I tried some UNIX examples but they don’t work, they just clear my
actual IP address.
Thanks,
Alain.
I hope it helps!
struct ifreq ifr;
int local_IP_socket;
// descriptor (an fd) for the socket use to set up IP data
char ip_addr[IP_ADDR_BUFLEN]=“10.2.12.89”; // device’s new IP address
char subnet_mask[IP_ADDR_BUFLEN]=“255.255.255.0”; //
subnet mask
bool iface_enabled;
// ‘true’ if we are to set a new IP address
local_IP_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (local_IP_socket == DIRECTIVE_FAILURE)
{
printf(“Cannot create local address socket”);
exit(0);
}
::memset(&ifr, 0, sizeof(ifr));
::strcpy(ifr.ifr_name, Interface);
/*
| For those of you that don’t speak the gibberish that passes for QNX (or
BSD, probably)
| ioctl() requests, here’s a part of the rosetta stone: SIOCDIFADDR means
“delete the current
| IF (which, for some unknown reason QNX uses to mean “IP” as in Internet
Protocol) address”.
| (I don’t know how you’re supposed to know that it takes a pointer to an
ifreq structure as
| the optional third parameter, or what’s supposed to go in there…)
/
if (ioctl(local_IP_socket, SIOCDIFADDR, &ifr) == DIRECTIVE_FAILURE)
{
/
| Attempt to delete the old IP address failed. It may have failed
because it was previously
| deleted in the session. We would know this because errno would be set
to EADDRNOTAVAIL.
| So if errno is set thus, we continue on knowing that the IP address
has been cleared.
|
| Of course, NONE of this is documented in the QNX “documentation”; this
has been determined
| by observation!
*/
if (errno != EADDRNOTAVAIL)
{
printf(“Cannot delete old IP address”);
exit(0);
}
else
{
printf(“IP addr already deleted” );
}
}
printf(“Old IP address deleted”);
/*
| If the interface is enabled, we (re)define the IP address of this device,
and define the
| subnet mask (in that order).
*/
struct sockaddr_in sa; // used to define the new IP address
::memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_len = sizeof(sa);
sa.sin_port = 0;
inet_aton(ip_addr, &sa.sin_addr);
::memcpy(&(ifr.ifr_addr), &sa, sizeof(sa));
/*
| Here’s a little bit more of the rosetta stone: SIOCSIFADDR means “set the
IF (which, we’ve
| established above, means IP) address”. Again, it magically takes a pointer
to an ifreq
| structure of unknown contents.
*/
if (ioctl(local_IP_socket, SIOCSIFADDR, &ifr) == DIRECTIVE_FAILURE)
{
printf(“Cannot define new IP address for this device”);
}
/*
| Local IP address set, now set the subnet mask. Again, more rosetta stone:
SIOCSIFNETMASK
| means “set the subnet mask for the device”. Note that the socket address
structure is
| mostly set up; all we really have to do is change the address portion of
it.
*/
inet_aton(subnet_mask, &sa.sin_addr);
::memcpy(&(ifr.ifr_addr), &sa, sizeof(sa));
if (ioctl(local_IP_socket, SIOCSIFNETMASK, &ifr) == DIRECTIVE_FAILURE)
{
printf(“Cannot define subnet mask for this device”);
exit(0);
}
/*
| All we needed the socket for is to set the IP (I will NOT call it an IF)
address, so
| we do not need to keep it open anymore.
*/
close(local_IP_socket);
“Alain Bonnefoy” <alain.bonnefoy@icbt.com> wrote in message
news:40066809.8030406@icbt.com…
Could you please tell me how to set an IP address with an ioctl() call.
I don’t know how to fill the sockaddr structure.I tried some UNIX examples but they don’t work, they just clear my
actual IP address.Thanks,
Alain.