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 -----
|