numbersendmail
sendmail 时间:2021-02-23 阅读:(
)
UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPShortCourseNotesAlanDix1996http://www.
hiraeth.
com/alan/tutorialsUNIXNetworkProgrammingwithTCP/IPCourseOutlineAlanDixhttp://www.
hcibook.
com/alanSession1InternetBasicsSession2FirstCodeSession3StandardApplicationsSession4BuildingClientsSession5ServersISession6ServersIISession7SecurityThreeinterrelatedaspects:rTCP/IPprotocolsuiterstandardInternetapplicationsrcodingusingUNIXsocketsAPIUNIXTCP/IPShortCourseNotesAlanDix19961UNIXNetworkProgrammingwithTCP/IPReadingBooks:1.
W.
RichardStevens,"TCP/IPIllustrated.
Vol.
1:Theprotocols",AddisonWesley,1994,(ISBN0-201-63346-9).
Explainstheprotocolsusingnetworkmonitoringtoolswithoutprogramming.
2.
DouglasE.
ComerandDavidL.
Stevens,"InternetworkingwithTCP/IP.
Vol.
3:Client-serverprogrammingandapplicationsBSDsocketversion",PrenticeHall,1993,(ISBN0-13-020272-X).
Goodbookaboutprinciplesofclient/serverdesign.
Assumesyouhavesomeknowledgeoratleastsomeotherreferenceforactualprogramming.
3.
MichaelSantifaller,translatedbyStephenS.
Wilson,"TCP/IPandONC/NFSinternetworkinginaUNIXenvironment",2ndEdition,AddisonWesley,1994,(ISBN0-201-42275-1).
Coversmoregroundlessdeeply.
TranslationfromGermanseemsgood.
4.
W.
RichardStevens,"UNIXNetworkProgramming",PrenticeHall,1990,(ISBN0-13-949876-1).
Aprogrammingbook.
I'mwaitingforacopy,butStevensisagoodwriterandthisbookisrecommendedbyotherauthors.
Seealso:yourlocalmanualpages(man2)RFCsRequestsforcomments(RFCs)thesearethedefinitionoftheInternetprotocolsobtainviaanonymousftpfromsun.
doc.
ic.
ac.
uk(193.
63.
255.
1)loginasanonymousgiveyouremailaddressaspasswordcdtorfcUNIXTCP/IPShortCourseNotesAlanDix19962UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPSession1InternetBasicsUNIXNetworkProgrammingwithTCP/IPSession1AlanDixhttp://www.
hcibook.
com/alanoriginsinternetsandtheInternetprotocollayersaddressingcommonapplicationsusingthemTCPandUDPportnumbersAPIsinformationcallsUNIXTCP/IPShortCourseNotesAlanDix19961OriginsDevelopmentofInternet&TCP/IP1968FirstproposalforARPANET–military&gov'tresearchContractedtoBolt,Beranek&Newman1971ARPANETentersregularuse1973/4redesignoflowerlevelprotocolsleadstoTCP/IP1983BerkeleyTCP/IPimplementationfor4.
2BSDpublicdomaincode1980srapidgrowthofNSFNET–broadacademicuse1990sWWWandpublicaccesstotheInternetTheInternetNowgrowingcommercialisationoftheInternet50,000networks6millionhosts30millionusersWWWdominatingInternetgrowthUNIXTCP/IPShortCourseNotesAlanDix19962internetsandtheInternetaninternetisacollectionofinterconnectednetworks(possiblydifferent)e.
g.
X25,AppleTalktheInternetisaparticularinternetwhichusestheTCP/IPprotocolsisglobalishardwareandnetworkindependentisnon-proprietaryinadditionsupportscommonlyusedapplicationspubliclyavailablestandards(RFCs)theInternetisnot(just)theweb!
UNIXTCP/IPShortCourseNotesAlanDix19963CharacteristicsoftheInternetTocommunicateyouneed:continuousconnectioncommonlanguagemeansofaddressingUNIXTCP/IPShortCourseNotesAlanDix19964GlobalConnectivityethernettokenringPPProutersroutersub-networkstarnetworklotsofnetworks:ethernet,FDDI,tokenringAppleTalk(itselfaninternet!
)etc.
etc.
etc.
connected(possiblyindirectly)toeachothertothecentral'ARPAnet'backboneintheUSprotocolscanbeusedinisolationbutisittheInternetUNIXTCP/IPShortCourseNotesAlanDix19965Protocols–theLanguageoftheInternetelectricalsignalslow-levelnetworks(e.
g.
ethernet)IPlayer(end-to-end)TCP/UDPlayerapplicationprotocols(e.
g.
FTP,telnet,http)applicationuserinterfaces(e.
g.
Fetch,mosaic)OSIICMP(controlandrouting)PhysicalLinkTransportNetworkSession,Presentation,Applicationroutersend-pointsTCP/IPStandardisation:RFC(requestforcomments)andDoDMILRFCsalsoinclude(definedbutnotrequired):PPP,ethernetpackaging,etc.
FTPandotherprotocolsUNIXTCP/IPShortCourseNotesAlanDix19966AddressingJ.
C.
T.
Jennings,LinburyCourtSchool,Dunhambury,Sussex,England,Europe,EasternHemisphere,Earth,nearMoon,SolarSystem,Space,nearMoreSpaceWithoutaddressescanonlybroadcastFourtypesofaddress:xlocationindependente.
g.
personalnamesyphysicallocatione.
g.
letteraddresseszlogicallocatione.
g.
organisationalhierarchy{routebasede.
g.
oldemailaddressesTwokindsofInternetaddress:IPaddress–typey(sortof)e.
g.
161.
12.
188.
167domainname–typeze.
g.
zeus.
hud.
ac.
ukextractfromJenningsGoestoSchool,AnthonyBuckeridge,Collins,1950.
UNIXTCP/IPShortCourseNotesAlanDix19967IPaddresses32bitinteger–2701966503Oftenrepresentedas4octets–161.
12.
188.
167Generalstructure:netid{sub-netid}hostidN.
B.
octetsdonotmapsimplyontocomponentsFiveclassesofIPaddress:ClassA0netidsubnet/hostid7bits24bitsClassB10netidsubnet/hostid14bits16bitsClassC110netidhostid21bits8bitsClassD&ClassE–experimentalhostidsmaydividedusingsubnetmaskrdifferentforeachmajornetwork(netid)needstobesetforeachmachineonnetworkUNIXTCP/IPShortCourseNotesAlanDix19968Domainnameshumanreadablenames.
.
.
.
.
oratleastASCII!
Heirarchical(roughlyorganisational)zeus.
hud.
ac.
ukuk–UnitedKingdomac–academichud–huddersfieldzeus–localmachineN.
B.
USAisimplicit–cs.
washington.
eduDecentralisedadministrationMappingfromnametoIPaddress–domainnameserversalsoreversemappingCAPI:gethostbyname–name→IPaddressgethostbyaddr–IPaddress→nameUNIXTCP/IPShortCourseNotesAlanDix19969CommonapplicationsFTP(filetransferprotocol)SMTP(simplemailtransferprotocol)telnet(remotelogins)rlogin(simpleremoteloginbetweenUNIXmachines)WorldWideWeb(builtonhttp)NFS(networkfilingsystem–originallyforSUNs)TFTP(trivialfiletransferprotocol–usedforbooting)SNMP(simplenetworkmanagementprotocol)gIneachcaseprotocolsaredefinedgUserinterfacesdependonplatform(whererelevant)UNIXTCP/IPShortCourseNotesAlanDix199610Handsonconnecttozeususingtelenet:%telnetzeus.
hud.
ac.
uklogin:c5.
.
.
etc.
whathappensifyoujustsay"telnetzeus"whatiszeus'IPaddresstry"telnetaa.
bb.
cc.
dd"(where'aa.
bb.
cc.
dd'iszeus'IPaddress)connecttozeususingftp:%ftpzeus.
hud.
ac.
ukconnectasyourselfandthenasanonymousReadbetweenthelinesUNIXTCP/IPShortCourseNotesAlanDix199611NetworkcommunicationsCommunicationcanbe:ConnectionlessraddresseverymessageglikelettersConnectionbasedruseaddresstoestablishafixedlinkrsendeachmessageusingthelinkgliketelephoneN.
B.
bothneedanaddresssomesortofsystemaddressbookor,publiclyknownaddressesUNIXTCP/IPShortCourseNotesAlanDix199612Networkcommunications–2Otherissues:ReliabilityDoallmessagesarriveDotheyarriveintherightorderBufferingeffectsresponsivenesshidespotentialdeadlockMessagesorbyte-streamsent:write1(len=26):"abcde.
.
.
.
vwxyz"write2(len=10):"0123456789"received:read1(len=20):"abcde.
.
.
.
qrst"read2(len=16):"uvwxyz012.
.
.
89"fixedlengthmessagesorprefixwithlengthUNIXTCP/IPShortCourseNotesAlanDix199613IP–thefundamentalInternetprotocolpointtopointrbetweenmachinesraddressedusingIPaddressmessage(packet)basedunreliablernetworkfailuresrrouterbuffersfillupdynamicroutingordermaybelostheterogeneousintermediatenetworksfragmentationUNIXTCP/IPShortCourseNotesAlanDix199614TCP&UDPBothbuiltontopofIPaddressedusingportnumbersprocesstoprocess(onUNIXplatforms)TCPconnectionbasedreliablebytestreamusedin:FTP,telnet,http,SMTPUDPconnectionlessunreliabledatagram(packetbased)usedin:NFS,TFTPUNIXTCP/IPShortCourseNotesAlanDix199615Portnumbers16bitintegersuniquewithinamachinetoconnectneedIPaddress+portnoTCPconnectiondefinedbyIPaddress&portofserver+IPaddress&portofclientUNIXport1023)N.
B.
different'name'spacesforTCP&UDPUNIXTCP/IPShortCourseNotesAlanDix199617API–thelanguageoftheprogrammerApplicationProgrammerInterfacesNotpartoftheInternetstandard–butveryimportant!
AstoryaboutDOSTCP/IPstackssuppliedbydifferentvendorsdifferentdevicedriversdifferentAPIschaosAPIsdependonplatform:UNIX–sockets(originalBerkleysystemcalls)–TLI(transportlayerinterface)AppleMac–MacTCPMSWindows–WinSock(similartosockets)UNIXTCP/IPAPIarekernelsystemcallsMac&Windowsareextensions/drivers(+DLL)UNIXTCP/IPShortCourseNotesAlanDix199618Handsoncopyskeleton.
cfromtcpdirectoryedittomaketwoprograms:getid.
c–returnsIPaddressofmachinegetname.
c–returnsnameofmachineusethefollowingCcalls:gethostid()returns(lonunsigned)integerresultgethostname(buff,len)returnserrorcodeputsnameintobuff(maximumlenbytes)ifyouhavetime,playwithtelnetondifferentports%telnetzeus.
hud.
ac.
ukport_noUNIXTCP/IPShortCourseNotesAlanDix199619UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPSession2FirstCodeUNIXNetworkProgrammingwithTCP/IPSession2AlanDixhttp://www.
hcibook.
com/alanfeaturesofsocketsAPIestablishingTCPconnectionssimpleclient/serverprogramuseitread&writewithsocketswrapperfunctionswhattheydoanechoserverUNIXTCP/IPShortCourseNotesAlanDix19962/1SocketsdevelopedforBerkeleyUNIXrrecallearlyBerkeleyTCP/IPimplementationrfirstdeliveredwithBSD2.
1centralfeaturesrcentralabstraction-thesocket-anend-pointlikeanelectricalconnectorrnotTCP/IPspecific(e.
g.
UNIXnamedpipes)rusesnormalread/writesystemcallsrsocketsassociatedwithUNIXfiledescriptorsbutsomenotfornormalI/OrsomeextrasystemcallssitsmorecomfortablywithTCPthanwithUDPbecauseofbyte-streamnatureofUNIXI/OspecialUDPfunctionse.
g.
,recv(.
.
.
)–acceptsaUDPdatagramadditionalnon-socketfunctionse.
g.
,gethostbyname(.
.
.
)–domainnameserverUNIXTCP/IPShortCourseNotesAlanDix19962/2EstablishingaTCPConnectionInitialStatedifferentprocesses151.
100.
17.
25differentprocesses161.
112.
192.
5differentprocesses161.
112.
4.
3InternetTCPisconnectionbased.
.
.
establishingitisacomplexmultistageprocessinitiallyallmachinesarethesamenospecial'server'machinesthedifferenceisallinthesoftwareUNIXTCP/IPShortCourseNotesAlanDix19962/3EstablishingaTCPConnectionPassiveOpen161.
112.
192.
5server21151.
100.
17.
25serverprocessdoesa'passive'openonaportitwaitsforaclienttoconnectatthisstagethereisnoInternetnetworktraffictellstheTCPlayerwhichprocesstoconnecttoUNIXTCP/IPShortCourseNotesAlanDix19962/4EstablishingaTCPConnectionActiveOpen151.
100.
17.
25client2397161.
112.
192.
5server21161.
112.
192.
5server21161.
112.
192.
5:21clientprocessusuallyonadifferentmachineperformsan'active'openontheportportnumberattheclientendisneededusuallyautomatic(e.
g.
,2397)butcanbechosennetworkmessage→servermachinerequestsconnectionUNIXTCP/IPShortCourseNotesAlanDix19962/5EstablishingaTCPConnectionRendezvous151.
100.
17.
25client161.
112.
192.
5server21161.
112.
192.
5server21serversideacceptsandTCPconnectionestablishedabi-directionalreliablebyte-streamconnectionidentifiedbybothhost/portnumberse.
g.
serverportisnotconsumedcanstay'passive'openformoreconnectionsliketelephonecalldesk:onenumbermanylinesUNIXTCP/IPShortCourseNotesAlanDix19962/6EstablishingaTCPConnectionandmore.
.
.
151.
100.
17.
25client161.
112.
192.
5server21161.
112.
192.
5server21161.
112.
4.
3clientotherclientscanconnecttothesameportstateforconnectionsintheclient/serveronlynoinformationneededinthenetworknotlikeoldstylerelay-basedexchangesservercanrestrictaccesstospecifiedhostorportservercanfindoutconnectedhost/portUNIXTCP/IPShortCourseNotesAlanDix19962/7Passive&ActiveOpenpassive–patientbutlazyactive–industriousbutimpatientpassiveactivewaitsforrequestforconnectionsendsoutrequestforconnectionwaitsforevertimesoutnormallyserverdoespassiveopen–waitingforclientbutnotalways(e.
g.
ftp)activeopenscanrendezvous.
.
.
.
.
.
butmaymissduetotime-outseithercanspecifylocalportbutifnotspecified,allocatedautomaticallyUNIXTCP/IPShortCourseNotesAlanDix19962/8Simpleclient/server'talk'usessimplifiedcallsserverhandlesonlyoneclientstrictturntakinguser1user2zeus:simple-serverstartupcompleteio:simple-client-hostzeusYoucansendnowspeak:hithereclientsays:hitherespeak:nicedayisn'titserversays:nicedayisn'titspeak:bitcoldhereclientsays:bitcoldherespeak:^D(EOF)byebyezeus:serverfinishedtheconversationio:UNIXTCP/IPShortCourseNotesAlanDix19962/9ServerCodeestablishportport_sk=tcp_passive_open(port)/*onlydoneonce*/waitforclienttoconnectclient_sk=tcp_accept(port_sk)/*repeatedformultipleclients*/thentalktoclientfor(;;){/*waitforclient'smessage*/len=read(client_sk,buff,buf_len);buff[len]='\0';printf("clientsays:%s\n",buff);/*nowit'sourturn*/printf("speak:");gets(buff);write(client_sk,buff,strlen(buff));}N.
B.
strictturntaking:client–server–client–server.
.
.
UNIXTCP/IPShortCourseNotesAlanDix19962/10ClientCoderequestconnectiontoserverserv_sk=tcp_active_open(host,port)/*waitsforservertoaccept*//*returnsnegativeonfailure*//*hostisserver'smachine*/thentalktoserverfor(;;){/*ourturnfirst*/printf("speak:");gets(buff);write(serv_sk,buff,strlen(buff));/*waitforserver'smessage*/len=read(serv_sk,buff,buf_len);buff[len]='\0';printf("serversays:%s\n",buff);}N.
B.
oppositeturnordernoerrorchecking!
UNIXTCP/IPShortCourseNotesAlanDix19962/11Handsoncopysimple-client.
cfromtcp/session2directorysimple-client.
csimple-server.
cmakefilecompileandruntheprograms:makesimple–compilesthembothononemachinetype:simple-serveronanothertype:simple-clientmachine-namewheremachine-nameisthenameofthefirstwhathappensifyoure-runtheserverstraightafteritfinishesusethe-portoptionzeus:simple-server-port3865io:simple-client-hostzeus-port3865tryaportlessthan1024!
UNIXTCP/IPShortCourseNotesAlanDix19962/12read&writeReminder:ret=read(fd,buff,len)intfd–afiledescriptor(int),openforreadingchar*buff–bufferinwhichtoputcharsintlen–maximumnumberofbytestoreadintret–returnsactualnumberreadretis0atendoffile,negativeforerrorbuffisnotNULLterminatedleaveroomifyouneedtoadd'\0'!
ret=write(fd,buff,len)intfd–afiledescriptor(int),openforwritingchar*buff–bufferfromwhichtogetcharsintlen–numberofbytestowriteintret–returnsactualnumberwrittenretisnegativeforerror,0means"endoffile"retmaybelessthanlene.
g.
ifOSbuffersfull*shouldreallycheckandrepeatuntilallgone*buffneednotbeNULLterminatedifbuffisaCstring,usestrlentogetitslengthN.
B.
Bothmayreturnnegativeafterinterrupt(signal)UNIXTCP/IPShortCourseNotesAlanDix19962/13read&writewithsocketssimilartonormalUNIXpipesbi-directionalbytestreamrreadandwritetosamefiledescriptor$difficulttocloseonedirectionspecialsocketcallshutdown(sock,dir)readingmayblockrreadingfromafileeither:(i)succeeds(ii)getsendoffile(ret=0)rreadingfromasocketwaitsuntil(i)networkdatareceived(ret>0)(ii)connectionclosed(ret=0)(iii)networkerror(ret0)(ii)findconnectionisclosed(ret=0)(iii)networkerror(retsendmailiscommonSMTPserverunderUNIXclientcommands:HELOclienttellsserverwhoitisMAILinitiatesmessageandsetssenderRCPTsetsoneoftherecipientsDATAsaysactualmessagecontentfollowsVRFYcheckthatrecipientexists(nomailsent)EXPNexpandmailalias(nomailsent)RSETstartfromscratchEHLOseeifserverhandlesadvancedfeaturesQUITfinishsessionUNIXTCP/IPShortCourseNotesAlanDix19963/14SMTP–2authentication,serverstypically:rdonottrustHELOusereversenamemappinginsteadrdotrustsendername(From:)howcouldtheyverifyitSMTPspecifiesdeliverynotcontentotherstandardsusedforcontent:rnon-ASCIIcharactersinheaders=ISO-8859-1QAlan=20Dix=rMIMEformulti-partmixedcontentmessagessimplemailmessageisjust:rheaderFrom:alan@zeus.
hud.
ukTo:R.
Beale@cs.
bham.
uk.
acSubject:HCIbook2ErblanklinerbodyRussell,haveyouheardfromPrenticeHallyetconcerningthewebpagesAlanUNIXTCP/IPShortCourseNotesAlanDix19963/15HandsonseewhatitdoeswewanttosendamailmessageusingrawSMTP!
firstofallseehow'mail'doesitcannotuseproxyasSMTPisatport25(protected)insteadtrythe-voptionofmailtype:mail-vc3–orwhoeveryouwanttosendmailto!
seethemessagesfromtheserverandtheclientN.
B.
notallmessagesareshownwhendoesmailestablishtheconnectionwhyUNIXTCP/IPShortCourseNotesAlanDix19963/16Handsondriveitbyhandusetelnettosendamessagetype:telnetzeus.
hud.
ac.
uk25youareconnectedtotheSMTPserveronzeussayhello!
whichmachineyouareonHELOwalt.
disney.
comdiditbelieveyouhowdoesitknownowsaywhothemessageisfromandwhoitistoMAILFrom:RCPTTo:nextsendthemessageDATAfirstlineofmessage.
.
dottyshearquackery.
finallysaygoodbyeQUITrunmailtoseeifanycelebrityhassentyouanyUNIXTCP/IPShortCourseNotesAlanDix19963/17argc&argvrecall:intmain(intargc,char**argv).
.
.
or:intmain(intargc,char*argv[oneofthewaystogetinformationintoaCprograminUNIXyoutype:myproga"bc"dtheprogramgets:argc=4–lengthofargvargv[0]="myprog"–programnameargv[1]="a"argv[2]="bc"–singlesecondargumentargv[3]="d"argv[4]=NULL–terminatorN.
B.
rDOSisidentical(exceptargv[0]isNULLearlyversions)rargcisonelessthanthenumberofarguments!
otherwaystogetinformationin(UNIX&DOS):rconfigurationfile(knownname)rstandardinputrenvironmentvariablesusinggetenv()or(UNIXonly)thirdargtomain:main(intargc,char**argv,char**envp)UNIXTCP/IPShortCourseNotesAlanDix19963/18Make'make'isaUNIXcommandwhich:automatesprogramconstructionandlinkingtracksdependencieskeepsthingsup-to-dateafterchangestouseit:rconstructafilewithrulesinityoucancallitanything,but'makefile'isthedefaultrrun'make'itselfmaketarget–(usesthedefaultmakefile)make-fmyfiletarget–(usestherulefilemyfile)eitherrebuildstheprogram'target'ifnecessaryeachmakefileconsistsof:rdefinitionsrrulesrulessayhowonethingdependsonanothertheyareeither:rspecific–e.
g.
tomakemail-clientdothis.
.
.
rgeneric–e.
g.
tomakeany'.
o'fromits'.
c'.
.
.
makeisalsoavailableinmanyotherprogrammingenvironmentsUNIXTCP/IPShortCourseNotesAlanDix19963/19MakefileformatDefinitionsgeneralform:variable=valueexample:SDIR=tcpMYLIBS=$(SDIR)/libN.
B.
onevariableusedinanother'sdefinitionmakevariablesarereferredtolaterusing$e.
g.
$(SDIR),$(MYLIBS)expandedlike#definesorshellvariables(someversionsofmakewillexpandshellvariablesalso)Rules(justspecificrules)generalform:target:dependent1dependent2.
.
.
command-lineN.
B.
thismustbeatabexample:myprog:myprog.
oanother.
occ-omyprogmyprog.
oanother.
o$(MYLIBS)thissays:tomakemyprogyouneedmyprog.
oandanother.
oifeitherofthemisnewerthanmyprogrebuilditusingthethenrebuilditusingthecommand:"cc-omyprog.
.
.
"UNIXTCP/IPShortCourseNotesAlanDix19963/20HelperFunctionsstandardresponselinestomakelifeeasier!
myownhelperfunctionsrtoreadstandardresponselines#include"protocol.
h"rtointeractwithSMTPserver#include"mail-helper.
h"intget_response_fd(intserver_fd,intecho_fd,char*buff,intlen);readsfromserver_fdparsesasingleormulti-lineresponsereturnstheresponsecode(oflastline)echoesfullresponsetoecho_fdalsocopiesitintobuffifnon-NULLintget_response_fp(FILE*server_fp,FILE*echo_fp,char*buff,intlen);similarwithstdiofilesUNIXTCP/IPShortCourseNotesAlanDix19963/21HelperFunctions–2forsendingmailintdo_mail_init(intserv_fd);awaitsfirstresponseanddoes'HELO'checksresponseandreturns0ifOKintdo_mail_from(intserv_fd,char*from);intdo_mail_to(intserv_fd,char*to);sends'MAIL'and'RCPT'commandsrespectivelysender(from)andrecipient(to)areCstringsintdo_mail_data_fp(intserv_fd,FILE*user_fp);intdo_mail_data_buff(intserv_fd,char*buff,intlen);send'DATA'commandandsendmessagecopiedfromuser_fporbuffrespectivelyintdo_mail_quit(intserv_fd);does'QUIT'commandAlloptionallyechoallexchangestoafile(orterminal)setby:FILE*do_mail_set_echo_fp(FILE*new_echo_fp)UNIXTCP/IPShortCourseNotesAlanDix19963/22Handsonbuildyourownmailclientcopysimple-client.
candcallitmail-client.
ccopythefollowingfromtcp/session3:mail-helper.
cmake3themakefileisreadytocompileyourmailclientyoucantype(whenready!
):make-fmake3mail-clientN.
B.
xSMTPobeysstrictturn-taking:server–client–server–client–serveryserverstartswithareturncodezbutclient'incontrol'modifytheclientcodexsetdefaulthost(zeus)andport(25)ytoandfromaddresses:eitherreadinoruseargvzmessage:initiallyreadasingleline{'unwrap'looptogivefixedturnsUNIXTCP/IPShortCourseNotesAlanDix19963/23Handsonmailclient–2resultingprogramstructure:(a)read(parse)to/fromaddressesfromuser(b)readmessagefromuser(getsorscanf)(c)opentcpconnectiontomailserveroncorrectport(d)waitforserverresponseline(s)(e)sayhellotoserver(f)waitforserverresponseline(s)(g)saywhothemailisfrom(h)waitforserverresponseline(s)(i)saywhothemailisto(j)waitforserverresponseline(s)(k)saythatdataiscoming(l)waitforserverresponseline(s)(m)sendonelinemessage(n)sendlinewithjustfullstop(o)waitforserverresponseline(s)(p)saygoodbye(q)waitforserverresponseline(s)(r)closeconnectioncompileandrunyourcode!
ifyouhavetimemodifyittosendlongermessageseither:changestep(b)and(m)toacceptlongmessagesor:removestep(b)andmake(m)readfromuserbeforesendingeachlineor:whateveryoulike.
.
.
UNIXTCP/IPShortCourseNotesAlanDix19963/24Session4UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPConcurrentClientsUNIXNetworkProgrammingwithTCP/IPSession4AlanDixhttp://www.
hcibook.
com/alansequentialandconcurrentclientstechniquesforconcurrencycall-backsknowingwhatyou'redoingcallback–basedclientusingitUNIXTCP/IPShortCourseNotesAlanDix19964/1SequentialClientse.
g.
FTP1.
clientwaitsforuserinput2.
usertypes"DIR"3.
clientperformspassiveopenondataport(2397)4.
clientsends"PORT161.
112.
192.
5.
9.
93"toserver5.
clientwaitsforstandard'200'replyline6.
ifnotOKthenfail7.
clientsends"LIST"toserver8.
clientwaitsforstandard'150'replyline9.
ifnotOKthenfail10.
clientreadsfromdataport11.
clientwaitsforstandard'226'replyline12.
ifnotOKthenfail13.
reportsuccesstouserclientisincontrolnextclientactiondependson:rwhathappenedlaste.
g.
whatcommendtheusertypesrNOTonwhenithappensUNIXTCP/IPShortCourseNotesAlanDix19964/2NaturallyConcurrentClientse.
g.
telnetatanymomenteitherusermaytypesomethingoroutputmaycomefromserverendclientmustrespondwhicheverhappensprogramabitlike:rwhenusertypesthensendtotheserverrwhenserversendsmessagethenprintonterminalUNIXTCP/IPShortCourseNotesAlanDix19964/3ConcurrencyforUsabilitye.
g.
Netscape–WWWclientbasicprotocoltransactionbased$.
butresponsecanbeslowinteractionallowedduringtransactionscrolling'STOP'buttonclienthastolistentoserver–moredatauser–mouseandkeyboardUNIXTCP/IPShortCourseNotesAlanDix19964/4ProgrammingConcurrencyProblemdoingmorethanonethingatoncelisteninguserterminal&TCPserverportSolutionspollingrusenon-blockingI/O$keepsprocessorbusythreadsrneedsbuilt-insupport(languageorOS)rprogramwrittenasseveralsequentialpartsrallexecutedatthesametimercommunicateusingshareddata(alsosemaphoresetc.
)eventdrivenprogrammingrlow-level–e.
g.
UNIXselectrevent-loop–e.
g.
,rawXandMacrprogramparadigm–e.
g.
VisualBasic,HyperCardrcall-backs–e.
g.
,Windows,XMotifUNIXTCP/IPShortCourseNotesAlanDix19964/5EventLoopTypicalprogramstructureforloopforever*/structevent_stevent;read_event(&event);if(event.
type==BUTTON&&event.
target=quit_button)returnOK;elseif(event.
type==KEYPRESS)insert_char(event.
char);elseif(event.
type==INPUT_READY)do_network_task(event.
buff);.
.
.
}programmerincontrol$relatedcodegetsspreadoutinif/casestatementsoftenwrittenwithsub-loopse.
g.
fordialogueboxesunforeseenevents(e.
g.
networkI/O)maybedelayedorevenignored!
UNIXTCP/IPShortCourseNotesAlanDix19964/6Event–BasedLanguagesprogram=collectionofeventhandlerse.
g.
HyperCardonmouseUpsetcursortowatchputgetServerAddress()intoserverAddrputgetUserName()intouserNameputcdfld"ToOrFrom"intotoNameputcdfld"Message"intotheMesssend"toServerSendMail"&"e&toName"e&comma&"e&userName"e&comma&"e&theMess"etoprogramserverAddrendmouseUponAppleEventclass,id,senderanswer"AppleEvent"&&class&&"from"&&sender--dialogueboxforuserendAppleEventconcurrencynaturallypartoflanguage$networkI/OnotalwaystreateduniformlyUNIXTCP/IPShortCourseNotesAlanDix19964/7Call-backsusedinmanytoolkitsandwindowmgrs:e.
g.
:rWinSock(TCP/IPunderWindows)rXMotifGeneralpatternProgramxdefineafunctionytelltoolkittoattachittoeventzgivecontroltothetoolkitToolkitiwheneventhappenscalluserdefinedfunctionUNIXTCP/IPShortCourseNotesAlanDix19964/8Example–XMotifCall-backsXtAddCallback(widget,callback-type,func,my-data)widget–awidgetsuchasabuttontype–acallbackresourcename:whichtypeofeventtorespondtoe.
g.
,XmNactivateCallbackfunc–pointertoCfunctiondefinedbyyoue.
g.
,quit_funcmy-data–anintegerorpointertoyourdatapassedontoyourcallbackThecallbackfunctiondefinition:voidquit_func(widget,my-data,event-data)widget–wheretheeventoccurredmy-data–theintegerorpointerpassedinthecalltoXtAddCallbackevent-data–theXeventstructurewhichcausedthecallbackUNIXTCP/IPShortCourseNotesAlanDix19964/9What'sgoingonSequentialProgramsforN.
B.
pseudo-C!
!
!
*/gets(command);ifif(commandis"quit"){charresponse[MAX_LINE_SIZE+1];←ywrite(serv_sd,"QUIT\n",5);xread(serv_fd,response,MAX_LINE_SIZE);if(response[0]!
='2'printf("sessioncomplete\n");exit(0);}if}featuresforfreexprogramcounter()–whatyouaredoingylocalvariables–whatyouaredoingittoUNIXTCP/IPShortCourseNotesAlanDix19964/10What'sgoingon-2sequentialconcurrentimplicitexplicitlocalvariablesglobalvariablesordynamicdatastructurese.
g.
partiallineofuserinputprogramcountermodevariableorfinitestatemachines!
e.
g.
TELNETcommandsequencesserveroutputmodes:xnormalechoingywaitingforcommandzwaitingforoptionnotbyte255byte255321253–DO254–DONTanyoptionotherbytesUNIXTCP/IPShortCourseNotesAlanDix19964/11Callbackbasedclient–1Initialisationmain(.
.
.
){/*requestconnectiontoserver*/sd=tcp_active_open(host,port)/*set-upcallbackforserver*/inform_input(sd,read_socket,NULL);/*set-upcall-backsforinterface*/.
.
.
/*givecontroltotoolkit*/inform_loop();}Whenserversendsamessage.
.
.
.
.
.
read_socketiscalledread_socket(intsd,/*readserver'smessage*/len=read(sd,buff,buf_len);/*processmessage*//*probablyupdateinterface*/}UNIXTCP/IPShortCourseNotesAlanDix19964/12Callbackbasedclient–2Whenuserdoessomething.
.
.
.
.
.
appropriatefunctioniscalledterm_line(intfd,void*id,char*buff){/*processinterfaceevent*/mess("sending{%s}\n",buff);/*possiblysendmessagetoserver*/write(sd,buff,strlen(buff));}steponceatinitialisationsteps&anynumberoftimesinanyorderUNIXTCP/IPShortCourseNotesAlanDix19964/13Handsonanelectronicconferencecopythefollowingfromtcp/session4:client.
cserver.
cmake4themakefileisreadytocompile,type:make-fmake4confonepersonruntheserver:io:servertwoormoreothersruntheclient:other:client-hostioN.
B.
youcannotparticipatefromtheservertojoininlaunchaclientinanotherwindowoftheserver'smachineUNIXTCP/IPShortCourseNotesAlanDix19964/14UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPSession5ServerDesignUNIXNetworkProgrammingwithTCP/IPSession5AlanDixhttp://www.
hcibook.
com/alantypesofserverhandlingserverconcurrencyserverstatestatelessserverswhenthingsgowrong!
survival–the3Rscallback–basedservermodifyserverUNIXTCP/IPShortCourseNotesAlanDix19965/1ServersKindsofserverxtransactionbasede.
g.
database:1query→1resultystrictturn-takinge.
g.
ftpzinherentconcurrencye.
g.
electronicconferences,MUDsforlotsofclientseither:serveoneatatimeinturnxmaybeslowymaytakeforever!
serveseveralatthesametimebothrequireconcurrencyUNIXTCP/IPShortCourseNotesAlanDix19965/2ServerConcurrencysimilarsolutionstoclientrpollingacceptableifmachinededicatedtoserverrthreadsrUNIXselectreventdriven$lesslikelytoruninevent-basedsystemsomewebbasedserversdoinaddition:rwhennointrinsicconcurrencyrcanuseUNIXforklaunchseparateprocesstoserveeachclientsoeachissimplerusesstandardUNIXprocessconcurrency$canbeexpensive(processcreation)especiallywithlotsofsmalltransactionsUNIXTCP/IPShortCourseNotesAlanDix19965/3ServerStateconcurrentserverneedstorememberrhowmanyclientsrstateoftheirconnectionrstateofeachtransaction/protocoletc.
etc.
etc.
$.
manyclientslargestate$.
disasterscenariosrclientestablishesconnectionrclientcrashesrclientrestartsrclientestablishesanewconnectionritcrashesagain.
.
.
solution–nostateUNIXTCP/IPShortCourseNotesAlanDix19965/4StatelessServersstateless=noperclientstatefortransactionbasedservicesrclientmakesrequestrserverperformsactionrserverreturnsresultreallyonlypossiblewithUDPe.
g.
http–transactionbased,butusesTCPmayneedseveralreadsforrequestneedtostorepartiallyfilledbuffer.
.
.
N.
B.
ingeneral,bufferspartoftheperclientstate$notallplainsailing.
.
.
rclientshavetomaintainmorestaterrequestsmorecomplex(nocontext)runreliableprotocoltransactionsmustbeidempotenttime-outsforlotstransactions.
.
.
UNIXTCP/IPShortCourseNotesAlanDix19965/5WhenthingsgowrongPCcrashonesaduserservercrashlotsofangryuserstakespecialcarewithservers!
probabilityoffailure:clients–prob.
offailure=pserver–prob.
offailure=qnclientsandonly1server,so:probabilityofsomefailure≈np+qgoodnews!
rserverfailurelesslikely(orisit)badnews!
rserversaremorecomplex(q>p)rwhatifclientbringsserverdownUNIXTCP/IPShortCourseNotesAlanDix19965/6CausesoffailurehardwarefailuresprogrammingerrorsunforeseensequencesofeventssystemdoesnotscaleLargenumberofcomponentsmorefrequentComplexityofalgorithmsmorelikelyInterleavinganddelaysdifficulttodebugLimitedtestingconditionsunexercisedUNIXTCP/IPShortCourseNotesAlanDix19965/7SurvivalNetworkorserverfailurestandardsolutionsClientfails—threeRsforserverrobustservershouldsurviveneverwaitforresponsefromclientnon-blockingnetworkI/Oreconfiguredetectandrespondtofailuretime-outorfailureofI/OoperationsresetinternaldatastructuresinformotherclientsresynchronisecatchupwhenclientrestartssimilartonewclientN.
B.
clientmaynotknow(network)UNIXTCP/IPShortCourseNotesAlanDix19965/8SoftwarefaultsDefensiveprogramminginconsistentclient/serverdatastructuresUsesimplealgorithmsfixedsizedstructures–butcheckbounds!
mayconflictwithscaleability–documentVerifyclosehandchecksforproductioncode–formalmethodsUnforeseensequencesofeventsdeadlock–neveruseblockingI/Oneverassumeparticularordersofeventsback-to-backmessagesnetworkpacket≠logicalmessageDebuggingandtestinglogging–toreproducefailurerandomdata–atinterfaceornetworkaskyourfriendsUNIXTCP/IPShortCourseNotesAlanDix19965/9Callbackbasedserver–1Initialisationmain(.
.
.
){/*establishport*/pd=tcp_passive_open(port)/*set-upcallbackforport*/inform_input(pd,accept_client,NULL);/*givecontroltonotifier*/inform_loop();}Whenclientrequestsconnection.
.
.
.
.
.
notifiercallsaccept_clientaccept_client(.
.
.
){/*acceptclient'sconnection*/fd=tcp_accept(port_fd);/*recordconnectiondetails*/client_fd[count]=fd;/*set-upcallbackforclient*/inform_input(fd,read_client,count);/*keeptrackofnumberofclients*/count=count+1;/*probablytellotherclientsalso*/}UNIXTCP/IPShortCourseNotesAlanDix19965/10Callbackbasedserver–2Whenclientsendsmessage.
.
.
.
.
.
notifiercallsread_clientread_client(c_fd,id){/*readclient'smessage*/len=read(c_fd,buff,buf_len);/*broadcasttootherclients*/for(c=0;c0)printf("parent\n");elseprintf("failed\n");DATAi=3c_pid=-1afterforkparentandchildidentical597632inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");DATAi=3DATAi=3c_pid=632c_pid=0exceptforthereturnvalueofforkUNIXTCP/IPShortCourseNotesAlanDix19966/10Execution–2becausedataaredifferent597632inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");DATAi=3DATAi=3c_pid=632c_pid=0programexecutiondiffers597632inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");inti=3,c_pid=-1;c_pid=fork();if(c_pid==0)printf("child\n");elseif(c_pid>0)printf("parent\n");elseprintf("failed\n");DATAi=3DATAi=3c_pid=632c_pid=0soparentandchildbehaviourdivergeUNIXTCP/IPShortCourseNotesAlanDix19966/11forkbasedshellserver–1Basicstructure:establishportloopforeveroneachloop:racceptasingleclientconnectionrforkachildtomanageclientchildexecsacopyoftheshellN.
B.
nologin–veryinsecure!
Mainloopmain(.
.
.
){/*openport*/port_sk=tcp_passive_open(port)/*loopforeveracceptingclients*/while(accept_one(port_sk)>0);/*onerrorcloseandexit*/close(port_sk);exit(0);}UNIXTCP/IPShortCourseNotesAlanDix19966/12forkbasedshellserver–2Processeachclientinturnaccept_one(intport_sk){/*acceptasingleconnection*/client_sk=tcp_accept(port_sk);/*performfork*/child_pid=fork();childgetszeroreturnfromforkif(child_pid==0){/*childclosespassiveport*/close(port_sk);/*thenstartsitsownbehaviour*/exec_a_shell(client_sk);}parentgetschildprocessidreturnedfromforkelseif(child_pid>0){/*parentclosesclientsocket*/close(client_sk);/*N.
B.
childhasopendescriptor*//*soclientisnotcutoff*//*returnschildpidtomainloop*/returnchild_pid;}negativeresultonfailureelsereturn0;}UNIXTCP/IPShortCourseNotesAlanDix19966/13forkbasedshellserver–3ChildexecsacopyoftheshellN.
B.
onlythechildprocesscallsthisfunctionintexec_a_shell(intfd)/*doesn'treturn*/{inttty_fd;;shellwillexpectI/Ofromstandardfiledescriptorsuse'dup2'systemcalltolinkthemtofddup2(fd,0);/*standardinputfromfd*/dup2(fd,1);/*standardoutputtofd*/dup2(fd,2);/*standarderrortofd*/close(fd);execv("/bin/sh",argv);execonlyreturnsifitfailsstandarderrorhasbeenclosedsoneedtoopen/dev/ttyexplicitlytty_fd=open("/dev/tty",1);write(tty_fd,exec_fail_mess);_exit(1);}UNIXTCP/IPShortCourseNotesAlanDix19966/14dup2systemcallintres=dup2(old_fd,new_fd);makesnew_fdpointtosamefile/streamasold_fdnew_fdisclosedifalreadyopenmostoftenusedwithstandardI/Odescriptors:dup2(fd,0);–standardinputreadsfromfdcanclosetheolddescriptor.
.
.
butnewdescriptorstillworksdup2(fd,0);close(fd):n=read(0,buff,buff_len);negativereturnonfailureUNIXTCP/IPShortCourseNotesAlanDix19966/15execsystemcallexecv(char*prog,char**argv);replacesthecurrentprocesswithprogneverreturnsexceptonfailureargvispassedtothe'main'ofprogN.
B.
needsatleastargv[0]settoprogramnamenewprocess:rcode–replacedbyprogrdata–reinitialisedrsystemdata–partlyretainedffiledescriptorsstillopenseveralvariants(execl,execvp,oftenusedafterforktospawnafreshprogramUNIXTCP/IPShortCourseNotesAlanDix19966/16execvs.
forkforkduplicatesprocessexecreplacesprocesscodedatasystem597codedatasystem632codedatasystem597codedatasystem493codedatasystem493forkexecforkchildsharesopenfiledescriptorsexec-edprocessretainsopenfdsUNIXTCP/IPShortCourseNotesAlanDix19966/17deathofaforkedprocesswhenparentdiesrchildrenbecomeorphans!
rsysteminitprocess'adopts'themwhenchilddiesrparent(orinit)informedbysignal(SIGCHLD)rchildprocesspartlydestroyedrrumpretaineduntilparent'reaps'–usingwaitorwait3systemcallruntilthenchildis'zombie'–pssaysorN.
B.
zombiestatenecessarysoparentcandiscoverwhichchilddiedUNIXTCP/IPShortCourseNotesAlanDix19966/18SIGCHLD&wait3ifparentdoesnotreapchildren.
.
.
theystayzombiesforever.
.
.
systemresourcesmayrunoutxfirstcatchyoursignalsignal(my_reaper,SIGCHLD);function'my_reaper'calledwhensignalarrivesythenreapachildintmy_reaper(){unionwaitstatus;while(wait3(&status,WNOHANG,NULL)>=0);}useWNOHANGsothatwait3doesn'tblocklooptoreapmultiplechildrenUNIXTCP/IPShortCourseNotesAlanDix19966/19forkandI/Olow-levelI/Oopenfiledescriptorssharedso:routputismergedrinputgoestofirstread–acceptsimilarrclosedownmaybedelayeduntilallprocessesclosefdcloseallunwantedfdsoruseioctltosetclose-on-exechigh-levelI/OCstdioisbuffered:rduplicatedatforkrmaygetflushedafterforkduplicatewritesstderrOK–unbufferedcarefulwithstdiousestderrorsetbuff(fd,NULL)UNIXTCP/IPShortCourseNotesAlanDix19966/20Handsoncopythefollowingfromtcp/session6:knife.
cmake6compileknife.
c:make-fmake6knifelaunchtheknifeserver:knife.
c:io3%knife-port2345connecttoitfromadifferentmachineorwindowklah7%telnetio2345doyougetashellprompttrysomethingsimplelikeechohellothentrypswhathappenstrytypinga#attheendofeachlineechohello#ps#whatishappeningUNIXTCP/IPShortCourseNotesAlanDix19966/21inetdemontherearemanyInternetservices:ftp,telnet,rlogin,echo,etc.
aserverforeachisexpensiveinetdisamulti-serviceserveritdoesapassiveopenonlotsofports:21–ftp,25–SMTP,etc.
whenaclientconnectsitforkstheappropriateserviceremoteloginssomewhatcomplicatedUNIXTCP/IPShortCourseNotesAlanDix19966/22remoteloginFirstsolution.
.
.
.
.
.
simplyforkashellorgetty$notranslationofcodese.
g.
endoflinesequence$noterminaldriveratserverendnottycontrolbyapplicatione.
g.
editorsneedttyrawmodeActualsolution.
.
.
.
.
.
intermediateprocessserver-endprocessbetweenclientandshell/gettycanperformtranslationpseudo-ttybetweenitandshellserver-endttycontrolUNIXTCP/IPShortCourseNotesAlanDix19966/23remotelogin–2xremoteloginclientconnectstoserverclientserveryserverforkschildtohandleloginclientchildserverforkUNIXTCP/IPShortCourseNotesAlanDix19966/24remotelogin–3zchildthenforksanotherprocess/devclientchildserverforkandexecshell{thenewprocessconnectstothechildusingapseudo-terminal|andfinallyexecsashell(orgettyetc.
)fuserisnowconnectedtoshellUNIXTCP/IPShortCourseNotesAlanDix19966/25remotelogin–4clientandserver-sidechildsimilarrbothconnectedtonetworkrbothconnectedto(pseudo)terminalgeneralalgorithm:rechoterminalinputtonetworkrechonetworkinputtoterminalN.
B.
bothconcurrentdifferenceinuseofterminal:rwhereclient–applicationendofttychild–'user'endofpseudo-ttyrhowclient–ttyalwaysinrawmodechild–pseudo-ttymodesetbyshellonlyonelayerofttyprocessingUNIXTCP/IPShortCourseNotesAlanDix19966/26Handsonechoservermodifyknife.
ctomakeaforkingechoserveryourpreviousechoserver(session2)onlydealtwithoneclient–thisonewilldealwithanynumbercopyknife.
cintoecho-alllocatethesub-routinewheretheshellisexec-edreplacethecodeduplicatingfiledescriptorsandexec-ingtheshell–simplyhavealoopwhichreadsfromthesocketandwritesbacktoitcompileandrunecho-allio15%make-fmake6echo-allio16%echo-all-port2345anconnecttoit:klah23%telnetio2345thereisanalternativesolutionwhichonlyinvolvesreplacing2charactersofknife.
chint:theanswerdoesn'tinvolveanydogsUNIXTCP/IPShortCourseNotesAlanDix19966/27MTUstheInternetisheterogeneousrheterogeneoustransportlayersdifferentpacketsizesrdynamicroutinghopsondifferentlayersunpredictablepacketsizetransportlayerlimitcalledMTU:–maximumtransmissionunittransportlayerMTUinbytesHyperchannel6553516MbpsIBMtokenring179144MbpsIEEE802.
5tokenring4464FDDI4352Ethernet1500IEEE802.
3/802.
21492X.
25576PPP(performancelimit)296(fromRFC1191)UNIXTCP/IPShortCourseNotesAlanDix19966/28IPfragmentationwhathappenswhensizeistoosmallfragmentationranyintermediaterouterdetectsproblemrIPdatagrambrokenintopiecesreachsentseparately(possiblydifferentroutes)rreconstructedatfurtherrouterordestinationreallimitisrecipient'sbuffersizer576bytesIPdatagramguaranteed.
.
.
butthisincludesheadersrUDPlimit=512bytesuserdatarTCPdividesdataupforyoulimitisUNIXread/writebuffersonlyendpointsmatterinacontrolledenvironment.
.
.
.
.
.
largerdatagramspossiblee.
g.
NFS=8192bytesUNIXTCP/IPShortCourseNotesAlanDix19966/29fragmentationconsideredharmfulfragmentationIPtransparenttounderlyinglinklayerMTU.
.
.
wellalmost.
.
.
IPisnotreliablesomepackets(fragments)maybelostnore-transmissionrIPhandlesreconstruction.
.
.
.
.
.
butnotfragmentretransmissionrfragmentlostwholeIPdatagramlostrprobabilityonefragmentlost=pnfragmentsprobabilityIPdatagramlost≈npavoidingfragmentationrUDP–mostprotocols≤512bytesrTCP–useslocal(end-point)MTU+pathMTUdiscoveryalgorithmUNIXTCP/IPShortCourseNotesAlanDix19966/30TCPreliabilityunderlyingIPunreliableTCPmusthandshakestreamprotocolrsender:thisisbytesn–mofthedatarrecipient:ackm–lastbytereceivedretransmissionrrecipient:outoforderreceipt→repeatackrtimeoutorseveralrepeatacks→retransmittoomanyacksravoidlotsoflittleacknowledgementpacketsrackoflastpacketpreviouspacketsarrivedrpiggybackA→BackonB→Amessagerdelayackstoallowpiggybackrturnoffdelayforsomeprotocols(e.
g.
X)UNIXTCP/IPShortCourseNotesAlanDix19966/31TCPflowcontrolCannotsendwithoutlimits:qnetworkcapacity→packetlossrexponentialbackoffrapidresend→nightmarescenariolongdelaybeforefailure(2-9mins)rslow-startalgorithmqlink-layerbufferrMSSannouncementqTCPbufferrwindowsizeannouncementonlysendtolastack+windowsizeknowntobereceivedsentbutnotacknowlegedmaybesentbeforenextackmustbeheldatsenderendwindowsizelastacklastbytesentUNIXTCP/IPShortCourseNotesAlanDix19966/32UNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPUNIXNetworkProgrammingwithTCP/IPSession7SelectandSecurityUNIXNetworkProgrammingwithTCP/IPSession7AlanDixhttp://www.
hcibook.
com/alanSelectandSecurityUNIXeventsselectsystemcallproxyserverrawclientsecurity,secrecyandprivacyunderattack:viruses&wormtheInternetwormlevelsofsecurityencryptionandauthenticationUNIXTCP/IPShortCourseNotesAlanDix19967/1UNIXEventsComputationalprograms:busymostofthetimeread/writewhentheyarereadyInteractiveprograms:servers&clientsidlemostofthetimerespondtoeventsUNIXprocesses–4typesofeventxsignal(interrupt)ytime(alarm)zinputreadyreadwillnotblock{outputcanaccept(more)datawritewillnotblockUNIXTCP/IPShortCourseNotesAlanDix19967/2RespondingtoeventsEvents:xsignal(interrupt)ytime(alarm)zinput(read)ready{output(write)readyRespondinginterrupthandler–x&yusesignalsystemcallusesetitimertosendSIGALRMturntaking–y,z&{callread/writewhenreadyusesleepfordelayspolling–y,z&{usenon-blockingread/writeusetimetodothingsatspecifictimeswaitforseveraleventsuseselectsystemcalltimeoutorSIGALRMUNIXTCP/IPShortCourseNotesAlanDix19967/3pollinginUNIX#includeioctl(fd,FIONBIO,1);calltoioctltellssystem:don'tblockonread/writepollingthereforepossiblestructureofpollingtelnet-likeclient:ioctl(tty_fd,FNBIO,1);ioctl(net_fd,FNBIO,1);for(;;){/*anyterminalinput*/n=read(tty_fd,buff,buff_len);if(n>0){/*yes!
dosomething*/}/*anynetworkinput*/n=read(net_fd,buff,buff_len);if(n>0){/*yes!
dosomething*/}}UNIXTCP/IPShortCourseNotesAlanDix19967/4read&writeread:waitsononefiledescriptorreturnswheninputdataisreadyandreadsthedataintoabufferread(0,buff,len)write:waitsononefiledescriptorreturnswhenoutputispossibleandwritesthedatafromthebufferwrite(1,buff,len)UNIXTCP/IPShortCourseNotesAlanDix19967/5selectselect:waitsonmanyfiledescriptorreturnswheninputoroutputreadybutdoesnoactualI/O+alsoallowstimeoutselect(width,&in_fds,&out_fds,&err_fds,&timeout)UNIXTCP/IPShortCourseNotesAlanDix19967/6selectsystemcall–2intret=select(size,&in_fds,&out_fds,&err_fds,&timeout);in_fds,out_fds:–bitmapsoffiledescriptorsrin_fds–waitforinputi.
e.
readwillnotblockrout_fds–waitforoutputi.
e.
writewillnotblocksize:–sizeofin_fds,out_fds,err_fdstimeout:–whentotimeoutinsecondsandmillisecondsReturnswhen:inputreadyononeofin_fds(ret>0)outputreadyononeofout_fds(ret>0)erroroccursononeoferr_fds(ret>0)timeoutexpires(ret==0)signalhasbeencaught(retfd_setin_fds,out_fds,err_fdsmodifiedbycall:call–bitset=waitforfiledescreturn–bitset=filedescreadyreturnvaluefromselect=numberreadylongintegerinearlyUNIXsystemsin_fds=in_fds||(1structtimevaltimeout;timeout.
tv_secstimeout.
tv_ms–maximumtimetowaitinsecondsandmsifnoI/OreadyandnosignalsintimelimitthenselectreturnswithzeroresultN.
B.
in_fds,out_fds,err_fdsallzeroalsomodifiedbycallrideallyshouldreturntimeremainingrdoesn'tnow.
.
.
.
.
.
butmaydoonedaydon'trelyontimeoutnotbeingchangedresetforeachcalltoselectUNIXTCP/IPShortCourseNotesAlanDix19967/10selectandsignalssignaloccursduringsystemcall:read,write,orselectsignalnotcaught.
.
.
.
.
.
processaborts!
signalcaught.
.
.
xrelevanthandlercalledysystemscallreturnswith'error'howdoyouknowrnegativereturnvaluererrnosettoEINTRnegativereturn&errno≠EINTRreallyanerror!
UNIXTCP/IPShortCourseNotesAlanDix19967/11carewithsignalssignalhandlerscanrunatanytimeinti=0intmy_handler(){i=i+1}main(){signal(my_handler,SIGINTR);for(;;)if(i>0){do_something();i=i-1;}}intention:executedo_somethingonceperinterruptwhatactuallyhappens:xinteruptprocessed(i=1)ydo_somethingexecuteszmaincalculatesi-1getsresult0{beforeitstorestheresult.
.
.
.
.
.
anotherinterupt(i=2)|mainstoresresult(i=0)UNIXTCP/IPShortCourseNotesAlanDix19967/12whentouseselectservers:rwhereconcurrencyessentialrpossiblyftpserver–listentocontrol&datartelnetserver–listentouserovernetwork+listentoshell/applicationclientsrnotwithmostwindowmanagers–insteadusecallbackrsomeeventstreamWMs–singlefdforWMevents–listentoWMandnetworkrterminalbasedclients–notneededforturn-taking–e.
g.
telnet/rloginclientsUNIXTCP/IPShortCourseNotesAlanDix19967/13proxyserverproxyserverusedinsession3structureofcodexpassiveopenonownportywaitforclientconnectionzactiveopenonremoteserver{loopforeverwaitingforclientorserverinput:rwhenclientdatareadyreaditsendtoserverechoittoterminalrwhenserverdatareadyreaditsendtoclientechoittoterminalUNIXTCP/IPShortCourseNotesAlanDix19967/14proxycode–1Mainloopmain(.
.
.
){/*establishport*/port_sk=tcp_passive_open(port);/*waitforclienttoconnect*/client_sk=tcp_accept(port_sk);/*onlywantoneclient,*//*socloseport_sk*/close(port_sk);/*nowconnecttoremoteserver*/serv_sk=tcp_active_open(rem_host,rem_port);ret=do_proxy(client_sk,serv_sk);exit(0);}whendo_proxyiscalledbothnetworksocketsopenUNIXTCP/IPShortCourseNotesAlanDix19967/15proxycode–2yperformproxyloopintdo_proxy(intclient_sk,intserv_sk){firstdeclareandinitialisefdbitmapsfd_setread_fds,write_fds,ex_fds;FD_ZERO(&read_fds);FD_ZERO(&write_fds);FD_ZERO(&ex_fds);FD_SET(client_sk,&read_fds);FD_SET(serv_sk,&read_fds);thenloopforeverfor(;;){intnum,len;copybitmapsbecauseselectmodifiesthemfd_setread_copy=read_fds;fd_setwrite_copy=write_fds;fd_setex_copy=ex_fds;staticstructtimevaltimeout={0,0};thencallselectnum=select(MAX_FD,&read_copy,&write_copy,&ex_copy,&timeout);checkreturn–z,{&|atthispoint}return0;}UNIXTCP/IPShortCourseNotesAlanDix19967/16proxycode–3zcheckforsignals,errorsandtimeoutfirstcheckforsignals:inthiscase,wearenotexpectinganysoreturningeneral,wemayneedtodosomeprocessingfollowingtheinterruptitisusuallybetterfortheinterrupttosetsomeflagandletthemainloopdomostoftheworkthisreducestheriskofstackedinterruptsandmistakesinconcurrentaccesstodatastructuresif(numsendmail)23:45infectsDartmouthandArmyBallisticsRes.
Lab.
November3rd,198800:21PrincetonUniversitymainmachinecrashesduetoload02:38emailfromBerkeley:"Weareunderattack"03:15anonymouswarningfromfoo@bar.
arpa05:54patchestosendmaildistributed06:45NationalComputerSecurityCentre(NCSC)informed11:30MilnetseversitselffromArpanettopreventinfection16:00inoculationmethodfound(directoryshin/usr/tmp)21:30Berkeleystarttodecompile'captured'wormNovember4th,198805:00MITfinishdecompilingworm11:00MilnetrejoinsArpanet17:20finalsetofpreventativepatchesmailed21:30worm'sauthoridentified–namedinthenextday'snewspaperasRobertT.
MorrissonoftheNCSC'schiefscientistRobertMorris!
infectionsstillnotedaslateasDecember1988UNIXTCP/IPShortCourseNotesAlanDix19967/24Whatwentwrongseveralmeansofattackbetweenmachines:rdebugmodeinsendmailrbufferoverflowinfingerdroncebrokenintoauseronamachine–rlogin/rshtootherhostswithinamachine:rsimplepasswordattacks–permutationsofuser'sownname–internallistof432commonpasswords–systemdictionaryattemptedtopreventrepeatinfectionrdidn'talwaysworkrmaindamagewasexcessiveloadduetorepeatinfections(often100s)ralsohowitwasdetectedUNIXTCP/IPShortCourseNotesAlanDix19967/25sendmailattacksendmailhadadebugmoderwormconnectstosendmailrwormsends'debug'commandrsendmailwillthenexecuteanycommand!
rshouldhavebeendisabledbutsendmailiscomplex!
similarattacksstillpossiblersystemengineeraccountsrremotevendormaintenanceanydebugmodesonyoursystemUNIXTCP/IPShortCourseNotesAlanDix19967/26fingerdattackfingerdusesgets–bufferoverflowrwormconnectstofingerdrwormsends536bytelineroverflowsfingerd'sbuffer(512bytes).
.
.
andcorruptsstackrextra24bytesexecutedascode!
lessons:rneverusegets!
–atbestmaycrash-atworstisaloopholeralwaysbecarefulofbufferlengthsneveragainrapopularWWWbrowser.
.
.
–correctedinlaterversionsUNIXTCP/IPShortCourseNotesAlanDix19967/27physicalsecurityphysicalsecurity:rarethemachinessecurecansomeonereboot,substitutedisksetc.
risthenetworksecurecansomeonelink-intheirowncomputerlocalorglobalxlocalnetworkandmachinesybackboneandrouterszremotenetworkandmachinessecurexpossibleyreasonablefornon-criticaldataznoway!
N.
B.
'listeningin'easyonmanynetworkse.
g.
ethernetnevertrusttransportlayerUNIXTCP/IPShortCourseNotesAlanDix19967/28logicalsecuritysecrecy:rTCP/IPpacketsnotsecuree.
g.
creditcardbyemailruseencryptione.
g.
NetscapesecuresocketslayerforWWWauthentication:rwhoamItalkingtorisittherealserverrelyoncorrectroutingandprotectedports$impostormachine,non-UNIXserverhostrisitanacceptableclientuserpasswords$oftensentasplaintext!
–e.
g.
telnetaudit:rriskofdetectiondetersrkeepinglogsrreliesonauthenticationSMTPreversenamelookup$can'tcheckFROMfield–e.
g.
wormwarningUNIXTCP/IPShortCourseNotesAlanDix19967/29low-levelprotection–firewallssimplemeasuresrisolation–don'tconnecttotheglobalInternet.
.
.
butlosethebenefitstooranonymity–don'tpublishdomainmachinenames.
.
.
butIPaddressesstillvalidfirewallsrapplicationindependentractatrouter/gatewayrcanonlylookatIPorTCPheaderswhatispossibleronlyallowfriendlyIPaddresses–N.
B.
impostorsrlimitedinternalrouting–protectsensitivemachines/datarrestrictincomingTCPpackets–onlyallowconnectiontoprotectedports.
.
.
butdifficultforftpUNIXTCP/IPShortCourseNotesAlanDix19967/30high-levelprotection–ringfencesrloginrbewareexternalrootlogins!
rpasswords:–ifreasonableno'equiv'hosts–certainlynoroot'equiv'hostsmeanslotsofduplicatepasswordfilesserversrneverrunasroot–impossible!
e.
g.
inetd,rshdrneverunnecessarilyrunasroot–speciallogine.
g.
user'ftp'–runasuser'nobody'therestofthesystem–normalmeasuresrbackups–damagelimitationrpermissions–restrict'other'accessrsetuid–dangerous,nowriteperm!
r/etc/passwd–encryptorrestrictedread–maycauseproblemsUNIXTCP/IPShortCourseNotesAlanDix19967/31encryptiononewayfunction:cypher=f(input)–easyinput=(cypher)–hardrusedin/etc/passwdrbruteforceattack:foreachpossibleinputinpiff(inp)iscypher–gotit!
singlekeycypher=code(key,input)input=decode(key,cypher)rinDES–code=decodepublickeyencryptioncypher=code(key1,input)input=decode(key2,cypher)rkey1–giventoeveryone–publickey2–keptbyyou–privateranyonecansendamessageonlyyoucandecryptitUNIXTCP/IPShortCourseNotesAlanDix19967/32sessionkeysandauthenticationpublickeysgood,but:rexpensiverthemoreyouuseakeytheeasieritistobreakusepublickeystoexchangesinglekeyxmachineAgeneratessessionkeyKSyAencryptsitusingB'spublickeyKSB=code(KB1,KS)zAsendsKSBtoB{BdecryptsKSBtoobtainKSKS=decode(KB2,KSB)|BgeneratesvalueX|BencryptsXandKsusingA'spublickeyKXA=code(KA1,X.
KS)}BsendsKXAtoA~AdecryptsKXAX.
KS=decode(KA2,KSA)AencryptsXusingB'spublickeyXB=code(KB1,X)andsendsittoBresult:rAandBshareasecretkeyrAandBsureofeachother'sidentitydiscardkeyaftersessionorfixedtimeUNIXTCP/IPShortCourseNotesAlanDix19967/33authenticationservershowdoyoufindoutB'spublickeyanswers:xBtellsyouysomeoneelse,C,tellsyouzusephysicalmeans(post,hand)ifxory:howdoyouknowitisB/Cify:whyshouldyoubelieveCz$nogoodforbroaddistributionuseanauthenticationserverrtrustedmachinereveryonetellsittheirpublickey(usingitspublickeyorphysical)raskitforother'spublickeysroraskitforsessionkeysUNIXTCP/IPShortCourseNotesAlanDix19967/34don'tpanic!
howsecureisafaxcreditcardnumberbyphonehacker≈burglar–iftheywantin,youwon'tstopthemmaindifferences–rateofloss(Mbytes/sec)–hiddenloss(electroniccopies)–automaticattackeaseofuse≈easeofaccess–wheredoyoudrawthelineUNIXTCP/IPShortCourseNotesAlanDix19967/35
DMIT怎么样?DMIT是一家美国主机商,主要提供KVM VPS、独立服务器等,主要提供香港CN2、洛杉矶CN2 GIA等KVM VPS,稳定性、网络都很不错。支持中文客服,可Paypal、支付宝付款。2020年推出的香港国际线路的KVM VPS,大带宽,适合中转落地使用。现在有永久9折优惠码:July-4-Lite-10OFF,季付及以上还有折扣,非 中国路由优化;AS4134,AS4837 均...
新网好不好?新网域名便宜吗?新网怎么样?新网是国内老牌知名域名注册商,企业正规化运营,资质齐全,与阿里云万网和腾讯云DNSPOD同为国内服务商巨头。近日新网发布了最新的七月放价季优惠活动,主要针对域名、云主机、企业邮箱、SSL证书等多款云产品推送了超值的优惠,其中.com顶级域名仅19.9元/首年,.cn域名仅16元/首年,云主机1核心2G内存3Mbps带宽仅9.9元/月,企业邮箱更是免费送1年,...
zji怎么样?zji是一家老牌国人主机商家,公司开办在香港,这个平台主要销售独立服务器业务,和hostkvm是同一样,两个平台销售的产品类别不一平,商家的技术非常不错,机器非常稳定。昨天收到商家的优惠推送,目前针对香港邦联四型推出了65折优惠BGP线路服务器,性价比非常不错,有需要香港独立服务器的朋友可以入手,非常适合做站。zji优惠码:月付/年付优惠码:zji 物理服务器/VDS/虚拟主机空间订...
sendmail为你推荐
51自学网站网上自学网站有哪些?最好是免费的,我想学习网页设计湖南商标注册在湖南搞商标注册是代理好还是自己去好一点?湖南商标注册的流程又是什么样的呢?缓冲区溢出教程适合黑客初级学者使用的黑客工具有那些 、中国电信互联星空互联星空是什么?是电信公司的吗?依赖注入请问下依赖注入的三种方式的区别iphone越狱后怎么恢复苹果越狱后怎么恢复出厂设置ejb开发什么是ejb?系统分析员系统分析员的工作内容宽带接入服务器网络已连接,可无法连接到服务器为什么?网络已连接,可无法连接到服务rewritebasehttp怎么做自动跳转https
fc2新域名 怎样注册域名 老左 美国主机网 webhosting 视频存储服务器 免费名片模板 网页背景图片 一元域名 gg广告 52测评网 促正网秒杀 什么是服务器托管 qq云端 独立主机 可外链的相册 后门 黑科云 月付空间 防盗链 更多