All include:
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/sctp.h>
#include <net/if.h>
#include <netdb.h>
The TCP/IP protocol has four layers and the ISO protocol has seven layers;
Application layer:
Transport Layer: TCP,UDP,SCTP
Network layer: IP,ICMP,IGMP
Link Layer: Arp,rarp
Network-related commands:
Netstat
Tcpdump
Arp
Lsof
Iptables
Ifconfig
Ping
Traceroute
Route
In the SVR4 system, compile the function to add the link library socket and NSL:
-lsocket-lnsl
Compiling the SCTP program requires a link to the SCTP library:
-lsctp
Problems with small ends of big-endian
Small end: Low byte at start address, high byte at high address
Big-endian: High byte at start address, low byte at high address
Linux is generally small-end
UNIX is usually big-endian.
16-bit data, consisting of 2 bytes, one byte at high address, one byte at low address.
32-bit data, which consists of 4 bytes, and so on.
Ack:acknowledgement, the confirmation character, a transmission class control character that the receiving station sends to the transmitter.
FIN: Indicates end of send, close connection
Syn:synchronize, handshake Signal
TCP: A full-duplex, connection-oriented Transmission Control protocol that provides a reliable byte stream for a user process.
UDP: Unreliable, non-connected User Datagram protocol.
SCTP: Provides a reliable, full-duplex connection-oriented flow control transport protocol.
The following TCP and UDP sockets are the Transport layer APIs that are called by the application layer;
The application layer is in user state;
The Transport layer, network layer, and link layer are in the kernel state.
The socket function is a POSIX standard function,
SCTP is an upgraded version of TCP and UDP, using the SCTP API as much as possible
IPv6 is an upgraded version of IPv4, use the Ipv6 API as much as possible
Applications that use the TCP/IP protocol typically use two programming interfaces:
1. Socket (BSD Socket interface)
2. Tli or XTi (transmission interface, XTi is a superset of Tli), is no longer available.
###########################################################
Network programming related library functions:
###########################################################
BYTE-order Conversion library functions:
Customers and service programs need to convert the representation of an internal integer into a network byte order before it is transmitted.
UNIX's network byte order is generally big-endian, Linux network byte order is generally small end.
The host byte-order decimal integer is converted to a short integer and a long integer of the network byte order:
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
The network byte order is converted to a short integer and a long integer of the host byte-order:
uint16_t ntohs(uint16_t net16bitvalue);
uint32_t Htohl(uint32_t net32bitvalue);
16-bit processing port
32-bit processing address
----------------------
BYTE Manipulation Library functions:
Bsd:
#include <strings.h>
void bzero(void *dest, size_t nbytes);
void bcopy(const void *src, void *dest, size_t nbytes);
int bcmp(const void *PTR1, const void *PTR2, size_t nbytes);
Ansic:
#include <string.h>
void *memset(void *dest, int c, size_t len);
void *memcpy (void *dest, const void *SRC, size_t nbytes);
int memcmp(const void *PTR1, const void *PTR2, size_t ntbytes);
Bzero and memset are typically used to initialize a socket address to 0.
----------------------
Internet address Translation Library functions:
In ASCII string (dotted decimal string, people prefer the format)
and the binary of the network byte order (the value stored in the socket address)
Convert the Internet address between.
Dotted decimal (a) also called expression (p)
Network byte order binary (n) also called value (n)
Converts a dotted decimal string into a 32-bit network byte-order binary IPv4 address:
int Inet_aton(const char *strptr, struct in_addr *addrptr);
in_addr_t inet_addr(const char *strptr);//generally no longer use this function
Convert the network byte-order binary IPv4 address of 32 to a dotted decimal string:
char *inet_ntoa(struct in_addr inaddr);
Two conversion functions for both IPv4 and IPv6 ( now generally used ):
The following two functions are reentrant.
Converts a dotted decimal string strptr to a 32-bit network byte-order binary address addrptr:
int Inet_pton(int family, const char *strptr, void *addrptr);
IPv4 void * is generally struct in_addr * i.e. &sin_addr.
StrPtr are generally IP addresses.
Converts a 32-bit network byte-order binary address addrptr to a dotted decimal string strptr:
const char *inet_ntop(int family, const void *addrptr, char *strptr, Size_tlen);
Family
af_inet //ipv4
Af_inet6 //ipv6
Len:
#define Inet_addrstrlen //ipv4
#define Inet6_addrstrlen //ipv6
-----------------------------------------------------------
IPv4 Socket Address structure:
typedef uint32_t in_addr_t; 32-bit unsigned integer
typedef unsigned short int sa_family_t;
typedef uint16_t IN_PORT_T;
struct in_addr //IPV4 address structure
{
in_addr_t s_addr; 32-bit binary IPv4 address
};
IPv4:
S_ADDR = htonl (Inaddr_any); 0, this wildcard address constant is 0, the kernel chooses its own address
struct sockaddr_in //ipv4 An internetwork socket address structure, which is generally defined by this variable
{
uint8_t Sin_len; Linux does not have this parameter
sa_family_t sin_family; Af_inet
in_port_t Sin_port; Port number
struct in_addr sin_addr; -Bit IPv4Address
Char Sin_zero[8]; Unused
};
SIN_ADDR.S_ADDR = htonl (Inaddr_any);
Sin_port = htons (Serv_port);
struct sockaddr //old version Universal socket address structure, general socket function with this variable
{
uint8_t Sa_len; Linux does not have this parameter
sa_family_t sa_family;
Char sa_data[14];
};
It is used in the socket API to turn a struct sockaddr_in strong into a struct sockaddr type.
This is a protocol-independent, universal socket address structure for IPv4.
-------------------------
IPv6 Socket Address structure:
struct in6_addr //IPV6 address structure
{
uint8_t s6_addr[16]; Address of the 128-bit binary IPv6
}
IPv6:
S_ADDR = htonl (in6addr_any_init);
struct sockaddr_in6 //ipv6 internetwork Socket Address structure
{
uint8_t Sin6_len; Lenth of structure
sa_family_t sin6_family; Af_inet6
in_port_t Sin6_port; Port number
uint32_t Sin6_flowinfo;
struct in6_addr sin6_addr; -Bit ipv6address
uint32_t sin6_scope_id;
}
struct sockaddr_storage //New Universal Socket address structure
{
uint8_t Sa_len;
sa_family_t sa_family;
....
}
This is a protocol-independent, universal socket address structure for IPv6.
Sockets:sockets Network Programming Basics