II- unit1

download II- unit1

of 22

Transcript of II- unit1

  • 8/13/2019 II- unit1

    1/22

    Introduction to Socket Details

    Unix Network Programming

    II - Unit

  • 8/13/2019 II- unit1

    2/22

    Socket Address Structure

    Most socket functions (Posix C lib) require apointer to a socket address struct as an

    argument Each supported protocol suite defines its own

    socket address struct Names begin with sockaddr_ End with a unique suffix for each protocol

  • 8/13/2019 II- unit1

    3/22

    IPv4 Socket Address Struct

    /usr/include/netinet/in.h [#include ]/* Internet address. */struct in_addr

    {in_addr_t s_addr;

    };

    /* Structure describing an Internet socket address. */struct sockaddr_in

    { __SOCKADDR_COMMON (sin_);

    in_port_t sin_port; /* Port number. */struct in_addr sin_addr; /* Internet address. */uint8_t sin_len; /* length of structure */sa_family_t sin_family; /* 32 bit IPV4 address */char sin_zero[8];

    };

  • 8/13/2019 II- unit1

    4/22

    Generic socket Address Struct

    int bind(int, struct sockaddr *,sock_len_t);

  • 8/13/2019 II- unit1

    5/22

    IPv4 Socket Address Structure

    IPv4 address and TCP or UDP port number

    are always stored in network byte order(discussed later)

    An IPv4 socket address structure, commonly

    called an Internet socket address structureis named sockaddr_in and is defined byincluding header.

  • 8/13/2019 II- unit1

    6/22

    Generic Socket Address Structure

    A socket address structure is always passedby reference when passed as an argument

    Any socket function must deal with structsfrom any of the supported protocol families Therefore they require a generic struct Defined in (actually pulled out of

    on our system/* Structure describing a generic socket address. */struct sockaddr

    { __SOCKADDR_COMMON (sa_); /* Common data: address family and length. */char sa_data[14]; /* Address data. */

    };

  • 8/13/2019 II- unit1

    7/22

    Generic Socket Address Struct

    Socket functions are then defined as taking apointer to the generic socket address

    structure:int bind(int, struct sockaddr *, socklen_t);

    To call bind, for example:struct sockaddr_in serv; /* IPv4 socket address structure */

    /* fill in serv{} */bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));

  • 8/13/2019 II- unit1

    8/22

    IPv6 Socket Address Structure

    IPv6 socket address is also defined in :/* IPv6 address */struct in6_addr

    {uint8_t s6_addr[16];

    };#define SIN6_LEN /* required for compile test */

    /* Ditto, for IPv6. */struct sockaddr_in6

    {__SOCKADDR_COMMON (sin6_);in_port_t sin6_port; /* Transport layer port # */uint32_t sin6_flowinfo; /* IPv6 flow information */struct in6_addr sin6_addr; /* IPv6 address */

    uint32_t sin6_scope_id; /* set of interfaces for a scope, IPv6 scope-id*/};

  • 8/13/2019 II- unit1

    9/22

    New Generic Socket Address

    IPv6 defined a new generic socket addressstructure:

    struct sockaddr_storage{

    __SOCKADDR_COMMON (ss_); /* Address family, etc. */__ss_aligntype __ss_align; /* Force desired alignment. */char __ss_padding[_SS_PADSIZE];

    };

  • 8/13/2019 II- unit1

    10/22

    Comparison of Socket Address

    Structures

  • 8/13/2019 II- unit1

    11/22

    Byte Ordering Functions

    Consider a 16-bit integer that is made up oftwo bytes.

    There are 2 ways to store the two bytes inmemory: With the low-order byte at the starting address

    (little-endian) With the high-order byte at the starting address

    (big-endian) Show figure 3.9, pg. 77

  • 8/13/2019 II- unit1

    12/22

  • 8/13/2019 II- unit1

    13/22

    Network Byte Order

    We must deal with these byte orderingdifferences as network programmers

    Networking protocols must specify a networkbyte order

    IP uses big-endian byte ordering

  • 8/13/2019 II- unit1

    14/22

    Value Result Arguments

    When a socket address structure is passed toany socket function, it is always passed byreference, i.e a pointer to the structure ispassed.

    The length of the structure is also passed asan argument

    Length is passed depends on which directionthe structure is being passedi.e from the process to kernel or vice versa.

  • 8/13/2019 II- unit1

    15/22

    Value-Result Argument

    Three functions, bind ,connect and sento pass asocket address structure from the process to the

    kernel One of the argument to these 3 functions is the

    pointer to the socket address structure

    Another argument is the integer size of thestructure.struct sockaddr_in serv;Connect(sockfd,(SA *) &serv, sizeof(serv));

  • 8/13/2019 II- unit1

    16/22

    Value -Result Arguments

    bind, connect, sento

  • 8/13/2019 II- unit1

    17/22

    Value-Result Arguments Kernel to the process, size can change accept , recvfrom , getsockname ,

    g etpeername

    Fixed size data structure Kernel informs howmuch information was

    stored

  • 8/13/2019 II- unit1

    18/22

    Size is both a value when the function iscalled( it tells the kernel the size of the

    structure so that kernel does not write pastthe end of the structure when filling it) A result when the function returns (it tells the

    process how much information the kernelactually stored in the structure)

    This type of argument is called a value-resultargument

    Value-Result Arguments

  • 8/13/2019 II- unit1

    19/22

    Byte Manipulation Functions

    2 groups of functions operate on multibytefields

    #include void bzero (void *dest, size_t nbytes);void bcopy (const void *src, void *dest, size_t nbytes);int bcmp (const void *ptr1, const void *ptr2, size_t nbytes);

    void* memset (void *dest, int c, size_t len);void* memcpy (void *dest, const void *src, size_t nbytes);int memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

  • 8/13/2019 II- unit1

    20/22

    The functions beginning with str (forstring),defined by including the

    header Whose names begin with b (for byte) Names begin with mem (for memory) bzero sets the specified number of bytes to 0

    in the destination . We often use this function to initialize a

    socket address structure to 0

    Byte Manipulation Functions

  • 8/13/2019 II- unit1

    21/22

    bcopy moves the specified number of bytesfrom source to the destination.

    bcmp compares two arbitrary bytes strings .returns zero if two byte strings are

    identicalreturns nonzero if two byte strings

    are not equal memset sets the specified number of bytes

    to the value c in the destination

    Byte Manipulation Functions

  • 8/13/2019 II- unit1

    22/22

    memcpy is similar to bcopy, but the order ofthe two pointer arguments is swapped

    bcopy correctly handles overlappingfields, while the behavior of memcpy isundefined if the source and destination

    overlap memcmp compares two arbitrary byte

    strings and return 0 or less than o,

    Byte Manipulation Functions