Tải bản đầy đủ - 0 (trang)
5 Binding, Connecting, and Demultiplexing

5 Binding, Connecting, and Demultiplexing

Tải bản đầy đủ - 0trang

Figure22.8.CombinationoflocalIP

addressandlocalportnumberforbind.



Thefirstthreelinesaretypicalfor

serverstheybindaspecificport,termed

theserver'swell-knownport,whosevalue

isknownbytheclient.Thelastthreelines

aretypicalforclientstheydon'tcarewhat

thelocalport,termedanephemeralport,

is,aslongasitisuniqueontheclient

host.

Mostserversandmostclientsspecifythe

wildcardIPaddressinthecalltobind.This

isindicatedinFigure22.8bythenotation

*onlines3and6.

IfaserverbindsaspecificIPaddresstoa

socket(i.e.,notthewildcardaddress),

thenonlyIPdatagramsarrivingwiththat

specificIPaddressasthedestinationIP

addressbeitunicast,broadcast,or



multicastaredeliveredtotheprocess.

Naturally,whentheprocessbindsa

specificunicastorbroadcastIPaddressto

asocket,thekernelverifiesthattheIP

addresscorrespondstoalocalinterface.

Itisrare,thoughpossible,foraclientto

bindaspecificIPaddress(lines4and5in

Figure22.8).Normallyaclientbindsthe

wildcardIPaddress(thefinallineinFigure

22.8),whichletsthekernelchoosethe

outgoinginterfacebasedontheroute

chosentoreachtheserver.

Whatwedon'tshowinFigure22.8iswhat

happensiftheclienttriestobindalocal

portthatisalreadyinusewithanother

socket.Bydefaultaprocesscannotbinda

portnumberifthatportisalreadyinuse.

TheerrorEADDRINUSE(addressalready

inuse)isreturnedifthisoccurs.The

definitionofinuseissimplywhetheraPCB

existswiththatportasitslocalport.This

notionof"inuse"isrelativetoagiven

protocol:TCPorUDP,sinceTCPport

numbersareindependentofUDPport

numbers.



Net/3allowsaprocesstochangethis

defaultbehaviorbyspecifyingoneof

followingtwosocketoptions:

SO_REUSEADDR Allowstheprocessto

bindaportnumberthat

isalreadyinuse,butthe

IPaddressbeingbound

(includingthewildcard)

mustnotalreadybe

boundtothatsameport.

Forexample,ifan

attachedinterfacehas

theIPaddress

140.252.1.29thenone

socketcanbeboundto

140.252.1.29,port5555;

anothersocketcanbe

boundto127.0.0.1,port

5555;andanothersocket

canbeboundtothe

wildcardIPaddress,port

5555.Thecalltobindfor

thesecondandthird

casesmustbepreceded

byacalltosetsockopt,



settingtheso_reuseaddr

option.

SO_REUSEPORT Allowsaprocesstoreuse

boththeIPaddressand

portnumber,buteach

bindingoftheIPaddress

andportnumber,

includingthefirst,must

specifythissocket

option.With

SO_REUSEADDR,the

firstbindingoftheport

numberneednotspecify

thesocketoption.

Forexample,ifan

attachedinterfacehas

theIPaddress

140.252.1.29anda

socketisboundto

140.252.1.29,port6666

specifyingthe

SO_REUSEPORTsocket

option,thenanother

socketcanalsospecify

thissamesocketoption



andbind140.252.1.29,

port6666.



Laterinthissectionwedescribewhat

happensinthisfinalexamplewhenanIP

datagramarriveswithadestination

addressof140.252.1.29andadestination

portof6666,sincetwosocketsarebound

tothatendpoint.

TheSO_REUSEPORToptionisnewwith

Net/3andwasintroducedwiththe

supportformulticastingin4.4BSD.

Beforethisreleaseitwasneverpossible

fortwosocketstobeboundtothe

sameIPaddressandsameport

number.

Unfortunatelytheso_REUSEPORT

optionwasnotpartoftheoriginal

Stanfordmulticastsourcesandis

thereforenotwidelysupported.Other

systemsthatsupportmulticasting,such

asSolaris2.x,letaprocessspecify

SO_REUSEADDRtospecifythatitisOK

tobindmultiplesocketstothesameIP

addressandsameportnumber.



ConnectingaUDPSocket

Wenormallyassociatetheconnectsystem

callwithTCPclients,butitisalsopossible

foraUDPclientoraUDPservertocall

connectandspecifytheforeignIPaddress

andforeignportnumberforthesocket.

Thisrestrictsthesockettoexchanging

UDPdatagramswiththatoneparticular

peer.

ThereisasideeffectwhenaUDPsocketis

connected:thelocalIPaddress,ifnot

alreadyspecifiedbyacalltobind,is

automaticallysetbyconnect.Itissetto

thelocalinterfaceaddresschosenbyIP

routingtoreachthespecifiedpeer.

Figure22.9showsthethreedifferent

statesofaUDPsocketalongwiththe

pseudocodeofthefunctioncallstoendup

inthatstate.



Figure22.9.Specificationoflocaland

foreignIPaddressesandportnumbers

forUDPsockets.



Thefirstofthethreestatesiscalleda

connectedUDPsocketandthenexttwo

statesarecalledunconnectedUDP

sockets.Thedifferencebetweenthetwo

unconnectedsocketsisthatthefirsthasa

fullyspecifiedlocaladdressandthesecond

hasawildcardedlocalIPaddress.



DemultiplexingofReceivedIP

DatagramsbyTCP

Figure22.10showsthestateofthree

Telnetserversocketsonthehostsun.The

firsttwosocketsareintheLISTENstate,

waitingforincomingconnectionrequests,

andthethirdisconnectedtoaclientat

port1500onthehostwithanIPaddress

of140.252.1.11.Thefirstlisteningsocket

willhandleconnectionrequeststhatarrive

onthe140.252.1.29interfaceandthe

secondlisteningsocketwillhandleall

otherinterfaces(sinceitslocalIPaddress







isthewildcard).



Figure22.10.ThreeTCPsocketswitha

localportof23.



Weshowbothofthelisteningsocketswith

unspecifiedforeignIPaddressesandport

numbersbecausethesocketsAPIdoesn't

allowaTCPservertorestricteitherof

thesevalues.ATCPservermustaccept

theclient'sconnectionandisthentoldof

theclient'sIPaddressandportnumber

aftertheconnectionestablishmentis

complete(i.e.,whenTCP'sthree-way

handshakeiscomplete).Onlythencanthe

serverclosetheconnectionifitdoesn't

liketheclient'sIPaddressandport

number.Thisisn'tarequiredTCPfeature,

itisjustthewaythesocketsAPIhas

alwaysworked.

WhenTCPreceivesasegmentwitha

destinationportof23itsearchesthrough



itslistofInternetPCBslookingforamatch

bycallingin_pcblookup.Whenweexamine

thisfunctionshortlywe'llseethatithasa

preferenceforthesmallestnumberof

wildcardmatches.Todeterminethe

numberofwildcardmatchesweconsider

onlythelocalandforeignIPaddresses.We

donotconsidertheforeignportnumber.

Thelocalportnumbermustmatch,orwe

don'tevenconsiderthePCB.Thenumber

ofwildcardmatchescanbe0,1(localIP

addressorforeignIPaddress),or2(both

localandforeignIPaddresses).

Forexample,assumetheincoming

segmentisfrom140.252.1.11,port1500,

destinedfor140.252.1.29,port23.Figure

22.11showsthenumberofwildcard

matchesforthethreesocketsfromFigure

22.10.



Figure22.11.Incomingsegmentfrom

{140.252.1.11,1500}to{140.252.1.29,23}.



Thefirstsocketmatchesthesefourvalues,

butwithonewildcardmatch(theforeign

IPaddress).Thesecondsocketalso

matchestheincomingsegment,butwith

twowildcardmatches(thelocaland

foreignIPaddresses).Thethirdsocketisa

completematchwithnowildcards.Net/3

usesthethirdsocket,theonewiththe

smallestnumberofwildcardmatches.

Continuingthisexample,assumethe

incomingsegmentisfrom140.252.1.11,

port1501,destinedfor140.252.1.29,port

23.Figure22.12showsthenumberof

wildcardmatches.



Figure22.12.Incomingsegmentfrom

{140.252.1.11,1501}to{140.252.1.29,23}.



Thefirstsocketmatcheswithonewildcard

match;thesecondsocketmatcheswith

twowildcardmatches;andthethird

socketdoesn'tmatchatall,sincethe



foreignportnumbersareunequal.(The

foreignportnumbersarecomparedonlyif

theforeignIPaddressinthePCBisnota

wildcard.)Thefirstsocketischosen.

Inthesetwoexamplesweneversaidwhat

typeofTCPsegmentarrived:weassume

thatthesegmentinFigure22.11contains

dataoranacknowledgmentforan

establishedconnectionsinceitisdelivered

toanestablishedsocket.Wealsoassume

thatthesegmentinFigure22.12isan

incomingconnectionrequest(aSYN)since

itisdeliveredtoalisteningsocket.Butthe

demultiplexingcodeinin_pcblookup

doesn'tcare.IftheTCPsegmentisthe

wrongtypeforthesocketthatitis

deliveredto,we'llseelaterhowTCP

handlesthis.Fornowtheimportantfactis

thatthedemultiplexingcodeonly

comparesthesourceanddestination

socketpairfromtheIPdatagramagainst

thevaluesinthePCB.



DemultiplexingofReceivedIP

DatagramsbyUDP



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5 Binding, Connecting, and Demultiplexing

Tải bản đầy đủ ngay(0 tr)

×