Linux C语言 取得MTU (最大传输单元)
参照这篇博客: 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 (最大传输单元)的更多相关文章
- (转载) 小议TCP的MSS(最大分段)以及MTU(最大传输单元)
[背景知识] MTU: Maximum Transmission Unit 最大传输单元 MSS: Maximum Segment Size 最大分段大小PPPoE: PPP Over Ethern ...
- NB-IOT使用LWM2M移动onenet对接之MTU最大传输单元设置
1. 最近遇到的一个项目NB-IOT使用LWM2M移动onenet对接,要求设置传输的MTU,因此首先需要搞懂MTU是什么? 以太网的MTU值是1500 bytes,假设发送者的协议高层向IP层发送了 ...
- 最大传输单元MTU
http://baike.baidu.com/link?url=mU41JFjZzOb3R5crQFCNdocT5ovAswcoIqL2A4U6O5Re_U0-HIYndHG0vSKwc6HbptvH ...
- Windows上最大传输单元MTU值的查看和设置
最近使用ssh工具在VPN环境下连接一个生产环境的Linux主机的时候,发现经常出现输入命令后卡死的情况.最开始以为是Linux主机的问题,问了一些老同事之后发现原来是我自己电脑的最大传输单元MTU和 ...
- 关于最大传输单元(MTU)的整理
MTU设置不当,可能会导致许多网络问题,如某些网络应用无法使用,某些网站无法访问等.下面是在网上搜索整理的关于MTU设置的东西,某些可能未作验证,仅供参考. 1. 如何确定网络MTU 某些ISP接入的 ...
- MTU(Maximum transmission unit) 最大传输单元
最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作. 路径MTU: 网路 ...
- TCP/IP协议:最大传输单元MTU 和 最大分节大小MSS
MTU = MSS + TCP Header + IP Header. mtu是网络传输最大报文包. mss是网络传输数据最大值. MTU:maximum transmission unit,最大传输 ...
- MTU介绍以及在windows和linux下怎么设置MTU值
最大传输单元MTU(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡 ...
- MTU 最大传输单位
MTU 最大传输单位 通过上面 MAC 封装的定义,现在我们知道标准以太网络frame所能传送的数据量最大可以到达 1500 bytes , 这个数值就被我们称为 MTU (Maximum Trans ...
随机推荐
- 使用Postfix与Dovecot收发电子邮件(物理机虚拟机之间)
邮件应用协议包括: 简单邮件传输协议(SMTP),用来发送或中转发出的电子邮件,占用tcp 25端口. 第三版邮局协议(POP3),用于将服务器上把邮件存储到本地主机,占用tcp 110端口. 第四版 ...
- 一个命令搞定 Web 国际化
背景 随着出海的业务越来越多,web 应用面临越来越多的国际化的工作.如何高效,高质量的完成 Web 前端国际化工作,已经是摆在 web 前端同学的急需解决的问题. i18n-helper-cli 是 ...
- Windows内核基础知识-2-段描述符
Windows内核基础知识-2-段描述符 比如: ES 002B 0(FFFFFFFF) 意思就是es段寄存器,段选择子/段选择符 为002B, 起始地址base为0, 限制范围Limit地址最大能寻 ...
- bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value
原文: https://feiffy.cc/uncaught-typeerror-cannot-convert-object-to-primitive-value 最近发现我的博客页面移动端上下拉菜单 ...
- vue:Missing space before value for key 'components'
原因是Vue对语法比较严格,而eslint是一个语法检查工具,对语法要求极其苛刻严格,于是就error了 解决办法是关闭eslint的语法规则,找到build/webpack.base.conf.js ...
- QT 自定义控件 以及信号和槽的使用
自定义login 控件 Login头文件 #ifndef LOGIN_H #define LOGIN_H #include <QWidget> namespace Ui { class L ...
- Ubuntu防火墙:ufw
原始linux的防火墙是iptables,以为过于繁琐,各个发行版几乎都有自己的方案; ubuntu下的防火墙是ufw[ubuntu fireward的缩写],centos的防火墙是fireward ...
- JSP编码问题
JSP的开头内容: 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 ...
- Win10安装gcc
使用MinGW安装gcc 1.下载MinGW,地址 https://sourceforge.net/projects/mingw/files/ ,选择Download mingw-get-setup. ...
- T-SQL - 习题02_将数据表year|month|amount查询成year|m1|m2|m3|m4的样式
时间:2017-09-11 整理:byzqy 题目:有个年度统计表,结构如下: 怎么样把这个表,查询成这样一个结果: 这是在面试过程中遇到的一个关于数据库的题,没有一点思路,不知它考查到的知识点是什么 ...