22-netprog2
22-netprog2
Instructors:
Randal
E.
Bryant
and
David
R.
O’Hallaron
2.
Start
client
Client
1.
Start
server
Server
Sockets
getaddrinfo getaddrinfo Interface
socket socket
open_listenfd
open_clientfd bind
listen
Connec?on
request
connect accept
3.
Exchange
Client
/
rio_writen rio_readlineb data
Server
Session
Await
connec?on
rio_readlineb rio_writen request
from
next
client
EOF
close rio_readlineb
5.
Drop
client
4.
Disconnect
client
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
2
Carnegie Mellon
sa_family
Family Specific
struct sockaddr_in {
uint16_t sin_family; /* Protocol family (always AF_INET) */
uint16_t sin_port; /* Port num in network byte order */
struct in_addr sin_addr; /* IP addr in network byte order */
unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */
};
sin_port sin_addr
AF_INET 0 0 0 0 0 0 0 0
sa_family
sin_family
Family
Specific
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
5
Carnegie Mellon
¢ Example:
int clientfd = Socket(AF_INET, SOCK_STREAM, 0);
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
7
Carnegie Mellon
¢ The
process
can
read
bytes
that
arrive
on
the
connec?on
whose
endpoint
is
addr by
reading
from
descriptor
sockfd.
¢ Similarly,
writes
to
sockfd
are
transferred
along
connec?on
whose
endpoint
is
addr.
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
9
Carnegie Mellon
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
11
Carnegie Mellon
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
13
Carnegie Mellon
accept
Illustrated
listenfd(3)
1.
Server
blocks
in
accept,
Client
Server
wai8ng
for
connec8on
request
clientfd on
listening
descriptor
listenfd
Connec?on
listenfd(3)
request
2.
Client
makes
connec8on
request
by
Client
Server
calling
and
blocking
in
connect
clientfd
listenfd(3)
3.
Server
returns
connfd
from
Client
Server
accept.
Client
returns
from
connect.
clientfd connfd(4)
Connec8on
is
now
established
between
clientfd
and
connfd
¢ Connected
descriptor
§ End
point
of
the
connec;on
between
client
and
server
§ A
new
descriptor
is
created
each
;me
the
server
accepts
a
connec;on
request
from
a
client
§ Exists
only
as
long
as
it
takes
to
service
client
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
17
Carnegie Mellon
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
18
Carnegie Mellon
/* Clean up */
Freeaddrinfo(listp);
if (!p) /* All connects failed */
return -1;
else /* The last connect succeeded */
return clientfd;
} csapp.c
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
20
Carnegie Mellon
listen
Connec?on
request
connect accept
close
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
21
Carnegie Mellon
csapp.c
host = argv[1];
port = argv[2];
listenfd = Open_listenfd(argv[1]);
while (1) {
clientlen = sizeof(struct sockaddr_storage); /* Important! */
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *) &clientaddr, clientlen,
client_hostname, MAXLINE, client_port, MAXLINE, 0);
printf("Connected to (%s, %s)\n", client_hostname, client_port);
echo(connfd);
Close(connfd);
}
exit(0);
} echoserveri.c
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
26
Carnegie Mellon
Rio_readinitb(&rio, connfd);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
printf("server received %d bytes\n", (int)n);
Rio_writen(connfd, buf, n);
}
} echo.c
¢ Usage:
§ linux> telnet <host> <portnumber>
§ Creates
a
connec;on
with
a
server
running
on
<host>
and
listening
on
port
<portnumber>
http://www.w3.org/Protocols/rfc2616/rfc2616.html
Web
Content
¢ Web
servers
return
content
to
clients
§ content:
a
sequence
of
bytes
with
an
associated
MIME
(Mul;purpose
Internet
Mail
Extensions)
type
You
can
find
the
complete
list
of
MIME
types
at:
http://www.iana.org/assignments/media-types/media-types.xhtml
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
31
Carnegie Mellon
¢ BoYom
line:
Web
content
is
associated
with
a
file
that
is
managed
by
the
server
HTTP
Requests
¢ HTTP
request
is
a
request
line,
followed
by
zero
or
more
request
headers
HTTP
Responses
¢ HTTP
response
is
a
response
line
followed
by
zero
or
more
response
headers,
possibly
followed
by
content,
with
blank
line
(“\r\n”)
separa?ng
headers
from
content.
¢ Response
line:
<version> <status code> <status msg>
§ <version>
is
HTTP
version
of
the
response
§ <status
code>
is
numeric
status
§ <status
msg>
is
corresponding
English
text
§ 200
OK
Request
was
handled
without
error
§ 301
Moved
Provide
alternate
URL
§ 404
Not
found
Server
couldn’t
find
the
file
¢ Response
headers:
<header name>: <header data>
§ Provide
addi;onal
informa;on
about
response
§ Content-Type: MIME
type
of
content
in
response
body
§ Content-Length: Length
of
content
in
response
body
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
35
Carnegie Mellon
¢ HTTP
standard
requires
that
each
text
line
end
with
“\r\n”
¢ Blank
line
(“\r\n”)
terminates
request
and
response
headers
36
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
Carnegie Mellon
Tiny
Opera?on
¢ Accept
connec?on
from
client
¢ Read
request
from
client
(via
connected
socket)
¢ Split
into
<method>
<uri>
<version>
§ If
method
not
GET,
then
return
error
¢ If
URI
contains
“cgi-bin”
then
serve
dynamic
content
§ (Would
do
wrong
thing
if
had
file
“abcgi-bingo.html”)
§ Fork
process
to
execute
program
¢ Otherwise
serve
sta?c
content
§ Copy
file
to
output
env.pl
CGI
Output page
¢ Can
be
encoded
directly
in
a
URL
typed
to
a
browser
or
a
URL
in
an
HTML
link
§ http://add.com/cgi-bin/adder?15213&18213
§ adder
is
the
CGI
program
on
the
server
that
will
do
the
addi;on.
§ argument
list
starts
with
“?”
§ arguments
separated
by
“&”
§ spaces
represented
by
“+” or “%20”
if (Fork() == 0) { /* Child */
/* Real server would set all CGI vars here */
setenv("QUERY_STRING", cgiargs, 1);
Dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */
Execve(filename, emptylist, environ); /* Run CGI program */
}
Wait(NULL); /* Parent waits for and reaps child */
} ?ny.c
50
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
Carnegie Mellon
exit(0); adder.c
Addi?onal slides
Web
History
¢ 1989:
§ Tim
Berners-‐Lee
(CERN)
writes
internal
proposal
to
develop
a
distributed
hypertext
system
§ Connects
“a
web
of
notes
with
links”
§ Intended
to
help
CERN
physicists
in
large
projects
share
and
manage
informa;on
¢ 1990:
§ Tim
BL
writes
a
graphical
browser
for
Next
machines
HTTP
Versions
¢ Major
differences
between
HTTP/1.1
and
HTTP/1.0
§ HTTP/1.0
uses
a
new
connec;on
for
each
transac;on
§ HTTP/1.1
also
supports
persistent
connec*ons
mul;ple
transac;ons
over
the
same
connec;on
§
§ Connection: Keep-Alive
§ HTTP/1.1
requires
HOST
header
§ Host: www.cmu.edu
§ Makes
it
possible
to
host
mul;ple
websites
at
single
Internet
host
§ HTTP/1.1
supports
chunked
encoding
§ Transfer-Encoding: chunked
§ HTTP/1.1
adds
addi;onal
support
for
caching
<body>
<h1>Some Tests</h1>
. . .
</body>
</html>
. . .
</body>
</html>
\r\n
0\r\n
\r\n
Second Chunk: 0 bytes (indicates last chunk)
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
61
Carnegie Mellon
Proxies
¢ A
proxy
is
an
intermediary
between
a
client
and
an
origin
server
§ To
the
client,
the
proxy
acts
like
a
server
§ To
the
server,
the
proxy
acts
like
a
client
Why
Proxies?
¢ Can
perform
useful
func?ons
as
requests
and
responses
pass
by
§ Examples:
Caching,
logging,
anonymiza;on,
filtering,
transcoding