Network Programming November 21, 2007Packet SniffingA Client-Server TransactionA Programmer’s View of the Internet1. IP Addresses2. Domain Naming System (DNS)3. Internet ConnectionsClientsUsing Ports to Identify ServicesServersServer ExamplesSockets InterfaceSocketsOverview of the Sockets InterfaceSocket Address StructuresSocket Address StructuresExample: Echo Client and ServerEcho Client Main RoutineEcho Client: open_clientfdEcho Client: open_clientfd (socket)Echo Client: open_clientfd (gethostbyname)A Careful Look at bcopy ArgumentsEcho Client: open_clientfd (connect)Echo Server: Main RoutineEcho Server: open_listenfdEcho Server: open_listenfd (cont)Echo Server: open_listenfd (socket)Echo Server: open_listenfd (setsockopt)Echo Server: open_listenfd (initialize socket address)Echo Server: open_listenfd (bind)Echo Server: open_listenfd (listen)Echo Server: Main LoopEcho Server: acceptEcho Server: accept IllustratedConnected vs. Listening DescriptorsEcho Server: Identifying the ClientEcho Server: echoTesting Servers Using telnetTesting the Echo Server With telnetFor More InformationNetwork ProgrammingNovember 21, 2007Network ProgrammingNovember 21, 2007TopicsTopics Peeking at Internet traffic Programmer’s view of the Internet (review) Sockets interface Writing clients and servers Understanding protocol15-213class23.ppt–2–15-213, F’07Packet SniffingPacket SniffingProgram That Records Network Traffic Visible at NodeProgram That Records Network Traffic Visible at Node Promiscuous Modez Record traffic that does not have this host as source or destination–3–15-213, F’07A Client-Server TransactionA Client-Server TransactionClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceMost network applications are based on the clientMost network applications are based on the client--server model:server model: A server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients.Note: clients and servers are processes running on hosts (can be the same or different hosts).–4–15-213, F’07A Programmer’s View of the InternetA Programmer’s View of the Internet1. Hosts are mapped to a set of 321. Hosts are mapped to a set of 32--bit bit IP addressesIP addresses.. 128.2.203.1792. The set of IP addresses is mapped to a set of 2. The set of IP addresses is mapped to a set of identifiers called Internet identifiers called Internet domain namesdomain names.. 128.2.203.179 is mapped to www.cs.cmu.edu3. A process on one Internet host can communicate 3. A process on one Internet host can communicate with a process on another Internet host over a with a process on another Internet host over a connectionconnection..–5–15-213, F’071. IP Addresses1. IP Addresses3232--bit IP addresses are stored in an bit IP addresses are stored in an IP address IP address structstruct IP addresses are always stored in memory in network byte order (big-endian byte order) True in general for any integer transferred in a packet header from one machine to another.z E.g., the port number used to identify an Internet connection./* Internet address structure */struct in_addr {unsigned int s_addr; /* network byte order (big-endian) */};Handy network byte-order conversion functions:htonl: convert long int from host to network byte order.htons: convert short int from host to network byte order.ntohl: convert long int from network to host byte order.ntohs: convert short int from network to host byte order.–6–15-213, F’072. Domain Naming System (DNS)2. Domain Naming System (DNS)The Internet maintains a mapping between IP addresses The Internet maintains a mapping between IP addresses and domain names in a huge worldwide distributed and domain names in a huge worldwide distributed database called database called DNSDNS.. Conceptually, programmers can view the DNS database as a collection of millions of host entry structures:Functions for retrieving host entries from DNS:Functions for retrieving host entries from DNS: gethostbyname: query key is a DNS domain name. gethostbyaddr: query key is an IP address./* DNS host entry structure */ struct hostent { char *h_name; /* official domain name of host */ char **h_aliases; /* null-terminated array of domain names */ int h_addrtype; /* host address type (AF_INET) */ int h_length; /* length of an address, in bytes */ char **h_addr_list; /* null-terminated array of in_addr structs */ };–7–15-213, F’073. Internet Connections3. Internet ConnectionsConnection socket pair(128.2.194.242:51213, 208.216.181.15:80)Server(port 80)ClientClient socket address128.2.194.242:51213Server socket address208.216.181.15:80Client host address128.2.194.242Server host address208.216.181.15Clients and servers communicate by sending streams Clients and servers communicate by sending streams of bytes over of bytes over connectionsconnections..Connections are pointConnections are point--toto--point, fullpoint, full--duplex (2duplex (2--way way communication), and reliable.communication), and reliable.Note: 51213 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web servers–8–15-213, F’07ClientsClientsExamples of client programsExamples of client programs Web browsers, ftp, telnet, sshHow does a client find the server?How does a client find the server? The IP address in the server socket address identifies the host (more precisely, an adapter on the host) The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. Examples of well know portsz Port 7: Echo serverz Port 23: Telnet serverz Port 25: Mail serverz Port 80: Web server–9–15-213, F’07Using Ports to Identify ServicesUsing Ports to Identify ServicesWeb server(port 80)Client hostServer host 128.2.194.242Echo server(port 7)Service request for128.2.194.242:80(i.e., the Web server)Web server(port 80)Echo server(port 7)Service request for128.2.194.242:7(i.e., the echo server)KernelKernelClientClient–10–15-213, F’07ServersServersServers are longServers are long--running processes (daemons).running processes (daemons). Created at boot-time (typically) by the init process (process 1) Run continuously until the machine is turned off.Each server waits for
View Full Document