Lecture 4 Socket Programming

33
Lecture 4 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger

description

Lecture 4 Socket Programming. CPE 401 / 601 Computer Network Systems. slides are modified from Dave Hollinger. Network Application Programming Interface (API). Services that provide the interface between application and protocol software (often by the operating system). Application. - PowerPoint PPT Presentation

Transcript of Lecture 4 Socket Programming

Page 1: Lecture 4 Socket Programming

Lecture 4

Socket Programming

CPE 401 / 601Computer Network Systems

slides are modified from Dave Hollinger

Page 2: Lecture 4 Socket Programming

Network Application Programming Interface (API) Services that provide the interface between

application and protocol software (often by the operating system).

ApplicationApplication

Network APINetwork API

Protocol AProtocol A Protocol BProtocol B Protocol CProtocol C

Socket Programming 2

Page 3: Lecture 4 Socket Programming

Network API wish list Generic Programming Interface

Support multiple communication protocol suites (families).

Address (endpoint) representation independence. Provide special services for Client and Server?

Support for message oriented and connection oriented communication

Work with existing I/O services (when this makes sense)

Operating System independence Socket Programming 3

Page 4: Lecture 4 Socket Programming

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

There are a variety of APIs for use with TCP/IP: Sockets TLI, XTI Winsock MacTCP

Socket Programming 4

Page 5: Lecture 4 Socket Programming

Functions needed: Specify local and remote

communication endpoints Initiate a connection Wait for incoming connection Send and receive data Terminate a connection gracefully Error handling

Socket Programming 5

Page 6: Lecture 4 Socket Programming

Berkeley Sockets Generic:

support for multiple protocol families. address representation independence

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

Socket Programming 6

Page 7: Lecture 4 Socket Programming

Socket A socket is an abstract representation

of a communication endpoint. Sockets work with Unix I/O services just

like files, pipes & FIFOs. Sockets (obviously) have special needs:

establishing a connection specifying communication endpoint

addresses

Socket Programming 7

Page 8: Lecture 4 Socket Programming

Unix Descriptor Table

Descriptor TableDescriptor Table

01234

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

Socket Programming 8

Page 9: Lecture 4 Socket Programming

Socket Descriptor Data Structure

Descriptor TableDescriptor Table

01234

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

Socket Programming 9

Page 10: Lecture 4 Socket Programming

Creating a Socketint socket(int family,int type,int proto);

family specifies the protocol family (PF_INET for TCP/IP).

type specifies the type of service (SOCK_STREAM, SOCK_DGRAM).

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

Socket Programming 10

Page 11: Lecture 4 Socket Programming

socket()

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

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

Socket Programming 11

Page 12: Lecture 4 Socket Programming

Specifying an Endpoint Address Sockets API is generic. There must be a generic way to specify

endpoint addresses. TCP/IP requires an IP address and a port

number for each endpoint address. Other protocol suites (families) may use

other schemes.

Socket Programming 12

Page 13: Lecture 4 Socket Programming

Necessary Background Information: POSIX data typesint8_t signed 8bit intuint8_t unsigned 8 bit intint16_t signed 16 bit intuint16_t unsigned 16 bit intint32_t signed 32 bit intuint32_t unsigned 32 bit int

u_char, u_short, u_int, u_long Obsolete

Socket Programming 13

Page 14: Lecture 4 Socket Programming

More POSIX data typessa_family_t address familysocklen_t length of structin_addr_t IPv4 addressin_port_t IP port number

Socket Programming 14

Page 15: Lecture 4 Socket Programming

Generic socket addresses

struct sockaddr {uint8_t sa_len;sa_family_t sa_family; char sa_data[14];};

sa_family specifies the address type. sa_data specifies the address value.

Used by k

ernel

Socket Programming 15

Page 16: Lecture 4 Socket Programming

sockaddr An address that will allow me to use

sockets to communicate with my family. address type AF_FAMILY address values:

Daughter 1Wife 2Mom 3Dad 4Sister 5Brother 6

Socket Programming 16

Page 17: Lecture 4 Socket Programming

AF_FAMILY Initializing a sockaddr structure to point

to Daughter:

struct sockaddr mary;

mary.sa_family = AF_FAMILY;mary.sa_data[0] = 1;

Socket Programming 17

Page 18: Lecture 4 Socket Programming

AF_INET For AF_FAMILY we only needed 1 byte to

specify the address.

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

IPv4 only!

Socket Programming 18

Page 19: Lecture 4 Socket Programming

struct sockaddr_in (IPv4)struct sockaddr_in {uint8_t sin_len;sa_family_t sin_family;in_port_t sin_port;

struct in_addr sin_addr; char sin_zero[8];

};struct in_addr { in_addr_t s_addr; };

Length of structure (16)

AF_INET

16 bit Port number

32 bit IPv4 address

Make structure 16 bytes

Socket Programming 19

Page 20: Lecture 4 Socket Programming

struct sockaddr_in (IPv6)struct sockaddr_in6 {uint8_t sin6_len;sa_family_t sin6_family;in_port_t sin6_port;

uint32_t sin6_flowinfo;struct in6_addr sin6_addr; uint32_t sin6_scope_id;

};struct in6_addr { uint8_t s6_addr[16];

};

Length of structure (28)

AF_INET6

Port number

128 bit IPv6 address

Scope of address

Flow label

Socket Programming 20

Page 21: Lecture 4 Socket Programming

Network Byte Order All values stored in a sockaddr_in

must be in network byte order. sin_port a TCP/IP port number. sin_addr an IP address.

!!! Common Mistake !!!

Socket Programming 21

Page 22: Lecture 4 Socket Programming

Network Byte Order Functions

‘h’ : host byte order ‘n’ : network byte order

‘s’ : short (16bit) ‘l’ : long (32bit)

uint16_t htons(uint16_t);uint16_t ntohs(uint_16_t);

uint32_t htonl(uint32_t);uint32_t ntohl(uint32_t);

Socket Programming 22

Page 23: Lecture 4 Socket Programming

TCP/IP Addresses We don’t need to deal with sockaddr

structures since we will only deal with a real protocol family.

We can use sockaddr_in structures.

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

Socket Programming 23

Page 24: Lecture 4 Socket Programming

sa_lensa_family

sa_data

lengthAF_INET

port

addr

zero

sockaddrsockaddr sockaddr_insockaddr_in

Socket Programming 24

sockaddr_in6sockaddr_in6length

AF_INET6port

Flow-label

Scope ID

addr

28 bytes

variable 16 bytes

Page 25: Lecture 4 Socket Programming

Assigning an address to a socket

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

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

bind returns 0 if successful or -1 on error.

const!

Socket Programming 25

Page 26: Lecture 4 Socket Programming

bind() calling bind() assigns the address

specified by the sockaddr structure to the socket descriptor.

You can give bind() a sockaddr_in structure:

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

Socket Programming 26

Page 27: Lecture 4 Socket Programming

bind() Exampleint mysock,err;struct sockaddr_in myaddr;

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

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

Socket Programming 27

Page 28: Lecture 4 Socket Programming

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

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

Client can bind to a specific port.

Client can ask the O.S. to assign any available port number.

Socket Programming 28

Page 29: Lecture 4 Socket Programming

Port schmort - who cares ? Clients typically don’t care what port they

are assigned.

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

myaddr.port = htons(0);

Socket Programming 29

Page 30: Lecture 4 Socket Programming

What is my IP address ? How can you find out what your IP

address is so you can tell bind() ?

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

interfaces?

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

Socket Programming 30

Page 31: Lecture 4 Socket Programming

IPv4 Address Conversionint inet_aton( char *, struct in_addr *);

Convert ASCII dotted-decimal IP address to network byte ordered 32 bit value.

Returns 1 on success, 0 on failure.

char *inet_ntoa(struct in_addr); Convert network byte ordered value to ASCII

dotted-decimal (a string).

Socket Programming 31

Page 32: Lecture 4 Socket Programming

IPv4 & IPv6 Address Conversion

int inet_pton( int, const char *, void *); (family, string_ptr, address_ptr) Convert IP address string to network byte ordered

32 or 128 bit value. Returns 1 on success, -1 on failure, 0 on invalid input

char *inet_ntop(int,const void*,char*,size_t); (family, address_ptr, string_ptr, length) Convert network byte ordered value to IP address

string• x:x:x:x:x:x:x:x or x:x:x:x:x:x:a.b.c.d

Socket Programming 32

Page 33: Lecture 4 Socket Programming

Other socket system calls

General Use read() write() close()

• Connection-oriented (TCP)– connect()– listen()– accept()

• Connectionless (UDP)– send()– recv()

Socket Programming 33