Skip to contentSkip to conversation

sockets -> MTU (Maximum Transfer Unit)

DISCUSSION ramzez 2 participants 2 messages Jul 25, 2003

lore
Share & tools
lore

ramzez

author
#0 lore raw

Hi friends...

In my C app, How do I can know the MTU (Maximum Transfer Unit) of my
net?

thanks

Signature · 7 lines
  Linux User Registered #232544
        ICQ : 337889406
  GnuPG-key : www.keyserver.net
 -------------------------------
"Software is like sex,
      it's better when it's free."
                  [Linus Torvalds]
#1 lore raw
Quoted 2 lines · ramzez

In my C app, How do I can know the MTU (Maximum Transfer Unit) of my
net?

You can obtain the MTU of an interface with ioctl(SIOCGIFMTU), e.g.

	const char if_name[] = "eth0";
	struct ifreq ifr;
	int fd;

	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

	strcpy(ifr.ifr_name, if_name);
	ioctl(fd, SIOCGIFMTU, &ifr);

	printf("MTU of %s is %d\n", if_name, ifr.ifr_mtu);

Assuming that path MTU discovery is enabled, you can obtain the path MTU of an individual connection using getsockopt(SOL_IP, IP_MTU), e.g.

	int mtu;
	int mtulen = sizeof(mtu);

	getsockopt(skt, SOL_IP, IP_MTU, &mtu, &mtulen);
Signature · 1 line
Glynn Clements <[email protected]>