Hierarchy of Network
BAN (Body Area Network): spans devices carried / worn on body (Bluetooth)
SAN (System Area Network): spans cluster or machine room
LAN (Local Area Network): spans a building or campus (Ethernet)
WAN (Wide Area Network): spans country or world (typically P2P phone lines)
inter-network (internet): set of networks
Internet
is the most famous example of an internet
Ethernet: many hosts are connected to one centralized hub by Ethernet cable
host: any device except the hub
hub: (obsolete) a device connected to all host
frames: data chunk
Bridged Ethernet:
bridge: high speed, long distant connection by wire from different hubs
span: building, campus
routing table: cache that can learn which hosts are reachable
connected LANs by router
no fixed topology:
Protocol: send bits across incompatible LANs and WANs
place: on each host and router
naming scheme: uniform format for host addresses
delivery mechanism:
Issues: (in computer networking)
What if different networks have different maximum frame sizes? (segmentation)
How do routers know where to forward frames?
How are routers informed when the network topology changes?
What if packets get lost?
IP (Internet Protocol): naming scheme, unreliable delivery capability (datagrams)
UDP (Unreliable Datagram Protocol): real time
process to process
datagram delivery
TCP (Transmission Control Protocol): crucial
process to process
using connection byte stream
IP address example:
128.2.xxx.xxx is CMU
127.0.0.1 is localhost
domain names mapped to IP
IPv4: 32-bit IPv6: 128-bit
IP Address:
stored in memory in big-endian byte order
translation: 0x8002C2F2 = 128.2.194.242
getaddrinfo()
, getnameinfo()
used to convert between IP address and dotted decimal format
DNS: distributed database that map domain names to IP address
host entry: equivalence class of domain names and IP addresses
nsloopup
: look up domain names
Connection:
P2P: connects a pair of processes
full-duplex: can flow in both directions at the same time
reliable: stream of bytes is received in the same order
Socket: an endpoint of a connection (IP:port
)
Port: 16 bit integer to identify a process
Ephemeral port: assigned by kernel to make connection request
Well-known port: service provided by server
/etc/services
service | port |
---|---|
echo | 7 |
ftp | 21 |
ssh | 22 |
smtp | 25 |
http | 80 |
minecraft | 25565 |
sockets: Set of system-level functions used in conjunction with Unix I/O to build network applications.
to kernel: an endpoint of communication
to application: a file descriptor (The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors)
getaddrinfo(char *host, char *port, struct addrinfo *hints, struct addrinfo *result)
: convert from name to address (allocate addinfo
)
Replaces obsolete gethostbyname()
and getservbyname()
Reentrant: multi-threaded safety
allows protocol-independent code (both IPv4, IPv6)
complex, a small number of usage patterns suffice in most cases
SOCK_STREAM
: open a connection
AI_ADDRCONFIG
: IPv4 or IPv6
AI_NUMERICSERV
: numeric port arg
Clients: walk this list, trying each socket address in turn, until the calls to socket
and connect
succeed.
Servers: walk the list until calls to socket
and bind
succeed.
void freeaddrinfo(struct addrinfo *result)
: free linked list
const char *gai_strerror(int errcode)
: return error message
struct addrinfo {
int ai_flags; /* Hints argument flags */
int ai_family; /* First arg to socket function */
int ai_socktype; /* Second arg to socket function */
int ai_protocol; /* Third arg to socket function */
char *ai_canonname; /* Canonical host name */
size_t ai_addrlen; /* Size of ai_addr struct */
struct sockaddr *ai_addr; /* Ptr to socket address structure */
struct addrinfo *ai_next; /* Ptr to next item in linked list */
};
Example:
int getnameinfo(const SA *sa, socklen_t salen, /* In: socket addr */
char *host, size_t hostlen, /* Out: host */
char *serv, size_t servlen, /* Out: service */
int flags); /* optional flags */
Replaces obsolete gethostbyaddr()
and getservbyport()
.
Reentrant and protocol independent.
TODO: return what?
int socket(int domain, int type, int protocol)
return file descriptor for both client and server
domain: AF_INET
for IP
type: SOCK_STREAM
for TCP
you should use getaddrinfo
to generate these inputs
int bind(int sockfd, SA *addr, socklen_t addrlen)
server-side
since one server can have multiple IP addresses
SA
: struct sockaddr
Process can read or write to addr
by read or write from descriptor sockfd
int listen(int sockfd, int backlog)
server-side
active listen as server
backlog: (a hint) number of outstanding connection requests that the kernel should queue up before starting to refuse requests (128-ish by default), if backlog, client will receive connection before accept
returns
int accept(int listenfd, SA *addr, int *addrlen)
server-side
Waits for connection request to arrive on the connection bound to listenfd
, then fills in client’s socket address in addr
and size of the socket address in addrlen
.
Returns a connected descriptor (different from listenfd
) for I/O
int connect(int clientfd, SA *addr, socklen_t addrlen)
client-side
establish a connection with server at socket address addr
If successful, then clientfd
is now ready for reading and writing
generate connection (client_add:client_ephemeral_port_, addr.sin_addr:addr.sin_port)
See: Here for code
telnet
for testing web server (with no security)
HTTP: HyperText Transfer Protocol
Client and server establish TCP connection
Client requests content
Server responds with requested content
Client and server close connection
Example MIME (Multipurpose Internet Mail Extensions) types
text/html HTML document
text/plain Unformatted text
image/gif Binary image encoded in GIF format
image/png Binary image encoded in PNG format
image/jpeg Binary image encoded in JPEG format
HTTP responses:
Static content: HTML files, images, audio clips, Javascript programs
Dynamic content: content produced by a program executed by the server on behalf of the client (games)
Universal Resource Locator (URL):
protocol: https://
server: 127.0.0.1
, localhost
port: 80
suffix: /index.html
cgi=bin
directory/
http request: request line + zero or more request header
Request line: <method> <uri> <version>
method: GET
, POST
, OPTIONS
, HEAD
, PUT
, DELETE
, TRACE
uri (uniform resource identifier): URL for proxies, URL suffix for servers
version: either HTTP/1.0
or HTTP/1.1
Request header (can be none): <header name>: <header data>
http response: request line + zero or more request header and possibly content
\r\n
separating headers from contentResponse line: <version> <status code> <status msg>
200 OK
301 Moved
404 Not found
Response header (can be none): <header name>: <header data>
Content-Type: MIME type of content in response body
Content-Length: Length of content in response body
\r\n
ending on each line
Dynamic: any request GET /cgi-bin/env.pl HTTP/1.1
Common Gateway Interface (CGI)
CGI programs: children written according to the CGI spec
Replaced by: fastCGI, Apache modules, Java servlets, Rails controllers (create process if expensive)
Example: http://add.com/cgi-bin/adder?15213&18213
adder
: the CGI program on the server that will do the addition?
: argument list start&
: arguments separator+
or %20
create child: argument passed by environment variable QUERY_STRING
. child directly send back response from stdout
more info:
THE network programming bible: W. Richard Stevens et. al. “Unix Network Programming: The Sockets Networking API”, Volume 1, Third Edition, Prentice Hall, 2003
THE Linux programming bible: Michael Kerrisk, “The Linux Programming Interface”, No Starch Press, 2010
code: csapp.{.c,h}, hostinfo.c, echoclient.c, echoserveri.c, tiny.c, adder.c
Table of Content