TCP/IP 协议栈和主要层
TCP/IP协议栈,指的是整个四(五)层协议组(protocol suite)。
TCP/IP RFCs
A TCP/IP Tutorial: https://tools.ietf.org/html/rfc1180
https://technet.microsoft.com/en-us/library/cc737968(v=ws.10).aspx
http://www.networksorcery.com/enp/topic/ipsuite.htm
一般参考资料
http://www.tcpipguide.com/free/t_TCPIPProtocols.htm
https://en.wikipedia.org/wiki/Internet_protocol_suite
总四层模型
- Application layer:提供让应用程序使用其他层的服务的能力(provides applications the ability to access the services of the other layers and defines the protocols that applications use to exchange data).
- Transport layer:为应用层提供会话和数据报通讯服务(providing the Application layer with session and datagram communication services),核心协议是TCP和UDP.
- Internet layer(Cisco称其为Internetwork):负责寻址、包和路由(responsible for addressing, packaging, and routing functions),核心协议是IP,ARP,ICMP,and IGMP.
- Link layer(Cisco称其为Network interface):负责收发传输(responsible for placing TCP/IP packets on the network medium and receiving TCP/IP packets off the network medium).
Physical(Cisco)
各层的主要功能描述,参见:https://technet.microsoft.com/en-us/library/cc958821.aspx
应用层协议描述:https://www.youtube.com/playlist?list=PL8lNYIt_LuG5hbKVquPm_HgiV8vgM54OB
传输层协议描述:https://www.youtube.com/playlist?list=PL8lNYIt_LuG5mr_M_K2jr9OnsNz9XAm11
IP寻址描述:https://www.youtube.com/playlist?list=PL8lNYIt_LuG52hLQJ_3GDcOCRJDX6RCvH
网络访问描述:https://www.youtube.com/playlist?list=PL8lNYIt_LuG6tDI6pJhbbvgoOSKC1ZVsN
链路层技术描述:https://www.youtube.com/playlist?list=PL8lNYIt_LuG4B1CFGu_XoC1Oy5e7O88Rz
各层之间的数据封装:
| Data | =Application
| UDP header | UDP data | =Transport
| IP header | IP data | =Internet
| Frame header | Frame data | Frame footer | =Link
在TCP/IP这两层的程序
这两层的程序
参考:
http://searchnetworking.techtarget.com/definition/TCP-IP
TCP/IP is a two-layer program.
The higher layer, Transmission Control Protocol, manages the assembling of a message or file into smaller packets that are transmitted over the Internet and received by a TCP layer that reassembles the packets into the original message.
The lower layer, Internet Protocol, handles the address part of each packet so that it gets to the right destination. Each gateway computer on the network checks this address to see where to forward the message. Even though some packets from the same message are routed differently than others, they’ll be reassembled at the destination.
端口号
参见:http://searchnetworking.techtarget.com/definition/port-number
A port number is a way to identify a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.
For the Transmission Control Protocol and the User Datagram Protocol, a port number is a 16-bit integer that is put in the header appended to a message unit. This port number is passed logically between client and server transport layers and physically between the transport layer and the Internet Protocol layer and forwarded on.
socket
参见:
http://www.tcpipguide.com/free/t_TCPIPSocketsandSocketPairsProcessandConnectionIden.htm
https://docs.oracle.com/javase/tutorial/networking/sockets/
https://en.wikipedia.org/wiki/Berkeley_sockets
socket = IP:端口号
每一条TCP连接唯一地被通讯两端的两个端点(即两个套接字)所确定:
TCP连接 = {socket1, socket2} = {(IP1:port1), (IP2:port2)}
TCP连接的端点是套接字。同一个IP地址可以有多个不同的TCP连接,而同一个端口号也可以出现在多个不同的TCP连接中。
message和datagram和packet和frame
参见:
http://www.tcpipguide.com/free/t_MessagesPacketsFramesDatagramsandCells-2.htm
https://www.youtube.com/watch?v=-5i1T7oru8o
https://tools.ietf.org/html/rfc1594#section-13
http://searchnetworking.techtarget.com/definition/datagram
http://searchnetworking.techtarget.com/definition/packet
http://searchnetworking.techtarget.com/definition/frame
说明:
Frame, packet and message are same entity but called differently at the different layer because there are data encapsulations at every layer.
- Datagram:
rfc定义:
A self-contained, independent entity of data carrying sufficient information to be routed from the source to the destination computer without reliance on earlier exchanges between this source and destination computer and the transporting network.
datagram用在3层上,与packet同义:
This term is basically synonymous with “packet” and is also used to refer to network layer technologies. It is also often used to refer to a message that is sent at a higher level of the OSI Reference Model (more often than “packet” is).
- Packet:
用在3层上:
This term is considered by many to most correctly refer to a message sent by protocols operating at the network layer of the OSI Reference Model. So, you will commonly see people refer to “IP packets”.
也就是说:A packet is the unit of data that is routed between an origin and a destination on the Internet or any other packet-switched network.
- Frame:
常用在2层上:
This term is most commonly associated with messages that travel at low levels of the OSI Reference Model. In particular, it is most commonly seen used in reference to data link layer messages.
小结:
在2层:frame
在3层:datagram,或packet
在3层以上:message,更上层的也称segment
即:frame -> datagram(packet) -> message -> segment
TCP
参见:
https://www.ietf.org/rfc/rfc793.txt
http://searchnetworking.techtarget.com/definition/TCP
TCP is a connection-oriented protocol, which means a connection is established and maintained until the application programs at each end have finished exchanging messages.
It determines how to break application data into packets that networks can deliver, sends packets to and accepts packets from the network layer, manages flow control, and—because it is meant to provide error-free data transmission—handles retransmission of dropped or garbled packets as well as acknowledgement of all packets that arrive.
tcp的三向握手,参考:
https://support.microsoft.com/en-us/kb/172983
http://www.tcpipguide.com/free/t_TCPConnectionEstablishmentProcessTheThreeWayHandsh.htm
http://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml
UDP
参见:
https://www.ietf.org/rfc/rfc768.txt
http://searchsoa.techtarget.com/definition/UDP
IP
参见:
https://www.ietf.org/rfc/rfc791.txt
http://searchunifiedcommunications.techtarget.com/definition/Internet-Protocol
Router和Gateway
参见:
https://tools.ietf.org/html/rfc1812#section-2.2.3
https://tools.ietf.org/html/rfc2664#section-2.5
router:
router:A device which forwards traffic between networks. The forwarding decision is based on network layer information and routing tables, often constructed by routing protocols.
Every computer on the Internet has a unique Internet “address” that identifies it from among the millions of computers. The Internet has specialized computers between the source and destination located at network inter-connection points. These computers are known as “routers.”
gateway:
In the Internet model, constituent networks are connected together by IP datagram forwarders which are called routers or IP routers. In this document, every use of the term router is equivalent to IP router. Many older Internet documents refer to routers as gateways.
—— rfc1812
socket编程
参考:
http://www.linuxhowtos.org/C_C++/socket.htm
http://www.tenouk.com/cnlinuxsockettutorials.html
UNIX domain示例,可以参考man 2 bind
socket程序流(flow)示意:
/* client socket */
socket()
connect()
while (x)
{
write()
read()
}
close()
/* server socket */
socket()
bind()
listen()
while (1)
{
// select()
accept() // new_socket_fd
while (x)
{
read()
write()
}
close() // new_socket_fd
}
close()
socket编程中的一些杂项
参考:
man 7 socket
man 7 ip
man 7 unix
几个重要数据结构:
/********************
* 类型定义见<sys/socket.h>等:
*
* sa_family_t 即 unsigned short
*
* struct sockaddr
*
* AF_UNIX,AF_INET 等常量
* SOCK_STREAM,SOCK_DGRAM 等枚举常量
********************
* 类型定义见<netinet/in.h>:
*
* in_port_t 即 uint16_t
* in_addr_t 即 uint32_t
*
* struct sockaddr_in
* struct in_addr
*
* INADDR_ANY 等常量
********************
* 类型定义见<sys/un.h>:
*
* struct sockaddr_un
********************
* 类型定义见<netdb.h>:
*
* struct hostent
********************/
struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];
}
//------Internet domain socket address------
struct sockaddr_in
{
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* internet address. */
struct in_addr
{
uint32_t s_addr; /* address in network byte order */
};
//------UNIX domain socket address------
struct sockaddr_un
{
sa_family_t sun_family; /* AF_UNIX */
char sun_path[108]; /* pathname */
};
主机地址(Host Address)与主机名(Host Names)的相关函数
参考: http://www.tenouk.com/Module39b.html
主机地址是什么
IPv4主机名是什么
参考 man 7 hostname主机地址的数据类型
// 头文件'in.h'
/* Internet address. */
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
INADDR_LOOPBACK (127.0.0.1)
INADDR_ANY (0.0.0.0)
INADDR_BROADCAST (255.255.255.255)主机地址的操作函数
// 头文件'arpa/inet.h'
inet_addr()
inet_network()
inet_aton() // 不建议使用inet_addr()和inet_network(),参考man 3 inet
inet_ntoa()
inet_makeaddr()
inet_lnaof()
inet_netof()
...
inet_pton()
inet_ntop()主机名与主机地址间的转换
// 头文件'netdb.h'
struct hostent
{
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
gethostbyname()
gethostbyaddr()
// gethostbyname()或gethostbyaddr()失败的原因:
extern int h_errno;
sethostent()
gethostent()
endhostent()
其他几个函数:
//主机与网络的字节序,参考man 3 byteorder
htonl()
htons()
ntohl()
ntohs()
select() // 参考man 2 select
poll() // 参考man 2 poll
epoll() // 参考man 7 epoll