您的位置:首页 > 理论基础 > 计算机网络

从TPS,TCP/IP到 IP,CIDR,Socket等相关网络概念的梳理

2014-02-25 16:44 1121 查看

0.前言:

本篇文章主要梳理了一下 IPS、TCP/IP、TCP、UDP、IP、IP Address、IPv4、CIDR、Socket、Socket API、Berkeley之间的关系。整篇文章主要借鉴了英文资料尽量调重点简洁明了的介绍。(不用中文主要是因为看中文资料时,英文名词简称不得不和中文翻译对应着看,但各个中文资料的翻译好像又不完全相同,反而会弄得晕呼呼的^^;)

做网络相关的项目也有一段时间了,这些名词看着眼熟又似懂非懂的,往往实际编码过程中遇到Bug是因为根本原理上不理解而找不到错误的源头。比如下面这个例子运行后sendto的时候提示错误。大牛估计一眼就看出来是哪里出现问题了,菜鸟比如我就找了很久(特别还是在Android和ndk联合调试中,折磨的我要死了。。。。)

int m_client_sockfd; //客户端套接字
struct sockaddr_in m_remote_addr; //客户端网络地址结构体
/* 服务器ip/port */
this->m_sServerIP = "127.0.0.0";
this->m_ServerPort = 8010;

/* 初始化Sockt UDP Clicet */
memset(&m_remote_addr, 0, sizeof(m_remote_addr)); //数据初始化--清零
m_remote_addr.sin_family = AF_INET; //设置为IP通信
m_remote_addr.sin_addr.s_addr = inet_addr(m_sServerIP.c_str()); //服务器IP地址
m_remote_addr.sin_port = htons(this->m_ServerPort); //服务器端口号

/*创建客户端套接字--IPv4协议,面向无连接通信,UDP协议*/
if ((m_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
this->m_registed = false;
}

DirectTalk_Head head;/*DirectTalk_Head为某结构体*/
memset(&head, 0, sizeof(head));

/** 向服务器注册UDPControl **/
if(sendto(m_client_sockfd, (const char*)&head, sizeof(head), 0,
(struct sockaddr *)&m_remote_addr, sizeof(struct sockaddr_in)) < 0){
/* >>实际运行时这里报错!!*/
this->m_registed = false;
}

ps:如果您对这些名词很熟,就直接关了吧,这里没什么特别的,我只是做了一些整理工作而已。

1.Internet protocol suite

The Internet protocol suite is the networking model and a set of communications
protocols used for the Internet and similar networks. It is commonly known
as TCP/IP, because its most important protocols, the Transmission Control Protocol (TCP) and the Internet
Protocol(IP), were the first networking protocols defined in this standard.
    






Encapsulation of application data descending through the layers described in RFC1122:

Application layer (user interface services and support services): This is the scope within which applications create user data and communicate this data to other applications on another
or the same host. This is where the higher level protocols such as SMTP, FTP, SSH, HTTP,
etc. operate.
Transport layer
(process-to-process): The transport layer constitutes the networking regime between two network processes, on either the same or different hosts and on either the local network or remote networks separated
by routers. Processes are addressed via "ports," and the transport layer header contains the port numbers. UDP is the basic transport layer protocol, providing communication between processes via port addresses in the header. 
Internet layer: The internet layer has the task of exchanging datagrams across network boundaries. This layer defines the addressing and routing structures used for the TCP/IP
protocol suite. The primary protocol in this scope is the Internet Protocol, which defines IP addresses. Its function in
routing is to transport datagrams to the next IP router that has the connectivity to a network closer to the final data destination.
Link layer: This layer defines the networking methods within the scope of the local network link on which hosts communicate without
intervening routers. The link layer is used to move packets between the Internet layer interfaces of two different hosts on the same link. 
The following table shows various networking models. The number of layers
varies between three and seven.

Kurose,[21] Forouzan [22]Stallings[25]Tanenbaum[26]"Arpanet Reference Model" (RFC 871)OSI model
Five layersFive layersFive layersThree layersSeven layers
"Five-layer Internet model" or "TCP/IP protocol suite""TCP/IP model""TCP/IP 5-layer reference model""Arpanet reference model"OSI model
ApplicationApplicationApplicationApplication/ProcessApplication
Presentation
Session
TransportHost-to-host or transportTransportHost-to-hostTransport
NetworkInternetInternetNetwork
Data linkNetwork accessData linkNetwork interfaceData link
PhysicalPhysicalPhysical Physical

1.1 TCP/IP model : Core Protocols of TCP/IP

1.1.1 TCP/IP model - Transport layer - User
Datagram Protocol (UDP)

The User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite .
With UDP, computer applications can send messages,in this case referred to as datagrams,
to other hosts on an Internet Protocol (IP) networkwithout
prior communicationsto set up special transmission channels or data paths. 

UDP messages are encapsulated and sent within IP datagrams, as shown in the following illustration.




Service ports
Applications use datagram sockets to
establish host-to-host communications. An application binds a socket to its endpoint of data transmission,which is a combination of an IP
address and a service port.(Note that it is the combination of IP
address and port number together that must be globally unique. ) A
port is a software structure that is identified by the port number, a 16 bit integer
value, allowing for port numbers between 0 and 65535. Port 0 is reserved, but is a permissible source port value if the sending process does not expect messages in response.

Ports 0        - 1023     - well known ports
Ports 1024  - 49151   - Registered port: vendors use for applications
Ports 49151- 65535   - dynamic / private ports
Packet structure-UDP Header

OffsetsOctet0123
OctetBit 0 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031
00Source portDestination port
432LengthChecksum

IPv4 Pseudo HeaderWhen UDP runs over IPv4, the
checksum is computed using a "pseudo header” that contains some of the same information from the real IPv4 header. The pseudo header is not the real IPv4 header used to send an IP packet, it is used only for the checksum calculation.

IPv4 Pseudo Header Format

OffsetsOctet0123
OctetBit012345678910111213141516171819202122232425262728293031
00Source IPv4 Address
432Destination IPv4 Address
864ZeroesProtocolUDP Length(of the UDP header and data)
1296Source PortDestination Port
16128LengthChecksum

1.1.2 TCP/IP model - Transport layer - Transmission
Control Protocol (TCP)

The Transmission Control Protocol (TCP) is one of the core protocols of
the Internet protocol suite. TCP
provides a communication service at an intermediate level between an application program and the Internet Protocol (IP). That is, when an application program desires
to send a large chunk of data across the Internet using IP, instead of breaking the data into IP-sized pieces and issuing a series of IP requests, the software can issue a single request to TCP and let TCP handle the IP details.

IP works by exchanging pieces of information called packets.
A packet is a sequence of octets (bytes) and consists
of a header followed by a body. The header describes the packet's source, destination and control information. The body contains the data IP is transmitting. 

TCP pseudo-header for checksum computation (IPv4)

Bit offset0–34–78–1516–31
0Source address
32Destination address
64ZerosProtocolTCP length
96Source portDestination port
128Sequence number
160Acknowledgement number
192Data offsetReservedFlagsWindow
224ChecksumUrgent pointer
256Options (optional)
256/288+ Data  

1.1.3 TCP/IP model - Internet layer - Internet
Protocol(IP)

The Internet Protocol is responsible for addressing hosts and for routing datagrams (packets) from a source host to a destination host across
one or more IP networks. For this purpose, the Internet Protocol defines the format of packets and provides an addressing system that has two functions: identifying hosts; and providing a logical location
service.

The first major version of IP, Internet Protocol Version 4 (IPv4), is the dominant protocol
of the Internet. Its successor is Internet Protocol Version 6(IPv6).

Datagram construction

Each datagram has two components: a header and a payload.The IP
header is tagged with the source IP address, the destination IP address, and other meta-data needed to route and deliver the datagram.
The payload is the data that is transported. This method of nesting the data payload in a packet with a header is called encapsulation.

IP Packet ( IP Datagram )
IP HeaderIP Date ( IPpayload)
IPv4 Header Format

OffsetsOctet0123
OctetBit012345678910111213141516171819202122232425262728293031
00VersionIHLDSCPECNTotal Length
432IdentificationFlagsFragment Offset
864Time To LiveProtocolHeader Checksum
1296Source IP Address
16128Destination IP Address
20160Options (if IHL > 5) 
IP Addressing and Routing

IP addressing entails the assignment of IP addresses and associated parameters to host interfaces.The address space is divided into networks and subnetworks, involving the designation of network or
routing prefixes.IP addressing and routing.

IP routing is performed by all hosts, but most importantly by routers, which transport packets across network boundaries. Routers communicate with one another via specially designed routing protocols,
either interior gateway protocols or exterior gateway protocols, as needed for the topology of the network. 

1.1.3.1 Internet Protocol(IP) - IP Packet - IP
AddressAn Internet
Protocol address (IP address) is a numerical labelassigned to each device (e.g., computer, printer) participating in a computer
network that uses the Internet Protocol for
communication. An IP address serves two principal functions: host or network interface identification and
location addressing.
Its role has been characterized as follows: "A name indicates what we seek. An address indicates where
it is. A route indicates how to get there.”

Two versions of the Internet Protocol (IP) are in use: IP Version 4 and IP Version 6. Each version defines an IP address differently. 

[b]1.1.3.1.1 IP address - IPv4 addressesIPv4 uses 32-bit (four-byte)
addresses, which limits the address space to 4294967296 (232)
addresses. [/b]
Decomposition of the quad-dotted IPv4 address representation to its binary value:


1.1.3.1.2 IPv4 addresses - AllocationOriginally, an IP address was divided
into two parts: the network identifierwas the most significant (highest order) octet of
the address, and the host identifierwas the rest of the address. The system defined
five classes, Class A, B, C, D, and E. The Classes A, B, and C had different bit lengths for the new network identification. The rest of an address was used as previously to identify a host within a network, which meant that each network class had a different
capacity to address hosts. 


ClassStart addressEnd addressByte  
0123
A0.0.0.0127.255.255.255000000000(0)000000000000000000000000  
B128.0.0.0191.255.255.255100000000(128)000000000000000000000000  
C192.0.0.0223.255.255.255110000000(192)000000000000000000000000 256 (28)
D224.0.0.0239.255.255.25511100000(224) allocated for multicast addressing 
E240.0.0.0255.255.255.25511110000(240) reserved for future applications
Note:
Leading bits
Size of network number bit field
Size of rest bit field
Based on the IETF standard RFC 1517 published in 1993, this system of classes was officially replaced with Classless
Inter-Domain Routing
 (CIDR), and the class-based scheme was dubbed classful, by contrast. CIDR was designed to permit repartitioning of any address space so that smaller
or larger blocks of addresses could be allocated to users. 

1) IPv4 addresses -Allocation - Classless
Inter-Domain Routing (CIDR)

CIDR blocks, share an initial sequence of bits in the binary representation
of their IP addresses. IPv4 CIDR blocks are identified using a syntax similar to that of IPv4 addresses: a dotted-decimal address, followed by a slash, then a number from 0 to 32, e.g., a.b.c.d/n. An IP address is part of a CIDR block, and is said to match
the CIDR prefix if the initial n bits of the address and the CIDR prefix are the same. 




For example, in the late 1990s, the IP address 208.130.29.33 (since reassigned) was used bywww.freesoft.org.
An analysis of this address identified three CIDR prefixes. 208.128.0.0/11, a large CIDR block containing over 2 million addresses, had been assigned by ARIN (the
North American RIR) to MCI. Automation Research Systems, a Virginia VAR, leased an Internet connection from MCI and
was assigned the 208.130.28.0/22 block, capable of addressing just over 1000 devices. ARS used a /24 block for its publicly accessible servers, of which 208.130.29.33 was one. All of these CIDR prefixes would be used, at different locations in the network.
Outside of MCI's network, the 208.128.0.0/11 prefix would be used to direct to MCI traffic bound not only for 208.130.29.33, but also for any of the roughly two million IP addresses with the same initial 11 bits. Within MCI's network, 208.130.28.0/22 would
become visible, directing traffic to the leased line serving ARS. Only within the ARS corporate network would the 208.130.29.0/24 prefix have been used.

CIDR - Special-use addressesReserved address blocks



[b][b]2) IPv4 addresses - Addresses ending in[/b]0 or 255[/b]
Networks with subnet masks of at least 24 bits, i.e. Class C networks in classful networking, and networks with CIDR suffixes /24 to /32 (255.255.255.0–255.255.255.255) may not have an
address ending in 0 or 255.

Classful addressing prescribed only three possible subnet masks: Class A, 255.0.0.0 or /8; Class B, 255.255.0.0 or /16; and Class C, 255.255.255.0 or /24. For example, in the subnet 192.168.5.0/255.255.255.0
(192.168.5.0/24) the identifier 192.168.5.0 commonly is used to refer to the entire subnet. To avoid ambiguity
in representation, the address ending in the octet 0 is reserved.
A broadcast address is an address that allows information to be sent
to all interfaces in a given subnet, rather than a specific machine. Generally, the broadcast address is found by obtaining the bit complement of the subnet mask and performing a bitwise OR operation with the network identifier. In other words, the broadcast
address is the last address in the address range of the subnet. For example, the broadcast address for the network 192.168.5.0 is 192.168.5.255.
For networks of size /24 or larger, the broadcast address always ends in 255.

1.1.3.2 Internet Protocol(IP) - Routing

Routing is the process of selecting best paths in a network. 


2 inter-process communication(IPC) 

In computing, inter-process
communication (IPC) is a set of methods for the exchange of data among multiple threads in one or more processes.
Processes may be running on one or more computers connected by a network. 

Main IPC Methods

MethodShort DescriptionProvided by (operating systems or
other environments)
FileA record stored on disk that can be accessed by name by any processMost operating systems
SignalA system message sent from one process to another, not usually used to store information but instead give commands.Most operating systems; some systems, such as Win NT subsystem, implement signals in only the C run-time library and provide no support for their use as an IPC method[citation
needed]. But other subsystems like the POSIX subsystem provided by default until windows 2000. Then available with interix
in XP/2003 then with « windows services for UNIX » (SFU).
SocketA data stream sent over a network interface, either to a different process on the same computer or to another computerMost operating systems
Message queueAn anonymous data stream similar to the pipe, but stores and retrieves information in packets.Most operating systems
PipeA two-way data stream interfaced throughstandard input and output and
is read character by character.
All POSIX systems, Windows
Named pipeA pipe implemented through a file on the file system instead of standard input and output.All POSIX systems, Windows
SemaphoreA simple structure that synchronizes threads or processes acting on shared resources.All POSIX systems, Windows
Shared memoryMultiple processes given access to the samememory, allowing all to
change it and read changes made by other processes.
All POSIX systems, Windows
Message passing(shared nothing)Similar to the message queue.Used in MPI paradigm, Java
RMI, CORBA, DDS, MSMQ, MailSlots, QNX,
others
Memory-mapped fileA file mapped to RAM and can be modified by changing memory addresses
directly instead of outputting to a stream, shares same benefits as a standard file.
All POSIX systems, Windows
2.1 Network socket - Berkeley

Berkeley sockets (or BSD sockets)
is a computing library with an application programming interface (API)
for internet sockets and Unix domain sockets,
used for inter-process communication (IPC).

[b][b]2.1.1 Socket API functions[/b][/b]

1.socket() creates a new socket 
int socket(int domain, int type, int protocol);
1)domain, which specifies the protocol family of the created socket. For example:AF_INET for network protocol IPv4 or
AF_INET6 for IPv6.
AF_UNIX for local socket (using a file).
2)type, one of:SOCK_STREAM (reliable stream-oriented service or Stream Sockets)
SOCK_DGRAM (datagram service or Datagram Sockets)
SOCK_SEQPACKET (reliable sequenced packet service), or
SOCK_RAW (raw protocols atop the network layer).
3)protocol specifying(Internet protocol suite - Transport layer) the actual transport protocol to use. IPPROTO_TCP,
IPPROTO_SCTP, 
IPPROTO_UDP, 
IPPROTO_DCCP. 
The function returns -1 if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.
2.bind() is typically used on the server side.
int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
sockfd, a descriptor representing the socket to perform the bind on.
my_addr, a pointer to a sockaddr structure representing the address to bind to.
addrlen, a socklen_t field specifying the size of the sockaddr structure.
Bind() returns 0 on success and -1 if an error occurs.
3.listen() is used on the server side.
int listen(int sockfd, int backlog);
this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAM, SOCK_SEQPACKET). listen() requires two arguments:sockfd, a valid socket descriptor.
backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value.
Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned.
4.accept() is used on the server side.
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

When an application is listening for stream-oriented connections from other hosts, it is notified of such events (cf. select() function) and must initialize the connection using the accept() function. The accept() function creates a new socket for each connection and removes the connection from the listen queue. It takes the following arguments:
sockfd, the descriptor of the listening socket that has the connection queued.
cliaddr, a pointer to a sockaddr structure to receive the client's address information.
addrlen, a pointer to a socklen_t location that specifies the size of the client address structure passed to accept(). When accept() returns, this location indicates how many bytes of the structure were actually used.
The accept() function returns the new socket descriptor for the accepted connection, or -1 if an error occurs. All further communication with the remote host now occurs via this new socket.
Datagram sockets do not require processing by accept() since the receiver may immediately respond to the request using the listening socket.

5.connect()  is used on the client side.
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
The connect() system call connects a socket, identified by its file descriptor, to a remote host specified by that host's address in the argument list.Certain types of sockets are connectionless, most commonly user datagram protocol sockets. For these sockets, connect takes on a special meaning: the default target for sending and receiving data gets set to the given address, allowing the use of functions such as send() and recv() on connectionless sockets.connect() returns an integer representing the error code: 0 represents success, while -1 represents an error.
6.send() and recv(), or write() and read(), or sendto() and recvfrom(), are used for sending and receiving data to/from a remote socket.

7.close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.
8.gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only.
9.select() is used to pend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors.
10.poll() is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read from or if an error occurred.
11.getsockopt() is used to retrieve the current value of a particular socket option for the specified socket.
12.setsockopt() is used to set a particular socket option for the specified socket.

[b]2.1.1.1 Socket API - TCP Socket flow diagram[/b]

[b]


[/b]

[b]


[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket tcp ipv4 udp 网络