Unix Network Programming.pdf

35
CS 241 Copyright ©: University of Illinois CS 241 Staff 1 Introduction to Unix Network Programming Reference: Stevens Unix Network Programming

description

Network programming in unix.

Transcript of Unix Network Programming.pdf

Page 1: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 1

Introduction to Unix Network Programming

Reference: Stevens Unix Network Programming

Page 2: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 2

Network Programming

  Key Components:   Internet protocols

  IP, TCP, UDP, etc   Sockets

  API - application programming interface

  Why focus on the Internet?   Internet Protocol (IP)

  IP is standard   allows a common namespace across most of Internet   reduces number of translations, which incur overhead

  Sockets   reasonably simple and elegant, Unix interface

Page 3: Unix Network Programming.pdf

Network Programming with Sockets

  Socket   Host-local, application-created, OS-controlled

Application process can both send and receive messages to/from another application process

  Sockets API   A transport layer service interface

  Introduced in 1981 by BSD 4.1   Implemented as library and/or system calls   Similar interfaces to TCP and UDP   Also interface to IP (for super-user); “raw sockets”

CS 241 Copyright ©: University of Illinois CS 241 Staff 3

Page 4: Unix Network Programming.pdf

Beej’s Guide

  How-to guide on network programming using Internet sockets, or "sockets programming" http://beej.us/guide/bgnet/

CS 241 Copyright ©: University of Illinois CS 241 Staff 4

Page 5: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 5

Outline

  Client-Sever Model   TCP Connection   UDP Services   Sockets API   Example

Page 6: Unix Network Programming.pdf

Client-Server Model

  Asymmetric Communication   Client sends requests   Server sends replies

  Server/Daemon   Well-known name   Waits for contact   Processes requests, sends

replies

  Client   Initiates contact   Waits for response

CS 241 Copyright ©: University of Illinois CS 241 Staff 6

Client

Server

Client

Client

Client

Page 7: Unix Network Programming.pdf

TCP Connections

  Transmission Control Protocol (TCP) Service   OSI Transport Layer

CS 241 Copyright ©: University of Illinois CS 241 Staff 7

process

TCP with buffers,

variables

socket

controlled by application developer

controlled by operating

system

host or server

process

TCP with buffers,

variables

socket

controlled by application developer

controlled by operating system

host or server

internet

Page 8: Unix Network Programming.pdf

TCP Connections

  Transmission Control Protocol (TCP) Service   OSI Transport Layer   Service Model

  Byte stream (interpreted by application)   16-bit port space allows multiple connections on a

single host   Connection-oriented

  Set up connection before communicating   Tear down connection when done

CS 241 Copyright ©: University of Illinois CS 241 Staff 8

Page 9: Unix Network Programming.pdf

TCP Service

  Reliable Data Transfer   Guaranteed delivery   Exactly once if no

catastrophic failures   Sequenced Data Transfer

  In-order delivery   Regulated Data Flow

  Monitors network and adjusts transmission appropriately

  Data Transmission   Full-Duplex byte stream

CS 241 Copyright ©: University of Illinois CS 241 Staff 9

  Telephone Call   Guaranteed delivery   In-order delivery   Connection-oriented   Setup connection followed

by conversation

Page 10: Unix Network Programming.pdf

UDP Services

  User Datagram Protocol Service   OSI Transport Layer   Provides a thin layer over IP   16-bit port space (distinct from TCP

ports) allows multiple recipients on a single host

CS 241 Copyright ©: University of Illinois CS 241 Staff 10

Page 11: Unix Network Programming.pdf

UDP Services

  Unit of Transfer   Datagram (variable length

packet)   Unreliable

  No guaranteed delivery   Drops packets silently

  Unordered   No guarantee of

maintained order of delivery

  Unlimited Transmission   No flow control

CS 241 Copyright ©: University of Illinois CS 241 Staff 11

  Postal Mail   Single mailbox to receive

all letters   Unreliable   Not necessarily in-order   Letters sent independently   Must address each reply

Page 12: Unix Network Programming.pdf

Choose between TCP and UDP for each of these apps

  File downloads (e.g., Web)   Sensor readings   Robot control   Nanny cam   Peer-to-peer video distribution

CS 241 Copyright ©: University of Illinois CS 241 Staff 12

Page 13: Unix Network Programming.pdf

Addresses and Data

  Goal: naming for machines on the Internet   Internet domain names

  Human readable   Variable length   Ex: sal.cs.uiuc.edu

  IP addresses   Each attachment point on Internet is given unique address   Easily handled by routers/computers   Fixed length   Somewhat geographical   Ex: 128.174.252.217

CS 241 Copyright ©: University of Illinois CS 241 Staff 13

Page 14: Unix Network Programming.pdf

Byte Ordering

  Big Endian vs. Little Endian   Little Endian (Intel, Arm):

  Least significant byte of word is stored in the lowest memory address

  Big Endian (Sun, SGI, HP):   Most significant byte of word is stored in the lowest

memory address

  Example: 128.2.194.95

CS 241 Copyright ©: University of Illinois CS 241 Staff 14

128 2 194 95

95 194 2 128

Big Endian

Little Endian

Page 15: Unix Network Programming.pdf

Byte Ordering

  Big Endian vs. Little Endian   Little Endian (Intel, Arm):

  Least significant byte of word is stored in the lowest memory address

  Big Endian (Sun, SGI, HP):   Most significant byte of word is stored in the lowest

memory address

  Network Byte Order = Big Endian   Must be used for some data (i.e. IP Addresses)

  For your app, be consistent   Key to transmitting binary data

CS 241 Copyright ©: University of Illinois CS 241 Staff 15

Page 16: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 16

Byte Ordering Functions

  16- and 32-bit conversion functions (for platform independence)

  Examples:

int m, n; short int s,t;

m = ntohl (n) // net-to-host long (32-bit) translation s = ntohs (t) // net-to-host short (16-bit) translation n = htonl (m) // host-to-net long (32-bit) translation t = htons (s) // host-to-net short (16-bit) translation

Page 17: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 17

Reserved Ports Keyword Decimal Description ------- ------- ----------- 0/tcp Reserved 0/udp Reserved tcpmux 1/tcp TCP Port Service tcpmux 1/udp TCP Port Service echo 7/tcp Echo echo 7/udp Echo systat 11/tcp Active Users systat 11/udp Active Users daytime 13/tcp Daytime (RFC 867) daytime 13/udp Daytime (RFC 867) qotd 17/tcp Quote of the Day qotd 17/udp Quote of the Day chargen 19/tcp Character Generator chargen 19/udp Character Generator ftp-data 20/tcp File Transfer Data ftp-data 20/udp File Transfer Data ftp 21/tcp File Transfer Ctl ftp 21/udp File Transfer Ctl ssh 22/tcp SSH Remote Login ssh 22/udp SSH Remote Login telnet 23/tcp Telnet telnet 23/udp Telnet smtp 25/tcp Simple Mail Transfer smtp 25/udp Simple Mail Transfer

Keyword Decimal Description ------- ------- ----------- time 37/tcp Time time 37/udp Time name 42/tcp Host Name Server name 42/udp Host Name Server nameserver 42/tcp Host Name Server nameserver 42/udp Host Name Server nicname 43/tcp Who Is nicname 43/udp Who Is domain 53/tcp Domain Name Server domain 53/udp Domain Name Server whois++ 63/tcp whois++ whois++ 63/udp whois++ gopher 70/tcp Gopher gopher 70/udp Gopher finger 79/tcp Finger finger 79/udp Finger http 80/tcp World Wide Web HTTP http 80/udp World Wide Web HTTP www 80/tcp World Wide Web HTTP www 80/udp World Wide Web HTTP www-http 80/tcp World Wide Web HTTP www-http 80/udp World Wide Web HTTP kerberos 88/tcp Kerberos kerberos 88/udp Kerberos

Page 18: Unix Network Programming.pdf

Socket interface

  A simplified API for accessing sockets   Similar to the interface from Java sockets   Function calls not found in Unix systems

  But do convey concepts

  Will go over the gory details in disc.

  Programming questions on final will be at this level of abstraction

CS 241 Copyright ©: University of Illinois CS 241 Staff 19

Page 19: Unix Network Programming.pdf

CS 241 Copyright ©: University of Illinois CS 241 Staff 20

Basic Unix Concepts

  Input/Output – I/O   Per-process table of I/O channels   Table entries describe files, sockets, devices, pipes, etc.   Unifies I/O interface   Table entry/index into table called “file descriptor”

  Error Model   Return value

  0 on success, num bytes for file descriptors   -1 on failure   NULL on failure for routines returning pointers

  errno variable

Page 20: Unix Network Programming.pdf

TCP Connection Example

CS 241 Copyright ©: University of Illinois CS 241 Staff 21

socket socket

connect bind listen

accept write read write

read close

close

client server

Page 21: Unix Network Programming.pdf

TCP Connection Example

CS 241 Copyright ©: University of Illinois CS 241 Staff 22

socket socket

connect bind listen

accept write read write

read close

close

client server

Page 22: Unix Network Programming.pdf

Socket TCP client

int NewConnection(char *inetAddr, ! int port)!

inetAddr – DNS name (“google.com”)!port – TCP port of server (e.g., 80)!

Returns a file descriptor for new socket!

CS 241 Copyright ©: University of Illinois CS 241 Staff 23

Page 23: Unix Network Programming.pdf

NewConnection behind the scenes

  Create a new socket file descriptor   Resolve hostname into IP addr   Connect to server

CS 241 Copyright ©: University of Illinois CS 241 Staff 24

Page 24: Unix Network Programming.pdf

Read/write

int read(int sock, uchar *buf, uint len) int write(int sock, uchar *buf, uint len)

buf – where data is stored len – max bytes to read/write returns num bytes read/write, 0 when

socket closed, -1 on error

CS 241 Copyright ©: University of Illinois CS 241 Staff 25

Page 25: Unix Network Programming.pdf

Read/write

  You should assume that read/write will process less data than you give it for sockets   Can play a little fast and loose with files,

not with sockets

CS 241 Copyright ©: University of Illinois CS 241 Staff 26

Page 26: Unix Network Programming.pdf

Closing connection

close(int sock)

sends any buffered bytes

disables further reads/writes on socket

notifies remote host

CS 241 Copyright ©: University of Illinois CS 241 Staff 27

Page 27: Unix Network Programming.pdf

Socket TCP server

  int NewServerSocket(int port)   Port – local port to use for server   Returns socket file descriptor

CS 241 Copyright ©: University of Illinois CS 241 Staff 28

Page 28: Unix Network Programming.pdf

NewServerSocket

  Create a new socket fd   “Bind” to a local port   Setup a listen queue

  Way of queuing up new client connections

CS 241 Copyright ©: University of Illinois CS 241 Staff 29

Page 29: Unix Network Programming.pdf

Accept

  int accept(int serverSockFd)   Accept new connections on server sock   Returns a new sock fd when client makes

new connection

  That new sock fd is different from serverSockFd and used for client comm.

CS 241 Copyright ©: University of Illinois CS 241 Staff 30

Page 30: Unix Network Programming.pdf

TCP Connection Example

CS 241 Copyright ©: University of Illinois CS 241 Staff 31

socket socket

connect bind listen

accept write read write

read close

close

client server

Page 31: Unix Network Programming.pdf

Client

#define BUF_SIZE 4096!char msg[] = “hello”;!char buffer[BUF_SIZE];!int ret, bytesWritten, bytesRead;!int len = strlen(msg) + 1;!int sock;!int bufSize = BUF_SIZE;!

sock = NewConnection(“localhost”, 8080);!assert(sock >= 0);!

CS 241 Copyright ©: University of Illinois CS 241 Staff 32

Page 32: Unix Network Programming.pdf

Client

while(bytesWritten < len) {! ret = write(sock, msg + bytesWritten, ! len – bytesWritten);! assert(ret > 0);! bytesWritten += ret;!}!

while((ret = read(sock, buffer+bytesRead, ! bufSize – bytesRead)) > 0) {! bytesRead += ret;!}!assert(ret == 0);!

CS 241 Copyright ©: University of Illinois CS 241 Staff 33

Page 33: Unix Network Programming.pdf

Server

#define BUF_SIZE 4096!char msg[] = “world”;!char buffer[BUF_SIZE];!int ret, bytesWritten, bytesRead;!int len = strlen(msg) + 1;!int servSock, cliSock;!int bufSize = BUF_SIZE;!

servSock = NewServerSocket(8080);!assert(servSock >= 0);!

cliSock = accept(servSock);!

CS 241 Copyright ©: University of Illinois CS 241 Staff 34

Page 34: Unix Network Programming.pdf

Server

while((ret = read(cliSock, buffer+bytesRead, ! bufSize – bytesRead)) > 0) {! bytesRead += ret;!

! if(buffer[bytesRead-1] == ‘\0’) break;!}!assert(ret > 0);!

while(bytesWritten < len) {! ret = write(cliSock, msg + bytesWritten, ! len – bytesWritten);! assert(ret > 0);! bytesWritten += ret;!}!

CS 241 Copyright ©: University of Illinois CS 241 Staff 35

Page 35: Unix Network Programming.pdf

UDP Connection Example

CS 241 Copyright ©: University of Illinois CS 241 Staff 36

client server

socket socket

sendto bind

recvfrom

sendto

recvfrom close