Finding MTU

Skip Ford skip.ford at verizon.net
Sun Aug 29 21:52:37 PDT 2004


Dennis George wrote:
> Can anybody tell me how to find the MTU (Maximum Transmitting
> Unit) in freeBSD programatically...

The full source for ifconfig(8) is available.  No need to ask
anyone...

http://www.freebsd.org/cgi/cvsweb.cgi/src/sbin/ifconfig/ifconfig.c?rev=1.106&content-type=text/x-cvsweb-markup

However, here's a small program, probably taken from the sources
above at some point, that prints the MTU for ed0.

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>

#include <err.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int
main(void)
{
	int s, af = AF_INET;
	char *name = "ed0";
	struct ifreq ifr;

	if ((s = socket(af, SOCK_DGRAM, 0)) < 0)
		err(1, "socket");

	ifr.ifr_addr.sa_family = AF_INET;
	strcpy(ifr.ifr_name, name);
	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
		warn("ioctl (get mtu)");

	fprintf(stdout, "MTU of %s is %d.\n", name, ifr.ifr_mtu);
	close(s);
	return(0);
}

-- 
Skip


More information about the freebsd-hackers mailing list