2nd Unit Ppt1

Embed Size (px)

Citation preview

  • 8/13/2019 2nd Unit Ppt1

    1/50

    Ipv4 Socket Address Structure

    struct in_addr {in_addr_t s_addr; /* 32-bit IPv4 address */

    /* network byte ordered */};

    struct sockaddr_in {uint8_t sin_len; /* length of structure (16) */sa_family_t sin_family; /* AF_INET */

    in_port_t sin_port; /* 16-bit TCP/UDP port number *//* network byte ordered */

    struct in_addr sin_addr; /* 32-bit IPv4 address *//*network byte ordered*/

    char sin_zero[8]; /* unused */ };

    data structurenot exchanged between hosts

  • 8/13/2019 2nd Unit Ppt1

    2/50

    struct in_addr {in_addr_t s_addr; /* 32-bit IPv4 address */

    /* network byte ordered */};

    struct sockaddr_in {uint8_t sin_len; /* length of structure (16) */sa_family_t sin_family; /* AF_INET */in_port_t sin_port; /* 16-bit TCP/UDP port number */

    /* network byte ordered */

    struct in_addr sin_addr; /* 32-bit IPv4 address *//*network byte ordered*/

    char sin_zero[8]; /* unused */ };

    Generic Socket Address Structure

    Passed as reference

    Prior to void definition

  • 8/13/2019 2nd Unit Ppt1

    3/50

    Generic Socket Address Structure

    int bind(int, struct sockaddr *,

    sock_len_t);

  • 8/13/2019 2nd Unit Ppt1

    4/50

    Ipv6 Socket Address Structure

    struct in6_addr {

    uint8_t s6_addr[16]; /* 128-bit IPv6 address */

    /* network byte ordered */};

    #define SIN6_LEN /* required for compile-time tests */

    struct sockaddr_in6 {uint8_t sin6_len; /* length of this struct (28) */

    sa_family_t sin6_family; /* AF_INET6 */

    in_port_t sin6_port; /* transport layer port# */

    /* network byte ordered */

    uint32_t sin6_flowinfo; /* flow information, undefined */struct in6_addr sin6_addr; /* IPv6 address */

    /* network byte ordered */

    uint32_t sin6_scope_id; /* set of interfaces for a scope */ };

  • 8/13/2019 2nd Unit Ppt1

    5/50

    Value-Results arguments

    bind, connect, sento

  • 8/13/2019 2nd Unit Ppt1

    6/50

    Value-Result arguments

    Kernel to the process, size can change accept, recvfrom, getsockname,

    getpeername

    Fixed size data structure

    Kernel informs howmuch information was

    stored

  • 8/13/2019 2nd Unit Ppt1

    7/50

    Socket Address Structures

  • 8/13/2019 2nd Unit Ppt1

    8/50

    Byte ordering functions

  • 8/13/2019 2nd Unit Ppt1

    9/50

    Internet Protocols

    Network byte order big-endian

    TCP Port: 16 bits

    Ipv4 address: 32 bits

    # include

    uint16_t htons(uint16_t host16bitvalue) ;

    uint32_t htonl(uint32_t host32bitvalue) ;

    Both return: value in network byte order

    uint16_t ntohs(uint16_t net16bitvalue) ;uint32_t ntohl(uint32_t net32bitvalue) ;

    Both return: value in host byte order

  • 8/13/2019 2nd Unit Ppt1

    10/50

    Byte manipulation function

    Operates on multibyte fileds, without

    interpreting the data, and without asumingthat the data is a null-terminated C string

    bzero, bcopy, bcmp

    memset, memcpy, memcmp

  • 8/13/2019 2nd Unit Ppt1

    11/50

    f

  • 8/13/2019 2nd Unit Ppt1

    12/50

    Address convertion function

  • 8/13/2019 2nd Unit Ppt1

    13/50

    d it

  • 8/13/2019 2nd Unit Ppt1

    14/50

    read, writelib/readn.c

    1 #include "unp.h"

    2 ssize_t /* Read "n" bytes from a descriptor. */

    3 readn(int fd, void *vptr, size_t n)4 {

    5 size_t nleft;

    6 ssize_t nread;

    7 char *ptr;

    8 ptr = vptr;

    9 nleft = n;10 while (nleft > 0) {

    11 if ( (nread = read(fd, ptr, nleft)) < 0) {

    12 if (errno == EINTR)

    13 nread = 0; /* and call read() again */

    14 else

    15 return (-1);

    16 } else if (nread == 0)17 break; /* EOF */

    18 nleft -= nread;

    19 ptr += nread;

    20 }

    21 return (n - nleft); /* return >= 0 */

    22 }

    d it

  • 8/13/2019 2nd Unit Ppt1

    15/50

    read, writelib/writen.c

    1 #include "unp.h"

    2 ssize_t /* Write "n" bytes to a descriptor. */

    3 writen(int fd, const void *vptr, size_t n)

    4 {

    5 size_t nleft;

    6 ssize_t nwritten;

    7 const char *ptr;

    8 ptr = vptr;

    9 nleft = n;

    10 while (nleft > 0) {11 if ( (nwritten = write(fd, ptr, nleft))

  • 8/13/2019 2nd Unit Ppt1

    16/50

    read, write1 # include "unp.h"

    2 static int read_cnt;

    3 static char *read_ptr;

    4 static char read_buf[MAXLINE];5 static ssize_t

    6 my_read(int fd, char *ptr)

    7 {

    8 if (read_cnt

  • 8/13/2019 2nd Unit Ppt1

    17/50

    _

    23 readline(int fd, void *vptr, size_t maxlen)

    24 {

    25 ssize_t n, rc;

    26 char c, *ptr;

    27 ptr = vptr;

    28 for (n = 1; n < maxlen; n++) {29 if ( (rc = my_read(fd, &c)) == 1) {

    30 *ptr++ = c;

    31 if (c == '\n')

    32 break; /* newline is stored, like fgets() */

    33 } else if (rc == 0) {

    34 *ptr = 0;

    35 return (n - 1); /* EOF, n - 1 bytes were read */

    36 } else

    37 return (-1); /* error, errno set by read() */

    38 }

    39 *ptr = 0; /* null terminate like fgets() */

    40 return (n);

    41 }42 ssize_t

    43 readlinebuf(void **vptrptr)

    44 {

    45 if (read_cnt)

    46 *vptrptr = read_ptr;

    47 return (read_cnt);48 }

    read, write

    isfdtype

  • 8/13/2019 2nd Unit Ppt1

    18/50

    isfdtype

    It checks whether the descriptor is a socket

    descriptor

    Socket functions for elementary TCP client server

  • 8/13/2019 2nd Unit Ppt1

    19/50

    Socket functions for elementary TCP client-server

    The socket API

  • 8/13/2019 2nd Unit Ppt1

    20/50

    The socket API

    Function socket

    int socket ( int family, int type, int protocol)

    return > 0 if OK, -1 if error

    Returns socket descriptor (small positive integer), similar to UNIX

    file descriptor, to be used perform functions on the socket such as

    read, write and close

    socket

  • 8/13/2019 2nd Unit Ppt1

    21/50

    socket

    #include

    int socket (int family, int type, int protocol);Returns: non-negative descriptor if OK, -1 on error

    Socket Function

  • 8/13/2019 2nd Unit Ppt1

    22/50

    Socket Function

    Parameters:

    family: specifies the protocol family:

    AF_INET

    for IPv4AF_INET6 for IPv6

    AF_UNIX ouAF_LOCAL -Unix domain socket

    type: specifies transport protocol:

    SOCK_STREAM: stream socket for TCP

    SOCK_DGRAM: datagram socket for UDP

    protocol: usually set to zero

    socket Function

  • 8/13/2019 2nd Unit Ppt1

    23/50

    Combination of interest

    Family Type

    AF_INET SOCK_STREAM SOCK_DGRAM

    AF_UNIX SOCK_STREAM SOCK_DGRAM

    Exemple:sockfd = socket(AF_INET, SOCK_STREAM, 0),

    creates an IPv4 socket to be used by TCP

    socket Function

    Connect Function

  • 8/13/2019 2nd Unit Ppt1

    24/50

    int connect ( int sockfd, const struct

    sockaddr *servaddr, int addrlen)

    return 0 if OK, -1 if error,

    Used for establishing a connection:three-wayhandshake

    sockfd: return parameter of socket() servaddr: data structure initiated with remote

    socket identifications (remote IP address, remoteport number)

    For a client TCP, it is not necessary to performbind: the kernel chooses the local port ephemeralport number and the source IP number ifnecessary

    Chosen in a way to avoid conflict of use of port

    Connect Function

    connect

  • 8/13/2019 2nd Unit Ppt1

    25/50

    connect

    errors

    timeouterro ETIMEDOUT, 75 seconds,

    configurable If the answer to a SYN segment is an RST one

    error ECONNREFUSEDno process

    listening to the port

    Destination unreachable (ICMP), soft error, triesfor 75 seconds (4.4 BSD)

    #include

    int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

    Returns: 0 if OK, -1 on error

    Bind Function

  • 8/13/2019 2nd Unit Ppt1

    26/50

    int bind(int sockfd, (struct sockaddr)* myaddr, int socklen)

    return 0 if OK, -1 if error

    binda local protocol address given by thestructure myaddr to a socket (sockfd)

    The application can let the kernel chooses the

    IP address and the port number;

  • 8/13/2019 2nd Unit Ppt1

    27/50

    If a host has more than one IP, then the kernel canchoose one

    Ephemeral port numbers are chosen to avoidconflict with other socket

    To let the kernel chooses the IP address theconstant INADDR_ANY should be assigned to theIP address myaddr

    To let the kernel chooses an ephemeral portnumber the constant zero should be set to thecorresponding field of myaddr:

    *myaddr.sin_port = 0;

    *myaddr.sin_addr.s_addr = INADDR_ANY

    bind

  • 8/13/2019 2nd Unit Ppt1

    28/50

    bind

    Bind performed by TCP server restricts the

    socket to receiving incoming clientconnection destined only to that IP address

    #include

    int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);

    Returns: 0 if OK, -1 on error

    bind

  • 8/13/2019 2nd Unit Ppt1

    29/50

    No cliente, kernel faz o bind com oendereo IP da interface

    Listen Function

  • 8/13/2019 2nd Unit Ppt1

    30/50

    An active (client) socket is one that called connect com osocket, starting a 3-way handshake with a remote host;

    An applications (server) which calls listen, makes anunconnected socket a passive one meaning that the kernelmust accept incoming connection requests directed to thissocket.

    CLOSED LISTEN

    Clients call connectwhile servers call listen followed byaccept

    The backlogparameter indicates the total size of twoqueues: the complete queue (ESTABLISHED state) and the

    incomplete connection queue (SYN_RVCD state)

    int listen (int sockfd, (struct sockaddr) * myaddr, int backlog)

    return 0 if OK, -1 if error

    listen

  • 8/13/2019 2nd Unit Ppt1

    31/50

    listen

  • 8/13/2019 2nd Unit Ppt1

    32/50

    listen

  • 8/13/2019 2nd Unit Ppt1

    33/50

    listen

  • 8/13/2019 2nd Unit Ppt1

    34/50

    accept Function

  • 8/13/2019 2nd Unit Ppt1

    35/50

    Called by a TCP server to return the next completed

    connection from the front of the completed connectionqueue. If the queue is empty the process is oput to sleep

    int accept (int sockfd, (struct sockaddr) * cliaddr, int * socklen)

    returns nonnegative descriptor if OK, -1 if error

    aadrlen is a value-result argument and it returns the size of

    the structure pointed to by cliaddr, i.e., the number of bytes

    stored by the kernel in the socket address structure.

    fork- exec

  • 8/13/2019 2nd Unit Ppt1

    36/50

    Fork is called once but it returns twice. The return valuetell the process whether it is a parent or a child

    It returns once to the calling process (parent) with a return

    value that is the process ID of the newly created process(child)

    It returns once in the child, with a return value of zero (0)

    All the descriptors opened by the parent before the forkare shared with the child after the fork.

    connected sockts aer an accept and a fork is shared withthe

    Two uses of fork: a process makes a copy of itself (fork)and a process executes another program (forke exec)

    # include

    pid_t fork(void);

    Returns: 0 in child, process ID of child in parent, -1 on error

    fork-exec

  • 8/13/2019 2nd Unit Ppt1

    37/50

    #include

    int execl (const char *pathname, const char *arg0, ... /* (char *) 0 */ );

    int execv (const char *pathname, char *const argv[ ]);

    int execle (const char *pathname, const char *arg0, ...

    /* (char *) 0, char *const envp[ ] */ );

    int execve (const char *pathname, char *const argv[ ], char *const envp[ ]);

    int execlp (const char *filename, const char *arg0, ... /* (char *) 0 */ );

    int execvp (const char *filename, char *const argv[ ]);

    All six return: -1 on error, no return on success

    fork-exec

  • 8/13/2019 2nd Unit Ppt1

    38/50

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    39/50

    Concurrent server (iterative server)one server,

    several connections. Concurrent server serves

    several active connections.

    Server performers a fork to create a child to

    handle the client request

    After accept, the server calls fork and the child

    serves the client (on connfd, the connected

    socket) and the parent waits for anotherconnection (on listenfd, the listening socket)

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    40/50

    Connect returns a brand new socketdescriptor for the connection to be

    established with the TCP client.

    The return of a connect makes a socket a

    connected socket.

    On closing a socket only the specific

    connection (socket) is closed, the server

    remains listening to new socket connectionrequest

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    41/50

    A close performed by the server does notimply in terminating the connection, a FYN

    is sent only when the number of reference

    to the descriptor in the file table is zero

    when a child is created the number ofreference is incremented by one.

    If a server does not close the socket none

    of the children connections will be closedsince the number of reference will never

    be zero and server runs out of descriptor

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    42/50

    pid_t pid;

    int listenfd, connfd;

    listenfd = Socket( ... ); /* fill in sockaddr_in{ } with server's well-known port */

    Bind(listenfd, ... );

    Listen(listenfd, LISTENQ);

    for ( ; ; ) {

    connfd = Accept (listenfd, ... ); /* probably blocks */

    if( (pid = Fork()) == 0) {

    Close(listenfd); /* child closes listening socket */

    doit(connfd); /* process the request */

    Close(connfd); /* done with this client */exit(0); /* child terminates */

    }

    Close(connfd); /* parent closes connected socket */

    }

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    43/50

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    44/50

    Concurrent server

  • 8/13/2019 2nd Unit Ppt1

    45/50

    close

  • 8/13/2019 2nd Unit Ppt1

    46/50

    SHUTDOWN used instead to force the

    termination of a connection

    #include

    int close (int sockfd);

    Returns: 0 if OK, -1 on error

    getsocknameand getpeername

  • 8/13/2019 2nd Unit Ppt1

    47/50

    Returns the protocol address either local orremote

    usos:

    connectnot followed bybind After calling bind with 0 srgument

    TCP server performs a bind in a wildcard port

    #include

    int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);

    int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);

    Returns: 0 if OK, -1 on error

    getsocketname and getpeername

  • 8/13/2019 2nd Unit Ppt1

    48/50

    lib/sockfd_to_family.c

    1 #include "unp.h"

    2 int

    3 sockfd_to_family(int sockfd)

    4 {

    5 struct sockaddr_storage ss;

    6 socklen_t len;7 len = sizeof(ss);

    8 if (getsockname(sockfd, (SA *) &ss, &len) < 0)

    9 return (-1);

    10 return (ss.ss_family);

    11 }

    TCP ports and concurrent servers

  • 8/13/2019 2nd Unit Ppt1

    49/50

    multihomedone-or-any

    any-wildcard

    TCP ports and concurrent servers

  • 8/13/2019 2nd Unit Ppt1

    50/50