도와주세요!!
글 수 15,339
2012.02.02 15:33:40 (*.174.184.100)
19240
tun/tap 가상 인터페이스를 생성하려고 하는데요
아래코드를 이용하여 ip주소를 갖는 가상 디바이스를 하나 올리려하는데요
파일디스크립터는 가져오는데..실제로 인터페이스가 올라오질 않는데...뭐가문제인가요?
IFF_TUN 를 IFF_TAP 으로 바꾸어도 마찬가지입니다..ㅠㅠ
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <asm/ioctl.h>
int tun_up(char *dev)
{
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
return -1;
memset(&ifr, 0, sizeof(ifr));
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if( *dev )
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int main(void)
{
char tun_dev[16]="tap0";
char set[50]="";
int n = 0;
int fd;
n += sprintf(set + n, "%s", "ifconfig ");
n += sprintf(set + n, "%s", tun_dev);
n += sprintf(set + n, "%s", " 192.168.0.1 netmask 255.255.255.0");
if ( (fd = tun_up(tun_dev)) < 0 )
{
printf("fail\n");
}
else
{
system(set);
printf("%s dev fd = %d\n",tun_dev,fd);
}
}

