I have put together a small program to try and set the IP address of the QNX
box from the command line. When I run the code for the first time, I get the
following message:
rtinit: wrong ifa (0x080d869c) was (0x080845c4)
before it tells me that the ioctl failed. When I use Photon’s network config
tool, it shows the address that I tried to set the interface to but the
other two interfaces in my system are missing and you can’t ping out from
the QNX box successfully. The QNX box only starts to work properly when you
apply a change to the IP address from within the photon config tool.
My code is below, hopefully someone will be able to spot the glaring
error:
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct sockaddr_in sin={0};
int sk;
unsigned char *ptr;
struct ifreq ifr={0};
sin.sin_family = AF_INET;
if (inet_aton(argv[1],&sin.sin_addr)==0) { // 0 if error occurs
printf(“failed conversion\n”);
return -1;
}
strcpy(ifr.ifr_name,“en0”);
memcpy(&ifr.ifr_addr, &sin, sizeof(ifr.ifr_addr));
sin1.sin_family = AF_INET;
if ((sk = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf(“no socket\n”);
return -1;
}
if (ioctl(sk,SIOCSIFADDR,&ifr)==-1) {
printf(“didn’t set IP\n”);
return -1;
}
memcpy(&sin,&ifr.ifr_addr,sizeof(sin));
ptr = inet_ntoa(sin.sin_addr);
printf(“IP Address is :%s\n”,ptr);
close (sk);
return 1;
}
Many Thanks