1 Network Programming Berkeley Sockets (BSD) Socket to me!

36
1 Network Programming Network Programming Berkeley Sockets (BSD) Berkeley Sockets (BSD) Socket to me!

Transcript of 1 Network Programming Berkeley Sockets (BSD) Socket to me!

1

Network Programming Network Programming Berkeley Sockets (BSD)Berkeley Sockets (BSD)

Socket to me!

2

Network Application Network Application Programming Interface (API)Programming Interface (API)

• The services provided by the operating The services provided by the operating system that provide the interface between system that provide the interface between application and protocol software.application and protocol software.

ApplicationApplication

Network APINetwork API

Protocol AProtocol A Protocol BProtocol B Protocol CProtocol C

3

Network APINetwork API

• Generic Programming Interface.Generic Programming Interface.

• Support for message oriented and Support for message oriented and connection oriented communication.connection oriented communication.

• Work similar with well-known I/O Work similar with well-known I/O services (when this makes sense).services (when this makes sense).

• Operating System independence.Operating System independence.

4

Generic Programming Generic Programming InterfaceInterface

• Support multiple communication Support multiple communication protocol suites (families).protocol suites (families).

• Address (endpoint) representation Address (endpoint) representation independence.independence.

• Provide special services for Client and Provide special services for Client and Server?Server?

5

TCP/IPTCP/IP

• TCP/IP does not include an API TCP/IP does not include an API definition.definition.

• There are a variety of APIs for use with There are a variety of APIs for use with TCP/IP:TCP/IP:– BSD Sockets (C language)BSD Sockets (C language)– TLI, XTI (C language)TLI, XTI (C language)– WinsockWinsock– MacTCPMacTCP

6

Functions needed:Functions needed:

• Specify local and remote Specify local and remote communication endpointscommunication endpoints

• Initiate a connectionInitiate a connection

• Wait for incoming connectionWait for incoming connection

• Send and receive dataSend and receive data

• Terminate a connection gracefullyTerminate a connection gracefully

• Error handlingError handling

7

BSD SocketsBSD Sockets

• Generic:Generic:– support for multiple protocol families.support for multiple protocol families.– address representation independenceaddress representation independence

• Uses existing I/O programming interface Uses existing I/O programming interface as much as possible.as much as possible.

8

SocketSocket• A socket is an abstract representation of a A socket is an abstract representation of a

communication endpoint.communication endpoint.• Sockets work like Unix I/O services (files I/O, pipes & Sockets work like Unix I/O services (files I/O, pipes &

message queues etc).message queues etc).• … … but Sockets (obviously) have special needs:but Sockets (obviously) have special needs:

– Client & Server role definitionClient & Server role definition– connection-oriented & connectionless network connectionsconnection-oriented & connectionless network connections– more parameters must be specified more parameters must be specified (protocol type, local-addr, (protocol type, local-addr,

local process, foreign-addr, foreign-process)local process, foreign-addr, foreign-process)

– specifying communication endpoint addresses and dealing specifying communication endpoint addresses and dealing with multiple communication protocols (INET, XNS, Unix with multiple communication protocols (INET, XNS, Unix Domain)Domain)

– stream and message oriented servicesstream and message oriented services

9

Unix Descriptor TableUnix Descriptor TableDescriptor TableDescriptor Table

0

1

2

3

4

Data structure for file 0Data structure for file 0

Data structure for file 1Data structure for file 1

Data structure for file 2Data structure for file 2

10

Socket Descriptor Data Socket Descriptor Data StructureStructure

Descriptor TableDescriptor Table

0

1

2

3

4

Family: PF_INETFamily: PF_INETService: SOCK_STREAMService: SOCK_STREAMLocal IP: 111.22.3.4Local IP: 111.22.3.4Remote IP: 123.45.6.78Remote IP: 123.45.6.78Local Port: 2249Local Port: 2249Remote Port: 3726Remote Port: 3726

Family: PF_INETFamily: PF_INETService: SOCK_STREAMService: SOCK_STREAMLocal IP: 111.22.3.4Local IP: 111.22.3.4Remote IP: 123.45.6.78Remote IP: 123.45.6.78Local Port: 2249Local Port: 2249Remote Port: 3726Remote Port: 3726

11

Socket system calls - OverviewSocket system calls - Overview

socket()socket() ““Install a telephone line”Install a telephone line”

Creates a communication endpoint (allocate the necessary resources) for Creates a communication endpoint (allocate the necessary resources) for later use. Returns a socket descriptor. later use. Returns a socket descriptor.

bind()bind() ““Get a telephone number”Get a telephone number”

BBinds a socketinds a socket (descriptor) (descriptor) to an address to an address

listen()listen() ““Enable Enable "call waiting" for your telephone"call waiting" for your telephone””

Sets the maximum number of requests that will be queued before Sets the maximum number of requests that will be queued before requests start being denied. requests start being denied.

accept()accept() ““Wait for incoming calls and pickup the telephone when it rings”Wait for incoming calls and pickup the telephone when it rings”

Blocks until connection request appears. Takes the first request on the Blocks until connection request appears. Takes the first request on the queue and creates (returns) a new socket (socket descriptor) (passive queue and creates (returns) a new socket (socket descriptor) (passive open)open)

connect()connect() ““Dialing (a specific phone number)”Dialing (a specific phone number)”

(Tries to) Make a connection to a listening socket (active open)(Tries to) Make a connection to a listening socket (active open)

read(), recvfrom()read(), recvfrom()

write(), sendto()write(), sendto()““Let’s talk now…”Let’s talk now…”

Read (write) from (to) a connected stream socket or a established Read (write) from (to) a connected stream socket or a established datagram socketdatagram socket

close(), shutdown()close(), shutdown() ““Hanging UpHanging Up””

Closes a socket (takes care about any unsent data)Closes a socket (takes care about any unsent data)

12

Socket system calls for a typical Socket system calls for a typical connection-oriented session (TCP)connection-oriented session (TCP)

13

Socket system calls for a typical Socket system calls for a typical connectionless session (UDP)connectionless session (UDP)

14

Creating a SocketCreating a Socket#include <sys/types>#include <sys/types>

#include <sys/socket.h>#include <sys/socket.h>

……

int socket(int family,int type,int proto);int socket(int family,int type,int proto);

• familyfamily specifies the protocol family specifies the protocol family • AF_INET (or PF_INET) for TCP/IP protocolsAF_INET (or PF_INET) for TCP/IP protocols• AF_NS (or PF_NS) for Xerox NS protocolsAF_NS (or PF_NS) for Xerox NS protocols• AF_UNIX (or PF_UNIX) for Unix internal protocolsAF_UNIX (or PF_UNIX) for Unix internal protocols

• typetype specifies the type of service specifies the type of service• SOCK_STREAM for stream socket (TCP)SOCK_STREAM for stream socket (TCP)• SOCK_DGRAM for datagram socket (UDP).SOCK_DGRAM for datagram socket (UDP).• SOCK_RAW for raw datagrams (IP)SOCK_RAW for raw datagrams (IP)

• protocolprotocol specifies the specific protocol specifies the specific protocol • (usually 0, which means (usually 0, which means the defaultthe default).).

15

socket()socket()

• socket()socket() system call returns a socket system call returns a socket descriptor (small integer) or a -1 on error.descriptor (small integer) or a -1 on error.

• socket()socket() allocates resources needed for a allocates resources needed for a communication endpoint - but it does not deal communication endpoint - but it does not deal with endpoint addressingwith endpoint addressing

• socket()socket().specifies one element of the .specifies one element of the TCP’s 5-tuple or the UDP’s 3-tupleTCP’s 5-tuple or the UDP’s 3-tuple

16

Specifying an Endpoint Specifying an Endpoint AddressAddress

• Remember that the sockets API is Remember that the sockets API is generic.generic.

• There must be a generic way to specify There must be a generic way to specify endpoint addresses.endpoint addresses.

• TCP/IP requires an IP address and a TCP/IP requires an IP address and a port number for each endpoint address.port number for each endpoint address.

• Other protocol suites (families) may use Other protocol suites (families) may use other schemes.other schemes.

17

POSIX data typesPOSIX data types

int8_tint8_t signed 8bit intsigned 8bit intuint8_tuint8_t unsigned 8 bit intunsigned 8 bit intint16_tint16_t signed 16 bit intsigned 16 bit intuint16_tuint16_t unsigned 16 bit intunsigned 16 bit intint32_tint32_t signed 32 bit intsigned 32 bit intuint32_tuint32_t unsigned 32 bit intunsigned 32 bit int

u_char, u_short, u_int, u_longu_char, u_short, u_int, u_long

18

More POSIX data typesMore POSIX data types

sa_family_tsa_family_t address familyaddress family

socklen_tsocklen_t length of structlength of struct

in_addr_tin_addr_t IPv4 addressIPv4 address

in_port_tin_port_t IP port numberIP port number

19

Generic socket addressesGeneric socket addresses

struct sockaddr {struct sockaddr {u_shortu_short sa_len;sa_len;u_short u_short sa_family; sa_family; charchar sa_data[14];sa_data[14];

};};

• definition in definition in <sys/socket.h><sys/socket.h>• sa_familysa_family specifies the address type.specifies the address type.• sa_datasa_data specifies the address value.specifies the address value.

Used b

y ke

rnel

20

sockaddrsockaddr• An address that will allow me to use An address that will allow me to use

sockets to communicate with my kids.sockets to communicate with my kids.

• address type address type AF_DAVESKIDSAF_DAVESKIDS• address values:address values:

AndreaAndrea 11 MomMom 55

JeffJeff 22 DadDad 66

RobertRobert 33 DogDog 77

EmilyEmily 44

21

AF_DAVESKIDSAF_DAVESKIDS• Initializing a sockaddr structure to point Initializing a sockaddr structure to point

to Robert:to Robert:

struct sockaddr robster;struct sockaddr robster;

robster.sa_family = AF_DAVESKIDS;robster.sa_family = AF_DAVESKIDS;

robster.sa_data[0] = 3;robster.sa_data[0] = 3;

Really old picture!

22

AF_INETAF_INET

• For AF_DAVESKIDS we only needed 1 For AF_DAVESKIDS we only needed 1 byte to specify the address.byte to specify the address.

• For AF_INET we need:For AF_INET we need:– 16 bit port number 16 bit port number – 32 bit IP address32 bit IP address

IPv4 only!

23

struct sockaddr_in (IPv4)struct sockaddr_in (IPv4)A special kind of sockaddr structureA special kind of sockaddr structure

struct in_addr {struct in_addr { u_longu_long s_addr;s_addr;

};};struct sockaddr_in {struct sockaddr_in {

u_shortu_short sin_len;sin_len;short short sin_family;sin_family;u_short u_short sin_port;sin_port;

struct in_addrstruct in_addr sin_addr; sin_addr; charchar sin_zero[8];sin_zero[8];

};};

• definition in definition in <sys/socket.h><sys/socket.h>

24

Data representationData representation• multibyte values (e.g. 16-bit integers)multibyte values (e.g. 16-bit integers)• little endian little endian

– least significant byte first least significant byte first ((DEC, IntelDEC, Intel))

• big endian big endian – most significant byte first most significant byte first ((Sun, SGI, HPSun, SGI, HP))

• hhow do we store the integer 0x12345678 ?ow do we store the integer 0x12345678 ?

0x12

0x34

0x56

0x78

0x101

0x102

0x103

0x100 0x78

0x56

0x34

0x12

0x101

0x102

0x103

0x100

Big Endian Little Endian

25

Network Byte OrderNetwork Byte Order• network byte order = big endiannetwork byte order = big endian

– (TCP/IP, XNS, SNA)(TCP/IP, XNS, SNA)

• integer fields in protocol headersinteger fields in protocol headers• all values stored in a all values stored in a sockaddr_insockaddr_in must be must be

in network byte order.in network byte order.– sin_portsin_port a TCP/IP port number.a TCP/IP port number.– sin_addrsin_addr an IP address.an IP address.

26

Network Byte Order FunctionsNetwork Byte Order Functions

‘‘hh’ : host byte order ‘’ : host byte order ‘nn’ : network byte order’ : network byte order

‘‘ss’ : short (16bit) ‘’ : short (16bit) ‘ll’ : long (32bit)’ : long (32bit)

u_short u_short hhtotonnss(u_short);(u_short);u_short u_short nntotohhss(u_short);(u_short);

u_long u_long hhtotonnll(u_long);(u_long);u_long u_long nntotohhll(u_long);(u_long);

27

TCP/IP AddressesTCP/IP Addresses

• We don’t need to deal with We don’t need to deal with sockaddr sockaddr structures since we will only deal with a structures since we will only deal with a real protocol family.real protocol family.

• We can use We can use sockaddr_insockaddr_in structures.structures.

BUT: The C functions that make up the BUT: The C functions that make up the sockets API expect structures of type sockets API expect structures of type sockaddrsockaddr..

28

sin_lensin_lensa_lensa_len

sa_familysa_family

sa_datasa_data

AF_INET

sin_port

sin_addr

sin_zero

sockaddrsockaddr sockaddr_insockaddr_in

29

Assigning an address to a Assigning an address to a socketsocket

• The The bind()bind() system call is used to assign system call is used to assign an address to an existing socket.an address to an existing socket.

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

int addrlen); int addrlen);

• bindbind returns 0 if successful or -1 on error. returns 0 if successful or -1 on error.

30

bind()bind()

• calling calling bind()bind() assigns the address assigns the address specified by the specified by the sockaddrsockaddr structure to the structure to the socket descriptor.socket descriptor.

• bind()bind() fills in the local-addr and local-port fills in the local-addr and local-port elements of the association 5-tuple.elements of the association 5-tuple.

• you can give you can give bind()bind() a a sockaddr_insockaddr_in structure:structure:

bind( mysock, bind( mysock, (struct sockaddr*) &myaddr,(struct sockaddr*) &myaddr, sizeof(myaddr) );sizeof(myaddr) );

31

bind()bind() Example Example

int mysock,err;int mysock,err;struct sockaddr_in myaddr;struct sockaddr_in myaddr;

mysock = socket(AF_INET,SOCK_STREAM,0);mysock = socket(AF_INET,SOCK_STREAM,0);myaddr.sin_family = AF_INET;myaddr.sin_family = AF_INET;myaddr.sin_port = htons( portnum );myaddr.sin_port = htons( portnum );myaddr.sin_addr = htonl( ipaddress);myaddr.sin_addr = htonl( ipaddress);

err=bind(mysock, (sockaddr *) &myaddr, err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));sizeof(myaddr));

32

Uses for Uses for bind()bind()• There are a number of uses for There are a number of uses for bind()bind()::

– Server would like to bind to a well known Server would like to bind to a well known address (port number).address (port number).

– Client can bind to a specific port.Client can bind to a specific port.– Client can ask the O.S. to assign Client can ask the O.S. to assign any availableany available

port number.port number.

TCP/UDP PortsTCP/UDP Ports

– 0 – 1023 : 0 – 1023 : – privileged ports (only for root processes) privileged ports (only for root processes) – typically used by serverstypically used by servers

– 1024 – 65535 : 1024 – 65535 : – automatically assigned (by the kernel) portsautomatically assigned (by the kernel) ports– typically used by clientstypically used by clients

TCP/UDP PortsTCP/UDP Ports

– 0 – 1023 : 0 – 1023 : – privileged ports (only for root processes) privileged ports (only for root processes) – typically used by serverstypically used by servers

– 1024 – 65535 : 1024 – 65535 : – automatically assigned (by the kernel) portsautomatically assigned (by the kernel) ports– typically used by clientstypically used by clients

33

Port assignment - who cares ?Port assignment - who cares ?

• Clients typically don’t care what port Clients typically don’t care what port they are assigned.they are assigned.

• When you call bind you can tell it to When you call bind you can tell it to assign you any available port:assign you any available port:

myaddr.port = htons(0);myaddr.port = htons(0);

34

What is my IP address ?What is my IP address ?

• How can you find out what your IP address is How can you find out what your IP address is so you can tell so you can tell bind()bind() ? ?

• There is no realistic way for you to know the There is no realistic way for you to know the right IP address to give bind() - what if the right IP address to give bind() - what if the computer has multiple network interfaces?computer has multiple network interfaces?

• specify the IP address as: specify the IP address as: INADDR_ANYINADDR_ANY, , this tells the OS to take care of things.this tells the OS to take care of things.

35

IPv4 Address ConversionIPv4 Address Conversion• unsigned long inet_addr( char *);unsigned long inet_addr( char *);

– converts ASCII dotted-decimal IP address to network converts ASCII dotted-decimal IP address to network byte order 32 bit value. Returns 1 on success, 0 on byte order 32 bit value. Returns 1 on success, 0 on failure.failure.

• char *inet_ntoa(struct in_addr);char *inet_ntoa(struct in_addr);

– converts network byte ordered value to ASCII dotted-converts network byte ordered value to ASCII dotted-decimal (a string).decimal (a string).

36

Byte OperationsByte Operations• bcopy (char *src, char *dest, int nbytes);bcopy (char *src, char *dest, int nbytes);

– moves the specified number of bytes from source to moves the specified number of bytes from source to destination (similar with destination (similar with memcpymemcpy))

• bzero (char *dest int nbytes);bzero (char *dest int nbytes);

– writes the specified number of null bytes to destination writes the specified number of null bytes to destination (similar with (similar with memsetmemset))