socket.texi 153 KB

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