NP Unit-3
NP Unit-3
NP Unit-3
UNIT-3
4.1 INTRODUCTION
• The figure shows a timeline of the typical scenario that takes place
between a TCP client and server.
• First, the server is started, then sometime later, a client is started that
connects to the server.
• We assume that the client sends a request to the server, the server
processes the request, and the server sends a reply back to the client.
• This continues until the client closes its end of the connection, which
sends an end-of-file notification to the server.
• The server then closes its end of the connection and either terminates
or waits for a new client connection.
1.SOCKET FUNCTION
To perform Network I/O, the first thing a process must do is, call the socket
function, specifying the type of communication protocol desired and protocol
family, etc.
#include <sys/socket.h>
int socket (intfamily, inttype, intprotocol);
• Family specifies the protocol family and is one of the constants shown
in Figure 4.2. This argument is often referred to as domain instead of
family.
• The socket type is one of the constants shown in Figure 4.3.
• The protocol argument to the socket function should be set to the
specific protocol type found in Figure 4.4, or 0 to select the system's
default for the given combination of family and type.
PARAMETERS
1.FAMILY
• family specifies the protocol family or domain
• Usually: AF_INET
• AF_LOCAL might work on CATS machines
2.Type
• type specifies type of communication.
• TCP supports only SOCK_STREAM
• UDP supports SOCK_DGRAM
3.Protocol
• protocol specifies transport protocol.
• TCP provides reliable, in-order streaming communication Resends lost packets,
guarantees order.
• UDP provides possibly unreliable, possibly out-of-order message delivery
Doesn’t guarantee anything, but usually works fine
Figure 4.5. Combinations of family and type for the socket function
2.CONNECT FUNCTION
The connect function is used by a TCP client to establish a connection with a TCP
server.
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
/* Returns: 0 if OK, -1 on error */
➢ The client kernel saves the message but keeps sending SYNs with the
same time between each SYN as in the first scenario.
➢ If no response is received after some fixed amount of time (75 seconds
for 4.4BSD), the saved ICMP error is returned to the process as
either EHOSTUNREACH or ENETUNREACH.
➢ Note that network unreachables are considered obsolete, and
applications should just treat ENETUNREACH and EHOSTUNREACH as
the same error.
Example: Nonexistent host on the local subnet *
We run the client and specify an IP address that is on the local subnet (192.168.1/24)
but the host ID (100) is nonexistent.
When the client host sends out ARP requests (asking for that host to respond with its
hardware address), it will never receive an ARP reply.
• connect moves from the CLOSED state (the state in which a socket begins
when it is created by the socket function) to the SYN_SENT state, and then,
on success, to the ESTABLISHED state.
• If connect fails, the socket is no longer usable and must be closed. We cannot
call connect again on the socket.
Each time connect fails, we must close the socket descriptor and
call socket again.
3.BIND FUNCTION
• The bind function assigns a local protocol address to a socket.
• The protocol address is the combination of either a 32-bit IPv4 address or a
128-bit IPv6 address, along with a 16-bit TCP or UDP port number.
#include <sys/socket.h>
int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
/* Returns: 0 if OK,-1 on error */
• Servers bind their well-known port when they start. (Figure 1.9) If a TCP
client or server does not do this, the kernel chooses an ephemeral port for
the socket either connect or listen is called.
• It is normal for a TCP client to let the kernel choose an ephemeral
port, unless the application requires a reserved port (Figure 2.10). But
it is rare because they are well-known port.
• Exceptions to this rule are Remote Procedure Call (RPC) servers. They
normally let the kernel choose an ephemeral port for their listening
socket since this port is then registered with the RPC port mapper.
• A process can bind a specific IP address to its socket. The IP address must
belong to an interface on the host.
• TCP client does not bind an IP address to its socket. The kernel chooses
the source IP address when the socket is connected, based on the
outgoing interface that is used.
• For a TCP server, this restricts the socket to receive incoming client
connections destined only to that IP address.
Fig 4.6 Result when specifying IP address and/or port number to bind
• If we specify a port number of 0, the kernel chooses an ephemeral port
when bind is called.
• If we specify a wildcard IP address, the kernel does not choose the local IP
address until either the socket is connected (TCP) or a datagram is sent on the
socket (UDP).
For example, if the subnet is 198.69.10, the first organization's IP address could be
198.69.10.128, the next 198.69.10.129, and so on.
4.LISTEN FUNCTION
The listen function is called only by a TCP server and it performs two actions:
1. The listen function converts an unconnected socket into a passive socket, indicating
that the kernel should accept incoming connection requests directed to this
socket. listen moves the socket from the CLOSED state to the LISTEN state.
2. The second argument backlog to this function specifies the maximum number of
connections the kernel should queue for this socket.
This function is normally called after both the socket and bind functions and must be called
before calling the accept function.
1. An incomplete connection queue, which contains an entry for each SYN that has
arrived from a client for which the server is awaiting completion of the TCP three-
way handshake. These sockets are in the SYN_RCVD state (Figure 2.4).
2. A completed connection queue, which contains an entry for each client with whom
the TCP three-way handshake has completed. These sockets are in the ESTABLISHED
state (Figure 2.4).
When an entry is created on the incomplete queue, the parameters from the listen socket are
copied over to the newly created connection. The connection creation mechanism is
completely automatic the server process is not involved.
Packet exchanges during Connection establishment *
• When a SYN arrives from a client, TCP creates a new entry on the incomplete queue
and then responds with the second segment of the three-way handshake: the
server's SYN with an ACK of the client's SYN (Section 2.6).
• This entry will remain on the incomplete queue, until:
o The third segment of the three-way handshake arrives (the client's ACK of the
server's SYN), or
o The entry times out. (Berkeley-derived implementations have a timeout of 75
seconds for these incomplete entries.)
• If the three-way handshake completes normally, the entry moves from the
incomplete queue to the end of the completed queue.
• When the process calls accept:
o The first entry on the completed queue is returned to the process, or
o If the queue is empty, the process is put to sleep until an entry is placed onto
the completed queue.
SYN Flooding *
SYN flooding is a type of attack (the attacker writes a program to send SYNs at a high
rate to the victim) that attempts to fill the incomplete connection queue for one or
more TCP ports.
Additionally, the source IP address of each SYN is set to a random number (called IP
spoofing) so that the server's SYN/ACK goes nowhere.
This also prevents the server from knowing the real IP address of the attacker
5.ACCEPT FUNCTION
• accept is called by a TCP server to return the next completed connection
from a client.
• Blocks if no connection is available.
#include <sys/socket.h>
int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
/* Returns: non-negative descriptor if OK, -1 on error */
• The cliaddr and addrlen arguments are used to return the protocol address of
the connected peer process (the client).
• addrlen is a value-result argument
• Before the call, we set the integer value *addrlen to the size of the socket
address structure pointed to by cliaddr;
• On return, this integer value contains the actual number of bytes stored by
the kernel in the socket address structure.
If successful, accept returns a new descriptor automatically created by the kernel. This new
descriptor refers to the TCP connection with the client.
• The server creates only one listening socket, which exists for the lifetime of the server.
• The kernel creates one connected socket for each client connection that is accepted.
• When the server is finished serving a given client, the connected socket is closed.
• An integer return code that is either a new socket descriptor or an error indication,
• The protocol address of the client process (through the cliaddr pointer),
• The size of this address (through the addrlen pointer).
Unix close function is used to close a socket and terminate a TCP connection.
#include <unistd.h>
int close (int sockfd);
/* Returns: 0 if OK, -1 on error */
• The default action of close with a TCP socket is to socket as closed and return
to the process immediately.
• The socket descriptor is no longer usable by the process.
• It cannot be used as an argument to read or write.
• But, TCP will try to send any data that is already queued to be sent to the
other end, and after this occurs, the normal TCP connection termination
sequence takes place.
#include <sys/socket.h>
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);
/* Both return: 0 if OK, -1 on error */
o fork is called and a child of inetd is created, with a copy of the parent's
memory image, so the socket address structure is available to the child,
as is the connected socket descriptor.
o When the child execs the real server the memory image of the child is
replaced with the new program file for the Telnet server (the socket
address structure containing the peer's address is lost), and the
connected socket descriptor remains open across the exec.
o first function calls performed by the Telnet server is getpeername to
obtain the IP address and port number of the client.
In this example, the Telnet server must know the value of connfd There are two
common ways to do this.