Skip to content

Commit

Permalink
blocks: fixed issue #853: set MTU on tun/tap network interface in TUN…
Browse files Browse the repository at this point in the history
…TAP PDU block xtor
  • Loading branch information
Sean Nowlan committed Jan 13, 2016
1 parent fc515b5 commit a736f88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gr-blocks/lib/tuntap_pdu_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions gr-blocks/lib/tuntap_pdu_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a736f88

Please sign in to comment.