CS 468 Secure Programming and Systems Fall 2021: How To Develop Network Applications?
CS 468 Secure Programming and Systems Fall 2021: How To Develop Network Applications?
1
Layers in Network Application
• Application
– Read data from and write data to the socket
– Interpret the data (e.g., render a Web page)
• Operating system
– Deliver data to the destination socket
– Based on the destination port number (e.g., 80)
• Network
– Deliver data packet to the destination host
– Based on the destination IP address
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 3
Wang
2
Network Applications and Application-
Layer Protocols
• Application: communicating,
distributed processes
application
– Running in network hosts in transport
network
“user space” data link
physical
– Exchange messages to
implement app
– e.g., email, file transfer, the Web
• Application-layer protocols
– One “piece” of an app
– Define messages exchanged by
application
apps and actions taken application transport
transport network
– Use services provided by lower network
data link
data link
physical
layer protocols physical
IP Network
X Y
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 6
Wang
3
Client-Server Paradigm
• Client program • Server program
– Running on end host – Running on end host
– Requests service – Provides service
(speaks first) – E.g., Web server
– E.g., Web browser
GET /index.html
Client-Server Communication
• Client “sometimes on” • Server is “always on”
– Starts later than server – Starts first, passively
– Initiates a request to the waiting for requests.
server when interested – Handles services requests
– Doesn’t communicate from many client hosts
directly with other clients – Doesn’t initiate contact
– Needs to know server’s with the clients
address – Needs fixed, known
address
request
response
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 8
Wang
4
Characteristics of a Client
• Arbitrary application program
• Becomes client temporarily
• Can also perform other computations
• Invoked directly by user
• Runs locally on user’s computer
• Actively initiates contact with a server
• A client can be a server at the same time!
Characteristics of a Server
• Special-purpose, privileged program
• Dedicated to providing one service
• Can handle multiple remote clients simultaneously
• Invoked automatically when system boots
• Executes forever
• Needs powerful computer and operating system
• Waits passively for client contact
• Accepts requests from arbitrary clients
• A server can be a client of another server at the
same time!
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 10
Wang
5
Layers of the IP Protocol Suite
e.g. ftp Application Layer
Application Layer
e.g. IP
Network Layer Network Layer
Link Layer
Ethernet Link Layer
Application Layer
Applications
(e.g. browser, game, ftp)
Application Programming
Interface (API)
Transport Layer (TCP, UDP) (e.g. network API)
Operating System
(e.g. Unix)
Network Layer (IP)
6
Network Application Programming
Interface (API)
• The interface between application and protocol
software (often by the operating system).
Application
Network API
Protocol A Protocol B Protocol C
7
API for Programming TCP/IP Applications
• TCP/IP does not include an API definition.
• There are a variety of APIs for use with
TCP/IP:
– Sockets
– TLI, XTI
– Winsock
– MacTCP
8
Berkeley Socket
• A socket is an abstract representation of a
communication endpoint.
– support for multiple protocol families.
– address representation independence
• Sockets work with Unix I/O services just like
files, pipes & FIFOs.
• Uses existing I/O programming interface as
much as possible.
• Sockets (obviously) have special needs:
– establishing a connection
– specifying communication endpoint addresses
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 17
Wang
Berkeley Socket
• Originally developed as part of BSD Unix
– BSD = Berkeley Software Distribution
– API=Application Program Interface
• Now the most popular API for C/C++
programmers writing applications over
TCP/IP
– Also supported in other languages
• Perl, Tcl/Tk, etc.
– Supported on other operating systems
• Mac
• Windows
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 18
Wang
9
Berkeley Socket Interface Internet
10
Types of Internet Sockets
• Different types of sockets implement different communication
types (stream vs. datagram)
• Type of socket: stream socket
– connection-oriented
– two way communication
– reliable (error free), in order delivery
– can use the Transmission Control Protocol (TCP)
– e.g. telnet, ssh, http
• Type of socket: datagram socket
– connectionless, does not maintain an open connection, each packet is
independent
– can use the User Datagram Protocol (UDP)
– e.g. IP telephony
• Other types exist: similar to the one above
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 21
Wang
11
Transmission Control Protocol (TCP):
Stream Socket
TCP Telephone
Postal Mail
Call
• Reliable – guarantee delivery •• Single
Guaranteed delivery
mailbox to receive
messages
• Byte stream – in-order delivery •• Unreliable
In-order delivery
☺
• Connection-oriented – single
•• Not necessarily in-order
Connection-oriented
socket per connection delivery
• Each letter is independent
• Setup connection followed by • Must address each
Setup connection reply
followed by
data transfer conversation
12
Socket Identification
• Communication Protocol
– TCP (Stream Socket): streaming, reliable
– UDP (Datagram Socket): packets, best effort
• Receiving host
– Destination address that uniquely identifies the host
– An IP address is a 32-bit quantity
• Receiving socket
– Host may be running many different processes
– Destination port that uniquely identifies the socket
– A port number is a 16-bit quantity
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 25
Wang
Process Process
A B
port X port Y Port Number
TCP/UDP Protocol
Host Address
IP
Ethernet Adapter
13
Knowing What Port Number To Use
• Popular applications have well-known ports
– E.g., port 80 for Web and port 25 for e-mail
– See http://www.iana.org/assignments/port-numbers
• Well-known vs. ephemeral ports
– Server has a well-known port (e.g., port 80)
• Between 0 and 1023 (requires root to use)
– Client picks an unused ephemeral (i.e., temporary) port
• Between 1024 and 65535
• Uniquely identifying traffic between the hosts
– Two IP addresses and two port numbers
– Underlying transport protocol (e.g., TCP or UDP)
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 27
Wang
14
Typical Client-Server Interaction
response
Client Server
request
socket()
bind()
socket() listen()
Connection
connect() establishment accept()
send()
Data request recv()
#include <netinet/in.h>
15
Byte Ordering
union {
u_int32_t addr; /* 4 bytes address */
char c[4];
} un;
/* 128.2.194.95 */
un.addr = 0x8002c25f;
/* c[0] = ? */
c[0] c[1] c[2] c[3]
#include <netinet/in.h>
16
Creating a Socket
int socket(int family,int type,int proto);
TCP Server
IP
Ethernet Adapter
17
Socket I/O: socket()
• Since web traffic uses TCP, the web server must create a
socket of type SOCK_STREAM
18
Socket I/O: listen()
• listen indicates that the server will accept a connection
if(listen(fd, 5) < 0) {
perror(“listen”);
exit(1);
}
19
Socket I/O: accept() continued...
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
20
TCP Client
• For example: web client
• How does a web client 2 Web Clients
TCP
IP
Ethernet Adapter
srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
if(srv.sin_addr.s_addr == (in_addr_t) -1) {
fprintf(stderr, "inet_addr failed!\n"); exit(1);
}
21
Translating Names to Addresses
• Gethostbyname provides interface to DNS
• Additional useful calls
– Gethostbyaddr – returns hostent given sockaddr_in
– Getservbyname
• Used to get service description (typically port number)
• Returns servent based on name
#include <netdb.h>
peeraddr.sin_family = AF_INET;
hp = gethostbyname(name)
peeraddr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
22
Socket I/O: write()
• write can be used with a socket
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
char buf[512]; /* used by write() */
int nbytes; /* used by write() */
bind()
close()
By Dr. Xinyuan (Frank)
CS at George Mason University CS 468 Fall 2021 46
Wang
23
UDP Server Example
IP
Ethernet Adapter
24
Socket I/O: bind()
• A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
25
Socket I/O: recvfrom() continued...
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) cli, &cli_len);
2 UDP Clients
IP
Ethernet Adapter
26
Socket I/O: sendto()
• write is not allowed
• Notice that the UDP client does not bind a port number
– a port number is dynamically assigned when the first sendto is called
UDP Server
socket()
bind()
UDP Client
recvfrom()
socket()
blocks until datagram
sendto() received from a client
data request
close()
27
The UDP Server
UDP
IP
Ethernet Adapter
/* 1) create socket s1 */
/* 2) create socket s2 */
/* 3) bind s1 to port 2000 */
/* 4) bind s2 to port 3000 */
while(1) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
28
Server Handling Concurrently Clients
• Multiple processes
– Forking a new process for each client: fork()
– Simple, but creating new process is expensive.
• Multiple threads
– Create a new thread for each client
– Light weight and more efficient than multi-process
approach.
29
Socket I/O: select()
int select(int maxfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
struct timeval {
long tv_sec; /* seconds /
long tv_usec; /* microseconds */
}
• timeout
– if NULL, wait forever and return only when one of the descriptors is
ready for I/O
– otherwise, wait up to a fixed amount of time specified by timeout
• if we don’t want to wait at all, create a timeout structure with timer value
equal to 0
30
Socket I/O: select()
• select allows synchronous I/O multiplexing
int s1, s2; /* socket descriptors */
fd_set readfds; /* used by select() */
TCP
IP
Ethernet Adapter
31
Socket I/O: select()
int fd, next=0; /* original socket */
int newfd[10]; /* new socket descriptors */
while(1) {
fd_set readfds;
FD_ZERO(&readfds); FD_SET(fd, &readfds);
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32
A Few Programming Notes: Building a
Packet in a Buffer
struct packet {
u_int32_t type;
u_int16_t length;
u_int16_t checksum;
u_int32_t address;
};
/* ================================================== */
char buf[1024];
struct packet *pkt;
33
Socket Programming References
• Man page
– usage: man <function name>
• Internetworking with TCP/IP Volume III
• Unix Network Programming : Networking
APIs: Sockets and XTI (Volume 1)
34
HW3: Implementing a primitive
authenticated remote shell client and server
35