Lecture 10 Overview. Network API Application Programming Interface – Services that provide the...
-
Upload
bernard-perkins -
Category
Documents
-
view
219 -
download
3
Transcript of Lecture 10 Overview. Network API Application Programming Interface – Services that provide the...
Lecture 10 Overview
Network API
• Application Programming Interface– Services that provide the interface between
application and protocol software• often by the operating system
CPE 401/601 Lecture 10 : Socket Programming 2
ApplicationApplication
Network APINetwork API
Protocol AProtocol A Protocol BProtocol B Protocol CProtocol C
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 independence3CPE 401/601 Lecture 10 : 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 needs to– establishing a connection– specifying communication endpoint addresses
4CPE 401/601 Lecture 10 : Socket Programming
Creating a Socket
int 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
5CPE 401/601 Lecture 10 : 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
6CPE 401/601 Lecture 10 : 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
7CPE 401/601 Lecture 10 : Socket Programming
sockaddr
8
sa_lensa_family
sa_data
lengthAF_INET
port
addr
zero
sockaddrsockaddr sockaddr_insockaddr_insockaddr_in6sockaddr_in6length
AF_INET6port
Flow-label
Scope ID
addr
28 bytes
variable 16 bytes
CPE 401/601 Lecture 10 : 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;
};9
Length of structure (16)
AF_INET
16 bit Port number
32 bit IPv4 address
Make structure 16 bytes
CPE 401/601 Lecture 10 : Socket Programming
CPE 401/601 Lecture 10 : 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];
};
10
Length of structure (28)
AF_INET6
Port number
128 bit IPv6 address
Scope of address
Flow label
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
• uint16_t htons(uint16_t);• uint16_t ntohs(uint_16_t);• uint32_t htonl(uint32_t);• uint32_t ntohl(uint32_t);
11CPE 401/601 Lecture 10 : 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
12CPE 401/601 Lecture 10 : 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);
• You can give bind() a sockaddr_in structure:int bind( mysock,
(struct sockaddr*) &myaddr,
sizeof(myaddr) );
13CPE 401/601 Lecture 10 : 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));
14CPE 401/601 Lecture 10 : 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);
15CPE 401/601 Lecture 10 : 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.
16CPE 401/601 Lecture 10 : 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).
17CPE 401/601 Lecture 10 : 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– 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
18CPE 401/601 Lecture 10 : Socket Programming
Other socket system calls
• General Use– read()– write()– close()
19
• Connection-oriented (TCP)– connect()– listen()– accept()
• Connectionless (UDP)– send()– recv()
CPE 401/601 Lecture 10 : Socket Programming
Lecture 11
TCP and UDP Sockets
CPE 401 / 601
Computer Network Systems
slides are modified from Dave Hollingerslides are modified from Dave Hollinger
TCP Sockets Programming
• Creating a passive mode (server) socket
• Establishing an application-level connection
• send/receive data
• Terminating a connection
21CPE 401/601 Lecture 11 : TCP Socket Programming
TCP state diagram
Creating a TCP socketint socket(int family,int type,int proto);
– family: AF_INET, AF_INET6, AF_LOCAL, …– type: SOCK_STREAM, SOCK_DGRAM,
SOCK_SEQPACKET, SOCK_RAW– protocol: IPPROTO_TCP, IPPROTO_UDP,
IPPROTO_SCTP
int sock;
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock<0) { /* ERROR */ }
23CPE 401/601 Lecture 11 : TCP Socket Programming
Binding to well known addressint bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
int mysock;
struct sockaddr_in myaddr;
mysock = socket(PF_INET,SOCK_STREAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( 80 );
myaddr.sin_addr = htonl( INADDR_ANY );
bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));
24CPE 401/601 Lecture 11 : TCP Socket Programming
Establishing a passive mode TCP socket
• Passive mode:– Address already determined
• Tell the kernel to accept incoming connection requests directed at the socket address– 3-way handshake
• Tell the kernel to queue incoming connections for us
25CPE 401/601 Lecture 11 : TCP Socket Programming
listen()int listen(int sockfd, int backlog);
• sockfd is the TCP socket – already bound to an address
• backlog is the number of incoming connections the kernel should be able to keep track of (queue for us)– Sum of incomplete and completed queues
26CPE 401/601 Lecture 11 : TCP Socket Programming
Accepting an incoming connection
• Once we call listen(), the O.S. will queue incoming connections– Handles the 3-way handshake– Queues up multiple connections
• When our application is ready to handle a new connection– we need to ask the O.S. for the next connection
27CPE 401/601 Lecture 11 : TCP Socket Programming
accept()int accept( int sockfd,
struct sockaddr* cliaddr,
socklen_t *addrlen);
• sockfd is the passive mode TCP socket– initiated by socket(), bind(), and listen()
• cliaddr is a pointer to allocated space• addrlen is a value-result argument– must be set to the size of cliaddr– on return, will be set to be the number of used bytes in
cliaddr
28CPE 401/601 Lecture 11 : TCP Socket Programming
accept() return value
• accept() returns a new socket descriptor (small positive integer) or -1 on error
• After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls
• read() and write() operate a little differently on sockets!– vs. file operation!
29CPE 401/601 Lecture 11 : TCP Socket Programming
Terminating a TCP connectionint close(int sockfd);
• Either end of the connection can call the close() system call
• What if there is data being sent?
• If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.
30CPE 401/601 Lecture 11 : TCP Socket Programming
Client Code
• TCP clients can call connect() which:– takes care of establishing an endpoint address for
the client socket– Attempts to establish a connection to the
specified server• 3-way handshake
• no need to call bind first, the O.S. will take care of assigning the local endpoint address – TCP port number, IP address
31CPE 401/601 Lecture 11 : TCP Socket Programming
connect()int connect( int sockfd,
const struct sockaddr *server,
socklen_t addrlen);
• sockfd is an already created TCP socket• server contains the address of the server • connect() returns 0 if OK, -1 on error– No response to SYN segment (3 trials)– RST signal– ICMP destination unreachable (3 trials)
32CPE 401/601 Lecture 11 : TCP Socket Programming
Reading from a TCP socketint read(int fd, char *buf, int max);
• By default read() will block until data is available
• reading from a TCP socket may return less than max bytes– whatever is available
• You must be prepared to read data 1 byte at a time!
33CPE 401/601 Lecture 11 : TCP Socket Programming
Writing to a TCP socketint write(int fd, char *buf, int num);
• write might not be able to write all num bytes on a nonblocking socket
• readn(), writen() and readline() functions
34CPE 401/601 Lecture 11 : TCP Socket Programming
Metaphor for Good Relationships
• To succeed in relationships*:– you need to establish your own identity.– you need to be open & accepting.– you need to establish contacts.– you need to take things as they come, not as you
expect them.– you need to handle problems as they arise.
35
bind()
accept()
connect()
check for errors
read might return 1 byte
*Copyright Dr. Laura’s Network Programming Corp.
CPE 401/601 Lecture 11 : TCP Socket Programming
UDP Sockets
UDP Sockets Programming
• Creating UDP sockets– Client– Server
• Sending data
• Receiving data
• Connected Mode37CPE 401/601 Lecture 11: UDP Socket Programming
Creating a UDP socketint socket(int family, int type, int
proto);
int sock;
sock = socket(PF_INET, SOCK_DGRAM,0);
if (sock<0) { /* ERROR */ }
38CPE 401/601 Lecture 11: UDP Socket Programming
Binding to well known address
• typically done by server onlyint mysock;
struct sockaddr_in myaddr;
Mysock=socket(PF_INET,SOCK_DGRAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(1234);
myaddr.sin_addr = htonl(INADDR_ANY);
bind(mysock, &myaddr,
sizeof(myaddr));
39CPE 401/601 Lecture 11: UDP Socket Programming
Sending UDP Datagramsssize_t sendto( int sockfd, void *buff,
size_t nbytes, int flags,
const struct sockaddr* to,
socklen_t addrlen);
• sockfd is a UDP socket• buff is the address of the data (nbytes long)• to is the destination address• Return value is the number of bytes sent, – or -1 on error.
40CPE 401/601 Lecture 11: UDP Socket Programming
sendto()
• The return value of sendto() indicates how much data was accepted by the O.S. for sending as a datagram– not how much data made it to the destination.
• There is no error condition that indicates that the destination did not get the data!!!
• You can send 0 bytes of data!41CPE 401/601 Lecture 11: UDP Socket Programming
Receiving UDP Datagramsssize_t recvfrom( int sockfd, void *buff,
size_t nbytes, int flags,
struct sockaddr* from,
socklen_t *fromaddrlen);
• sockfd is a UDP socket• buff is the address of a buffer (nbytes long)• from is the address of a sockaddr• Return value is the number of bytes received
and put into buff, or -1 on error42CPE 401/601 Lecture 11: UDP Socket Programming
recvfrom()
• If buff is not large enough, any extra data is lost forever...
• You can receive 0 bytes of data!
• The sockaddr at from is filled in with the address of the sender
43CPE 401/601 Lecture 11: UDP Socket Programming
More recvfrom()
• recvfrom doesn’t return until there is a datagram available,– unless you do something special
• You should set fromaddrlen before calling
• If from and fromaddrlen are NULL we don’t find out who sent the data
44CPE 401/601 Lecture 11: UDP Socket Programming
Typical UDP client code
• Create UDP socket
• Create sockaddr with address of server
• Call sendto(), sending request to the server– No call to bind() is necessary!
• Possibly call recvfrom()– if we need a reply
45CPE 401/601 Lecture 11: UDP Socket Programming
Typical UDP Server code
• Create UDP socket and bind to well known address
• Call recvfrom() to get a request, noting the address of the client
• Process request and send reply back with sendto()
46CPE 401/601 Lecture 11: UDP Socket Programming
Typical UDP Communication
47CPE 401/601 Lecture 11: UDP Socket Programming
UDP Echo Serverint mysock;
struct sockaddr_in myaddr, cliaddr;
char msgbuf[MAXLEN];
socklen_t clilen;
int msglen;
mysock = socket(PF_INET,SOCK_DGRAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( S_PORT );
myaddr.sin_addr = htonl( INADDR_ANY );
bind(mysock, &myaddr, sizeof(myaddr));
while (1) {
clilen=sizeof(cliaddr);
msglen=recvfrom(mysock,msgbuf,MAXLEN,0, cliaddr,&clilen);
sendto(mysock,msgbuf,msglen,0,cliaddr,clilen);
}
48
NEED TO CHECK
FOR ERRORS!!!
CPE 401/601 Lecture 11: UDP Socket Programming