socket.texi 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735
  1. @node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top
  2. @c %MENU% A more complicated IPC mechanism, with networking support
  3. @chapter Sockets
  4. This chapter describes the GNU facilities for interprocess
  5. communication using sockets.
  6. @cindex socket
  7. @cindex interprocess communication, with sockets
  8. A @dfn{socket} is a generalized interprocess communication channel.
  9. Like a pipe, a socket is represented as a file descriptor. Unlike pipes
  10. sockets support communication between unrelated processes, and even
  11. between processes running on different machines that communicate over a
  12. network. Sockets are the primary means of communicating with other
  13. machines; @code{telnet}, @code{rlogin}, @code{ftp}, @code{talk} and the
  14. other familiar network programs use sockets.
  15. Not all operating systems support sockets. In @theglibc{}, the
  16. header file @file{sys/socket.h} exists regardless of the operating
  17. system, and the socket functions always exist, but if the system does
  18. not really support sockets these functions always fail.
  19. @strong{Incomplete:} We do not currently document the facilities for
  20. broadcast messages or for configuring Internet interfaces. The
  21. reentrant functions and some newer functions that are related to IPv6
  22. aren't documented either so far.
  23. @menu
  24. * Socket Concepts:: Basic concepts you need to know about.
  25. * Communication Styles::Stream communication, datagrams and other styles.
  26. * Socket Addresses:: How socket names (``addresses'') work.
  27. * Interface Naming:: Identifying specific network interfaces.
  28. * Local Namespace:: Details about the local namespace.
  29. * Internet Namespace:: Details about the Internet namespace.
  30. * Misc Namespaces:: Other namespaces not documented fully here.
  31. * Open/Close Sockets:: Creating sockets and destroying them.
  32. * Connections:: Operations on sockets with connection state.
  33. * Datagrams:: Operations on datagram sockets.
  34. * Inetd:: Inetd is a daemon that starts servers on request.
  35. The most convenient way to write a server
  36. is to make it work with Inetd.
  37. * Socket Options:: Miscellaneous low-level socket options.
  38. * Networks Database:: Accessing the database of network names.
  39. * Other Socket APIs:: Other socket-related functions.
  40. @end menu
  41. @node Socket Concepts
  42. @section Socket Concepts
  43. @cindex communication style (of a socket)
  44. @cindex style of communication (of a socket)
  45. When you create a socket, you must specify the style of communication
  46. you want to use and the type of protocol that should implement it.
  47. The @dfn{communication style} of a socket defines the user-level
  48. semantics of sending and receiving data on the socket. Choosing a
  49. communication style specifies the answers to questions such as these:
  50. @itemize @bullet
  51. @item
  52. @cindex packet
  53. @cindex byte stream
  54. @cindex stream (sockets)
  55. @strong{What are the units of data transmission?} Some communication
  56. styles regard the data as a sequence of bytes with no larger
  57. structure; others group the bytes into records (which are known in
  58. this context as @dfn{packets}).
  59. @item
  60. @cindex loss of data on sockets
  61. @cindex data loss on sockets
  62. @strong{Can data be lost during normal operation?} Some communication
  63. styles guarantee that all the data sent arrives in the order it was
  64. sent (barring system or network crashes); other styles occasionally
  65. lose data as a normal part of operation, and may sometimes deliver
  66. packets more than once or in the wrong order.
  67. Designing a program to use unreliable communication styles usually
  68. involves taking precautions to detect lost or misordered packets and
  69. to retransmit data as needed.
  70. @item
  71. @strong{Is communication entirely with one partner?} Some
  72. communication styles are like a telephone call---you make a
  73. @dfn{connection} with one remote socket and then exchange data
  74. freely. Other styles are like mailing letters---you specify a
  75. destination address for each message you send.
  76. @end itemize
  77. @cindex namespace (of socket)
  78. @cindex domain (of socket)
  79. @cindex socket namespace
  80. @cindex socket domain
  81. You must also choose a @dfn{namespace} for naming the socket. A socket
  82. name (``address'') is meaningful only in the context of a particular
  83. namespace. In fact, even the data type to use for a socket name may
  84. depend on the namespace. Namespaces are also called ``domains'', but we
  85. avoid that word as it can be confused with other usage of the same
  86. term. Each namespace has a symbolic name that starts with @samp{PF_}.
  87. A corresponding symbolic name starting with @samp{AF_} designates the
  88. address format for that namespace.
  89. @cindex network protocol
  90. @cindex protocol (of socket)
  91. @cindex socket protocol
  92. @cindex protocol family
  93. Finally you must choose the @dfn{protocol} to carry out the
  94. communication. The protocol determines what low-level mechanism is used
  95. to transmit and receive data. Each protocol is valid for a particular
  96. namespace and communication style; a namespace is sometimes called a
  97. @dfn{protocol family} because of this, which is why the namespace names
  98. start with @samp{PF_}.
  99. The rules of a protocol apply to the data passing between two programs,
  100. perhaps on different computers; most of these rules are handled by the
  101. operating system and you need not know about them. What you do need to
  102. know about protocols is this:
  103. @itemize @bullet
  104. @item
  105. In order to have communication between two sockets, they must specify
  106. the @emph{same} protocol.
  107. @item
  108. Each protocol is meaningful with particular style/namespace
  109. combinations and cannot be used with inappropriate combinations. For
  110. example, the TCP protocol fits only the byte stream style of
  111. communication and the Internet namespace.
  112. @item
  113. For each combination of style and namespace there is a @dfn{default
  114. protocol}, which you can request by specifying 0 as the protocol
  115. number. And that's what you should normally do---use the default.
  116. @end itemize
  117. Throughout the following description at various places
  118. variables/parameters to denote sizes are required. And here the trouble
  119. starts. In the first implementations the type of these variables was
  120. simply @code{int}. On most machines at that time an @code{int} was 32
  121. bits wide, which created a @emph{de facto} standard requiring 32-bit
  122. variables. This is important since references to variables of this type
  123. are passed to the kernel.
  124. Then the POSIX people came and unified the interface with the words "all
  125. size values are of type @code{size_t}". On 64-bit machines
  126. @code{size_t} is 64 bits wide, so pointers to variables were no longer
  127. possible.
  128. The Unix98 specification provides a solution by introducing a type
  129. @code{socklen_t}. This type is used in all of the cases that POSIX
  130. changed to use @code{size_t}. The only requirement of this type is that
  131. it be an unsigned type of at least 32 bits. Therefore, implementations
  132. which require that references to 32-bit variables be passed can be as
  133. happy as implementations which use 64-bit values.
  134. @node Communication Styles
  135. @section Communication Styles
  136. @Theglibc{} includes support for several different kinds of sockets,
  137. each with different characteristics. This section describes the
  138. supported socket types. The symbolic constants listed here are
  139. defined in @file{sys/socket.h}.
  140. @pindex sys/socket.h
  141. @deftypevr Macro int SOCK_STREAM
  142. @standards{BSD, sys/socket.h}
  143. The @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs}).
  144. It operates over a connection with a particular remote socket and
  145. transmits data reliably as a stream of bytes.
  146. Use of this style is covered in detail in @ref{Connections}.
  147. @end deftypevr
  148. @deftypevr Macro int SOCK_DGRAM
  149. @standards{BSD, sys/socket.h}
  150. The @code{SOCK_DGRAM} style is used for sending
  151. individually-addressed packets unreliably.
  152. It is the diametrical opposite of @code{SOCK_STREAM}.
  153. Each time you write data to a socket of this kind, that data becomes
  154. one packet. Since @code{SOCK_DGRAM} sockets do not have connections,
  155. you must specify the recipient address with each packet.
  156. The only guarantee that the system makes about your requests to
  157. transmit data is that it will try its best to deliver each packet you
  158. send. It may succeed with the sixth packet after failing with the
  159. fourth and fifth packets; the seventh packet may arrive before the
  160. sixth, and may arrive a second time after the sixth.
  161. The typical use for @code{SOCK_DGRAM} is in situations where it is
  162. acceptable to simply re-send a packet if no response is seen in a
  163. reasonable amount of time.
  164. @xref{Datagrams}, for detailed information about how to use datagram
  165. sockets.
  166. @end deftypevr
  167. @ignore
  168. @c This appears to be only for the NS domain, which we aren't
  169. @c discussing and probably won't support either.
  170. @deftypevr Macro int SOCK_SEQPACKET
  171. @standards{BSD, sys/socket.h}
  172. This style is like @code{SOCK_STREAM} except that the data are
  173. structured into packets.
  174. A program that receives data over a @code{SOCK_SEQPACKET} socket
  175. should be prepared to read the entire message packet in a single call
  176. to @code{read}; if it only reads part of the message, the remainder of
  177. the message is simply discarded instead of being available for
  178. subsequent calls to @code{read}.
  179. Many protocols do not support this communication style.
  180. @end deftypevr
  181. @end ignore
  182. @ignore
  183. @deftypevr Macro int SOCK_RDM
  184. @standards{BSD, sys/socket.h}
  185. This style is a reliable version of @code{SOCK_DGRAM}: it sends
  186. individually addressed packets, but guarantees that each packet sent
  187. arrives exactly once.
  188. @strong{Warning:} It is not clear this is actually supported
  189. by any operating system.
  190. @end deftypevr
  191. @end ignore
  192. @deftypevr Macro int SOCK_RAW
  193. @standards{BSD, sys/socket.h}
  194. This style provides access to low-level network protocols and
  195. interfaces. Ordinary user programs usually have no need to use this
  196. style.
  197. @end deftypevr
  198. @node Socket Addresses
  199. @section Socket Addresses
  200. @cindex address of socket
  201. @cindex name of socket
  202. @cindex binding a socket address
  203. @cindex socket address (name) binding
  204. The name of a socket is normally called an @dfn{address}. The
  205. functions and symbols for dealing with socket addresses were named
  206. inconsistently, sometimes using the term ``name'' and sometimes using
  207. ``address''. You can regard these terms as synonymous where sockets
  208. are concerned.
  209. A socket newly created with the @code{socket} function has no
  210. address. Other processes can find it for communication only if you
  211. give it an address. We call this @dfn{binding} the address to the
  212. socket, and the way to do it is with the @code{bind} function.
  213. You need only be concerned with the address of a socket if other processes
  214. are to find it and start communicating with it. You can specify an
  215. address for other sockets, but this is usually pointless; the first time
  216. you send data from a socket, or use it to initiate a connection, the
  217. system assigns an address automatically if you have not specified one.
  218. Occasionally a client needs to specify an address because the server
  219. discriminates based on address; for example, the rsh and rlogin
  220. protocols look at the client's socket address and only bypass passphrase
  221. checking if it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).
  222. The details of socket addresses vary depending on what namespace you are
  223. using. @xref{Local Namespace}, or @ref{Internet Namespace}, for specific
  224. information.
  225. Regardless of the namespace, you use the same functions @code{bind} and
  226. @code{getsockname} to set and examine a socket's address. These
  227. functions use a phony data type, @code{struct sockaddr *}, to accept the
  228. address. In practice, the address lives in a structure of some other
  229. data type appropriate to the address format you are using, but you cast
  230. its address to @code{struct sockaddr *} when you pass it to
  231. @code{bind}.
  232. @menu
  233. * Address Formats:: About @code{struct sockaddr}.
  234. * Setting Address:: Binding an address to a socket.
  235. * Reading Address:: Reading the address of a socket.
  236. @end menu
  237. @node Address Formats
  238. @subsection Address Formats
  239. The functions @code{bind} and @code{getsockname} use the generic data
  240. type @code{struct sockaddr *} to represent a pointer to a socket
  241. address. You can't use this data type effectively to interpret an
  242. address or construct one; for that, you must use the proper data type
  243. for the socket's namespace.
  244. Thus, the usual practice is to construct an address of the proper
  245. namespace-specific type, then cast a pointer to @code{struct sockaddr *}
  246. when you call @code{bind} or @code{getsockname}.
  247. The one piece of information that you can get from the @code{struct
  248. sockaddr} data type is the @dfn{address format designator}. This tells
  249. you which data type to use to understand the address fully.
  250. @pindex sys/socket.h
  251. The symbols in this section are defined in the header file
  252. @file{sys/socket.h}.
  253. @deftp {Data Type} {struct sockaddr}
  254. @standards{BSD, sys/socket.h}
  255. The @code{struct sockaddr} type itself has the following members:
  256. @table @code
  257. @item short int sa_family
  258. This is the code for the address format of this address. It
  259. identifies the format of the data which follows.
  260. @item char sa_data[14]
  261. This is the actual socket address data, which is format-dependent. Its
  262. length also depends on the format, and may well be more than 14. The
  263. length 14 of @code{sa_data} is essentially arbitrary.
  264. @end table
  265. @end deftp
  266. Each address format has a symbolic name which starts with @samp{AF_}.
  267. Each of them corresponds to a @samp{PF_} symbol which designates the
  268. corresponding namespace. Here is a list of address format names:
  269. @vtable @code
  270. @item AF_LOCAL
  271. @standards{POSIX, sys/socket.h}
  272. This designates the address format that goes with the local namespace.
  273. (@code{PF_LOCAL} is the name of that namespace.) @xref{Local Namespace
  274. Details}, for information about this address format.
  275. @item AF_UNIX
  276. @standards{BSD, sys/socket.h}
  277. @standards{Unix98, sys/socket.h}
  278. This is a synonym for @code{AF_LOCAL}. Although @code{AF_LOCAL} is
  279. mandated by POSIX.1g, @code{AF_UNIX} is portable to more systems.
  280. @code{AF_UNIX} was the traditional name stemming from BSD, so even most
  281. POSIX systems support it. It is also the name of choice in the Unix98
  282. specification. (The same is true for @code{PF_UNIX}
  283. vs. @code{PF_LOCAL}).
  284. @item AF_FILE
  285. @standards{GNU, sys/socket.h}
  286. This is another synonym for @code{AF_LOCAL}, for compatibility.
  287. (@code{PF_FILE} is likewise a synonym for @code{PF_LOCAL}.)
  288. @item AF_INET
  289. @standards{BSD, sys/socket.h}
  290. This designates the address format that goes with the Internet
  291. namespace. (@code{PF_INET} is the name of that namespace.)
  292. @xref{Internet Address Formats}.
  293. @item AF_INET6
  294. @standards{IPv6 Basic API, sys/socket.h}
  295. This is similar to @code{AF_INET}, but refers to the IPv6 protocol.
  296. (@code{PF_INET6} is the name of the corresponding namespace.)
  297. @item AF_UNSPEC
  298. @standards{BSD, sys/socket.h}
  299. This designates no particular address format. It is used only in rare
  300. cases, such as to clear out the default destination address of a
  301. ``connected'' datagram socket. @xref{Sending Datagrams}.
  302. The corresponding namespace designator symbol @code{PF_UNSPEC} exists
  303. for completeness, but there is no reason to use it in a program.
  304. @end vtable
  305. @file{sys/socket.h} defines symbols starting with @samp{AF_} for many
  306. different kinds of networks, most or all of which are not actually
  307. implemented. We will document those that really work as we receive
  308. information about how to use them.
  309. @node Setting Address
  310. @subsection Setting the Address of a Socket
  311. @pindex sys/socket.h
  312. Use the @code{bind} function to assign an address to a socket. The
  313. prototype for @code{bind} is in the header file @file{sys/socket.h}.
  314. For examples of use, see @ref{Local Socket Example}, or see @ref{Inet Example}.
  315. @deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
  316. @standards{BSD, sys/socket.h}
  317. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  318. @c Direct syscall, except on Hurd.
  319. The @code{bind} function assigns an address to the socket
  320. @var{socket}. The @var{addr} and @var{length} arguments specify the
  321. address; the detailed format of the address depends on the namespace.
  322. The first part of the address is always the format designator, which
  323. specifies a namespace, and says that the address is in the format of
  324. that namespace.
  325. The return value is @code{0} on success and @code{-1} on failure. The
  326. following @code{errno} error conditions are defined for this function:
  327. @table @code
  328. @item EBADF
  329. The @var{socket} argument is not a valid file descriptor.
  330. @item ENOTSOCK
  331. The descriptor @var{socket} is not a socket.
  332. @item EADDRNOTAVAIL
  333. The specified address is not available on this machine.
  334. @item EADDRINUSE
  335. Some other socket is already using the specified address.
  336. @item EINVAL
  337. The socket @var{socket} already has an address.
  338. @item EACCES
  339. You do not have permission to access the requested address. (In the
  340. Internet domain, only the super-user is allowed to specify a port number
  341. in the range 0 through @code{IPPORT_RESERVED} minus one; see
  342. @ref{Ports}.)
  343. @end table
  344. Additional conditions may be possible depending on the particular namespace
  345. of the socket.
  346. @end deftypefun
  347. @node Reading Address
  348. @subsection Reading the Address of a Socket
  349. @pindex sys/socket.h
  350. Use the function @code{getsockname} to examine the address of an
  351. Internet socket. The prototype for this function is in the header file
  352. @file{sys/socket.h}.
  353. @deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
  354. @standards{BSD, sys/socket.h}
  355. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsmem{/hurd}}}
  356. @c Direct syscall, except on Hurd, where it seems like it might leak
  357. @c VM if cancelled.
  358. The @code{getsockname} function returns information about the
  359. address of the socket @var{socket} in the locations specified by the
  360. @var{addr} and @var{length-ptr} arguments. Note that the
  361. @var{length-ptr} is a pointer; you should initialize it to be the
  362. allocation size of @var{addr}, and on return it contains the actual
  363. size of the address data.
  364. The format of the address data depends on the socket namespace. The
  365. length of the information is usually fixed for a given namespace, so
  366. normally you can know exactly how much space is needed and can provide
  367. that much. The usual practice is to allocate a place for the value
  368. using the proper data type for the socket's namespace, then cast its
  369. address to @code{struct sockaddr *} to pass it to @code{getsockname}.
  370. The return value is @code{0} on success and @code{-1} on error. The
  371. following @code{errno} error conditions are defined for this function:
  372. @table @code
  373. @item EBADF
  374. The @var{socket} argument is not a valid file descriptor.
  375. @item ENOTSOCK
  376. The descriptor @var{socket} is not a socket.
  377. @item ENOBUFS
  378. There are not enough internal buffers available for the operation.
  379. @end table
  380. @end deftypefun
  381. You can't read the address of a socket in the file namespace. This is
  382. consistent with the rest of the system; in general, there's no way to
  383. find a file's name from a descriptor for that file.
  384. @node Interface Naming
  385. @section Interface Naming
  386. Each network interface has a name. This usually consists of a few
  387. letters that relate to the type of interface, which may be followed by a
  388. number if there is more than one interface of that type. Examples
  389. might be @code{lo} (the loopback interface) and @code{eth0} (the first
  390. Ethernet interface).
  391. Although such names are convenient for humans, it would be clumsy to
  392. have to use them whenever a program needs to refer to an interface. In
  393. such situations an interface is referred to by its @dfn{index}, which is
  394. an arbitrarily-assigned small positive integer.
  395. The following functions, constants and data types are declared in the
  396. header file @file{net/if.h}.
  397. @deftypevr Constant size_t IFNAMSIZ
  398. @standards{???, net/if.h}
  399. This constant defines the maximum buffer size needed to hold an
  400. interface name, including its terminating zero byte.
  401. @end deftypevr
  402. @deftypefun {unsigned int} if_nametoindex (const char *@var{ifname})
  403. @standards{IPv6 basic API, net/if.h}
  404. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  405. @c It opens a socket to use ioctl on the fd to get the index.
  406. @c opensock may call socket and access multiple times until it finds a
  407. @c socket family that works. The Linux implementation has a potential
  408. @c concurrency issue WRT last_type and last_family not being updated
  409. @c atomically, but it is harmless; the generic implementation, OTOH,
  410. @c takes a lock, which makes all callers AS- and AC-Unsafe.
  411. @c opensock @asulock @aculock @acsfd
  412. This function yields the interface index corresponding to a particular
  413. name specified with @var{ifname}.
  414. The return value is the interface index on success. On failure, the
  415. function's return value is zero and @code{errno} is set accordingly.
  416. The following @code{errno} values are specific to this function:
  417. @table @code
  418. @item ENODEV
  419. There is no interface by the name requested.
  420. @end table
  421. Additionally, since @code{if_nametoindex} invokes @code{socket}
  422. internally, @code{errno} may also be set to a value listed for the
  423. @code{socket} function (@pxref{Creating a Socket}).
  424. @end deftypefun
  425. @deftypefun {char *} if_indextoname (unsigned int @var{ifindex}, char *@var{ifname})
  426. @standards{IPv6 basic API, net/if.h}
  427. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  428. @c It opens a socket with opensock to use ioctl on the fd to get the
  429. @c name from the index.
  430. This function maps an interface index @var{ifindex} to its corresponding
  431. name. The returned name is placed in the buffer pointed to by @var{ifname},
  432. which must be at least @code{IFNAMSIZ} bytes in length.
  433. The return value is @var{ifname} on success. On failure, the function's
  434. return value is a null pointer and @code{errno} is set accordingly. The
  435. following @code{errno} values are specific to this function:
  436. @table @code
  437. @item ENXIO
  438. There is no interface at the index requested.
  439. @end table
  440. Additionally, since @code{if_indextoname} invokes @code{socket}
  441. internally, @code{errno} may also be set to a value listed for the
  442. @code{socket} function (@pxref{Creating a Socket}).
  443. @end deftypefun
  444. @deftp {Data Type} {struct if_nameindex}
  445. @standards{IPv6 basic API, net/if.h}
  446. This data type is used to hold the information about a single
  447. interface. It has the following members:
  448. @table @code
  449. @item unsigned int if_index;
  450. This is the interface index.
  451. @item char *if_name
  452. This is the null-terminated index name.
  453. @end table
  454. @end deftp
  455. @deftypefun {struct if_nameindex *} if_nameindex (void)
  456. @standards{IPv6 basic API, net/if.h}
  457. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{/hurd}}@acunsafe{@aculock{/hurd} @acsfd{} @acsmem{}}}
  458. @c if_nameindex @ascuheap @asulock/hurd @aculock/hurd @acsfd @acsmem
  459. @c [linux]
  460. @c netlink_open @acsfd @acsmem/hurd
  461. @c socket dup @acsfd
  462. @c memset dup ok
  463. @c bind dup ok
  464. @c netlink_close dup @acsfd
  465. @c getsockname dup @acsmem/hurd
  466. @c netlink_request @ascuheap @acsmem
  467. @c getpagesize dup ok
  468. @c malloc dup @ascuheap @acsmem
  469. @c netlink_sendreq ok
  470. @c memset dup ok
  471. @c sendto dup ok
  472. @c recvmsg dup ok
  473. @c memcpy dup ok
  474. @c free dup @ascuheap @acsmem
  475. @c netlink_free_handle @ascuheap @acsmem
  476. @c free dup @ascuheap @acsmem
  477. @c netlink_close @acsfd
  478. @c close dup @acsfd
  479. @c malloc dup @asuheap @acsmem
  480. @c strndup @ascuheap @acsmem
  481. @c if_freenameindex @ascuheap @acsmem
  482. @c [hurd]
  483. @c opensock dup @asulock @aculock @acsfd
  484. @c hurd_socket_server ok
  485. @c pfinet_siocgifconf ok
  486. @c malloc @ascuheap @acsmem
  487. @c strdup @ascuheap @acsmem
  488. @c ioctl dup ok
  489. @c free @ascuheap @acsmem
  490. This function returns an array of @code{if_nameindex} structures, one
  491. for every interface that is present. The end of the list is indicated
  492. by a structure with an interface of 0 and a null name pointer. If an
  493. error occurs, this function returns a null pointer.
  494. The returned structure must be freed with @code{if_freenameindex} after
  495. use.
  496. @end deftypefun
  497. @deftypefun void if_freenameindex (struct if_nameindex *@var{ptr})
  498. @standards{IPv6 basic API, net/if.h}
  499. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  500. @c if_freenameindex @ascuheap @acsmem
  501. @c free dup @ascuheap @acsmem
  502. This function frees the structure returned by an earlier call to
  503. @code{if_nameindex}.
  504. @end deftypefun
  505. @node Local Namespace
  506. @section The Local Namespace
  507. @cindex local namespace, for sockets
  508. This section describes the details of the local namespace, whose
  509. symbolic name (required when you create a socket) is @code{PF_LOCAL}.
  510. The local namespace is also known as ``Unix domain sockets''. Another
  511. name is file namespace since socket addresses are normally implemented
  512. as file names.
  513. @menu
  514. * Concepts: Local Namespace Concepts. What you need to understand.
  515. * Details: Local Namespace Details. Address format, symbolic names, etc.
  516. * Example: Local Socket Example. Example of creating a socket.
  517. @end menu
  518. @node Local Namespace Concepts
  519. @subsection Local Namespace Concepts
  520. In the local namespace socket addresses are file names. You can specify
  521. any file name you want as the address of the socket, but you must have
  522. write permission on the directory containing it.
  523. @c XXX The following was said to be wrong.
  524. @c In order to connect to a socket you must have read permission for it.
  525. It's common to put these files in the @file{/tmp} directory.
  526. One peculiarity of the local namespace is that the name is only used
  527. when opening the connection; once open the address is not meaningful and
  528. may not exist.
  529. Another peculiarity is that you cannot connect to such a socket from
  530. another machine--not even if the other machine shares the file system
  531. which contains the name of the socket. You can see the socket in a
  532. directory listing, but connecting to it never succeeds. Some programs
  533. take advantage of this, such as by asking the client to send its own
  534. process ID, and using the process IDs to distinguish between clients.
  535. However, we recommend you not use this method in protocols you design,
  536. as we might someday permit connections from other machines that mount
  537. the same file systems. Instead, send each new client an identifying
  538. number if you want it to have one.
  539. After you close a socket in the local namespace, you should delete the
  540. file name from the file system. Use @code{unlink} or @code{remove} to
  541. do this; see @ref{Deleting Files}.
  542. The local namespace supports just one protocol for any communication
  543. style; it is protocol number @code{0}.
  544. @node Local Namespace Details
  545. @subsection Details of Local Namespace
  546. @pindex sys/socket.h
  547. To create a socket in the local namespace, use the constant
  548. @code{PF_LOCAL} as the @var{namespace} argument to @code{socket} or
  549. @code{socketpair}. This constant is defined in @file{sys/socket.h}.
  550. @deftypevr Macro int PF_LOCAL
  551. @standards{POSIX, sys/socket.h}
  552. This designates the local namespace, in which socket addresses are local
  553. names, and its associated family of protocols. @code{PF_LOCAL} is the
  554. macro used by POSIX.1g.
  555. @end deftypevr
  556. @deftypevr Macro int PF_UNIX
  557. @standards{BSD, sys/socket.h}
  558. This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
  559. @end deftypevr
  560. @deftypevr Macro int PF_FILE
  561. @standards{GNU, sys/socket.h}
  562. This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
  563. @end deftypevr
  564. The structure for specifying socket names in the local namespace is
  565. defined in the header file @file{sys/un.h}:
  566. @pindex sys/un.h
  567. @deftp {Data Type} {struct sockaddr_un}
  568. @standards{BSD, sys/un.h}
  569. This structure is used to specify local namespace socket addresses. It has
  570. the following members:
  571. @table @code
  572. @item short int sun_family
  573. This identifies the address family or format of the socket address.
  574. You should store the value @code{AF_LOCAL} to designate the local
  575. namespace. @xref{Socket Addresses}.
  576. @item char sun_path[108]
  577. This is the file name to use.
  578. @strong{Incomplete:} Why is 108 a magic number? RMS suggests making
  579. this a zero-length array and tweaking the following example to use
  580. @code{alloca} to allocate an appropriate amount of storage based on
  581. the length of the filename.
  582. @end table
  583. @end deftp
  584. You should compute the @var{length} parameter for a socket address in
  585. the local namespace as the sum of the size of the @code{sun_family}
  586. component and the string length (@emph{not} the allocation size!) of
  587. the file name string. This can be done using the macro @code{SUN_LEN}:
  588. @deftypefn {Macro} int SUN_LEN (@emph{struct sockaddr_un *} @var{ptr})
  589. @standards{BSD, sys/un.h}
  590. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  591. This macro computes the length of the socket address in the local namespace.
  592. @end deftypefn
  593. @node Local Socket Example
  594. @subsection Example of Local-Namespace Sockets
  595. Here is an example showing how to create and name a socket in the local
  596. namespace.
  597. @smallexample
  598. @include mkfsock.c.texi
  599. @end smallexample
  600. @node Internet Namespace
  601. @section The Internet Namespace
  602. @cindex Internet namespace, for sockets
  603. This section describes the details of the protocols and socket naming
  604. conventions used in the Internet namespace.
  605. Originally the Internet namespace used only IP version 4 (IPv4). With
  606. the growing number of hosts on the Internet, a new protocol with a
  607. larger address space was necessary: IP version 6 (IPv6). IPv6
  608. introduces 128-bit addresses (IPv4 has 32-bit addresses) and other
  609. features, and will eventually replace IPv4.
  610. To create a socket in the IPv4 Internet namespace, use the symbolic name
  611. @code{PF_INET} of this namespace as the @var{namespace} argument to
  612. @code{socket} or @code{socketpair}. For IPv6 addresses you need the
  613. macro @code{PF_INET6}. These macros are defined in @file{sys/socket.h}.
  614. @pindex sys/socket.h
  615. @deftypevr Macro int PF_INET
  616. @standards{BSD, sys/socket.h}
  617. This designates the IPv4 Internet namespace and associated family of
  618. protocols.
  619. @end deftypevr
  620. @deftypevr Macro int PF_INET6
  621. @standards{X/Open, sys/socket.h}
  622. This designates the IPv6 Internet namespace and associated family of
  623. protocols.
  624. @end deftypevr
  625. A socket address for the Internet namespace includes the following components:
  626. @itemize @bullet
  627. @item
  628. The address of the machine you want to connect to. Internet addresses
  629. can be specified in several ways; these are discussed in @ref{Internet
  630. Address Formats}, @ref{Host Addresses} and @ref{Host Names}.
  631. @item
  632. A port number for that machine. @xref{Ports}.
  633. @end itemize
  634. You must ensure that the address and port number are represented in a
  635. canonical format called @dfn{network byte order}. @xref{Byte Order},
  636. for information about this.
  637. @menu
  638. * Internet Address Formats:: How socket addresses are specified in the
  639. Internet namespace.
  640. * Host Addresses:: All about host addresses of Internet host.
  641. * Ports:: Internet port numbers.
  642. * Services Database:: Ports may have symbolic names.
  643. * Byte Order:: Different hosts may use different byte
  644. ordering conventions; you need to
  645. canonicalize host address and port number.
  646. * Protocols Database:: Referring to protocols by name.
  647. * Inet Example:: Putting it all together.
  648. @end menu
  649. @node Internet Address Formats
  650. @subsection Internet Socket Address Formats
  651. In the Internet namespace, for both IPv4 (@code{AF_INET}) and IPv6
  652. (@code{AF_INET6}), a socket address consists of a host address
  653. and a port on that host. In addition, the protocol you choose serves
  654. effectively as a part of the address because local port numbers are
  655. meaningful only within a particular protocol.
  656. The data types for representing socket addresses in the Internet namespace
  657. are defined in the header file @file{netinet/in.h}.
  658. @pindex netinet/in.h
  659. @deftp {Data Type} {struct sockaddr_in}
  660. @standards{BSD, netinet/in.h}
  661. This is the data type used to represent socket addresses in the
  662. Internet namespace. It has the following members:
  663. @table @code
  664. @item sa_family_t sin_family
  665. This identifies the address family or format of the socket address.
  666. You should store the value @code{AF_INET} in this member. The address
  667. family is stored in host byte order. @xref{Socket Addresses}.
  668. @item struct in_addr sin_addr
  669. This is the IPv4 address. @xref{Host Addresses}, and @ref{Host
  670. Names}, for how to get a value to store here. The IPv4 address is
  671. stored in network byte order.
  672. @item unsigned short int sin_port
  673. This is the port number. @xref{Ports}. The port number is stored in
  674. network byte order.
  675. @end table
  676. @end deftp
  677. When you call @code{bind} or @code{getsockname}, you should specify
  678. @code{sizeof (struct sockaddr_in)} as the @var{length} parameter if
  679. you are using an IPv4 Internet namespace socket address.
  680. @deftp {Data Type} {struct sockaddr_in6}
  681. This is the data type used to represent socket addresses in the IPv6
  682. namespace. It has the following members:
  683. @table @code
  684. @item sa_family_t sin6_family
  685. This identifies the address family or format of the socket address.
  686. You should store the value of @code{AF_INET6} in this member.
  687. @xref{Socket Addresses}. The address family is stored in host byte
  688. order.
  689. @item struct in6_addr sin6_addr
  690. This is the IPv6 address of the host machine. @xref{Host
  691. Addresses}, and @ref{Host Names}, for how to get a value to store
  692. here. The address is stored in network byte order.
  693. @item uint32_t sin6_flowinfo
  694. @cindex flow label
  695. @cindex IPv6 flow label
  696. @cindex traffic class
  697. @cindex IPv6 traffic class
  698. This combines the IPv6 traffic class and flow label values, as found
  699. in the IPv6 header. This field is stored in network byte order. Only
  700. the 28 lower bits (of the number in network byte order) are used; the
  701. remaining bits must be zero. The lower 20 bits are the flow label, and
  702. bits 20 to 27 are the the traffic class. Typically, this field is
  703. zero.
  704. @item uint32_t sin6_scope_id
  705. @cindex scope ID
  706. @cindex IPv6 scope ID
  707. For link-local addresses, this identifies the interface on which this
  708. address is valid. The scope ID is stored in host byte order.
  709. Typically, this field is zero.
  710. @item uint16_t sin6_port
  711. This is the port number. @xref{Ports}. The port number is stored in
  712. network byte order.
  713. @end table
  714. @end deftp
  715. @node Host Addresses
  716. @subsection Host Addresses
  717. Each computer on the Internet has one or more @dfn{Internet addresses},
  718. numbers which identify that computer among all those on the Internet.
  719. Users typically write IPv4 numeric host addresses as sequences of four
  720. numbers, separated by periods, as in @samp{128.52.46.32}, and IPv6
  721. numeric host addresses as sequences of up to eight numbers separated by
  722. colons, as in @samp{5f03:1200:836f:c100::1}.
  723. Each computer also has one or more @dfn{host names}, which are strings
  724. of words separated by periods, as in @samp{www.gnu.org}.
  725. Programs that let the user specify a host typically accept both numeric
  726. addresses and host names. To open a connection a program needs a
  727. numeric address, and so must convert a host name to the numeric address
  728. it stands for.
  729. @menu
  730. * Abstract Host Addresses:: What a host number consists of.
  731. * Data type: Host Address Data Type. Data type for a host number.
  732. * Functions: Host Address Functions. Functions to operate on them.
  733. * Names: Host Names. Translating host names to host numbers.
  734. @end menu
  735. @node Abstract Host Addresses
  736. @subsubsection Internet Host Addresses
  737. @cindex host address, Internet
  738. @cindex Internet host address
  739. @ifinfo
  740. Each computer on the Internet has one or more Internet addresses,
  741. numbers which identify that computer among all those on the Internet.
  742. @end ifinfo
  743. @cindex network number
  744. @cindex local network address number
  745. An IPv4 Internet host address is a number containing four bytes of data.
  746. Historically these are divided into two parts, a @dfn{network number} and a
  747. @dfn{local network address number} within that network. In the
  748. mid-1990s classless addresses were introduced which changed this
  749. behavior. Since some functions implicitly expect the old definitions,
  750. we first describe the class-based network and will then describe
  751. classless addresses. IPv6 uses only classless addresses and therefore
  752. the following paragraphs don't apply.
  753. The class-based IPv4 network number consists of the first one, two or
  754. three bytes; the rest of the bytes are the local address.
  755. IPv4 network numbers are registered with the Network Information Center
  756. (NIC), and are divided into three classes---A, B and C. The local
  757. network address numbers of individual machines are registered with the
  758. administrator of the particular network.
  759. Class A networks have single-byte numbers in the range 0 to 127. There
  760. are only a small number of Class A networks, but they can each support a
  761. very large number of hosts. Medium-sized Class B networks have two-byte
  762. network numbers, with the first byte in the range 128 to 191. Class C
  763. networks are the smallest; they have three-byte network numbers, with
  764. the first byte in the range 192-255. Thus, the first 1, 2, or 3 bytes
  765. of an Internet address specify a network. The remaining bytes of the
  766. Internet address specify the address within that network.
  767. The Class A network 0 is reserved for broadcast to all networks. In
  768. addition, the host number 0 within each network is reserved for broadcast
  769. to all hosts in that network. These uses are obsolete now but for
  770. compatibility reasons you shouldn't use network 0 and host number 0.
  771. The Class A network 127 is reserved for loopback; you can always use
  772. the Internet address @samp{127.0.0.1} to refer to the host machine.
  773. Since a single machine can be a member of multiple networks, it can
  774. have multiple Internet host addresses. However, there is never
  775. supposed to be more than one machine with the same host address.
  776. @c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.
  777. @c No, it shouldn't since they're obsolete.
  778. @cindex standard dot notation, for Internet addresses
  779. @cindex dot notation, for Internet addresses
  780. There are four forms of the @dfn{standard numbers-and-dots notation}
  781. for Internet addresses:
  782. @table @code
  783. @item @var{a}.@var{b}.@var{c}.@var{d}
  784. This specifies all four bytes of the address individually and is the
  785. commonly used representation.
  786. @item @var{a}.@var{b}.@var{c}
  787. The last part of the address, @var{c}, is interpreted as a 2-byte quantity.
  788. This is useful for specifying host addresses in a Class B network with
  789. network address number @code{@var{a}.@var{b}}.
  790. @item @var{a}.@var{b}
  791. The last part of the address, @var{b}, is interpreted as a 3-byte quantity.
  792. This is useful for specifying host addresses in a Class A network with
  793. network address number @var{a}.
  794. @item @var{a}
  795. If only one part is given, this corresponds directly to the host address
  796. number.
  797. @end table
  798. Within each part of the address, the usual C conventions for specifying
  799. the radix apply. In other words, a leading @samp{0x} or @samp{0X} implies
  800. hexadecimal radix; a leading @samp{0} implies octal; and otherwise decimal
  801. radix is assumed.
  802. @subsubheading Classless Addresses
  803. IPv4 addresses (and IPv6 addresses also) are now considered classless;
  804. the distinction between classes A, B and C can be ignored. Instead an
  805. IPv4 host address consists of a 32-bit address and a 32-bit mask. The
  806. mask contains set bits for the network part and cleared bits for the
  807. host part. The network part is contiguous from the left, with the
  808. remaining bits representing the host. As a consequence, the netmask can
  809. simply be specified as the number of set bits. Classes A, B and C are
  810. just special cases of this general rule. For example, class A addresses
  811. have a netmask of @samp{255.0.0.0} or a prefix length of 8.
  812. Classless IPv4 network addresses are written in numbers-and-dots
  813. notation with the prefix length appended and a slash as separator. For
  814. example the class A network 10 is written as @samp{10.0.0.0/8}.
  815. @subsubheading IPv6 Addresses
  816. IPv6 addresses contain 128 bits (IPv4 has 32 bits) of data. A host
  817. address is usually written as eight 16-bit hexadecimal numbers that are
  818. separated by colons. Two colons are used to abbreviate strings of
  819. consecutive zeros. For example, the IPv6 loopback address
  820. @samp{0:0:0:0:0:0:0:1} can just be written as @samp{::1}.
  821. @node Host Address Data Type
  822. @subsubsection Host Address Data Type
  823. IPv4 Internet host addresses are represented in some contexts as integers
  824. (type @code{uint32_t}). In other contexts, the integer is
  825. packaged inside a structure of type @code{struct in_addr}. It would
  826. be better if the usage were made consistent, but it is not hard to extract
  827. the integer from the structure or put the integer into a structure.
  828. You will find older code that uses @code{unsigned long int} for
  829. IPv4 Internet host addresses instead of @code{uint32_t} or @code{struct
  830. in_addr}. Historically @code{unsigned long int} was a 32-bit number but
  831. with 64-bit machines this has changed. Using @code{unsigned long int}
  832. might break the code if it is used on machines where this type doesn't
  833. have 32 bits. @code{uint32_t} is specified by Unix98 and guaranteed to have
  834. 32 bits.
  835. IPv6 Internet host addresses have 128 bits and are packaged inside a
  836. structure of type @code{struct in6_addr}.
  837. The following basic definitions for Internet addresses are declared in
  838. the header file @file{netinet/in.h}:
  839. @pindex netinet/in.h
  840. @deftp {Data Type} {struct in_addr}
  841. @standards{BSD, netinet/in.h}
  842. This data type is used in certain contexts to contain an IPv4 Internet
  843. host address. It has just one field, named @code{s_addr}, which records
  844. the host address number as an @code{uint32_t}.
  845. @end deftp
  846. @deftypevr Macro {uint32_t} INADDR_LOOPBACK
  847. @standards{BSD, netinet/in.h}
  848. You can use this constant to stand for ``the address of this machine,''
  849. instead of finding its actual address. It is the IPv4 Internet address
  850. @samp{127.0.0.1}, which is usually called @samp{localhost}. This
  851. special constant saves you the trouble of looking up the address of your
  852. own machine. Also, the system usually implements @code{INADDR_LOOPBACK}
  853. specially, avoiding any network traffic for the case of one machine
  854. talking to itself.
  855. @end deftypevr
  856. @deftypevr Macro {uint32_t} INADDR_ANY
  857. @standards{BSD, netinet/in.h}
  858. You can use this constant to stand for ``any incoming address'' when
  859. binding to an address. @xref{Setting Address}. This is the usual
  860. address to give in the @code{sin_addr} member of @w{@code{struct
  861. sockaddr_in}} when you want to accept Internet connections.
  862. @end deftypevr
  863. @deftypevr Macro {uint32_t} INADDR_BROADCAST
  864. @standards{BSD, netinet/in.h}
  865. This constant is the address you use to send a broadcast message.
  866. @c !!! broadcast needs further documented
  867. @end deftypevr
  868. @deftypevr Macro {uint32_t} INADDR_NONE
  869. @standards{BSD, netinet/in.h}
  870. This constant is returned by some functions to indicate an error.
  871. @end deftypevr
  872. @deftp {Data Type} {struct in6_addr}
  873. @standards{IPv6 basic API, netinet/in.h}
  874. This data type is used to store an IPv6 address. It stores 128 bits of
  875. data, which can be accessed (via a union) in a variety of ways.
  876. @end deftp
  877. @deftypevr Constant {struct in6_addr} in6addr_loopback
  878. @standards{IPv6 basic API, netinet/in.h}
  879. This constant is the IPv6 address @samp{::1}, the loopback address. See
  880. above for a description of what this means. The macro
  881. @code{IN6ADDR_LOOPBACK_INIT} is provided to allow you to initialize your
  882. own variables to this value.
  883. @end deftypevr
  884. @deftypevr Constant {struct in6_addr} in6addr_any
  885. @standards{IPv6 basic API, netinet/in.h}
  886. This constant is the IPv6 address @samp{::}, the unspecified address. See
  887. above for a description of what this means. The macro
  888. @code{IN6ADDR_ANY_INIT} is provided to allow you to initialize your
  889. own variables to this value.
  890. @end deftypevr
  891. @node Host Address Functions
  892. @subsubsection Host Address Functions
  893. @pindex arpa/inet.h
  894. @noindent
  895. These additional functions for manipulating Internet addresses are
  896. declared in the header file @file{arpa/inet.h}. They represent Internet
  897. addresses in network byte order, and network numbers and
  898. local-address-within-network numbers in host byte order. @xref{Byte
  899. Order}, for an explanation of network and host byte order.
  900. @deftypefun int inet_aton (const char *@var{name}, struct in_addr *@var{addr})
  901. @standards{BSD, arpa/inet.h}
  902. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  903. @c inet_aton @mtslocale
  904. @c isdigit dup @mtslocale
  905. @c strtoul dup @mtslocale
  906. @c isascii dup @mtslocale
  907. @c isspace dup @mtslocale
  908. @c htonl dup ok
  909. This function converts the IPv4 Internet host address @var{name}
  910. from the standard numbers-and-dots notation into binary data and stores
  911. it in the @code{struct in_addr} that @var{addr} points to.
  912. @code{inet_aton} returns nonzero if the address is valid, zero if not.
  913. @end deftypefun
  914. @deftypefun {uint32_t} inet_addr (const char *@var{name})
  915. @standards{BSD, arpa/inet.h}
  916. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  917. @c inet_addr @mtslocale
  918. @c inet_aton dup @mtslocale
  919. This function converts the IPv4 Internet host address @var{name} from the
  920. standard numbers-and-dots notation into binary data. If the input is
  921. not valid, @code{inet_addr} returns @code{INADDR_NONE}. This is an
  922. obsolete interface to @code{inet_aton}, described immediately above. It
  923. is obsolete because @code{INADDR_NONE} is a valid address
  924. (255.255.255.255), and @code{inet_aton} provides a cleaner way to
  925. indicate error return.
  926. @end deftypefun
  927. @deftypefun {uint32_t} inet_network (const char *@var{name})
  928. @standards{BSD, arpa/inet.h}
  929. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  930. @c inet_network @mtslocale
  931. @c isdigit dup @mtslocale
  932. @c isxdigit dup @mtslocale
  933. @c tolower dup @mtslocale
  934. @c isspace dup @mtslocale
  935. This function extracts the network number from the address @var{name},
  936. given in the standard numbers-and-dots notation. The returned address is
  937. in host order. If the input is not valid, @code{inet_network} returns
  938. @code{-1}.
  939. The function works only with traditional IPv4 class A, B and C network
  940. types. It doesn't work with classless addresses and shouldn't be used
  941. anymore.
  942. @end deftypefun
  943. @deftypefun {char *} inet_ntoa (struct in_addr @var{addr})
  944. @standards{BSD, arpa/inet.h}
  945. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asurace{}}@acsafe{}}
  946. @c inet_ntoa @mtslocale @asurace
  947. @c writes to a thread-local static buffer
  948. @c snprintf @mtslocale [no @ascuheap or @acsmem]
  949. This function converts the IPv4 Internet host address @var{addr} to a
  950. string in the standard numbers-and-dots notation. The return value is
  951. a pointer into a statically-allocated buffer. Subsequent calls will
  952. overwrite the same buffer, so you should copy the string if you need
  953. to save it.
  954. In multi-threaded programs each thread has its own statically-allocated
  955. buffer. But still subsequent calls of @code{inet_ntoa} in the same
  956. thread will overwrite the result of the last call.
  957. Instead of @code{inet_ntoa} the newer function @code{inet_ntop} which is
  958. described below should be used since it handles both IPv4 and IPv6
  959. addresses.
  960. @end deftypefun
  961. @deftypefun {struct in_addr} inet_makeaddr (uint32_t @var{net}, uint32_t @var{local})
  962. @standards{BSD, arpa/inet.h}
  963. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  964. @c inet_makeaddr ok
  965. @c htonl dup ok
  966. This function makes an IPv4 Internet host address by combining the network
  967. number @var{net} with the local-address-within-network number
  968. @var{local}.
  969. @end deftypefun
  970. @deftypefun uint32_t inet_lnaof (struct in_addr @var{addr})
  971. @standards{BSD, arpa/inet.h}
  972. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  973. @c inet_lnaof ok
  974. @c ntohl dup ok
  975. @c IN_CLASSA ok
  976. @c IN_CLASSB ok
  977. This function returns the local-address-within-network part of the
  978. Internet host address @var{addr}.
  979. The function works only with traditional IPv4 class A, B and C network
  980. types. It doesn't work with classless addresses and shouldn't be used
  981. anymore.
  982. @end deftypefun
  983. @deftypefun uint32_t inet_netof (struct in_addr @var{addr})
  984. @standards{BSD, arpa/inet.h}
  985. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  986. @c inet_netof ok
  987. @c ntohl dup ok
  988. @c IN_CLASSA ok
  989. @c IN_CLASSB ok
  990. This function returns the network number part of the Internet host
  991. address @var{addr}.
  992. The function works only with traditional IPv4 class A, B and C network
  993. types. It doesn't work with classless addresses and shouldn't be used
  994. anymore.
  995. @end deftypefun
  996. @deftypefun int inet_pton (int @var{af}, const char *@var{cp}, void *@var{buf})
  997. @standards{IPv6 basic API, arpa/inet.h}
  998. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  999. @c inet_pton @mtslocale
  1000. @c inet_pton4 ok
  1001. @c memcpy dup ok
  1002. @c inet_pton6 @mtslocale
  1003. @c memset dup ok
  1004. @c tolower dup @mtslocale
  1005. @c strchr dup ok
  1006. @c inet_pton4 dup ok
  1007. @c memcpy dup ok
  1008. This function converts an Internet address (either IPv4 or IPv6) from
  1009. presentation (textual) to network (binary) format. @var{af} should be
  1010. either @code{AF_INET} or @code{AF_INET6}, as appropriate for the type of
  1011. address being converted. @var{cp} is a pointer to the input string, and
  1012. @var{buf} is a pointer to a buffer for the result. It is the caller's
  1013. responsibility to make sure the buffer is large enough.
  1014. The return value is @code{1} on success and @code{0} if @var{cp} does not
  1015. point to a valid address string for the address family @var{af} requested.
  1016. On failure, the function's return value is @code{-1} and @code{errno} is
  1017. set accordingly. The following @code{errno} values are specific to this
  1018. function:
  1019. @table @code
  1020. @item EAFNOSUPPORT
  1021. The address family requested is neither @code{AF_INET} nor @code{AF_INET6}.
  1022. @end table
  1023. @end deftypefun
  1024. @deftypefun {const char *} inet_ntop (int @var{af}, const void *@var{cp}, char *@var{buf}, socklen_t @var{len})
  1025. @standards{IPv6 basic API, arpa/inet.h}
  1026. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  1027. @c inet_ntop @mtslocale
  1028. @c inet_ntop4 @mtslocale
  1029. @c sprintf dup @mtslocale [no @ascuheap or @acsmem]
  1030. @c strcpy dup ok
  1031. @c inet_ntop6 @mtslocale
  1032. @c memset dup ok
  1033. @c inet_ntop4 dup @mtslocale
  1034. @c sprintf dup @mtslocale [no @ascuheap or @acsmem]
  1035. @c strcpy dup ok
  1036. This function converts an Internet address (either IPv4 or IPv6) from
  1037. network (binary) to presentation (textual) form. @var{af} should be
  1038. either @code{AF_INET} or @code{AF_INET6}, as appropriate. @var{cp} is a
  1039. pointer to the address to be converted. @var{buf} should be a pointer
  1040. to a buffer to hold the result, and @var{len} is the length of this
  1041. buffer.
  1042. The return value is @var{buf} on success. On failure, the function's
  1043. return value is a null pointer and @code{errno} is set accordingly.
  1044. The following @code{errno} values are specific to this function:
  1045. @table @code
  1046. @item EAFNOSUPPORT
  1047. The address family requested is neither @code{AF_INET} nor @code{AF_INET6}.
  1048. @item ENOSPC
  1049. Insufficient space available for the result in the buffer provided.
  1050. @end table
  1051. @end deftypefun
  1052. @node Host Names
  1053. @subsubsection Host Names
  1054. @cindex hosts database
  1055. @cindex converting host name to address
  1056. @cindex converting host address to name
  1057. Besides the standard numbers-and-dots notation for Internet addresses,
  1058. you can also refer to a host by a symbolic name. The advantage of a
  1059. symbolic name is that it is usually easier to remember. For example,
  1060. the machine with Internet address @samp{158.121.106.19} is also known as
  1061. @samp{alpha.gnu.org}; and other machines in the @samp{gnu.org}
  1062. domain can refer to it simply as @samp{alpha}.
  1063. @pindex /etc/hosts
  1064. @pindex netdb.h
  1065. Internally, the system uses a database to keep track of the mapping
  1066. between host names and host numbers. This database is usually either
  1067. the file @file{/etc/hosts} or an equivalent provided by a name server.
  1068. The functions and other symbols for accessing this database are declared
  1069. in @file{netdb.h}. They are BSD features, defined unconditionally if
  1070. you include @file{netdb.h}.
  1071. @deftp {Data Type} {struct hostent}
  1072. @standards{BSD, netdb.h}
  1073. This data type is used to represent an entry in the hosts database. It
  1074. has the following members:
  1075. @table @code
  1076. @item char *h_name
  1077. This is the ``official'' name of the host.
  1078. @item char **h_aliases
  1079. These are alternative names for the host, represented as a null-terminated
  1080. vector of strings.
  1081. @item int h_addrtype
  1082. This is the host address type; in practice, its value is always either
  1083. @code{AF_INET} or @code{AF_INET6}, with the latter being used for IPv6
  1084. hosts. In principle other kinds of addresses could be represented in
  1085. the database as well as Internet addresses; if this were done, you
  1086. might find a value in this field other than @code{AF_INET} or
  1087. @code{AF_INET6}. @xref{Socket Addresses}.
  1088. @item int h_length
  1089. This is the length, in bytes, of each address.
  1090. @item char **h_addr_list
  1091. This is the vector of addresses for the host. (Recall that the host
  1092. might be connected to multiple networks and have different addresses on
  1093. each one.) The vector is terminated by a null pointer.
  1094. @item char *h_addr
  1095. This is a synonym for @code{h_addr_list[0]}; in other words, it is the
  1096. first host address.
  1097. @end table
  1098. @end deftp
  1099. As far as the host database is concerned, each address is just a block
  1100. of memory @code{h_length} bytes long. But in other contexts there is an
  1101. implicit assumption that you can convert IPv4 addresses to a
  1102. @code{struct in_addr} or an @code{uint32_t}. Host addresses in
  1103. a @code{struct hostent} structure are always given in network byte
  1104. order; see @ref{Byte Order}.
  1105. You can use @code{gethostbyname}, @code{gethostbyname2} or
  1106. @code{gethostbyaddr} to search the hosts database for information about
  1107. a particular host. The information is returned in a
  1108. statically-allocated structure; you must copy the information if you
  1109. need to save it across calls. You can also use @code{getaddrinfo} and
  1110. @code{getnameinfo} to obtain this information.
  1111. @deftypefun {struct hostent *} gethostbyname (const char *@var{name})
  1112. @standards{BSD, netdb.h}
  1113. @safety{@prelim{}@mtunsafe{@mtasurace{:hostbyname} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1114. @c gethostbyname @mtasurace:hostbyname @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1115. @c libc_lock_lock dup @asulock @aculock
  1116. @c malloc dup @ascuheap @acsmem
  1117. @c nss_hostname_digits_dots @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1118. @c res_maybe_init(!preinit) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1119. @c res_iclose @acsuheap @acsmem @acsfd
  1120. @c close_not_cancel_no_status dup @acsfd
  1121. @c free dup @acsuheap @acsmem
  1122. @c res_vinit @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1123. @c res_randomid ok
  1124. @c getpid dup ok
  1125. @c getenv dup @mtsenv
  1126. @c strncpy dup ok
  1127. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  1128. @c fsetlocking dup ok [no concurrent uses]
  1129. @c fgets_unlocked dup ok [no concurrent uses]
  1130. @c MATCH ok
  1131. @c strncmp dup ok
  1132. @c strpbrk dup ok
  1133. @c strchr dup ok
  1134. @c inet_aton dup @mtslocale
  1135. @c htons dup
  1136. @c inet_pton dup @mtslocale
  1137. @c malloc dup @ascuheap @acsmem
  1138. @c IN6_IS_ADDR_LINKLOCAL ok
  1139. @c htonl dup ok
  1140. @c IN6_IS_ADDR_MC_LINKLOCAL ok
  1141. @c if_nametoindex dup @asulock @aculock @acsfd
  1142. @c strtoul dup @mtslocale
  1143. @c ISSORTMASK ok
  1144. @c strchr dup ok
  1145. @c isascii dup @mtslocale
  1146. @c isspace dup @mtslocale
  1147. @c net_mask ok
  1148. @c ntohl dup ok
  1149. @c IN_CLASSA dup ok
  1150. @c htonl dup ok
  1151. @c IN_CLASSB dup ok
  1152. @c res_setoptions @mtslocale
  1153. @c strncmp dup ok
  1154. @c atoi dup @mtslocale
  1155. @c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
  1156. @c inet_makeaddr dup ok
  1157. @c gethostname dup ok
  1158. @c strcpy dup ok
  1159. @c rawmemchr dup ok
  1160. @c res_ninit @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1161. @c res_vinit dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1162. @c isdigit dup @mtslocale
  1163. @c isxdigit dup @mtslocale
  1164. @c strlen dup ok
  1165. @c realloc dup @ascuheap @acsmem
  1166. @c free dup @ascuheap @acsmem
  1167. @c memset dup ok
  1168. @c inet_aton dup @mtslocale
  1169. @c inet_pton dup @mtslocale
  1170. @c strcpy dup ok
  1171. @c memcpy dup ok
  1172. @c strchr dup ok
  1173. @c gethostbyname_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1174. @c realloc dup @ascuheap @acsmem
  1175. @c free dup @ascuheap @acsmem
  1176. @c libc_lock_unlock dup @aculock
  1177. @c set_h_errno ok
  1178. The @code{gethostbyname} function returns information about the host
  1179. named @var{name}. If the lookup fails, it returns a null pointer.
  1180. @end deftypefun
  1181. @deftypefun {struct hostent *} gethostbyname2 (const char *@var{name}, int @var{af})
  1182. @standards{IPv6 Basic API, netdb.h}
  1183. @safety{@prelim{}@mtunsafe{@mtasurace{:hostbyname2} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1184. @c gethostbyname2 @mtasurace:hostbyname2 @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1185. @c libc_lock_lock dup @asulock @aculock
  1186. @c malloc dup @ascuheap @acsmem
  1187. @c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1188. @c gethostbyname2_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1189. @c realloc dup @ascuheap @acsmem
  1190. @c free dup @ascuheap @acsmem
  1191. @c libc_lock_unlock dup @aculock
  1192. @c set_h_errno dup ok
  1193. The @code{gethostbyname2} function is like @code{gethostbyname}, but
  1194. allows the caller to specify the desired address family (e.g.@:
  1195. @code{AF_INET} or @code{AF_INET6}) of the result.
  1196. @end deftypefun
  1197. @deftypefun {struct hostent *} gethostbyaddr (const void *@var{addr}, socklen_t @var{length}, int @var{format})
  1198. @standards{BSD, netdb.h}
  1199. @safety{@prelim{}@mtunsafe{@mtasurace{:hostbyaddr} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1200. @c gethostbyaddr @mtasurace:hostbyaddr @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1201. @c libc_lock_lock dup @asulock @aculock
  1202. @c malloc dup @ascuheap @acsmem
  1203. @c gethostbyaddr_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1204. @c realloc dup @ascuheap @acsmem
  1205. @c free dup @ascuheap @acsmem
  1206. @c libc_lock_unlock dup @aculock
  1207. @c set_h_errno dup ok
  1208. The @code{gethostbyaddr} function returns information about the host
  1209. with Internet address @var{addr}. The parameter @var{addr} is not
  1210. really a pointer to char - it can be a pointer to an IPv4 or an IPv6
  1211. address. The @var{length} argument is the size (in bytes) of the address
  1212. at @var{addr}. @var{format} specifies the address format; for an IPv4
  1213. Internet address, specify a value of @code{AF_INET}; for an IPv6
  1214. Internet address, use @code{AF_INET6}.
  1215. If the lookup fails, @code{gethostbyaddr} returns a null pointer.
  1216. @end deftypefun
  1217. @vindex h_errno
  1218. If the name lookup by @code{gethostbyname} or @code{gethostbyaddr}
  1219. fails, you can find out the reason by looking at the value of the
  1220. variable @code{h_errno}. (It would be cleaner design for these
  1221. functions to set @code{errno}, but use of @code{h_errno} is compatible
  1222. with other systems.)
  1223. Here are the error codes that you may find in @code{h_errno}:
  1224. @vtable @code
  1225. @item HOST_NOT_FOUND
  1226. @standards{BSD, netdb.h}
  1227. No such host is known in the database.
  1228. @item TRY_AGAIN
  1229. @standards{BSD, netdb.h}
  1230. This condition happens when the name server could not be contacted. If
  1231. you try again later, you may succeed then.
  1232. @item NO_RECOVERY
  1233. @standards{BSD, netdb.h}
  1234. A non-recoverable error occurred.
  1235. @item NO_ADDRESS
  1236. @standards{BSD, netdb.h}
  1237. The host database contains an entry for the name, but it doesn't have an
  1238. associated Internet address.
  1239. @end vtable
  1240. The lookup functions above all have one thing in common: they are not
  1241. reentrant and therefore unusable in multi-threaded applications.
  1242. Therefore provides @theglibc{} a new set of functions which can be
  1243. used in this context.
  1244. @deftypefun int gethostbyname_r (const char *restrict @var{name}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
  1245. @standards{GNU, netdb.h}
  1246. @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1247. @c gethostbyname_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1248. @c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1249. @c nscd_gethostbyname_r @mtsenv @ascuheap @acsfd @acsmem
  1250. @c nscd_gethst_r @mtsenv @ascuheap @acsfd @acsmem
  1251. @c getenv dup @mtsenv
  1252. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  1253. @c nscd_cache_search dup ok
  1254. @c memcpy dup ok
  1255. @c nscd_open_socket dup @acsfd
  1256. @c readvall dup ok
  1257. @c readall dup ok
  1258. @c close_not_cancel_no_status dup @acsfd
  1259. @c nscd_drop_map_ref dup @ascuheap @acsmem
  1260. @c nscd_unmap dup @ascuheap @acsmem
  1261. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1262. @c res_hconf_init @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
  1263. @c res_hconf.c:do_init @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1264. @c memset dup ok
  1265. @c getenv dup @mtsenv
  1266. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  1267. @c fsetlocking dup ok [no concurrent uses]
  1268. @c fgets_unlocked dup ok [no concurrent uses]
  1269. @c strchrnul dup ok
  1270. @c res_hconf.c:parse_line @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1271. @c skip_ws dup @mtslocale
  1272. @c skip_string dup @mtslocale
  1273. @c strncasecmp dup @mtslocale
  1274. @c strlen dup ok
  1275. @c asprintf dup @mtslocale @ascuheap @acsmem
  1276. @c fxprintf dup @asucorrupt @aculock @acucorrupt
  1277. @c free dup @ascuheap @acsmem
  1278. @c arg_trimdomain_list dup @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1279. @c arg_spoof dup @mtslocale
  1280. @c arg_bool dup @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1281. @c isspace dup @mtslocale
  1282. @c fclose dup @ascuheap @asulock @acsmem @acsfd @aculock
  1283. @c arg_spoof @mtslocale
  1284. @c skip_string @mtslocale
  1285. @c isspace dup @mtslocale
  1286. @c strncasecmp dup @mtslocale
  1287. @c arg_bool @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1288. @c strncasecmp dup @mtslocale
  1289. @c asprintf dup @mtslocale @ascuheap @acsmem
  1290. @c fxprintf dup @asucorrupt @aculock @acucorrupt
  1291. @c free dup @ascuheap @acsmem
  1292. @c arg_trimdomain_list @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
  1293. @c skip_string dup @mtslocale
  1294. @c asprintf dup @mtslocale @ascuheap @acsmem
  1295. @c fxprintf dup @asucorrupt @aculock @acucorrupt
  1296. @c free dup @ascuheap @acsmem
  1297. @c strndup dup @ascuheap @acsmem
  1298. @c skip_ws @mtslocale
  1299. @c isspace dup @mtslocale
  1300. @c nss_hosts_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1301. @c nss_database_lookup dup @mtslocale @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  1302. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1303. @c *fct.l -> _nss_*_gethostbyname_r @ascuplugin
  1304. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1305. @c res_hconf_reorder_addrs @asulock @ascuheap @aculock @acsmem @acsfd
  1306. @c socket dup @acsfd
  1307. @c libc_lock_lock dup @asulock @aculock
  1308. @c ifreq @ascuheap @acsmem
  1309. @c malloc dup @ascuheap @acsmem
  1310. @c if_nextreq dup ok
  1311. @c ioctl dup ok
  1312. @c realloc dup @ascuheap @acsmem
  1313. @c if_freereq dup @acsmem
  1314. @c libc_lock_unlock dup @aculock
  1315. @c close dup @acsfd
  1316. The @code{gethostbyname_r} function returns information about the host
  1317. named @var{name}. The caller must pass a pointer to an object of type
  1318. @code{struct hostent} in the @var{result_buf} parameter. In addition
  1319. the function may need extra buffer space and the caller must pass a
  1320. pointer and the size of the buffer in the @var{buf} and @var{buflen}
  1321. parameters.
  1322. A pointer to the buffer, in which the result is stored, is available in
  1323. @code{*@var{result}} after the function call successfully returned. The
  1324. buffer passed as the @var{buf} parameter can be freed only once the caller
  1325. has finished with the result hostent struct, or has copied it including all
  1326. the other memory that it points to. If an error occurs or if no entry is
  1327. found, the pointer @code{*@var{result}} is a null pointer. Success is
  1328. signalled by a zero return value. If the function failed the return value
  1329. is an error number. In addition to the errors defined for
  1330. @code{gethostbyname} it can also be @code{ERANGE}. In this case the call
  1331. should be repeated with a larger buffer. Additional error information is
  1332. not stored in the global variable @code{h_errno} but instead in the object
  1333. pointed to by @var{h_errnop}.
  1334. Here's a small example:
  1335. @smallexample
  1336. struct hostent *
  1337. gethostname (char *host)
  1338. @{
  1339. struct hostent *hostbuf, *hp;
  1340. size_t hstbuflen;
  1341. char *tmphstbuf;
  1342. int res;
  1343. int herr;
  1344. hostbuf = malloc (sizeof (struct hostent));
  1345. hstbuflen = 1024;
  1346. tmphstbuf = malloc (hstbuflen);
  1347. while ((res = gethostbyname_r (host, hostbuf, tmphstbuf, hstbuflen,
  1348. &hp, &herr)) == ERANGE)
  1349. @{
  1350. /* Enlarge the buffer. */
  1351. tmphstbuf = reallocarray (tmphstbuf, hstbuflen, 2);
  1352. hstbuflen *= 2;
  1353. @}
  1354. free (tmphstbuf);
  1355. /* Check for errors. */
  1356. if (res || hp == NULL)
  1357. return NULL;
  1358. return hp;
  1359. @}
  1360. @end smallexample
  1361. @end deftypefun
  1362. @deftypefun int gethostbyname2_r (const char *@var{name}, int @var{af}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
  1363. @standards{GNU, netdb.h}
  1364. @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1365. @c gethostbyname2_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1366. @c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1367. @c nscd_gethostbyname2_r @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
  1368. @c nscd_gethst_r dup @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
  1369. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1370. @c res_hconf_init dup @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
  1371. @c nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1372. @c *fct.l -> _nss_*_gethostbyname2_r @ascuplugin
  1373. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1374. @c res_hconf_reorder_addrs dup @asulock @ascuheap @aculock @acsmem @acsfd
  1375. The @code{gethostbyname2_r} function is like @code{gethostbyname_r}, but
  1376. allows the caller to specify the desired address family (e.g.@:
  1377. @code{AF_INET} or @code{AF_INET6}) for the result.
  1378. @end deftypefun
  1379. @deftypefun int gethostbyaddr_r (const void *@var{addr}, socklen_t @var{length}, int @var{format}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
  1380. @standards{GNU, netdb.h}
  1381. @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  1382. @c gethostbyaddr_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
  1383. @c memcmp dup ok
  1384. @c nscd_gethostbyaddr_r @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
  1385. @c nscd_gethst_r dup @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
  1386. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1387. @c res_hconf_init dup @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
  1388. @c nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1389. @c *fct.l -> _nss_*_gethostbyaddr_r @ascuplugin
  1390. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1391. @c res_hconf_reorder_addrs dup @asulock @ascuheap @aculock @acsmem @acsfd
  1392. @c res_hconf_trim_domains @mtslocale
  1393. @c res_hconf_trim_domain @mtslocale
  1394. @c strlen dup ok
  1395. @c strcasecmp dup @mtslocale
  1396. The @code{gethostbyaddr_r} function returns information about the host
  1397. with Internet address @var{addr}. The parameter @var{addr} is not
  1398. really a pointer to char - it can be a pointer to an IPv4 or an IPv6
  1399. address. The @var{length} argument is the size (in bytes) of the address
  1400. at @var{addr}. @var{format} specifies the address format; for an IPv4
  1401. Internet address, specify a value of @code{AF_INET}; for an IPv6
  1402. Internet address, use @code{AF_INET6}.
  1403. Similar to the @code{gethostbyname_r} function, the caller must provide
  1404. buffers for the result and memory used internally. In case of success
  1405. the function returns zero. Otherwise the value is an error number where
  1406. @code{ERANGE} has the special meaning that the caller-provided buffer is
  1407. too small.
  1408. @end deftypefun
  1409. You can also scan the entire hosts database one entry at a time using
  1410. @code{sethostent}, @code{gethostent} and @code{endhostent}. Be careful
  1411. when using these functions because they are not reentrant.
  1412. @deftypefun void sethostent (int @var{stayopen})
  1413. @standards{BSD, netdb.h}
  1414. @safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1415. @c sethostent @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1416. @c libc_lock_lock dup @asulock @aculock
  1417. @c nss_setent(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1418. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1419. @c set_h_errno dup ok
  1420. @c setup(nss_hosts_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1421. @c *lookup_fct = nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1422. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1423. @c *fct.f @mtasurace:hostent @ascuplugin
  1424. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1425. @c libc_lock_unlock dup @aculock
  1426. This function opens the hosts database to begin scanning it. You can
  1427. then call @code{gethostent} to read the entries.
  1428. @c There was a rumor that this flag has different meaning if using the DNS,
  1429. @c but it appears this description is accurate in that case also.
  1430. If the @var{stayopen} argument is nonzero, this sets a flag so that
  1431. subsequent calls to @code{gethostbyname} or @code{gethostbyaddr} will
  1432. not close the database (as they usually would). This makes for more
  1433. efficiency if you call those functions several times, by avoiding
  1434. reopening the database for each call.
  1435. @end deftypefun
  1436. @deftypefun {struct hostent *} gethostent (void)
  1437. @standards{BSD, netdb.h}
  1438. @safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtasurace{:hostentbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1439. @c gethostent @mtasurace:hostent @mtasurace:hostentbuf @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1440. @c libc_lock_lock dup @asulock @aculock
  1441. @c nss_getent(gethostent_r) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1442. @c malloc dup @ascuheap @acsmem
  1443. @c *func = gethostent_r dup @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1444. @c realloc dup @ascuheap @acsmem
  1445. @c free dup @ascuheap @acsmem
  1446. @c libc_lock_unlock dup @aculock
  1447. @c
  1448. @c gethostent_r @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1449. @c libc_lock_lock dup @asulock @aculock
  1450. @c nss_getent_r(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1451. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1452. @c setup(nss_hosts_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1453. @c *fct.f @mtasurace:hostent @ascuplugin
  1454. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1455. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1456. @c *sfct.f @mtasurace:hostent @ascuplugin
  1457. @c libc_lock_unlock dup @aculock
  1458. This function returns the next entry in the hosts database. It
  1459. returns a null pointer if there are no more entries.
  1460. @end deftypefun
  1461. @deftypefun void endhostent (void)
  1462. @standards{BSD, netdb.h}
  1463. @safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1464. @c endhostent @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1465. @c libc_lock_lock @asulock @aculock
  1466. @c nss_endent(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1467. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1468. @c setup(nss_passwd_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1469. @c *fct.f @mtasurace:hostent @ascuplugin
  1470. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1471. @c libc_lock_unlock @aculock
  1472. This function closes the hosts database.
  1473. @end deftypefun
  1474. @node Ports
  1475. @subsection Internet Ports
  1476. @cindex port number
  1477. A socket address in the Internet namespace consists of a machine's
  1478. Internet address plus a @dfn{port number} which distinguishes the
  1479. sockets on a given machine (for a given protocol). Port numbers range
  1480. from 0 to 65,535.
  1481. Port numbers less than @code{IPPORT_RESERVED} are reserved for standard
  1482. servers, such as @code{finger} and @code{telnet}. There is a database
  1483. that keeps track of these, and you can use the @code{getservbyname}
  1484. function to map a service name onto a port number; see @ref{Services
  1485. Database}.
  1486. If you write a server that is not one of the standard ones defined in
  1487. the database, you must choose a port number for it. Use a number
  1488. greater than @code{IPPORT_USERRESERVED}; such numbers are reserved for
  1489. servers and won't ever be generated automatically by the system.
  1490. Avoiding conflicts with servers being run by other users is up to you.
  1491. When you use a socket without specifying its address, the system
  1492. generates a port number for it. This number is between
  1493. @code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.
  1494. On the Internet, it is actually legitimate to have two different
  1495. sockets with the same port number, as long as they never both try to
  1496. communicate with the same socket address (host address plus port
  1497. number). You shouldn't duplicate a port number except in special
  1498. circumstances where a higher-level protocol requires it. Normally,
  1499. the system won't let you do it; @code{bind} normally insists on
  1500. distinct port numbers. To reuse a port number, you must set the
  1501. socket option @code{SO_REUSEADDR}. @xref{Socket-Level Options}.
  1502. @pindex netinet/in.h
  1503. These macros are defined in the header file @file{netinet/in.h}.
  1504. @deftypevr Macro int IPPORT_RESERVED
  1505. @standards{BSD, netinet/in.h}
  1506. Port numbers less than @code{IPPORT_RESERVED} are reserved for
  1507. superuser use.
  1508. @end deftypevr
  1509. @deftypevr Macro int IPPORT_USERRESERVED
  1510. @standards{BSD, netinet/in.h}
  1511. Port numbers greater than or equal to @code{IPPORT_USERRESERVED} are
  1512. reserved for explicit use; they will never be allocated automatically.
  1513. @end deftypevr
  1514. @node Services Database
  1515. @subsection The Services Database
  1516. @cindex services database
  1517. @cindex converting service name to port number
  1518. @cindex converting port number to service name
  1519. @pindex /etc/services
  1520. The database that keeps track of ``well-known'' services is usually
  1521. either the file @file{/etc/services} or an equivalent from a name server.
  1522. You can use these utilities, declared in @file{netdb.h}, to access
  1523. the services database.
  1524. @pindex netdb.h
  1525. @deftp {Data Type} {struct servent}
  1526. @standards{BSD, netdb.h}
  1527. This data type holds information about entries from the services database.
  1528. It has the following members:
  1529. @table @code
  1530. @item char *s_name
  1531. This is the ``official'' name of the service.
  1532. @item char **s_aliases
  1533. These are alternate names for the service, represented as an array of
  1534. strings. A null pointer terminates the array.
  1535. @item int s_port
  1536. This is the port number for the service. Port numbers are given in
  1537. network byte order; see @ref{Byte Order}.
  1538. @item char *s_proto
  1539. This is the name of the protocol to use with this service.
  1540. @xref{Protocols Database}.
  1541. @end table
  1542. @end deftp
  1543. To get information about a particular service, use the
  1544. @code{getservbyname} or @code{getservbyport} functions. The information
  1545. is returned in a statically-allocated structure; you must copy the
  1546. information if you need to save it across calls.
  1547. @deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})
  1548. @standards{BSD, netdb.h}
  1549. @safety{@prelim{}@mtunsafe{@mtasurace{:servbyname} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1550. @c getservbyname =~ getpwuid @mtasurace:servbyname @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1551. @c libc_lock_lock dup @asulock @aculock
  1552. @c malloc dup @ascuheap @acsmem
  1553. @c getservbyname_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1554. @c realloc dup @ascuheap @acsmem
  1555. @c free dup @ascuheap @acsmem
  1556. @c libc_lock_unlock dup @aculock
  1557. @c
  1558. @c getservbyname_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1559. @c nscd_getservbyname_r @ascuheap @acsfd @acsmem
  1560. @c nscd_getserv_r @ascuheap @acsfd @acsmem
  1561. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  1562. @c strlen dup ok
  1563. @c malloc dup @ascuheap @acsmem
  1564. @c mempcpy dup ok
  1565. @c memcpy dup ok
  1566. @c nscd_cache_search dup ok
  1567. @c nscd_open_socket dup @acsfd
  1568. @c readvall dup ok
  1569. @c readall dup ok
  1570. @c close_not_cancel_no_status dup @acsfd
  1571. @c nscd_drop_map_ref dup @ascuheap @acsmem
  1572. @c nscd_unmap dup @ascuheap @acsmem
  1573. @c free dup @ascuheap @acsmem
  1574. @c nss_services_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1575. @c *fct.l -> _nss_*_getservbyname_r @ascuplugin
  1576. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1577. The @code{getservbyname} function returns information about the
  1578. service named @var{name} using protocol @var{proto}. If it can't find
  1579. such a service, it returns a null pointer.
  1580. This function is useful for servers as well as for clients; servers
  1581. use it to determine which port they should listen on (@pxref{Listening}).
  1582. @end deftypefun
  1583. @deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})
  1584. @standards{BSD, netdb.h}
  1585. @safety{@prelim{}@mtunsafe{@mtasurace{:servbyport} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1586. @c getservbyport =~ getservbyname @mtasurace:servbyport @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1587. @c libc_lock_lock dup @asulock @aculock
  1588. @c malloc dup @ascuheap @acsmem
  1589. @c getservbyport_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1590. @c realloc dup @ascuheap @acsmem
  1591. @c free dup @ascuheap @acsmem
  1592. @c libc_lock_unlock dup @aculock
  1593. @c
  1594. @c getservbyport_r =~ getservbyname_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1595. @c nscd_getservbyport_r @ascuheap @acsfd @acsmem
  1596. @c nscd_getserv_r dup @ascuheap @acsfd @acsmem
  1597. @c nss_services_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1598. @c *fct.l -> _nss_*_getservbyport_r @ascuplugin
  1599. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1600. The @code{getservbyport} function returns information about the
  1601. service at port @var{port} using protocol @var{proto}. If it can't
  1602. find such a service, it returns a null pointer.
  1603. @end deftypefun
  1604. @noindent
  1605. You can also scan the services database using @code{setservent},
  1606. @code{getservent} and @code{endservent}. Be careful when using these
  1607. functions because they are not reentrant.
  1608. @deftypefun void setservent (int @var{stayopen})
  1609. @standards{BSD, netdb.h}
  1610. @safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1611. @c setservent @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1612. @c libc_lock_lock dup @asulock @aculock
  1613. @c nss_setent(nss_services_lookup2) @mtasurace:servenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1614. @c setup(nss_services_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1615. @c *lookup_fct = nss_services_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1616. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1617. @c *fct.f @mtasurace:servent @ascuplugin
  1618. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1619. @c libc_lock_unlock dup @aculock
  1620. This function opens the services database to begin scanning it.
  1621. If the @var{stayopen} argument is nonzero, this sets a flag so that
  1622. subsequent calls to @code{getservbyname} or @code{getservbyport} will
  1623. not close the database (as they usually would). This makes for more
  1624. efficiency if you call those functions several times, by avoiding
  1625. reopening the database for each call.
  1626. @end deftypefun
  1627. @deftypefun {struct servent *} getservent (void)
  1628. @standards{BSD, netdb.h}
  1629. @safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtasurace{:serventbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1630. @c getservent @mtasurace:servent @mtasurace:serventbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1631. @c libc_lock_lock dup @asulock @aculock
  1632. @c nss_getent(getservent_r) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1633. @c malloc dup @ascuheap @acsmem
  1634. @c *func = getservent_r dup @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1635. @c realloc dup @ascuheap @acsmem
  1636. @c free dup @ascuheap @acsmem
  1637. @c libc_lock_unlock dup @aculock
  1638. @c
  1639. @c getservent_r @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1640. @c libc_lock_lock dup @asulock @aculock
  1641. @c nss_getent_r(nss_services_lookup2) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1642. @c setup(nss_services_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1643. @c *fct.f @mtasurace:servent @ascuplugin
  1644. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1645. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1646. @c *sfct.f @mtasurace:servent @ascuplugin
  1647. @c libc_lock_unlock dup @aculock
  1648. This function returns the next entry in the services database. If
  1649. there are no more entries, it returns a null pointer.
  1650. @end deftypefun
  1651. @deftypefun void endservent (void)
  1652. @standards{BSD, netdb.h}
  1653. @safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1654. @c endservent @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1655. @c libc_lock_lock @asulock @aculock
  1656. @c nss_endent(nss_services_lookup2) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1657. @c setup(nss_services_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1658. @c *fct.f @mtasurace:servent @ascuplugin
  1659. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1660. @c libc_lock_unlock @aculock
  1661. This function closes the services database.
  1662. @end deftypefun
  1663. @node Byte Order
  1664. @subsection Byte Order Conversion
  1665. @cindex byte order conversion, for socket
  1666. @cindex converting byte order
  1667. @cindex big-endian
  1668. @cindex little-endian
  1669. Different kinds of computers use different conventions for the
  1670. ordering of bytes within a word. Some computers put the most
  1671. significant byte within a word first (this is called ``big-endian''
  1672. order), and others put it last (``little-endian'' order).
  1673. @cindex network byte order
  1674. So that machines with different byte order conventions can
  1675. communicate, the Internet protocols specify a canonical byte order
  1676. convention for data transmitted over the network. This is known
  1677. as @dfn{network byte order}.
  1678. When establishing an Internet socket connection, you must make sure that
  1679. the data in the @code{sin_port} and @code{sin_addr} members of the
  1680. @code{sockaddr_in} structure are represented in network byte order.
  1681. If you are encoding integer data in the messages sent through the
  1682. socket, you should convert this to network byte order too. If you don't
  1683. do this, your program may fail when running on or talking to other kinds
  1684. of machines.
  1685. If you use @code{getservbyname} and @code{gethostbyname} or
  1686. @code{inet_addr} to get the port number and host address, the values are
  1687. already in network byte order, and you can copy them directly into
  1688. the @code{sockaddr_in} structure.
  1689. Otherwise, you have to convert the values explicitly. Use @code{htons}
  1690. and @code{ntohs} to convert values for the @code{sin_port} member. Use
  1691. @code{htonl} and @code{ntohl} to convert IPv4 addresses for the
  1692. @code{sin_addr} member. (Remember, @code{struct in_addr} is equivalent
  1693. to @code{uint32_t}.) These functions are declared in
  1694. @file{netinet/in.h}.
  1695. @pindex netinet/in.h
  1696. @deftypefun {uint16_t} htons (uint16_t @var{hostshort})
  1697. @standards{BSD, netinet/in.h}
  1698. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1699. @c htons ok
  1700. @c bswap_16 ok
  1701. @c bswap_constant_16 ok
  1702. This function converts the @code{uint16_t} integer @var{hostshort} from
  1703. host byte order to network byte order.
  1704. @end deftypefun
  1705. @deftypefun {uint16_t} ntohs (uint16_t @var{netshort})
  1706. @standards{BSD, netinet/in.h}
  1707. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1708. @c Alias to htons.
  1709. This function converts the @code{uint16_t} integer @var{netshort} from
  1710. network byte order to host byte order.
  1711. @end deftypefun
  1712. @deftypefun {uint32_t} htonl (uint32_t @var{hostlong})
  1713. @standards{BSD, netinet/in.h}
  1714. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1715. @c htonl ok
  1716. @c bswap_32 dup ok
  1717. This function converts the @code{uint32_t} integer @var{hostlong} from
  1718. host byte order to network byte order.
  1719. This is used for IPv4 Internet addresses.
  1720. @end deftypefun
  1721. @deftypefun {uint32_t} ntohl (uint32_t @var{netlong})
  1722. @standards{BSD, netinet/in.h}
  1723. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1724. @c Alias to htonl.
  1725. This function converts the @code{uint32_t} integer @var{netlong} from
  1726. network byte order to host byte order.
  1727. This is used for IPv4 Internet addresses.
  1728. @end deftypefun
  1729. @node Protocols Database
  1730. @subsection Protocols Database
  1731. @cindex protocols database
  1732. The communications protocol used with a socket controls low-level
  1733. details of how data are exchanged. For example, the protocol implements
  1734. things like checksums to detect errors in transmissions, and routing
  1735. instructions for messages. Normal user programs have little reason to
  1736. mess with these details directly.
  1737. @cindex TCP (Internet protocol)
  1738. The default communications protocol for the Internet namespace depends on
  1739. the communication style. For stream communication, the default is TCP
  1740. (``transmission control protocol''). For datagram communication, the
  1741. default is UDP (``user datagram protocol''). For reliable datagram
  1742. communication, the default is RDP (``reliable datagram protocol'').
  1743. You should nearly always use the default.
  1744. @pindex /etc/protocols
  1745. Internet protocols are generally specified by a name instead of a
  1746. number. The network protocols that a host knows about are stored in a
  1747. database. This is usually either derived from the file
  1748. @file{/etc/protocols}, or it may be an equivalent provided by a name
  1749. server. You look up the protocol number associated with a named
  1750. protocol in the database using the @code{getprotobyname} function.
  1751. Here are detailed descriptions of the utilities for accessing the
  1752. protocols database. These are declared in @file{netdb.h}.
  1753. @pindex netdb.h
  1754. @deftp {Data Type} {struct protoent}
  1755. @standards{BSD, netdb.h}
  1756. This data type is used to represent entries in the network protocols
  1757. database. It has the following members:
  1758. @table @code
  1759. @item char *p_name
  1760. This is the official name of the protocol.
  1761. @item char **p_aliases
  1762. These are alternate names for the protocol, specified as an array of
  1763. strings. The last element of the array is a null pointer.
  1764. @item int p_proto
  1765. This is the protocol number (in host byte order); use this member as the
  1766. @var{protocol} argument to @code{socket}.
  1767. @end table
  1768. @end deftp
  1769. You can use @code{getprotobyname} and @code{getprotobynumber} to search
  1770. the protocols database for a specific protocol. The information is
  1771. returned in a statically-allocated structure; you must copy the
  1772. information if you need to save it across calls.
  1773. @deftypefun {struct protoent *} getprotobyname (const char *@var{name})
  1774. @standards{BSD, netdb.h}
  1775. @safety{@prelim{}@mtunsafe{@mtasurace{:protobyname} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1776. @c getprotobyname =~ getpwuid @mtasurace:protobyname @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1777. @c libc_lock_lock dup @asulock @aculock
  1778. @c malloc dup @ascuheap @acsmem
  1779. @c getprotobyname_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1780. @c realloc dup @ascuheap @acsmem
  1781. @c free dup @ascuheap @acsmem
  1782. @c libc_lock_unlock dup @aculock
  1783. @c
  1784. @c getprotobyname_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1785. @c no nscd support
  1786. @c nss_protocols_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1787. @c *fct.l -> _nss_*_getprotobyname_r @ascuplugin
  1788. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1789. The @code{getprotobyname} function returns information about the
  1790. network protocol named @var{name}. If there is no such protocol, it
  1791. returns a null pointer.
  1792. @end deftypefun
  1793. @deftypefun {struct protoent *} getprotobynumber (int @var{protocol})
  1794. @standards{BSD, netdb.h}
  1795. @safety{@prelim{}@mtunsafe{@mtasurace{:protobynumber} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1796. @c getprotobynumber =~ getpwuid @mtasurace:protobynumber @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1797. @c libc_lock_lock dup @asulock @aculock
  1798. @c malloc dup @ascuheap @acsmem
  1799. @c getprotobynumber_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1800. @c realloc dup @ascuheap @acsmem
  1801. @c free dup @ascuheap @acsmem
  1802. @c libc_lock_unlock dup @aculock
  1803. @c
  1804. @c getprotobynumber_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1805. @c no nscd support
  1806. @c nss_protocols_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1807. @c *fct.l -> _nss_*_getprotobynumber_r @ascuplugin
  1808. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1809. The @code{getprotobynumber} function returns information about the
  1810. network protocol with number @var{protocol}. If there is no such
  1811. protocol, it returns a null pointer.
  1812. @end deftypefun
  1813. You can also scan the whole protocols database one protocol at a time by
  1814. using @code{setprotoent}, @code{getprotoent} and @code{endprotoent}.
  1815. Be careful when using these functions because they are not reentrant.
  1816. @deftypefun void setprotoent (int @var{stayopen})
  1817. @standards{BSD, netdb.h}
  1818. @safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1819. @c setprotoent @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1820. @c libc_lock_lock dup @asulock @aculock
  1821. @c nss_setent(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1822. @c setup(nss_protocols_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1823. @c *lookup_fct = nss_protocols_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1824. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1825. @c *fct.f @mtasurace:protoent @ascuplugin
  1826. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1827. @c libc_lock_unlock dup @aculock
  1828. This function opens the protocols database to begin scanning it.
  1829. If the @var{stayopen} argument is nonzero, this sets a flag so that
  1830. subsequent calls to @code{getprotobyname} or @code{getprotobynumber} will
  1831. not close the database (as they usually would). This makes for more
  1832. efficiency if you call those functions several times, by avoiding
  1833. reopening the database for each call.
  1834. @end deftypefun
  1835. @deftypefun {struct protoent *} getprotoent (void)
  1836. @standards{BSD, netdb.h}
  1837. @safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtasurace{:protoentbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1838. @c getprotoent @mtasurace:protoent @mtasurace:protoentbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1839. @c libc_lock_lock dup @asulock @aculock
  1840. @c nss_getent(getprotoent_r) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1841. @c malloc dup @ascuheap @acsmem
  1842. @c *func = getprotoent_r dup @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1843. @c realloc dup @ascuheap @acsmem
  1844. @c free dup @ascuheap @acsmem
  1845. @c libc_lock_unlock dup @aculock
  1846. @c
  1847. @c getprotoent_r @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1848. @c libc_lock_lock dup @asulock @aculock
  1849. @c nss_getent_r(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1850. @c setup(nss_protocols_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1851. @c *fct.f @mtasurace:servent @ascuplugin
  1852. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1853. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1854. @c *sfct.f @mtasurace:protoent @ascuplugin
  1855. @c libc_lock_unlock dup @aculock
  1856. This function returns the next entry in the protocols database. It
  1857. returns a null pointer if there are no more entries.
  1858. @end deftypefun
  1859. @deftypefun void endprotoent (void)
  1860. @standards{BSD, netdb.h}
  1861. @safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1862. @c endprotoent @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1863. @c libc_lock_lock @asulock @aculock
  1864. @c nss_endent(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1865. @c setup(nss_protocols_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1866. @c *fct.f @mtasurace:protoent @ascuplugin
  1867. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1868. @c libc_lock_unlock @aculock
  1869. This function closes the protocols database.
  1870. @end deftypefun
  1871. @node Inet Example
  1872. @subsection Internet Socket Example
  1873. Here is an example showing how to create and name a socket in the
  1874. Internet namespace. The newly created socket exists on the machine that
  1875. the program is running on. Rather than finding and using the machine's
  1876. Internet address, this example specifies @code{INADDR_ANY} as the host
  1877. address; the system replaces that with the machine's actual address.
  1878. @smallexample
  1879. @include mkisock.c.texi
  1880. @end smallexample
  1881. Here is another example, showing how you can fill in a @code{sockaddr_in}
  1882. structure, given a host name string and a port number:
  1883. @smallexample
  1884. @include isockad.c.texi
  1885. @end smallexample
  1886. @node Misc Namespaces
  1887. @section Other Namespaces
  1888. @vindex PF_NS
  1889. @vindex PF_ISO
  1890. @vindex PF_CCITT
  1891. @vindex PF_IMPLINK
  1892. @vindex PF_ROUTE
  1893. Certain other namespaces and associated protocol families are supported
  1894. but not documented yet because they are not often used. @code{PF_NS}
  1895. refers to the Xerox Network Software protocols. @code{PF_ISO} stands
  1896. for Open Systems Interconnect. @code{PF_CCITT} refers to protocols from
  1897. CCITT. @file{socket.h} defines these symbols and others naming protocols
  1898. not actually implemented.
  1899. @code{PF_IMPLINK} is used for communicating between hosts and Internet
  1900. Message Processors. For information on this and @code{PF_ROUTE}, an
  1901. occasionally-used local area routing protocol, see the GNU Hurd Manual
  1902. (to appear in the future).
  1903. @node Open/Close Sockets
  1904. @section Opening and Closing Sockets
  1905. This section describes the actual library functions for opening and
  1906. closing sockets. The same functions work for all namespaces and
  1907. connection styles.
  1908. @menu
  1909. * Creating a Socket:: How to open a socket.
  1910. * Closing a Socket:: How to close a socket.
  1911. * Socket Pairs:: These are created like pipes.
  1912. @end menu
  1913. @node Creating a Socket
  1914. @subsection Creating a Socket
  1915. @cindex creating a socket
  1916. @cindex socket, creating
  1917. @cindex opening a socket
  1918. The primitive for creating a socket is the @code{socket} function,
  1919. declared in @file{sys/socket.h}.
  1920. @pindex sys/socket.h
  1921. @deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})
  1922. @standards{BSD, sys/socket.h}
  1923. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  1924. This function creates a socket and specifies communication style
  1925. @var{style}, which should be one of the socket styles listed in
  1926. @ref{Communication Styles}. The @var{namespace} argument specifies
  1927. the namespace; it must be @code{PF_LOCAL} (@pxref{Local Namespace}) or
  1928. @code{PF_INET} (@pxref{Internet Namespace}). @var{protocol}
  1929. designates the specific protocol (@pxref{Socket Concepts}); zero is
  1930. usually right for @var{protocol}.
  1931. The return value from @code{socket} is the file descriptor for the new
  1932. socket, or @code{-1} in case of error. The following @code{errno} error
  1933. conditions are defined for this function:
  1934. @table @code
  1935. @item EAFNOSUPPORT
  1936. The @var{namespace} requested is not supported.
  1937. @item ESOCKTNOSUPPORT
  1938. @itemx EPROTONOSUPPORT
  1939. @itemx EPROTOTYPE
  1940. The @var{style} is not supported by the @var{namespace} specified.
  1941. @item EPROTONOSUPPORT
  1942. The @var{protocol} is not supported by the @var{namespace} specified.
  1943. @item EINVAL
  1944. The @var{style} or @var{protocol} requested is not valid.
  1945. @item EMFILE
  1946. The process already has too many file descriptors open.
  1947. @item ENFILE
  1948. The system already has too many file descriptors open.
  1949. @item EACCES
  1950. @itemx EPERM
  1951. The process does not have the privilege to create a socket of the specified
  1952. @var{style} or @var{protocol}.
  1953. @item ENOBUFS
  1954. @itemx ENOMEM
  1955. Insufficient memory was available.
  1956. @end table
  1957. The file descriptor returned by the @code{socket} function supports both
  1958. read and write operations. However, like pipes, sockets do not support file
  1959. positioning operations.
  1960. @end deftypefun
  1961. For examples of how to call the @code{socket} function,
  1962. see @ref{Local Socket Example}, or @ref{Inet Example}.
  1963. @node Closing a Socket
  1964. @subsection Closing a Socket
  1965. @cindex socket, closing
  1966. @cindex closing a socket
  1967. @cindex shutting down a socket
  1968. @cindex socket shutdown
  1969. When you have finished using a socket, you can simply close its
  1970. file descriptor with @code{close}; see @ref{Opening and Closing Files}.
  1971. If there is still data waiting to be transmitted over the connection,
  1972. normally @code{close} tries to complete this transmission. You
  1973. can control this behavior using the @code{SO_LINGER} socket option to
  1974. specify a timeout period; see @ref{Socket Options}.
  1975. @pindex sys/socket.h
  1976. You can also shut down only reception or transmission on a
  1977. connection by calling @code{shutdown}, which is declared in
  1978. @file{sys/socket.h}.
  1979. @deftypefun int shutdown (int @var{socket}, int @var{how})
  1980. @standards{BSD, sys/socket.h}
  1981. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1982. The @code{shutdown} function shuts down the connection of socket
  1983. @var{socket}. The argument @var{how} specifies what action to
  1984. perform:
  1985. @vtable @code
  1986. @item SHUT_RD
  1987. Stop receiving data on the socket.
  1988. @item SHUT_WR
  1989. Indicate to the peer that no further data will be transmitted on the
  1990. socket. This indication is ordered with regard to past send
  1991. operations on the socket, and data pending at the time of the call is
  1992. still delivered.
  1993. @item SHUT_RDWR
  1994. Combine the actions of @code{SHUT_RD} and @code{SHUT_WR}.
  1995. @end vtable
  1996. The return value is @code{0} on success and @code{-1} on failure. The
  1997. following generic @code{errno} error conditions are defined for this
  1998. function:
  1999. @table @code
  2000. @item EBADF
  2001. @var{socket} is not a valid file descriptor.
  2002. @item ENOTSOCK
  2003. @var{socket} is not a socket.
  2004. @item ENOTCONN
  2005. @var{socket} is not connected.
  2006. @end table
  2007. @end deftypefun
  2008. Additional errors can be reported for specific socket types.
  2009. The exact impact of the @code{shutdown} function depends on the socket
  2010. protocol and its implementation. In portable code, the @code{shutdown}
  2011. function cannot be used on its own to gracefully terminate a connection
  2012. which is operated in full-duplex mode (with both peers sending data).
  2013. On Linux, when @code{SHUT_RD} is used to shut down a TCP socket, any
  2014. pending data in the incoming socket buffer and any data that arrives
  2015. subsequently is discarded, without reporting an error or generating a
  2016. TCP RST segment. Attempts to read data from this socket using
  2017. @code{recv} and similar functions (@pxref{Receiving Data}) return zero.
  2018. (Other systems may treat @code{SHUT_RD} with pending data as a data loss
  2019. event and generate RST segments. Linux @code{AF_LOCAL}/@code{AF_UNIX}
  2020. sockets also report errors to peers.)
  2021. Similarly, when @code{SHUT_WR} is used on a Linux TCP socket, a FIN
  2022. segment is sent to the peer, ordered after any data written previously
  2023. to the socket. After encountering the FIN segment, the peer will
  2024. recognize this as an end-of-stream condition.
  2025. @node Socket Pairs
  2026. @subsection Socket Pairs
  2027. @cindex creating a socket pair
  2028. @cindex socket pair
  2029. @cindex opening a socket pair
  2030. @pindex sys/socket.h
  2031. A @dfn{socket pair} consists of a pair of connected (but unnamed)
  2032. sockets. It is very similar to a pipe and is used in much the same
  2033. way. Socket pairs are created with the @code{socketpair} function,
  2034. declared in @file{sys/socket.h}. A socket pair is much like a pipe; the
  2035. main difference is that the socket pair is bidirectional, whereas the
  2036. pipe has one input-only end and one output-only end (@pxref{Pipes and
  2037. FIFOs}).
  2038. @deftypefun int socketpair (int @var{namespace}, int @var{style}, int @var{protocol}, int @var{filedes}@t{[2]})
  2039. @standards{BSD, sys/socket.h}
  2040. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  2041. This function creates a socket pair, returning the file descriptors in
  2042. @code{@var{filedes}[0]} and @code{@var{filedes}[1]}. The socket pair
  2043. is a full-duplex communications channel, so that both reading and writing
  2044. may be performed at either end.
  2045. The @var{namespace}, @var{style} and @var{protocol} arguments are
  2046. interpreted as for the @code{socket} function. @var{style} should be
  2047. one of the communication styles listed in @ref{Communication Styles}.
  2048. The @var{namespace} argument specifies the namespace, which must be
  2049. @code{AF_LOCAL} (@pxref{Local Namespace}); @var{protocol} specifies the
  2050. communications protocol, but zero is the only meaningful value.
  2051. If @var{style} specifies a connectionless communication style, then
  2052. the two sockets you get are not @emph{connected}, strictly speaking,
  2053. but each of them knows the other as the default destination address,
  2054. so they can send packets to each other.
  2055. The @code{socketpair} function returns @code{0} on success and @code{-1}
  2056. on failure. The following @code{errno} error conditions are defined
  2057. for this function:
  2058. @table @code
  2059. @item EMFILE
  2060. The process has too many file descriptors open.
  2061. @item EAFNOSUPPORT
  2062. The specified namespace is not supported.
  2063. @item EPROTONOSUPPORT
  2064. The specified protocol is not supported.
  2065. @item EOPNOTSUPP
  2066. The specified protocol does not support the creation of socket pairs.
  2067. @end table
  2068. @end deftypefun
  2069. @node Connections
  2070. @section Using Sockets with Connections
  2071. @cindex connection
  2072. @cindex client
  2073. @cindex server
  2074. The most common communication styles involve making a connection to a
  2075. particular other socket, and then exchanging data with that socket
  2076. over and over. Making a connection is asymmetric; one side (the
  2077. @dfn{client}) acts to request a connection, while the other side (the
  2078. @dfn{server}) makes a socket and waits for the connection request.
  2079. @iftex
  2080. @itemize @bullet
  2081. @item
  2082. @ref{Connecting}, describes what the client program must do to
  2083. initiate a connection with a server.
  2084. @item
  2085. @ref{Listening} and @ref{Accepting Connections} describe what the
  2086. server program must do to wait for and act upon connection requests
  2087. from clients.
  2088. @item
  2089. @ref{Transferring Data}, describes how data are transferred through the
  2090. connected socket.
  2091. @end itemize
  2092. @end iftex
  2093. @menu
  2094. * Connecting:: What the client program must do.
  2095. * Listening:: How a server program waits for requests.
  2096. * Accepting Connections:: What the server does when it gets a request.
  2097. * Who is Connected:: Getting the address of the
  2098. other side of a connection.
  2099. * Transferring Data:: How to send and receive data.
  2100. * Byte Stream Example:: An example program: a client for communicating
  2101. over a byte stream socket in the Internet namespace.
  2102. * Server Example:: A corresponding server program.
  2103. * Out-of-Band Data:: This is an advanced feature.
  2104. @end menu
  2105. @node Connecting
  2106. @subsection Making a Connection
  2107. @cindex connecting a socket
  2108. @cindex socket, connecting
  2109. @cindex socket, initiating a connection
  2110. @cindex socket, client actions
  2111. In making a connection, the client makes a connection while the server
  2112. waits for and accepts the connection. Here we discuss what the client
  2113. program must do with the @code{connect} function, which is declared in
  2114. @file{sys/socket.h}.
  2115. @deftypefun int connect (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
  2116. @standards{BSD, sys/socket.h}
  2117. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2118. The @code{connect} function initiates a connection from the socket
  2119. with file descriptor @var{socket} to the socket whose address is
  2120. specified by the @var{addr} and @var{length} arguments. (This socket
  2121. is typically on another machine, and it must be already set up as a
  2122. server.) @xref{Socket Addresses}, for information about how these
  2123. arguments are interpreted.
  2124. Normally, @code{connect} waits until the server responds to the request
  2125. before it returns. You can set nonblocking mode on the socket
  2126. @var{socket} to make @code{connect} return immediately without waiting
  2127. for the response. @xref{File Status Flags}, for information about
  2128. nonblocking mode.
  2129. @c !!! how do you tell when it has finished connecting? I suspect the
  2130. @c way you do it is select for writing.
  2131. The normal return value from @code{connect} is @code{0}. If an error
  2132. occurs, @code{connect} returns @code{-1}. The following @code{errno}
  2133. error conditions are defined for this function:
  2134. @table @code
  2135. @item EBADF
  2136. The socket @var{socket} is not a valid file descriptor.
  2137. @item ENOTSOCK
  2138. File descriptor @var{socket} is not a socket.
  2139. @item EADDRNOTAVAIL
  2140. The specified address is not available on the remote machine.
  2141. @item EAFNOSUPPORT
  2142. The namespace of the @var{addr} is not supported by this socket.
  2143. @item EISCONN
  2144. The socket @var{socket} is already connected.
  2145. @item ETIMEDOUT
  2146. The attempt to establish the connection timed out.
  2147. @item ECONNREFUSED
  2148. The server has actively refused to establish the connection.
  2149. @item ENETUNREACH
  2150. The network of the given @var{addr} isn't reachable from this host.
  2151. @item EADDRINUSE
  2152. The socket address of the given @var{addr} is already in use.
  2153. @item EINPROGRESS
  2154. The socket @var{socket} is non-blocking and the connection could not be
  2155. established immediately. You can determine when the connection is
  2156. completely established with @code{select}; @pxref{Waiting for I/O}.
  2157. Another @code{connect} call on the same socket, before the connection is
  2158. completely established, will fail with @code{EALREADY}.
  2159. @item EALREADY
  2160. The socket @var{socket} is non-blocking and already has a pending
  2161. connection in progress (see @code{EINPROGRESS} above).
  2162. @end table
  2163. This function is defined as a cancellation point in multi-threaded
  2164. programs, so one has to be prepared for this and make sure that
  2165. allocated resources (like memory, file descriptors, semaphores or
  2166. whatever) are freed even if the thread is canceled.
  2167. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2168. @end deftypefun
  2169. @node Listening
  2170. @subsection Listening for Connections
  2171. @cindex listening (sockets)
  2172. @cindex sockets, server actions
  2173. @cindex sockets, listening
  2174. Now let us consider what the server process must do to accept
  2175. connections on a socket. First it must use the @code{listen} function
  2176. to enable connection requests on the socket, and then accept each
  2177. incoming connection with a call to @code{accept} (@pxref{Accepting
  2178. Connections}). Once connection requests are enabled on a server socket,
  2179. the @code{select} function reports when the socket has a connection
  2180. ready to be accepted (@pxref{Waiting for I/O}).
  2181. The @code{listen} function is not allowed for sockets using
  2182. connectionless communication styles.
  2183. You can write a network server that does not even start running until a
  2184. connection to it is requested. @xref{Inetd Servers}.
  2185. In the Internet namespace, there are no special protection mechanisms
  2186. for controlling access to a port; any process on any machine
  2187. can make a connection to your server. If you want to restrict access to
  2188. your server, make it examine the addresses associated with connection
  2189. requests or implement some other handshaking or identification
  2190. protocol.
  2191. In the local namespace, the ordinary file protection bits control who has
  2192. access to connect to the socket.
  2193. @deftypefun int listen (int @var{socket}, int @var{n})
  2194. @standards{BSD, sys/socket.h}
  2195. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  2196. The @code{listen} function enables the socket @var{socket} to accept
  2197. connections, thus making it a server socket.
  2198. The argument @var{n} specifies the length of the queue for pending
  2199. connections. When the queue fills, new clients attempting to connect
  2200. fail with @code{ECONNREFUSED} until the server calls @code{accept} to
  2201. accept a connection from the queue.
  2202. The @code{listen} function returns @code{0} on success and @code{-1}
  2203. on failure. The following @code{errno} error conditions are defined
  2204. for this function:
  2205. @table @code
  2206. @item EBADF
  2207. The argument @var{socket} is not a valid file descriptor.
  2208. @item ENOTSOCK
  2209. The argument @var{socket} is not a socket.
  2210. @item EOPNOTSUPP
  2211. The socket @var{socket} does not support this operation.
  2212. @end table
  2213. @end deftypefun
  2214. @node Accepting Connections
  2215. @subsection Accepting Connections
  2216. @cindex sockets, accepting connections
  2217. @cindex accepting connections
  2218. When a server receives a connection request, it can complete the
  2219. connection by accepting the request. Use the function @code{accept}
  2220. to do this.
  2221. A socket that has been established as a server can accept connection
  2222. requests from multiple clients. The server's original socket
  2223. @emph{does not become part of the connection}; instead, @code{accept}
  2224. makes a new socket which participates in the connection.
  2225. @code{accept} returns the descriptor for this socket. The server's
  2226. original socket remains available for listening for further connection
  2227. requests.
  2228. The number of pending connection requests on a server socket is finite.
  2229. If connection requests arrive from clients faster than the server can
  2230. act upon them, the queue can fill up and additional requests are refused
  2231. with an @code{ECONNREFUSED} error. You can specify the maximum length of
  2232. this queue as an argument to the @code{listen} function, although the
  2233. system may also impose its own internal limit on the length of this
  2234. queue.
  2235. @deftypefun int accept (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length_ptr})
  2236. @standards{BSD, sys/socket.h}
  2237. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  2238. This function is used to accept a connection request on the server
  2239. socket @var{socket}.
  2240. The @code{accept} function waits if there are no connections pending,
  2241. unless the socket @var{socket} has nonblocking mode set. (You can use
  2242. @code{select} to wait for a pending connection, with a nonblocking
  2243. socket.) @xref{File Status Flags}, for information about nonblocking
  2244. mode.
  2245. The @var{addr} and @var{length-ptr} arguments are used to return
  2246. information about the name of the client socket that initiated the
  2247. connection. @xref{Socket Addresses}, for information about the format
  2248. of the information.
  2249. Accepting a connection does not make @var{socket} part of the
  2250. connection. Instead, it creates a new socket which becomes
  2251. connected. The normal return value of @code{accept} is the file
  2252. descriptor for the new socket.
  2253. After @code{accept}, the original socket @var{socket} remains open and
  2254. unconnected, and continues listening until you close it. You can
  2255. accept further connections with @var{socket} by calling @code{accept}
  2256. again.
  2257. If an error occurs, @code{accept} returns @code{-1}. The following
  2258. @code{errno} error conditions are defined for this function:
  2259. @table @code
  2260. @item EBADF
  2261. The @var{socket} argument is not a valid file descriptor.
  2262. @item ENOTSOCK
  2263. The descriptor @var{socket} argument is not a socket.
  2264. @item EOPNOTSUPP
  2265. The descriptor @var{socket} does not support this operation.
  2266. @item EWOULDBLOCK
  2267. @var{socket} has nonblocking mode set, and there are no pending
  2268. connections immediately available.
  2269. @end table
  2270. This function is defined as a cancellation point in multi-threaded
  2271. programs, so one has to be prepared for this and make sure that
  2272. allocated resources (like memory, file descriptors, semaphores or
  2273. whatever) are freed even if the thread is canceled.
  2274. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2275. @end deftypefun
  2276. The @code{accept} function is not allowed for sockets using
  2277. connectionless communication styles.
  2278. @node Who is Connected
  2279. @subsection Who is Connected to Me?
  2280. @deftypefun int getpeername (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
  2281. @standards{BSD, sys/socket.h}
  2282. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2283. The @code{getpeername} function returns the address of the socket that
  2284. @var{socket} is connected to; it stores the address in the memory space
  2285. specified by @var{addr} and @var{length-ptr}. It stores the length of
  2286. the address in @code{*@var{length-ptr}}.
  2287. @xref{Socket Addresses}, for information about the format of the
  2288. address. In some operating systems, @code{getpeername} works only for
  2289. sockets in the Internet domain.
  2290. The return value is @code{0} on success and @code{-1} on error. The
  2291. following @code{errno} error conditions are defined for this function:
  2292. @table @code
  2293. @item EBADF
  2294. The argument @var{socket} is not a valid file descriptor.
  2295. @item ENOTSOCK
  2296. The descriptor @var{socket} is not a socket.
  2297. @item ENOTCONN
  2298. The socket @var{socket} is not connected.
  2299. @item ENOBUFS
  2300. There are not enough internal buffers available.
  2301. @end table
  2302. @end deftypefun
  2303. @node Transferring Data
  2304. @subsection Transferring Data
  2305. @cindex reading from a socket
  2306. @cindex writing to a socket
  2307. Once a socket has been connected to a peer, you can use the ordinary
  2308. @code{read} and @code{write} operations (@pxref{I/O Primitives}) to
  2309. transfer data. A socket is a two-way communications channel, so read
  2310. and write operations can be performed at either end.
  2311. There are also some I/O modes that are specific to socket operations.
  2312. In order to specify these modes, you must use the @code{recv} and
  2313. @code{send} functions instead of the more generic @code{read} and
  2314. @code{write} functions. The @code{recv} and @code{send} functions take
  2315. an additional argument which you can use to specify various flags to
  2316. control special I/O modes. For example, you can specify the
  2317. @code{MSG_OOB} flag to read or write out-of-band data, the
  2318. @code{MSG_PEEK} flag to peek at input, or the @code{MSG_DONTROUTE} flag
  2319. to control inclusion of routing information on output.
  2320. @menu
  2321. * Sending Data:: Sending data with @code{send}.
  2322. * Receiving Data:: Reading data with @code{recv}.
  2323. * Socket Data Options:: Using @code{send} and @code{recv}.
  2324. @end menu
  2325. @node Sending Data
  2326. @subsubsection Sending Data
  2327. @pindex sys/socket.h
  2328. The @code{send} function is declared in the header file
  2329. @file{sys/socket.h}. If your @var{flags} argument is zero, you can just
  2330. as well use @code{write} instead of @code{send}; see @ref{I/O
  2331. Primitives}. If the socket was connected but the connection has broken,
  2332. you get a @code{SIGPIPE} signal for any use of @code{send} or
  2333. @code{write} (@pxref{Miscellaneous Signals}).
  2334. @deftypefun ssize_t send (int @var{socket}, const void *@var{buffer}, size_t @var{size}, int @var{flags})
  2335. @standards{BSD, sys/socket.h}
  2336. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2337. The @code{send} function is like @code{write}, but with the additional
  2338. flags @var{flags}. The possible values of @var{flags} are described
  2339. in @ref{Socket Data Options}.
  2340. This function returns the number of bytes transmitted, or @code{-1} on
  2341. failure. If the socket is nonblocking, then @code{send} (like
  2342. @code{write}) can return after sending just part of the data.
  2343. @xref{File Status Flags}, for information about nonblocking mode.
  2344. Note, however, that a successful return value merely indicates that
  2345. the message has been sent without error, not necessarily that it has
  2346. been received without error.
  2347. The following @code{errno} error conditions are defined for this function:
  2348. @table @code
  2349. @item EBADF
  2350. The @var{socket} argument is not a valid file descriptor.
  2351. @item EINTR
  2352. The operation was interrupted by a signal before any data was sent.
  2353. @xref{Interrupted Primitives}.
  2354. @item ENOTSOCK
  2355. The descriptor @var{socket} is not a socket.
  2356. @item EMSGSIZE
  2357. The socket type requires that the message be sent atomically, but the
  2358. message is too large for this to be possible.
  2359. @item EWOULDBLOCK
  2360. Nonblocking mode has been set on the socket, and the write operation
  2361. would block. (Normally @code{send} blocks until the operation can be
  2362. completed.)
  2363. @item ENOBUFS
  2364. There is not enough internal buffer space available.
  2365. @item ENOTCONN
  2366. You never connected this socket.
  2367. @item EPIPE
  2368. This socket was connected but the connection is now broken. In this
  2369. case, @code{send} generates a @code{SIGPIPE} signal first; if that
  2370. signal is ignored or blocked, or if its handler returns, then
  2371. @code{send} fails with @code{EPIPE}.
  2372. @end table
  2373. This function is defined as a cancellation point in multi-threaded
  2374. programs, so one has to be prepared for this and make sure that
  2375. allocated resources (like memory, file descriptors, semaphores or
  2376. whatever) are freed even if the thread is canceled.
  2377. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2378. @end deftypefun
  2379. @node Receiving Data
  2380. @subsubsection Receiving Data
  2381. @pindex sys/socket.h
  2382. The @code{recv} function is declared in the header file
  2383. @file{sys/socket.h}. If your @var{flags} argument is zero, you can
  2384. just as well use @code{read} instead of @code{recv}; see @ref{I/O
  2385. Primitives}.
  2386. @deftypefun ssize_t recv (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
  2387. @standards{BSD, sys/socket.h}
  2388. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2389. The @code{recv} function is like @code{read}, but with the additional
  2390. flags @var{flags}. The possible values of @var{flags} are described
  2391. in @ref{Socket Data Options}.
  2392. If nonblocking mode is set for @var{socket}, and no data are available to
  2393. be read, @code{recv} fails immediately rather than waiting. @xref{File
  2394. Status Flags}, for information about nonblocking mode.
  2395. This function returns the number of bytes received, or @code{-1} on failure.
  2396. The following @code{errno} error conditions are defined for this function:
  2397. @table @code
  2398. @item EBADF
  2399. The @var{socket} argument is not a valid file descriptor.
  2400. @item ENOTSOCK
  2401. The descriptor @var{socket} is not a socket.
  2402. @item EWOULDBLOCK
  2403. Nonblocking mode has been set on the socket, and the read operation
  2404. would block. (Normally, @code{recv} blocks until there is input
  2405. available to be read.)
  2406. @item EINTR
  2407. The operation was interrupted by a signal before any data was read.
  2408. @xref{Interrupted Primitives}.
  2409. @item ENOTCONN
  2410. You never connected this socket.
  2411. @end table
  2412. This function is defined as a cancellation point in multi-threaded
  2413. programs, so one has to be prepared for this and make sure that
  2414. allocated resources (like memory, file descriptors, semaphores or
  2415. whatever) are freed even if the thread is canceled.
  2416. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2417. @end deftypefun
  2418. @node Socket Data Options
  2419. @subsubsection Socket Data Options
  2420. @pindex sys/socket.h
  2421. The @var{flags} argument to @code{send} and @code{recv} is a bit
  2422. mask. You can bitwise-OR the values of the following macros together
  2423. to obtain a value for this argument. All are defined in the header
  2424. file @file{sys/socket.h}.
  2425. @deftypevr Macro int MSG_OOB
  2426. @standards{BSD, sys/socket.h}
  2427. Send or receive out-of-band data. @xref{Out-of-Band Data}.
  2428. @end deftypevr
  2429. @deftypevr Macro int MSG_PEEK
  2430. @standards{BSD, sys/socket.h}
  2431. Look at the data but don't remove it from the input queue. This is
  2432. only meaningful with input functions such as @code{recv}, not with
  2433. @code{send}.
  2434. @end deftypevr
  2435. @deftypevr Macro int MSG_DONTROUTE
  2436. @standards{BSD, sys/socket.h}
  2437. Don't include routing information in the message. This is only
  2438. meaningful with output operations, and is usually only of interest for
  2439. diagnostic or routing programs. We don't try to explain it here.
  2440. @end deftypevr
  2441. @node Byte Stream Example
  2442. @subsection Byte Stream Socket Example
  2443. Here is an example client program that makes a connection for a byte
  2444. stream socket in the Internet namespace. It doesn't do anything
  2445. particularly interesting once it has connected to the server; it just
  2446. sends a text string to the server and exits.
  2447. This program uses @code{init_sockaddr} to set up the socket address; see
  2448. @ref{Inet Example}.
  2449. @smallexample
  2450. @include inetcli.c.texi
  2451. @end smallexample
  2452. @node Server Example
  2453. @subsection Byte Stream Connection Server Example
  2454. The server end is much more complicated. Since we want to allow
  2455. multiple clients to be connected to the server at the same time, it
  2456. would be incorrect to wait for input from a single client by simply
  2457. calling @code{read} or @code{recv}. Instead, the right thing to do is
  2458. to use @code{select} (@pxref{Waiting for I/O}) to wait for input on
  2459. all of the open sockets. This also allows the server to deal with
  2460. additional connection requests.
  2461. This particular server doesn't do anything interesting once it has
  2462. gotten a message from a client. It does close the socket for that
  2463. client when it detects an end-of-file condition (resulting from the
  2464. client shutting down its end of the connection).
  2465. This program uses @code{make_socket} to set up the socket address; see
  2466. @ref{Inet Example}.
  2467. @smallexample
  2468. @include inetsrv.c.texi
  2469. @end smallexample
  2470. @node Out-of-Band Data
  2471. @subsection Out-of-Band Data
  2472. @cindex out-of-band data
  2473. @cindex high-priority data
  2474. Streams with connections permit @dfn{out-of-band} data that is
  2475. delivered with higher priority than ordinary data. Typically the
  2476. reason for sending out-of-band data is to send notice of an
  2477. exceptional condition. To send out-of-band data use
  2478. @code{send}, specifying the flag @code{MSG_OOB} (@pxref{Sending
  2479. Data}).
  2480. Out-of-band data are received with higher priority because the
  2481. receiving process need not read it in sequence; to read the next
  2482. available out-of-band data, use @code{recv} with the @code{MSG_OOB}
  2483. flag (@pxref{Receiving Data}). Ordinary read operations do not read
  2484. out-of-band data; they read only ordinary data.
  2485. @cindex urgent socket condition
  2486. When a socket finds that out-of-band data are on their way, it sends a
  2487. @code{SIGURG} signal to the owner process or process group of the
  2488. socket. You can specify the owner using the @code{F_SETOWN} command
  2489. to the @code{fcntl} function; see @ref{Interrupt Input}. You must
  2490. also establish a handler for this signal, as described in @ref{Signal
  2491. Handling}, in order to take appropriate action such as reading the
  2492. out-of-band data.
  2493. Alternatively, you can test for pending out-of-band data, or wait
  2494. until there is out-of-band data, using the @code{select} function; it
  2495. can wait for an exceptional condition on the socket. @xref{Waiting
  2496. for I/O}, for more information about @code{select}.
  2497. Notification of out-of-band data (whether with @code{SIGURG} or with
  2498. @code{select}) indicates that out-of-band data are on the way; the data
  2499. may not actually arrive until later. If you try to read the
  2500. out-of-band data before it arrives, @code{recv} fails with an
  2501. @code{EWOULDBLOCK} error.
  2502. Sending out-of-band data automatically places a ``mark'' in the stream
  2503. of ordinary data, showing where in the sequence the out-of-band data
  2504. ``would have been''. This is useful when the meaning of out-of-band
  2505. data is ``cancel everything sent so far''. Here is how you can test,
  2506. in the receiving process, whether any ordinary data was sent before
  2507. the mark:
  2508. @smallexample
  2509. success = ioctl (socket, SIOCATMARK, &atmark);
  2510. @end smallexample
  2511. The @code{integer} variable @var{atmark} is set to a nonzero value if
  2512. the socket's read pointer has reached the ``mark''.
  2513. @c Posix 1.g specifies sockatmark for this ioctl. sockatmark is not
  2514. @c implemented yet.
  2515. Here's a function to discard any ordinary data preceding the
  2516. out-of-band mark:
  2517. @smallexample
  2518. int
  2519. discard_until_mark (int socket)
  2520. @{
  2521. while (1)
  2522. @{
  2523. /* @r{This is not an arbitrary limit; any size will do.} */
  2524. char buffer[1024];
  2525. int atmark, success;
  2526. /* @r{If we have reached the mark, return.} */
  2527. success = ioctl (socket, SIOCATMARK, &atmark);
  2528. if (success < 0)
  2529. perror ("ioctl");
  2530. if (atmark)
  2531. return;
  2532. /* @r{Otherwise, read a bunch of ordinary data and discard it.}
  2533. @r{This is guaranteed not to read past the mark}
  2534. @r{if it starts before the mark.} */
  2535. success = read (socket, buffer, sizeof buffer);
  2536. if (success < 0)
  2537. perror ("read");
  2538. @}
  2539. @}
  2540. @end smallexample
  2541. If you don't want to discard the ordinary data preceding the mark, you
  2542. may need to read some of it anyway, to make room in internal system
  2543. buffers for the out-of-band data. If you try to read out-of-band data
  2544. and get an @code{EWOULDBLOCK} error, try reading some ordinary data
  2545. (saving it so that you can use it when you want it) and see if that
  2546. makes room. Here is an example:
  2547. @smallexample
  2548. struct buffer
  2549. @{
  2550. char *buf;
  2551. int size;
  2552. struct buffer *next;
  2553. @};
  2554. /* @r{Read the out-of-band data from SOCKET and return it}
  2555. @r{as a `struct buffer', which records the address of the data}
  2556. @r{and its size.}
  2557. @r{It may be necessary to read some ordinary data}
  2558. @r{in order to make room for the out-of-band data.}
  2559. @r{If so, the ordinary data are saved as a chain of buffers}
  2560. @r{found in the `next' field of the value.} */
  2561. struct buffer *
  2562. read_oob (int socket)
  2563. @{
  2564. struct buffer *tail = 0;
  2565. struct buffer *list = 0;
  2566. while (1)
  2567. @{
  2568. /* @r{This is an arbitrary limit.}
  2569. @r{Does anyone know how to do this without a limit?} */
  2570. #define BUF_SZ 1024
  2571. char *buf = (char *) xmalloc (BUF_SZ);
  2572. int success;
  2573. int atmark;
  2574. /* @r{Try again to read the out-of-band data.} */
  2575. success = recv (socket, buf, BUF_SZ, MSG_OOB);
  2576. if (success >= 0)
  2577. @{
  2578. /* @r{We got it, so return it.} */
  2579. struct buffer *link
  2580. = (struct buffer *) xmalloc (sizeof (struct buffer));
  2581. link->buf = buf;
  2582. link->size = success;
  2583. link->next = list;
  2584. return link;
  2585. @}
  2586. /* @r{If we fail, see if we are at the mark.} */
  2587. success = ioctl (socket, SIOCATMARK, &atmark);
  2588. if (success < 0)
  2589. perror ("ioctl");
  2590. if (atmark)
  2591. @{
  2592. /* @r{At the mark; skipping past more ordinary data cannot help.}
  2593. @r{So just wait a while.} */
  2594. sleep (1);
  2595. continue;
  2596. @}
  2597. /* @r{Otherwise, read a bunch of ordinary data and save it.}
  2598. @r{This is guaranteed not to read past the mark}
  2599. @r{if it starts before the mark.} */
  2600. success = read (socket, buf, BUF_SZ);
  2601. if (success < 0)
  2602. perror ("read");
  2603. /* @r{Save this data in the buffer list.} */
  2604. @{
  2605. struct buffer *link
  2606. = (struct buffer *) xmalloc (sizeof (struct buffer));
  2607. link->buf = buf;
  2608. link->size = success;
  2609. /* @r{Add the new link to the end of the list.} */
  2610. if (tail)
  2611. tail->next = link;
  2612. else
  2613. list = link;
  2614. tail = link;
  2615. @}
  2616. @}
  2617. @}
  2618. @end smallexample
  2619. @node Datagrams
  2620. @section Datagram Socket Operations
  2621. @cindex datagram socket
  2622. This section describes how to use communication styles that don't use
  2623. connections (styles @code{SOCK_DGRAM} and @code{SOCK_RDM}). Using
  2624. these styles, you group data into packets and each packet is an
  2625. independent communication. You specify the destination for each
  2626. packet individually.
  2627. Datagram packets are like letters: you send each one independently
  2628. with its own destination address, and they may arrive in the wrong
  2629. order or not at all.
  2630. The @code{listen} and @code{accept} functions are not allowed for
  2631. sockets using connectionless communication styles.
  2632. @menu
  2633. * Sending Datagrams:: Sending packets on a datagram socket.
  2634. * Receiving Datagrams:: Receiving packets on a datagram socket.
  2635. * Datagram Example:: An example program: packets sent over a
  2636. datagram socket in the local namespace.
  2637. * Example Receiver:: Another program, that receives those packets.
  2638. @end menu
  2639. @node Sending Datagrams
  2640. @subsection Sending Datagrams
  2641. @cindex sending a datagram
  2642. @cindex transmitting datagrams
  2643. @cindex datagrams, transmitting
  2644. @pindex sys/socket.h
  2645. The normal way of sending data on a datagram socket is by using the
  2646. @code{sendto} function, declared in @file{sys/socket.h}.
  2647. You can call @code{connect} on a datagram socket, but this only
  2648. specifies a default destination for further data transmission on the
  2649. socket. When a socket has a default destination you can use
  2650. @code{send} (@pxref{Sending Data}) or even @code{write} (@pxref{I/O
  2651. Primitives}) to send a packet there. You can cancel the default
  2652. destination by calling @code{connect} using an address format of
  2653. @code{AF_UNSPEC} in the @var{addr} argument. @xref{Connecting}, for
  2654. more information about the @code{connect} function.
  2655. @deftypefun ssize_t sendto (int @var{socket}, const void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t @var{length})
  2656. @standards{BSD, sys/socket.h}
  2657. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2658. The @code{sendto} function transmits the data in the @var{buffer}
  2659. through the socket @var{socket} to the destination address specified
  2660. by the @var{addr} and @var{length} arguments. The @var{size} argument
  2661. specifies the number of bytes to be transmitted.
  2662. The @var{flags} are interpreted the same way as for @code{send}; see
  2663. @ref{Socket Data Options}.
  2664. The return value and error conditions are also the same as for
  2665. @code{send}, but you cannot rely on the system to detect errors and
  2666. report them; the most common error is that the packet is lost or there
  2667. is no-one at the specified address to receive it, and the operating
  2668. system on your machine usually does not know this.
  2669. It is also possible for one call to @code{sendto} to report an error
  2670. owing to a problem related to a previous call.
  2671. This function is defined as a cancellation point in multi-threaded
  2672. programs, so one has to be prepared for this and make sure that
  2673. allocated resources (like memory, file descriptors, semaphores or
  2674. whatever) are freed even if the thread is canceled.
  2675. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2676. @end deftypefun
  2677. @node Receiving Datagrams
  2678. @subsection Receiving Datagrams
  2679. @cindex receiving datagrams
  2680. The @code{recvfrom} function reads a packet from a datagram socket and
  2681. also tells you where it was sent from. This function is declared in
  2682. @file{sys/socket.h}.
  2683. @deftypefun ssize_t recvfrom (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
  2684. @standards{BSD, sys/socket.h}
  2685. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2686. The @code{recvfrom} function reads one packet from the socket
  2687. @var{socket} into the buffer @var{buffer}. The @var{size} argument
  2688. specifies the maximum number of bytes to be read.
  2689. If the packet is longer than @var{size} bytes, then you get the first
  2690. @var{size} bytes of the packet and the rest of the packet is lost.
  2691. There's no way to read the rest of the packet. Thus, when you use a
  2692. packet protocol, you must always know how long a packet to expect.
  2693. The @var{addr} and @var{length-ptr} arguments are used to return the
  2694. address where the packet came from. @xref{Socket Addresses}. For a
  2695. socket in the local domain the address information won't be meaningful,
  2696. since you can't read the address of such a socket (@pxref{Local
  2697. Namespace}). You can specify a null pointer as the @var{addr} argument
  2698. if you are not interested in this information.
  2699. The @var{flags} are interpreted the same way as for @code{recv}
  2700. (@pxref{Socket Data Options}). The return value and error conditions
  2701. are also the same as for @code{recv}.
  2702. This function is defined as a cancellation point in multi-threaded
  2703. programs, so one has to be prepared for this and make sure that
  2704. allocated resources (like memory, file descriptors, semaphores or
  2705. whatever) are freed even if the thread is canceled.
  2706. @c @xref{pthread_cleanup_push}, for a method how to do this.
  2707. @end deftypefun
  2708. You can use plain @code{recv} (@pxref{Receiving Data}) instead of
  2709. @code{recvfrom} if you don't need to find out who sent the packet
  2710. (either because you know where it should come from or because you
  2711. treat all possible senders alike). Even @code{read} can be used if
  2712. you don't want to specify @var{flags} (@pxref{I/O Primitives}).
  2713. If you need more flexibility and/or control over sending and receiving
  2714. packets, see @code{sendmsg} and @code{recvmsg} (@pxref{Other Socket APIs}).
  2715. @node Datagram Example
  2716. @subsection Datagram Socket Example
  2717. Here is a set of example programs that send messages over a datagram
  2718. stream in the local namespace. Both the client and server programs use
  2719. the @code{make_named_socket} function that was presented in @ref{Local
  2720. Socket Example}, to create and name their sockets.
  2721. First, here is the server program. It sits in a loop waiting for
  2722. messages to arrive, bouncing each message back to the sender.
  2723. Obviously this isn't a particularly useful program, but it does show
  2724. the general ideas involved.
  2725. @smallexample
  2726. @include filesrv.c.texi
  2727. @end smallexample
  2728. @node Example Receiver
  2729. @subsection Example of Reading Datagrams
  2730. Here is the client program corresponding to the server above.
  2731. It sends a datagram to the server and then waits for a reply. Notice
  2732. that the socket for the client (as well as for the server) in this
  2733. example has to be given a name. This is so that the server can direct
  2734. a message back to the client. Since the socket has no associated
  2735. connection state, the only way the server can do this is by
  2736. referencing the name of the client.
  2737. @smallexample
  2738. @include filecli.c.texi
  2739. @end smallexample
  2740. Keep in mind that datagram socket communications are unreliable. In
  2741. this example, the client program waits indefinitely if the message
  2742. never reaches the server or if the server's response never comes
  2743. back. It's up to the user running the program to kill and restart
  2744. it if desired. A more automatic solution could be to use
  2745. @code{select} (@pxref{Waiting for I/O}) to establish a timeout period
  2746. for the reply, and in case of timeout either re-send the message or
  2747. shut down the socket and exit.
  2748. @node Inetd
  2749. @section The @code{inetd} Daemon
  2750. We've explained above how to write a server program that does its own
  2751. listening. Such a server must already be running in order for anyone
  2752. to connect to it.
  2753. Another way to provide a service on an Internet port is to let the daemon
  2754. program @code{inetd} do the listening. @code{inetd} is a program that
  2755. runs all the time and waits (using @code{select}) for messages on a
  2756. specified set of ports. When it receives a message, it accepts the
  2757. connection (if the socket style calls for connections) and then forks a
  2758. child process to run the corresponding server program. You specify the
  2759. ports and their programs in the file @file{/etc/inetd.conf}.
  2760. @menu
  2761. * Inetd Servers::
  2762. * Configuring Inetd::
  2763. @end menu
  2764. @node Inetd Servers
  2765. @subsection @code{inetd} Servers
  2766. Writing a server program to be run by @code{inetd} is very simple. Each time
  2767. someone requests a connection to the appropriate port, a new server
  2768. process starts. The connection already exists at this time; the
  2769. socket is available as the standard input descriptor and as the
  2770. standard output descriptor (descriptors 0 and 1) in the server
  2771. process. Thus the server program can begin reading and writing data
  2772. right away. Often the program needs only the ordinary I/O facilities;
  2773. in fact, a general-purpose filter program that knows nothing about
  2774. sockets can work as a byte stream server run by @code{inetd}.
  2775. You can also use @code{inetd} for servers that use connectionless
  2776. communication styles. For these servers, @code{inetd} does not try to accept
  2777. a connection since no connection is possible. It just starts the
  2778. server program, which can read the incoming datagram packet from
  2779. descriptor 0. The server program can handle one request and then
  2780. exit, or you can choose to write it to keep reading more requests
  2781. until no more arrive, and then exit. You must specify which of these
  2782. two techniques the server uses when you configure @code{inetd}.
  2783. @node Configuring Inetd
  2784. @subsection Configuring @code{inetd}
  2785. The file @file{/etc/inetd.conf} tells @code{inetd} which ports to listen to
  2786. and what server programs to run for them. Normally each entry in the
  2787. file is one line, but you can split it onto multiple lines provided
  2788. all but the first line of the entry start with whitespace. Lines that
  2789. start with @samp{#} are comments.
  2790. Here are two standard entries in @file{/etc/inetd.conf}:
  2791. @smallexample
  2792. ftp stream tcp nowait root /libexec/ftpd ftpd
  2793. talk dgram udp wait root /libexec/talkd talkd
  2794. @end smallexample
  2795. An entry has this format:
  2796. @smallexample
  2797. @var{service} @var{style} @var{protocol} @var{wait} @var{username} @var{program} @var{arguments}
  2798. @end smallexample
  2799. The @var{service} field says which service this program provides. It
  2800. should be the name of a service defined in @file{/etc/services}.
  2801. @code{inetd} uses @var{service} to decide which port to listen on for
  2802. this entry.
  2803. The fields @var{style} and @var{protocol} specify the communication
  2804. style and the protocol to use for the listening socket. The style
  2805. should be the name of a communication style, converted to lower case
  2806. and with @samp{SOCK_} deleted---for example, @samp{stream} or
  2807. @samp{dgram}. @var{protocol} should be one of the protocols listed in
  2808. @file{/etc/protocols}. The typical protocol names are @samp{tcp} for
  2809. byte stream connections and @samp{udp} for unreliable datagrams.
  2810. The @var{wait} field should be either @samp{wait} or @samp{nowait}.
  2811. Use @samp{wait} if @var{style} is a connectionless style and the
  2812. server, once started, handles multiple requests as they come in.
  2813. Use @samp{nowait} if @code{inetd} should start a new process for each message
  2814. or request that comes in. If @var{style} uses connections, then
  2815. @var{wait} @strong{must} be @samp{nowait}.
  2816. @var{user} is the user name that the server should run as. @code{inetd} runs
  2817. as root, so it can set the user ID of its children arbitrarily. It's
  2818. best to avoid using @samp{root} for @var{user} if you can; but some
  2819. servers, such as Telnet and FTP, read a username and passphrase
  2820. themselves. These servers need to be root initially so they can log
  2821. in as commanded by the data coming over the network.
  2822. @var{program} together with @var{arguments} specifies the command to
  2823. run to start the server. @var{program} should be an absolute file
  2824. name specifying the executable file to run. @var{arguments} consists
  2825. of any number of whitespace-separated words, which become the
  2826. command-line arguments of @var{program}. The first word in
  2827. @var{arguments} is argument zero, which should by convention be the
  2828. program name itself (sans directories).
  2829. If you edit @file{/etc/inetd.conf}, you can tell @code{inetd} to reread the
  2830. file and obey its new contents by sending the @code{inetd} process the
  2831. @code{SIGHUP} signal. You'll have to use @code{ps} to determine the
  2832. process ID of the @code{inetd} process as it is not fixed.
  2833. @c !!! could document /etc/inetd.sec
  2834. @node Socket Options
  2835. @section Socket Options
  2836. @cindex socket options
  2837. This section describes how to read or set various options that modify
  2838. the behavior of sockets and their underlying communications protocols.
  2839. @cindex level, for socket options
  2840. @cindex socket option level
  2841. When you are manipulating a socket option, you must specify which
  2842. @dfn{level} the option pertains to. This describes whether the option
  2843. applies to the socket interface, or to a lower-level communications
  2844. protocol interface.
  2845. @menu
  2846. * Socket Option Functions:: The basic functions for setting and getting
  2847. socket options.
  2848. * Socket-Level Options:: Details of the options at the socket level.
  2849. @end menu
  2850. @node Socket Option Functions
  2851. @subsection Socket Option Functions
  2852. @pindex sys/socket.h
  2853. Here are the functions for examining and modifying socket options.
  2854. They are declared in @file{sys/socket.h}.
  2855. @deftypefun int getsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t *@var{optlen-ptr})
  2856. @standards{BSD, sys/socket.h}
  2857. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2858. The @code{getsockopt} function gets information about the value of
  2859. option @var{optname} at level @var{level} for socket @var{socket}.
  2860. The option value is stored in the buffer that @var{optval} points to.
  2861. Before the call, you should supply in @code{*@var{optlen-ptr}} the
  2862. size of this buffer; on return, it contains the number of bytes of
  2863. information actually stored in the buffer.
  2864. Most options interpret the @var{optval} buffer as a single @code{int}
  2865. value.
  2866. The actual return value of @code{getsockopt} is @code{0} on success
  2867. and @code{-1} on failure. The following @code{errno} error conditions
  2868. are defined:
  2869. @table @code
  2870. @item EBADF
  2871. The @var{socket} argument is not a valid file descriptor.
  2872. @item ENOTSOCK
  2873. The descriptor @var{socket} is not a socket.
  2874. @item ENOPROTOOPT
  2875. The @var{optname} doesn't make sense for the given @var{level}.
  2876. @end table
  2877. @end deftypefun
  2878. @deftypefun int setsockopt (int @var{socket}, int @var{level}, int @var{optname}, const void *@var{optval}, socklen_t @var{optlen})
  2879. @standards{BSD, sys/socket.h}
  2880. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2881. This function is used to set the socket option @var{optname} at level
  2882. @var{level} for socket @var{socket}. The value of the option is passed
  2883. in the buffer @var{optval} of size @var{optlen}.
  2884. @c Argh. -zw
  2885. @iftex
  2886. @hfuzz 6pt
  2887. The return value and error codes for @code{setsockopt} are the same as
  2888. for @code{getsockopt}.
  2889. @end iftex
  2890. @ifinfo
  2891. The return value and error codes for @code{setsockopt} are the same as
  2892. for @code{getsockopt}.
  2893. @end ifinfo
  2894. @end deftypefun
  2895. @node Socket-Level Options
  2896. @subsection Socket-Level Options
  2897. @deftypevr Constant int SOL_SOCKET
  2898. @standards{BSD, sys/socket.h}
  2899. Use this constant as the @var{level} argument to @code{getsockopt} or
  2900. @code{setsockopt} to manipulate the socket-level options described in
  2901. this section.
  2902. @end deftypevr
  2903. @pindex sys/socket.h
  2904. @noindent
  2905. Here is a table of socket-level option names; all are defined in the
  2906. header file @file{sys/socket.h}.
  2907. @vtable @code
  2908. @item SO_DEBUG
  2909. @standards{BSD, sys/socket.h}
  2910. @c Extra blank line here makes the table look better.
  2911. This option toggles recording of debugging information in the underlying
  2912. protocol modules. The value has type @code{int}; a nonzero value means
  2913. ``yes''.
  2914. @c !!! should say how this is used
  2915. @c OK, anyone who knows, please explain.
  2916. @item SO_REUSEADDR
  2917. @standards{BSD, sys/socket.h}
  2918. This option controls whether @code{bind} (@pxref{Setting Address})
  2919. should permit reuse of local addresses for this socket. If you enable
  2920. this option, you can actually have two sockets with the same Internet
  2921. port number; but the system won't allow you to use the two
  2922. identically-named sockets in a way that would confuse the Internet. The
  2923. reason for this option is that some higher-level Internet protocols,
  2924. including FTP, require you to keep reusing the same port number.
  2925. The value has type @code{int}; a nonzero value means ``yes''.
  2926. @item SO_KEEPALIVE
  2927. @standards{BSD, sys/socket.h}
  2928. This option controls whether the underlying protocol should
  2929. periodically transmit messages on a connected socket. If the peer
  2930. fails to respond to these messages, the connection is considered
  2931. broken. The value has type @code{int}; a nonzero value means
  2932. ``yes''.
  2933. @item SO_DONTROUTE
  2934. @standards{BSD, sys/socket.h}
  2935. This option controls whether outgoing messages bypass the normal
  2936. message routing facilities. If set, messages are sent directly to the
  2937. network interface instead. The value has type @code{int}; a nonzero
  2938. value means ``yes''.
  2939. @item SO_LINGER
  2940. @standards{BSD, sys/socket.h}
  2941. This option specifies what should happen when the socket of a type
  2942. that promises reliable delivery still has untransmitted messages when
  2943. it is closed; see @ref{Closing a Socket}. The value has type
  2944. @code{struct linger}.
  2945. @deftp {Data Type} {struct linger}
  2946. @standards{BSD, sys/socket.h}
  2947. This structure type has the following members:
  2948. @table @code
  2949. @item int l_onoff
  2950. This field is interpreted as a boolean. If nonzero, @code{close}
  2951. blocks until the data are transmitted or the timeout period has expired.
  2952. @item int l_linger
  2953. This specifies the timeout period, in seconds.
  2954. @end table
  2955. @end deftp
  2956. @item SO_BROADCAST
  2957. @standards{BSD, sys/socket.h}
  2958. This option controls whether datagrams may be broadcast from the socket.
  2959. The value has type @code{int}; a nonzero value means ``yes''.
  2960. @item SO_OOBINLINE
  2961. @standards{BSD, sys/socket.h}
  2962. If this option is set, out-of-band data received on the socket is
  2963. placed in the normal input queue. This permits it to be read using
  2964. @code{read} or @code{recv} without specifying the @code{MSG_OOB}
  2965. flag. @xref{Out-of-Band Data}. The value has type @code{int}; a
  2966. nonzero value means ``yes''.
  2967. @item SO_SNDBUF
  2968. @standards{BSD, sys/socket.h}
  2969. This option gets or sets the size of the output buffer. The value is a
  2970. @code{size_t}, which is the size in bytes.
  2971. @item SO_RCVBUF
  2972. @standards{BSD, sys/socket.h}
  2973. This option gets or sets the size of the input buffer. The value is a
  2974. @code{size_t}, which is the size in bytes.
  2975. @item SO_STYLE
  2976. @itemx SO_TYPE
  2977. @standards{GNU, sys/socket.h}
  2978. @standardsx{SO_TYPE, BSD, sys/socket.h}
  2979. This option can be used with @code{getsockopt} only. It is used to
  2980. get the socket's communication style. @code{SO_TYPE} is the
  2981. historical name, and @code{SO_STYLE} is the preferred name in GNU.
  2982. The value has type @code{int} and its value designates a communication
  2983. style; see @ref{Communication Styles}.
  2984. @item SO_ERROR
  2985. @standards{BSD, sys/socket.h}
  2986. @c Extra blank line here makes the table look better.
  2987. This option can be used with @code{getsockopt} only. It is used to reset
  2988. the error status of the socket. The value is an @code{int}, which represents
  2989. the previous error status.
  2990. @c !!! what is "socket error status"? this is never defined.
  2991. @end vtable
  2992. @node Networks Database
  2993. @section Networks Database
  2994. @cindex networks database
  2995. @cindex converting network number to network name
  2996. @cindex converting network name to network number
  2997. @pindex /etc/networks
  2998. @pindex netdb.h
  2999. Many systems come with a database that records a list of networks known
  3000. to the system developer. This is usually kept either in the file
  3001. @file{/etc/networks} or in an equivalent from a name server. This data
  3002. base is useful for routing programs such as @code{route}, but it is not
  3003. useful for programs that simply communicate over the network. We
  3004. provide functions to access this database, which are declared in
  3005. @file{netdb.h}.
  3006. @deftp {Data Type} {struct netent}
  3007. @standards{BSD, netdb.h}
  3008. This data type is used to represent information about entries in the
  3009. networks database. It has the following members:
  3010. @table @code
  3011. @item char *n_name
  3012. This is the ``official'' name of the network.
  3013. @item char **n_aliases
  3014. These are alternative names for the network, represented as a vector
  3015. of strings. A null pointer terminates the array.
  3016. @item int n_addrtype
  3017. This is the type of the network number; this is always equal to
  3018. @code{AF_INET} for Internet networks.
  3019. @item unsigned long int n_net
  3020. This is the network number. Network numbers are returned in host
  3021. byte order; see @ref{Byte Order}.
  3022. @end table
  3023. @end deftp
  3024. Use the @code{getnetbyname} or @code{getnetbyaddr} functions to search
  3025. the networks database for information about a specific network. The
  3026. information is returned in a statically-allocated structure; you must
  3027. copy the information if you need to save it.
  3028. @deftypefun {struct netent *} getnetbyname (const char *@var{name})
  3029. @standards{BSD, netdb.h}
  3030. @safety{@prelim{}@mtunsafe{@mtasurace{:netbyname} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  3031. @c getnetbyname =~ getpwuid @mtasurace:netbyname @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3032. @c libc_lock_lock dup @asulock @aculock
  3033. @c malloc dup @ascuheap @acsmem
  3034. @c getnetbyname_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3035. @c realloc dup @ascuheap @acsmem
  3036. @c free dup @ascuheap @acsmem
  3037. @c libc_lock_unlock dup @aculock
  3038. @c
  3039. @c getnetbyname_r =~ getpwuid_r @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3040. @c no nscd support
  3041. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  3042. @c nss_networks_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3043. @c *fct.l -> _nss_*_getnetbyname_r @ascuplugin
  3044. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3045. The @code{getnetbyname} function returns information about the network
  3046. named @var{name}. It returns a null pointer if there is no such
  3047. network.
  3048. @end deftypefun
  3049. @deftypefun {struct netent *} getnetbyaddr (uint32_t @var{net}, int @var{type})
  3050. @standards{BSD, netdb.h}
  3051. @safety{@prelim{}@mtunsafe{@mtasurace{:netbyaddr} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  3052. @c getnetbyaddr =~ getpwuid @mtasurace:netbyaddr @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3053. @c libc_lock_lock dup @asulock @aculock
  3054. @c malloc dup @ascuheap @acsmem
  3055. @c getnetbyaddr_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3056. @c realloc dup @ascuheap @acsmem
  3057. @c free dup @ascuheap @acsmem
  3058. @c libc_lock_unlock dup @aculock
  3059. @c
  3060. @c getnetbyaddr_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3061. @c no nscd support
  3062. @c nss_networks_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3063. @c *fct.l -> _nss_*_getnetbyaddr_r @ascuplugin
  3064. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3065. The @code{getnetbyaddr} function returns information about the network
  3066. of type @var{type} with number @var{net}. You should specify a value of
  3067. @code{AF_INET} for the @var{type} argument for Internet networks.
  3068. @code{getnetbyaddr} returns a null pointer if there is no such
  3069. network.
  3070. @end deftypefun
  3071. You can also scan the networks database using @code{setnetent},
  3072. @code{getnetent} and @code{endnetent}. Be careful when using these
  3073. functions because they are not reentrant.
  3074. @deftypefun void setnetent (int @var{stayopen})
  3075. @standards{BSD, netdb.h}
  3076. @safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  3077. @c setnetent @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3078. @c libc_lock_lock dup @asulock @aculock
  3079. @c nss_setent(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3080. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  3081. @c setup(nss_networks_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3082. @c *lookup_fct = nss_networks_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3083. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3084. @c *fct.f @mtasurace:netent @ascuplugin
  3085. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3086. @c libc_lock_unlock dup @aculock
  3087. This function opens and rewinds the networks database.
  3088. If the @var{stayopen} argument is nonzero, this sets a flag so that
  3089. subsequent calls to @code{getnetbyname} or @code{getnetbyaddr} will
  3090. not close the database (as they usually would). This makes for more
  3091. efficiency if you call those functions several times, by avoiding
  3092. reopening the database for each call.
  3093. @end deftypefun
  3094. @deftypefun {struct netent *} getnetent (void)
  3095. @standards{BSD, netdb.h}
  3096. @safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtasurace{:netentbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  3097. @c getnetent @mtasurace:netent @mtasurace:netentbuf @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3098. @c libc_lock_lock dup @asulock @aculock
  3099. @c nss_getent(getnetent_r) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3100. @c malloc dup @ascuheap @acsmem
  3101. @c *func = getnetent_r dup @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3102. @c realloc dup @ascuheap @acsmem
  3103. @c free dup @ascuheap @acsmem
  3104. @c libc_lock_unlock dup @aculock
  3105. @c
  3106. @c getnetent_r @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3107. @c libc_lock_lock dup @asulock @aculock
  3108. @c nss_getent_r(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3109. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  3110. @c setup(nss_networks_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3111. @c *fct.f @mtasurace:servent @ascuplugin
  3112. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3113. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3114. @c *sfct.f @mtasurace:netent @ascuplugin
  3115. @c libc_lock_unlock dup @aculock
  3116. This function returns the next entry in the networks database. It
  3117. returns a null pointer if there are no more entries.
  3118. @end deftypefun
  3119. @deftypefun void endnetent (void)
  3120. @standards{BSD, netdb.h}
  3121. @safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  3122. @c endnetent @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3123. @c libc_lock_lock @asulock @aculock
  3124. @c nss_endent(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3125. @c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  3126. @c setup(nss_networks_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3127. @c *fct.f @mtasurace:netent @ascuplugin
  3128. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  3129. @c libc_lock_unlock @aculock
  3130. This function closes the networks database.
  3131. @end deftypefun
  3132. @node Other Socket APIs
  3133. @section Other Socket APIs
  3134. @deftp {Data Type} {struct msghdr}
  3135. @standards{BSD, sys/socket.h}
  3136. @end deftp
  3137. @deftypefun ssize_t sendmsg (int @var{socket}, const struct msghdr *@var{message}, int @var{flags})
  3138. @manpagefunctionstub{sendmsg,2}
  3139. @end deftypefun
  3140. @deftypefun ssize_t recvmsg (int @var{socket}, struct msghdr *@var{message}, int @var{flags})
  3141. @manpagefunctionstub{recvmsg,2}
  3142. @end deftypefun