Unit 2
Unit 2
Unit-2
2
Byte ordering
● An arrangement of bytes when data is transmitted over the network is called byte ordering.
Different computers will use different byte ordering.
● When communication taking place between two machines byte ordering should not make
discomfort.
● Generally an Internet protocol will specify a common form to allow different machines
byte ordering. TCP/IP is the Internet Protocol in use.
● Two ways to store bytes : Big endian and little endian
● Big-endian – High order byte is stored on starting address and low order byte is stored on
next address
● Little-endian – Low order byte is stored on starting address and high order byte is stored on
next address
● unsigned short htons() - This function converts 16-bit (2-byte) data from
host byte order to network byte order.
● unsigned long htonl() - This function converts 32-bit (4-byte) data from
host byte order to network byte order.
● unsigned short ntohs() - This function converts 16-bit (2-byte) data from
network byte order to host byte order.
● unsigned long ntohl() - This function converts 32-bit (4- byte) data from
network byte order to host byte order.
6
System calls
System Calls – An interface between process and operating systems
● It provides
the services of the operating system to the user programs via Application Program
Interface(API).
System calls are the only entry points into the kernel system.
APPLICATION
socket socket
socket
Source:https://www.scribd.com/presenta
tion/74843220/Socket-01
In UNIX systems, socket is just a special file, and socket descriptors are kept in the file descriptor table.
The Windows operating system keeps a separate table of socket descriptors (named socket descriptor table, or
SDT) for each process.
○ A process may use multiple sockets at the same time. The socket descriptor table is used to manage the sockets for this
process.
● The internal data structure for a socket contains many fields, but the system leaves most of them unfilled. The application
must make additional procedure calls to fill in the socket data structure before the socket can be used.
● The socket is used for data communication between two processes (which may locate at different machines). So the
socket data structure should at least contain the address information, e.g., IP addresses, port numbers, etc.
● Conceptual operating system (Windows) data structures after five calls to socket() by a
process. The system keeps a separate socket descriptor table for each process; threads in
the process share the table.
15
Socket()
int socket(int domain, int type, int protocol)
Returns a descriptor (or handle) for the socket
• Domain: protocol family
PF_INET for the Internet
• Type: semantics of the communication
SOCK_STREAM: Connection oriented
SOCK_DGRAM: Connectionless
• Protocol: specific protocol
UNSPEC: unspecified
(PF_INET and SOCK_STREAM already implies TCP)
• E.g., TCP: sd = socket(PF_INET, SOCK_STREAM, 0);
• E.g., UDP: sd = socket(PF_INET, SOCK_DGRAM, 0);
16
Connect()
int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen)
17
Read(), Write() and Close()
Sending data
write(int sockfd, void *buf, size_t len)
• Arguments: socket descriptor, pointer to buffer of data, and length of the buffer
• Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer
• Returns the number of characters read (where 0 implies “end of file”), and -1 on error
Closing the socket
int close(int sockfd)
18
Functions used in server program
socket()- create the socket descriptor
bind()- associate the local address
listen()- wait for incoming connections from clients
accept()- accept incoming connection
read(),write()- communicate with client
close()- close the socket descriptor
19
Bind()
Bind socket to the local address and port
int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
length
20
Listen()
Define the number of pending connections
21
Accept()
int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen)
• Arguments: socket descriptor, structure that will provide client address and port,
and length of the structure
• Returns descriptor for a new socket for this connection
22
RPC – REMOTE PROCEDURE CALL
23
RPC Model(Remote Procedure Call)
● A remote procedure call is an interprocess communication technique that is used for client-server
based applications. It is also known as a subroutine call or a function call.
● RPC allows programs to call the procedure which is located on the other machines.
● Message passing is not visible to the programmer , so it is called as Remote Procedure call (RPC).
● RPC enables a procedure call that does not reside in the address space of the calling process.
● In RPC, the caller and the callee has disjoint address space, hence there is no access to data and variables
in the callers environment.
● RPC performs a message passing scheme for information exchange between the caller and the callee
process.
24
18CSC302J- School of Computing (Odd sem 2020)
RPC Model(Remote Procedure Call)
is finished.
26
18CSC302J- School of Computing (Odd sem 2020)
Client Server RPC Model
Source:https://www.tutorialspoint.com/remote-procedure-call-rpc
27
18CSC302J- School of Computing (Odd sem 2020)
Sequence of events in a RPC
● The client stub is called by the client.
● The client stub makes a system call to send the message to the server and puts the parameters in
the message.
● The message is sent from the client to the server by the client’s operating system.
● The message is passed to the server stub by the server operating system.
● The parameters are removed from the message by the server stub.
● Then, the server procedure is called by the server stub.
30
Client Server Program
Source: Behrouz A. Forouzan, “TCP IP Protocol Suite ” 4th edition, 2010, McGraw-Hill ISBN: 0073376043
18CSC302J- School of Computing (Odd sem 2020) 31
Client Server Program
Source: https://aticleworld.com/socket-programming-in-c-using-tcpip
18CSC302J- School of Computing (Odd sem 2020) 32
Server Processing using TCP
● Create a socket using the socket() function in c.
● Initialize the socket address structure and bind the socket to an address using the bind() function.
● Listen for connections with the listen() function.
● Accept a connection with the accept() function system call.
● This call typically blocks until a client connects to the server.
● Receive and send data by using the recv() and send() function in c.
● Close the connection by using the close() function
Source: Behrouz A. Forouzan, “TCP IP Protocol Suite ” 4th edition, 2010, McGraw-Hill ISBN: 0073376043
40
Introduction
● Connectionless
● Located between application layer and network layer in the TCP/IP protocol suite
41
18CSC302J- School of Computing (Odd sem 2020)
Limitations of UDP
● There is no flow control mechanism.
● Application which use small messages to be sent without reliability then UDP is best.
● For small messages less no. of interactions is required between sender and receiver for UDP compared
to TCP.
● Connectionless services
No difference between different user datagram even the source and destination are
the same
UDP can’t send a stream of data. Hence the message should fit in one user
datagram(less than 65,507 bytes)
● Congestion control – does not provide congestion control and has an assumption that the packets are
small and sporadic so they cant create congestion.
● Error control
If the receiver checks the error through checksum then that user datagram is discarded.
The data link layer adds its own header and passes it to
physical layer
The client sends the message to UDP using the outgoing queue
If the client receives the message the UDP checks if there is a incoming queue created. If it is available it will
deliver else discards the packet sending a ICMP message “port unreachable” to the server.
Server side uses well known port numbers.
The server side the queue remains open as long as the server is running
If the server receives the message the UDP checks if there is a incoming queue created. If it is available it will
deliver else discards the packet sending a ICMP message “port unreachable” to the client
Source:
http://www.myreadingroom.co.in/notes
-and-studymaterial/68-dcn/848-user-
datagram-protocol-udp.html
● The overhead to establish and close a connection may be significant whereas in TCP it takes 9 packets
for exchanges between client and server to achieve the above goal.
● In reliable service the transport layer needs to take care of the lost packet by resending it. So there will
be a uneven delay between different parts of the message delivered.
● TCP leads to creation of congestion or additional congestion in network by resending packets several
times when a packet are lost.
● Used for real time applications which does not accept uneven delay
56
Client-Server Program
Source:
https://www.geeksforgeeks.org/udp-server-client-
implementation-c/
57
Server Processing using UDP
1. Create UDP socket.
2. Bind the socket to server address.
3. Wait until datagram packet arrives from client.
4. Process the datagram packet and send a reply to client.
5. Go back to Step 3.
58
Client Processing using UDP
1. Create UDP socket.
2. Send message to server.
3. Wait until response from server is received.
4. Process reply and go back to step 2, if necessary.
5. Close socket descriptor and exit.
59
UDP Package
Source: https://www.slideshare.net/Thanveen/user-datagram-protocol-for-msc-cs-42037663
● Input queues
● Control-block module
● Input module
● Output module
Process Id
Port number
● The OS assigns well known ports for server and ephemeral ports for clients.
● The process passes the Process Id and port no to the control block module to create an entry in the
table for a process.
● The value of the field queue is zero since the control- block module does not create queues.
If found -> the module uses the info in the entry to enqueue the data.
1. UDP_OUTPUT_MODULE(Data)
2. {
3. Create a user datagram
4. Send the user datagram
5. Return.
6. }
* SCTP is a message-oriented, reliable protocol that combines the best features of UDP and TCP.
Multiple Streams
• SCTP allows multistream service in each connection called as association
• If one of the streams is blocked, the other streams can still deliver their data
Full-Duplex Communication
• SCTP has sending and receiving buffer, hence packets are sent in both directions.
Reliable Service
• It uses an acknowledgment mechanism to check the safe and sound arrival of data.
Types of chunks
• Control chunks - controls and maintains the association
• Data chunks - carries user data
• TSN - Sequence number initialized in an INIT chunk for one direction and in the INIT ACK chunk for the opposite
direction
• SI - all chunks of same stream in one direction have same stream identifier
• Protocol identifier: 32-bit field used by the application program to define the type of data which is ignored by SCTP
• User data: carries the actual user data
• No chunk can carry data belonging to more than one message
• A message can be spread over several data chunks
• Must have at least one byte of user data, can’t be empty
• If the data cannot end at a 32-bit boundary, padding must be added
81
18CSC302J-School of Computing(Odd sem 2020)
SCTP Packet Format
INIT (Initiation chunk)
• First chunk sent by an end point to establish an association
• Cannot carry any other control or data chunks
• Verification tag for this packet is 0
• Common fields
• Type field has a value of 1
• Flag field is 0
• Length field value is minimum of 20 Fig: INIT chunk
• Initiation tag
• 32-bit field defines the value of the verification tag for packets traveling in the opposite direction
• Tag is same for all packets traveling in one direction in an association
• Random number between 0 and 232 − 1
• Advertised receiver window credit:
• 32-bit field used in flow control
• Defines the initial amount of data in bytes that the sender of the INIT chunk can allow
• Outbound stream: 16-bit field defines the number of streams an initiator of the association suggests for streams in
outbound direction.
• Maximum inbound stream: 16-bit field defines the maximum number of streams an initiator of the association can
support in inbound direction
• Initial TSN: initializes TSN in the outbound direction
• Variable-length parameters: optional parameters to define the IP address of sending end point, multihome, cookie state
etc.
18CSC302J-School of Computing(Odd sem 2020) 82
SCTP Packet Format
INIT ack(initiation acknowledgment chunk)
• Second chunk sent during association establishment
• Value of the verification tag is the value of the initiation tag of INIT chunk.
• The parameter of type 7 defines the state cookie sent by the sender of this chunk
• Initiation tag field in this chunk initiates the value of the verification tag for future packets
traveling from the opposite direction
COOKIE ACK
• fourth and last chunk sent during association establishment with data chunk too
• sent by an end point that receives a COOKIE ECHO chunk
• chunk of type 11
ABORT
• Sent when an end point finds a fatal error and needs to abort the association.
FORWARD TSN
• This is a chunk recently added to the standard to inform the receiver to adjust its cumulative TSN
Verification tag
• It is a common value carried in all packets traveling in one direction in an association
• Blind attacker cannot inject a random packet into an association
• A packet from an old association cannot show up in an incarnation
Cookie
• Cookie is sent with the second packet to the address received in the first packet
• If the sender of the first packet is an attacker, the server never receives the third packet
• If the sender of the first packet is an honest client, it receives the second packet, with the cookie
95
18CSC302J- School of Computing (Odd sem 2020)