tags : Networking, Operating Systems, Subnetting, TCP&UDP
Building blocks
File Descriptors
- See File Descriptors
- A file descriptor is simply an integer associated with an open file. But (and here’s the catch), that file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else.
Socket
Means different things
- Kernel: Endpoint of communication
- Application
- socket() return the
socket descriptor
and we can use send() and recv() socket calls to communicate through it. - You can even use read() and write() but send() and recv() gives more control.
- Client and Server communicate via reading/writing to
socket descriptors
- socket() return the
- O and Socket I/O differs in how we
open
the socket descriptor.
OSI model
- The actual network hardware and topology is transparent to the socket programmer
Network algorithms
Ports
Meta
- There is no such thing as a port being open or closed. Ports aren’t real.
- They are just a two byte label in a network packet that tell both the router and client what to do with it.
Open ports
- In both TCP and UDP, source and destination ports are of 16bit (2byte)
- Max number of open ports in an IP is 216 = 65536 => 65535
- If a computer gets another IP assigned to it, in that case it gets another 65535 ports.
- So the no. of open ports a computer can have =
no. of ip * 65535
Listening ports
- If you connect to some port which is listening, you don’t consume the remote port. (In case of Web Server there’s additional things that happen)
- It remains open for others and also for you.
- Each connection you make, makes a
4-tuple
combination and when you make a connection tosever_ip:80
- connection1:
your_local_ip:random_port1
<=>server_ip:80
- connection2:
your_local_ip:random_port2
<=>server_ip:80
- See this in action w
ss -tpn
- connection1:
TCP and UDP
To learn TCP, read the RFCs to understand the design, then dive into the Linux kernel’s TCP code. Build your own TCP stream replay tool (around 1,000 lines of code), and within a year, you’ll be ahead of 99.99% of people in this field.
wangbin579
See TCP&UDP
Different types of sockets
Internet sockets (DARPA sockets, INET Sockets)
Stream Socket (SOCK_STREAM
)
- Reliable two way connected communication
- Order is maintained
- Error free
- Uses TCP and IP
send()
syscall- Examples: telnet, http
Datagram Socket (SOCK_DGRAM
)
- Sometimes called connectionless sockets, but you can use connect() with it.
- Error free
- Uses UDP and IP
sendto()
just encaptulate the data with a method of choice- Examples: tftp, dhcpcd, games, streaming audio, video conf etc.
Raw socket
Other sockets
Unix sockets
- Unix sockets are faster than TCP sockets over loopback
- path names on a local node (Unix sockets)
- Unix domain sockets use the file system as their address name space.
- Processes reference Unix domain sockets as file system inodes
- In addition to sending data, processes may send file descriptors across a Unix domain socket connection using the sendmsg() and recvmsg() system calls.
- This allows the sending processes to grant the receiving process access to a file descriptor for which the receiving process otherwise does not have access to.
X.25 sockets
Netlink socket
- Successor to
ioctl
- Netlink is designed and used for transferring miscellaneous networking information between the kernel space and userspace processes.
Network API
Endian ordering
- When about to transmit data, assume that data is in wrong host byte order; that way each time you transmit put the data in a conversion function that arranges data in big endian order.
- 32 bit variant examples: htons(), htonl(), ntohs(), ntohl()
IP Address related functions
inet_pton()
: converts an IP address in numbers and dots tosin_addr
inet_ntop()
: converts asin_addr
to printable IP address in numbers and dots- Printable to Network (pton)
- Network to Printable (ntop)
getaddrinfo
prefered overgethostbyname()
??????getnameinfo
prefered overgethostbyaddr()
??????
TODO Data types used by the socket interface
- a socket descriptor
int
- addrinfo struct : Prepare the socket address structure.
getaddrinfo()
returns pointer to a LL(Link local??) of this structureai_addr
field is a pointer to structsockaddr
sockaddr
: Dealing withsockaddr
is done with structsockaddr_in
orsockaddr_in6
for ipv6 -> they can be cast vice versa- The
connect()
system by default takes insockaddr
sockaddr_in
is therefore used as an replacement forsockaddr
sockaddr_in
is padded with 0s insin_zero[8]
field using thememset()
function- if using
sockaddr_in6
there is nosin_zero
field
- if using
sockaddr_in
’ssin_port
should be in network byte order.sockaddr_in
hassin_addr
struct which hass_addr
field which is auint32_t
- The
tun/tab/linux
- https://www.kernel.org/doc/Documentation/networking/tuntap.txt
- TUN/TAP - Wikipedia
- Tun/Tap interface tutorial
Use-cases
Bidirectional socket
- What’s the read logic when I call recvfrom()
- Maintaining a bidirectional UDP connection
- The SO_REUSEPORT socket option {LWN.net} (Diff applications, listen on the same socket)