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

ParkInHost - 俄罗斯VPS主机 抗投诉 55折,月付2.75欧元起

ParkInHost主机商是首次介绍到的主机商,这个商家是2013年的印度主机商,隶属于印度DiggDigital公司,主营业务有俄罗斯、荷兰、德国等机房的抗投诉虚拟主机、VPS主机和独立服务器。也看到商家的数据中心还有中国香港和美国、法国等,不过香港机房肯定不是直连的。根据曾经对于抗投诉外贸主机的了解,虽然ParkInHost以无视DMCA的抗投诉VPS和抗投诉服务器,但是,我们还是要做好数据备...

虎跃云-物理机16H/32G/50M山东枣庄高防BGP服务器低至550元每月!

虎跃科技怎么样?虎跃科技(虎跃云)是一家成立于2017年的国内专业服务商,专业主营云服务器和独立服务器(物理机)高防机房有着高端华为T级清洗能力,目前产品地区有:山东,江苏,浙江等多地区云服务器和独立服务器,今天虎跃云给大家带来了优惠活动,为了更好的促销,枣庄高防BGP服务器最高配置16核32G仅需550元/月,有需要的小伙伴可以来看看哦!产品可以支持24H无条件退款(活动产品退款请以活动规则为准...

LayerStack$10.04/月(可选中国香港、日本、新加坡和洛杉矶)高性能AMD EPYC (霄龙)云服务器,

LayerStack(成立于2017年),当前正在9折促销旗下的云服务器,LayerStack的云服务器采用第 3 代 AMD EPYC™ (霄龙) 处理器,DDR4内存和企业级 PCIe Gen 4 NVMe SSD。数据中心可选中国香港、日本、新加坡和洛杉矶!其中中国香港、日本和新加坡分为国际线路和CN2线路,如果选择CN2线路,价格每月要+3.2美元,付款支持paypal,支付宝,信用卡等!...

sendmail为你推荐
简体翻译成繁体简体字怎么换成繁体。。?人人时光机怎么查看人人网的注册时间?安卓应用平台有没有什么安卓游戏都能找到的应用商店或者游戏中心9flashIE9flash模块异常。怎么点亮qq空间图标如何点亮QQ空间图标免费免费建站电脑上有真正免费的网站吗??商标注册查询官网商标注册查询官方网站?srv记录SRV记录的简介怎么上传音乐如何将电脑上的音乐传到MP3上微信怎么看聊天记录微信在手机上怎么查看聊天记录
网络域名注册 花生壳域名 美国域名注册 国外vps租用 godaddy域名解析教程 泛域名绑定 google镜像 fastdomain godaddy evssl 大容量存储 北京主机 牛人与腾讯客服对话 权嘉云 阿里云浏览器 ftp教程 hostker 秒杀预告 可外链相册 免费美国空间 更多