diff options
author | Sean Nowlan <sean.nowlan@gtri.gatech.edu> | 2016-01-13 16:54:43 -0500 |
---|---|---|
committer | Sean Nowlan <sean.nowlan@gtri.gatech.edu> | 2016-01-13 16:54:43 -0500 |
commit | a736f885aef1f40d163a55185533c851c3a33a6e (patch) | |
tree | 238255db1a40437b346838ef1cab87922ebf8802 | |
parent | fc515b5e5333e5fbdbdadda6cd626150de190ade (diff) |
blocks: fixed issue #853: set MTU on tun/tap network interface in TUNTAP PDU block xtor
-rw-r--r-- | gr-blocks/lib/tuntap_pdu_impl.cc | 33 | ||||
-rw-r--r-- | gr-blocks/lib/tuntap_pdu_impl.h | 1 |
2 files changed, 34 insertions, 0 deletions
diff --git a/gr-blocks/lib/tuntap_pdu_impl.cc b/gr-blocks/lib/tuntap_pdu_impl.cc index 45995e4803..391b33937a 100644 --- a/gr-blocks/lib/tuntap_pdu_impl.cc +++ b/gr-blocks/lib/tuntap_pdu_impl.cc @@ -76,6 +76,14 @@ namespace gr { if (d_fd <= 0) throw std::runtime_error("gr::tuntap_pdu::make: tun_alloc failed (are you running as root?)"); + int err = set_mtu(dev_cstr, MTU); + if(err < 0) + std::cerr << boost::format( + "gr::tuntap_pdu: failed to set MTU to %d.\n" + "You should use ifconfig to set the MTU. E.g.,\n" + " $ sudo ifconfig %s mtu %d\n" + ) % MTU % dev % MTU << std::endl; + std::cout << boost::format( "Allocated virtual ethernet interface: %s\n" "You must now use ifconfig to set its IP address. E.g.,\n" @@ -140,6 +148,31 @@ namespace gr { */ return fd; } + + int + tuntap_pdu_impl::set_mtu(const char *dev, int MTU) + { + struct ifreq ifr; + int sfd, err; + + /* MTU must be set by passing a socket fd to ioctl; + * create an arbitrary socket for this purpose + */ + if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) + return sfd; + + /* preparation of the struct ifr, of type "struct ifreq" */ + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, dev, IFNAMSIZ); + ifr.ifr_addr.sa_family = AF_INET; /* address family */ + ifr.ifr_mtu = MTU; + + /* try to set MTU */ + if ((err = ioctl(sfd, SIOCSIFMTU, (void *) &ifr)) < 0) + return err; + + return MTU; + } #endif } /* namespace blocks */ diff --git a/gr-blocks/lib/tuntap_pdu_impl.h b/gr-blocks/lib/tuntap_pdu_impl.h index 360f954630..3a53e3acb5 100644 --- a/gr-blocks/lib/tuntap_pdu_impl.h +++ b/gr-blocks/lib/tuntap_pdu_impl.h @@ -40,6 +40,7 @@ namespace gr { std::string d_dev; bool d_istunflag; int tun_alloc(char *dev, int flags); + int set_mtu(const char *dev, int MTU); public: tuntap_pdu_impl(std::string dev, int MTU, bool istunflag); |