%!PS-Adobe-3.0 %%Creator: groff version 1.02 %%DocumentNeededResources: font Times-Bold %%+ font Times-Italic %%+ font Times-Roman %%DocumentSuppliedResources: procset grops 1.02 0 %%Pages: 40 %%PageOrder: Ascend %%Orientation: Portrait %%EndComments %%BeginProlog %%BeginResource: procset grops 1.02 0 /setpacking where { pop currentpacking true setpacking } if /grops 120 dict dup begin % The ASCII code of the space character. /SC 32 def /A /show load def /B { 0 SC 3 -1 roll widthshow } bind def /C { 0 exch ashow } bind def /D { 0 exch 0 SC 5 2 roll awidthshow } bind def /E { 0 rmoveto show } bind def /F { 0 rmoveto 0 SC 3 -1 roll widthshow } bind def /G { 0 rmoveto 0 exch ashow } bind def /H { 0 rmoveto 0 exch 0 SC 5 2 roll awidthshow } bind def /I { 0 exch rmoveto show } bind def /J { 0 exch rmoveto 0 SC 3 -1 roll widthshow } bind def /K { 0 exch rmoveto 0 exch ashow } bind def /L { 0 exch rmoveto 0 exch 0 SC 5 2 roll awidthshow } bind def /M { rmoveto show } bind def /N { rmoveto 0 SC 3 -1 roll widthshow } bind def /O { rmoveto 0 exch ashow } bind def /P { rmoveto 0 exch 0 SC 5 2 roll awidthshow } bind def /Q { moveto show } bind def /R { moveto 0 SC 3 -1 roll widthshow } bind def /S { moveto 0 exch ashow } bind def /T { moveto 0 exch 0 SC 5 2 roll awidthshow } bind def % name size font SF - /SF { findfont exch [ exch dup 0 exch 0 exch neg 0 0 ] makefont dup setfont [ exch /setfont cvx ] cvx bind def } bind def % name a c d font MF - /MF { findfont [ 5 2 roll 0 3 1 roll % b neg 0 0 ] makefont dup setfont [ exch /setfont cvx ] cvx bind def } bind def /level0 0 def /RES 0 def /PL 0 def /LS 0 def % BP - /BP { /level0 save def 1 setlinecap 1 setlinejoin 72 RES div dup scale LS { 90 rotate } { 0 PL translate } ifelse 1 -1 scale } bind def /EP { level0 restore showpage } bind def % centerx centery radius startangle endangle DA - /DA { newpath arcn stroke } bind def % x y SN - x' y' % round a position to nearest (pixel + (.25,.25)) /SN { transform .25 sub exch .25 sub exch round .25 add exch round .25 add exch itransform } bind def % endx endy startx starty DL - % we round the endpoints of the line, so that parallel horizontal % and vertical lines will appear even /DL { SN moveto SN lineto stroke } bind def % centerx centery radius DC - /DC { newpath 0 360 arc closepath } bind def /TM matrix def % width height centerx centery DE - /DE { TM currentmatrix pop translate scale newpath 0 0 .5 0 360 arc closepath TM setmatrix } bind def % these are for splines /RC /rcurveto load def /RL /rlineto load def /ST /stroke load def /MT /moveto load def /CL /closepath load def % fill the last path % amount FL - /FL { currentgray exch setgray fill setgray } bind def % fill with the ``current color'' /BL /fill load def /LW /setlinewidth load def % new_font_name encoding_vector old_font_name RE - /RE { findfont dup maxlength dict begin { 1 index /FID ne { def } { pop pop } ifelse } forall /Encoding exch def dup /FontName exch def currentdict end definefont pop } bind def /DEFS 0 def % hpos vpos EBEGIN - /EBEGIN { moveto DEFS begin } bind def /EEND /end load def /CNT 0 def /level1 0 def % llx lly newwid wid newht ht newllx newlly PBEGIN - /PBEGIN { /level1 save def translate div 3 1 roll div exch scale neg exch neg exch translate % set the graphics state to default values 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin 10 setmiterlimit [] 0 setdash /setstrokeadjust where { pop false setstrokeadjust } if /setoverprint where { pop false setoverprint } if newpath /CNT countdictstack def /showpage {} def } bind def /PEND { clear countdictstack CNT sub { end } repeat level1 restore } bind def end def /setpacking where { pop setpacking } if %%EndResource %%IncludeResource: font Times-Bold %%IncludeResource: font Times-Italic %%IncludeResource: font Times-Roman grops begin/DEFS 1 dict def DEFS begin/u{.001 mul}bind def end/RES 72 def/PL 792 def/LS false def/ENC0[/asciicircum/asciitilde/Scaron/Zcaron/scaron/zcaron /Ydieresis/trademark/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef /.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef /.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/space/exclam /quotedbl/numbersign/dollar/percent/ampersand/quoteright/parenleft/parenright /asterisk/plus/comma/hyphen/period/slash/zero/one/two/three/four/five/six/seven /eight/nine/colon/semicolon/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J /K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright/circumflex /underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z /braceleft/bar/braceright/tilde/.notdef/quotesinglbase/guillemotleft /guillemotright/bullet/florin/fraction/perthousand/dagger/daggerdbl/endash /emdash/ff/fi/fl/ffi/ffl/dotlessi/dotlessj/grave/hungarumlaut/dotaccent/breve /caron/ring/ogonek/quotedblleft/quotedblright/oe/lslash/quotedblbase/OE/Lslash /.notdef/exclamdown/cent/sterling/currency/yen/brokenbar/section/dieresis /copyright/ordfeminine/guilsinglleft/logicalnot/minus/registered/macron/degree /plusminus/twosuperior/threesuperior/acute/mu/paragraph/periodcentered/cedilla /onesuperior/ordmasculine/guilsinglright/onequarter/onehalf/threequarters /questiondown/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla /Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex/Idieresis/Eth /Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis/multiply/Oslash/Ugrave /Uacute/Ucircumflex/Udieresis/Yacute/Thorn/germandbls/agrave/aacute/acircumflex /atilde/adieresis/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave /iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex/otilde /odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis/yacute/thorn /ydieresis]def/Times-Roman@0 ENC0/Times-Roman RE/Times-Italic@0 ENC0 /Times-Italic RE/Times-Bold@0 ENC0/Times-Bold RE %%EndProlog %%Page: 1 1 %%BeginPageSetup BP %%EndPageSetup /F0 12/Times-Bold@0 SF(An Advanced 4.3BSD Interpr)133.992 123 Q (ocess Communication T)-.216 E(utorial)-1.104 E/F1 10/Times-Italic@0 SF (Samuel J. Lef\215er)254.255 147 Q(Robert S. Fabry)255.92 165 Q -.55(Wi)258.415 183 S(lliam N. Joy).55 E(Phil Lapsley)262.86 201 Q/F2 10/Times-Roman@0 SF (Computer Systems Research Group)216.48 219 Q (Department of Electrical Engineering and Computer Science)166.36 231 Q (University of California, Berkeley)219.4 243 Q(Berkeley)232.5 255 Q 2.5(,C) -.65 G 2.5(alifornia 94720)279.62 255 R F1(Steve Miller)263.7 303 Q(Chris T) 264.615 321 Q(or)-.92 E(ek)-.37 E F2(Heterogeneous Systems Laboratory)216.625 339 Q(Department of Computer Science)220.93 351 Q (University of Maryland, College Park)212.315 363 Q (College Park, Maryland 20742)226.34 375 Q F1(ABSTRACT)264.385 411 Q F2 .899(T\ his document provides an introduction to the interprocess communication facili\ -)133 442.2 R(ties included in the 4.3BSD release of the UNIX* system.)108 454.2 Q 1.116(It discusses the overall model for interprocess communication an\ d introduces the)133 469.8 R .048 (interprocess communication primitives which have been added to the system.)108 481.8 R .048(The major)5.048 F(-)-.2 E .238(ity of the document considers the \ use of these primitives in developing applications.)108 493.8 R(The)5.238 E .958(reader is expected to be familiar with the C programming language as all \ examples are)108 505.8 R(written in C.)108 517.8 Q .32 LW 144 708.2 72 708.2 DL /F3 8/Times-Roman@0 SF(*)72 720 Q/F4 6/Times-Roman@0 SF(UNIX)2 E F3(is a T)2 E (rademark of Bell Laboratories.)-.28 E EP %%Page: 2 2 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 269.85(PS1:8-2 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 12/Times-Bold@0 SF(1. INTRODUCTION)234 84 Q F0 .205(One of \ the most important additions to UNIX in 4.2BSD was interprocess communication.) 72 120 R .205(These facilities)5.205 F .102 (were the result of more than two years of discussion and research.)72 132 R .101(The facilities provided in 4.2BSD incor)5.102 F(-)-.2 E .216(porated many\ of the ideas from current research, while trying to maintain the UNIX philoso\ phy of simplic-)72 144 R .472(ity and conciseness.)72 156 R .472(The current r\ elease of Berkeley UNIX, 4.3BSD, completes some of the IPC facilities)5.472 F 1.376(and provides an upward-compatible interface.)72 168 R 1.377 (It is hoped that the interprocess communication facilities)6.377 F .529 (included in 4.3BSD will establish a standard for UNIX.)72 180 R .529 (From the response to the design, it appears many)5.529 F(or)72 192 Q (ganizations carrying out work with UNIX are adopting it.)-.18 E .416 (UNIX has previously been very weak in the area of interprocess communication.) 97 207.6 R .416(Prior to the 4BSD)5.416 F .406(facilities, the only standard m\ echanism which allowed two processes to communicate were pipes \(the mpx)72 219.6 R .502(\214les which were part of V)72 231.6 R .502 (ersion 7 were experimental\).)-1.11 F(Unfortunately)5.502 E 3.002(,p)-.65 G .502(ipes are very restrictive in that the)364.908 231.6 R .314 (two communicating processes must be related through a common ancestor)72 243.6 R 5.314(.F)-.55 G(urther)385.664 243.6 Q 2.814(,t)-.4 G .314 (he semantics of pipes)417.238 243.6 R (makes them almost impossible to maintain in a distributed environment.)72 255.6 Q .193(Earlier attempts at extending the IPC facilities of UNIX have met\ with mixed reaction.)97 271.2 R .193(The majority)5.193 F .841(of the problem\ s have been related to the fact that these facilities have been tied to the UN\ IX \214le system,)72 283.2 R .569(either through naming or implementation.)72 295.2 R(Consequently)5.569 E 3.069(,t)-.65 G .569 (he IPC facilities provided in 4.3BSD have been)310.034 295.2 R 1.197 (designed as a totally independent subsystem.)72 307.2 R 1.197 (The 4.3BSD IPC allows processes to rendezvous in many)6.197 F 3.436 (ways. Processes)72 319.2 R .937(may rendezvous through a UNIX \214le system-l\ ike name space \(a space where all names)3.436 F 1.009 (are path names\) as well as through a network name space.)72 331.2 R 1.009 (In fact, new name spaces may be added at a)6.009 F 1.821 (future time with only minor changes visible to users.)72 343.2 R(Further)6.822 E 4.322(,t)-.4 G 1.822(he communication facilities have been)342.852 343.2 R .581(extended to include more than the simple byte stream provided by a pipe.) 72 355.2 R .58(These extensions have resulted)5.58 F 1.194(in a completely new\ part of the system which users will need time to familiarize themselves with.) 72 367.2 R 1.194(It is)6.194 F(likely that as more use is made of these facili\ ties they will be re\214ned; only time will tell.)72 379.2 Q .734(This documen\ t provides a high-level description of the IPC facilities in 4.3BSD and their \ use.)97 394.8 R .734(It is)5.734 F .372(designed to complement the manual page\ s for the IPC primitives by examples of their use.)72 406.8 R .372 (The remainder)5.372 F .904(of this document is or)72 418.8 R .903 (ganized in four sections.)-.18 F .903 (Section 2 introduces the IPC-related system calls and the)5.903 F .302 (basic model of communication.)72 430.8 R .303 (Section 3 describes some of the supporting library routines users may \214nd) 5.302 F .424(useful in constructing distributed applications.)72 442.8 R .423 (Section 4 is concerned with the client/server model used in)5.424 F 1.229(dev\ eloping applications and includes examples of the two major types of servers.) 72 454.8 R 1.23(Section 5 delves into)6.23 F(advanced topics which sophisticat\ ed users are likely to encounter when using the IPC facilities.)72 466.8 Q EP %%Page: 3 3 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 269.85(utorial PS1:8-3) -.35 F/F1 12/Times-Bold@0 SF(2. BASICS)260.328 84 Q F0 .044 (The basic building block for communication is the)97 123.6 R/F2 10 /Times-Italic@0 SF(socket)2.543 E F0 5.043(.A)C .043 (socket is an endpoint of communication)344.075 123.6 R .021 (to which a name may be)72 135.6 R F2(bound)2.521 E F0 5.021(.E)C .021 (ach socket in use has a)210.737 135.6 R F2(type)2.522 E F0 .022 (and one or more associated processes.)2.522 F(Sock-)5.022 E .477 (ets exist within)72 147.6 R F2 .476(communication domains)2.976 F F0 5.476(.A) C .476(communication domain is an abstraction introduced to bundle)253.188 147.6 R .266(common properties of processes communicating through sockets.)72 159.6 R .267(One such property is the scheme used to)5.266 F 1.795 (name sockets.)72 171.6 R 1.794(For example, in the UNIX communication domain \ sockets are named with UNIX path)6.795 F .569 (names; e.g. a socket may be named `)72 183.6 R(`/dev/foo')-.74 E 3.069 ('. Sockets)-.74 F .57(normally exchange data only with sockets in the)3.069 F .785(same domain \(it may be possible to cross domain boundaries, but only if \ some translation process is per)72 195.6 R(-)-.2 E 2.636(formed\). The)72 207.6 R .137(4.3BSD IPC facilities support three separate communication domains: the\ UNIX domain, for)2.637 F .585(on-system communication; the Internet domain, w\ hich is used by processes which communicate using the)72 219.6 R 1.54(the DARP) 72 231.6 R 4.04(As)-.92 G 1.541(tandard communication protocols; and the NS do\ main, which is used by processes which)129.16 231.6 R .594 (communicate using the Xerox standard communication protocols*.)72 243.6 R .594 (The underlying communication facili-)5.594 F .673(ties provided by these doma\ ins have a signi\214cant in\215uence on the internal system implementation as \ well)72 255.6 R .173(as the interface to socket facilities available to a user) 72 267.6 R 5.174(.A)-.55 G 2.674(ne)298.684 267.6 S .174 (xample of the latter is that a socket `)310.798 267.6 R(`operating')-.74 E(') -.74 E .239(in the UNIX domain sees a subset of the error conditions which are\ possible when operating in the Internet)72 279.6 R(\(or NS\) domain.)72 291.6 Q/F3 10/Times-Bold@0 SF 2.5(0.1. Socket)72 315.6 R(types)2.5 E F0 1.259 (Sockets are typed according to the communication properties visible to a user) 97 331.2 R 6.26(.P)-.55 G 1.26(rocesses are pre-)434.85 331.2 R .076(sumed to \ communicate only between sockets of the same type, although there is nothing t\ hat prevents com-)72 343.2 R .161(munication between sockets of dif)72 355.2 R .162(ferent types should the underlying communication protocols support this.) -.18 F .031(Four types of sockets currently are available to a user)97 370.8 R 5.031(.A)-.55 G F2(str)327.575 370.8 Q(eam)-.37 E F0 .031 (socket provides for the bidirectional,)2.531 F .011(reliable, sequenced, and \ unduplicated \215ow of data without record boundaries.)72 382.8 R .011 (Aside from the bidirectional-)5.011 F(ity of data \215ow)72 394.8 Q 2.5(,ap) -.65 G(air of connected stream sockets provides an interface nearly identical \ to that of pipes\207.)149.12 394.8 Q(A)97 410.4 Q F2(datagram)3.266 E F0 .766(\ socket supports bidirectional \215ow of data which is not promised to be seque\ nced, reli-)3.266 F 1.713(able, or unduplicated.)72 422.4 R 1.713(That is, a p\ rocess receiving messages on a datagram socket may \214nd messages)6.713 F .15 (duplicated, and, possibly)72 434.4 R 2.65(,i)-.65 G 2.65(na)179.02 434.4 S 2.65(no)191.11 434.4 S .15(rder dif)203.76 434.4 R .151 (ferent from the order in which it was sent.)-.18 F .151 (An important character)5.151 F(-)-.2 E .375 (istic of a datagram socket is that record boundaries in data are preserved.)72 446.4 R .374(Datagram sockets closely model)5.374 F(the facilities found in ma\ ny contemporary packet switched networks such as the Ethernet.)72 458.4 Q(A)97 474 Q F2(raw)2.721 E F0 .222(socket provides users access to the underlying co\ mmunication protocols which support socket)2.721 F 3.44(abstractions. These)72 486 R .94(sockets are normally datagram oriented, though their exact character\ istics are depen-)3.44 F .024(dent on the interface provided by the protocol.) 72 498 R .024(Raw sockets are not intended for the general user; they have) 5.024 F 1.91(been provided mainly for those interested in developing new commu\ nication protocols, or for gaining)72 510 R .363 (access to some of the more esoteric facilities of an existing protocol.)72 522 R .363(The use of raw sockets is considered)5.363 F(in section 5.)72 534 Q(A)97 549.6 Q F2 .954(sequenced packet)3.454 F F0 .953(socket is similar to a stream\ socket, with the exception that record boundaries)3.454 F .192(are preserved.) 72 561.6 R .193(This interface is provided only as part of the NS socket abstr\ action, and is very important in)5.192 F 1.733(most serious NS applications.)72 573.6 R 1.733 (Sequenced-packet sockets allow the user to manipulate the SPP or IDP)6.733 F .418(headers on a packet or a group of packets either by writing a prototype h\ eader along with whatever data is)72 585.6 R .033(to be sent, or by specifying\ a default header to be used with all outgoing data, and allows the user to re\ ceive)72 597.6 R(the headers on incoming packets.)72 609.6 Q (The use of these options is considered in section 5.)5 E 2.177 (Another potential socket type which has interesting properties is the)97 625.2 R F2 -.37(re)4.678 G 2.178(liably deliver).37 F 2.178(ed message)-.37 F F0 2.639(socket. The)72 637.2 R .139(reliably delivered message socket has simila\ r properties to a datagram socket, but with reliable)2.639 F(delivery)72 649.2 Q 6.055(.T)-.65 G 1.056(here is currently no support for this type of socket, \ but a reliably delivered message protocol)118.785 649.2 R .452 (similar to Xerox')72 661.2 R 2.952(sP)-.55 G .452 (acket Exchange Protocol \(PEX\) may be simulated at the user level.)153.636 661.2 R .452(More information)5.452 F(on this topic can be found in section 5.) 72 673.2 Q .32 LW 144 681.2 72 681.2 DL/F4 8/Times-Roman@0 SF 2.978(*S)72 693 S (ee)83.426 693 Q/F5 8/Times-Italic@0 SF .978(Internet T)2.978 F .978 (ransport Pr)-.44 F(otocols)-.296 E F4 2.977(,X)C .977 (erox System Integration Standard \(XSIS\)0281)197.929 693 R .977 (12 for more information.)-.296 F (This document is almost a necessity for one trying to write NS applications.) 72 703 Q 2.811<8749>72 714.8 S 2.811(nt)81.475 714.8 S .81(he UNIX domain, in \ fact, the semantics are identical and, as one might expect, pipes have been im\ ple-)90.51 714.8 R (mented internally as simply a pair of connected stream sockets.)72 724.8 Q EP %%Page: 4 4 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 269.85(PS1:8-4 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 10/Times-Bold@0 SF 2.5(0.2. Socket)72 84 R(cr)2.5 E(eation) -.18 E F0 1.4 -.7(To c)97 99.6 T(reate a socket the).7 E/F2 10/Times-Italic@0 SF(socket)2.5 E F0(system call is used:)2.5 E 2.5(s=s)108 117.6 S (ocket\(domain, type, protocol\);)126.42 117.6 Q .515 (This call requests that the system create a socket in the speci\214ed)72 135.6 R F2(domain)3.015 E F0 .515(and of the speci\214ed)3.015 F F2(type)3.015 E F0 5.515(.A)C(par)488.1 135.6 Q(-)-.2 E 1.118 (ticular protocol may also be requested.)72 147.6 R 1.118 (If the protocol is left unspeci\214ed \(a value of 0\), the system will)6.118 F .479(select an appropriate protocol from those protocols which comprise the \ communication domain and which)72 159.6 R .61 (may be used to support the requested socket type.)72 171.6 R .609 (The user is returned a descriptor \(a small integer num-)5.609 F .533 (ber\) which may be used in later system calls which operate on sockets.)72 183.6 R .533(The domain is speci\214ed as one of)5.533 F .051 (the manifest constants de\214ned in the \214le <)72 195.6 R F2(sys/socket.h)A F0 2.551(>. For)B .05(the UNIX domain the constant is AF_UNIX*;)2.551 F .873 (for the Internet domain AF_INET)72 207.6 R 3.373(;a)-.55 G .872 (nd for the NS domain, AF_NS.)221.065 207.6 R .872 (The socket types are also de\214ned in)5.872 F 1.128 (this \214le and one of SOCK_STREAM, SOCK_DGRAM, SOCK_RA)72 219.6 R 2.968 -.92 (W, o)-.8 H 3.628(rS).92 G(OCK_SEQP)384.882 219.6 Q 1.129(ACKET must be)-.92 F 2.5(speci\214ed. T)72 231.6 R 2.5(oc)-.7 G(reate a stream socket in the Intern\ et domain the following call might be used:)132.4 231.6 Q 2.5(s=s)108 249.6 S (ocket\(AF_INET)126.42 249.6 Q 2.5(,S)-.74 G(OCK_STREAM, 0\);)201.78 249.6 Q 1.308(This call would result in a stream socket being created with the TCP pro\ tocol providing the underlying)72 267.6 R(communication support.)72 279.6 Q 1.4 -.7(To c)5 H(reate a datagram socket for on-machine use the call might be:).7 E 2.5(s=s)108 297.6 S(ocket\(AF_UNIX, SOCK_DGRAM, 0\);)126.42 297.6 Q .661 (The default protocol \(used when the)97 319.2 R F2(pr)3.161 E(otocol)-.37 E F0 (ar)3.161 E .661(gument to the)-.18 F F2(socket)3.161 E F0 .661 (call is 0\) should be correct for)3.161 F .553(most every situation.)72 331.2 R(However)5.553 E 3.053(,i)-.4 G 3.052(ti)206.852 331.2 S 3.052(sp)215.464 331.2 S .552 (ossible to specify a protocol other than the default; this will be cov-) 227.406 331.2 R(ered in section 5.)72 343.2 Q .567 (There are several reasons a socket call may fail.)97 358.8 R .567 (Aside from the rare occurrence of lack of memory)5.567 F .004(\(ENOBUFS\), a \ socket request may fail due to a request for an unknown protocol \(EPROT)72 370.8 R(ONOSUPPOR)-.18 E(T\),)-.6 E(or a request for a type of socket for whic\ h there is no supporting protocol \(EPROT)72 382.8 Q(OTYPE\).)-.18 E F1 2.5 (0.3. Binding)72 406.8 R(local names)2.5 E F0 2.618(As)97 422.4 S .118 (ocket is created without a name.)110.728 422.4 R .118 (Until a name is bound to a socket, processes have no way to ref-)5.118 F .032 (erence it and, consequently)72 434.4 R 2.532(,n)-.65 G 2.532(om)190.338 434.4 S .032(essages may be received on it.)205.65 434.4 R .032 (Communicating processes are bound by an)5.032 F F2(association)72 446.4 Q F0 6.044(.I)C 3.544(nt)129.434 446.4 S 1.045(he Internet and NS domains, an assoc\ iation is composed of local and foreign addresses,)140.758 446.4 R 1.053(and l\ ocal and foreign ports, while in the UNIX domain, an association is composed o\ f local and foreign)72 458.4 R 1.041(path names \(the phrase `)72 470.4 R 1.041 (`foreign pathname')-.74 F 3.541('m)-.74 G 1.041 (eans a pathname created by a foreign process, not a path-)265.046 470.4 R .979 (name on a foreign system\).)72 482.4 R .979 (In most domains, associations must be unique.)5.979 F .978 (In the Internet domain there)5.979 F 1.01(may never be duplicate tuples.)72 494.4 R (UNIX)6.01 E .126(domain sockets need not always be bound to a name, but when \ bound there may never be duplicate tuples.)72 518.4 R .929 (The pathnames may not refer to \214les already existing on)5.929 F (the system in 4.3; the situation may change in future releases.)72 530.4 Q (The)97 546 Q F2(bind)2.563 E F0 .063(system call allows a process to specify \ half of an association, \(or)2.563 F (\), while the)72 558 Q F2(connect)2.5 E F0(and)2.5 E F2 (accept)2.5 E F0(primitives are used to complete a socket')2.5 E 2.5(sa)-.55 G (ssociation.)445.15 558 Q .202 (In the Internet domain, binding names to sockets can be fairly complex.)97 573.6 R(Fortunately)5.203 E 2.703(,i)-.65 G 2.703(ti)444.771 573.6 S 2.703(su) 453.034 573.6 S .203(sually not)464.627 573.6 R .744(necessary to speci\214cal\ ly bind an address and port number to a socket, because the)72 585.6 R F2 (connect)3.244 E F0(and)3.244 E F2(send)3.244 E F0(calls)3.244 E 1.171(will au\ tomatically bind an appropriate address if they are used with an unbound socke\ t.)72 597.6 R 1.172(The process of)6.172 F(binding names to NS sockets is simi\ lar in most ways to that of binding names to Internet sockets.)72 609.6 Q(The) 97 625.2 Q F2(bind)2.5 E F0(system call is used as follows:)2.5 E (bind\(s, name, namelen\);)108 643.2 Q 1.448(The bound name is a variable leng\ th byte string which is interpreted by the supporting protocol\(s\).)72 661.2 R (Its)6.447 E .557(interpretation may vary from communication domain to communi\ cation domain \(this is one of the proper)72 673.2 R(-)-.2 E 1.94 (ties which comprise the `)72 685.2 R(`domain')-.74 E 4.44('\). As)-.74 F 1.94 (mentioned, in the Internet domain names contain an Internet)4.44 F .023 (address and port number)72 697.2 R 5.023(.N)-.55 G 2.523(Sd)184.851 697.2 S .023(omain names contain an NS address and port number)197.934 697.2 R 5.023 (.I)-.55 G 2.523(nt)421.721 697.2 S .023(he UNIX domain,)432.024 697.2 R .32 LW 144 705.2 72 705.2 DL/F3 8/Times-Roman@0 SF 2.551(*T)72 717 S .551 (he manifest constants are named AF_whatever as they indicate the `)83.439 717 R .551(`address format')-.592 F 2.551('t)-.592 G 2.551(ou)366.131 717 S .551 (se in interpreting)376.682 717 R(names.)72 727 Q EP %%Page: 5 5 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 269.85(utorial PS1:8-5) -.35 F 1.433(names contain a path name and a family)72 84 R 3.932(,w)-.65 G 1.432(hich is always AF_UNIX.)255.83 84 R 1.432(If one wanted to bind the name) 6.432 F -.74(``)72 96 S(/tmp/foo').74 E 2.5('t)-.74 G 2.5(oaU)123.57 96 S (NIX domain socket, the following code would be used*:)145.23 96 Q (#include )108 114 Q(...)110.5 126 Q(struct sockaddr_un addr;)108 138 Q(...)110.5 150 Q(strcpy\(addr)108 162 Q(.sun_path, "/tmp/foo"\);)-.55 E(addr) 108 174 Q(.sun_family = AF_UNIX;)-.55 E(bind\(s, \(struct sockaddr *\) &addr) 108 186 Q 2.5(,s)-.4 G(trlen\(addr)249.52 186 Q(.sun_path\) +)-.55 E (sizeof \(addr)118 198 Q(.sun_family\)\);)-.55 E .079(Note that in determining\ the size of a UNIX domain address null bytes are not counted, which is why)72 216 R/F1 10/Times-Italic@0 SF(strlen)2.578 E F0 1.052(is used.)72 228 R 1.052(\ In the current implementation of UNIX domain IPC under 4.3BSD, the \214le name\ referred to in)6.052 F F1(addr)72 240 Q(.sun_path)-1.11 E F0 .155 (is created as a socket in the system \214le space.)2.656 F .155 (The caller must, therefore, have write permis-)5.155 F .102 (sion in the directory where)72 252 R F1(addr)2.602 E(.sun_path)-1.11 E F0 .102 (is to reside, and this \214le should be deleted by the caller when it is)2.602 F(no longer needed.)72 264 Q (Future versions of 4BSD may not create this \214le.)5 E (In binding an Internet address things become more complicated.)97 279.6 Q (The actual call is similar)5 E(,)-.4 E(#include )108 297.6 Q (#include )108 309.6 Q(...)110.5 321.6 Q(struct sockaddr_in sin;) 108 333.6 Q(...)110.5 345.6 Q (bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)108 357.6 Q .867 (but the selection of what to place in the address)72 375.6 R F1(sin)3.367 E F0 .867(requires some discussion.)3.367 F 2.467 -.8(We w)5.867 H .866 (ill come back to the).8 F .971(problem of formulating Internet addresses in s\ ection 3 when the library routines used in name resolution)72 387.6 R (are discussed.)72 399.6 Q .123 (Binding an NS address to a socket is even more dif)97 415.2 R .122 (\214cult, especially since the Internet library routines)-.18 F (do not work with NS hostnames.)72 427.2 Q(The actual call is again similar:)5 E(#include )108 445.2 Q(#include )108 457.2 Q(...) 110.5 469.2 Q(struct sockaddr_ns sns;)108 481.2 Q(...)110.5 493.2 Q (bind\(s, \(struct sockaddr *\) &sns, sizeof \(sns\)\);)108 505.2 Q (Again, discussion of what to place in a `)72 523.2 Q(`struct sockaddr_ns')-.74 E 2.5('w)-.74 G(ill be deferred to section 3.)325.75 523.2 Q/F2 10/Times-Bold@0 SF 2.5(0.4. Connection)72 547.2 R(establishment)2.5 E F0 2.793 (Connection establishment is usually asymmetric, with one process a `)97 562.8 R(`client')-.74 E 5.293('a)-.74 G 2.793(nd the other a)440.91 562.8 R -.74(``) 72 574.8 S -2.13(server ').74 F 2.772('. The)-.74 F(server)2.772 E 2.772(,w)-.4 G .272(hen willing to of)171.256 574.8 R .272 (fer its advertised services, binds a socket to a well-known address)-.18 F .747(associated with the service and then passively `)72 586.8 R(`listens')-.74 E 3.248('o)-.74 G 3.248(ni)308.678 586.8 S .748(ts socket.)319.706 586.8 R .748 (It is then possible for an unrelated)5.748 F .178 (process to rendezvous with the server)72 598.8 R 5.178(.T)-.55 G .177 (he client requests services from the server by initiating a `)236.368 598.8 R (`connec-)-.74 E(tion')72 610.8 Q 3.051('t)-.74 G 3.051(ot)99.311 610.8 S .551 (he server)110.142 610.8 R 1.651 -.55('s s).37 H 3.051(ocket. On).55 F .551 (the client side the)3.051 F F1(connect)3.052 E F0 .552 (call is used to initiate a connection.)3.052 F .552(Using the)5.552 F (UNIX domain, this might appear as,)72 622.8 Q(struct sockaddr_un server;)108 640.8 Q(...)110.5 652.8 Q(connect\(s, \(struct sockaddr *\)&server)108 664.8 Q 2.5(,s)-.4 G(trlen\(server)267 664.8 Q(.sun_path\) +)-.55 E(sizeof \(server)118 676.8 Q(.sun_family\)\);)-.55 E(while in the Internet domain,)72 694.8 Q .32 LW 144 702.8 72 702.8 DL/F3 8/Times-Roman@0 SF 2.147(*N)72 714.6 S .148 (ote that, although the tendency here is to call the `)83.923 714.6 R -1.704 (`addr ')-.592 F 2.148('s)-.592 G .148(tructure `)271.732 714.6 R(`sun')-.592 E .148(', doing so would cause problems if)-.592 F (the code were ever ported to a Sun workstation.)72 724.6 Q EP %%Page: 6 6 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 269.85(PS1:8-6 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(struct sockaddr_in server;)108 84 Q(...)110.5 96 Q (connect\(s, \(struct sockaddr *\)&server)108 108 Q 2.5(,s)-.4 G (izeof \(server\)\);)267 108 Q(and in the NS domain,)72 126 Q (struct sockaddr_ns server;)108 144 Q(...)110.5 156 Q (connect\(s, \(struct sockaddr *\)&server)108 168 Q 2.5(,s)-.4 G (izeof \(server\)\);)267 168 Q(where)72 186 Q/F1 10/Times-Italic@0 SF(server) 3.826 E F0 1.325(in the example above would contain either the UNIX pathname, \ Internet address and port)3.826 F(number)72 198 Q 3.361(,o)-.4 G 3.361(rN) 113.011 198 S 3.361(Sa)126.922 198 S .862(ddress and port number of the server\ to which the client process wishes to speak.)140.283 198 R .862(If the)5.862 F .611(client process')72 210 R 3.111(ss)-.55 G .611(ocket is unbound at the tim\ e of the connect call, the system will automatically select and)140.992 210 R .09(bind a name to the socket if necessary; c.f. section 5.4.)72 222 R .09 (This is the usual way that local addresses are bound)5.09 F(to a socket.)72 234 Q .787(An error is returned if the connection was unsuccessful \(any name \ automatically bound by the sys-)97 249.6 R .888(tem, however)72 261.6 R 3.388 (,r)-.4 G 3.388(emains\). Otherwise,)136.136 261.6 R .889 (the socket is associated with the server and data transfer may begin.)3.388 F (Some of the more common errors returned when a connection attempt fails are:) 72 273.6 Q(ETIMEDOUT)72 289.2 Q .469(After failing to establish a connection f\ or a period of time, the system decided there was no point in)97 301.2 R .404 (retrying the connection attempt any more.)97 313.2 R .404 (This usually occurs because the destination host is down,)5.404 F (or because problems in the network resulted in transmissions being lost.)97 325.2 Q(ECONNREFUSED)72 340.8 Q .147(The host refused service for some reason.) 97 352.8 R .146(This is usually due to a server process not being present at) 5.147 F(the requested name.)97 364.8 Q(ENETDOWN or EHOSTDOWN)72 380.4 Q .713(T\ hese operational errors are returned based on status information delivered to \ the client host by the)97 392.4 R(underlying communication services.)97 404.4 Q (ENETUNREACH or EHOSTUNREACH)72 420 Q 1.034(These operational errors can occur\ either because the network or host is unknown \(no route to the)97 432 R 1.121 (network or host is present\), or because of status information returned by in\ termediate gateways or)97 444 R 1.705(switching nodes.)97 456 R 1.704 (Many times the status returned is not suf)6.705 F 1.704 (\214cient to distinguish a network being)-.18 F(down from a host being down, \ in which case the system indicates the entire network is unreachable.)97 468 Q .203(For the server to receive a client')97 483.6 R 2.703(sc)-.55 G .203 (onnection it must perform two steps after binding its socket.)240.88 483.6 R (The)5.203 E(\214rst is to indicate a willingness to listen for incoming conne\ ction requests:)72 495.6 Q(listen\(s, 5\);)108 513.6 Q .901 (The second parameter to the)72 531.6 R F1(listen)3.401 E F0 .901 (call speci\214es the maximum number of outstanding connections which)3.401 F 1.64(may be queued awaiting acceptance by the server process; this number may \ be limited by the system.)72 543.6 R .393(Should a connection be requested whi\ le the queue is full, the connection will not be refused, but rather the)72 555.6 R .476(individual messages which comprise the request will be ignored.)72 567.6 R .477(This gives a harried server time to make)5.476 F .786(room in its\ pending connection queue while the client retries the connection request.)72 579.6 R .786(Had the connection)5.786 F .646 (been returned with the ECONNREFUSED error)72 591.6 R 3.146(,t)-.4 G .647 (he client would be unable to tell if the server was up or)276.276 591.6 R 2.925(not. As)72 603.6 R .425(it is now it is still possible to get the ETIMED\ OUT error back, though this is unlikely)2.925 F 5.425(.T)-.65 G .425(he back-) 469.425 603.6 R .588(log \214gure supplied with the listen call is currently l\ imited by the system to a maximum of 5 pending con-)72 615.6 R .65 (nections on any one queue.)72 627.6 R .649 (This avoids the problem of processes hogging system resources by setting an) 5.65 F(in\214nite backlog, then ignoring all connection requests.)72 639.6 Q -.4(Wi)97 655.2 S(th a socket marked as listening, a server may).4 E F1(accept) 2.5 E F0 2.5(ac)2.5 G(onnection:)330.43 655.2 Q(struct sockaddr_in from;)108 673.2 Q(...)110.5 685.2 Q(fromlen = sizeof \(from\);)108 697.2 Q (newsock = accept\(s, \(struct sockaddr *\)&from, &fromlen\);)108 709.2 Q 1.406 (\(For the UNIX domain,)72 727.2 R F1(fr)3.906 E(om)-.37 E F0 1.406 (would be declared as a)3.906 F F1 1.407(struct sockaddr_un)3.907 F F0 3.907 (,a)C 1.407(nd for the NS domain,)386.785 727.2 R F1(fr)3.907 E(om)-.37 E EP %%Page: 7 7 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 269.85(utorial PS1:8-7) -.35 F .226(would be declared as a)72 84 R/F1 10/Times-Italic@0 SF .226 (struct sockaddr_ns)2.726 F F0 2.726(,b)C .226(ut nothing dif)252.992 84 R .226 (ferent would need to be done as far as)-.18 F F1(fr)2.725 E(omlen)-.37 E F0 (is)2.725 E 3.459(concerned. In)72 96 R .959(the examples which follow)3.459 F 3.459(,o)-.65 G .959(nly Internet routines will be discussed.\))254.564 96 R 3.46(An)5.96 G .96(ew descriptor is)438.76 96 R .049 (returned on receipt of a connection \(along with a new socket\).)72 108 R .048 (If the server wishes to \214nd out who its client)5.048 F .415 (is, it may supply a buf)72 120 R .415(fer for the client socket')-.18 F 2.915 (sn)-.55 G 2.915(ame. The)271.78 120 R .416(value-result parameter)2.915 F F1 (fr)2.916 E(omlen)-.37 E F0 .416(is initialized by)2.916 F .861 (the server to indicate how much space is associated with)72 132 R F1(fr)3.361 E(om)-.37 E F0 3.36(,t)C .86(hen modi\214ed on return to re\215ect the true) 336.06 132 R(size of the name.)72 144 Q(If the client')5 E 2.5(sn)-.55 G (ame is not of interest, the second parameter may be a null pointer)205.03 144 Q(.)-.55 E F1(Accept)97 159.6 Q F0 .561(normally blocks.)3.061 F .562(That is,) 5.562 F F1(accept)3.062 E F0 .562 (will not return until a connection is available or the system)3.062 F .197 (call is interrupted by a signal to the process.)72 171.6 R(Further)5.197 E 2.697(,t)-.4 G .197(here is no way for a process to indicate it will accept) 291.603 171.6 R .314 (connections from only a speci\214c individual, or individuals.)72 183.6 R .314 (It is up to the user process to consider who the)5.314 F .409(connection is f\ rom and close down the connection if it does not wish to speak to the process.) 72 195.6 R .409(If the server)5.409 F .054(process wants to accept connections\ on more than one socket, or wants to avoid blocking on the accept call,)72 207.6 R(there are alternatives; they will be considered in section 5.)72 219.6 Q/F2 10/Times-Bold@0 SF 2.5(0.5. Data)72 243.6 R(transfer)2.5 E F0 -.4(Wi)97 259.2 S .455(th a connection established, data may begin to \215ow).4 F 5.455 (.T)-.65 G 2.955(os)329.875 259.2 S .454 (end and receive data there are a number)341.72 259.2 R .751 (of possible calls.)72 271.2 R -.4(Wi)5.751 G .751(th the peer entity at each \ end of a connection anchored, a user can send or receive a).4 F .042 (message without specifying the peer)72 283.2 R 5.042(.A)-.55 G 2.542(so)231.91 283.2 S .042(ne might expect, in this case, then the normal)243.342 283.2 R F1 -.37(re)2.541 G(ad).37 E F0(and)2.541 E F1(write)2.541 E F0(sys-)2.541 E (tem calls are usable,)72 295.2 Q(write\(s, buf, sizeof \(buf\)\);)108 313.2 Q (read\(s, buf, sizeof \(buf\)\);)108 325.2 Q(In addition to)72 343.2 Q F1 -.37 (re)2.5 G(ad).37 E F0(and)2.5 E F1(write)2.5 E F0 2.5(,t)C(he new calls)194.13 343.2 Q F1(send)2.5 E F0(and)2.5 E F1 -.37(re)2.5 G(cv).37 E F0(may be used:) 2.5 E(send\(s, buf, sizeof \(buf\), \215ags\);)108 361.2 Q (recv\(s, buf, sizeof \(buf\), \215ags\);)108 373.2 Q(While)72 391.2 Q F1(send) 3.492 E F0(and)3.492 E F1 -.37(re)3.492 G(cv).37 E F0 .992 (are virtually identical to)3.492 F F1 -.37(re)3.492 G(ad).37 E F0(and)3.492 E F1(write)3.492 E F0 3.492(,t)C .993(he extra)331.022 391.2 R F1(\215ags)3.493 E F0(ar)3.493 E .993(gument is important.)-.18 F(The)5.993 E .955 (\215ags, de\214ned in)72 403.2 R F1()3.455 E F0 3.455(,m)C .954 (ay be speci\214ed as a non-zero value if one or more of the following is)215.7 403.2 R(required:)72 415.2 Q 2.5(ll)108 439.2 S(.)116.06 439.2 Q -.28 (MSG_OOB send/receive)108 451.2 R(out of band data)2.5 E 20.83(MSG_PEEK look) 108 463.2 R(at data without reading)2.5 E 9.73(MSG_DONTROUTE send)108 475.2 R (data without routing packets)2.5 E .823(Out of band data is a notion speci\ \214c to stream sockets, and one which we will not immediately consider)72 499.2 R(.)-.55 E .607(The option to have data sent without routing applied to \ the outgoing packets is currently used only by the)72 511.2 R .839(routing tab\ le management process, and is unlikely to be of interest to the casual user)72 523.2 R 5.84(.T)-.55 G .84(he ability to pre-)435.1 523.2 R 1.442 (view data is, however)72 535.2 R 3.942(,o)-.4 G 3.942(fi)174.568 535.2 S 3.942 (nterest. When)184.62 535.2 R 1.442(MSG_PEEK is speci\214ed with a)3.942 F F1 -.37(re)3.942 G(cv).37 E F0 1.441(call, any data present is)3.942 F .652 (returned to the user)72 547.2 R 3.152(,b)-.4 G .652(ut treated as still `) 161.688 547.2 R(`unread')-.74 E 3.152('. That)-.74 F .652(is, the next)3.152 F F1 -.37(re)3.152 G(ad).37 E F0(or)3.152 E F1 -.37(re)3.152 G(cv).37 E F0 .652 (call applied to the socket)3.152 F(will return the data previously previewed.) 72 559.2 Q F2 2.5(0.6. Discarding)72 583.2 R(sockets)2.5 E F0 (Once a socket is no longer of interest, it may be discarded by applying a)97 598.8 Q F1(close)2.5 E F0(to the descriptor)2.5 E(,)-.4 E(close\(s\);)108 616.8 Q 1.349(If data is associated with a socket which promises reliable delivery \ \(e.g. a stream socket\) when a close)72 634.8 R .446 (takes place, the system will continue to attempt to transfer the data.)72 646.8 R(However)5.447 E 2.947(,a)-.4 G .447(fter a fairly long period of) 397.625 646.8 R .204 (time, if the data is still undelivered, it will be discarded.)72 658.8 R .203 (Should a user have no use for any pending data, it)5.203 F(may perform a)72 670.8 Q F1(shutdown)2.5 E F0(on the socket prior to closing it.)2.5 E (This call is of the form:)5 E(shutdown\(s, how\);)108 688.8 Q(where)72 706.8 Q F1(how)2.993 E F0 .494(is 0 if the user is no longer interested in reading dat\ a, 1 if no more data will be sent, or 2 if no)2.993 F (data is to be sent or received.)72 718.8 Q EP %%Page: 8 8 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 269.85(PS1:8-8 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 10/Times-Bold@0 SF 2.5(0.7. Connectionless)72 84 R(sockets) 2.5 E F0 3.542 -.7(To t)97 99.6 T 2.141(his point we have been concerned mostl\ y with sockets which follow a connection oriented).7 F 4.143(model. However)72 111.6 R 4.143(,t)-.4 G 1.643(here is also support for connectionless interacti\ ons typical of the datagram facilities)151.816 111.6 R 1.208 (found in contemporary packet switched networks.)72 123.6 R 3.708(Ad)6.208 G 1.207(atagram socket provides a symmetric interface to)300.136 123.6 R .507 (data exchange.)72 135.6 R .507 (While processes are still likely to be client and server)5.507 F 3.007(,t)-.4 G .507(here is no requirement for connec-)363.454 135.6 R(tion establishment.) 72 147.6 Q(Instead, each message includes the destination address.)5 E 1.105 (Datagram sockets are created as before.)97 163.2 R 1.105 (If a particular local address is needed, the)6.105 F/F2 10/Times-Italic@0 SF (bind)3.604 E F0(operation)3.604 E .388 (must precede the \214rst data transmission.)72 175.2 R .389 (Otherwise, the system will set the local address and/or port when)5.389 F (data is \214rst sent.)72 187.2 Q 1.4 -.7(To s)5 H(end data, the).7 E F2 (sendto)2.5 E F0(primitive is used,)2.5 E (sendto\(s, buf, bu\215en, \215ags, \(struct sockaddr *\)&to, tolen\);)108 205.2 Q(The)72 223.2 Q F2(s)2.786 E F0(,)A F2(buf)2.786 E F0(,)A F2(bu\215en) 2.786 E F0 2.786(,a)C(nd)151.744 223.2 Q F2(\215ags)2.786 E F0 .286 (parameters are used as before.)2.786 F(The)5.285 E F2(to)2.785 E F0(and)2.785 E F2(tolen)2.785 E F0 .285(values are used to indicate the)2.785 F 2.213 (address of the intended recipient of the message.)72 235.2 R 2.214 (When using an unreliable datagram interface, it is)7.213 F .596 (unlikely that any errors will be reported to the sender)72 247.2 R 5.596(.W) -.55 G .596(hen information is present locally to recognize a)306.81 247.2 R .344(message that can not be delivered \(for instance when a network is unreac\ hable\), the call will return \2551 and)72 259.2 R(the global value)72 271.2 Q F2(errno)2.5 E F0(will contain an error number)2.5 E(.)-.55 E 1.4 -.7(To r)97 286.8 T(eceive messages on an unconnected datagram socket, the).7 E F2 -.37(re) 2.5 G(cvfr).37 E(om)-.37 E F0(primitive is provided:)2.5 E (recvfrom\(s, buf, bu\215en, \215ags, \(struct sockaddr *\)&from, &fromlen\);) 108 304.8 Q .778(Once again, the)72 322.8 R F2(fr)3.278 E(omlen)-.37 E F0 .777 (parameter is handled in a value-result fashion, initially containing the size\ of the)3.278 F F2(fr)72 334.8 Q(om)-.37 E F0(buf)2.931 E(fer)-.18 E 2.931(,a) -.4 G .431(nd modi\214ed on return to indicate the actual size of the address \ from which the datagram was)127.172 334.8 R(received.)72 346.8 Q .121(In addit\ ion to the two calls mentioned above, datagram sockets may also use the)97 362.4 R F2(connect)2.621 E F0 .121(call to asso-)2.621 F .84 (ciate a socket with a speci\214c destination address.)72 374.4 R .84 (In this case, any data sent on the socket will automati-)5.84 F .059 (cally be addressed to the connected peer)72 386.4 R 2.559(,a)-.4 G .059 (nd only data received from that peer will be delivered to the user)242.513 386.4 R(.)-.55 E .178(Only one connected address is permitted for each socket \ at one time; a second connect will change the des-)72 398.4 R .563(tination ad\ dress, and a connect to a null address \(family AF_UNSPEC\) will disconnect.)72 410.4 R .562(Connect requests)5.562 F .896 (on datagram sockets return immediately)72 422.4 R 3.396(,a)-.65 G 3.396(st) 245.8 422.4 S .896(his simply results in the system recording the peer)255.866 422.4 R 1.996 -.55('s a).37 H(ddress).55 E .581(\(as compared to a stream sock\ et, where a connect request initiates establishment of an end to end connec-)72 434.4 R(tion\).)72 446.4 Q F2(Accept)5 E F0(and)2.5 E F2(listen)2.5 E F0 (are not used with datagram sockets.)2.5 E 1.14 (While a datagram socket socket is connected, errors from recent)97 462 R F2 (send)3.641 E F0 1.141(calls may be returned asyn-)3.641 F(chronously)72 474 Q 6.738(.T)-.65 G 1.738(hese errors may be reported on subsequent operations on \ the socket, or a special socket)131.138 474 R .267(option used with)72 486 R F2 (getsockopt)2.767 E F0 2.767(,S)C .267 (O_ERROR, may be used to interrogate the error status.)195.568 486 R(A)5.268 E F2(select)2.768 E F0 .268(for reading)2.768 F .793 (or writing will return true when an error indication has been received.)72 498 R .793(The next operation will return the)5.793 F(error)72 510 Q 3.072(,a)-.4 G .572(nd the error status is cleared.)101.042 510 R .573 (Other of the less important details of datagram sockets are described)5.572 F (in section 5.)72 522 Q F1 2.5(0.8. Input/Output)72 546 R(multiplexing)2.5 E F0 .671(One last facility often used in developing applications is the ability to\ multiplex i/o requests among)97 561.6 R(multiple sockets and/or \214les.)72 573.6 Q(This is done using the)5 E F2(select)2.5 E F0(call:)2.5 E (#include )108 591.6 Q(#include )108 603.6 Q(...)110.5 615.6 Q(fd_set readmask, writemask, exceptmask;)108 639.6 Q (struct timeval timeout;)108 651.6 Q(...)110.5 663.6 Q (select\(nfds, &readmask, &writemask, &exceptmask, &timeout\);)108 675.6 Q F2 (Select)72 693.6 Q F0 .031(takes as ar)2.531 F .032(guments pointers to three \ sets, one for the set of \214le descriptors for which the caller wishes)-.18 F .12(to be able to read data on, one for those descriptors to which data is to \ be written, and one for which excep-)72 705.6 R .392(tional conditions are pen\ ding; out-of-band data is the only exceptional condition currently implemented\ by)72 717.6 R 3.359(the socket If the user is not interested in certain condi\ tions \(i.e., read, write, or exceptions\), the)72 729.6 R EP %%Page: 9 9 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 269.85(utorial PS1:8-9) -.35 F(corresponding ar)72 84 Q(gument to the)-.18 E/F1 10/Times-Italic@0 SF (select)2.5 E F0(should be a null pointer)2.5 E(.)-.55 E .563(Each set is actu\ ally a structure containing an array of long integer bit masks; the size of th\ e array is)97 99.6 R .484(set by the de\214nition FD_SETSIZE.)72 111.6 R .483 (The array is be long enough to hold one bit for each of FD_SETSIZE)5.484 F (\214le descriptors.)72 123.6 Q 2.347(The macros FD_SET\()97 139.2 R F1 2.347 (fd, &mask)B F0 4.847(\)a)C 2.347(nd FD_CLR\()246.088 139.2 R F1 2.347 (fd, &mask)B F0 4.848(\)h)C 2.348(ave been provided for adding and)358.13 139.2 R 3.031(removing \214le descriptor)72 151.2 R F1(fd)5.531 E F0 3.031 (in the set)5.531 F F1(mask)5.531 E F0 8.031(.T)C 3.03 (he set should be zeroed before use, and the macro)277.898 151.2 R(FD_ZERO\()72 163.2 Q F1(&mask)A F0 2.838(\)h)C .338(as been provided to clear the set) 158.718 163.2 R F1(mask)2.839 E F0 5.339(.T)C .339(he parameter)326.387 163.2 R F1(nfds)2.839 E F0 .339(in the)2.839 F F1(select)2.839 E F0 .339 (call speci\214es)2.839 F(the range of \214le descriptors)72 175.2 Q (\(i.e. one plus the value of the lar)5 E (gest descriptor\) to be examined in a set.)-.18 E 3.139(At)97 190.8 S .639(im\ eout value may be speci\214ed if the selection is not to last more than a pred\ etermined period of)110.139 190.8 R 3.19(time. If)72 202.8 R .69 (the \214elds in)3.19 F F1(timeout)3.19 E F0 .691 (are set to 0, the selection takes the form of a)3.19 F F1(poll)3.191 E F0 3.191(,r)C .691(eturning immediately)403.278 202.8 R 5.691(.I)-.65 G(f)500.67 202.8 Q .413(the last parameter is a null pointer)72 214.8 R 2.913(,t)-.4 G .413(he selection will block inde\214nitely*.)218.361 214.8 R F1(Select)5.413 E F0 .414(normally returns the num-)2.913 F 1.308 (ber of \214le descriptors selected; if the)72 226.8 R F1(select)3.808 E F0 1.307(call returns due to the timeout expiring, then the value 0 is)3.807 F 2.69(returned. If)72 238.8 R(the)2.69 E F1(select)2.69 E F0 .19(terminates bec\ ause of an error or interruption, a \2551 is returned with the error number) 2.69 F(in)72 250.8 Q F1(errno)2.5 E F0 2.5(,a)C (nd with the \214le descriptor masks unchanged.)113.94 250.8 Q .634(Assuming a\ successful return, the three sets will indicate which \214le descriptors are \ ready to be read)97 266.4 R .987 (from, written to, or have exceptional conditions pending.)72 278.4 R .987 (The status of a \214le descriptor in a select mask)5.987 F .61 (may be tested with the)72 290.4 R F1 .61(FD_ISSET\(fd, &mask\))3.11 F F0 .609 (macro, which returns a non-zero value if)3.11 F F1(fd)3.109 E F0 .609 (is a member of)3.109 F(the set)72 302.4 Q F1(mask)2.5 E F0 2.5(,a)C (nd 0 if it is not.)130.32 302.4 Q 1.459 -.7(To d)97 318 T .059 (etermine if there are connections waiting on a socket to be used with an).7 F F1(accept)2.56 E F0(call,)2.56 E F1(select)2.56 E F0 .06(can be)2.56 F .34 (used, followed by a)72 330 R F1 .34(FD_ISSET\(fd, &mask\))2.84 F F0 .34 (macro to check for read readiness on the appropriate socket.)2.84 F(If)5.34 E F1(FD_ISSET)72 342 Q F0 1.415(returns a non-zero value, indicating permission \ to read, then a connection is pending on the)3.915 F(socket.)72 354 Q .404 (As an example, to read data from two sockets,)97 369.6 R F1(s1)2.903 E F0(and) 2.903 E F1(s2)2.903 E F0 .403(as it is available from each and with a one-) 2.903 F(second timeout, the following code might be used:)72 381.6 Q .32 LW 144 698.2 72 698.2 DL/F2 8/Times-Roman@0 SF 2.296(*T)72 710 S 2.296(ob)82.624 710 S 2.296(em)92.92 710 S .295(ore speci\214c, a return takes place only when a des\ criptor is selectable, or when a signal is received by)104.992 710 R (the caller)72 720 Q 2(,i)-.32 G(nterrupting the system call.)107.448 720 Q EP %%Page: 10 10 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-10 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(#include )108 84 Q(#include )108 96 Q (...)110.5 108 Q(fd_set read_template;)108 120 Q(struct timeval wait;)108 132 Q (...)110.5 144 Q(for \(;;\) {)108 156 Q(wait.tv_sec = 1;)133 168 Q (/* one second */)233 168 Q(wait.tv_usec = 0;)133 180 Q (FD_ZERO\(&read_template\);)133 204 Q(FD_SET\(s1, &read_template\);)133 228 Q (FD_SET\(s2, &read_template\);)133 240 Q(nb = select\(FD_SETSIZE, &read_templa\ te, \(fd_set *\) 0, \(fd_set *\) 0, &wait\);)133 264 Q(if \(nb <= 0\) {)133 276 Q/F1 10/Times-Italic@0 SF(An err)158 288 Q(or occurr)-.37 E(ed during the)-.37 E F0(select)2.5 E F1 2.5(,o)C(r)309.74 288 Q(the)158 300 Q F0(select)2.5 E F1 (timed out.)2.5 E F0(})133 312 Q(if \(FD_ISSET\(s1, &read_template\)\) {)133 336 Q F1(Socket #1 is r)158 348 Q(eady to be r)-.37 E(ead fr)-.37 E(om.)-.37 E F0(})133 360 Q(if \(FD_ISSET\(s2, &read_template\)\) {)133 384 Q F1 (Socket #2 is r)158 396 Q(eady to be r)-.37 E(ead fr)-.37 E(om.)-.37 E F0(})133 408 Q(})108 420 Q .768(In 4.2, the ar)97 441.6 R .768(guments to)-.18 F F1 (select)3.268 E F0 .768(were pointers to integers instead of pointers to)3.268 F F1(fd_set)3.269 E F0 3.269(s. This)B .769(type of)3.269 F .435(call will sti\ ll work as long as the number of \214le descriptors being examined is less tha\ n the number of bits)72 453.6 R(in an integer; however)72 465.6 Q 2.5(,t)-.4 G (he methods illustrated above should be used in all current programs.)169.08 465.6 Q F1(Select)97 481.2 Q F0 1.259 (provides a synchronous multiplexing scheme.)3.759 F 1.259 (Asynchronous noti\214cation of output comple-)6.259 F .604 (tion, input availability)72 493.2 R 3.104(,a)-.65 G .604(nd exceptional condi\ tions is possible through use of the SIGIO and SIGURG sig-)171.222 493.2 R (nals described in section 5.)72 505.2 Q EP %%Page: 11 11 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 265.22(utorial PS1:8-1) -.35 F(1)-.37 E/F1 12/Times-Bold@0 SF(3. NETWORK LIBRAR)188.202 84 Q 3(YR)-.42 G(OUTINES)333.786 84 Q F0 1.377(The discussion in section 2 indicated the poss\ ible need to locate and construct network addresses)97 123.6 R 1.201(when usin\ g the interprocess communication facilities in a distributed environment.)72 135.6 R 2.6 -.7(To a)6.201 H 1.2(id in this task a).7 F .213 (number of routines have been added to the standard C run-time library)72 147.6 R 5.213(.I)-.65 G 2.713(nt)366.622 147.6 S .213 (his section we will consider the)377.115 147.6 R .889 (new routines provided to manipulate network addresses.)72 159.6 R .888 (While the 4.3BSD networking facilities support)5.888 F .272(both the DARP)72 171.6 R 2.772(As)-.92 G .273(tandard Internet protocols and the Xerox NS proto\ cols, most of the routines presented in)147.176 171.6 R .22 (this section do not apply to the NS domain.)72 183.6 R .22 (Unless otherwise stated, it should be assumed that the routines)5.22 F (presented in this section do not apply to the NS domain.)72 195.6 Q .929(Loca\ ting a service on a remote host requires many levels of mapping before client \ and server may)97 211.2 R 3.89(communicate. A)72 223.2 R 1.39 (service is assigned a name which is intended for human consumption; e.g. `) 3.89 F(`the)-.74 E/F2 10/Times-Italic@0 SF(login)3.89 E(server)72 235.2 Q F0 1.05(on host monet')3.55 F 3.55('. This)-.74 F 1.051 (name, and the name of the peer host, must then be translated into network)3.55 F F2(addr)72 247.2 Q(esses)-.37 E F0 .715 (which are not necessarily suitable for human consumption.)3.215 F(Finally) 5.715 E 3.215(,t)-.65 G .715(he address must then used)396.71 247.2 R .412 (in locating a physical)72 259.2 R F2(location)2.912 E F0(and)2.913 E F2 -.37 (ro)2.913 G(ute).37 E F0 .413(to the service.)2.913 F .413 (The speci\214cs of these three mappings are likely to)5.413 F .973 (vary between network architectures.)72 271.2 R .972 (For instance, it is desirable for a network to not require hosts to be)5.973 F .578 (named in such a way that their physical location is known by the client host.) 72 283.2 R .579(Instead, underlying services)5.579 F .166(in the network may d\ iscover the actual location of the host at the time a client host wishes to co\ mmunicate.)72 295.2 R 1.452(This ability to have hosts named in a location ind\ ependent manner may induce overhead in connection)72 307.2 R 1.03(establishmen\ t, as a discovery process must take place, but allows a host to be physically \ mobile without)72 319.2 R (requiring it to notify its clientele of its current location.)72 331.2 Q .128 (Standard routines are provided for: mapping host names to network addresses, \ network names to net-)97 346.8 R .083(work numbers, protocol names to protocol\ numbers, and service names to port numbers and the appropriate)72 358.8 R .134 (protocol to use in communicating with the server process.)72 370.8 R .134 (The \214le <)5.134 F F2(netdb.h)A F0 2.635(>m)C .135 (ust be included when using)394.58 370.8 R(any of these routines.)72 382.8 Q/F3 10/Times-Bold@0 SF 2.5(3.1. Host)72 406.8 R(names)2.5 E F0 (An Internet host name to address mapping is represented by the)97 422.4 Q F2 (hostent)2.5 E F0(structure:)2.5 E 18.48(struct hostent)108 440.4 R({)2.5 E 16.29(char *h_name;)151.2 452.4 R(/* of)295.2 452.4 Q(\214cial name of host */) -.18 E 16.29(char **h_aliases;)151.2 464.4 R(/* alias list */)295.2 464.4 Q 22.94(int h_addrtype;)151.2 476.4 R(/* host address type \(e.g., AF_INET\) */) 295.2 476.4 Q 22.94(int h_length;)151.2 488.4 R(/* length of address */)295.2 488.4 Q 16.29(char **h_addr_list;)151.2 500.4 R (/* list of addresses, null terminated */)295.2 500.4 Q(};)108 512.4 Q 11.26 (#de\214ne h_addr)108 536.4 R 48.84(h_addr_list[0] /*)8.23 F (\214rst address, network byte order */)2.5 E .547(The routine)72 554.4 R F2 (gethostbyname)3.047 E F0 .547 (\(3N\) takes an Internet host name and returns a)B F2(hostent)3.046 E F0 .546 (structure, while the rou-)3.046 F(tine)72 566.4 Q F2(gethostbyaddr)2.5 E F0 (\(3N\) maps Internet host addresses into a)A F2(hostent)2.5 E F0(structure.) 2.5 E 1.373(The of)97 582 R 1.374(\214cial name of the host and its public ali\ ases are returned by these routines, along with the)-.18 F .171(address type \ \(family\) and a null terminated list of variable length address.)72 594 R .17 (This list of addresses is required)5.17 F .469(because it is possible for a h\ ost to have many addresses, all having the same name.)72 606 R(The)5.469 E F2 (h_addr)2.969 E F0(de\214nition)2.969 E .639 (is provided for backward compatibility)72 618 R 3.139(,a)-.65 G .639 (nd is de\214ned to be the \214rst address in the list of addresses in the) 240.075 618 R F2(hostent)72 630 Q F0(structure.)2.5 E .624 (The database for these calls is provided either by the \214le)97 645.6 R F2 (/etc/hosts)3.125 E F0(\()3.125 E F2(hosts)A F0 .625 (\(5\)\), or by use of a name-)1.666 F(server)72 657.6 Q(,)-.4 E F2(named)2.733 E F0 2.733(\(8\). Because)1.666 F .233(of the dif)2.733 F .232 (ferences in these databases and their access protocols, the information)-.18 F 2.408(returned may dif)72 669.6 R(fer)-.18 E 7.408(.W)-.55 G 2.409 (hen using the host table version of)173.184 669.6 R F2(gethostbyname)4.909 E F0 4.909(,o)C 2.409(nly one address will be)402.154 669.6 R .195 (returned, but all listed aliases will be included.)72 681.6 R .194 (The nameserver version may return alternate addresses, but)5.195 F (will not provide any aliases other than one given as ar)72 693.6 Q(gument.) -.18 E .155(Unlike Internet names, NS names are always mapped into host addres\ ses by the use of a standard NS)97 709.2 R F2 2.139(Clearinghouse service)72 721.2 R F0 4.639(,ad)C 2.138(istributed name and authentication server)185.067 721.2 R 7.138(.T)-.55 G 2.138(he algorithms for mapping NS)373.788 721.2 R EP %%Page: 12 12 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-12 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E .156(names to addresses via a Clearinghouse are rather complic\ ated, and the routines are not part of the standard)72 84 R 3.139 (libraries. The)72 96 R(user)3.139 E .638(-contributed Courier \(Xerox remote \ procedure call protocol\) compiler contains routines)-.2 F .067(to accomplish \ this mapping; see the documentation and examples provided therein for more inf\ ormation.)72 108 R(It)5.068 E .473(is expected that almost all software that h\ as to communicate using NS will need to use the facilities of the)72 120 R (Courier compiler)72 132 Q(.)-.55 E (An NS host address is represented by the following:)97 147.6 Q (union ns_host {)108 165.6 Q 20.29(u_char c_host[6];)133 177.6 R 17.5 (u_short s_host[3];)133 189.6 R(};)108 201.6 Q(union ns_net {)108 225.6 Q 20.29 (u_char c_net[4];)133 237.6 R 17.5(u_short s_net[2];)133 249.6 R(};)108 261.6 Q (struct ns_addr {)108 285.6 Q(union ns_net)133 297.6 Q(x_net;)208 297.6 Q (union ns_host)133 309.6 Q(x_host;)208 309.6 Q 17.5(u_short x_port;)133 321.6 R (};)108 333.6 Q(The following code fragment inserts a known NS address into a) 72 351.6 Q/F1 10/Times-Italic@0 SF(ns_addr)2.5 E F0(:)A EP %%Page: 13 13 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-13) -.35 F(#include )108 84 Q(#include )108 96 Q (#include )108 108 Q(...)110.5 120 Q(u_long netnum;)108 132 Q (struct sockaddr_ns dst;)108 144 Q(...)110.5 156 Q (bzero\(\(char *\)&dst, sizeof\(dst\)\);)108 168 Q(/*)108 192 Q 2.5(*T)110.5 204 S(here is no convenient way to assign a long)124.11 204 Q 2.5(*i)110.5 216 S(nteger to a `)120.78 216 Q(`union ns_net')-.74 E 2.5('a)-.74 G 2.5(tp)235.66 216 S(resent; in)245.94 216 Q 2.5(*t)110.5 228 S (he future, something will hopefully be provided,)120.78 228 Q 2.5(*b)110.5 240 S(ut this is the portable way to go for now)123 240 Q(.)-.65 E 2.5(*T)110.5 252 S(he network number below is the one for the NS net)124.11 252 Q 2.5(*t)110.5 264 S(hat the desired host \(gyre\) is on.)120.78 264 Q(*/)110.5 276 Q (netnum = htonl\(2266\);)108 288 Q(dst.sns_addr)108 300 Q (.x_net = *\(union ns_net *\) &netnum;)-.55 E(dst.sns_family = AF_NS;)108 312 Q (/*)108 336 Q 2.5(*h)110.5 348 S (ost 2.7.1.0.2a.18 == "gyre:Computer Science:UofMaryland")123 348 Q(*/)110.5 360 Q(dst.sns_addr)108 372 Q(.x_host.c_host[0] = 0x02;)-.55 E(dst.sns_addr)108 384 Q(.x_host.c_host[1] = 0x07;)-.55 E(dst.sns_addr)108 396 Q (.x_host.c_host[2] = 0x01;)-.55 E(dst.sns_addr)108 408 Q (.x_host.c_host[3] = 0x00;)-.55 E(dst.sns_addr)108 420 Q (.x_host.c_host[4] = 0x2a;)-.55 E(dst.sns_addr)108 432 Q (.x_host.c_host[5] = 0x18;)-.55 E(dst.sns_addr)108 444 Q (.x_port = htons\(75\);)-.55 E/F1 10/Times-Bold@0 SF 2.5(3.2. Network)72 474 R (names)2.5 E F0 .509(As for host names, routines for mapping network names to \ numbers, and back, are provided.)97 489.6 R(These)5.509 E(routines return a)72 501.6 Q/F2 10/Times-Italic@0 SF(netent)2.5 E F0(structure:)2.5 E(/*)108 519.6 Q 2.5(*A)110.5 531.6 S(ssumption here is that a network number)125.22 531.6 Q 2.5 <2a8c>110.5 543.6 S(ts in 32 bits -- probably a poor one.)123.56 543.6 Q(*/) 110.5 555.6 Q 11.28(struct netent)108 567.6 R({)2.5 E 34.29(char *n_name;)144 579.6 R(/* of)288 579.6 Q(\214cial name of net */)-.18 E 34.29 (char **n_aliases;)144 591.6 R(/* alias list */)288 591.6 Q 40.94 (int n_addrtype;)144 603.6 R(/* net address type */)288 603.6 Q 40.94 (int n_net;)144 615.6 R(/* network number)288 615.6 Q 2.5(,h)-.4 G (ost byte order */)373.7 615.6 Q(};)108 627.6 Q 1.211(The routines)72 645.6 R F2(getnetbyname)3.711 E F0(\(3N\),)A F2(getnetbynumber)3.711 E F0 1.211 (\(3N\), and)B F2(getnetent)3.711 E F0 1.211 (\(3N\) are the network counterparts to)B(the host routines described above.)72 657.6 Q(The routines extract their information from)5 E F2(/etc/networks)2.5 E F0(.)A .523(NS network numbers are determined either by asking your local Xero\ x Network Administrator \(and)97 673.2 R .407(hardcoding the information into \ your code\), or by querying the Clearinghouse for addresses.)72 685.2 R .406 (The internet-)5.406 F .702(work router is the only process that needs to mani\ pulate network numbers on a regular basis; if a process)72 697.2 R .433(wishes\ to communicate with a machine, it should ask the Clearinghouse for that machi\ ne')72 709.2 R 2.933(sa)-.55 G .433(ddress \(which)447.747 709.2 R (will include the net number\).)72 721.2 Q EP %%Page: 14 14 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-14 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 10/Times-Bold@0 SF 2.5(3.3. Pr)72 84 R(otocol names)-.18 E F0 1.066(For protocols, which are de\214ned in)97 99.6 R/F2 10/Times-Italic@0 SF(/etc/pr)3.566 E(otocols)-.37 E F0 3.566(,t)C(he)309.35 99.6 Q F2(pr)3.566 E (otoent)-.37 E F0 1.066(structure de\214nes the protocol-name)3.566 F (mapping used with the routines)72 111.6 Q F2(getpr)2.5 E(otobyname)-.37 E F0 (\(3N\),)A F2(getpr)2.5 E(otobynumber)-.37 E F0(\(3N\), and)A F2(getpr)2.5 E (otoent)-.37 E F0(\(3N\):)A 11.28(struct protoent)108 129.6 R({)2.5 E 34.29 (char *p_name;)144 141.6 R(/* of)288 141.6 Q(\214cial protocol name */)-.18 E 34.29(char **p_aliases;)144 153.6 R(/* alias list */)288 153.6 Q 40.94 (int p_proto;)144 165.6 R(/* protocol number */)288 165.6 Q(};)108 177.6 Q 1.144(In the NS domain, protocols are indicated by the "client type" \214eld o\ f a IDP header)97 199.2 R 6.144(.N)-.55 G 3.644(op)462.026 199.2 S(rotocol) 475.67 199.2 Q(database exists; see section 5 for more information.)72 211.2 Q F1 2.5(3.4. Service)72 235.2 R(names)2.5 E F0 .061 (Information regarding services is a bit more complicated.)97 250.8 R 2.561(As) 5.061 G .062(ervice is expected to reside at a speci\214c)345.559 250.8 R -.74 (``)72 262.8 S(port').74 E 2.773('a)-.74 G .272 (nd employ a particular communication protocol.)107.163 262.8 R .272 (This view is consistent with the Internet domain,)5.272 F .793 (but inconsistent with other network architectures.)72 274.8 R(Further)5.793 E 3.293(,as)-.4 G .794(ervice may reside on multiple ports.)325.964 274.8 R .794 (If this)5.794 F .6(occurs, the higher level library routines will have to be \ bypassed or extended.)72 286.8 R .6(Services available are con-)5.6 F (tained in the \214le)72 298.8 Q F2(/etc/services)2.5 E F0 5(.A)C (service mapping is described by the)205.87 298.8 Q F2(servent)2.5 E F0 (structure,)2.5 E 11.28(struct servent)108 316.8 R({)2.5 E 34.29(char *s_name;) 144 328.8 R(/* of)288 328.8 Q(\214cial service name */)-.18 E 34.29 (char **s_aliases;)144 340.8 R(/* alias list */)288 340.8 Q 40.94(int s_port;) 144 352.8 R(/* port number)288 352.8 Q 2.5(,n)-.4 G(etwork byte order */)357.04 352.8 Q 34.29(char *s_proto;)144 364.8 R(/* protocol to use */)288 364.8 Q(};) 108 376.8 Q 1.222(The routine)72 394.8 R F2(getservbyname)3.722 E F0 1.222 (\(3N\) maps service names to a servent structure by specifying a service name) B(and, optionally)72 406.8 Q 2.5(,aq)-.65 G(ualifying protocol.)148.29 406.8 Q (Thus the call)5 E(sp = getservbyname\("telnet", \(char *\) 0\);)108 424.8 Q(r\ eturns the service speci\214cation for a telnet server using any protocol, whi\ le the call)72 442.8 Q(sp = getservbyname\("telnet", "tcp"\);)108 460.8 Q 1.114 (returns only that telnet server which uses the TCP protocol.)72 478.8 R 1.114 (The routines)6.114 F F2(getservbyport)3.614 E F0 1.114(\(3N\) and)B F2(getser) 3.614 E(-)-.2 E(vent)72 490.8 Q F0 1.454(\(3N\) are also provided.)B(The)6.454 E F2(getservbyport)3.954 E F0 1.454 (routine has an interface similar to that provided by)3.954 F F2(get-)3.954 E (servbyname)72 502.8 Q F0 2.5(;a)C 2.5(no)129.48 502.8 S (ptional protocol name may be speci\214ed to qualify lookups.)141.98 502.8 Q .091(In the NS domain, services are handled by a central dispatcher provided a\ s part of the Courier remote)97 518.4 R 1.214(procedure call facilities.)72 530.4 R 1.214(Again, the reader is referred to the Courier compiler documentat\ ion and to the)6.214 F(Xerox standard* for further details.)72 542.4 Q F1 2.5 (3.5. Miscellaneous)72 566.4 R F0 -.4(Wi)97 582 S .078(th the support routines\ described above, an Internet application program should rarely have to deal).4 F .025(directly with addresses.)72 594 R .025(This allows services to be devel\ oped as much as possible in a network independent)5.025 F 2.834(fashion. It)72 606 R .334(is clear)2.834 F 2.834(,h)-.4 G(owever)157.086 606 Q 2.834(,t)-.4 G .334(hat pur)194.23 606 R .333(ging all network dependencies is very dif)-.18 F 2.833(\214cult. So)-.18 F .333(long as the user is)2.833 F .942(required to su\ pply network addresses when naming services and sockets there will always some\ network)72 618 R .755(dependency in a program.)72 630 R .755 (For example, the normal code included in client programs, such as the remote) 5.755 F .049(login program, is of the form shown in Figure 1.)72 642 R .049 (\(This example will be considered in more detail in section)5.049 F(4.\))72 654 Q .231(If we wanted to make the remote login program independent of the In\ ternet protocols and addressing)97 669.6 R .663(scheme we would be forced to a\ dd a layer of routines which masked the network dependent aspects from)72 681.6 R 1.412(the mainstream login code.)72 693.6 R 1.412 (For the current facilities available in the system this does not appear to be) 6.412 F(worthwhile.)72 705.6 Q .32 LW 144 713.6 72 713.6 DL/F3 8/Times-Roman@0 SF(*)72 725.4 Q/F4 8/Times-Italic@0 SF(Courier: The Remote Pr)2 E(ocedur)-.296 E 2(eC)-.296 G(all Pr)188.504 725.4 Q(otocol)-.296 E F3 2(,X)C(SIS 0381)236.432 725.4 Q(12.)-.296 E EP %%Page: 15 15 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-15) -.35 F 1.281(Aside from the address-related data base routines, there are seve\ ral other routines available in the)97 84 R 1.711 (run-time library which are of interest to users.)72 96 R 1.71 (These are intended mostly to simplify manipulation of)6.711 F 1.03 (names and addresses.)72 108 R -.7(Ta)6.03 G 1.031(ble 1 summarizes the routin\ es for manipulating variable length byte strings and).7 F (handling byte swapping of network addresses and values.)72 120 Q(box;)152.18 144 Q 2.5(l|l)152.18 156 S 2.5(l|l)152.18 168 S(.)164.74 168 Q 5.83 (Call Synopsis)152.18 180 R(_)152.18 192 Q(bcmp\(s1, s2, n\))152.18 204 Q (compare byte-strings; 0 if same, not 0 otherwise)227.18 204 Q (bcopy\(s1, s2, n\))152.18 216 Q(copy n bytes from s1 to s2)227.18 216 Q (bzero\(base, n\))152.18 228 Q(zero-\214ll n bytes starting at base)227.18 228 Q 8.06(htonl\(val\) convert)152.18 240 R (32-bit quantity from host to network byte order)2.5 E 6.95 (htons\(val\) convert)152.18 252 R (16-bit quantity from host to network byte order)2.5 E 8.06 (ntohl\(val\) convert)152.18 264 R (32-bit quantity from network to host byte order)2.5 E 6.95 (ntohs\(val\) convert)152.18 276 R (16-bit quantity from network to host byte order)2.5 E -.7(Ta)229.05 300 S (ble 1.).7 E 2.5(Cr)5 G(un-time routines.)278.62 300 Q .625(The byte swapping \ routines are provided because the operating system expects addresses to be sup\ -)97 315.6 R .78(plied in network order)72 327.6 R 5.78(.O)-.55 G 3.28(ns) 178.44 327.6 S .781(ome architectures, such as the V)190.61 327.6 R .781 (AX, host byte ordering is dif)-1.29 F .781(ferent than net-)-.18 F .637 (work byte ordering.)72 339.6 R(Consequently)5.636 E 3.136(,p)-.65 G .636 (rograms are sometimes required to byte swap quantities.)223.046 339.6 R .636 (The library)5.636 F .407(routines which return network addresses provide them\ in network order so that they may simply be copied)72 351.6 R .302 (into the structures provided to the system.)72 363.6 R .302 (This implies users should encounter the byte swapping problem)5.302 F .404 (only when)72 375.6 R/F1 10/Times-Italic@0 SF(interpr)2.904 E(eting)-.37 E F0 .404(network addresses.)2.904 F .404 (For example, if an Internet port is to be printed out the follow-)5.404 F (ing code would be required:)72 387.6 Q (printf\("port number %d\\n", ntohs\(sp->s_port\)\);)108 405.6 Q (On machines where unneeded these routines are de\214ned as null macros.)72 423.6 Q EP %%Page: 16 16 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-16 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(#include )108 84 Q(#include )108 96 Q(#include )108 108 Q(#include )108 120 Q (#include )108 132 Q(...)110.5 144 Q(main\(ar)108 156 Q(gc, ar)-.18 E (gv\))-.18 E(int ar)144 168 Q(gc;)-.18 E(char *ar)144 180 Q(gv[];)-.18 E({)108 192 Q(struct sockaddr_in server;)144 204 Q(struct servent *sp;)144 216 Q (struct hostent *hp;)144 228 Q(int s;)144 240 Q(...)144 252 Q (sp = getservbyname\("login", "tcp"\);)144 264 Q(if \(sp == NULL\) {)144 276 Q (fprintf\(stderr)180 288 Q 2.5(,")-.4 G (rlogin: tcp/login: unknown service\\n"\);)240.33 288 Q(exit\(1\);)180 300 Q(}) 144 312 Q(hp = gethostbyname\(ar)144 324 Q(gv[1]\);)-.18 E(if \(hp == NULL\) {) 144 336 Q(fprintf\(stderr)180 348 Q 2.5(,")-.4 G (rlogin: %s: unknown host\\n", ar)240.33 348 Q(gv[1]\);)-.18 E(exit\(2\);)180 360 Q(})144 372 Q(bzero\(\(char *\)&server)144 384 Q 2.5(,s)-.4 G (izeof \(server\)\);)241.61 384 Q(bcopy\(hp->h_addr)144 396 Q 2.5(,\()-.4 G (char *\)&server)226.44 396 Q(.sin_addr)-.55 E 2.5(,h)-.4 G(p->h_length\);) 332.68 396 Q(server)144 408 Q(.sin_family = hp->h_addrtype;)-.55 E(server)144 420 Q(.sin_port = sp->s_port;)-.55 E 2.5(s=s)144 432 S(ocket\(AF_INET)162.42 432 Q 2.5(,S)-.74 G(OCK_STREAM, 0\);)237.78 432 Q(if \(s < 0\) {)144 444 Q (perror\("rlogin: socket"\);)180 456 Q(exit\(3\);)180 468 Q(})144 480 Q(...)144 492 Q(/* Connect does the bind\(\) for us */)144 504 Q (if \(connect\(s, \(char *\)&server)144 528 Q 2.5(,s)-.4 G (izeof \(server\)\) < 0\) {)271.33 528 Q(perror\("rlogin: connect"\);)180 540 Q (exit\(5\);)180 552 Q(})144 564 Q(...)144 576 Q(})108 588 Q(Figure 1.)216.06 606 Q(Remote login client code.)5 E EP %%Page: 17 17 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-17) -.35 F/F1 12/Times-Bold@0 SF(4. CLIENT/SER)209.034 84 Q(VER MODEL)-.42 E F0 2.634(The most commonly used paradigm in constructing distributed applications\ is the client/server)97 123.6 R 2.73(model. In)72 135.6 R .231 (this scheme client applications request services from a server process.)2.73 F .231(This implies an asymme-)5.231 F .407(try in establishing communication be\ tween the client and server which has been examined in section 2.)72 147.6 R (In)5.406 E .733(this section we will look more closely at the interactions be\ tween client and server)72 159.6 R 3.234(,a)-.4 G .734(nd consider some of) 420.978 159.6 R(the problems in developing client and server applications.)72 171.6 Q .758(The client and server require a well known set of conventions bef\ ore service may be rendered \(and)97 187.2 R 2.67(accepted\). This)72 199.2 R .171(set of conventions comprises a protocol which must be implemented at both\ ends of a con-)2.67 F 2.711(nection. Depending)72 211.2 R .211 (on the situation, the protocol may be symmetric or asymmetric.)2.711 F .21 (In a symmetric proto-)5.21 F .022 (col, either side may play the master or slave roles.)72 223.2 R .022 (In an asymmetric protocol, one side is immutably recog-)5.022 F .072 (nized as the master)72 235.2 R 2.572(,w)-.4 G .072 (ith the other as the slave.)160.478 235.2 R .071 (An example of a symmetric protocol is the TELNET proto-)5.072 F .313 (col used in the Internet for remote terminal emulation.)72 247.2 R .314 (An example of an asymmetric protocol is the Inter)5.314 F(-)-.2 E .448 (net \214le transfer protocol, FTP)72 259.2 R 5.448(.N)-1.11 G 2.948(om)206.45 259.2 S .447 (atter whether the speci\214c protocol used in obtaining a service is sym-) 222.178 259.2 R 1.115 (metric or asymmetric, when accessing a service there is a `)72 271.2 R 1.116 (`client process')-.74 F 3.616('a)-.74 G 1.116(nd a `)390.756 271.2 R 1.116 (`server process')-.74 F 3.616('. W)-.74 F(e)-.8 E(will \214rst consider the p\ roperties of server processes, then client processes.)72 283.2 Q 3.482(As)97 298.8 S .981 (erver process normally listens at a well known address for service requests.) 111.592 298.8 R .981(That is, the server)5.981 F .106 (process remains dormant until a connection is requested by a client')72 310.8 R 2.606(sc)-.55 G .107(onnection to the server)355.066 310.8 R 1.207 -.55('s a) .37 H 2.607(ddress. At).55 F .092(such a time the server process `)72 322.8 R .092(`wakes up')-.74 F 2.592('a)-.74 G .092 (nd services the client, performing whatever appropriate actions)251.196 322.8 R(the client requests of it.)72 334.8 Q .479(Alternative schemes which use a s\ ervice server may be used to eliminate a \215ock of server processes)97 350.4 R 2.042(clogging the system while remaining dormant most of the time.)72 362.4 R 2.042(For Internet servers in 4.3BSD, this)7.042 F .496 (scheme has been implemented via)72 374.4 R/F2 10/Times-Italic@0 SF(inetd)2.996 E F0 2.996(,t)C .496(he so called `)241.896 374.4 R .496(`internet super)-.74 F (-server)-.2 E(.')-.55 E(')-.74 E F2(Inetd)5.496 E F0 .497 (listens at a variety of)2.996 F .776 (ports, determined at start-up by reading a con\214guration \214le.)72 386.4 R .775(When a connection is requested to a port on)5.775 F(which)72 398.4 Q F2 (inetd)4.367 E F0 1.867(is listening,)4.367 F F2(inetd)4.367 E F0 1.867 (executes the appropriate server program to handle the client.)4.367 F -.4(Wi) 6.868 G 1.868(th this).4 F .189 (method, clients are unaware that an intermediary such as)72 410.4 R F2(inetd) 2.688 E F0 .188(has played any part in the connection.)2.688 F F2(Inetd)5.188 E F0(will be described in more detail in section 5.)72 422.4 Q 3.342(As)97 438 S .842(imilar alternative scheme is used by most Xerox services.)111.452 438 R .842(In general, the Courier dispatch pro-)5.842 F .862(cess \(if used\) accep\ ts connections from processes requesting services of some sort or another)72 450 R 5.862(.T)-.55 G .862(he client)468.978 450 R 1.47 (processes request a particular triple.) 352.985 462 R 1.471(If the dis-)6.471 F .205(patcher knows of such a program, \ it is started to handle the request; if not, an error is reported to the clien\ t.)72 474 R 1.067(In this way)72 486 R 3.567(,o)-.65 G 1.067 (nly one port is required to service a lar)128.991 486 R 1.067 (ge variety of dif)-.18 F 1.068(ferent requests.)-.18 F 1.068 (Again, the Courier)6.068 F .933(facilities are not available without the use \ and installation of the Courier compiler)72 498 R 5.933(.T)-.55 G .933 (he information pre-)424.374 498 R(sented in this section applies only to NS c\ lients and services that do not use Courier)72 510 Q(.)-.55 E/F3 10 /Times-Bold@0 SF 2.5(4.1. Servers)72 534 R F0 .361(In 4.3BSD most servers are \ accessed at well known Internet addresses or UNIX domain names.)97 549.6 R(For) 5.362 E(example, the remote login server)72 561.6 Q 1.1 -.55('s m).37 H (ain loop is of the form shown in Figure 2.).55 E (The \214rst step taken by the server is look up its service de\214nition:)97 577.2 Q(sp = getservbyname\("login", "tcp"\);)122 601.2 Q(if \(sp == NULL\) {) 122 613.2 Q(fprintf\(stderr)158 625.2 Q 2.5(,")-.4 G (rlogind: tcp/login: unknown service\\n"\);)218.33 625.2 Q(exit\(1\);)158 637.2 Q(})122 649.2 Q .16(The result of the)72 673.2 R F2(getservbyname)2.66 E F0 .159(call is used in later portions of the code to de\214ne the Internet port \ at which)2.66 F(it listens for service requests \(indicated by a connection\).) 72 685.2 Q EP %%Page: 18 18 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-18 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(main\(ar)108 126 Q(gc, ar)-.18 E(gv\))-.18 E(int ar)133 138 Q (gc;)-.18 E(char *ar)133 150 Q(gv[];)-.18 E({)108 162 Q(int f;)133 174 Q (struct sockaddr_in from;)133 186 Q(struct servent *sp;)133 198 Q (sp = getservbyname\("login", "tcp"\);)133 222 Q(if \(sp == NULL\) {)133 234 Q (fprintf\(stderr)158 246 Q 2.5(,")-.4 G (rlogind: tcp/login: unknown service\\n"\);)218.33 246 Q(exit\(1\);)158 258 Q (})133 270 Q(...)133 282 Q(#ifndef DEBUG)108 294 Q (/* Disassociate server from controlling terminal */)133 306 Q(...)133 318 Q (#endif)108 330 Q(sin.sin_port = sp->s_port;)133 354 Q (/* Restricted port -- see section 5 */)258 354 Q(...)133 366 Q 2.5(f=s)133 378 S(ocket\(AF_INET)150.86 378 Q 2.5(,S)-.74 G(OCK_STREAM, 0\);)226.22 378 Q(...) 133 390 Q(if \(bind\(f, \(struct sockaddr *\) &sin, sizeof \(sin\)\) < 0\) {) 133 402 Q(...)158 414 Q(})133 426 Q(...)133 438 Q(listen\(f, 5\);)133 450 Q (for \(;;\) {)133 462 Q(int g, len = sizeof \(from\);)158 474 Q 2.5(g=a)158 498 S(ccept\(f, \(struct sockaddr *\) &from, &len\);)178.08 498 Q(if \(g < 0\) {) 158 510 Q(if \(errno != EINTR\))183 522 Q (syslog\(LOG_ERR, "rlogind: accept: %m"\);)208 534 Q(continue;)183 546 Q(})158 558 Q(if \(fork\(\) == 0\) {)158 570 Q(close\(f\);)183 582 Q(doit\(g, &from\);) 183 594 Q(})158 606 Q(close\(g\);)158 618 Q(})133 630 Q(})108 642 Q(Figure 2.) 225.92 660 Q(Remote login server)5 E(.)-.55 E EP %%Page: 19 19 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-19) -.35 F(Step two is to disassociate the server from the controlling terminal of\ its invoker:)97 84 Q(for \(i = 0; i < 3; ++i\))133 102 Q(close\(i\);)158 114 Q (open\("/", O_RDONL)133 138 Q(Y\);)-1 E(dup2\(0, 1\);)133 150 Q(dup2\(0, 2\);) 133 162 Q 2.5(i=o)133 186 S(pen\("/dev/tty", O_RDWR\);)151.42 186 Q (if \(i >= 0\) {)133 198 Q(ioctl\(i, TIOCNOTTY)158 210 Q 2.5(,0)-1.29 G(\);) 252.81 210 Q(close\(i\);)158 222 Q(})133 234 Q .327(This step is important as \ the server will likely not want to receive signals delivered to the process gr\ oup of)72 252 R 1.058(the controlling terminal.)72 264 R 1.058(Note, however) 6.058 F 3.558(,t)-.4 G 1.057 (hat once a server has disassociated itself it can no longer send)245.48 264 R (reports of errors to a terminal, and must log errors via)72 276 Q/F1 10 /Times-Italic@0 SF(syslog)2.5 E F0(.)A .251(Once a server has established a pr\ istine environment, it creates a socket and begins accepting service)97 291.6 R 3.232(requests. The)72 303.6 R F1(bind)3.232 E F0 .732 (call is required to insure the server listens at its expected location.)3.232 F .731(It should be noted)5.731 F .55 (that the remote login server listens at a restricted port number)72 315.6 R 3.051(,a)-.4 G .551(nd must therefore be run with a user)333.162 315.6 R .551 (-id of)-.2 F 2.5(root. This)72 327.6 R(concept of a `)2.5 E (`restricted port number)-.74 E 1.48 -.74('' i).37 H 2.5(s4).74 G (BSD speci\214c, and is covered in section 5.)285.01 327.6 Q (The main body of the loop is fairly simple:)97 343.2 Q(for \(;;\) {)108 361.2 Q(int g, len = sizeof \(from\);)144 373.2 Q 2.5(g=a)144 397.2 S (ccept\(f, \(struct sockaddr *\)&from, &len\);)164.08 397.2 Q(if \(g < 0\) {) 144 409.2 Q(if \(errno != EINTR\))180 421.2 Q (syslog\(LOG_ERR, "rlogind: accept: %m"\);)216 433.2 Q(continue;)180 445.2 Q(}) 144 457.2 Q(if \(fork\(\) == 0\) {)144 469.2 Q(/* Child */)4.83 E(close\(f\);) 180 481.2 Q(doit\(g, &from\);)180 493.2 Q(})144 505.2 Q 34.51(close\(g\); /*) 144 517.2 R(Parent */)2.5 E(})108 529.2 Q(An)72 547.2 Q F1(accept)2.917 E F0 .417(call blocks the server until a client requests service.)2.917 F .417 (This call could return a failure status if the)5.417 F .316(call is interrupt\ ed by a signal such as SIGCHLD \(to be discussed in section 5\).)72 559.2 R .317(Therefore, the return value)5.316 F(from)72 571.2 Q F1(accept)3.212 E F0 .712(is checked to insure a connection has actually been established, and an e\ rror report is logged)3.212 F(via)72 583.2 Q F1(syslog)2.5 E F0 (if an error has occurred.)2.5 E -.4(Wi)97 598.8 S .955(th a connection in han\ d, the server then forks a child process and invokes the main body of the).4 F .305(remote login protocol processing.)72 610.8 R .305 (Note how the socket used by the parent for queuing connection requests)5.305 F .311(is closed in the child, while the socket created as a result of the)72 622.8 R F1(accept)2.811 E F0 .311(is closed in the parent.)2.811 F .311 (The address)5.311 F(of the client is also handed the)72 634.8 Q F1(doit)2.5 E F0(routine because it requires it in authenticating clients.)2.5 E/F2 10 /Times-Bold@0 SF 2.5(4.2. Clients)72 658.8 R F0 .502 (The client side of the remote login service was shown earlier in Figure 1.)97 674.4 R .502(One can see the separate,)5.502 F .939 (asymmetric roles of the client and server clearly in the code.)72 686.4 R .939 (The server is a passive entity)5.939 F 3.439(,l)-.65 G .939(istening for) 457.231 686.4 R (client connections, while the client process is an active entity)72 698.4 Q 2.5(,i)-.65 G(nitiating a connection when invoked.)323.54 698.4 Q .177(Let us \ consider more closely the steps taken by the client remote login process.)97 714 R .176(As in the server pro-)5.176 F(cess, the \214rst step is to locate t\ he service de\214nition for a remote login:)72 726 Q EP %%Page: 20 20 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-20 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(sp = getservbyname\("login", "tcp"\);)108 84 Q (if \(sp == NULL\) {)108 96 Q(fprintf\(stderr)133 108 Q 2.5(,")-.4 G (rlogin: tcp/login: unknown service\\n"\);)193.33 108 Q(exit\(1\);)133 120 Q(}) 108 132 Q(Next the destination host is looked up with a)72 150 Q/F1 10 /Times-Italic@0 SF(gethostbyname)2.5 E F0(call:)2.5 E(hp = gethostbyname\(ar) 108 168 Q(gv[1]\);)-.18 E(if \(hp == NULL\) {)108 180 Q(fprintf\(stderr)133 192 Q 2.5(,")-.4 G(rlogin: %s: unknown host\\n", ar)193.33 192 Q(gv[1]\);)-.18 E (exit\(2\);)133 204 Q(})108 216 Q -.4(Wi)72 234 S .769(th this accomplished, a\ ll that is required is to establish a connection to the server at the requeste\ d host).4 F .11(and start up the remote login protocol.)72 246 R .11 (The address buf)5.11 F .11 (fer is cleared, then \214lled in with the Internet address)-.18 F(of the fore\ ign host and the port number at which the login process resides on the foreign\ host:)72 258 Q(bzero\(\(char *\)&server)108 276 Q 2.5(,s)-.4 G (izeof \(server\)\);)205.61 276 Q(bcopy\(hp->h_addr)108 288 Q 2.5(,\()-.4 G (char *\) &server)190.44 288 Q(.sin_addr)-.55 E 2.5(,h)-.4 G(p->h_length\);) 299.18 288 Q(server)108 300 Q(.sin_family = hp->h_addrtype;)-.55 E(server)108 312 Q(.sin_port = sp->s_port;)-.55 E 2.768(As)72 330 S .269 (ocket is created, and a connection initiated.)85.878 330 R .269(Note that) 5.269 F F1(connect)2.769 E F0 .269(implicitly performs a)2.769 F F1(bind)2.769 E F0 .269(call, since)2.769 F F1(s)2.769 E F0(is)2.769 E(unbound.)72 342 Q 2.5 (s=s)108 360 S(ocket\(hp->h_addrtype, SOCK_STREAM, 0\);)126.42 360 Q (if \(s < 0\) {)108 372 Q(perror\("rlogin: socket"\);)133 384 Q(exit\(3\);)133 396 Q(})108 408 Q(...)110.5 420 Q (if \(connect\(s, \(struct sockaddr *\) &server)108 432 Q 2.5(,s)-.4 G (izeof \(server\)\) < 0\) {)281.44 432 Q(perror\("rlogin: connect"\);)133 444 Q (exit\(4\);)133 456 Q(})108 468 Q (The details of the remote login protocol will not be considered here.)72 486 Q /F2 10/Times-Bold@0 SF 2.5(4.3. Connectionless)72 510 R(servers)2.5 E F0 .252(\ While connection-based services are the norm, some services are based on the u\ se of datagram sock-)97 525.6 R 3.233(ets. One,)72 537.6 R .733(in particular) 3.233 F 3.233(,i)-.4 G 3.233(st)171.182 537.6 S .733(he `)181.085 537.6 R (`rwho')-.74 E 3.233('s)-.74 G .733 (ervice which provides users with status information for hosts con-)233.271 537.6 R .274(nected to a local area network.)72 549.6 R .274 (This service, while predicated on the ability to)5.274 F F1(br)2.774 E (oadcast)-.37 E F0 .274(information to all)2.774 F(hosts connected to a partic\ ular network, is of interest as an example usage of datagram sockets.)72 561.6 Q 2.682(Au)97 577.2 S .182(ser on any machine running the rwho server may \214\ nd out the current status of a machine with the)111.902 577.2 R F1(ruptime)72 589.2 Q F0(\(1\) program.)A(The output generated is illustrated in Figure 3.)5 E .209(Status information for each host is periodically broadcast by rwho serv\ er processes on each machine.)97 604.8 R .009(The same server process also rec\ eives the status information and uses it to update a database.)72 616.8 R .009 (This database)5.009 F .334 (is then interpreted to generate the status information for each host.)72 628.8 R .333(Servers operate autonomously)5.333 F 2.833(,c)-.65 G(oupled)476.78 628.8 Q(only by the local network and its broadcast capabilities.)72 640.8 Q .265 (Note that the use of broadcast for such a task is fairly inef)97 656.4 R .265 (\214cient, as all hosts must process each mes-)-.18 F .394 (sage, whether or not using an rwho server)72 668.4 R 5.394(.U)-.55 G .394 (nless such a service is suf)256.504 668.4 R .394 (\214ciently universal and is frequently)-.18 F (used, the expense of periodic broadcasts outweighs the simplicity)72 680.4 Q (.)-.65 E 1.481(The rwho server)97 696 R 3.981(,i)-.4 G 3.981(nas)174.353 696 S 1.481(impli\214ed form, is pictured in Figure 4.)195.645 696 R 1.482 (There are two separate tasks per)6.481 F(-)-.2 E .298(formed by the server)72 708 R 5.298(.T)-.55 G .298(he \214rst task is to act as a receiver of status i\ nformation broadcast by other hosts on)169.282 708 R .215(the network.)72 720 R .215(This job is carried out in the main loop of the program.)5.215 F .215 (Packets received at the rwho port are)5.215 F EP %%Page: 21 21 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-21) -.35 F 2.5(lrlllll)154.25 96 S(.)189.26 96 Q 5.29(arpa up)154.25 108 R 2.22 (9:45, 5)204.25 108 R(users, load)2.5 E 2.5(1.15, 1.39, 1.31)304.25 108 R 8.62 (cad up)154.25 120 R 11.58(2+12:04, 8)204.25 120 R(users, load)2.5 E 2.5 (4.67, 5.13, 4.59)329.25 120 R -1.93(calder up)154.25 132 R 22.22(10:10, 0) 204.25 132 R(users, load)2.5 E 2.5(0.27, 0.15, 0.14)329.25 132 R 7.5(dali up) 154.25 144 R 11.58(2+06:28, 9)204.25 144 R(users, load)2.5 E 2.5 (1.04, 1.20, 1.65)329.25 144 R -.27(degas up)154.25 156 R 6.58(25+09:48, 0) 204.25 156 R(users, load)2.5 E 2.5(1.49, 1.43, 1.41)329.25 156 R 10.29(ear up) 154.25 168 R 11.58(5+00:05, 0)204.25 168 R(users, load)2.5 E 2.5 (1.51, 1.54, 1.56)329.25 168 R 2.51(ernie down)154.25 180 R(0:24)2.78 E -.27 (esvax down)154.25 192 R(17:04)2.78 E -1.94(ingres down)154.25 204 R(0:26)2.78 E 6.94(kim up)154.25 216 R 11.58(3+09:16, 8)204.25 216 R(users, load)2.5 E 2.5 (2.03, 2.46, 3.1)329.25 216 R(1)-.37 E 17.5(matisse up)154.25 228 R 11.58 (3+06:18, 0)229.25 228 R(users, load)2.5 E 2.5(0.03, 0.03, 0.05)354.25 228 R 21.4(medea up)154.25 240 R 11.58(3+09:39, 2)229.25 240 R(users, load)2.5 E 2.5 (0.35, 0.37, 0.50)354.25 240 R 21.39(merlin down)154.25 252 R(19+15:37)2.78 E 3.61(miro up)154.25 264 R 11.58(1+07:20, 7)204.25 264 R(users, load)2.5 E 2.5 (4.59, 3.28, 2.12)329.25 264 R 22.5(monet up)154.25 276 R 11.58(1+00:43, 2) 229.25 276 R(users, load)2.5 E 2.5(0.22, 0.09, 0.07)354.25 276 R 13.06(oz down) 154.25 288 R(16:09)2.78 E 19.17(statvax up)154.25 300 R 11.58(2+15:57, 3)229.25 300 R(users, load)2.5 E 2.5(1.52, 1.81, 1.86)354.25 300 R 18.62(ucbvax up) 154.25 312 R 2.22(9:34, 2)229.25 312 R(users, load)2.5 E 2.5(6.08, 5.16, 3.28) 329.25 312 R(Figure 3. ruptime output.)237.86 336 Q .773(interrogated to insur\ e they've been sent by another rwho server process, then are time stamped with\ their)72 360 R .111 (arrival time and used to update a \214le indicating the status of the host.)72 372 R .112(When a host has not been heard from)5.112 F .911(for an extended pe\ riod of time, the database interpretation routines assume the host is down and\ indicate)72 384 R .111(such on the status reports.)72 396 R .112(This algorit\ hm is prone to error as a server may be down while a host is actually)5.111 F (up, but serves our current needs.)72 408 Q 1.291(The second task performed by\ the server is to supply information regarding the status of its host.)97 423.6 R .047(This involves periodically acquiring system status information, packagi\ ng it up in a message and broadcast-)72 435.6 R .52 (ing it on the local network for other rwho servers to hear)72 447.6 R 5.52(.T) -.55 G .52(he supply function is triggered by a timer and)317.64 447.6 R .535 (runs of)72 459.6 R 3.035(fas)-.18 G 3.035(ignal. Locating)118.135 459.6 R .535 (the system status information is somewhat involved, but uninteresting.)3.035 F (Decid-)5.535 E (ing where to transmit the resultant packet is somewhat problematical, however) 72 471.6 Q(.)-.55 E .747 (Status information must be broadcast on the local network.)97 487.2 R .746 (For networks which do not support the)5.747 F .302(notion of broadcast anothe\ r scheme must be used to simulate or replace broadcasting.)72 499.2 R .302 (One possibility is to)5.302 F 1.161(enumerate the known neighbors \(based on \ the status messages received from other rwho servers\).)72 511.2 R(This,)6.161 E(unfortunately)72 523.2 Q 2.515(,r)-.65 G .015(equires some bootstrapping inf\ ormation, for a server will have no idea what machines are its)133.575 523.2 R 1.78(neighbors until it receives status messages from them.)72 535.2 R 1.779 (Therefore, if all machines on a net are freshly)6.779 F .129(booted, no machi\ ne will have any known neighbors and thus never receive, or send, any status i\ nformation.)72 547.2 R .637(This is the identical problem faced by the routing\ table management process in propagating routing status)72 559.2 R 2.8 (information. The)72 571.2 R .301(standard solution, unsatisfactory as it may \ be, is to inform one or more servers of known)2.8 F .617 (neighbors and request that they always communicate with these neighbors.)72 583.2 R .617(If each server has at least one)5.617 F .874(neighbor supplied to\ it, status information may then propagate through a neighbor to hosts which a\ re not)72 595.2 R .565(\(possibly\) directly neighbors.)72 607.2 R .565 (If the server is able to support networks which provide a broadcast capabil-) 5.565 F(ity)72 619.2 Q 2.5(,a)-.65 G 2.5(sw)91.35 619.2 S(ell as those which d\ o not, then networks with an arbitrary topology may share status information*.) 104.96 619.2 Q .042(It is important that software operating in a distributed e\ nvironment not have any site-dependent infor)97 634.8 R(-)-.2 E .794 (mation compiled into it.)72 646.8 R .795(This would require a separate copy o\ f the server at each host and make mainte-)5.794 F .929 (nance a severe headache.)72 658.8 R .928(4.3BSD attempts to isolate host-spec\ i\214c information from applications by pro-)5.929 F .666 (viding system calls which return the necessary information*.)72 670.8 R 3.166 (Am)5.666 G .666(echanism exists, in the form of an)343.794 670.8 R/F1 10 /Times-Italic@0 SF(ioctl)3.167 E F0 .684(call, for \214nding the collection of\ networks to which a host is directly connected.)72 682.8 R(Further)5.684 E 3.184(,al)-.4 G .684(ocal network)451.386 682.8 R .32 LW 144 690.8 72 690.8 DL /F2 8/Times-Roman@0 SF 2.327(*O)72 702.6 S .327(ne must, however)84.103 702.6 R 2.327(,b)-.32 G 2.327(ec)149.42 702.6 S .327(oncerned about `)158.851 702.6 R (`loops')-.592 E 2.327('. That)-.592 F .327 (is, if a host is connected to multiple networks, it will)2.327 F (receive status information from itself.)72 712.6 Q (This can lead to an endless, wasteful, exchange of information.)4 E 2(*A)72 724.4 S 2(ne)83.776 724.4 S(xample of such a system call is the)93.328 724.4 Q /F3 8/Times-Italic@0 SF(gethostname)2 E F2(\(2\) call which returns the host')A 2(s`)-.44 G(`of)352.256 724.4 Q(\214cial')-.144 E 2('n)-.592 G(ame.)388.176 724.4 Q EP %%Page: 22 22 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-22 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(main\(\))108 90 Q({)108 102 Q(...)144 114 Q (sp = getservbyname\("who", "udp"\);)144 126 Q (net = getnetbyname\("localnet"\);)144 138 Q (sin.sin_addr = inet_makeaddr\(INADDR_ANY)144 150 Q 2.5(,n)-1.29 G(et\);)340.26 150 Q(sin.sin_port = sp->s_port;)144 162 Q(...)144 174 Q 2.5(s=s)144 186 S (ocket\(AF_INET)162.42 186 Q 2.5(,S)-.74 G(OCK_DGRAM, 0\);)237.78 186 Q(...)144 198 Q(on = 1;)144 210 Q(if \(setsockopt\(s, SOL_SOCKET)144 222 Q 2.5(,S)-.74 G (O_BROADCAST)282.98 222 Q 2.5(,&)-.74 G(on, sizeof\(on\)\) < 0\) {)367.8 222 Q (syslog\(LOG_ERR, "setsockopt SO_BROADCAST)180 234 Q 2.5(:%)-.5 G(m"\);)399.14 234 Q(exit\(1\);)180 246 Q(})144 258 Q (bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)144 270 Q(...)144 282 Q (signal\(SIGALRM, onalrm\);)144 294 Q(onalrm\(\);)144 306 Q(for \(;;\) {)144 318 Q(struct whod wd;)180 330 Q(int cc, whod, len = sizeof \(from\);)180 342 Q (cc = recvfrom\(s, \(char *\)&wd, sizeof \(struct whod\), 0,)180 366 Q (\(struct sockaddr *\)&from, &len\);)190 378 Q(if \(cc <= 0\) {)180 390 Q (if \(cc < 0 && errno != EINTR\))216 402 Q (syslog\(LOG_ERR, "rwhod: recv: %m"\);)252 414 Q(continue;)216 426 Q(})180 438 Q(if \(from.sin_port != sp->s_port\) {)180 450 Q (syslog\(LOG_ERR, "rwhod: %d: bad from port",)216 462 Q (ntohs\(from.sin_port\)\);)252 474 Q(continue;)216 486 Q(})180 498 Q(...)180 510 Q(if \(!verify\(wd.wd_hostname\)\) {)180 522 Q (syslog\(LOG_ERR, "rwhod: malformed host name from %x",)216 534 Q (ntohl\(from.sin_addr)252 546 Q(.s_addr\)\);)-.55 E(continue;)216 558 Q(})180 570 Q(\(void\) sprintf\(path, "%s/whod.%s", R)180 582 Q (WHODIR, wd.wd_hostname\);)-.55 E(whod = open\(path, O_WRONL)180 594 Q 2.5(Y|O) -1 G(_CREA)327.17 594 Q 2.5(T|O)-1.11 G(_TRUNC, 0666\);)378.06 594 Q(...)180 606 Q(\(void\) time\(&wd.wd_recvtime\);)180 618 Q (\(void\) write\(whod, \(char *\)&wd, cc\);)180 630 Q(\(void\) close\(whod\);) 180 642 Q(})144 654 Q(})108 666 Q(Figure 4.)242.73 684 Q(rwho server)5 E(.)-.55 E .266(broadcasting mechanism has been implemented at the socket level.)72 708 R .267(Combining these two features allows a)5.267 F .026(process to broadcast\ on any directly connected local network which supports the notion of broadcas\ ting in a)72 720 R EP %%Page: 23 23 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-23) -.35 F 1.135(site independent manner)72 84 R 6.135(.T)-.55 G 1.135 (his allows 4.3BSD to solve the problem of deciding how to propagate status) 186.225 84 R .354(information in the case of)72 96 R/F1 10/Times-Italic@0 SF (rwho)2.854 E F0 2.854(,o)C 2.854(rm)209.944 96 S .354 (ore generally in broadcasting: Such status information is broadcast to)223.908 96 R .178(connected networks at the socket level, where the connected networks\ have been obtained via the appropri-)72 108 R(ate)72 120 Q F1(ioctl)2.778 E F0 2.777(calls. The)2.777 F .277 (speci\214cs of such broadcastings are complex, however)2.777 F 2.777(,a)-.4 G .277(nd will be covered in section 5.)377.078 120 R EP %%Page: 24 24 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-24 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 12/Times-Bold@0 SF(5. ADV)224.718 84 Q(ANCED T)-1.548 E (OPICS)-.216 E F0 3.16(An)97 123.6 S .66 (umber of facilities have yet to be discussed.)112.38 123.6 R .66 (For most users of the IPC the mechanisms already)5.66 F .366 (described will suf)72 135.6 R .366 (\214ce in constructing distributed applications.)-.18 F(However)5.366 E 2.866 (,o)-.4 G .365(thers will \214nd the need to utilize)372.368 135.6 R (some of the features which we consider in this section.)72 147.6 Q/F2 10 /Times-Bold@0 SF 2.5(5.1. Out)72 171.6 R(of band data)2.5 E F0 .413 (The stream socket abstraction includes the notion of `)97 187.2 R .413 (`out of band')-.74 F 2.913('d)-.74 G 2.913(ata. Out)377.513 187.2 R .414 (of band data is a logi-)2.914 F .109(cally independent transmission channel a\ ssociated with each pair of connected stream sockets.)72 199.2 R .109 (Out of band)5.109 F .146 (data is delivered to the user independently of normal data.)72 211.2 R .146 (The abstraction de\214nes that the out of band data)5.146 F 1.071(facilities \ must support the reliable delivery of at least one out of band message at a ti\ me.)72 223.2 R 1.071(This message)6.071 F .47(may contain at least one byte of\ data, and at least one message may be pending delivery to the user at any)72 235.2 R .055(one time.)72 247.2 R .055(For communications protocols which supp\ ort only in-band signaling \(i.e. the ur)5.055 F .054(gent data is deliv-)-.18 F .449(ered in sequence with the normal data\), the system normally extracts t\ he data from the normal data stream)72 259.2 R .32(and stores it separately)72 271.2 R 5.32(.T)-.65 G .319 (his allows users to choose between receiving the ur)177.61 271.2 R .319 (gent data in order and receiv-)-.18 F 1.78 (ing it out of sequence without having to buf)72 283.2 R 1.781 (fer all the intervening data.)-.18 F 1.781(It is possible to `)6.781 F(`peek') -.74 E 4.281('\()-.74 G(via)491.78 283.2 Q .656 (MSG_PEEK\) at out of band data.)72 295.2 R .655 (If the socket has a process group, a SIGURG signal is generated when)5.656 F .075(the protocol is noti\214ed of its existence.)72 307.2 R 2.575(Ap)5.075 G .076(rocess can set the process group or process id to be informed by)247.59 307.2 R .807(the SIGURG signal via the appropriate)72 319.2 R/F3 10 /Times-Italic@0 SF(fcntl)3.306 E F0 .806(call, as described below for SIGIO.) 3.306 F .806(If multiple sockets may)5.806 F .618 (have out of band data awaiting delivery)72 331.2 R 3.118(,a)-.65 G F3(select)A F0 .618(call for exceptional conditions may be used to determine)3.118 F .085 (those sockets with such data pending.)72 343.2 R .084 (Neither the signal nor the select indicate the actual arrival of the out-) 5.085 F(of-band data, but only noti\214cation that it is pending.)72 355.2 Q .222(In addition to the information passed, a logical mark is placed in the da\ ta stream to indicate the point)97 370.8 R .503 (at which the out of band data was sent.)72 382.8 R .502 (The remote login and remote shell applications use this facility to)5.503 F .675(propagate signals between client and server processes.)72 394.8 R .676 (When a signal \215ushs any pending output from the)5.675 F(remote process\(es\ \), all data up to the mark in the data stream is discarded.)72 406.8 Q 2.217 -.7(To s)97 422.4 T .816 (end an out of band message the MSG_OOB \215ag is supplied to a).7 F F3(send) 3.316 E F0(or)3.316 E F3(sendto)3.316 E F0 .816(calls, while to)3.316 F .343 (receive out of band data MSG_OOB should be indicated when performing a)72 434.4 R F3 -.37(re)2.844 G(cvfr).37 E(om)-.37 E F0(or)2.844 E F3 -.37(re)2.844 G(cv).37 E F0 2.844(call. T)2.844 F 2.844<6f8c>-.7 G(nd)494 434.4 Q .4(out if \ the read pointer is currently pointing at the mark in the data stream, the SIO\ CA)72 446.4 R .4(TMARK ioctl is pro-)-1.11 F(vided:)72 458.4 Q(ioctl\(s, SIOCA) 108 476.4 Q(TMARK, &yes\);)-1.11 E(If)72 494.4 Q F3(yes)2.943 E F0 .444 (is a 1 on return, the next read will return data after the mark.)2.943 F .444 (Otherwise \(assuming out of band data)5.444 F .534(has arrived\), the next re\ ad will provide data sent by the client prior to transmission of the out of ba\ nd sig-)72 506.4 R 2.879(nal. The)72 518.4 R .38(routine used in the remote lo\ gin process to \215ush output on receipt of an interrupt or quit signal is) 2.879 F(shown in Figure 5.)72 530.4 Q(It reads the normal data up to the mark \ \(to discard it\), then reads the out-of-band byte.)5 E 2.77(Ap)97 546 S .27(r\ ocess may also read or peek at the out-of-band data without \214rst reading up\ to the mark.)111.99 546 R .269(This is)5.269 F .203(more dif)72 558 R .203 (\214cult when the underlying protocol delivers the ur)-.18 F .203 (gent data in-band with the normal data, and only)-.18 F .937(sends noti\214ca\ tion of its presence ahead of time \(e.g., the TCP protocol used to implement \ streams in the)72 570 R .429(Internet domain\).)72 582 R -.4(Wi)5.429 G .429 (th such protocols, the out-of-band byte may not yet have arrived when a).4 F F3 -.37(re)2.93 G(cv).37 E F0 .43(is done)2.93 F 1.118 (with the MSG_OOB \215ag.)72 594 R 1.117 (In that case, the call will return an error of EWOULDBLOCK.)6.118 F -.8(Wo) 6.117 G 1.117(rse, there).8 F .186(may be enough in-band data in the input buf) 72 606 R .187 (fer that normal \215ow control prevents the peer from sending the)-.18 F(ur)72 618 Q 1.525(gent data until the buf)-.18 F 1.525(fer is cleared.)-.18 F 1.525 (The process must then read enough of the queued data that the)6.525 F(ur)72 630 Q(gent data may be delivered.)-.18 E .122 (Certain programs that use multiple bytes of ur)97 645.6 R .123 (gent data and must handle multiple ur)-.18 F .123(gent signals \(e.g.,)-.18 F F3(telnet)72 657.6 Q F0 1.129(\(1C\)\) need to retain the position of ur)1.666 F 1.128(gent data within the stream.)-.18 F 1.128 (This treatment is available as a)6.128 F .052 (socket-level option, SO_OOBINLINE; see)72 669.6 R F3(setsockopt)2.552 E F0 .052(\(2\) for usage.)1.666 F -.4(Wi)5.053 G .053 (th this option, the position of ur).4 F(gent)-.18 E 1.258(data \(the `)72 681.6 R(`mark')-.74 E 1.258('\) is retained, but the ur)-.74 F 1.257 (gent data immediately follows the mark within the normal data)-.18 F .595 (stream returned without the MSG_OOB \215ag.)72 693.6 R .595 (Reception of multiple ur)5.595 F .595(gent indications causes the mark to)-.18 F(move, but no out-of-band data are lost.)72 705.6 Q EP %%Page: 25 25 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-25) -.35 F(#include )108 90 Q(#include )108 102 Q(...) 110.5 114 Q(oob\(\))108 126 Q({)108 138 Q(int out = FWRITE, mark;)133 150 Q (char waste[BUFSIZ];)133 162 Q(/* \215ush local terminal output */)133 186 Q (ioctl\(1, TIOCFLUSH, \(char *\)&out\);)133 198 Q(for \(;;\) {)133 210 Q (if \(ioctl\(rem, SIOCA)158 222 Q(TMARK, &mark\) < 0\) {)-1.11 E (perror\("ioctl"\);)183 234 Q(break;)183 246 Q(})158 258 Q(if \(mark\))158 270 Q(break;)183 282 Q(\(void\) read\(rem, waste, sizeof \(waste\)\);)158 294 Q(}) 133 306 Q(if \(recv\(rem, &mark, 1, MSG_OOB\) < 0\) {)133 318 Q (perror\("recv"\);)158 330 Q(...)158 342 Q(})133 354 Q(...)133 366 Q(})108 378 Q(Figure 5.)162.735 396 Q (Flushing terminal I/O on receipt of out of band data.)5 E/F1 10/Times-Bold@0 SF 2.5(5.2. Non-Blocking)72 420 R(Sockets)2.5 E F0 .611(It is occasionally con\ venient to make use of sockets which do not block; that is, I/O requests which) 97 435.6 R .608(cannot complete immediately and would therefore cause the proc\ ess to be suspended awaiting completion)72 447.6 R .524 (are not executed, and an error code is returned.)72 459.6 R .523 (Once a socket has been created via the)5.524 F/F2 10/Times-Italic@0 SF(socket) 3.023 E F0 .523(call, it may)3.023 F(be marked as non-blocking by)72 471.6 Q F2 (fcntl)2.5 E F0(as follows:)2.5 E(#include )108 489.6 Q(...)110.5 501.6 Q 11.94(int s;)108 513.6 R(...)110.5 525.6 Q 2.5(s=s)108 537.6 S (ocket\(AF_INET)126.42 537.6 Q 2.5(,S)-.74 G(OCK_STREAM, 0\);)201.78 537.6 Q (...)110.5 549.6 Q(if \(fcntl\(s, F_SETFL, FNDELA)108 561.6 Q(Y\) < 0\))-.92 E (perror\("fcntl F_SETFL, FNDELA)133 573.6 Q(Y"\);)-.92 E(exit\(1\);)133 585.6 Q (})108 597.6 Q(...)110.5 609.6 Q 3.502(When performing non-blocking I/O on soc\ kets, one must be careful to check for the error)97 631.2 R .673 (EWOULDBLOCK \(stored in the global variable)72 643.2 R F2(errno)3.172 E F0 .672(\), which occurs when an operation would normally)B .554 (block, but the socket it was performed on is marked as non-blocking.)72 655.2 R .555(In particular)5.555 F(,)-.4 E F2(accept)3.055 E F0(,)A F2(connect)3.055 E F0(,)A F2(send)3.055 E F0(,)A F2 -.37(re)72 667.2 S(cv).37 E F0(,)A F2 -.37 (re)2.834 G(ad).37 E F0 2.834(,a)C(nd)121.908 667.2 Q F2(write)2.833 E F0 .333 (can all return EWOULDBLOCK, and processes should be prepared to deal with suc\ h)2.833 F .904(return codes.)72 679.2 R .904(If an operation such as a)5.904 F F2(send)3.404 E F0 .904(cannot be done in its entirety)3.404 F 3.404(,b)-.65 G .905(ut partial writes are sensible)388.18 679.2 R .162(\(for example, when us\ ing a stream socket\), the data that can be sent immediately will be processed\ , and the)72 691.2 R(return value will indicate the amount actually sent.)72 703.2 Q EP %%Page: 26 26 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-26 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 10/Times-Bold@0 SF 2.5(5.3. Interrupt)72 84 R (driven socket I/O)2.5 E F0 .231(The SIGIO signal allows a process to be noti\ \214ed via a signal when a socket \(or more generally)97 99.6 R -5.23 2.731 (,a \214)-.65 H(le)496.78 99.6 Q .891 (descriptor\) has data waiting to be read.)72 111.6 R .89 (Use of the SIGIO facility requires three steps:)5.891 F .89 (First, the process)5.89 F .004 (must set up a SIGIO signal handler by use of the)72 123.6 R/F2 10 /Times-Italic@0 SF(signal)2.505 E F0(or)2.505 E F2(sigvec)2.505 E F0 2.505 (calls. Second,)2.505 F .005(it must set the process id or)2.505 F .02(process\ group id which is to receive noti\214cation of pending input to its own proce\ ss id, or the process group)72 135.6 R .182(id of its process group \(note tha\ t the default process group of a socket is group zero\).)72 147.6 R .183 (This is accomplished)5.182 F .023(by use of an)72 159.6 R F2(fcntl)2.523 E F0 2.523(call. Third,)2.523 F .022(it must enable asynchronous noti\214cation of \ pending I/O requests with another)2.522 F F2(fcntl)72 171.6 Q F0 3.618 (call. Sample)3.618 F 1.118(code to allow a given process to receive informati\ on on pending I/O requests as they)3.618 F .226(occur for a socket)72 183.6 R F2(s)2.726 E F0 .226(is given in Figure 6.)2.726 F -.4(Wi)5.226 G .226 (th the addition of a handler for SIGURG, this code can also be).4 F (used to prepare for receipt of SIGURG signals.)72 195.6 Q(#include ) 108 213.6 Q(...)110.5 225.6 Q 11.94(int io_handler\(\);)108 237.6 R(...)110.5 249.6 Q(signal\(SIGIO, io_handler\);)108 261.6 Q (/* Set the process receiving SIGIO/SIGURG signals to us */)108 285.6 Q (if \(fcntl\(s, F_SET)108 309.6 Q(OWN, getpid\(\)\) < 0\) {)-.18 E (perror\("fcntl F_SET)133 321.6 Q(OWN"\);)-.18 E(exit\(1\);)133 333.6 Q(})108 345.6 Q(/* Allow receipt of asynchronous I/O signals */)108 369.6 Q (if \(fcntl\(s, F_SETFL, F)108 393.6 Q(ASYNC\) < 0\) {)-.74 E (perror\("fcntl F_SETFL, F)133 405.6 Q(ASYNC"\);)-.74 E(exit\(1\);)133 417.6 Q (})108 429.6 Q(Figure 6.)169.265 447.6 Q (Use of asynchronous noti\214cation of I/O requests.)5 E F1 2.5(5.4. Signals)72 483.6 R(and pr)2.5 E(ocess gr)-.18 E(oups)-.18 E F0 .477(Due to the existence \ of the SIGURG and SIGIO signals each socket has an associated process num-)97 499.2 R(ber)72 511.2 Q 2.816(,j)-.4 G .316(ust as is done for terminals.)92.466 511.2 R .315(This value is initialized to zero, but may be rede\214ned at a la\ ter time with)5.315 F .886(the F_SET)72 523.2 R(OWN)-.18 E F2(fcntl)3.386 E F0 3.386(,s)C .886(uch as was done in the code above for SIGIO.)170.588 523.2 R 2.287 -.7(To s)5.886 H .887(et the socket').7 F 3.387(sp)-.55 G .887 (rocess id for)452.796 523.2 R .548(signals, positive ar)72 535.2 R .547 (guments should be given to the)-.18 F F2(fcntl)3.047 E F0 3.047(call. T)3.047 F 3.047(os)-.7 G .547(et the socket')338.798 535.2 R 3.047(sp)-.55 G .547 (rocess group for signals,)404.599 535.2 R .464(negative ar)72 547.2 R .464 (guments should be passed to)-.18 F F2(fcntl)2.965 E F0 5.465(.N)C .465 (ote that the process number indicates either the associated)268.66 547.2 R .049(process id or the associated process group; it is impossible to specify b\ oth at the same time.)72 559.2 R 2.548(As)5.048 G(imilar)457.282 559.2 Q F2 (fcntl)2.548 E F0(,)A(F_GET)72 571.2 Q (OWN, is available for determining the current process number of a socket.)-.18 E 1.931 (Another signal which is useful when constructing server processes is SIGCHLD.) 97 586.8 R 1.931(This signal is)6.931 F 1.173 (delivered to a process when any child processes have changed state.)72 598.8 R 1.172(Normally servers use the signal to)6.172 F -.74(``)72 610.8 S(reap').74 E 2.713('c)-.74 G .214(hild processes that have exited without explicitly awaiti\ ng their termination or periodic polling for)108.203 610.8 R .747(exit status.) 72 622.8 R .746(For example, the remote login server loop shown in Figure 2 ma\ y be augmented as shown in)5.747 F(Figure 7.)72 634.8 Q .582 (If the parent server process fails to reap its children, a lar)97 650.4 R .582 (ge number of `)-.18 F(`zombie')-.74 E 3.082('p)-.74 G .582(rocesses may be) 437.856 650.4 R(created.)72 662.4 Q F1 2.5(5.5. Pseudo)72 686.4 R(terminals)2.5 E F0 .979(Many programs will not function properly without a terminal for stan\ dard input and output.)97 702 R(Since)5.978 E .91(sockets do not provide the s\ emantics of terminals, it is often necessary to have a process communicating)72 714 R .22(over the network do so through a)72 726 R F2(pseudo-terminal)2.72 E F0 5.22(.A)C .22(pseudo- terminal is actually a pair of devices, master)291.45 726 R EP %%Page: 27 27 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-27) -.35 F(int reaper\(\);)108 90 Q(...)110.5 102 Q(signal\(SIGCHLD, reaper\);)108 114 Q(listen\(f, 5\);)108 126 Q(for \(;;\) {)108 138 Q (int g, len = sizeof \(from\);)133 150 Q 2.5(g=a)133 174 S (ccept\(f, \(struct sockaddr *\)&from, &len,\);)153.08 174 Q(if \(g < 0\) {)133 186 Q(if \(errno != EINTR\))158 198 Q (syslog\(LOG_ERR, "rlogind: accept: %m"\);)183 210 Q(continue;)158 222 Q(})133 234 Q(...)133 246 Q(})108 258 Q(...)110.5 270 Q(#include )108 282 Q (reaper\(\))108 294 Q({)108 306 Q(union wait status;)133 318 Q (while \(wait3\(&status, WNOHANG, 0\) > 0\))133 342 Q(;)158 354 Q(})108 366 Q (Figure 7.)209.535 396 Q(Use of the SIGCHLD signal.)5 E 1.572(and slave, which\ allow a process to serve as an active agent in communication between processe\ s and)72 420 R 2.595(users. Data)72 432 R .094(written on the slave side of a \ pseudo-terminal is supplied as input to a process reading from the)2.595 F .129 (master side, while data written on the master side are processed as terminal \ input for the slave.)72 444 R .129(In this way)5.129 F(,)-.65 E .564(the proce\ ss manipulating the master side of the pseudo-terminal has control over the in\ formation read and)72 456 R .432(written on the slave side as if it were manip\ ulating the keyboard and reading the screen on a real terminal.)72 468 R .737(\ The purpose of this abstraction is to preserve terminal semantics over a netwo\ rk connection\212 that is, the)72 480 R(slave side appears as a normal termina\ l to any process reading from or writing to it.)72 492 Q .921(For example, the\ remote login server uses pseudo-terminals for remote login sessions.)97 507.6 R 3.421(Au)5.921 G .921(ser log-)472.809 507.6 R .377(ging in to a machine acr\ oss the network is provided a shell with a slave pseudo-terminal as standard i\ nput,)72 519.6 R .922(output, and error)72 531.6 R 5.922(.T)-.55 G .922(he ser\ ver process then handles the communication between the programs invoked by) 154.756 531.6 R .51(the remote shell and the user)72 543.6 R 1.61 -.55('s l).37 H .51(ocal client process.).55 F .509 (When a user sends a character that generates an inter)5.509 F(-)-.2 E .802(ru\ pt on the remote machine that \215ushes terminal output, the pseudo-terminal g\ enerates a control message)72 555.6 R .382(for the server process.)72 567.6 R .382(The server then sends an out of band message to the client process to sig\ nal a \215ush)5.382 F (of data at the real terminal and on the intervening data buf)72 579.6 Q (fered in the network.)-.18 E .197 (Under 4.3BSD, the name of the slave side of a pseudo-terminal is of the form) 97 595.2 R/F1 10/Times-Italic@0 SF(/dev/ttyxy)2.698 E F0 2.698(,w)C(here) 463.146 595.2 Q F1(x)2.698 E F0 .198(is a)2.698 F 1.279 (single letter starting at `p' and continuing to `t'.)72 607.2 R F1(y)6.278 E F0 1.278(is a hexadecimal digit \(i.e., a single character in the)3.778 F .959 (range 0 through 9 or `a' through `f)72 619.2 R 3.459('\). The).55 F .96 (master side of a pseudo-terminal is)3.459 F F1(/dev/ptyxy)3.46 E F0 3.46(,w)C (here)453.09 619.2 Q F1(x)3.46 E F0(and)3.46 E F1(y)3.46 E F0 (correspond to the slave side of the pseudo-terminal.)72 631.2 Q .644(In gener\ al, the method of obtaining a pair of master and slave pseudo-terminals is to \ \214nd a pseudo-)97 646.8 R .513(terminal which is not currently in use.)72 658.8 R .514 (The master half of a pseudo-terminal is a single-open device; thus,)5.513 F .733(each master may be opened in turn until an open succeeds.)72 670.8 R .732 (The slave side of the pseudo-terminal is then)5.733 F .784 (opened, and is set to the proper terminal modes if necessary)72 682.8 R 5.784 (.T)-.65 G .784(he process then)332.702 682.8 R F1(fork)3.284 E F0 .784 (s; the child closes the)B .865(master side of the pseudo-terminal, and)72 694.8 R F1(exec)3.364 E F0 3.364(st)C .864(he appropriate program.)265.232 694.8 R .864(Meanwhile, the parent closes the)5.864 F .442(slave side of the p\ seudo-terminal and begins reading and writing from the master side.)72 706.8 R .442(Sample code mak-)5.442 F .604(ing use of pseudo-terminals is given in Fig\ ure 8; this code assumes that a connection on a socket)72 718.8 R F1(s)3.104 E F0(exists,)3.104 E .934(connected to a peer who wants a service of some kind, \ and that the process has disassociated itself from)72 730.8 R EP %%Page: 28 28 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-28 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(any previous controlling terminal.)72 84 Q(gotpty = 0;)108 102 Q(for \(c = 'p'; !gotpty && c <= ')108 114 Q(s'; c++\) {)-.55 E (line = "/dev/ptyXX";)133 126 Q(line[sizeof\("/dev/pty"\)-1] = c;)133 138 Q (line[sizeof\("/dev/ptyp"\)-1] = '0';)133 150 Q (if \(stat\(line, &statbuf\) < 0\))133 162 Q(break;)158 174 Q (for \(i = 0; i < 16; i++\) {)133 186 Q (line[sizeof\("/dev/ptyp"\)-1] = "0123456789abcdef"[i];)158 198 Q (master = open\(line, O_RDWR\);)158 210 Q(if \(master > 0\) {)158 222 Q (gotpty = 1;)183 234 Q(break;)183 246 Q(})158 258 Q(})133 270 Q(})108 282 Q (if \(!gotpty\) {)108 294 Q(syslog\(LOG_ERR, "All network ports in use"\);)133 306 Q(exit\(1\);)133 318 Q(})108 330 Q(line[sizeof\("/dev/"\)-1] = ')108 354 Q (t';)-.18 E(slave = open\(line, O_RDWR\);)108 366 Q(/*)2.71 E/F1 10 /Times-Italic@0 SF(slave)2.5 E F0(is now slave side */)2.5 E (if \(slave < 0\) {)108 378 Q (syslog\(LOG_ERR, "Cannot open slave pty %s", line\);)133 390 Q(exit\(1\);)133 402 Q(})108 414 Q(ioctl\(slave, TIOCGETP)108 438 Q 2.5(,&)-1.11 G 4.73(b\); /*) 214.66 438 R(Set slave tty modes */)2.5 E(b.sg_\215ags = CRMOD|XT)108 450 Q (ABS|ANYP;)-.8 E(ioctl\(slave, TIOCSETP)108 462 Q 2.5(,&)-1.11 G(b\);)213 462 Q 2.5(i=f)108 486 S(ork\(\);)124.75 486 Q(if \(i < 0\) {)108 498 Q (syslog\(LOG_ERR, "fork: %m"\);)133 510 Q(exit\(1\);)133 522 Q 2.5(}e)108 534 S (lse if \(i\) {)119.74 534 Q(/* Parent */)208 534 Q(close\(slave\);)133 546 Q (...)133 558 Q 2.5(}e)108 570 S(lse {)119.74 570 Q(/* Child */)185.5 570 Q (\(void\) close\(s\);)133 582 Q(\(void\) close\(master\);)133 594 Q (dup2\(slave, 0\);)133 606 Q(dup2\(slave, 1\);)133 618 Q(dup2\(slave, 2\);)133 630 Q(if \(slave > 2\))133 642 Q(\(void\) close\(slave\);)158 654 Q(...)133 666 Q(})108 678 Q(Figure 8.)191.625 696 Q(Creation and use of a pseudo terminal)5 E EP %%Page: 29 29 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-29) -.35 F/F1 10/Times-Bold@0 SF 2.5(5.6. Selecting)72 84 R(speci\214c pr)2.5 E (otocols)-.18 E F0 1.609(If the third ar)97 99.6 R 1.609(gument to the)-.18 F /F2 10/Times-Italic@0 SF(socket)4.109 E F0 1.609(call is 0,)4.109 F F2(socket) 4.109 E F0 1.609(will select a default protocol to use with the)4.109 F .357 (returned socket of the type requested.)72 111.6 R .358 (The default protocol is usually correct, and alternate choices are not)5.357 F .093(usually available.)72 123.6 R(However)5.093 E 2.593(,w)-.4 G .093 (hen using `)195.739 123.6 R(`raw')-.74 E 2.593('s)-.74 G .092 (ockets to communicate directly with lower)270.348 123.6 R .092 (-level protocols)-.2 F .617(or hardware interfaces, the protocol ar)72 135.6 R .617(gument may be important for setting up demultiplexing.)-.18 F .618 (For exam-)5.618 F .621(ple, raw sockets in the Internet family may be used to\ implement a new protocol above IP)72 147.6 R 3.12(,a)-1.11 G .62 (nd the socket)449.99 147.6 R .208 (will receive packets only for the protocol speci\214ed.)72 159.6 R 1.609 -.7 (To o)5.209 H .209(btain a particular protocol one determines the pro-).7 F .857(tocol number as de\214ned within the communication domain.)72 171.6 R .857 (For the Internet domain one may use one of)5.857 F (the library routines discussed in section 3, such as)72 183.6 Q F2(getpr)2.5 E (otobyname)-.37 E F0(:)A(#include )108 201.6 Q (#include )108 213.6 Q(#include )108 225.6 Q (#include )108 237.6 Q(...)110.5 249.6 Q (pp = getprotobyname\("newtcp"\);)108 261.6 Q 2.5(s=s)108 273.6 S (ocket\(AF_INET)126.42 273.6 Q 2.5(,S)-.74 G(OCK_STREAM, pp->p_proto\);)201.78 273.6 Q 1.809(This would result in a socket)72 291.6 R F2(s)4.309 E F0 1.81 (using a stream based connection, but with protocol type of `)4.309 F(`newtcp') -.74 E(')-.74 E(instead of the default `)72 303.6 Q(`tcp.')-.74 E(')-.74 E .302 (In the NS domain, the available socket protocols are de\214ned in <)97 319.2 R F2(netns/ns.h)A F0 2.802(>. T)B 2.802(oc)-.7 G .301(reate a raw socket)431.186 319.2 R(for Xerox Error Protocol messages, one might use:)72 331.2 Q (#include )108 349.2 Q(#include )108 361.2 Q (#include )108 373.2 Q(...)110.5 385.2 Q 2.5(s=s)108 397.2 S (ocket\(AF_NS, SOCK_RA)126.42 397.2 Q 1.84 -.92(W, N)-.8 H(SPROT).92 E (O_ERROR\);)-.18 E F1 2.5(5.7. Addr)72 427.2 R(ess binding)-.18 E F0 .611(As w\ as mentioned in section 2, binding addresses to sockets in the Internet and NS\ domains can be)97 442.8 R .852(fairly complex.)72 454.8 R .852 (As a brief reminder)5.852 F 3.352(,t)-.4 G .852 (hese associations are composed of local and foreign addresses, and)228.622 454.8 R .256(local and foreign ports.)72 466.8 R .256(Port numbers are allocat\ ed out of separate spaces, one for each system and one for)5.256 F .792 (each domain on that system.)72 478.8 R .791(Through the)5.791 F F2(bind)3.291 E F0 .791(system call, a process may specify half of an association,)3.291 F .129(the part, while the)72 490.8 R F2(connect) 2.629 E F0(and)2.629 E F2(accept)2.629 E F0 .129 (primitives are used to complete a socket')2.629 F(s)-.55 E .72 (association by specifying the part.)72 502.8 R .719(Since the association is created in two)5.719 F 1.142(steps the associati\ on uniqueness requirement indicated previously could be violated unless care i\ s taken.)72 514.8 R(Further)72 526.8 Q 3.024(,i)-.4 G 3.024(ti)109.344 526.8 S 3.024(su)117.928 526.8 S .523(nrealistic to expect user programs to always kno\ w proper values to use for the local address)129.842 526.8 R 1.029(and local p\ ort since a host may reside on multiple networks and the set of allocated port\ numbers is not)72 538.8 R(directly accessible to a user)72 550.8 Q(.)-.55 E 1.562 -.7(To s)97 566.4 T .161 (implify local address binding in the Internet domain the notion of a `).7 F (`wildcard')-.74 E 2.661('a)-.74 G .161(ddress has been)440.918 566.4 R 2.838 (provided. When)72 578.4 R .339(an address is speci\214ed as INADDR_ANY \(a ma\ nifest constant de\214ned in \),)2.838 F .429 (the system interprets the address as `)72 590.4 R .429(`any valid address') -.74 F 2.929('. For)-.74 F .429(example, to bind a speci\214c port number to a) 2.929 F(socket, but leave the local address unspeci\214ed, the following code \ might be used:)72 602.4 Q EP %%Page: 30 30 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-30 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(#include )108 84 Q(#include )108 96 Q(...)110.5 108 Q(struct sockaddr_in sin;)108 120 Q(...)110.5 132 Q 2.5(s=s)108 144 S(ocket\(AF_INET)126.42 144 Q 2.5(,S)-.74 G(OCK_STREAM, 0\);)201.78 144 Q (sin.sin_family = AF_INET)108 156 Q(;)-.55 E(sin.sin_addr)108 168 Q (.s_addr = htonl\(INADDR_ANY\);)-.55 E(sin.sin_port = htons\(MYPOR)108 180 Q (T\);)-.6 E(bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)108 192 Q .793(Sockets with wildcarded local addresses may receive messages directed to \ the speci\214ed port number)72 210 R 3.293(,a)-.4 G(nd)494 210 Q .006 (sent to any of the possible addresses assigned to a host.)72 222 R .006 (For example, if a host has addresses 128.32.0.4 and)5.006 F .317(10.0.0.78, a\ nd a socket is bound as above, the process will be able to accept connection r\ equests which are)72 234 R 1.02(addressed to 128.32.0.4 or 10.0.0.78.)72 246 R 1.02(If a server process wished to only allow hosts on a given network)6.02 F(\ connect to it, it would bind the address of the host on the appropriate networ\ k.)72 258 Q .239(In a similar fashion, a local port may be left unspeci\214ed \ \(speci\214ed as zero\), in which case the system)97 273.6 R .345 (will select an appropriate port number for it.)72 285.6 R .345 (This shortcut will work both in the Internet and NS domains.)5.345 F(For exam\ ple, to bind a speci\214c local address to a socket, but to leave the local po\ rt number unspeci\214ed:)72 297.6 Q(hp = gethostbyname\(hostname\);)108 315.6 Q (if \(hp == NULL\) {)108 327.6 Q(...)133 339.6 Q(})108 351.6 Q (bcopy\(hp->h_addr)108 363.6 Q 2.5(,\()-.4 G(char *\) sin.sin_addr)190.44 363.6 Q 2.5(,h)-.4 G(p->h_length\);)279.19 363.6 Q(sin.sin_port = htons\(0\);)108 375.6 Q(bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)108 387.6 Q .283 (The system selects the local port number based on two criteria.)72 405.6 R .284(The \214rst is that on 4BSD systems, Internet)5.283 F .863 (ports below IPPOR)72 417.6 R(T_RESER)-.6 E .863 (VED \(1024\) \(for the Xerox domain, 0 through 3000\) are reserved for privi-) -.8 F .225(leged users \(i.e., the super user\); Internet ports above IPPOR)72 429.6 R(T_USERRESER)-.6 E .226(VED \(50000\) are reserved for)-.8 F .269 (non-privileged servers.)72 441.6 R .268(The second is that the port number is\ not currently bound to some other socket.)5.269 F(In)5.268 E .115 (order to \214nd a free Internet port number in the privileged range the)72 453.6 R/F1 10/Times-Italic@0 SF(rr)2.615 E(esvport)-.37 E F0 .115 (library routine may be used as)2.615 F (follows to return a stream socket in with a privileged port number:)72 465.6 Q (int lport = IPPOR)108 483.6 Q(T_RESER)-.6 E(VED \255 1;)-.8 E(int s;)108 495.6 Q 2.5(s=r)108 507.6 S(resvport\(&lport\);)125.86 507.6 Q(if \(s < 0\) {)108 519.6 Q(if \(errno == EAGAIN\))133 531.6 Q(fprintf\(stderr)158 543.6 Q 2.5(,") -.4 G(socket: all ports in use\\n"\);)218.33 543.6 Q(else)133 555.6 Q (perror\("rresvport: socket"\);)158 567.6 Q(...)133 579.6 Q(})108 591.6 Q .028 (The restriction on allocating ports was done to allow processes executing in \ a `)72 609.6 R(`secure')-.74 E 2.527('e)-.74 G .027(nvironment to per)429.156 609.6 R(-)-.2 E 1.238 (form authentication based on the originating address and port number)72 621.6 R 6.238(.F)-.55 G 1.238(or example, the)375.458 621.6 R F1(rlogin)3.738 E F0 1.238(\(1\) com-)B .684(mand allows users to log in across a network without b\ eing asked for a password, if two conditions hold:)72 633.6 R .744 (First, the name of the system the user is logging in from is in the \214le)72 645.6 R F1(/etc/hosts.equiv)3.244 E F0 .744(on the system he is)3.244 F .494 (logging in to \(or the system name and the user name are in the user)72 657.6 R -.55('s).37 G F1(.r)3.544 E(hosts)-.18 E F0 .494(\214le in the user)2.994 F 1.594 -.55('s h).37 H .494(ome direc-).55 F 1.488 (tory\), and second, that the user)72 669.6 R 2.588 -.55('s r).37 H 1.488 (login process is coming from a privileged port on the machine from).55 F .393 (which he is logging.)72 681.6 R .392(The port number and network address of t\ he machine from which the user is logging)5.393 F (in can be determined either by the)72 693.6 Q F1(fr)2.5 E(om)-.37 E F0 (result of the)2.5 E F1(accept)2.5 E F0(call, or from the)2.5 E F1(getpeername) 2.5 E F0(call.)2.5 E .137(In certain cases the algorithm used by the system in\ selecting port numbers is unsuitable for an appli-)97 709.2 R 4.523 (cation. This)72 721.2 R 2.023 (is because associations are created in a two step process.)4.523 F 2.022 (For example, the Internet \214le)7.022 F EP %%Page: 31 31 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-31) -.35 F 1.793(transfer protocol, FTP)72 84 R 4.293(,s)-1.11 G 1.794(peci\214es \ that data connections must always originate from the same local port.)173.759 84 R(However)72 96 Q 3.225(,d)-.4 G .725 (uplicate associations are avoided by connecting to dif)118.975 96 R .725 (ferent foreign ports.)-.18 F .725(In this situation the)5.725 F .516(system w\ ould disallow binding the same local address and port number to a socket if a \ previous data con-)72 108 R(nection')72 120 Q 3.188(ss)-.55 G .688 (ocket still existed.)115.188 120 R 2.088 -.7(To o)5.688 H .687 (verride the default port selection algorithm, an option call must be per).7 F (-)-.2 E(formed prior to address binding:)72 132 Q(...)110.5 150 Q 11.94 (int on)108 162 R 2.5(=1)2.5 G(;)158.64 162 Q(...)110.5 174 Q (setsockopt\(s, SOL_SOCKET)108 186 Q 2.5(,S)-.74 G (O_REUSEADDR, &on, sizeof\(on\)\);)235.04 186 Q (bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)108 198 Q -.4(Wi)72 216 S 1.357 (th the above call, local addresses may be bound which are already in use.).4 F 1.357(This does not violate the)6.357 F 1.281(uniqueness requirement as the sy\ stem still checks at connect time to be sure any other sockets with the)72 228 R 1.61 (same local address and port do not have the same foreign address and port.)72 240 R 1.61(If the association already)6.61 F (exists, the error EADDRINUSE is returned.)72 252 Q/F1 10/Times-Bold@0 SF 2.5 (5.8. Br)72 276 R(oadcasting and determining network con\214guration)-.18 E F0 .388(By using a datagram socket, it is possible to send broadcast packets on m\ any networks supported by)97 291.6 R .635(the system.)72 303.6 R .635(The netw\ ork itself must support broadcast; the system provides no simulation of broadc\ ast in)5.635 F 3.235(software. Broadcast)72 315.6 R .734(messages can place a \ high load on a network since they force every host on the net-)3.235 F 1.735 (work to service them.)72 327.6 R(Consequently)6.735 E 4.235(,t)-.65 G 1.735 (he ability to send broadcast packets has been limited to sockets)234.455 327.6 R .025(which are explicitly marked as allowing broadcasting.)72 339.6 R .025 (Broadcast is typically used for one of two reasons: it)5.025 F .593(is desire\ d to \214nd a resource on a local network without prior knowledge of its addre\ ss, or important func-)72 351.6 R(tions such as routing require that informati\ on be sent to all accessible neighbors.)72 363.6 Q 1.4 -.7(To s)97 379.2 T (end a broadcast message, a datagram socket should be created:).7 E 2.5(s=s)108 397.2 S(ocket\(AF_INET)126.42 397.2 Q 2.5(,S)-.74 G(OCK_DGRAM, 0\);)201.78 397.2 Q(or)72 415.2 Q 2.5(s=s)108 433.2 S(ocket\(AF_NS, SOCK_DGRAM, 0\);)126.42 433.2 Q(The socket is marked as allowing broadcasting,)72 451.2 Q 11.94(int on) 108 469.2 R 2.5(=1)2.5 G(;)158.64 469.2 Q(setsockopt\(s, SOL_SOCKET)108 493.2 Q 2.5(,S)-.74 G(O_BROADCAST)235.04 493.2 Q 2.5(,&)-.74 G(on, sizeof \(on\)\);) 319.86 493.2 Q(and at least a port number should be bound to the socket:)72 511.2 Q(sin.sin_family = AF_INET)108 529.2 Q(;)-.55 E(sin.sin_addr)108 541.2 Q (.s_addr = htonl\(INADDR_ANY\);)-.55 E(sin.sin_port = htons\(MYPOR)108 553.2 Q (T\);)-.6 E(bind\(s, \(struct sockaddr *\) &sin, sizeof \(sin\)\);)108 565.2 Q (or)72 583.2 Q 2.5(,f)-.4 G(or the NS domain,)88.26 583.2 Q (sns.sns_family = AF_NS;)108 601.2 Q(netnum = htonl\(net\);)108 613.2 Q (sns.sns_addr)108 625.2 Q (.x_net = *\(union ns_net *\) &netnum; /* insert net number */)-.55 E (sns.sns_addr)108 637.2 Q(.x_port = htons\(MYPOR)-.55 E(T\);)-.6 E (bind\(s, \(struct sockaddr *\) &sns, sizeof \(sns\)\);)108 649.2 Q .269(The d\ estination address of the message to be broadcast depends on the network\(s\) \ on which the message is)72 667.2 R .32(to be broadcast.)72 679.2 R .321(The In\ ternet domain supports a shorthand notation for broadcast on the local network\ , the)5.32 F 1.859(address INADDR_BROADCAST \(de\214ned in <)72 691.2 R/F2 10 /Times-Italic@0 SF(netinet/in.h)A F0 4.358(>. T)B 4.358(od)-.7 G 1.858 (etermine the list of addresses for all)350.102 691.2 R .726(reachable neighbo\ rs requires knowledge of the networks to which the host is connected.)72 703.2 R .726(Since this infor)5.726 F(-)-.2 E 1.154(mation should be obtained in a h\ ost-independent fashion and may be impossible to derive, 4.3BSD pro-)72 715.2 R .978(vides a method of retrieving this information from the system data struct\ ures.)72 727.2 R .979(The SIOCGIFCONF)5.979 F F2(ioctl)3.479 E EP %%Page: 32 32 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-32 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E .609 (call returns the interface con\214guration of a host in the form of a single) 72 84 R/F1 10/Times-Italic@0 SF(ifconf)3.109 E F0 .608 (structure; this structure con-)3.109 F .796(tains a `)72 96 R .796 (`data area')-.74 F 3.296('w)-.74 G .796(hich is made up of an array of of) 160.884 96 R F1(ifr)3.297 E(eq)-.37 E F0 .797 (structures, one for each network interface to)3.297 F (which the host is connected.)72 108 Q(These structures are de\214ned in)5 E F1 ()2.5 E F0(as follows:)2.5 E(struct ifconf {)108 126 Q 22.94 (int ifc_len;)144 138 R(/* size of associated buf)360 138 Q(fer */)-.18 E (union {)144 150 Q 3.51(caddr_t ifcu_buf;)180 162 R 11.28(struct ifreq)180 174 R(*ifcu_req;)2.5 E 2.5(}i)144 186 S(fc_ifcu;)154.08 186 Q(};)108 198 Q 4.06 (#de\214ne ifc_buf)108 222 R 110.02(ifc_ifcu.ifcu_buf /*)7.12 F(buf)2.5 E (fer address */)-.18 E 4.06(#de\214ne ifc_req)108 234 R 110.58 (ifc_ifcu.ifcu_req /*)7.68 F(array of structures returned */)2.5 E 4.06 (#de\214ne IFNAMSIZ)108 258 R(16)216 258 Q(struct ifreq {)108 282 Q 16.29 (char ifr_name[IFNAMSIZ];)144 294 R(/* if name, e.g. "en0" */)360 294 Q (union {)144 306 Q 11.28(struct sockaddr)180 318 R(ifru_addr;)2.5 E 11.28 (struct sockaddr)180 330 R(ifru_dstaddr;)2.5 E 11.28(struct sockaddr)180 342 R (ifru_broadaddr;)2.5 E 13.5(short ifru_\215ags;)180 354 R 3.51 (caddr_t ifru_data;)180 366 R 2.5(}i)144 378 S(fr_ifru;)154.08 378 Q(};)108 390 Q 2.5(#de\214ne ifr_addr)108 414 R 25.27(ifr_ifru.ifru_addr /*)202.42 414 R (address */)2.5 E 2.5(#de\214ne ifr_dstaddr)108 426 R 13.6 (ifr_ifru.ifru_dstaddr /*)202.42 426 R(other end of p-to-p link */)2.5 E 2.5 (#de\214ne ifr_broadaddr ifr_ifru.ifru_broadaddr /*)108 438 R (broadcast address */)2.5 E 2.5(#de\214ne ifr_\215ags)108 450 R 24.15 (ifr_ifru.ifru_\215ags /*)202.42 450 R(\215ags */)2.5 E 2.5(#de\214ne ifr_data) 108 462 R 26.38(ifr_ifru.ifru_data /*)202.42 462 R(for use by interface */)2.5 E(The actual call which obtains the interface con\214guration is)72 480 Q (struct ifconf ifc;)108 498 Q(char buf[BUFSIZ];)108 510 Q (ifc.ifc_len = sizeof \(buf\);)108 534 Q(ifc.ifc_buf = buf;)108 546 Q (if \(ioctl\(s, SIOCGIFCONF)108 558 Q 2.5(,\()-.8 G(char *\) &ifc\) < 0\) {) 223.03 558 Q(...)133 570 Q(})108 582 Q 1.293(After this call)72 600 R F1(buf) 3.793 E F0 1.293(will contain one)3.793 F F1(ifr)3.793 E(eq)-.37 E F0 1.293 (structure for each network to which the host is connected, and)3.793 F F1 (ifc.ifc_len)72 612 Q F0 (will have been modi\214ed to re\215ect the number of bytes used by the)2.5 E F1(ifr)2.5 E(eq)-.37 E F0(structures.)2.5 E .517 (For each structure there exists a set of `)97 627.6 R .517 (`interface \215ags')-.74 F 3.017('w)-.74 G .518 (hich tell whether the network correspond-)333.95 627.6 R .954 (ing to that interface is up or down, point to point or broadcast, etc.)72 639.6 R .953(The SIOCGIFFLAGS)5.953 F F1(ioctl)3.453 E F0(retrieves)3.453 E (these \215ags for an interface speci\214ed by an)72 651.6 Q F1(ifr)2.5 E(eq) -.37 E F0(structure as follows:)2.5 E EP %%Page: 33 33 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-33) -.35 F(struct ifreq *ifr;)108 84 Q(ifr = ifc.ifc_req;)108 108 Q (for \(n = ifc.ifc_len / sizeof \(struct ifreq\); --n >= 0; ifr++\) {)108 132 Q (/*)133 144 Q 2.5(*W)135.5 156 S 2.5(em)151.64 156 S (ust be careful that we don')166.36 156 Q 2.5(tu)-.18 G(se an interface)282.82 156 Q 2.5(*d)135.5 168 S (evoted to an address family other than those intended;)148 168 Q 2.5(*i)135.5 180 S 2.5(fw)145.78 180 S 2.5(ew)158.83 180 S (ere interested in NS interfaces, the)172.99 180 Q 2.5(*A)135.5 192 S (F_INET would be AF_NS.)150.22 192 Q(*/)135.5 204 Q(if \(ifr)133 216 Q (->ifr_addr)-.2 E(.sa_family != AF_INET\))-.55 E(continue;)158 228 Q (if \(ioctl\(s, SIOCGIFFLAGS, \(char *\) ifr\) < 0\) {)133 240 Q(...)158 252 Q (})133 264 Q(/*)133 276 Q 2.5(*S)135.5 288 S(kip boring cases.)148.56 288 Q(*/) 135.5 300 Q(if \(\(ifr)133 312 Q(->ifr_\215ags & IFF_UP\) == 0 ||)-.2 E(\(ifr) 143 324 Q(->ifr_\215ags & IFF_LOOPBACK\) ||)-.2 E(\(ifr)143 336 Q (->ifr_\215ags & \(IFF_BROADCAST | IFF_POINTT)-.2 E(OPOINT\)\) == 0\))-.18 E (continue;)158 348 Q .426 (Once the \215ags have been obtained, the broadcast address must be obtained.) 97 369.6 R .427(In the case of broadcast)5.426 F .76 (networks this is done via the SIOCGIFBRDADDR)72 381.6 R/F1 10/Times-Italic@0 SF(ioctl)3.26 E F0 3.26(,w)C .76 (hile for point-to-point networks the address of)315.02 381.6 R (the destination host is obtained with SIOCGIFDST)72 393.6 Q(ADDR.)-.8 E (struct sockaddr dst;)108 411.6 Q(if \(ifr)108 435.6 Q (->ifr_\215ags & IFF_POINTT)-.2 E(OPOINT\) {)-.18 E(if \(ioctl\(s, SIOCGIFDST) 133 447.6 Q(ADDR, \(char *\) ifr\) < 0\) {)-.8 E(...)158 459.6 Q(})133 471.6 Q (bcopy\(\(char *\) ifr)133 483.6 Q(->ifr_dstaddr)-.2 E 2.5(,\()-.4 G (char *\) &dst, sizeof \(ifr)264.66 483.6 Q(->ifr_dstaddr\)\);)-.2 E 2.5(}e)108 495.6 S(lse if \(ifr)119.74 495.6 Q(->ifr_\215ags & IFF_BROADCAST\) {)-.2 E (if \(ioctl\(s, SIOCGIFBRDADDR, \(char *\) ifr\) < 0\) {)133 507.6 Q(...)158 519.6 Q(})133 531.6 Q(bcopy\(\(char *\) ifr)133 543.6 Q(->ifr_broadaddr)-.2 E 2.5(,\()-.4 G(char *\) &dst, sizeof \(ifr)275.76 543.6 Q(->ifr_broadaddr\)\);) -.2 E(})108 555.6 Q 1.755(After the appropriate)97 577.2 R F1(ioctl)4.255 E F0 2.855 -.55('s h)D 1.755 (ave obtained the broadcast or destination address \(now in).55 F F1(dst)4.255 E F0 1.755(\), the)B F1(sendto)72 589.2 Q F0(call may be used:)2.5 E (sendto\(s, buf, bu\215en, 0, \(struct sockaddr *\)&dst, sizeof \(dst\)\);)133 607.2 Q(})108 619.2 Q 1.104(In the above loop one)72 637.2 R F1(sendto)3.604 E F0 1.103 (occurs for every interface to which the host is connected that supports the) 3.604 F .268(notion of broadcast or point-to-point addressing.)72 649.2 R .268 (If a process only wished to send broadcast messages on a)5.268 F .396(given n\ etwork, code similar to that outlined above would be used, but the loop would \ need to \214nd the cor)72 661.2 R(-)-.2 E(rect destination address.)72 673.2 Q 1.102(Received broadcast messages contain the senders address and port, as dat\ agram sockets are bound)97 688.8 R(before a message is allowed to go out.)72 700.8 Q EP %%Page: 34 34 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-34 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E/F1 10/Times-Bold@0 SF 2.5(5.9. Socket)72 84 R(Options)2.5 E F0 .422(It is possible to set and get a number of options on sockets via the)97 99.6 R/F2 10/Times-Italic@0 SF(setsockopt)2.921 E F0(and)2.921 E F2(getsockopt) 2.921 E F0(system)2.921 E 3.617(calls. These)72 111.6 R 1.117(options include \ such things as marking a socket for broadcasting, not to route, to linger on) 3.617 F(close, etc.)72 123.6 Q(The general forms of the calls are:)5 E (setsockopt\(s, level, optname, optval, optlen\);)108 141.6 Q(and)72 159.6 Q (getsockopt\(s, level, optname, optval, optlen\);)108 177.6 Q .27 (The parameters to the calls are as follows:)97 199.2 R F2(s)2.77 E F0 .269 (is the socket on which the option is to be applied.)2.77 F F2(Level)5.269 E F0 .799(speci\214es the protocol layer on which the option is to be applied; in m\ ost cases this is the `)72 211.2 R .8(`socket level')-.74 F(',)-.74 E .564 (indicated by the symbolic constant SOL_SOCKET)72 223.2 R 3.064(,d)-.74 G .564 (e\214ned in)288.804 223.2 R F2(.)3.064 E F0 .564 (The actual option is speci-)5.564 F .321(\214ed in)72 235.2 R F2(optname)2.821 E F0 2.821(,a)C .322(nd is a symbolic constant also de\214ned in)144.623 235.2 R F2()2.822 E F0(.)A F2(Optval)5.322 E F0(and)2.822 E F2(Optlen) 2.822 E F0 .322(point to the)2.822 F .279(value of the option \(in most cases,\ whether the option is to be turned on or of)72 247.2 R .278 (f\), and the length of the value)-.18 F .313(of the option, respectively)72 259.2 R 5.313(.F)-.65 G(or)190.092 259.2 Q F2(getsockopt)2.813 E F0(,)A F2 (optlen)2.813 E F0 .314(is a value-result parameter)2.813 F 2.814(,i)-.4 G .314 (nitially set to the size of the)391.566 259.2 R(storage area pointed to by)72 271.2 Q F2(optval)2.5 E F0 2.5(,a)C (nd modi\214ed upon return to indicate the actual amount of storage used.) 212.25 271.2 Q 1.155(An example should help clarify things.)97 286.8 R 1.154 (It is sometimes useful to determine the type \(e.g., stream,)6.155 F 1.102 (datagram, etc.\))72 298.8 R 1.102(of an existing socket; programs under)6.102 F F2(inetd)3.602 E F0 1.102(\(described below\) may need to perform this)3.602 F 2.5(task. This)72 310.8 R (can be accomplished as follows via the SO_TYPE socket option and the)2.5 E F2 (getsockopt)2.5 E F0(call:)2.5 E(#include )108 328.8 Q (#include )108 340.8 Q(int type, size;)108 364.8 Q (size = sizeof \(int\);)108 388.8 Q(if \(getsockopt\(s, SOL_SOCKET)108 412.8 Q 2.5(,S)-.74 G(O_TYPE, \(char *\) &type, &size\) < 0\) {)248.09 412.8 Q(...)133 424.8 Q(})108 436.8 Q .554(After the)72 454.8 R F2(getsockopt)3.054 E F0(call,) 3.054 E F2(type)3.054 E F0 .554 (will be set to the value of the socket type, as de\214ned in)3.054 F F2 ()3.053 E F0 5.553(.I)C(f,)498.17 454.8 Q 4.475 (for example, the socket were a datagram socket,)72 466.8 R F2(type)6.975 E F0 4.475(would have the value corresponding to)6.975 F(SOCK_DGRAM.)72 478.8 Q F1 2.5(5.10. NS)72 502.8 R(Packet Sequences)2.5 E F0 .007(The semantics of NS con\ nections demand that the user both be able to look inside the network header)97 518.4 R .361(associated with any incoming packet and be able to specify what s\ hould go in certain \214elds of an outgoing)72 530.4 R 2.589(packet. Using)72 542.4 R(dif)2.589 E .088(ferent calls to)-.18 F F2(setsockopt)2.588 E F0 2.588 (,i)C 2.588(ti)249.82 542.4 S 2.588(sp)257.968 542.4 S .088 (ossible to indicate whether prototype headers will be asso-)269.446 542.4 R 1.723(ciated by the user with each outgoing packet \(SO_HEADERS_ON_OUTPUT\), t\ o indicate whether the)72 554.4 R .763(headers received by the system should b\ e delivered to the user \(SO_HEADERS_ON_INPUT\), or to indi-)72 566.4 R 4.154(\ cate default information that should be associated with all outgoing packets o\ n a given socket)72 578.4 R(\(SO_DEF)72 590.4 Q(AUL)-.74 E(T_HEADERS\).)-.92 E (The contents of a SPP header \(minus the IDP header\) are:)97 606 Q EP %%Page: 35 35 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-35) -.35 F(struct sphdr {)108 84 Q 5.29(u_char sp_cc;)142.44 96 R (/* connection control */)321.44 96 Q 2.5(#de\214ne SP_SP)108 108 R 121.5 (0x80 /*)7.76 F(system packet */)2.5 E 2.5(#de\214ne SP_SA)108 120 R 121.5 (0x40 /*)6.1 F(send acknowledgement */)2.5 E 2.5(#de\214ne SP_OB)108 132 R 121.5(0x20 /*)4.99 F(attention \(out of band data\) */)2.5 E 2.5 (#de\214ne SP_EM)108 144 R 121.5(0x10 /*)3.88 F(end of message */)2.5 E 5.29 (u_char sp_dt;)142.44 156 R(/* datastream type */)321.44 156 Q 2.5 (u_short sp_sid;)142.44 168 R(/* source connection identi\214er */)321.44 168 Q 2.5(u_short sp_did;)142.44 180 R(/* destination connection identi\214er */) 321.44 180 Q 2.5(u_short sp_seq;)142.44 192 R(/* sequence number */)321.44 192 Q 2.5(u_short sp_ack;)142.44 204 R(/* acknowledge number */)321.44 204 Q 2.5 (u_short sp_alo;)142.44 216 R(/* allocation number */)321.44 216 Q(};)108 228 Q .525(Here, the items of interest are the)72 246 R/F1 10/Times-Italic@0 SF (datastr)3.025 E .525(eam type)-.37 F F0 .525(and the)3.025 F F1 .525 (connection contr)3.025 F(ol)-.37 E F0 3.024(\214elds. The)3.024 F .524 (semantics of the)3.024 F .377(datastream type are de\214ned by the applicatio\ n\(s\) in question; the value of this \214eld is, by default, zero, but)72 258 R .009(it can be used to indicate things such as Xerox')72 270 R 2.509(sB)-.55 G .009(ulk Data T)273.181 270 R .009 (ransfer Protocol \(in which case it is set to one\).)-.35 F .244(The connecti\ on control \214eld is a mask of the \215ags de\214ned just below it.)72 282 R .244(The user may set or clear the end-)5.244 F .752(of-message bit to indicat\ e that a given message is the last of a given substream type, or may set/clear\ the)72 294 R 1.025(attention bit as an alternate way to indicate that a packe\ t should be sent out-of-band.)72 306 R 1.026(As an example, to)6.026 F (associate prototype headers with outgoing SPP packets, consider:)72 318 Q (#include )108 336 Q(#include )108 348 Q (#include )108 360 Q(#include )108 372 Q(...)110.5 384 Q(struct sockaddr_ns sns, to;)108 396 Q(int s, on = 1;)108 408 Q (struct databuf {)108 420 Q(struct sphdr proto_spp;)133 432 Q (/* prototype header */)7.78 E(char buf[534];)133 444 Q (/* max. possible data by Xerox std. */)233 444 Q 2.5(}b)108 456 S(uf;)120.3 456 Q(...)110.5 468 Q 2.5(s=s)108 480 S(ocket\(AF_NS, SOCK_SEQP)126.42 480 Q (ACKET)-.92 E 2.5(,0)-.74 G(\);)284.76 480 Q(...)110.5 492 Q (bind\(s, \(struct sockaddr *\) &sns, sizeof \(sns\)\);)108 504 Q (setsockopt\(s, NSPROT)108 516 Q(O_SPP)-.18 E 2.5(,S)-1.11 G (O_HEADERS_ON_OUTPUT)238.95 516 Q 2.5(,&)-.74 G(on, sizeof\(on\)\);)373.2 516 Q (...)110.5 528 Q(buf.proto_spp.sp_dt = 1;)108 540 Q(/* bulk data */)1.58 E (buf.proto_spp.sp_cc = SP_EM;)108 552 Q(/* end-of-message */)258 552 Q (strcpy\(buf.buf, "hello world\\n"\);)108 564 Q(sendto\(s, \(char *\) &buf, si\ zeof\(struct sphdr\) + strlen\("hello world\\n"\),)108 576 Q (\(struct sockaddr *\) &to, sizeof\(to\)\);)118 588 Q(...)110.5 600 Q 1.103(No\ te that one must be careful when writing headers; if the prototype header is n\ ot written with the data)72 618 R 1.215(with which it is to be associated, the\ kernel will treat the \214rst few bytes of the data as the header)72 630 R 3.716(,w)-.4 G(ith)493.44 630 Q .239(unpredictable results.)72 642 R 1.638 -.7 (To t)5.239 H .238(urn of).7 F 2.738(ft)-.18 G .238 (he above association, and to indicate that packet headers received by the) 212.012 642 R(system should be passed up to the user)72 654 Q 2.5(,o)-.4 G (ne might use:)236.31 654 Q EP %%Page: 36 36 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-36 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(#include )108 84 Q(#include )108 96 Q(#include )108 108 Q(#include )108 120 Q(...)110.5 132 Q(struct sockaddr sns;)108 144 Q(int s, on = 1, of)108 156 Q 2.5(f=0)-.18 G(;) 187.71 156 Q(...)110.5 168 Q 2.5(s=s)108 180 S(ocket\(AF_NS, SOCK_SEQP)126.42 180 Q(ACKET)-.92 E 2.5(,0)-.74 G(\);)284.76 180 Q(...)110.5 192 Q (bind\(s, \(struct sockaddr *\) &sns, sizeof \(sns\)\);)108 204 Q (setsockopt\(s, NSPROT)108 216 Q(O_SPP)-.18 E 2.5(,S)-1.11 G (O_HEADERS_ON_OUTPUT)238.95 216 Q 2.5(,&)-.74 G(of)373.2 216 Q(f, sizeof\(of) -.18 E(f\)\);)-.18 E(setsockopt\(s, NSPROT)108 228 Q(O_SPP)-.18 E 2.5(,S)-1.11 G(O_HEADERS_ON_INPUT)238.95 228 Q 2.5(,&)-.74 G(on, sizeof\(on\)\);)363.2 228 Q (...)110.5 240 Q .65(Output is handled somewhat dif)97 261.6 R .65 (ferently in the IDP world.)-.18 F .65(The header of an IDP-level packet looks) 5.65 F(like:)72 273.6 Q(struct idp {)108 291.6 Q 28.88(u_short idp_sum;)135.22 303.6 R(/* Checksum */)340.6 303.6 Q 28.88(u_short idp_len;)135.22 315.6 R (/* Length, in bytes, including header */)340.6 315.6 Q 31.67(u_char idp_tc;) 135.22 327.6 R(/* T)340.6 327.6 Q(ransport Control \(i.e., hop count\) */)-.35 E 31.67(u_char idp_pt;)135.22 339.6 R(/* Packet T)340.6 339.6 Q (ype \(i.e., level 2 protocol\) */)-.7 E(struct ns_addr)135.22 351.6 Q 106.5 (idp_dna; /*)5 F(Destination Network Address */)2.5 E(struct ns_addr)135.22 363.6 Q 107.61(idp_sna; /*)5 F(Source Network Address */)2.5 E(};)108 375.6 Q .188(The primary \214eld of interest in an IDP header is the)72 393.6 R/F1 10 /Times-Italic@0 SF .187(packet type)2.687 F F0 2.687(\214eld. The)2.687 F .187 (standard values for this \214eld are)2.687 F(\(as de\214ned in <)72 405.6 Q F1 (netns/ns.h)A F0(>\):)A(#de\214ne NSPROT)108 423.6 Q 28.34(O_RI 1)-.18 F (/* Routing Information */)303.16 423.6 Q(#de\214ne NSPROT)108 435.6 Q 11.12 (O_ECHO 2)-.18 F(/* Echo Protocol */)303.16 435.6 Q(#de\214ne NSPROT)108 447.6 Q 5(O_ERROR 3)-.18 F(/* Error Protocol */)303.16 447.6 Q(#de\214ne NSPROT)108 459.6 Q 26.67(O_PE 4)-.18 F(/* Packet Exchange */)303.16 459.6 Q (#de\214ne NSPROT)108 471.6 Q 21.66(O_SPP 5)-.18 F(/* Sequenced Packet */) 303.16 471.6 Q .794(For SPP connections, the contents of this \214eld are auto\ matically set to NSPROT)72 489.6 R .794(O_SPP; for IDP packets,)-.18 F (this value defaults to zero, which means `)72 501.6 Q(`unknown')-.74 E('.)-.74 E(Setting the value of that \214eld with SO_DEF)97 517.2 Q(AUL)-.74 E (T_HEADERS is easy:)-.92 E(#include )108 535.2 Q (#include )108 547.2 Q(#include )108 559.2 Q (#include )108 571.2 Q(...)110.5 583.2 Q(struct sockaddr sns;)108 595.2 Q(struct idp proto_idp;)108 607.2 Q(/* prototype header */)233 607.2 Q (int s, on = 1;)108 619.2 Q(...)110.5 631.2 Q 2.5(s=s)108 643.2 S (ocket\(AF_NS, SOCK_DGRAM, 0\);)126.42 643.2 Q(...)110.5 655.2 Q (bind\(s, \(struct sockaddr *\) &sns, sizeof \(sns\)\);)108 667.2 Q (proto_idp.idp_pt = NSPROT)108 679.2 Q 5.08(O_PE; /*)-.18 F(packet exchange */) 2.5 E(setsockopt\(s, NSPROT)108 691.2 Q(O_IDP)-.18 E 2.5(,S)-1.11 G(O_DEF) 238.38 691.2 Q(AUL)-.74 E(T_HEADERS, \(char *\) &proto_idp,)-.92 E (sizeof\(proto_idp\)\);)118 703.2 Q(...)110.5 715.2 Q EP %%Page: 37 37 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-37) -.35 F 18.7(Using SO_HEADERS_ON_OUTPUT is somewhat more dif)97 84 R 21.2 (\214cult. When)-.18 F .262(SO_HEADERS_ON_OUTPUT is turned on for an IDP socke\ t, the socket becomes \(for all intents and pur)72 96 R(-)-.2 E 1.027 (poses\) a raw socket.)72 108 R 1.027(In this case, all the \214elds of the pr\ ototype header \(except the length and checksum)6.027 F 1.162(\214elds, which \ are computed by the kernel\) must be \214lled in correctly in order for the so\ cket to send and)72 120 R .792(receive data in a sensible manner)72 132 R 5.792 (.T)-.55 G 3.292(ob)221.572 132 S 3.292(em)234.864 132 S .791 (ore speci\214c, the source address must be set to that of the host)250.376 132 R .574(sending the data; the destination address must be set to that of the ho\ st for whom the data is intended; the)72 144 R 1.318(packet type must be set t\ o whatever value is desired; and the hopcount must be set to some reasonable)72 156 R 1.344(value \(almost always zero\).)72 168 R 1.345 (It should also be noted that simply sending data using)6.344 F/F1 10 /Times-Italic@0 SF(write)3.845 E F0 1.345(will not work)3.845 F .415(unless a) 72 180 R F1(connect)2.915 E F0(or)2.915 E F1(sendto)2.915 E F0 .415(call is us\ ed, in spite of the fact that it is the destination address in the prototype) 2.915 F 1.642(header that is used, not the one given in either of those calls.) 72 192 R 1.642(For almost all IDP applications , using)6.642 F(SO_DEF)72 204 Q (AUL)-.74 E(T_HEADERS is easier and more desirable than writing headers.)-.92 E /F2 10/Times-Bold@0 SF(5.1)72 228 Q 2.5(1. Thr)-.55 F(ee-way Handshake)-.18 E F0 1.269(The semantics of SPP connections indicates that a three-way handshake\ , involving changes in the)97 243.6 R 1.787(datastream type, should \212 but i\ s not absolutely required to \212 take place before a SPP connection is)72 255.6 R 3.734(closed. Almost)72 267.6 R 1.233(all SPP connections are `)3.733 F (`well-behaved')-.74 E 3.733('i)-.74 G 3.733(nt)313.282 267.6 S 1.233 (his manner; when communicating with any)324.795 267.6 R .2(process, it is bes\ t to assume that the three-way handshake is required unless it is known for ce\ rtain that it is)72 279.6 R .809(not required.)72 291.6 R .808(In a three-way \ close, the closing process indicates that it wishes to close the connection by) 5.809 F .443(sending a zero-length packet with end-of-message set and with dat\ astream type 254.)72 303.6 R .443(The other side of the)5.443 F 1.171(connecti\ on indicates that it is OK to close by sending a zero-length packet with end-o\ f-message set and)72 315.6 R 1.364(datastream type 255.)72 327.6 R(Finally) 6.364 E 3.864(,t)-.65 G 1.364 (he closing process replies with a zero-length packet with substream type) 200.966 327.6 R .322(255; at this point, the connection is considered closed.) 72 339.6 R .322(The following code fragments are simpli\214ed exam-)5.322 F .196(ples of how one might handle this three-way handshake at the user level; \ in the future, support for this type)72 351.6 R .377(of close will probably be\ provided as part of the C library or as part of the kernel.)72 363.6 R .377 (The \214rst code fragment)5.377 F .203(below illustrates how a process might \ handle three-way handshake if it sees that the process it is communi-)72 375.6 R(cating with wants to close the connection:)72 387.6 Q EP %%Page: 38 38 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-38 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E(#include )108 84 Q(#include )108 96 Q(#include )108 108 Q(#include )108 120 Q(...)110.5 132 Q(#ifndef SPPSST_END)108 144 Q(#de\214ne SPPSST_END 254)108 156 Q (#de\214ne SPPSST_ENDREPL)108 168 Q 2.5(Y2)-1 G(55)237.57 168 Q(#endif)108 180 Q(struct sphdr proto_sp;)108 192 Q(int s;)108 204 Q(...)110.5 216 Q (read\(s, buf, BUFSIZE\);)108 228 Q (if \(\(\(struct sphdr *\)buf\)->sp_dt == SPPSST_END\) {)108 240 Q(/*)133 252 Q 2.5(*S)135.5 264 S(PPSST_END indicates that the other side wants to)148.56 264 Q 2.5(*c)135.5 276 S(lose.)147.44 276 Q(*/)135.5 288 Q (proto_sp.sp_dt = SPPSST_ENDREPL)133 300 Q -.92(Y;)-1 G (proto_sp.sp_cc = SP_EM;)133 312 Q(setsockopt\(s, NSPROT)133 324 Q(O_SPP)-.18 E 2.5(,S)-1.11 G(O_DEF)263.95 324 Q(AUL)-.74 E(T_HEADERS, \(char *\)&proto_sp,) -.92 E(sizeof\(proto_sp\)\);)143 336 Q(write\(s, buf, 0\);)133 348 Q(/*)133 360 Q 2.5(*W)135.5 372 S (rite a zero-length packet with datastream type = SPPSST_ENDREPL)152.04 372 Q (Y)-1 E 2.5(*t)135.5 384 S 2.5(oi)145.78 384 S (ndicate that the close is OK with us.)156.06 384 Q(The packet that we)5 E 2.5 (*d)135.5 396 S(on')148 396 Q 2.5(ts)-.18 G(ee \(because we don')170.32 396 Q 2.5(tl)-.18 G(ook for it\) is another packet)259.55 396 Q 2.5(*f)135.5 408 S (rom the other side of the connection, with SPPSST_ENDREPL)146.33 408 Q(Y)-1 E 2.5(*o)135.5 420 S 2.5(ni)148 420 S 2.5(ti)158.28 420 S(t, too.)166.34 420 Q (Once that packet is sent, the connection is)5 E 2.5(*c)135.5 432 S (onsidered closed; note that we really ought to retransmit)147.44 432 Q 2.5(*t) 135.5 444 S(he close for some time if we do not get a reply)145.78 444 Q(.)-.65 E(*/)135.5 456 Q(close\(s\);)133 468 Q(})108 480 Q(...)110.5 492 Q 1.508 -.7 (To i)72 510 T .108(ndicate to another process that we would like to close the\ connection, the following code would suf).7 F(\214ce:)-.18 E EP %%Page: 39 39 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF(Advanced 4.3BSD IPC T)72 48 Q 264.85(utorial PS1:8-39) -.35 F(#include )108 84 Q(#include )108 96 Q (#include )108 108 Q(#include )108 120 Q(...)110.5 132 Q(#ifndef SPPSST_END)108 144 Q(#de\214ne SPPSST_END 254)108 156 Q (#de\214ne SPPSST_ENDREPL)108 168 Q 2.5(Y2)-1 G(55)237.57 168 Q(#endif)108 180 Q(struct sphdr proto_sp;)108 192 Q(int s;)108 204 Q(...)110.5 216 Q (proto_sp.sp_dt = SPPSST_END;)108 228 Q(proto_sp.sp_cc = SP_EM;)108 240 Q (setsockopt\(s, NSPROT)108 252 Q(O_SPP)-.18 E 2.5(,S)-1.11 G(O_DEF)238.95 252 Q (AUL)-.74 E(T_HEADERS, \(char *\)&proto_sp,)-.92 E(sizeof\(proto_sp\)\);)118 264 Q(write\(s, buf, 0\);)108 276 Q(/* send the end request */)183 276 Q (proto_sp.sp_dt = SPPSST_ENDREPL)108 288 Q -.92(Y;)-1 G(setsockopt\(s, NSPROT) 108 300 Q(O_SPP)-.18 E 2.5(,S)-1.11 G(O_DEF)238.95 300 Q(AUL)-.74 E (T_HEADERS, \(char *\)&proto_sp,)-.92 E(sizeof\(proto_sp\)\);)118 312 Q(/*)108 324 Q 2.5(*W)110.5 336 S 2.5(ea)126.64 336 S(ssume \(perhaps unwisely\))138.02 336 Q 2.5(*t)110.5 348 S(hat the other side will send the)120.78 348 Q 2.5(*E) 110.5 360 S(NDREPL)124.11 360 Q 2.58 -1.29(Y, s)-1 H 2.5(ow)1.29 G (e'll just send our \214nal ENDREPL)191.54 360 Q(Y)-1 E 2.5(*a)110.5 372 S 2.5 (si)122.44 372 S 2.5(fw)131.61 372 S(e'd seen theirs already)144.66 372 Q(.) -.65 E(*/)110.5 384 Q(write\(s, buf, 0\);)108 396 Q(close\(s\);)108 408 Q(...) 110.5 420 Q/F1 10/Times-Bold@0 SF 2.5(5.12. Packet)72 450 R(Exchange)2.5 E F0 1.206(The Xerox standard protocols include a protocol that is both reliable an\ d datagram-oriented.)97 465.6 R(This)6.207 E .251 (protocol is known as Packet Exchange \(PEX or PE\) and, like SPP)72 477.6 R 2.75(,i)-1.11 G 2.75(sl)344.99 477.6 S .25(ayered on top of IDP)354.41 477.6 R 5.25(.P)-1.11 G .25(EX is impor)451.48 477.6 R(-)-.2 E .378(tant for a number \ of things: Courier remote procedure calls may be expedited through the use of \ PEX, and)72 489.6 R .811(many Xerox servers are located by doing a PEX `)72 501.6 R(`BroadcastForServers')-.74 E 3.311('o)-.74 G 3.31(peration. Although) 378.55 501.6 R .81(there is no)3.31 F .166(implementation of PEX in the kernel\ , it may be simulated at the user level with some clever coding and the)72 513.6 R(use of one peculiar)72 525.6 Q/F2 10/Times-Italic@0 SF(getsockopt)2.5 E F0 5(.A)C(PEX packet looks like:)210.3 525.6 Q(/*)108 543.6 Q 2.5(*T)110.5 555.6 S(he packet-exchange header shown here is not de\214ned)124.11 555.6 Q 2.5(*a)110.5 567.6 S 2.5(sp)122.44 567.6 S (art of any of the system include \214les.)133.83 567.6 Q(*/)110.5 579.6 Q (struct pex {)108 591.6 Q(struct idp)135.22 603.6 Q 115.94(p_idp; /*)5 F (idp header */)2.5 E 10(u_short ph_id[2];)135.22 615.6 R (/* unique transaction ID for pex */)321.72 615.6 Q 10(u_short ph_client;) 135.22 627.6 R(/* client type \214eld for pex */)321.72 627.6 Q(};)108 639.6 Q (The)72 657.6 Q F2(ph_id)2.816 E F0 .316(\214eld is used to hold a `)2.816 F .316(`unique id')-.74 F 2.816('t)-.74 G .316 (hat is used in duplicate suppression; the)260.89 657.6 R F2(ph_client)2.815 E F0 .315(\214eld indi-)2.815 F .731(cates the PEX client type \(similar to the \ packet type \214eld in the IDP header\).)72 669.6 R .732 (PEX reliability stems from)5.731 F .17(the fact that it is an idempotent \(`) 72 681.6 R .169(`I send a packet to you, you send a packet to me')-.74 F .169 ('\) protocol.)-.74 F .169(Processes on)5.169 F .188(each side of the connecti\ on may use the unique id to determine if they have seen a given packet before \ \(the)72 693.6 R .543(unique id \214eld dif)72 705.6 R .542(fers on each packe\ t sent\) so that duplicates may be detected, and to indicate which mes-)-.18 F .221(sage a given packet is in response to.)72 717.6 R .221 (If a packet with a given unique id is sent and no response is received)5.221 F 1.569(in a given amount of time, the packet is retransmitted until it is decid\ ed that no response will ever be)72 729.6 R EP %%Page: 40 40 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 264.85(PS1:8-40 Advanced)72 48 R(4.3BSD IPC T)2.5 E (utorial)-.35 E 3.096(received. T)72 84 R 3.096(os)-.7 G .596(imulate PEX, one\ must be able to generate unique ids -- something that is hard to do at the) 131.362 84 R .133 (user level with any real guarantee that the id is really unique.)72 96 R .133 (Therefore, a means \(via)5.133 F/F1 10/Times-Italic@0 SF(getsockopt)2.632 E F0 2.632(\)h)C .132(as been)474.158 96 R .007 (provided for getting unique ids from the kernel.)72 108 R .007 (The following code fragment indicates how to get a unique)5.007 F(id:)72 120 Q (long uniqueid;)108 138 Q(int s, idsize = sizeof\(uniqueid\);)108 150 Q(...) 110.5 162 Q 2.5(s=s)108 174 S(ocket\(AF_NS, SOCK_DGRAM, 0\);)126.42 174 Q(...) 110.5 186 Q(/* get id from the kernel -- only on IDP sockets */)108 198 Q (getsockopt\(s, NSPROT)108 210 Q (O_PE, SO_SEQNO, \(char *\)&uniqueid, &idsize\);)-.18 E(...)110.5 222 Q .373(T\ he retransmission and duplicate suppression code required to simulate PEX full\ y is left as an exercise for)72 240 R(the reader)72 252 Q(.)-.55 E/F2 10 /Times-Bold@0 SF 2.5(5.13. Inetd)72 276 R F0 .934 (One of the daemons provided with 4.3BSD is)97 291.6 R F1(inetd)3.435 E F0 3.435(,t)C .935(he so called `)317.9 291.6 R .935(`internet super)-.74 F (-server)-.2 E(.')-.55 E(')-.74 E F1(Inetd)5.935 E F0(is)3.435 E .299 (invoked at boot time, and determines from the \214le)72 303.6 R F1 (/etc/inetd.conf)2.799 E F0 .299(the servers for which it is to listen.)2.799 F (Once)5.298 E .704 (this information has been read and a pristine environment created,)72 315.6 R F1(inetd)3.204 E F0 .705(proceeds to create one socket for)3.205 F (each service it is to listen for)72 327.6 Q 2.5(,b)-.4 G (inding the appropriate port number to each socket.)196.58 327.6 Q F1(Inetd)97 343.2 Q F0 .084(then performs a)2.584 F F1(select)2.583 E F0 .083 (on all these sockets for read availability)2.583 F 2.583(,w)-.65 G .083 (aiting for somebody wishing a)382.008 343.2 R .008 (connection to the service corresponding to that socket.)72 355.2 R F1(Inetd) 5.008 E F0 .008(then performs an)2.508 F F1(accept)2.508 E F0 .009 (on the socket in ques-)2.508 F(tion,)72 367.2 Q F1(fork)2.754 E F0(s,)A F1 (dup)2.754 E F0 2.754(st)C .254(he new socket to \214le descriptors 0 and 1 \(\ stdin and stdout\), closes other open \214le descrip-)142.492 367.2 R (tors, and)72 379.2 Q F1(exec)2.5 E F0 2.5(st)C(he appropriate server)135.87 379.2 Q(.)-.55 E .055(Servers making use of)97 394.8 R F1(inetd)2.556 E F0 .056 (are considerably simpli\214ed, as)2.556 F F1(inetd)2.556 E F0 .056 (takes care of the majority of the IPC)2.556 F .095 (work required in establishing a connection.)72 406.8 R .095 (The server invoked by)5.095 F F1(inetd)2.595 E F0 .095 (expects the socket connected to its)2.595 F .104(client on \214le descriptors\ 0 and 1, and may immediately perform any operations such as)72 418.8 R F1 -.37 (re)2.604 G(ad).37 E F0(,)A F1(write)2.604 E F0(,)A F1(send)2.604 E F0 2.604 (,o)C(r)500.67 418.8 Q F1 -.37(re)72 430.8 S(cv).37 E F0 6.403(.I)C 1.403 (ndeed, servers may use buf)101.073 430.8 R 1.403 (fered I/O as provided by the `)-.18 F(`stdio')-.74 E 3.903('c)-.74 G 1.403 (onventions, as long as as they)378.106 430.8 R(remember to use)72 442.8 Q F1 (f\215ush)2.5 E F0(when appropriate.)2.5 E .459 (One call which may be of interest to individuals writing servers under)97 458.4 R F1(inetd)2.959 E F0 .459(is the)2.959 F F1(getpeername)2.959 E F0 (call,)2.959 E .626(which returns the address of the peer \(process\) connecte\ d on the other end of the socket.)72 470.4 R .626(For example, to)5.626 F .167 (log the Internet address in `)72 482.4 R .167(`dot notation')-.74 F 2.667('\() -.74 G .167(e.g., `)245.269 482.4 R(`128.32.0.4')-.74 E .167 ('\) of a client connected to a server under)-.74 F F1(inetd)2.668 E F0(,)A (the following code might be used:)72 494.4 Q(struct sockaddr_in name;)108 512.4 Q(int namelen = sizeof \(name\);)108 524.4 Q(...)110.5 536.4 Q (if \(getpeername\(0, \(struct sockaddr *\)&name, &namelen\) < 0\) {)108 548.4 Q(syslog\(LOG_ERR, "getpeername: %m"\);)133 560.4 Q(exit\(1\);)133 572.4 Q 2.5 (}e)108 584.4 S(lse)119.74 584.4 Q (syslog\(LOG_INFO, "Connection from %s", inet_ntoa\(name.sin_addr\)\);)133 596.4 Q(...)110.5 608.4 Q 1.102(While the)72 626.4 R F1(getpeername)3.602 E F0 1.102(call is especially useful when writing programs to run with)3.602 F F1 (inetd)3.602 E F0 3.601(,i)C 3.601(tc)448.767 626.4 S 1.101(an be used)459.588 626.4 R(under other circumstances.)72 638.4 Q(Be warned, however)5 E 2.5(,t)-.4 G(hat)274.32 638.4 Q F1(getpeername)2.5 E F0(will fail on UNIX domain sockets.) 2.5 E EP %%Trailer end %%EOF