setting IP address from C

bridged with qdn.public.qnxrtp.os
Post Reply
Alain Bonnefoy

setting IP address from C

Post by Alain Bonnefoy » Thu Jan 15, 2004 10:17 am

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.

Sean Boudreau

Re: setting IP address from C

Post by Sean Boudreau » Thu Jan 15, 2004 2:36 pm

Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:
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.

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


int
main(void)
{
int s;
struct ifreq ifreq;
struct sockaddr_in *sa_in;

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return EXIT_FAILURE;
}

memset(&ifreq, 0x00, sizeof(ifreq));

strcpy(ifreq.ifr_name, "en0");

sa_in = (struct sockaddr_in *)&ifreq.ifr_addr;

sa_in->sin_family = AF_INET;
sa_in->sin_len = sizeof(*sa_in);
sa_in->sin_addr.s_addr = htonl(0x0a000001);

if (ioctl(s, SIOCSIFADDR, &ifreq) == -1) {
perror("ioctl");
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

Post Reply

Return to “qdn.public.qnxrtp.os”