more ioctl

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

I have had similar problems running under 6.0 Patch C, although I have
managed to set the netmask without any problems. Sean usually answers
queries like this, I wonder if he has seen this message yet. I would be
interested in example code for setting IP Address and 0.0.0.0 route also.
Are you out there today, Sean?

Regards

Poseidon

“A” <A.B@C.D> wrote in message news:9rmodj$7ld$1@inn.qnx.com

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

You need to zero out your structures:



#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
struct sockaddr_in sin;
int sk;
unsigned char *ptr;
struct ifreq ifr;

memset(&ifr, 0x00, sizeof ifr);
memset(&sin, 0x00, sizeof sin);

sin.sin_family = AF_INET;
sin.sin_len = sizeof sin;
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));

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: %s\n”, strerror(errno));
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 0;
}

A <A.B@c.d> wrote:
: 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

Thank you very much, Sean, it is working. I will try and do my SIOCADDRT
ioctl for the default route and then I will be happy. I’ll just remember to
zero everything before filling it up.

The SIOC*RT ioctls are specific to the tiny stack. The full stack
uses routing sockets to manipulate the routing tables:

socket(AF_ROUTE, …);

-seanb

A <A.B@c.d> wrote:
: Thank you very much, Sean, it is working. I will try and do my SIOCADDRT
: ioctl for the default route and then I will be happy. I’ll just remember to
: zero everything before filling it up.

Look’s Like I’ll have to search out how to do that from a routing socket as
I’m using the big stack. Thank you for pointing that out and saving me a few
wasted hours with ioctl’s not working.

“Sean Boudreau” <seanb@qnx.com> wrote in message
news:9rp5st$ls1$1@nntp.qnx.com

The SIOC*RT ioctls are specific to the tiny stack. The full stack
uses routing sockets to manipulate the routing tables:

socket(AF_ROUTE, …);

-seanb

A <> A.B@c.d> > wrote:
: Thank you very much, Sean, it is working. I will try and do my SIOCADDRT
: ioctl for the default route and then I will be happy. I’ll just remember
to
: zero everything before filling it up.