参照这篇博客: http://www.geekpage.jp/programming/linux-network/book/04/4-21.php

* 查看主机当前网卡,哪块在使用.

ifconfig

  

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128
inet 127.0.0.1 netmask 0xff000000
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether a4:5e:60:d6:57:17
inet6 fe80::a65e:60ff:fed6:5717%en0 prefixlen 64 scopeid 0x4
inet 192.168.16.103 netmask 0xffffff00 broadcast 192.168.16.255
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
en1: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500
options=60<TSO4,TSO6>
ether 4a:00:01:33:73:10
media: autoselect <full-duplex>
status: inactive
en2: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500
options=60<TSO4,TSO6>
ether 4a:00:01:33:73:11
media: autoselect <full-duplex>
status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 06:5e:60:d6:57:17
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether 7a:00:b6:6c:ea:a0
inet6 fe80::7800:b6ff:fe6c:eaa0%awdl0 prefixlen 64 scopeid 0x8
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
bridge0: flags=8822<BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether a6:5e:60:6d:62:00
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 5 priority 0 path cost 0
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 6 priority 0 path cost 0
media: <unknown type>
status: inactive

ifconfig 输出

ifconfig en0

  

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether a4:5e:60:d6:57:17
inet6 fe80::a65e:60ff:fed6:5717%en0 prefixlen 64 scopeid 0x4
inet 192.168.16.103 netmask 0xffffff00 broadcast 192.168.16.255
nd6 options=1<PERFORMNUD>
media: autoselect
status: active

可以看到en0网卡有有效的ip地址net 192.168.16.103, 说明这块网卡在使用, Linux环境通常是eth0.

* 创建文件main.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h> int main(int argc, const char * argv[]) {
int fd;
struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); strncpy(ifr.ifr_name, "en0", IFNAMSIZ-1); // eth0
if (ioctl(fd, SIOCGIFMTU, &ifr)) {
perror("ioctl");
return 1;
}
close(fd); printf("%d\n", ifr.ifr_mtu); return 0;
}

  

* 编译运行

p.p1 { margin: 0; font: 11px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures }

1500

Program ended with exit code: 0

得出MTU为1500

* 一些函数原型

#include <sys/socket.h>

int socket(int domain, int type, int protocol);

socket.h

p.p1 { margin: 0; font: 14px Menlo; color: rgba(0, 132, 0, 1) }
p.p2 { margin: 0; font: 14px Menlo; color: rgba(120, 73, 42, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; color: rgba(39, 42, 216, 1) }
span.s3 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 132, 0, 1) }
span.s4 { font-variant-ligatures: no-common-ligatures; color: rgba(120, 73, 42, 1) }
span.Apple-tab-span { white-space: pre }

/*

* Types

*/

#define SOCK_STREAM 1 /* stream socket */

#define SOCK_DGRAM 2 /* datagram socket */

#define SOCK_RAW 3 /* raw-protocol interface */

// ...

p.p1 { margin: 0; font: 14px Menlo; color: rgba(0, 132, 0, 1) }
p.p2 { margin: 0; font: 14px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; color: rgba(187, 44, 162, 1) }
span.s3 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 132, 0, 1) }
span.s4 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 0, 0, 1) }
span.s5 { font-variant-ligatures: no-common-ligatures; color: rgba(39, 42, 216, 1) }
span.Apple-tab-span { white-space: pre }

/*

* [XSI] Structure used by kernel to store most addresses.

*/

struct sockaddr {

__uint8_t sa_len; /* total length */

sa_family_t sa_family; /* [XSI] address family */

char sa_data[14]; /* [XSI] addr value (actually larger) */

};

p.p1 { margin: 0; font: 14px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures; color: rgba(187, 44, 162, 1) }
span.s2 { font-variant-ligatures: no-common-ligatures }
span.Apple-tab-span { white-space: pre }
p.p1 { margin: 0; font: 14px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures; color: rgba(187, 44, 162, 1) }
span.s2 { font-variant-ligatures: no-common-ligatures }
span.Apple-tab-span { white-space: pre }

int accept(int, struct sockaddr * __restrict, socklen_t * __restrict)

__DARWIN_ALIAS_C(accept);

int bind(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS(bind);

int connect(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(connect);

////////////////////////////////////////////////////////////////

if.h

////////////////////////////////////////////////////////////////

p.p1 { margin: 0; font: 14px Menlo; color: rgba(0, 132, 0, 1) }
p.p2 { margin: 0; font: 14px Menlo }
p.p3 { margin: 0; font: 14px Menlo; color: rgba(120, 73, 42, 1) }
p.p4 { margin: 0; font: 14px Menlo; color: rgba(187, 44, 162, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; color: rgba(187, 44, 162, 1) }
span.s3 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 0, 0, 1) }
span.s4 { font-variant-ligatures: no-common-ligatures; color: rgba(39, 42, 216, 1) }
span.s5 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 132, 0, 1) }
span.Apple-tab-span { white-space: pre }

/*

* Interface request structure used for socket

* ioctl's.  All interface ioctl's must have parameter

* definitions which begin with ifr_name.  The

* remainder may be interface specific.

*/

struct ifreq {

#ifndef IFNAMSIZ

#define IFNAMSIZ IF_NAMESIZE

#endif

char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */

union {

struct sockaddr ifru_addr;

struct sockaddr ifru_dstaddr;

struct sockaddr ifru_broadaddr;

short ifru_flags;

int ifru_metric;

int ifru_mtu;

int ifru_phys;

int ifru_media;

int ifru_intval;

caddr_t ifru_data;

struct ifdevmtu ifru_devmtu;

struct ifkpi ifru_kpi;

u_int32_t ifru_wake_flags;

u_int32_t ifru_route_refcnt;

int ifru_cap[2];

} ifr_ifru;

#define ifr_addr ifr_ifru.ifru_addr /* address */

#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */

#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */

/////////////////////////////////////////////////

ioctl -- control device

SYNOPSIS
#include <sys/ioctl.h>

int ioctl(int fildes, unsigned long request, ...);

p.p1 { margin: 0; font: 14px Menlo; color: rgba(187, 44, 162, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 0, 0, 1) }
span.Apple-tab-span { white-space: pre }
p.p1 { margin: 0; font: 14px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures; color: rgba(187, 44, 162, 1) }
span.s2 { font-variant-ligatures: no-common-ligatures }
span.Apple-tab-span { white-space: pre }

Linux C语言 取得MTU (最大传输单元)的更多相关文章

  1. (转载) 小议TCP的MSS(最大分段)以及MTU(最大传输单元)

    [背景知识]  MTU: Maximum Transmission Unit 最大传输单元 MSS: Maximum Segment Size 最大分段大小PPPoE: PPP Over Ethern ...

  2. NB-IOT使用LWM2M移动onenet对接之MTU最大传输单元设置

    1. 最近遇到的一个项目NB-IOT使用LWM2M移动onenet对接,要求设置传输的MTU,因此首先需要搞懂MTU是什么? 以太网的MTU值是1500 bytes,假设发送者的协议高层向IP层发送了 ...

  3. 最大传输单元MTU

    http://baike.baidu.com/link?url=mU41JFjZzOb3R5crQFCNdocT5ovAswcoIqL2A4U6O5Re_U0-HIYndHG0vSKwc6HbptvH ...

  4. Windows上最大传输单元MTU值的查看和设置

    最近使用ssh工具在VPN环境下连接一个生产环境的Linux主机的时候,发现经常出现输入命令后卡死的情况.最开始以为是Linux主机的问题,问了一些老同事之后发现原来是我自己电脑的最大传输单元MTU和 ...

  5. 关于最大传输单元(MTU)的整理

    MTU设置不当,可能会导致许多网络问题,如某些网络应用无法使用,某些网站无法访问等.下面是在网上搜索整理的关于MTU设置的东西,某些可能未作验证,仅供参考. 1. 如何确定网络MTU 某些ISP接入的 ...

  6. MTU(Maximum transmission unit) 最大传输单元

    最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作.   路径MTU: 网路 ...

  7. TCP/IP协议:最大传输单元MTU 和 最大分节大小MSS

    MTU = MSS + TCP Header + IP Header. mtu是网络传输最大报文包. mss是网络传输数据最大值. MTU:maximum transmission unit,最大传输 ...

  8. MTU介绍以及在windows和linux下怎么设置MTU值

    最大传输单元MTU(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡 ...

  9. MTU 最大传输单位

    MTU 最大传输单位 通过上面 MAC 封装的定义,现在我们知道标准以太网络frame所能传送的数据量最大可以到达 1500 bytes , 这个数值就被我们称为 MTU (Maximum Trans ...

随机推荐

  1. cobaltstrike 框架简述

    关于cobalt strike,火起来也有好几年了,首先感谢大佬们慷慨相助愿意在网上分享和翻译相关资料,让这么好的渗透测试框架工具被更多人知道 那就来整理一下在使用这个框架的过程中我认为需要了解的小知 ...

  2. Linux centos 安装 ftp(Vsftp) 与 设置ftp(Vsftp)

    本文章只是简单搭建,因为公司只须要简单使用,虽然简单但是之前也走了一些弯路,所以决定把过程记录下来. 一.Vsftp安装与卸载 安装:yum install vsftpd 卸载:yum remove ...

  3. JAVA 各种时间类型转换

    final Date date = new Date(); final Timestamp timestamp = new Timestamp(date.getTime()); final Calen ...

  4. 微信小程序切换选中状态

     实现的主要思路是根据每一项的index值,动态改变idx值,当index==idx值的时候,添加点击选中样式的类名. wxml: <scroll-view scroll-x="tru ...

  5. 使用 & 进行高效率取余运算

    Java的HashMap源码中用到的(n-1)&hash这样的运算,这是一种高效的求余数的方法 结论:假设被除数是x,对于除数是2n的取余操作x%2n,都可以写成x&(2n-1),位运 ...

  6. Servlet 之文件下载

    Servlet 之文件下载 import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; impor ...

  7. openresty lua-resty-string md5 sha aes random string

    安装 https://github.com/openresty/lua-resty-string $ sudo opm get openresty/lua-resty-string $ ls -al ...

  8. JS 之 每日一题 之 算法 ( 划分字母区间 )

    题目详解: 字符串 S 由小写字母组成.我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段.返回一个表示每个字符串片段的长度的列表. 例子: 示例 1: 输入:S = &quo ...

  9. Kubernetes-kubectl介绍

    前言 本篇是Kubernetes第三篇,大家一定要把环境搭建起来,看是解决不了问题的,必须实战.本篇重要介绍kubectl的使用. Kubernetes系列文章: Kubernetes介绍 Kuber ...

  10. K8S最小硬件配置