LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2011, 01:58 AM   #1
turbosur
LQ Newbie
 
Registered: May 2011
Posts: 7

Rep: Reputation: Disabled
Problem reconfiguring dst address for TUN interface


Hi,

I'm having trouble reconfiguring my TUN interface from userspace running LynxOS 4.0.0. The TUN interface tun0 is configured at startup using:
#ifconfig tun0 192.168.0.24 172.16.0.114 up

After running the code below, the dst address is changed to 172.16.0.124 (as confirmed with ifconfig), which is exactly what I want, however it's unreachable. The host is reachable prior to running the provided code.

So I'm hoping your expertice can help me in figuring out what I'm doing wrong.

Thanks in advance!

Regards,
Dan

----- Start of code -----
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdint.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <bsd/in.h>
#include <arpa/inet.h>
#include <bsd/if.h>
#include <sockio.h>

int main()
{
/// Create the interface request message
struct ifreq my_ifr;
struct sockaddr_in ip_addr, netmask_addr;
char if_ar[15];
char ip_ar[15];
char nm_ar[15];
char* interfaceName = if_ar;
char* ipAddress = ip_ar;
char* netmask = nm_ar;

sprintf(interfaceName, "tun0\0", 5);
sprintf(ipAddress, "172.16.0.124\0", 13);
sprintf(netmask, "255.255.255.0\0", 14);

ip_addr.sin_family = AF_INET;
netmask_addr.sin_family = AF_INET;

inet_pton(AF_INET, ipAddress, &(ip_addr.sin_addr));
inet_pton(AF_INET, netmask, &(netmask_addr.sin_addr));

int32_t sd = socket(AF_INET, SOCK_DGRAM, 0);

/// Copy the interface name to the request message
bcopy(interfaceName, (char*)&my_ifr.ifr_name, sizeof(struct ifreq));

/// Try reading the Interface flags
if(ioctl(sd, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
{
printf("ERROR reading flags\n");
}

/// Try to write the netmask
my_ifr.ifr_ifru.ifru_addr = *(struct sockaddr *)(&netmask_addr);
if(ioctl(sd, SIOCSIFNETMASK, (caddr_t)&my_ifr) < 0)
{
printf("ERROR setting netmask\n");
}

/// Try to write the new dst addr
my_ifr.ifr_ifru.ifru_addr = *(struct sockaddr *)(&ip_addr);
if(ioctl(sd, SIOCSIFDSTADDR, (caddr_t)&my_ifr) < 0)
{
printf("ERROR setting dst addr\n");
}

return 0;
}
----- End of code -----
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I associated tun to an unnumbered interface? Fustian Linux - Networking 0 09-17-2010 10:30 AM
set up tun interface birjodh Linux - Networking 0 06-18-2007 03:12 PM
Reconfiguring an internal IP address RoughEdge Linux - Networking 3 06-11-2007 06:34 AM
TUN Interface Peter John Linux - Networking 1 01-09-2007 08:18 PM
Reconfiguring an interface to run in promiscuous mode with no ip address kaplan71 Fedora 1 10-13-2004 02:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration