Networking
UDP vs TCP
UDP is connectionless
Good for VPN tunneling / streaming / gaming / DNS / VoIP
TCP is connection-oriented
Provides error-checking, guarantees delivery & consistency of data
Flow control: send buffer, receive buffer
Receiver will drop packet if the buffer is full
Receiver will tell sender how much room is left in the receive buffer
Good for HTTP/HTTPS/SSH/FTP/SMTP/IMTP/POP protocols
Congestion control
TCP 在网络波动下的行为是什么样的?
Socket API
Socket pair
Each socket has two important attributes: a communication domain and a type. There are two main types, stream and datagram.
Server Lifecycle
create a new socket
and bind
to a port (externally visible)
listen
on the socket -> mark it as passive for accepting incoming connection requests
accept
an incoming connection (blocking if not any), and create a new socket for it.
read
and write
to the socket (blocking -- depending on available data/space in buffer)
close
the connection after done
Client lifecycle
create a new socket
connect
to a passive socket.
read
and write
to the socket ...
Socket type for IP socket
STREAM
for TCP
DATAGRAM
for UDP
Socket type for UDS: https://stackoverflow.com/questions/13953912/difference-between-unix-domain-stream-and-datagram-sockets
TCP/IP model
App -> Transport -> Internet -> Network access
Network access: Hardware addressing and physical transmission of data (WiFi? Wired? Ethernet vs. Infiniband?)
Internet layer
IP
Address Resolution Protocol -- ARP. Find MAC etc.
ICMP (providing hosts with information about the network problems)
Transport: TCP/UDP
Application: ...
Explain session?
HTTP/2
Problem of HTTP 1.0 -- connection can't be reused, head of line blocking
Each connection requires time for slow start (throughput) and handshake (latency)
HoL blocking: earlier request blocks later request
Reuse connection
Connection: Keep-Alive
(normally within a minute for web, for mobile people typically use ad-hoc long connection)
WebSocket (long connection): support message
http long polling: establish connection before the data is prepared
HoL blocking
pipelining the requests
Only for GET/HEAD etc. ... similar to pipelining the CPU instructions
HTTP/2 solutions
Frame format is similar to TCP (binary payload)
So one connection can be used for sending multiple frames.
Avoid sending duplicates headers by caching
Server can send many things even if only HTML is requested
Explain HTTPS?
Encrypt traffic
对称加密,每个 connection 对应唯一的密钥
Check integrity
Digital signature: From CA (digest algorithm)
Check ID (authenticity) of website
非对称加密: encrypt request using pubkey From CA
pubkey
cert: CA generated cert for server from submitted & verified pubkey
Signature is part of the cert, cert includes many other info as well
cert is issued by server back to client
cert is checked by client using CA's pubkey' in order to trust the pubkey inside cert
Flow (TSL handshake)
client -> https request -> server
server turns cert
client checks cert
if good, generate symkey and send to server after encryping using pubkey in cert
server establishes symmetric encrypted connection
HTTPS is just HTTP over SSL
Networking code optimizing/tuning?
How does network look like?
How does a network packet look like?
The lower protocol header is on the outside
TCP connection stage
Three-way handshake for opening
Client -> SYN
"Hi, should I start from x?" -> Server
Server -> SYN ACK
"Hi, start from x + 1, y" -> Client
Client -> ACK
"Got it, x + 2, y + 1" -> Client
(Then client sends the actual data, while server waits for ACK
)
Why three-way handshake?
Why bump sequence number in handshake?
Four-way handshake for terminating
Client -> FIN
"Hi, I want to terminate" -> Server
Server -> ACK
"okay, I will start cleaning up for it" -> Client
Server -> FIN
"Hi, I'm finished, cya!" -> Client
Client -> ACK
"Cya!" -> Server
https://stackoverflow.com/questions/46212623/why-tcp-connect-termination-need-4-way-handshake
Because setup doesn't take time in the server end, but closing does (sending remaining data)
What is TCP ACK seq?
The sequence number is the byte number of the first byte of data in the TCP packet sent (also called a TCP segment). The acknowledgement number is the sequence number of the next byte the receiver expects to receive.
Connection reset: RST:(Reset the connection)用于复位因某种原因引起出现的错误连接,也用来拒绝非法数据和请求。如果接收到 RST 位时候,通常发生了某些错误;发送 RST 包关闭连接时,不必等缓冲区的包都发出去,直接就丢弃缓冲区中的包,发送 RST;接收端收到 RST 包后,也不必发送 ACK 包来确认。
Fast open: ??
TCP Connection states
TCP 粘包?
QUIC, BBR?
Infiniband vs Ethernet
How to maximize the socket performance?
Imagine a user sitting at an Ethernet-connected PC. He has a browser open. He types "www.google.com " in the address bar and hits enter.
Now tell me what the first packet to appear on the Ethernet is.
It depends...
asynchronous and synchronous sockets?
Explained p2p protocol?
Discovery/directory service
Connection
Configure port forwarding on public facing router
NAT traversal / "hole punching"
Nagle's algorithm and TCP optimization
https://www.extrahop.com/company/blog/2016/tcp-nodelay-nagle-quickack-best-practices/
Nagle: improving TCP efficiency by reducing the number of small packets sent over the network.
TCP delayed acknowledgment or Delayed ACK is another technique used by some implementations of the TCP in an effort to improve network performance and reduce congestion.
Nagle's algorithm and Delayed ACKs together create a problem because Delayed ACKs are waiting around to send the ACK while Nagle's is waiting around to receive the ACK!
Nagle's algorithm is undesirable in highly interactive environments.
TCP_NODELAY
socket option allows your network to bypass Nagle Delays by disabling Nagle's algorithm
To disable Delayed ACKs, use the TCP_QUICKACK
socket option.
Design a protocol for communicating between a client and a server for sending audio/video
https://juejin.im/post/6867043669682651143
Reliable UDP: QUIC, WebRTC, Aeron etc.
QUIC (Quick UDP Internet Connection):
QUIC 通过 DH 算法创建一个安全的连接后,客户端会缓存起来原始的连接信息等。在后续的过程中只要和同一个服务器建立链接都是直接发送数据,不需要再次协商秘钥,从而实现了后续的 0RTT。
QUIC focus on handshake optimization (very important!) QUIC does bring (at least) twice as fast connection establishment and reduces dramatically the impact of handover between different networks.
How does TCP windows work and what can I do to optimize performance for an application that performs lots of large reads across continents.
syn 洪泛攻击,如何应对?
DNS is using UDP than TCP
It is much faster
Fits in the UDP packet
Reliability can be added at app-layer
HTTP protocol
缓存并发问题: https://www.cnblogs.com/dinglang/p/6133501.html