/* cattap.c 2004 David Baird This program demonstrates how to cat data from a tun interface. tun gives user space programs the ability to tunnel network traffic through custom protocols and hardware. Resources: http://vtun.sourceforge.net/tun/ http://www.sics.se/~adam/uip/download.html (uip) /usr/src/linux/Documentation/networking/tuntap.txt rainbow.nmt.edu: /usr/src/linux-2.4/Documentation/networking/tuntap.txt qemu-0.6.0/vl.c tun_open */ #include // open #include #include // system #include // struct timeval #include #include #include // ifru_* type completion #include // struct ifreq #include // constants: IFF_TUN, etc. #define TUNDEV "/dev/net/tun" int tun_init(char *ipaddr) { int fd; struct ifreq ifr; char buf[512]; fd = open(TUNDEV, O_RDWR); if(fd == -1) { perror(TUNDEV); exit(1); } bzero(&ifr, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; strcpy(); if(ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { close(fd); perror("ioctl TUNSETIFF"); exit(1); } //sprintf(buf, "/sbin/ifconfig tun0 inet %s", ipaddr); sprintf(buf, "/sbin/ifconfig tun0 %s netmask 255.255.255.0", ipaddr); system(buf); return fd; } int my_select(int fd, int msec_timeout) { fd_set fdset; struct timeval tv; int ret; tv.tv_sec = msec_timeout / 1000; tv.tv_usec = (msec_timeout - tv.tv_sec * 1000) * 1000; FD_ZERO(&fdset); FD_SET(fd, &fdset); ret = select(fd + 1, &fdset, NULL, NULL, &tv); return ret; } int main() { int tunfd; const int BUF_SIZE = 2048; char buf[BUF_SIZE]; int ret; tunfd = tun_init("192.168.3.33"); while(1) { if(my_select(tunfd, 100)) { //printf("selected\n"); ret = read(tunfd, buf, BUF_SIZE); fwrite(buf, ret, 1, stdout); fwrite("*--*", 4, 1, stdout); fflush(stdout); } } return 0; }