Carnegie Mellon 1 Network(Programming(15#213:'Introduc0on'to'Computer'Systems'20th'Lecture,'Nov.'2,'2010'Instructors:''Randy'Bryant'and'Dave'O’Hallaron'Carnegie Mellon 2 Last(Time:(Client8Server(Transac;on(Client(process(Server(process(1.#Client#sends#request#2.#Server##handles#request#3.#Server#sends#response#4.#Client##handles#response#Resource(Note:#clients#and#servers#are#processes#running#on#hosts##(can#be#the#same#or#different#hosts)#Carnegie Mellon 3 Last(Time:(Logical(Structure(of(an(internet(router(router(router(router(router(router(host(host(Carnegie Mellon 4 Internet(Connec;ons( Clients(and(servers(communicate(by(sending(streams(of(bytes(over(connecAons:( Point#to#point,'full#duplex'(2#way'communica0on),'and'reliable.' A#socket(is(an(endpoint(of(a(connec;on( Socket'address'is'an'IPaddress:port''pair' A(port(is(a(168bit(integer(that(iden;fies(a(process:( Ephemeral#port:(Assigned'automa0cally'on'client'when'client'makes'a'connec0on'request' WellFknown#port:#Associated'with'some'service'provided'by'a'server'(e.g.,'port'80'is'associated'with'Web'servers)' A(connec;on(is(uniquely(iden;fied(by(the(socket(addresses(of(its(endpoints((socket#pair)( (cliaddr:cliport, servaddr:servport)Carnegie Mellon 5 PuJng(it(all(Together:((Anatomy(of(an(Internet(Connec;on(Connec;on(socket(pair((128.2.194.242:51213,(208.216.181.15:80)(Server((port(80)(Client(Client#socket#address#128.2.194.242:51213(Server#socket#address#208.216.181.15:80(Client(host(address(128.2.194.242(!Server(host(address(208.216.181.15(51213(is'an'ephemeral'port''allocated'by'the'kernel''80(is'a'well#known'port'associated'with'Web'servers'Carnegie Mellon 6 Clients( Examples(of(client(programs( Web'browsers,'ftp,(telnet,(ssh How(does(a(client(find(the(server?( The'IP'address'in'the'server'socket'address'iden0fies'the'host!!!(more'precisely,'an'adapter'on'the'host)' The'(well#known)'port'in'the'server'socket'address'iden0fies'the'service,'and'thus'implicitly'iden0fies'the'server'process'that'performs'that'service.' Examples'of'well'know'ports' Port'7:'Echo'server' Port'23:'Telnet'server' Port'25:'Mail'server' Port'80:'Web'server'Carnegie Mellon 7 Using(Ports(to(Iden;fy(Services(Web(server((port(80)(Client(host(Server(host(128.2.194.242(Echo(server((port(7)(Service(request(for(128.2.194.242:80((i.e.,(the(Web(server)(Web(server((port(80)(Echo(server((port(7)(Service(request(for(128.2.194.242:7((i.e.,(the(echo(server)(Kernel(Kernel(Client(Client(Carnegie Mellon 8 Servers( Servers(are(long8running(proce sses((daemons)( Created'at'boot#0me'(typically)'by'the'init'process'(process'1)' Run'con0nuously'un0l'the'machine'is'turned'off' Each(server(waits(for(requests(to(arrive(on(a(well8known(port(associated(with(a(par;cular(service( Port'7:'echo'server' Port'23:'telnet'server' Port'25:'mail'server' Port'80:'HTTP'server' A(machine(that(runs(a(server(process(is(also(o\en(referred(to(as(a(“server”(Carnegie Mellon 9 Server(Examples( Web(server((port(80)( Resource:'files/compute'cycles'(CGI'programs)' Service:'retrieves'files'and'runs'CGI'programs'on'behalf'of'the'client' FTP(server((20,(21)( Resource:'files' Service:'stores'and'retrieve'files' Telnet(server((23)( Resource:'terminal' Service:'proxies'a'terminal'on'the'server'machine' Mail(server((25)( Resource:'email'“spool”'file' Service:'stores'mail'messages'in'spool'file''See(/etc/services(for(a(comprehensive(list(of(the(port(mappings(on(a(Linux(machine(Carnegie Mellon 10 Sockets(Interface( Created(in(the(early(80’s(as(part(of(the(original(Berkeley(distribu;on(of(Unix(that(contained( an(early(version(of(the(Internet(protocols( Provides(a(user8level(interface(to(the(network( Underlying(basis(for(all(Internet(applica;ons( Based(on(client/server( programming(model(Carnegie Mellon 11 Sockets( What(is(a(socket?( To'the'kernel,'a'socket'is'an'endpoint'of'communica0on' To'an'applica0on,'a'socket'is'a'file'descriptor'that'lets'the'applica0on'read/write'from/to'the'network' Remember:'All'Unix'I/O'devices,'including'networks,'are'modeled'as'files' Clients(and(servers(communicate(with(each(other(by(reading(from(and(wri;ng(to(socket(descriptors( The(main(dis;nc;on(between(regular(file(I/O(and(socket(I/O(is(how(the(applica;on(“opens”(the(socket(descriptors(Client(clientfd Server(serverfdCarnegie Mellon 12 Example:(Echo(Client(and(Server(greatwhite> ./echoserveri 15213 On(Server(On(Client(Connection closed server received 12 bytes server connected to BRYANT-TP4.VLSI.CS.CMU.EDU (128.2.213.29), port 64690 echo: HELLO THERE type: ^D type: hello there linux> echoclient greatwhite.ics.cs.cmu.edu 15213Carnegie Mellon 13 Watching(Echo(Client(/(Server(Carnegie Mellon 14 Ethical(Issues( Packet(Sniffer( Program'that'records'network'traffic'visible'at'node' Promiscuous'mode:'Record'traffic'that'does'not'have'this'host'as'source'or'des0na0on' University(Policy(Network Traffic: Network traffic should be considered private. Because of this, any "packet sniffing", or other deliberate attempts to read network information which is not intended for your use will be grounds for loss of network privileges for a period of not less than one full semester. In some cases, the loss of privileges may be permanent. Note that it is permissable to run a packet sniffer explicitely configured in non-promiscuous mode (you may sniff packets going to or from your machine). This allows users to explore aspects of networking while protecting the privacy of others.(Carnegie Mellon 15 Client(/(Server(Session(Overview(of(the(Sockets(Interface(Client# Server#socket socket bind listen rio_readlineb rio_writen rio_readlineb rio_writen Connec;on(request(rio_readlineb close close EOF(Await(connec;on(request(from(next(client(open_listenfd open_clientfd accept connectCarnegie Mellon 16 Socket(Address(Structures( Generic(socket(address:( For'address'arguments'to'connect,'bind,'and'accept Necessary'only'because'C'did'not'have'generic'(void *)'pointers'when'the'sockets'interface'was'designed'struct sockaddr { unsigned short sa_family; /* protocol family */ char sa_data[14]; /* address data. */ }; sa_family Family(Specific(Carnegie Mellon 17
View Full Document