Linux Network Management


Published on

Published in: Technology, Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Linux Network Management

  1. 1. Linux Network Management © 2010 Anil Kumar Pugalia <> All Rights Reserved.
  2. 2. What to Expect?Ws of NetworkingIntroduction to SocketsAddressing at the LayersProgramming the SocketsClient-Server Concepts © 2010 Anil Kumar Pugalia <> 2 All Rights Reserved.
  3. 3. Ws of NetworkingCommunication is the KeyUser Space & User Space Signals, IPC, Shared Address SpaceKernel Space & User Space System Calls, SignalsKernel Space & Kernel Space Kernel Communication & Synchronization MechanismsHardware Space & Kernel Space Interrupts, Device Access MechanismsWhats common in all of these? All within the same systemNetworking extends a hand outside the system © 2010 Anil Kumar Pugalia <> 3 All Rights Reserved.
  4. 4. Network Stack & Sockets7 Layers Application Program Stream Datagram Socket Socket RawApplication Interface Interface SocketPresentation Interface Session TCP UDP Transport Network IP Data Link Interface Layer (Ethernet, SLIP, loopback, etc) Physical Media © 2010 Anil Kumar Pugalia <> 4 All Rights Reserved.
  5. 5. Inter System Communication System I System II User Space User Space DProcess ... Process Process ... Process Socket Interface Socket Interface Kernel Space Kernel Space Hardware Space Hardware Space Network Link © 2010 Anil Kumar Pugalia <> 5 All Rights Reserved.
  6. 6. ExamplesNetwork Daemons (Servers) with default socket numbers ftpd (Port 21) sshd (Port 22) telnetd (Port 23) smtp (Port 25) httpd (Port 80)Network Applications (Clients) ftp ssh telnet Mail Clients (pine, mutt, ...) Web Browsers (firefox, ...) © 2010 Anil Kumar Pugalia <> 6 All Rights Reserved.
  7. 7. System DependenceWire Transmission – Bit-wise MSB firstSystem Data – Word-wise Which end first? Depends on the ProcessorTwo prevalent Endians Little Endian (x86 systems, PPC, ...) Big Endian (Sun systems, PPC, ...) © 2010 Anil Kumar Pugalia <> 7 All Rights Reserved.
  8. 8. Little EndianMS Byte LS Byte00101000 01111100 00101110 00101010 Data Memory 00101010 A 00101010 A+1 01111100 A+2 00101000 A+3 © 2010 Anil Kumar Pugalia <> 8 All Rights Reserved.
  9. 9. Big EndianMS Byte LS Byte00101000 01111100 00101110 00101010 Data Memory 00101000 A 01111100 A+1 00101110 A+2 00101010 A+3 © 2010 Anil Kumar Pugalia <> 9 All Rights Reserved.
  10. 10. Trivial functionsuint16_t htons(uint16_t host_short);uint16_t ntohs(uint16_t network_short);uint32_t htonl(uint32_t host_long);uint32_t ntohl(uint32_t network_long);Header: <arpa/inet.h> © 2010 Anil Kumar Pugalia <> 10 All Rights Reserved.
  11. 11. Addressing at Layers Application Program User-specificApplicationPresentation Session Port Address TCP UDP (Socket Address) Transport Network IP IP Address Data Link Physical Physical Address Physical Networks (MAC Address) © 2010 Anil Kumar Pugalia <> 11 All Rights Reserved.
  12. 12. Socket AddressBasic Structure (16 bytes)struct sockaddr{ sa_family_t sa_family; // Protocol Family char sa_data[14]; // Protocol Address}typedef unsigned short sa_family_t; © 2010 Anil Kumar Pugalia <> 12 All Rights Reserved.
  13. 13. Socket Address ...With Internet Addressstruct sockaddr_in{ sa_family_t sin_family; // Protocol Family in_port_t sin_port; // Port Number / Socket Address struct in_addr sin_addr; // IP Protocol Address unsigned char sin_zero[8]; // Pad to sizeof(struct sockaddr)}typedef uint16_t in_port_t;struct in_addr { in_addr_t s_addr; }typedef uint32_t in_addr_t; © 2010 Anil Kumar Pugalia <> 13 All Rights Reserved.
  14. 14. Socket Programming UsageSocket Creation: socket()Attaching with an address: bind()Preparing for accepting connections: listen()Waiting for & Accepting connections: accept()Setting up the connection: connect()Sending data: send(), sendto(), sendmsg()Receiving data: recv(), recvfrom(), recvmsg()Cleaning up: close()Example Pairs Connection-oriented (TCP based): sock_server.c, sock_client.c Connection-less (UDP based): sock_dgram_*.c © 2010 Anil Kumar Pugalia <> 14 All Rights Reserved.
  15. 15. Creating a socketfd = socket(family, type, protocol);Family AF_UNIX / AF_LOCAL, AF_INET, AF_INET6, ...Type SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ... Can be ored with SOCK_NONBLOCK, SOCK_CLOEXECProtocol Typically one per family. So, pass zeroReturns file descriptor of the new socket on success -1 on error (and sets errno) © 2010 Anil Kumar Pugalia <> 15 All Rights Reserved.
  16. 16. Attaching an addressDone by a serverstatus = bind(fd, addresssp, addrlen);fd: File descriptor returned by socket()addressp: Pointer to address structureaddrlen: Size of address structureReturns 0 on success -1 on error (and sets errno) © 2010 Anil Kumar Pugalia <> 16 All Rights Reserved.
  17. 17. Preparing for connectionsDone by a server for transport connectionsstatus = listen(fd, qlen);fd: File descriptor returned by socket()qlen Length of the pending connection queueReturns 0 on success -1 on error (and sets errno) © 2010 Anil Kumar Pugalia <> 17 All Rights Reserved.
  18. 18. Accepting new connectionsDone by a server for connection based socketsnewfd = accept(fd, addresssp, addrlen);fd: File descriptor returned by socket()addressp (Could be NULL) Pointer to structure of address of the connected peeraddrlen: Value-result address structure sizeBlocking call (by default), waiting for new connectionsReturns File descriptor of the new accepted socket connection -1 on error (and sets errno) © 2010 Anil Kumar Pugalia <> 18 All Rights Reserved.
  19. 19. Connecting to serverDone by a client for connection based socketsstatus = connect(fd, addresssp, addrlen);fd File descriptor returned by socket() to be connectedaddressp Pointer to structure of address to connect toaddrlen: Size of address structureReturns 0 on success -1 on error (and sets errno) © 2010 Anil Kumar Pugalia <> 19 All Rights Reserved.
  20. 20. Connection Establishment socket() socket() bind() connect()Server Application Client Application listen() accept()Transport / Network Transport / Network Layer Layer Physical Layer © 2010 Anil Kumar Pugalia <> 20 All Rights Reserved.
  21. 21. Sending DataCould be done by both server and clientsent = send(fd, buf, len, flags);fd: File descriptor of the connected socketbuf: Buffer of Data to be sentlen: Length of the data to be sentflags: MSG_DONTWAIT, MSG_NOSIGNAL, ...Returns Bytes of data sent on success -1 on error (and sets errno)Other APIs: write(), sendto(), sendmsg() © 2010 Anil Kumar Pugalia <> 21 All Rights Reserved.
  22. 22. Receiving DataCould be done by both server and clientreceived = recv(fd, buf, len, flags);fd: File descriptor of the connected socketbuf: Buffer to receive Data intolen: Length of the Bufferflags: MSG_DONTWAIT, MSG_PEEK, MSG_WAITALL, ...Returns Bytes of data received on success -1 on error (and sets errno)Other APIs: read(), recvfrom(), recvmsg() © 2010 Anil Kumar Pugalia <> 22 All Rights Reserved.
  23. 23. Closing socketsTo be done by both server and client On all the not-needed socket file descriptors Unless they were opened with SOCK_CLOEXECTerminates both directions of data transfer Reading and WritingCleans up all the socket related resourcesshutdown(fd, how);fd: File descriptor of the socket to be closedhow: SHUT_RD, SHUT_WR, SHUT_RDWRReturns 0 on success -1 on error (and sets errno)Other API: close() © 2010 Anil Kumar Pugalia <> 23 All Rights Reserved.
  24. 24. Non-blocking OptionsTypical blocking system calls accept() send*(), write() recv*(), read()Achieving non-blocking behaviour Non-blocking: Socket opened with SOCK_NONBLOCK Multiplexing: Use select() or poll() or epoll() on socket fd Signal driven: Set socket to deliver SIGIO on activity Using FIOSETOWN cmd of fcntl, Or Using SIOCSPGRP cmd of ioctlExamples Select Usage: pipe_window.c → pipe_window0, pipe_window1 Server-Client Pair: sock_server_select.c, sock_client.c © 2010 Anil Kumar Pugalia <> 24 All Rights Reserved.
  25. 25. System Call selectHeader File: <sys/select.h>int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);File Descriptor Set APIs void FD_ZERO(fd_set *set); void FD_SET(int fd, fd_set *set); void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, fd_set *set); © 2010 Anil Kumar Pugalia <> 25 All Rights Reserved.
  26. 26. System Call pollHeader File: <poll.h>int poll( struct pollfd *array_fds, nfds_t nfds, struct timespec *timeout);struct pollfd int fd; short events /* requested events */ short revents /* returned events */Events: POLLIN, POLLOUT, POLLPRIAdditional returned Events: POLLERR, POLLHUP, POLLNVAL © 2010 Anil Kumar Pugalia <> 26 All Rights Reserved.
  27. 27. Socket related Informationcat /proc/sys/net/core/ rmem_default: Default receive buffer size rmem_max: Maximum receive buffer size wmem_default: Default send buffer size wmem_max: Maximum send buffer size …man 7 socket © 2010 Anil Kumar Pugalia <> 27 All Rights Reserved.
  28. 28. Client-Server ConceptsTypes of Connections Control connections Data connectionsTypes of Servers Iterative servers (Single Process) Concurrent servers (Multi-Process) © 2010 Anil Kumar Pugalia <> 28 All Rights Reserved.
  29. 29. Iterative ServersExample: sock_server_select.c Client Server Client Ephemeral Port Well-known Port © 2010 Anil Kumar Pugalia <> 29 All Rights Reserved.
  30. 30. Concurrent ServersExample: sock_server_concurrent.c Child Child Client Server Server Server Client Ephemeral Port Well-known Port © 2010 Anil Kumar Pugalia <> 30 All Rights Reserved.
  31. 31. Miscellaneous ExamplesNamed (AF_UNIX / AF_LOCAL) Sockets named_socket_server.c named_socket_client.cMulticast Operations mcast_recv.c mcast_send.c © 2010 Anil Kumar Pugalia <> 31 All Rights Reserved.
  32. 32. What all have we learnt?Ws of NetworkingIntroduction to SocketsNetworking with EndianAddressing at the LayersProgramming the SocketsClient-Server Concepts © 2010 Anil Kumar Pugalia <> 32 All Rights Reserved.
  33. 33. Any Queries?© 2010 Anil Kumar Pugalia <> 33 All Rights Reserved.