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
  • 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 to sever_ip:80
      • connection1: your_local_ip:random_port1
      • connection2: your_local_ip:random_port2
      • See this in action w ss -tpn

TCP and UDP

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

  • 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()
  • inet_pton() : converts an IP address in numbers and dots to sin_addr
  • inet_ntop() : converts a sin_addr to printable IP address in numbers and dots
  • Printable to Network (pton)
  • Network to Printable (ntop)
  • getaddrinfo prefered over gethostbyname() ??????
  • getnameinfo prefered over gethostbyaddr() ??????

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 structure
    • ai_addr field is a pointer to struct sockaddr
      • sockaddr : Dealing with sockaddr is done with struct sockaddr_in or sockaddr_in6 for ipv6 -> they can be cast vice versa
        • The connect() system by default takes in sockaddr
        • sockaddr_in is therefore used as an replacement for sockaddr
        • sockaddr_in is padded with 0s in sin_zero[8] field using the memset() function
          • if using sockaddr_in6 there is no sin_zero field
        • sockaddr_in’s sin_port should be in network byte order.
        • sockaddr_in has sin_addr struct which has s_addr field which is a uint32_t

tun/tab/linux

Use-cases

Bidirectional socket

Network Programming for Games