Docsity

Socket Programming: Understanding Socket Address Structures and Byte Ordering, Slides of Network Programming

Partial preview of the text

Download Socket Programming: Understanding Socket Address Structures and Byte Ordering and more Slides Network Programming in PDF only on Docsity!

Network^ Programming^ (Lec^8

Socket

-^ A^ socket^ is^ an^ abstraction

of^ a^ communication^ endpoint

-^ Applications^ use^ socket

descriptors^ to^ access^ sockets

-^ Socket^ descriptors^ are

implemented^ as^ file^ descriptors

in the^ UNIX^ System • Many^ of^ the^ functions^

that^ deal^ with^ file^ descriptors, such^ as^ read^ and^ write

,^ work^ with^ a^ socket^ descriptor

-^ Uniquely^ identified^ by

an^ Internet^ address,^ an

end‐to‐end protocol^ (TCP,^ UDP,^ SCTP),

and^ a^ port^ number

4

IPv4^ Socket^ Address

Structure^4

^ 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^ or^ UDP^ port^ number

/ /^ network^ byte^ ordered

*/ struct in_addr sin_addr;

/*^32 ‐bit^ IPv4^ address^ / /^ network^ byte^ ordered

/ char^ sin_zero[8];^ /^ unused

*/ };

Data^ types

IPv4^ Socket^ Address

Structure

Data^ types^ for^ the^ s_addr,

sin_family and^ sin_port members. The^ in_addr_t must^

be^ unsigned^ integer^ type

of^ at^ least 32 bits. in_port_t at^ least^16

bits. And^ sa_family_t can

be^ any^ unsigned^ integer

type. The^ latter^ is^ normally

an^8 ‐bit^ unsigned^ integer

if^ the length^ field^ is^ supported. Otherwise^ unsigned

16 ‐bit^ integer^ if^ not^ supported. Both^ the^ IPv4^ address

and^ the^ TCP^ and^ UDP

port^ numbers are^ stored^ in^ the^ structure

in^ network^ byte^ order.

IPv4^ Socket^ Address

Structure

The^ sin_zero^ is^ unused,

but^ set^ to^ zero^ when

filing^ these^ structures. Always^ set^ the^ entire

structure^ to^0 before

filing

it.

IPv6^ Socket^ Address

Structure

The^ SIN6_LEN^ constant

must^ be^ defined^ if^ the system^ supports^ the

length^ member^ for^

socket^ address structure. IPv6^ is^ AF_INET6^. ^128 bit^ sin6_addr member. The^ sin_flowinfo member

is^ divided^ into^ two^ fields: The^ low‐order^20 bytes

are^ the^ flow^ label The^ high‐order^12 bytes

are^ reserved Research^ topic The^ sin6_scope_id^ identifies

the^ scope^ zone^ in^ which scope^ is^ meaningful.

IPv6^ Socket^ Address

Structure struct in6_addr {^ uint8_t^ s6_addr[16];

/*^128 ‐bit^ IPv6^ address

}; #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#^

uint32_t^ sin6_flowinfo;

/*^ flow^ information,^ undefined

struct in6_addr^ sin6_addr;

/*^ IPv6^ address^ */ uint32_t^ sin6_scope_id;

/*^ set^ of^ interfaces^ for

a^ scope^ */ };

Byte^ Ordering

Functions

Byte^ Ordering

Functions

Consider^2 ‐bytes

,^ two^ ways^ to^ store

in^ memory.

With^ lower‐order

at^ the^ starting^ address,

known

as^ little‐endian^ byte

order.

Or^ high‐order^ byte

at^ the^ starting^ address,

known^ as^ big‐endian

byte^ order.

No^ standard^ between

two^ byte^ ordering

and

encounter^ systems

using^ both.

Byte^ order^ used^

by^ given^ system^ is^

host^ byte

order.

Byte^ Ordering

Functions

#include^ <netinet/in.h>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 orderdocsity.com

Byte^ Manipulation

Functions

-^ There^ are^ two^ groups

of^ functions^ that^ operate

on

multibyte^ fields,^ without

interpreting^ the^ data,

and

without^ assuming^ that

data^ is^ a^ null‐terminated

C^ string

-^ These^ functions^ are

required^ when^ dealing

with^ socket

address^ structures^ to

manipulate^ fields^ such

as^ IP

addresses,^ which^ can

contain^ bytes^ of^ 0,^ but

are^ not^ C

character^ strings • The^ functions^ beginning

with^ str^ (for^ string),^ defined

by

including^ the^ <string.h>

header,^ deal^ with^ null

terminated^ C^ character

strings

19

Byte^ Manipulation

Function Berkeley‐derived Functions^19

#include^ <strings.h> 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);^ Returns: 0 if equal, nonzero if unequal

Byte^ Manipulation

Function ANSI C Functions

memset sets^ the^ specified

number^ of^ bytes^ to^ the

value^ c in^ the^ destination. memcpy is^ similar^ to

bcopy,^ but^ the^ order^ of

the^ two pointer^ arguments^ is^ swapped.

bcopy correctly^ handles overlapping^ fields,^ while

the^ behavior^ of^ memcpy is undefined^ if^ the^ source

and^ destination^ overlap.

The^ ANSI^ C memmove function^ must

be^ used^ when^ the^ fields

overlap. memcmp compares

two^ arbitrary^ byte^ strings

and^ returns 0 if^ they^ are^ identical. If^ not^ identical,^ the^ return

value^ is either^ greater^ than^0 or

less^ than^ 0,^ depending

on^ whether the^ first^ unequal^ byte^

pointed^ to^ by^ ptr1^ is^ greater

than^ or less^ than^ the^ corresponding

byte^ pointed^ to^ by^ ptr2.