TAPデバイスへの設定
30分位悩んでしまったが、tapデバイス作成の時のファイルディスクリプタはソケットじゃないのでネットワークインタフェースの設定に使えないのだった。なので、インタフェース設定の為に別途ソケットを生成する必要がある。以下のような関数を呼び出すことでTAPデバイス作成後にデバイスを有効にしてかつPromiscousモードに設定する(参考URL:NETDEVICE(7))
int set_tapinf(char *dev) { int fd; struct ifreq ifr; //main中のfdはソケットではないので改めてソケットを生成 fd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)); if(fd == -1){ perror("Error Creating Scoket"); } memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, dev, IFNAMSIZ); // インタフェースの状態を取得 if(ioctl(fd,SIOCGIFFLAGS,&ifr) < 0){ perror("Getting information"); close(fd); exit(1); } // インタフェースを有効にする ifr.ifr_flags |= IFF_UP; // Pormiscous Mode ifr.ifr_flags |= IFF_PROMISC; if(ioctl(fd,SIOCSIFFLAGS,&ifr) < 0){ perror("Setting Promiscous Mode"); close(fd); exit(1); } close(fd); return 0; }
The above function is to be used for setting flags on the tap device. The file descriptor generated on creating tap device is not a socket so we need to open socket separately.