Title: CS 241 Section Week
1CS 241 Section Week 12(04/23/09)
2Outline
- LMP2 Overview
- Brief intro to Networking
- What is a protocol?
- OSI Model
- TCP/IP Model
- UDP and TCP
- Socket Programming Library Functions
- Hypertext Transfer Protocol
3LMP2 Overview
4LMP2 Overview
- LMP2 encodes or decodes a number of files
- It has the following parameters
- the filenames
- the number of bytes (rw_units) for each
read/write from the file - But, process files in round robin fashion
- encrypt rw_unit of file1
- encrypt rw_unit of file2
- encrypt rw_unit of file3
- encrypt rw_unit of file1
-
5LMP2 Overview
- For the output in 2nd part,
- print in get_frame() will suffice
- you may print in my_munmap.
- Most csil machines, use signed char, whose range
is from -128 to 127. So use unsigned char if you
compare with 255.
6LMP2 Overview
- For N files, -The Table of File-Mappings has
exactly N entries -The Virtual Page Table has
enough entries to fit each file's length divided
by 4096B -The Physical Memory Frame Table has 16
entries - Example 3 files of length 3072bytes, 2048bytes,
and 8192bytes -The Table of File-Mappings has 3
entries-The Virtual Page Table has - (3072/4096gt1) (2048/4096gt1) (8192/4096gt2)
4 entries -The Physical Memory Frame Table has
16 entries and four of them are filled
7Brief intro to Networking
8Networking
- Allows computers and other networked devices to
talk to each other - How can we tell what the packet we just received
means? - Interactions between applications on different
machines are governed by protocols - Protocols dictate the format and sequence of the
information exchange
9Names and Addresses
- A network address identifies a specific computer
on the network - Several kinds of addresses exist MAC address
(for LANs), IP address (for the Internet), etc. - Domain or DNS names are used for convenience, so
we dont have to remember numerical addresses
10Ports
- Ports are numbers that represent an end-point for
communication - Ports are logical, not physical, entities
- All packets arrive to the same physical
interface, and are then differentiated based on
the port number (and other contents of the packet
header) - Usually, distinct ports are used for
communication via different protocols - E.g., port 80 is for HTTP, 22 is for SSH, etc.
- See /etc/services for a list
11What is a protocol?
12What is a protocol?
- It is a formal description of message formats and
the rules that two computers must follow in order
to exchange messages. - This set of rules describes how data is
transmitted over a network.
13Why are protocols needed?
- Protocols are needed for communication between
any two devices. - In what format will the messages be transmitted?
- At what speed should messages be transmitted?
- What to do if errors take place?
- What to do if parts of a message are lost?
14Network Model
- What is a model? A hypothetical description of
a complex entity or process. - Network model - A method of describing and
analyzing data communications networks by
breaking the entire set of communications process
into a number of layers - Each layer has a specific function
15Open Systems Interconnect (OSI) Model
- Who made
- International Standards Organization (ISO)
- A Model of How Protocols and Networking
Components Could be Made - Open means the concepts are non-proprietary
can be used by anyone. - OSI is not a protocol. It is a model for
understanding and designing a network
architecture that is flexible and robust.
16Network Architecture
- A set of layers and protocols is called a network
architecture - It refers to the physical and logical design of a
network
177-layer OSI model
- Why so many layers?
- To reduce complexity, networks are organized as a
stack of layers, one below the other - Each layer performs a specific task. It provides
services to an adjacent layer - This is similar to the concept of a function in
programming languages function do a specific
task
18The Layers of the OSI Model
19The Layers of the OSI ModelSome Mnemonics
All People Seem To Need Data Processing
Please Do Not Tell Secret Passwords Anytime
20Physical layer
- Specifications for the physical components of the
network - Bit representation encode bits into electrical
or optical signals - Transmission rate the number of bits sent each
second
21Data Link Layer
- Responsible for delivery of data between two
systems on the same network. Main functions are - Framing divides the stream of bits received from
network layer into manageable data units called
frames. - Physical Addressing add a header to the frame to
define the physical address of the source and the
destination machines.
22Network Layer
- Main functions of this layer are
- Responsible for delivery of packets across
multiple networks. - Routing Provide mechanisms to transmit data over
independent networks that are linked together.
23Transport Layer
- Main functions of this layer are
- Responsible for source-to-destination delivery of
the entire message. - Segmentation and reassembly divide message into
smaller segments, number them and transmit.
Reassemble these messages at the receiving end.
24Session Layer
- Main functions of this layer are
- Dialog control allows two systems to enter into
a dialog, keep a track of whose turn it is to
transmit.
25Presentation Layer
- Responsibilities of this layer are
- Translation since different computers use
different encoding systems (bit order translation
26Application Layer
- Contains protocols that allow the users to
access the network (FTP, HTTP, SMTP, etc) - DOES NOT include application programs such as
email, browsers, word processing applications,
etc.
27Summary of Functions of Layers
28TCP/IP Model
- Layers in the TCP/IP model
- Physical Layer
- Data Link Layer (MAC)
- Network Layer (IPv4, IPv6)
- Transport Layer (UDP, TCP)
- Application Layer (HTTP, SSH)
29Networking
- Each layer is (traditionally) self-contained.
- The data-link layer doesnt care if its running
IPv4 or IPv6, doesnt care it its TCP or UDP. - When processing the data-link layer, no other
layer is (traditionally) considered.
30Networking
31Networking
- Data Link Layer
- Purpose To transfer packets in a flat addressing
space. - Multiple packets go into a hub.
- The hub bundles packets together to send them
upstream if necessary, based only on the MAC
address. - Not significantly covered in this class.
32Networking
- Network Layer
- Purpose To transfer packets in a hierarchical
addressing space. - Streams of packets go into switches.
- Switches determine if the address is within its
subnet and will route based on subnets
(IPv4/IPv6). - Also, not significantly covered in this class.
33Networking
- Transport Layer
- After the data link and network layer, packets
have arrived at the destination computer. - The transport layer
- Provides port multiplexing, allowing for
multiple programs all communicate without
receiving every packet. - Protocol-specific assurances.
34UDP
- User Datagram Protocol (UDP)
- The simplest commonly used transport protocol.
- Provides
- Port multiplexing
- Error-checking (via simple checksum)
35UDP
- User Datagram Protocol (UDP)
- Does NOT provide
- Ordering
- Packet A may be sent before Packet B, but Packet
B may arrive after Packet A - Reliability
- The sender has no way to know that Destination
received some packet sent by Sender (assured by
retransmissions) - Flow / Congestion Control
- Theres no mechanism to tweak packet size
36UDP
- User Datagram Protocol (UDP)
- Only a few parameters are needed for UDP
- Source Port
- Destination Port
- From the network layer (IP), you have two more
parameters - Source IP
- Destination IP
37UDP
Source Port
Destination Port
Data Length
Checksum
Data
38TCP
- Transmission Control Protocol
- TCP provides an alternative to UDP to allow for
more assurances - Provides
- Port multiplexing
- Ordered data / no duplicates
- Error-free transmission
- Flow control
- Congestion control
39TCP
Source Port
Destination Port
SEQ Number
ACK Number
Offset / Flags
Window Size
Checksum
Urgent Pointer
Optional Fields
Data
40Socket Programming
41Socket
- Standard APIs for sending and receiving data
across computer networks - Introduced by BSD operating systems in 1983
- POSIX incorporated 4.3BSD sockets and XTI in 2001
- include ltsys/socket.hgt
42Typical TCP Server-Client
43Typical UDP Server-Client
44Programming Sockets
- Lucky for us, the OS does most the work between
UDP and TCP! - TCP Socket
- socket(PF_INET, SOCK_STREAM,
IPPROTO_TCP) - UDP Socket
- socket(PF_INET, SOCK_DGRAM, 0)
45Programming Sockets
- To create a socket in C, you need to run two
commands - socket()
46socket
- int socket(int domain, int type, int protocol)
- Creates a communication endpoint
- Parameters
- domain AF_INET (IPv4)
- type SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)
- protocol 0 (socket chooses the correct protocol
based on type) - Returns a nonnegative integer corresponding to a
socket file descriptor if successful, -1 with
errno set if unsuccessful
47Programming Sockets
- To create a socket in C, you need to run two
commands - socket()
- bind()
48bind
- int bind(int socket, const struct sockaddr
address, socklen_t address_len) - Associates the socket with a port on your local
machine - struct sockaddr_in used for struct sockaddr
- sa_family_t sin_family / AF_INET /
- in_port_t sinport / port number /
- struct in_addr sin_addr / IP address /
- Returns 0 if successful, -1 with errno set if
unsuccessful
49Programming Sockets
- Since UDP is packet-based and TCP is
connection-based, you need to establish a
connection in TCP - Server listen(), accept()
50listen
- int listen(int socket, int backlog)
- Puts the socket into the passive state to accept
incoming requests - Internally, it causes the network infrastructure
to allocate queues to hold pending requests - backlog number of connections allowed on the
incoming queue - bind should be called beforehand
- Returns 0 if successful, -1 with errno set if
unsuccessful
51accept
- int accept(int socket, struct sockaddr restrict
address, socklen_t restrict address_len) - Accepts the pending requests in the incoming
queue - address is used to return the information about
the client making the connection. - sin_addr.s_addr holds the Internet address
- listen should be called beforehand
- Returns nonnegative file descriptor corresponding
to the accepted socket if successful, -1 with
errno set if unsuccessful
52Programming Sockets
- Since UDP is packet-based and TCP is
connection-based, you need to establish a
connection in TCP - Server listen(), accept()
- Client connect()
53connect
- int connect(int socket, const struct sockaddr
address, socklen_t address_len) - Establishes a link to the well-known port of the
remote server - Initiates the TCP 3-way handshake
- Cannot be restarted even if interrupted
- Returns 0 if successful, -1 with errno set if
unsuccessful
54Programming Sockets
- In both TCP and UDP, you send and receive by
using the same calls - recv() / recvfrom()
55recv and recvfrom
- int recv(int socket, void buf, int len, int
flags) - int recvfrom(int socket, void buf, int len, int
flags, const struct sockaddr from, socklet_t
fromlen) - receives data into the buffer buf
- recvfrom is used for unconnected datagram
sockets. If used in connection-mode, last two
parameters are ignored. - Returns the number of bytes actually read if
successful, -1 with errno set if unsuccessful
56Programming Sockets
- In both TCP and UDP, you send and receive by
using the same calls - recv() / recvfrom()
- send() / sendto()
57send and sendto
- int send(int socket, const void msg, int len,
int flags) - int sendto(int socket, const void msg, int len,
int flags, const struct sockaddr to, socklet_t
tolen) - sends data pointed by msg
- sendto is used for unconnected datagram sockets.
If used in connection-mode, last two parameters
are ignored. - Returns the number of bytes actually sent out if
successful, -1 with errno set if unsuccessful
58close and shutdown
- int close(int socket)
- int shutdown(int socket, int how)
- close
- Prevents any more reads and writes
- same function covered in file systems
- shutdown
- provides a little more control
- how
- 0 Further receives are disallowed
- 1 Further sends are disallowed
- 2 same as close
- Returns 0 if successful, -1 with errno set if
unsuccessful
59Using Sockets in C
- include ltsys/socket.hgt
- include ltsys/types.hgt
- include ltnetinet/in.hgt
- include ltunistd.hgt
- On csil-core
- gcc o test test.c
- On some systems, e.g., Solaris
- gcc o test test.c lsocket -lnsl
60TCP Client/Server Example
- Run the provided test-server and test-client
executables in two separate windows. - test-client sends the string Hello World! to IP
address 127.0.0.1 port 10000 - test-server listens on port 10000 and prints out
any text received - Next week, we will reproduce this behavior with
codes client.c and server.c
61HyperText Transfer Protocol
62HTTP
- Hypertext Transfer Protocol
- Delivers virtually all files and resources on the
World Wide Web - Uses Client-Server Model
- HTTP transaction
- HTTP client opens a connection and sends a
request to HTTP server - HTTP server returns a response message
63HTTP (continued)
- Request
- GET /path/to/file/index.html HTTP/1.0
- Other methods (POST, HEAD) possible for request
- Response
- HTTP/1.0 200 OK
- Common Status Codes
- 200 OK
- 404 Not Found
- 500 Server Error
64Sample HTTP exchange
- Scenario
- Client wants to retrieve the file at the
following URL (http//www.somehost.com/path/file.h
tml) - What a client does
- Client opens a socket to the host
www.somehost.com, port 80 - Client sends the following message through the
socket - GET /path/file.html HTTP/1.0
- From someuser_at_uiuc.edu
- User-Agent HTTPTool/1.0
- blank line here
65Sample HTTP exchange
- What a server does
- Server responds through the same socket
- HTTP/1.0 200 OK
- Date Mon, 17 Apr 2006 235959 GMT
- Content-Type text/html
- Content-Length 1354
- lthtmlgt
- ltbodygt
- (more file contents)
- .
- .
- .
- lt/bodygt
- lt/htmlgt
66Reference
- Beej's Guide to Network Programming
- http//beej.us/guide/bgnet/
67Summary
- Protocols
- Socket Programming
- Library Functions
- Hypertext Transfer Protocol
- Request
- Response