Reading: Kurose Chapter 2

jinkscabbageΔίκτυα και Επικοινωνίες

23 Οκτ 2013 (πριν από 3 χρόνια και 9 μήνες)

80 εμφανίσεις

Reading: Kurose Chapter 2
Electronic mail
WWW
Instant messaging
SSH
P2P file sharing
File transfer
Streaming audio/video
Internet phone
Video
-
conferencing
Application layer: where "useful work" is done.
Applications
Wednesday, September 07, 2005
4:51 PM

Application_Layer Page 1

Where we are in the ISO/OSI reference
model:
(Political)
How do we make decisions?
(Financial)
How much money do we
have?
Application
7.
Services with meaning to
users:
HTTP, SMTP, ...
Presentation
6.
How to portably exchange
data: MIME, XDR, SSL, ...
Session
5.
How to maintain session
state:
NetBIOS, SOCKS, ...
Transport
4.
How to send/receive data
between programs: TCP,
UDP, ...
Network
3.
How to address nodes, send
(simple) messages between
non
-
neighboring devices: IP,
ICMP, ...
Link
2.
How to communicate
between directly connected
devices: Ethernet, ATM,
Frame Relay, Wireless, ...
Physical
1.
The actual communication
Where we are in the ISO/OSI model
Friday, January 20, 2012
11:37 AM

Application_Layer Page 2

Physical
1.
The actual communication
media: wires, wireless
frequencies, ...

Application_Layer Page 3

"Don't pay any attention to that man behind the curtain."
Describe the internet from the top down in the ISO/OSI
reference model.
Assume that the layers below "just work".
The game of this part of the course:
The game of this part of the course
Friday, January 20, 2012
11:35 AM

Application_Layer Page 4

Client:
a node that wishes to make a request.
Server:
a node that handles requests.
Most internet applications have a client/server
architecture.
Socket:
a communication input/output connection
between processes in the internet domain.
Address:
a notion of the location of a host on the
internet.
Port:
a number identifying a socket or its associated
service.
Protocol:
the pattern of communication necessary to
obtain service over a socket.
Client
-
server concepts:
Client/Server architecture
Wednesday, September 07, 2005
4:56 PM

Application_Layer Page 5

Every host has an address (IP number),
every service has a port number,
and the pair (address,port) serves to
define a service.
In the IP domain,
One host may implement both a client and a
server side, by fulfilling the requirements for
each.
Client/Server Caveats
Monday, January 23, 2012
1:45 PM

Application_Layer Page 6

On top of TCP (connection
-
based, reliable)
HTTP request: what client sends to server.
HTTP response: what gets sent back from
server.
One request per connection.
Example of application service:
Hypertext Transfer Protocol (HTTP)
Example: HTTP
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 7

Time/space diagram
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 8

HTTP request:
GET /somedir/page.html HTTP/1.1
Host:
www.someschool.edu
Connection: close
User
-
agent: Mozilla/4.0
Accept
-
language: fr
Payload starts here …
HTTP response:
HTTP/1.1 200 OK
Connection: close
Date: Thu, 03 Jul 2003 12:00:15 GMT
Server: Apache/1.3.0 (Unix)
Last
-
modified: Sun, 5 May 2003 09:23:24 GMT
Content
-
length: 6821
Content
-
type: text/html
Payload starts here …
A sample HTTP exchange
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 9

Request is
\
n
-
delimited key/value pairs
and a payload section that is free
-
form.
Response has a header of
\
n
-
delimited
pairs, and a payload section that is free
-
form.
Request/Response protocol.
Payload can be binary data (MIME)
Features of HTTP
Features of HTTP
Friday, January 20, 2012
5:07 PM

Application_Layer Page 10

Transparency:
simple, easily understood transactions.
Exception handling:
exceptions are reported.
Recovery:
application recovers from bad requests and
errors on part of client or server.
Salient features of a good application protocol
What is a good application protocol?
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 11

telnet
www.eecs.tufts.edu
80
Escape character is ^]
GET /g/112 HTTP/1.1
Host:
www.eecs.tufts.edu
(hit return)
… response ensues …
You can test HTTP via telnet
Response is independent of user agent
(browser
or other depiction)
Telnet is a perfectly valid user agent (though its
depiction leaves something to be desired)
So one can use telnet to talk to a web server or
--
for that matter
--
any text
-
based TCP protocol.
Why does this work?
How to test HTTP
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 12

Web caching/proxying
Conditional GET.
Some finer points of HTTP
Some finer points of HTTP
Friday, January 20, 2012
12:11 PM

Application_Layer Page 13

Web caching (proxy servers)
Direct all requests to a proxy server.
Proxy server asks for content from real
server (and also stores a copy)
Further requests for same data do not have
to fetch the page from the source.
Caching and proxies
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 14

Conditional GET
GET /something.html HTTP/1.1
If
-
modified
-
since: <date>
If not modified, returns
HTTP/1.1 304 Not Modified
Date: <modification date>
Server: <server name>
{empty body}
Do a conditional get relative to
cache copy date.
If newer, cache result.
If older, return cache to client.
Purpose of conditional get: caching
Wednesday, September 07, 2005
5:03 PM

Application_Layer Page 15

Faithfully interpret the HTTP pattern.
Using a lower
-
level pattern (TCP) as transport.
Implementation is a
combination of two patterns.
Implementing HTTP
Implementing HTTP
Friday, January 20, 2012
12:13 PM

Application_Layer Page 16

Presentation: how data is interpreted.
Session: how state is managed.
Transport: how data is moved.
The layers below the application layer:
Presentation layer: ASCII text headers for
requests and responses, arbitrary payloads
using MIME types.
Session: N/A.
Transport: TCP
For HTTP,
To implement HTTP, we need the TCP
pattern.
Down the ladder
Friday, January 20, 2012
5:10 PM

Application_Layer Page 17

A network protocol is a
pattern of
interaction
.
That corresponds to a
design pattern
for
programming.
These design patterns are fairly
rigid:
one
cannot vary from them without violating
the protocol.
Network programming is not an exercise in
creativity!
Pattern
-
based programming
Pattern
-
based programming
Friday, January 20, 2012
5:12 PM

Application_Layer Page 18

When you deploy a network sevice,
any
process on the internet can connect to it!
You must be ready to
respond to any input
whatever,
whether malicious or simply
clueless.
Thus you must
validate every input
and be
ready to flag an exception for any
variation.
Just because you aren't paranoid doesn't
mean the rest of the internet isn't out to get
you.
The network programmer's mindset
Friday, January 20, 2012
5:19 PM

Application_Layer Page 19

The
client pattern
: how a client requests
services.
The
server pattern
: how a server
responds to requests.
Basics of TCP programming:
Basics of TCP programming
Friday, January 20, 2012
5:21 PM

Application_Layer Page 20

Create
socket.
Connect
socket to remote port.
Read/write (or fprintf/fscanf)
.
Close
socket.
Client pattern:
Create
socket.
Bind
socket to a port.
Listen
on the socket for connections.
Accept
a particular connection to the socket.
Read/write (or fprintf/fscanf)
.
Close
socket.
Repeat (forever):
Server pattern:
Client
-
server TCP code goes through several phases and
states
A socket is a special kind of I/O device.
A socket connection is a quadruple
(client address,client port,server address, server port).
The socket in the client is "connected" to the socket in
the server.
Connections are bi
-
directional: one can write and read
from them.
Anything written to the socket by one side can be read
by the other side.
Some TCP pattern concepts
Client/Server patterns
Monday, September 12, 2005
5:02 PM

Application_Layer Page 21

Humans read ASCII, but the system calls require machine
-
readable binary.
There is a lot of format conversion between ASCII and binary
(network representation).
Some TCP pattern caveats
Some TCP pattern caveats
Friday, January 20, 2012
5:26 PM

Application_Layer Page 22

Sample client code simplified from
/g/112/examples/tcp/client1.c
/* simple tcp client based upon Stevens examples
Source: Stevens, "Unix Network Programming" */
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include "shared.h"
int main(int argc, char *argv[])
/* ip address of server in ASCII */
char *serv_ip = "130.64.23.40";
/* port number as an integer */
int serv_port = 9040;
/* address of remote socket in internet format */
struct sockaddr_in serv_addr;
/* socket file descriptor */
int sockfd;
/* message to send */
char *request = "hello world";
{
Monday, September 12, 2005
5:02 PM

Application_Layer Page 23

/* response from server */
char response[MAX_MESG];
/* length of response */
int resplen;
/* construct a server address */
/* zero the address to turn off all flags */
/* corrected to conform to current standards */
memset((char *) &serv_addr, 0, sizeof(serv_addr));
/* this is an internet address */
serv_addr.sin_family =
PF_INET
;
/* convert IP address from ASCII to binary */
inet_pton(PF_INET,serv_ip,(void *)

&serv_addr.sin_addr.s_addr)
;
/* convert port to internal format */
serv_addr.sin_port =
htons(serv_port)
;
/* open a TCP socket */
perror("can't open stream socket");
exit(1);
if((sockfd =
socket(PF_INET, SOCK_STREAM, 0)
) < 0) {
}
/* postcondition: sockfd is a valid socket */
/* connect to the server */
if (
connect(sockfd, (struct sockaddr *)
perror("can't connect to server");
exit(1);

&serv_addr,sizeof(serv_addr))
< 0) {
}
/* postcondition: sockfd is open and connected */
/* write a message to the server */
printf("client sends: %s
\
n",request);

Application_Layer Page 24

printf("client sends: %s
\
n",request);
write(sockfd, request, strlen(request));
resplen=
read(sockfd, response, MAX_MESG);
response[resplen]='
\
0'; /* make character array a
string */
printf("client receives: %s
\
n",response);
close(sockfd);
}

Application_Layer Page 25

A socket is a network
-
based I/O device.
Its data format is determined by the
application.
The simplest socket client sends and
receives plaintext.
It can also send binary data, but this takes
extra care and mechanism.
Client caveats:
Client caveats
Friday, January 20, 2012
5:00 PM

Application_Layer Page 26

FILE *s = fdopen(sockfd, "w");
fprintf(s,"yo!
\
n");

fclose(s); // calls close(sockfd)
Make the socket a formatted I/O device (so
you can use fprintf to print to it):
Client tricks:
Client tricks
Friday, January 20, 2012
5:05 PM

Application_Layer Page 27

Client pattern
Client pattern
Monday, September 12, 2005
5:02 PM

Application_Layer Page 28

Socket states

Application_Layer Page 29

Example server code
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "shared.h"
int main(int argc, char *argv[])
{
int sockfd, newsockfd;
struct sockaddr_in serv_addr;
int port = 9040;
/* server */
char message[MAX_MESG];
int mesglen; int i;
/* message */
struct sockaddr_in cli_addr;
int cli_len;
/* client */
/* open a TCP socket (an Internet stream socket) */
perror("can't open stream socket");
exit(1);
if((sockfd =
socket(AF_INET, SOCK_STREAM, 0)
) < 0) {
}
/* postconditions: socket is valid and open */
/* bind the local address,

so that the client can send to server */
/* correct to conform to current standards */
memset((char *) &serv_addr, 0, sizeof(serv_addr));
Example server code
Monday, September 12, 2005
5:02 PM

Application_Layer Page 30

memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family =
PF_INET
;
serv_addr.sin_addr.s_addr =
htonl(INADDR_ANY)
;
serv_addr.sin_port =
htons(port)
;
if(
bind(sockfd, (struct sockaddr *) &serv_addr,
perror("can't bind local address");
exit(1);

sizeof(serv_addr))
< 0) {
}
/* postconditions: socket is bound */
/* listen to the socket: queue 5 connections */
listen(sockfd, 5);
/* wait for a connection from a client */
cli_len = sizeof(cli_addr);
&cli_addr, &cli_len);
newsockfd =
accept(sockfd, (struct sockaddr *)
for(;;) {
perror("can't bind local address");
if(newsockfd < 0) {
/* read a message from the client */
mesglen =
read(newsockfd, message, MAX_MESG);
/* terminate text strings */
message[mesglen] = '
\
0';
/* convert message to upper case */
message[i]=toupper(message[i]);
for (i=0; i<mesglen; i++)
/* and send it back to the client */
write(newsockfd,message,mesglen);
close(newsockfd);
} else {
}
}
}

Application_Layer Page 31

Most services run forever, until killed.
sockfd
, one which one listens for
connections.
newsockfd
, which is connected to
clients.
To shut down a service, must close both
of them.
There are two sockets:
SIGTERM: shut down
SIGHUP: re
-
read configuration)
(Process signals with meanings to services:
Server pattern caveats:
Server pattern caveats
Friday, January 20, 2012
5:36 PM

Application_Layer Page 32

The Server State Machine
Monday, September 12, 2005
5:02 PM

Application_Layer Page 33

"130.64.23.40"
ASCII string, e.g.,
inet_pton("130.64.23.40")
Network binary representation, e.g.,
struct sockaddr_in
Combined IP/port (service) address:
Several kinds of addresses:
Subroutines need the proper form of address
and conversion is usually necessary.
PF_INET: this is an internet address.
INADDR_ANY: I don't care: used to default
to address of current machine.
Integer flags denote meaning of addresses:
Things to notice about client
-
server code:
Monday, September 12, 2005
5:02 PM

Application_Layer Page 34

Complete TCP client/server examples:
http://www.cs.tufts.edu/comp/112/examples/Application_Layer
Some examples
Monday, September 12, 2005
5:02 PM

Application_Layer Page 35