Networking, Part 1: Introduction - tcloaa/SystemProgramming GitHub Wiki

What is "IP4" "IP6"?

  • IP: Internet Protocal that tells machine how to send packet

IP4

  • IPv4, version of 4 of the Internet Protocol that describes how to send packets of information across a network from one machine to another.
  • Roughly 95% of all packets on the Internet today are IPv4 packets.
  • Limitation: source and destination addresses are limited to 32 bits (IPv4 was designed at a time when the idea of 4 billion devices connected to the same network was unthinkable - or at least not worth making the packet size larger)
  • Each IPv4 packet includes a very small header - typically 20 bytes (more precisely, "octets"), that includes a source and destination address.
  • Conceptually, the source and destination addresses can be split into two: a network number (the upper bits) and the lower bits represent a particular host number on that network.

IP6

  • solves many of the limitations of IPv4 (e.g. makes routing tables simpler and 128 bit addresses)
  • however less than 5% of web traffic is IPv6 based.

A machine can have an IPv6 address and an IPv4 address.

127.0.0.1, localhost, never leave the machine

A special IPv4 address is 127.0.0.1 also known as localhost. Packets sent to 127.0.0.1 will never leave the machine; the address is specified to be the same machine.

Notice that the 32 bits address is split into 4 octets i.e. each number in the dot notation can be 0-255 inclusive.

However IPv4 addresses can also be written as exact 1 integer.

0:0:0:0:0:0:0:1, localhost for IP6

The 128bit localhost address in IPv6 is 0:0:0:0:0:0:0:1 which can be written in its shortened form, ::1

Port

To send a packet to a host on the Internet using IPv4 (or IPv6), you need to specify the host address and a port. The port is an unsigned 16 bit number (i.e. the maximum port number is 65535).

A process can listen for incoming packets on a particular port. However only processes with super-user (root) access can listen on ports < 1024. Any process can listen on ports 1024 or higher.

Q: 为什么一台Host要分那么多port?

A: 回想MP, receive_port(用来收请求发数据), wearable_port (用来收数据)

An often used port is port 80: Port 80 is used for unencrypted http requests (i.e. web pages). For example, if a web browser connects to http://www.bbc.com/, then it will be connecting to port 80.

UDP?

What is it?

UDP is a connectionless protocol that is built on top of IPv4 and IPv6. It's very simple to use: Decide the destination address and port and send your data packet!

Limitation

  • The network makes no guarantee about whether the packets will arrive.
  • Packets may be dropped if the network is congested. Packets may be duplicated or arrive out of order.

Between two distant data-centers it's typical to see 3% packet loss.

Usage

When receiving up to date data is more important than receiving all of the data.

For example:

  1. a game may send continuous updates of player positions.
  2. A streaming video signal may send picture updates using UDP.

TCP

What

  • Like UDP, a protocol built on top of IPv4 and IPv6 (and therefore can be described as "TCP/IP" or "TCP over IP").
  • TCP creates a pipe between two machines and abstracts away the low level packet-nature of the Internet: Thus, under most conditions, bytes sent from one machine will eventually arrive at the other end without duplication or data loss.
  • Most services on the Internet today (e.g. a web service) use TCP because it hides the complexity of lower, packet-level nature of the Internet.

Why better than UDP?

TCP will automatically manage resending packets, ignoring duplicate packets, re-arranging out-of-order packets and changing the rate at which packets are sent.

3 handshakes

SYN, SYN-ACK, and ACK.

  1. Host A sends a TCP SYNchronize packet to Host B

Host B receives A's SYN

  1. Host B sends a SYNchronize-ACKnowledgement

Host A receives B's SYN-ACK

  1. Host A sends ACKnowledge

Host B receives ACK.

  1. TCP socket connection is ESTABLISHED.

TCP 3-Way Handshake Diagram

TCP header

  1. ACK bit: initiate sequence numbers
  2. SYN bit: acknowledge sequence numbers
  3. FIN bit: close connection
  4. source port

不是的: IP source address is part of the IP header, not TCP header

#Lab Chatroom question.txt 1. * What is a socket? * A socket is the endpoint of networking communication.

    • What is the difference between the domains AF_INET and AF_UNIX?
    • AF_INET connect to a remote host.
    • AF_UNIX is used to communicate between processes on the same machine efficiently.
    • The difference is that an INET socket is bound to an IP address-port tuple, while a UNIX socket is "bound" to a special file on your filesystem.
  1. What is the difference between SOCK_STREAM and SOCK_DGRAM?

    • SOCK_STREAM is used for TCP server
    • SOCK_DGRAM is used for UDP server.
  1. Why is it important to clear all the values the addrinfo struct before using it in getaddrinfo? We need to initialize the unused struct entires.

  2. What are ai_family and ai_socktype? ai_family specifies the desired address family for the returned addresses. ai_socktype specifies the preferred socket type, such as SOCK_STREAM and SOCK_DGRAM.

  3. What does getaddrinfo do? The function getaddrinfo can convert a human readable domain name into an IPv4 and IPv6 address.

  4. What does connect do? The connect call shall attempt to make a connection on a socket.

  5. What is setsockopt? The setsockopt function set the option specified by the option_name, at the protocol level specified by the level, to the value pointed to by the option_value for the socket associated with the file descriptor specified by the socket argument.

  6. What does bind do? The bind call associates an abstract socket with an actual network interface and port.

  7. What does listen do? The listen call specifies the queue size for the number of incoming, unhandled connections that have not yet been assigned a network descriptor by accpet.

  8. What does accept do? Once the server socket has been initialized the server calls accept to wait for new connections.