Chapter 2 - Standard Unix API

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 52

Chapter 2

Standard Unix API

By
En. Mohd Nizam bin Osman
(Senior Lecturer)
Department of Computer Science
Faculty of Computer and Mathematical Science, UiTM Perlis
Course Outline
• Socket Functions
bind
listen
accept
connect
recv/recvfrom
send/sendto
close
htons/ntohs/htonl/ntohl
ined_addr/ined_aton/inet_ntoa
Recap
(Client-Server Model)
1. Client sends request
Client/sever model
Client Server
process process Resource
2. Server Processes
4. Client request
3. Server sends response
processes
response
Client-Server transaction
Note: clients and servers are processes running on hosts (can be the same
or different hosts).

Client Server
• Running on end host • Running on end host
• Request service • Provides service
• e.g.: Web browser • e.g.: Web server
Recap
(Client-Server Model)
TCP/IP allows two application programs to pass data
Client/sever model
back and forth:
The applications can execute on the same machine
or on different machines.
How to organize the application programs?
Client-server paradigm is the most popular model
The communication applications are divided into
two categories: client and server
Client-server paradigm uses the direction of initiation
to categorize whether a program is a client or server.
Recap
(Client-Server Model)
 An application that initiates the communication is called a client.
Client/sever model
 E.g.: Web browser, FTP client, Telnet client, Email client
 The client contacts a server, sends a request, and awaits a
response
 An application that waits for incoming communication requests
from clients is called a server.
 E.g.: Web server, FTP server, Telnet server, SMTP server
 The server receives a client’s request, performs the necessary
computation, and returns the result to the client
 Remark: A server is usually designed to provide service to
multiple clients.
 How ???
Client-Server Communication
Client/sever model
Client Server
• Sometimes on • Always on
• Initiates a request to • Server services to
the server when many clients
interested • e.g.:
• e.g.: Web browser www.uitm.edu.my
• Needs to know the • Not initiate contact
server’s address with the clients
• Need a fixed address
Socket
Means for inter-process communication (IPC)

application layer application layer

Client Process Internet Server Process


Socket Socket
transport layer (TCP/UDP) transport layer (TCP/UDP)
OS network Internet OS network
network layer (IP) network layer (IP)
stack stack
link layer (e.g. ethernet) link layer (e.g. ethernet)
physical layer
Internet physical layer

The interface that the OS provides to its networking subsystem


Socket
(Internet Connection (TCP/IP))
 Address the machine on the network:
By IP address
 Address the process:
By the “port”-number
 The pair of IP-address + port – makes up a “socket-address”
Client socket address Server socket address
128.2.194.242:3479 208.216.181.15:80

Server
Client
Connection socket pair (port 80)
(128.2.194.242:3479, 208.216.181.15:80)

Client host address Server host address


128.2.194.242 208.216.181.15

Note: 3479 is an Note: 80 is a well-known port


ephemeral port allocated associated with Web servers
by the kernel
Socket
(Clients)
Examples of client programs:
Web browsers, ftp, telnet, ssh…..
How does a client find the server?
The IP address in the server socket address identifies the host.
The (well-known) port in the server socket address identifies
the service, and thus implicitly identifies the server process that
performs that service.
Examples of well known ports:
 Port 7: Echo server
 Port 23: Telnet server
 Port 25: Mail server
 Port 80: Web server
Socket
(Using Ports to Identify Service)
Server host 128.2.194.242

Client host Service request for Web server


128.2.194.242:80 (port 80)
(i.e., the Web server)
Client Kernel
Echo server
(port 7)

Service request for Web server


128.2.194.242:7 (port 80)
(i.e., the echo server)
Client Kernel
Echo server
(port 7)
Socket
(Servers)
Servers are long-running processes (daemons).
Created at boot-time (typically) by the init process
(process 1)
Run continuously until the machine is turned off.
Each server waits for requests to arrive on a well-
known port associated with a particular service.
Port 7: echo server See /etc/services for a
Port 23: telnet server comprehensive list of the
services available on a
Port 25: mail server Linux machine.
Port 80: HTTP server
Other applications should choose between 1024 and
Sockets
What is a socket?
To the kernel, a socket is an endpoint of
communication.
To an application, a socket is a file descriptor that lets
the application read/write from/to the network.
 Remember: All Unix I/O devices, including
networks, are modeled as files.

Clients and servers communicate with each by reading


from and writing to socket descriptors.

The main distinction between regular file I/O and socket


I/O is how the application “opens” the socket descriptors.
Sockets
(Types of Sockets)

Stream Socket (TCP) Datagram Socket (UDP)


• Connection-oriented • Connection-less
 Requires connection • “Best-effort” delivery
establishment & termination  Possible out-of-order
• Reliable delivery delivery
 In-order delivery  No retransmission
 Retransmission  Possible duplicates
 No duplicates • Low variance in latency
• High variance in latency • Packet-like interface
 Cost of reliable service  Requires packetizing
• File-like interface (streaming) • E.g.: DNS, VoIP, VOD, AOD
• E.g.: HTTP, SSH, FTP
Sockets
(Types of Sockets)
When sending “Hi!” and “How are you”
TCP treats them as a single bytes stream:
Bytes stream
u o y e r e w o H

UDP treats them as separate messages:

How are you Hi!


Sockets
(Types of Sockets)
Thus, TCP needs application level message boundary.
By carrying length in application-level header
E.g.:
struct my_app_hdr{
int length;
}

Bytes stream

u o y e r e w o H 11 ! i H 3
Sockets Parameters
A socket connection has five (5) general parameters:

The Local
The Local
The and Remote
and Remote
protocol Port
Address
Number
Some ports
are reserved
(e.g.: 80 for
HTTP)
Example: TCP Example:
and UDP 128.100.3.40
Root access
require to
listen on port
numbers
below 1024
Sockets Functions
Function Description
socket() Creates a new socket endpoint. This needs to know the protocol
family and the protocol type.
connect() A client uses this to build a connection with a server.
write() Send data through a socket.
read() Receive data through a socket.
close() Closes a socket endpoint
bind() Allows server to bind a local address to the socket
listen() Permits incoming connections for a server socket
accept() Accepts a new client connection for a server socket.

https://www.tutorialspoint.com/unix_sockets/index.htm (Good Web Site to learn Unix Socket)


Sockets Functions
Function Description
recv() Receives data through a socket
recvfrom() Receives data through a datagram socket
send() Sends data through a socket
sendto() Sends data through a datagram socket
htons() Convers short to network byte order
ntohs() Convers short to host byte order
htonl() Convers long to network byte order
ntohl() Convers long to host byte order

https://www.tutorialspoint.com/unix_sockets/index.htm (Good Web Site to learn Unix Socket)


Sockets Functions
Function Description
inet_addr() Converts a dotted-notation string to a 32-bit network address
inet_ntoa() Converts a 32-bit network address to a dotted-notation string
inet_aton() Converts a dotted-notation string to a 32-bit network address

https://www.tutorialspoint.com/unix_sockets/index.htm (Good Web Site to learn Unix Socket)


Typical Client Program

Prepare to communicate
• Create a socket
• Determine server address and port number
• Initiate the connection to the server

Exchange data with the server


• Write data to the socket
• Read data from the socket
• Do stuff with the data (e.g.: render a Web Page)

Close the socket


Typical Server Program
Prepare to communicate
• Create a socket
• Associate local address and port with the socket

Wait to hear from a client (passive open)


• Indicate how many clients-in waiting to permit
• Accept an incoming connection from a client

Exchange data with the client over new socket


• Receive data from the socket
• Send data to the socket
• Close the socket

Repeat with the next connection request


Client - Server
Flow chart

CREATE
BIND

SEND

RECEIVE

SEND

CLOSE
History of Socket
 InHistory
earlyof 1980s,
Sockets the Advanced Research Projects Agency
(ARPA) funded a group at UC Berkeley to transport TCP/IP
software to the UNIX operating system.
 As part of the project, the designers created an interface
that applications use for network communication.
 The interface used an abstraction known as a socket, and
the API became known as socket API.
 Many computer vendors adopted the Berkeley UNIX
operating system, and the socket interface became very
popular.
 The socket interface has become a de facto standard
throughout the computer industry.
23
How to Write Network Application?
TCP/IP is implemented inside the OS
OS exports the TCP/IP functionalities through SOCKET
APIs
API: Application Program Interface
A set of functions
Programmers develop network applications by using
SOCKET APIs
To learn SOCKET programming
Learn some data structures
Learn the socket APIs
Learn multi-threading 24
Create a Socket
Necessary “includes”
#include <netinet/in.h> • Prototype & define (htons, etc)

#include <sys/socket.h> • Prototype & define (send, connect, etc)

#include <netdb.h> • Prototype & define (gethostbyame, etc)

#include <stdlio.h> • Prototype & define (printf, etc)

#include <string.h> • Prototype & define (bzero, memset, etc)

These five files are needed by both client and server.


Create a Socket
socket()
Client Server
/*Declarations*/ /*Declarations*/
int sd; int request_sd;
/*Creation of socket*/
/*Creation of socket*/
request_sd = socket(PF_INET,
sd = socket(PF_INET, SOCK_STREAM, SOCK_STREAM,
IPPROTO_TCP); IPPROTO_TCP);

To create a new socket endpoint for


Purpose communication.

Call to function socket() creates a transport control block


(hidden in kernel), and returns a reference to it (integer as
index)
Create a Socket
socket()
int socket(int domain, int type, int protocol);

Returns a descriptor (or handle) for the socket.


Arguments:
 domain: Protocol family
 Example: AF_INET, AF_INET6, AF_ROUTE,…
 type: Semantics of the communication
 Example: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, …
 protocol: Specific protocol
 Example: IPPROTO_TCP, IPPROTO_UDP, ….
 AF_INET, SOC_STREAM and IPPROTO_TCP are constants that are
defined in include files.
 <bits/socket.h> which include by <sys/socket.h>
 <netinet/in.h>
Create a Socket
How to identify clients to accept, and server to contact?
Machine??
By its IP address (e.g.: 129.240.64.199)
Application/service/program?
By (IP address and) port number
Standard applications have own, “well-known” port
numbers:
SSH: 22
Mail: 25
Web: 80
Look in /etc/services for more.
Address Structure
struct socaddr_in {
int16_t sin_family;
uint16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};

struct in_addr {
uint32_t s_addr;
};

• sin_family -address family used (Defined through a macro)


• sin_port -16-bit transport protocol port number.
• sin_addr -32-bit IP address defined as a new structure in_addr having one s_addr
element only.
• sin_zero -padding (to have an equal size as sockaddr)
• Declare in <netinet/in.h>
Defines IP address and port number in a way the Berkeley socket API needs it.
Address Structure
Client Server
/* declaration */ /* declaration */
struct sockaddr_in serveraddr;
struct sockaddr_in serveraddr;

/* clear the structure */


bzero(&serveraddr,
sizeof(struct sockaddr_in)); /* clear the structure */
bzero(&serveraddr,
/* This will be an address of the sizeof(struct sockaddr_in));
Internet family */
serveraddr.sin_family = AF_INET; /* This will be an address of the
* Internet family */
/* Add the port number */
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(2009);

/* Allow all own addresses to receive */


/* Add the server address – localhost */ serveraddr.sin_addr.s_addr =
servaddr.sin_addr.s_addr = inet_addr( htonl(INADDR_ANY);
"127.0.0.1");
/* Add the port number */
serveraddr.sin_port = htons(2009);
Address Structure
 Fill address type ("family"), address and port number into the
structure
 serveraddr.sin_family = AF_INET;
 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY;)(@ server)
 servaddr.sin_addr.s_addr = inet_addr(
"127.0.0.1");(@ client)
 serveraddr.sin_port = htons( 2009 );
.0.1?
0
127.
 AF_INET dd r =
r . s_a 09;
 a constant ly: indicating
n_ addthat= Internet
20 protocols will be used.
t on .si o rt
no d r _p
Why rverad dr.sin
 INADDR_ANY
e
• s rverad
 a •constant
se meaning any (Internet) address
 in this context: any own Internet address
Byte Order
Different machines may have different representation of
multi-byte values.

Little
Endian

Big
Endian
Byte Order
Example: Storing 32-bit 0X0A0B0C0D
A s s u m in g 8 - b it ( o n e b y t e ) a t o m ic e le m e n t s …

 … b i g e n d ia n :
- the most significant byte (MSB), 0x0A, is stored on the
lowest memory address
- the least significant byte (LSB), 0x0D, is stored on the
highest memory address
increasing memory addresses
… 0x0A 0x0B 0x0C 0x0D …

 … l i t t l e e n d i a n :
- 0x0A is stored on the highest memory address
- 0x0D is stored on the lowest memory address
increasing memory addresses
… 0x0D 0x0C 0x0B 0x0A …
Byte Order
Example: Storing 32-bit 0X0A0B0C0D
I P v 4 h o s t a d d r e s s : r e p r e s e n t s a 3 2 - b it a d d r e s s
- written on paper (”dotted decimal notation”): 129.240.71.213
- b in a r y in b it s : 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1

- hexadecimal in bytes: 0x81 0xf0 0x47 0x8b

 B i g - e n d i a n ( ” n o r m a l ” l e f t t o r i g h t ) :
- one 4 byte int on PowerPC, POWER, Sparc, …: 0x81f0478b

 L i t t l e - e n d i a n :
- one 4 byte int on x86, StrongARM, XScale, …: 0x8b47f081

 M i d d l e / m i x e d / P D P e n d ia n :
- one 4 byte int on PDP-11: 0xf0818b47

 N e t w o r k b y t e o r d e r : 0 x 8 1 f0 4 7 8 b
Byte Order
Translation
 Byte order translation makes communication over several platforms possible:
htons() / htonl()
host-to-network short / long
translate a 16 / 32-bit integer value to network format
ntohs() / ntohl()
network-to-host short/long
translate a 16 / 32-bit integer value to host format

 Example:
 ntohl(0x81f0478b)  == 0x8b47f081
Little-endian (x86 etc.):

 Big-endian (PowerPC etc.):   ntohl(0x81f0478b)  


 == 0x81f0478b
Presentation and Numerical Address
Format
The network…
Does not interpret the “doted decimal notation” presentation
format. inet_pton() is new IPv6
Need a numeric binary formatOldest:
in network byte order.
serveraddr.sin_addr.s_addr =
 inet_pton()
inet_addr(“129.240.64.199”);
inet_addr(“129.240.64.199”);
Translate the text string to a numeric binary format needed
Newer:
by the address structure. inet_aton(“129.240.64.199”,
&serveraddr.sin_addr);
 inet_ntop()
Translate the (numeric binary) network address structure to
a text string.
Binding, Listening, Accepting and
Connecting
Client Server
/*Bind the address to the socket*/
bind(request_sd,
(struct sockaddr*)&serveraddr,
sizeof(struct socaddr_in));

/*Activate listening on the


socket*/
/*connect*/ listen(request_sd, 5);
connect(sd,
(struct sockaddr*)&serveraddr,
sizeof(struct socaddr_in)); /*Wait for connection*/
clientaddrlen =
sizeof(struct sockaddr_in);

sd = accept(request_sd,
struct sockaddr*)&clientaddr,
&clientaddrlen);
Details about the previous slides
bind()
int bind(int sfd, struct sockaddr *a, int addrlen);

Bind a local name to a newly created


Purpose socket.
Tells the socket on the server side which local protocol (i.e., IP address
and port number) to listen to.
Server need to bind a particular port number.
 Kernel remembers which process has bound which port(s).
 Only one process can bind a particular port number a time.
Arguments: Socket descriptor, server address, address length.
Return 0 on success, and -1 if an error occurs.
Details about the previous slides
listen()
int listen(int sfd, int backlog);
Used for stream sockets to mark a
Purpose socket as willing to accept incoming
connections
Define the number of pending connections.
Prepares the server for listening to connect requests, and initializes
a queue for connect requests (->passive)
Arguments: Socket descriptor and acceptable backlog
 The second parameter backlog represents the size of the queue that will
hold accepted connections.
Returns 0 on success and -1 on error.
Details about the previous slides
accept()
int accept(int sfd, struct sockaddr *a, socklen_t *al);

Used by server application to accept


Purpose incoming client connections.
Take the first connect request from the connect request queue
Wait for the connect request to arrive if the queue is empty
Returns a new socket that the server can use to communicate with the client.
Arguments: Socket descriptor, structure that will provide client address and
port, and length of the structure
 a (clientaddr) contains information about the client
 al must be initialized, so accept knows size of a
Details about the previous slides
connect()
int connect(int sfd, struct sockaddr *serv_a, socklen_t *al);

Purpose Connect a client socket to a server


 Connects client socket to a server that is specified in the address structure
 Arguments: socket descriptor, server address, and address size.
 Returns 0 on success, and -1 if an error occurs.
 Possible errors:
ETIMEDOUT - no response (after several tries) and timer expired
ECONNREFUSED – server not running or not allowed to connect
EHOSTUNREACH – HOST not reachable
ENETUNREACH – NET not reachable
Details about the previous slides
close()
Client Server
/*Close the socket*/ /*Close both socket*/
close(sd); close(sd);
close(request_sd)

Used to mark the socket as closed so


Purpose that no further activity may occur on it

Close the socket descriptor


Note that the semantics of close depends
 On the kind of protocol
 Some possible extra setting
 (similar for file descriptor used to operate on disk)
 All data that has been read yet may be thrown away
Complete Coding-Client/Server
Server
#include <stdio.h> /*---- Bind the address struct to the socket ----*/
#include <sys/socket.h> bind(welcomeSocket, (struct sockaddr *) &serverAddr,
#include <netinet/in.h> sizeof(serverAddr));
#include <string.h>
/*---- Listen on the socket, with 5 max connection
int main(){ requests queued ----*/
int welcomeSocket, newSocket; if(listen(welcomeSocket,5)==0)
char buffer[1024]; printf("Listening\n");
struct sockaddr_in serverAddr; else
struct sockaddr_storage serverStorage; printf("Error\n");
socklen_t addr_size;
/*---- Accept call creates a new socket for the incoming
/*---- Create the socket. The three arguments are: ----*/ connection ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *)
(TCP in this case) */ &serverStorage, &addr_size);
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Send message to the socket of the incoming
/*--- Configure settings of the server address struct---*/ connection ----*/
/* Address family = Internet */ strcpy(buffer,"Hello World\n");
serverAddr.sin_family = AF_INET; send(newSocket,buffer,13,0);
/* Set port number, using htons function to use proper //or write(newSocket, buffer, 13);
byte order */
serverAddr.sin_port = htons(7891); return 0;
/* Set IP address to localhost */ }
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof
serverAddr.sin_zero);
Complete Coding-Client/Server
Client
#include <stdio.h> /*---- Connect the socket to the server using the
#include <sys/socket.h> address struct ----*/
#include <netinet/in.h> addr_size = sizeof serverAddr;
#include <string.h> connect(clientSocket, (struct sockaddr *)
&serverAddr, addr_size);
int main(){
int clientSocket; /*---- Read the message from the server into the
char buffer[1024];
buffer ----*/
struct sockaddr_in serverAddr;
recv(clientSocket, buffer, 1024, 0);
socklen_t addr_size;
//or read(clientSocket,buffer, 1024);

/*---- Create the socket. The three arguments are:*/


/*---- Print the received message ----*/
/* 1) Internet domain 2) Stream socket 3) Default
printf("Data received: %s",buffer);
protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
return 0;
/*Configure settings of the server address struct*/ }
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use
proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof
serverAddr.sin_zero);
Details about the previous slides
(Sending/Receiving Data)
ssize_t write( int sockfd, void *buf, size_t len);

writes up to count bytes from the buffer


Purpose pointed buf to the file referred to by the file
descriptor sockfd.

 Arguments: socket descriptor, pointer to buffer of data to send, and


length of the buffer size.
 Returns the number of characters written, and -1 if an error occurs.
Details about the previous slides
(Sending/Receiving Data)
ssize_t read( int sockfd, void *buf, size_t len);

attempts to read up to count bytes from file


Purpose descriptor sockfd into the buffer starting
at buf.

 Arguments: socket descriptor, pointer to buffer to place the data, and


size of the buffer.
 Returns the number of characters read (where 0 implies “end of file”),
and -1 on error.
Details about the previous slides
(Sending/Receiving Data)
 Note: instead of using write(), you can instead use
send(), which is intended for use with sockets.
Only difference is send() takes one additional
argument of flags.
 Similarly, instead of using read(), you can instead use
recv().
Again, only difference is one additional argument of
flags.
 Important to realize they’re basically equivalent
 Most common flags: MSG_OOB, MSG_PEEK,
MSG_WAITALL
Details about the previous slides
(Sending/Receiving Data)
int recv( int sockfd, char *buf, int len, int flag);

int recvfrom( int sockfd, const char *buf, int len, int flag,
struct sockaddr *addr, socklen_t *len);

Used to retrieve data that has been queued


Purpose for a given socket
Host and Network byte Order Conversion Functions
htons/ntohs/htonl/ntohl
 Used to convert short and long values between host and
network byte order.
 The function prototype:

unsigned int htonl( unsigned int hostlong);


unsigned int htons( unsigned short int hostshort);
unsigned int ntohl( unsigned int netlong);
unsigned int ntohs( unsigned short int netshort);
IP Address Conversion Functions
inet_addr/inet_aton/inet_ntoa
 Provide the ability to convert IP address from string form
(dotted-notation) to binary network format (and vice versa)
 The function prototype:

unsigned long inet_addr( const char *addr);


char *inet_ntoa( struct in_addr in);
int inet_aton( const char *addr, struct in_addr *inp);
Summary of Socket Function
(TCP Client-Server)
Server
socket()

bind() Initialization
Client
listen() socket()

accept() connect() Connection


Establishment
Send data:
read() write()
“hello world”
Data Transfer
Send back received data:
write() read()
“hello world”
close() close() Termination
The End
Q&A

You might also like