Process Process Process Layer CSCE 515: Computer Network TCP UDP Transport Layer Programming ------Sockets ICMP, ARP & IP Network Layer Wenyuan Xu RARP
Department of Computer Science and Engineering University of South Carolina 802.3 Data-Link Layer
9/8/2008 CSCE515 – Computer Network Programming
Network API Network API
API - Application Programming Interface API is a set of functionality/services delivered by a Internet OSI model Application programming system. protocol suite details Application User processor Presentation Application Network API Session The services ( often provided by the operating system) Transport TCP UDP that provide the interface between application and Network IPv4, IPv6 protocol software. Data link Data link kernel Physical Physical Communications details
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Network API wish list TCP/IP
Generic Programming Interface. TCP/IP does not include an API definition. Support multiple communication protocol suites (families). Address (endpoint) representation independence. There are a variety of APIs for use with Provide special services for Client and Server? TCP/IP: Sockets by Berkeley Support for message oriented and connection XTI (X/Open Transport Interface) by AT&T oriented communication. Winsock - Windows Sockets API by Microsoft Work with existing I/O services (when this MacTCP / Open Transport by Apple makes sense). Operating System independence
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Client-Server Model Functions needed:
Client 1 Specify local and remote communication Server Client 2 endpoints Initiate a connection Client 3 Wait for incoming connection
One side of communication is client, and the other side Send and receive data is server Terminate a connection gracefully Server waits for a client request to arrive Server processes the client request and sends the Error handling response back to the client Iterative or concurrent
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Berkeley Sockets Elements of a Socket
A socket is an abstract representation of a Each socket can be uniquely identified by communication endpoint. Source IP address Generic: Source port number support for multiple protocol families. address representation independence Destination IP address Destination port number Sockets (obviously) have special needs: An end-to-end protocol (TCP or UDP) establishing a connection specifying communication endpoint addresses
Sockets work with Unix I/O services just like files, pipes & FIFOs
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Types of Sockets Stream Sockets
Two different types of sockets Also known as connection-oriented socket Stream sockets Use TCP Datagram sockets Provide reliable, connected networking service Error free; no out-of-order packets Applications: telnet, ssh, http
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Datagram Sockets Unix Descriptor Table
Also known as connectionless socket Descriptor Table Use UDP Data structure for file 0 Provide unreliable, best-effort networking 0 service 1 Data structure for file 1 Packets may be lost; may arrive out of 2 order 3 Data structure for file 2 Applications: streaming audio/video 4 int fd; int cc, nbytes; char *buf;
fd = open (my_filename, O_RDONLY ); cc = write (fd, buf, nbytes); cc = read (fd, buf, nbytes);
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Socket Descriptor Data Structure Client-Server Model
Server Descriptor Table Create a socket with the socket() system call Family: PF_INET 0 Family: PF_INET Bind the socket to an address using the bind() system Service:Service: SOCK_STREAMSOCK_STREAM call. For a server socket on the Internet, an address 1 LocalLocal IP:IP: 111.22.3.4111.22.3.4 consists of a port number on the host machine. RemoteRemote IP:IP: 123.45.6.78123.45.6.78 2 Listen for connections with the listen() system call LocalLocal Port:Port: 22492249 3 RemoteRemote Port:Port: 37263726 Accept a connection with the accept() system call. This call typically blocks until a client connects with 4 the server. int s, family, type, protocol; Send and receive data s = socket(family, type, protocol); etc... cc = read(s, buf, nbytes);
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Client-Server Model Creating a Socket
Client int socket(int family,int type,int proto); Create a socket with the socket() system call specifies the protocol family Connect the socket to the address of the family AF_INET: IPv4 protocols server using the connect() system call AF_INET6: IPv6 protocols AF_ROUTE: Routing sockets Send and receive data. There are a number type specifies the type of service SOCK_STREAM of ways to do this, but the simplest is to use SOCK_DGRAM the read() and write() system calls. SOCK_RAW protocol specifies the specific protocol (usually 0, which means the default). IPPROTO_TCP: TCP transport protocol IPPROTO_UDP: UDP transport protocol
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming socket() Specifying an Endpoint Address
The socket() system call returns a Remember that the sockets API is generic socket descriptor (small integer) or -1 on error. There must be a generic way to specify endpoint addresses.
socket() allocates resources needed for TCP/IP requires an IP address and a port a communication endpoint - but it does not number for each endpoint address. deal with endpoint addressing.
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Necessary Background Information: bind() POSIX data types calling bind() assigns the address specified by the sockaddr structure to the int8_t signed 8bit int socket descriptor. uint8_t unsigned 8 bit int int16_t signed 16 bit int bind( mysock, (struct sockaddr*) &myaddr, uint16_t unsigned 16 bit int sizeof(myaddr) ); int32_t signed 32 bit int uint32_t unsigned 32 bit int
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
More POSIX data types Generic socket addresses el rn ke y sa_family_t address family struct sockaddr { b ed s socklen_t length of struct uint8_t sa_len; U in_addr_t IPv4 address sa_family_t sa_family; char sa_data[14]; in_port_t IP port number };
sa_family specifies the address type. sa_data specifies the address value.
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming sockaddr AF_CSCE515
An address that will allow me to use sockets Initializing a sockaddr structure to point to communicate with you. to Henry : address type AF_CSCE515 address values: struct sockaddr henry; Dean 1 Sayan 6 Devon 2 Yuliya 7 henry.sa_family = AF_CSCE515; Samuel 3 Razvan 8 henry.sa_data[0] = 5; Shamik 4 Mythri 9 Henry 5 Femitolu 10
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
AF_INET struct sockaddr_in (IPv4)
For AF_CSCE515 we only needed 1 byte struct sockaddr_in { to specify the address. uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; For AF_INET we need: ly! on struct in_addr sin_addr; 16 bit port number v4 IP char sin_zero[8]; 32 bit IP address }; A special kind of sockaddr structure
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
struct in_addr Byte Ordering struct in_addr { Different computer architectures use in_addr_t s_addr; different byte ordering to represent }; multibyte values. 16 bit integer: in_addr just provides a name for the ‘C’ type associated with IP addresses. Low Byte Address A High Byte High Byte Address A+1 Low Byte
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Byte Ordering Byte Order and Networking
Little-Endian Big-Endian Suppose a Big Endian machine sends a Low Byte High Byte High Byte Low Byte 16 bit integer with the value 2: Addr A Addr A+1 Addr A Addr A+1 0000000000000010
IBM 80x86 IBM 370 A Little Endian machine will think it got DEC VAX Motorola 68000 the number 512: DEC PDP-11 Sun 0000001000000000
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Network Byte Order Network Byte Order
All values stored in a sockaddr_in must Conversion of application-level data is be in network byte order. left up to the presentation layer. sin_port a TCP/IP port number. But hold on !!! How do lower level layers sin_addr an IP address. communicate if they all represent values differently ? (data length fields in headers) A fixed byte order is used (called network byte order) for all control data.
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Network Byte Order Functions TCP/IP Addresses
We don’t need to deal with sockaddr ‘h’ : host byte order ‘n’ : network byte order structures since we will only deal with a real protocol family. ‘s’ : short (16bit) ‘l’ : long (32bit)
We can use structures. uint16_t htons(uint16_t); sockaddr_in uint16_t ntohs(uint_16_t); BUT: The C functions that make up the sockets uint32_t htonl(uint32_t); API expect structures of type sockaddr. uint32_t ntohl(uint32_t); int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Assigning an address to a socket sockaddr sockaddr_in
sa_lensa_len sin_lensin_len The bind() system call is used to assign sa_familysa_family AF_INET an address to an existing socket. sin_port int bind( int sockfd, sin_addr const struct sockaddr *myaddr, sa_datasa_data const! int addrlen);
sin_zero bind returns 0 if successful or -1 on error.
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
bind() Example Uses for bind() int mysock,err; struct sockaddr_in myaddr; Why no htons/htosl? There are a number of uses for bind(): mysock = socket(PF_INET,SOCK_STREAM,0); Server would like to bind to a well known myaddr.sin_family = AF_INET; address (port number). myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress); Client can bind to a specific port. err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); Client can ask the OS to assign any available port number.
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Port schmo - who cares ? What is my IP address ?
Clients typically don’t care what port they are How can you find out what your IP address is so assigned. you can tell bind() ?
When you call bind you can tell it to assign you There is no realistic way for you to know the
any available port: Why htons? 0 is 1 byte right IP address to give bind() - what if the computer has multiple network interfaces? myaddr.port = htons(0); specify the IP address as: INADDR_ANY, this
1-1024: reserved port (assigned by tells the OS to take care of things. 1 byte, Why htonl? privileged processes) myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming IPv4 Address Conversion Client-Server Communication (TCP) int inet_aton( char *, struct in_addr *); socket() int socket(int family, int type, int protocol); TCP Server bind() well-known port Convert ASCII dotted-decimal IP address to int bind(int sockfd, struct sockaddr *my_addr, int addrlen); TCP Client listen() int listen(int sockfd, int backlog); network byte order 32 bit value. Returns 1 int socket(int family, int type, int protocol); socket() int accept(int sockfd, void *addr, int *addrlen); accept() on success, 0 on failure. int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); connect() connection establishment blocks until connection from client data(re write() quest) char *inet_ntoa(struct in_addr); read() process request eply) data(r write() Convert network byte ordered value to read()
ASCII dotted-decimal (a string). end-of-f close() ile notification read() int close(int sockfd); int close(int sockfd); close() 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming
Other socket system calls Assignment & Next time
General Use • Connection-oriented Reading: read() (TCP) UNP1, 3** write() – connect() Socket Programming FAQ close() – listen() – accept() Next Lecture: TCP Details • Connectionless (UDP) – send() – recv()
9/8/2008 CSCE515 – Computer Network Programming 9/8/20082007 CSCE515 – Computer Network Programming