Transcript ppt

CS 241 Section Week #12
(04/23/09)
Outline
•
•
•
•
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
LMP2 Overview
LMP2 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
encrypt
encrypt
encrypt
………
rw_unit
rw_unit
rw_unit
rw_unit
of
of
of
of
file1
file2
file3
file1
LMP2 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.
LMP2 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/4096=>1) + (2048/4096=>1) + (8192/4096=>2) = 4 entries
-The Physical Memory Frame Table has 16 entries
and four of them are filled
Brief intro to Networking
Networking
• 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
Names 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 don’t have to remember numerical addresses
Ports
• 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
What is a protocol?
What 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.
12
Why 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?
13
Network 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
14
Open 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 nonproprietary; 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.
15
7-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
17
The Layers of the OSI Model
Application
Presentation
Session
Transport
Network
Data Link
Physical
18
The Layers of the OSI Model
Some Mnemonics
All
People
Seem
To
Need
Data
Processing
Application
Presentation
Session
Transport
Network
Data Link
Physical
Please
Do
Not
Tell
Secret
Passwords
Anytime
19
Physical 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
Application
Presentation
Session
Transport
Network
Data Link
Physical
20
Data 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.
Application
Presentation
Session
Transport
Network
Data Link
Physical
21
Network 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.
Application
Presentation
Session
Transport
Network
Data Link
Physical
22
Transport 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.
Application
Presentation
Session
Transport
Network
Data Link
Physical
23
Session 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.
From Presentation
Layer
Session
Layer
H5
synsyn syn
To Transport
Layer
To Presentation
Layer
Session
Layer
H5
synsyn syn
From Transport
Layer
Application
Presentation
Session
Transport
Network
Data Link
Physical
24
Presentation Layer
Responsibilities of this layer are:
• Translation since different computers
use different encoding systems (bit
order translation
Application
Presentation
Session
Transport
Network
Data Link
Physical
25
Application 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.
Application
Presentation
Session
Transport
Network
Data Link
Physical
To Presentation Layer
From Presentation Layer
26
Summary of Functions of Layers
Application
To translate, encrypt
and compress data
To provide reliable endto-end message
delivery
Presentation
Session
To establish, manage &
terminate sessions
Transport
Network
To organise bits into
frames
To allow access to
network resources
To move packets from
source to destination
Data Link
Physical
To transmit bits over a
medium & provide
electrical specs.
27
TCP/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)
Networking
• Each layer is (traditionally) self-contained.
– The data-link layer doesn’t care if it’s running IPv4
or IPv6, doesn’t care it it’s TCP or UDP.
– When processing the data-link layer, no other
layer is (traditionally) considered.
Networking
Networking
• 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.
Networking
• Network Layer:
– Purpose: To transfer packets in a hierarchical
addressing space.
• Streams of packets go into switches.
• Switches determine if the address is within it’s
“subnet” and will route based on “subnets”
(IPv4/IPv6).
– Also, not significantly covered in this class.
Networking
• 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.
UDP
• User Datagram Protocol (UDP)
– The simplest commonly used transport protocol.
– Provides:
• Port multiplexing
• Error-checking (via simple checksum)
UDP
• 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
– There’s no mechanism to “tweak” packet size
UDP
• 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
UDP
• UDP Packet:
Source Port
Destination Port
Data Length
Checksum
Data
TCP
• 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
TCP
• TCP Packet:
Source Port
Destination Port
SEQ Number
ACK Number
Offset / Flags
Window Size
Checksum
Urgent Pointer
Optional Fields
Data
Socket Programming
Socket
• 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 <sys/socket.h>
Typical TCP Server-Client
Typical UDP Server-Client
Programming 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);
Programming Sockets
• To create a socket in C, you need to run two
commands:
– socket()
socket
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
Programming Sockets
• To create a socket in C, you need to run two
commands:
– socket()
– bind()
bind
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
in_port_t
struct in_addr
sin_family; /* AF_INET */
sinport;
/* port number */
sin_addr;
/* IP address */
• Returns 0 if successful, -1 with errno set if unsuccessful
Programming Sockets
• Since UDP is packet-based and TCP is
connection-based, you need to establish a
connection in TCP:
– Server: listen(), accept()
listen
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
accept
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
Programming Sockets
• Since UDP is packet-based and TCP is
connection-based, you need to establish a
connection in TCP:
– Server: listen(), accept()
– Client: connect()
connect
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
Programming Sockets
• In both TCP and UDP, you send and receive by
using the same calls:
– recv() / recvfrom()
recv 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
Programming Sockets
• In both TCP and UDP, you send and receive by
using the same calls:
– recv() / recvfrom()
– send() / sendto()
send 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
close 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
Using Sockets in C
#include
#include
#include
#include
<sys/socket.h>
<sys/types.h>
<netinet/in.h>
<unistd.h>
On csil-core:
gcc –o test test.c
On some systems, e.g., Solaris:
gcc –o test test.c –lsocket -lnsl
TCP 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
HyperText Transfer Protocol
HTTP
• 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
HTTP (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
Sample HTTP exchange
• Scenario
– Client wants to retrieve the file at the following URL
(http://www.somehost.com/path/file.html)
• 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: [email protected]
User-Agent: HTTPTool/1.0
[blank line here]
Sample HTTP exchange
• What a server does
– Server responds through the same socket
HTTP/1.0 200 OK
Date: Mon, 17 Apr 2006 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354
<html>
<body>
(more file contents)
.
.
.
</body>
</html>
Reference
• Beej's Guide to Network Programming
– http://beej.us/guide/bgnet/
Summary
• Protocols
• Socket Programming
– Library Functions
• Hypertext Transfer Protocol
– Request
– Response