210 likes | 220 Views
CS 5565 Network Architecture and Protocols. Godmar Back. Lecture 15. Announcements. Problem Set 1 hopefully graded by end of this week Project 1A due Wed Feb 22 11:59pm Will try for quick turn-around time Problem Set 2 due Mar 4 12:20pm Project 1B due date & midterm after Spring Break
E N D
CS 5565Network Architecture and Protocols Godmar Back Lecture 15
Announcements • Problem Set 1 hopefully graded by end of this week • Project 1A due Wed Feb 22 11:59pm • Will try for quick turn-around time • Problem Set 2 due Mar 4 12:20pm • Project 1B due date & midterm after Spring Break • Office hours this week MWR 3-4pm CS 5565 Spring 2006
Wrong: main { s = socket f = open file get file size udp_send fsize to s while read from f udp_send to s } // no layering at all! Also wrong: main { f = ftp::init() f.transfer(file) } ftp :: transfer(file) s = socket f = open file get file size udp_send fsize to s while read data from f udp_send data to s } // no transport layer between file transfer service and UDP! Layering – The Wrong Way CS 5565 Spring 2006
Application Protocols Part 3: XMPP Slides by John Linford & Rahul Agarwal
XMPP • Extensible Messaging and Presence Protocol • A protocol for streaming XML elements in close to real time between any two network endpoints • Provides a generalized, extensible framework for exchanging XML data • Mainly used in instant messaging and presence applications (Jabber) RFC 3920: XMPP Core. http://www.xmpp.org/specs/rfc3920.html. CS 5565 Spring 2006
openXMPP • A modular, multi-platform standards-compliant XMPP library • Provides required functionality listed in RFC 3920 and RFC 3921: • XML streams • XML stanzas • TLS stream encryption • SASL authentication • Resource binding • Roster and subscription management • Internationalization (*) • Conversation threads (*) • Directed presence information (*) • New account registration (*) • Library: 3601 lines, 1379 statements • Project: 5825 lines, 2304 statements C1----S1---S2---C3 | C2----+--G1===FN1===FC1 * Optional RFC functionality CS 5565 Spring 2006
XMPP session establishment Offline Connected StartingTLS Connected StartingSASL StartingSession LoggedIn Client Server <stream:features> <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'> <required/> </starttls> <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'> <mechanism>DIGEST-MD5</mechanism> <mechanism>PLAIN</mechanism> </mechanisms> </stream:features> <stream:features> <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'> <required/> </starttls> <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'> <mechanism>DIGEST-MD5</mechanism> <mechanism>PLAIN</mechanism> </mechanisms> </stream:features> <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN"> amNsLm9wZW5YTVBQQGdtYWlsLmNvbQBqY2wu b3BlblhNUFAAT3BFblhtUHA= </auth> <iq type="set" id="openXMPP_luxqnoei1"> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"> <resource>openXMPP</resource> </bind> </iq> <iq id="openXMPP_luxqnoei1" type="result" xmlns="jabber:client"> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"> <jid>somenode@example.com/openXMPP128A956D</jid> </bind> </iq> <iq type="set" id="openXMPP_luxqnoei2"> <session xmlns="urn:ietf:params:xml:ns:xmpp-session" /> </iq> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <?xml version='1.0'?> <stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'> <iq type="result" id="openXMPP_luxqnoei2" xmlns="jabber:client" /> <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/> <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/> <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" /> CS 5565 Spring 2006
State Diagram CS 5565 Spring 2006
XMPP • Subsequently exchange XML stanzas • For presence management • For roster management • For sending messages • See Java Example on Class Website & Homework 2 CS 5565 Spring 2006
Summary Application Protocols • Request/Reply pattern pervasive • Persistent vs. Nonpersistent Connections • Simplicity! • Few states, if any • Stateless protocols are used where possible • Human-readable message formats often preferred (despite overhead) • Including XML CS 5565 Spring 2006
Socket API Part 2: TCP Sockets & TCP Demultiplexing
TCP Sockets • Provide reliable byte-stream abstraction • In-order, reliable delivery of bytes • Connection-oriented • Client must connect(2) • Server performs “passive open” using accept(2) CS 5565 Spring 2006
TCP Sockets: Overview socket() listen() socket() Left side: client Right side: server connection setup connect() accept() bind() write() read() read() write() connection shutdown close() close() CS 5565 Spring 2006
connect(2) int connect(int sockfd, const struct sockaddr *peeraddr, int addrlen) • sockfd: returned by socket() • peeraddr: peer’s address (type sockaddr_in) • this initiates hand-shake with server CS 5565 Spring 2006
listen(2), accept(2) int listen(int s, int backlog) int accept(int s, struct sockaddr *addr, int *addrlen); • addr: accepted peer’s (aka client) address • of type sockaddr_in • listen() must precede accept • No network traffic, but informs OS to start queuing connection requests • accept() returns new socket • But does not assign new port – why not? CS 5565 Spring 2006
socket() bind(*, 80) listen(5) connect(10.0.0.1,80) socket() S: 10.0.0.3:512 S: 10.0.0.2:2047D: 10.0.0.1:80 S: 10.0.0.3:512 D: 10.0.0.1:80 10.0.0.1:80 ???D: 10.0.0.1:80 S: 10.0.0.1:80D: 10.0.0.2:3045 accept() S: 10.0.0.1:80D: 10.0.0.2:2047 bind(10.0.0.3,512) S: 10.0.0.1:80D: 10.0.0.3:512 connect(10.0.0.1,80) TCP Demultiplexing 10.0.0.1 10.0.0.2 S: 10.0.0.2:3045D: 10.0.0.1:80 10.0.0.3 CS 5565 Spring 2006
Utility Functions in_addr_t inet_addr(const char *cp); char *inet_ntoa(struct in_addr in); int gethostname(char *name, size_t len); int getpeername(int s, struct sockaddr *name, socklen_t *namelen); int getsockname(int s, struct sockaddr *name, socklen_t *namelen); int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen); int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); struct hostent *gethostbyname(const char *name); struct hostent *gethostbyaddr(const char *addr, int len, int type); int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); void freeaddrinfo(struct addrinfo *res); CS 5565 Spring 2006
struct hostent struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } Note: IPv4 addresses, *h_addr_list points to array of long (32-bit) Struct hostent *hent = gethostbyname(hostname); If (hent == 0) { herror(hostname); exit(-1); } addr = ((long*)*hent->h_addr_list)[0]; extracts first IP for host. As always, in network order! CS 5565 Spring 2006
Common Pitfalls (3) • What is wrong with this code? struct sockaddr_in server1addr; struct sockaddr_in server2addr; /* not shown: initialize server1addr, server2addr */ printf(“using server 1 at %s and server 2 at %s\n”, inet_ntoa(serveraddr1.sin_addr), inet_ntoa(serveraddr2.sin_addr)); Beware of statically allocated buffers! Use alternatives where available: inet_ntop, getaddrinfo, etc. CS 5565 Spring 2006
Java Binding • Does not expose byte order • gethostbyname() is hidden (use a “String” as a hostname to get default 1st IP address) • Does not expose bind/listen directly • Use different types for different sockets: • DatagramSocket, Socket, ServerSocket • Does not expose universal file descriptor CS 5565 Spring 2006
Summary • Application Protocols • Layer 4 Addressing • IP Address, Port • Demultiplexing different for UDP/TCP • Socket Programming API CS 5565 Spring 2006