socket_ops.ipp 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573
  1. //
  2. // detail/impl/socket_ops.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
  11. #define BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cctype>
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <cstring>
  20. #include <cerrno>
  21. #include <new>
  22. #include <boost/asio/detail/assert.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/error.hpp>
  25. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  26. # include <codecvt>
  27. # include <locale>
  28. # include <string>
  29. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  30. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) \
  31. || defined(__MACH__) && defined(__APPLE__)
  32. # if defined(BOOST_ASIO_HAS_PTHREADS)
  33. # include <pthread.h>
  34. # endif // defined(BOOST_ASIO_HAS_PTHREADS)
  35. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  36. // || defined(__MACH__) && defined(__APPLE__)
  37. #include <boost/asio/detail/push_options.hpp>
  38. namespace boost {
  39. namespace asio {
  40. namespace detail {
  41. namespace socket_ops {
  42. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  43. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  44. struct msghdr { int msg_namelen; };
  45. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  46. #if defined(__hpux)
  47. // HP-UX doesn't declare these functions extern "C", so they are declared again
  48. // here to avoid linker errors about undefined symbols.
  49. extern "C" char* if_indextoname(unsigned int, char*);
  50. extern "C" unsigned int if_nametoindex(const char*);
  51. #endif // defined(__hpux)
  52. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  53. inline void clear_last_error()
  54. {
  55. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  56. WSASetLastError(0);
  57. #else
  58. errno = 0;
  59. #endif
  60. }
  61. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  62. template <typename ReturnType>
  63. inline ReturnType error_wrapper(ReturnType return_value,
  64. boost::system::error_code& ec)
  65. {
  66. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  67. ec = boost::system::error_code(WSAGetLastError(),
  68. boost::asio::error::get_system_category());
  69. #else
  70. ec = boost::system::error_code(errno,
  71. boost::asio::error::get_system_category());
  72. #endif
  73. return return_value;
  74. }
  75. template <typename SockLenType>
  76. inline socket_type call_accept(SockLenType msghdr::*,
  77. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  78. {
  79. SockLenType tmp_addrlen = addrlen ? (SockLenType)*addrlen : 0;
  80. socket_type result = ::accept(s, addr, addrlen ? &tmp_addrlen : 0);
  81. if (addrlen)
  82. *addrlen = (std::size_t)tmp_addrlen;
  83. return result;
  84. }
  85. socket_type accept(socket_type s, socket_addr_type* addr,
  86. std::size_t* addrlen, boost::system::error_code& ec)
  87. {
  88. if (s == invalid_socket)
  89. {
  90. ec = boost::asio::error::bad_descriptor;
  91. return invalid_socket;
  92. }
  93. clear_last_error();
  94. socket_type new_s = error_wrapper(call_accept(
  95. &msghdr::msg_namelen, s, addr, addrlen), ec);
  96. if (new_s == invalid_socket)
  97. return new_s;
  98. #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
  99. int optval = 1;
  100. int result = error_wrapper(::setsockopt(new_s,
  101. SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
  102. if (result != 0)
  103. {
  104. ::close(new_s);
  105. return invalid_socket;
  106. }
  107. #endif
  108. ec = boost::system::error_code();
  109. return new_s;
  110. }
  111. socket_type sync_accept(socket_type s, state_type state,
  112. socket_addr_type* addr, std::size_t* addrlen, boost::system::error_code& ec)
  113. {
  114. // Accept a socket.
  115. for (;;)
  116. {
  117. // Try to complete the operation without blocking.
  118. socket_type new_socket = socket_ops::accept(s, addr, addrlen, ec);
  119. // Check if operation succeeded.
  120. if (new_socket != invalid_socket)
  121. return new_socket;
  122. // Operation failed.
  123. if (ec == boost::asio::error::would_block
  124. || ec == boost::asio::error::try_again)
  125. {
  126. if (state & user_set_non_blocking)
  127. return invalid_socket;
  128. // Fall through to retry operation.
  129. }
  130. else if (ec == boost::asio::error::connection_aborted)
  131. {
  132. if (state & enable_connection_aborted)
  133. return invalid_socket;
  134. // Fall through to retry operation.
  135. }
  136. #if defined(EPROTO)
  137. else if (ec.value() == EPROTO)
  138. {
  139. if (state & enable_connection_aborted)
  140. return invalid_socket;
  141. // Fall through to retry operation.
  142. }
  143. #endif // defined(EPROTO)
  144. else
  145. return invalid_socket;
  146. // Wait for socket to become ready.
  147. if (socket_ops::poll_read(s, 0, -1, ec) < 0)
  148. return invalid_socket;
  149. }
  150. }
  151. #if defined(BOOST_ASIO_HAS_IOCP)
  152. void complete_iocp_accept(socket_type s,
  153. void* output_buffer, DWORD address_length,
  154. socket_addr_type* addr, std::size_t* addrlen,
  155. socket_type new_socket, boost::system::error_code& ec)
  156. {
  157. // Map non-portable errors to their portable counterparts.
  158. if (ec.value() == ERROR_NETNAME_DELETED)
  159. ec = boost::asio::error::connection_aborted;
  160. if (!ec)
  161. {
  162. // Get the address of the peer.
  163. if (addr && addrlen)
  164. {
  165. LPSOCKADDR local_addr = 0;
  166. int local_addr_length = 0;
  167. LPSOCKADDR remote_addr = 0;
  168. int remote_addr_length = 0;
  169. GetAcceptExSockaddrs(output_buffer, 0, address_length,
  170. address_length, &local_addr, &local_addr_length,
  171. &remote_addr, &remote_addr_length);
  172. if (static_cast<std::size_t>(remote_addr_length) > *addrlen)
  173. {
  174. ec = boost::asio::error::invalid_argument;
  175. }
  176. else
  177. {
  178. using namespace std; // For memcpy.
  179. memcpy(addr, remote_addr, remote_addr_length);
  180. *addrlen = static_cast<std::size_t>(remote_addr_length);
  181. }
  182. }
  183. // Need to set the SO_UPDATE_ACCEPT_CONTEXT option so that getsockname
  184. // and getpeername will work on the accepted socket.
  185. SOCKET update_ctx_param = s;
  186. socket_ops::state_type state = 0;
  187. socket_ops::setsockopt(new_socket, state,
  188. SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
  189. &update_ctx_param, sizeof(SOCKET), ec);
  190. }
  191. }
  192. #else // defined(BOOST_ASIO_HAS_IOCP)
  193. bool non_blocking_accept(socket_type s,
  194. state_type state, socket_addr_type* addr, std::size_t* addrlen,
  195. boost::system::error_code& ec, socket_type& new_socket)
  196. {
  197. for (;;)
  198. {
  199. // Accept the waiting connection.
  200. new_socket = socket_ops::accept(s, addr, addrlen, ec);
  201. // Check if operation succeeded.
  202. if (new_socket != invalid_socket)
  203. return true;
  204. // Retry operation if interrupted by signal.
  205. if (ec == boost::asio::error::interrupted)
  206. continue;
  207. // Operation failed.
  208. if (ec == boost::asio::error::would_block
  209. || ec == boost::asio::error::try_again)
  210. {
  211. // Fall through to retry operation.
  212. }
  213. else if (ec == boost::asio::error::connection_aborted)
  214. {
  215. if (state & enable_connection_aborted)
  216. return true;
  217. // Fall through to retry operation.
  218. }
  219. #if defined(EPROTO)
  220. else if (ec.value() == EPROTO)
  221. {
  222. if (state & enable_connection_aborted)
  223. return true;
  224. // Fall through to retry operation.
  225. }
  226. #endif // defined(EPROTO)
  227. else
  228. return true;
  229. return false;
  230. }
  231. }
  232. #endif // defined(BOOST_ASIO_HAS_IOCP)
  233. template <typename SockLenType>
  234. inline int call_bind(SockLenType msghdr::*,
  235. socket_type s, const socket_addr_type* addr, std::size_t addrlen)
  236. {
  237. return ::bind(s, addr, (SockLenType)addrlen);
  238. }
  239. int bind(socket_type s, const socket_addr_type* addr,
  240. std::size_t addrlen, boost::system::error_code& ec)
  241. {
  242. if (s == invalid_socket)
  243. {
  244. ec = boost::asio::error::bad_descriptor;
  245. return socket_error_retval;
  246. }
  247. clear_last_error();
  248. int result = error_wrapper(call_bind(
  249. &msghdr::msg_namelen, s, addr, addrlen), ec);
  250. if (result == 0)
  251. ec = boost::system::error_code();
  252. return result;
  253. }
  254. int close(socket_type s, state_type& state,
  255. bool destruction, boost::system::error_code& ec)
  256. {
  257. int result = 0;
  258. if (s != invalid_socket)
  259. {
  260. // We don't want the destructor to block, so set the socket to linger in
  261. // the background. If the user doesn't like this behaviour then they need
  262. // to explicitly close the socket.
  263. if (destruction && (state & user_set_linger))
  264. {
  265. ::linger opt;
  266. opt.l_onoff = 0;
  267. opt.l_linger = 0;
  268. boost::system::error_code ignored_ec;
  269. socket_ops::setsockopt(s, state, SOL_SOCKET,
  270. SO_LINGER, &opt, sizeof(opt), ignored_ec);
  271. }
  272. clear_last_error();
  273. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  274. result = error_wrapper(::closesocket(s), ec);
  275. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  276. result = error_wrapper(::close(s), ec);
  277. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  278. if (result != 0
  279. && (ec == boost::asio::error::would_block
  280. || ec == boost::asio::error::try_again))
  281. {
  282. // According to UNIX Network Programming Vol. 1, it is possible for
  283. // close() to fail with EWOULDBLOCK under certain circumstances. What
  284. // isn't clear is the state of the descriptor after this error. The one
  285. // current OS where this behaviour is seen, Windows, says that the socket
  286. // remains open. Therefore we'll put the descriptor back into blocking
  287. // mode and have another attempt at closing it.
  288. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  289. ioctl_arg_type arg = 0;
  290. ::ioctlsocket(s, FIONBIO, &arg);
  291. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  292. # if defined(__SYMBIAN32__)
  293. int flags = ::fcntl(s, F_GETFL, 0);
  294. if (flags >= 0)
  295. ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
  296. # else // defined(__SYMBIAN32__)
  297. ioctl_arg_type arg = 0;
  298. ::ioctl(s, FIONBIO, &arg);
  299. # endif // defined(__SYMBIAN32__)
  300. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  301. state &= ~non_blocking;
  302. clear_last_error();
  303. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  304. result = error_wrapper(::closesocket(s), ec);
  305. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  306. result = error_wrapper(::close(s), ec);
  307. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  308. }
  309. }
  310. if (result == 0)
  311. ec = boost::system::error_code();
  312. return result;
  313. }
  314. bool set_user_non_blocking(socket_type s,
  315. state_type& state, bool value, boost::system::error_code& ec)
  316. {
  317. if (s == invalid_socket)
  318. {
  319. ec = boost::asio::error::bad_descriptor;
  320. return false;
  321. }
  322. clear_last_error();
  323. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  324. ioctl_arg_type arg = (value ? 1 : 0);
  325. int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
  326. #elif defined(__SYMBIAN32__)
  327. int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
  328. if (result >= 0)
  329. {
  330. clear_last_error();
  331. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  332. result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
  333. }
  334. #else
  335. ioctl_arg_type arg = (value ? 1 : 0);
  336. int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
  337. #endif
  338. if (result >= 0)
  339. {
  340. ec = boost::system::error_code();
  341. if (value)
  342. state |= user_set_non_blocking;
  343. else
  344. {
  345. // Clearing the user-set non-blocking mode always overrides any
  346. // internally-set non-blocking flag. Any subsequent asynchronous
  347. // operations will need to re-enable non-blocking I/O.
  348. state &= ~(user_set_non_blocking | internal_non_blocking);
  349. }
  350. return true;
  351. }
  352. return false;
  353. }
  354. bool set_internal_non_blocking(socket_type s,
  355. state_type& state, bool value, boost::system::error_code& ec)
  356. {
  357. if (s == invalid_socket)
  358. {
  359. ec = boost::asio::error::bad_descriptor;
  360. return false;
  361. }
  362. if (!value && (state & user_set_non_blocking))
  363. {
  364. // It does not make sense to clear the internal non-blocking flag if the
  365. // user still wants non-blocking behaviour. Return an error and let the
  366. // caller figure out whether to update the user-set non-blocking flag.
  367. ec = boost::asio::error::invalid_argument;
  368. return false;
  369. }
  370. clear_last_error();
  371. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  372. ioctl_arg_type arg = (value ? 1 : 0);
  373. int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
  374. #elif defined(__SYMBIAN32__)
  375. int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
  376. if (result >= 0)
  377. {
  378. clear_last_error();
  379. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  380. result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
  381. }
  382. #else
  383. ioctl_arg_type arg = (value ? 1 : 0);
  384. int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
  385. #endif
  386. if (result >= 0)
  387. {
  388. ec = boost::system::error_code();
  389. if (value)
  390. state |= internal_non_blocking;
  391. else
  392. state &= ~internal_non_blocking;
  393. return true;
  394. }
  395. return false;
  396. }
  397. int shutdown(socket_type s, int what, boost::system::error_code& ec)
  398. {
  399. if (s == invalid_socket)
  400. {
  401. ec = boost::asio::error::bad_descriptor;
  402. return socket_error_retval;
  403. }
  404. clear_last_error();
  405. int result = error_wrapper(::shutdown(s, what), ec);
  406. if (result == 0)
  407. ec = boost::system::error_code();
  408. return result;
  409. }
  410. template <typename SockLenType>
  411. inline int call_connect(SockLenType msghdr::*,
  412. socket_type s, const socket_addr_type* addr, std::size_t addrlen)
  413. {
  414. return ::connect(s, addr, (SockLenType)addrlen);
  415. }
  416. int connect(socket_type s, const socket_addr_type* addr,
  417. std::size_t addrlen, boost::system::error_code& ec)
  418. {
  419. if (s == invalid_socket)
  420. {
  421. ec = boost::asio::error::bad_descriptor;
  422. return socket_error_retval;
  423. }
  424. clear_last_error();
  425. int result = error_wrapper(call_connect(
  426. &msghdr::msg_namelen, s, addr, addrlen), ec);
  427. if (result == 0)
  428. ec = boost::system::error_code();
  429. #if defined(__linux__)
  430. else if (ec == boost::asio::error::try_again)
  431. ec = boost::asio::error::no_buffer_space;
  432. #endif // defined(__linux__)
  433. return result;
  434. }
  435. void sync_connect(socket_type s, const socket_addr_type* addr,
  436. std::size_t addrlen, boost::system::error_code& ec)
  437. {
  438. // Perform the connect operation.
  439. socket_ops::connect(s, addr, addrlen, ec);
  440. if (ec != boost::asio::error::in_progress
  441. && ec != boost::asio::error::would_block)
  442. {
  443. // The connect operation finished immediately.
  444. return;
  445. }
  446. // Wait for socket to become ready.
  447. if (socket_ops::poll_connect(s, -1, ec) < 0)
  448. return;
  449. // Get the error code from the connect operation.
  450. int connect_error = 0;
  451. size_t connect_error_len = sizeof(connect_error);
  452. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
  453. &connect_error, &connect_error_len, ec) == socket_error_retval)
  454. return;
  455. // Return the result of the connect operation.
  456. ec = boost::system::error_code(connect_error,
  457. boost::asio::error::get_system_category());
  458. }
  459. #if defined(BOOST_ASIO_HAS_IOCP)
  460. void complete_iocp_connect(socket_type s, boost::system::error_code& ec)
  461. {
  462. // Map non-portable errors to their portable counterparts.
  463. switch (ec.value())
  464. {
  465. case ERROR_CONNECTION_REFUSED:
  466. ec = boost::asio::error::connection_refused;
  467. break;
  468. case ERROR_NETWORK_UNREACHABLE:
  469. ec = boost::asio::error::network_unreachable;
  470. break;
  471. case ERROR_HOST_UNREACHABLE:
  472. ec = boost::asio::error::host_unreachable;
  473. break;
  474. case ERROR_SEM_TIMEOUT:
  475. ec = boost::asio::error::timed_out;
  476. break;
  477. default:
  478. break;
  479. }
  480. if (!ec)
  481. {
  482. // Need to set the SO_UPDATE_CONNECT_CONTEXT option so that getsockname
  483. // and getpeername will work on the connected socket.
  484. socket_ops::state_type state = 0;
  485. const int so_update_connect_context = 0x7010;
  486. socket_ops::setsockopt(s, state, SOL_SOCKET,
  487. so_update_connect_context, 0, 0, ec);
  488. }
  489. }
  490. #endif // defined(BOOST_ASIO_HAS_IOCP)
  491. bool non_blocking_connect(socket_type s, boost::system::error_code& ec)
  492. {
  493. // Check if the connect operation has finished. This is required since we may
  494. // get spurious readiness notifications from the reactor.
  495. #if defined(BOOST_ASIO_WINDOWS) \
  496. || defined(__CYGWIN__) \
  497. || defined(__SYMBIAN32__)
  498. fd_set write_fds;
  499. FD_ZERO(&write_fds);
  500. FD_SET(s, &write_fds);
  501. fd_set except_fds;
  502. FD_ZERO(&except_fds);
  503. FD_SET(s, &except_fds);
  504. timeval zero_timeout;
  505. zero_timeout.tv_sec = 0;
  506. zero_timeout.tv_usec = 0;
  507. int ready = ::select(s + 1, 0, &write_fds, &except_fds, &zero_timeout);
  508. #else // defined(BOOST_ASIO_WINDOWS)
  509. // || defined(__CYGWIN__)
  510. // || defined(__SYMBIAN32__)
  511. pollfd fds;
  512. fds.fd = s;
  513. fds.events = POLLOUT;
  514. fds.revents = 0;
  515. int ready = ::poll(&fds, 1, 0);
  516. #endif // defined(BOOST_ASIO_WINDOWS)
  517. // || defined(__CYGWIN__)
  518. // || defined(__SYMBIAN32__)
  519. if (ready == 0)
  520. {
  521. // The asynchronous connect operation is still in progress.
  522. return false;
  523. }
  524. // Get the error code from the connect operation.
  525. int connect_error = 0;
  526. size_t connect_error_len = sizeof(connect_error);
  527. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
  528. &connect_error, &connect_error_len, ec) == 0)
  529. {
  530. if (connect_error)
  531. {
  532. ec = boost::system::error_code(connect_error,
  533. boost::asio::error::get_system_category());
  534. }
  535. else
  536. ec = boost::system::error_code();
  537. }
  538. return true;
  539. }
  540. int socketpair(int af, int type, int protocol,
  541. socket_type sv[2], boost::system::error_code& ec)
  542. {
  543. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  544. (void)(af);
  545. (void)(type);
  546. (void)(protocol);
  547. (void)(sv);
  548. ec = boost::asio::error::operation_not_supported;
  549. return socket_error_retval;
  550. #else
  551. clear_last_error();
  552. int result = error_wrapper(::socketpair(af, type, protocol, sv), ec);
  553. if (result == 0)
  554. ec = boost::system::error_code();
  555. return result;
  556. #endif
  557. }
  558. bool sockatmark(socket_type s, boost::system::error_code& ec)
  559. {
  560. if (s == invalid_socket)
  561. {
  562. ec = boost::asio::error::bad_descriptor;
  563. return false;
  564. }
  565. #if defined(SIOCATMARK)
  566. ioctl_arg_type value = 0;
  567. # if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  568. int result = error_wrapper(::ioctlsocket(s, SIOCATMARK, &value), ec);
  569. # else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  570. int result = error_wrapper(::ioctl(s, SIOCATMARK, &value), ec);
  571. # endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  572. if (result == 0)
  573. ec = boost::system::error_code();
  574. # if defined(ENOTTY)
  575. if (ec.value() == ENOTTY)
  576. ec = boost::asio::error::not_socket;
  577. # endif // defined(ENOTTY)
  578. #else // defined(SIOCATMARK)
  579. int value = error_wrapper(::sockatmark(s), ec);
  580. if (value != -1)
  581. ec = boost::system::error_code();
  582. #endif // defined(SIOCATMARK)
  583. return ec ? false : value != 0;
  584. }
  585. size_t available(socket_type s, boost::system::error_code& ec)
  586. {
  587. if (s == invalid_socket)
  588. {
  589. ec = boost::asio::error::bad_descriptor;
  590. return 0;
  591. }
  592. ioctl_arg_type value = 0;
  593. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  594. int result = error_wrapper(::ioctlsocket(s, FIONREAD, &value), ec);
  595. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  596. int result = error_wrapper(::ioctl(s, FIONREAD, &value), ec);
  597. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  598. if (result == 0)
  599. ec = boost::system::error_code();
  600. #if defined(ENOTTY)
  601. if (ec.value() == ENOTTY)
  602. ec = boost::asio::error::not_socket;
  603. #endif // defined(ENOTTY)
  604. return ec ? static_cast<size_t>(0) : static_cast<size_t>(value);
  605. }
  606. int listen(socket_type s, int backlog, boost::system::error_code& ec)
  607. {
  608. if (s == invalid_socket)
  609. {
  610. ec = boost::asio::error::bad_descriptor;
  611. return socket_error_retval;
  612. }
  613. clear_last_error();
  614. int result = error_wrapper(::listen(s, backlog), ec);
  615. if (result == 0)
  616. ec = boost::system::error_code();
  617. return result;
  618. }
  619. inline void init_buf_iov_base(void*& base, void* addr)
  620. {
  621. base = addr;
  622. }
  623. template <typename T>
  624. inline void init_buf_iov_base(T& base, void* addr)
  625. {
  626. base = static_cast<T>(addr);
  627. }
  628. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  629. typedef WSABUF buf;
  630. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  631. typedef iovec buf;
  632. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  633. void init_buf(buf& b, void* data, size_t size)
  634. {
  635. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  636. b.buf = static_cast<char*>(data);
  637. b.len = static_cast<u_long>(size);
  638. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  639. init_buf_iov_base(b.iov_base, data);
  640. b.iov_len = size;
  641. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  642. }
  643. void init_buf(buf& b, const void* data, size_t size)
  644. {
  645. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  646. b.buf = static_cast<char*>(const_cast<void*>(data));
  647. b.len = static_cast<u_long>(size);
  648. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  649. init_buf_iov_base(b.iov_base, const_cast<void*>(data));
  650. b.iov_len = size;
  651. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  652. }
  653. inline void init_msghdr_msg_name(void*& name, socket_addr_type* addr)
  654. {
  655. name = addr;
  656. }
  657. inline void init_msghdr_msg_name(void*& name, const socket_addr_type* addr)
  658. {
  659. name = const_cast<socket_addr_type*>(addr);
  660. }
  661. template <typename T>
  662. inline void init_msghdr_msg_name(T& name, socket_addr_type* addr)
  663. {
  664. name = reinterpret_cast<T>(addr);
  665. }
  666. template <typename T>
  667. inline void init_msghdr_msg_name(T& name, const socket_addr_type* addr)
  668. {
  669. name = reinterpret_cast<T>(const_cast<socket_addr_type*>(addr));
  670. }
  671. signed_size_type recv(socket_type s, buf* bufs, size_t count,
  672. int flags, boost::system::error_code& ec)
  673. {
  674. clear_last_error();
  675. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  676. // Receive some data.
  677. DWORD recv_buf_count = static_cast<DWORD>(count);
  678. DWORD bytes_transferred = 0;
  679. DWORD recv_flags = flags;
  680. int result = error_wrapper(::WSARecv(s, bufs,
  681. recv_buf_count, &bytes_transferred, &recv_flags, 0, 0), ec);
  682. if (ec.value() == ERROR_NETNAME_DELETED)
  683. ec = boost::asio::error::connection_reset;
  684. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  685. ec = boost::asio::error::connection_refused;
  686. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  687. ec.assign(0, ec.category());
  688. if (result != 0)
  689. return socket_error_retval;
  690. ec = boost::system::error_code();
  691. return bytes_transferred;
  692. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  693. msghdr msg = msghdr();
  694. msg.msg_iov = bufs;
  695. msg.msg_iovlen = static_cast<int>(count);
  696. signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
  697. if (result >= 0)
  698. ec = boost::system::error_code();
  699. return result;
  700. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  701. }
  702. size_t sync_recv(socket_type s, state_type state, buf* bufs,
  703. size_t count, int flags, bool all_empty, boost::system::error_code& ec)
  704. {
  705. if (s == invalid_socket)
  706. {
  707. ec = boost::asio::error::bad_descriptor;
  708. return 0;
  709. }
  710. // A request to read 0 bytes on a stream is a no-op.
  711. if (all_empty && (state & stream_oriented))
  712. {
  713. ec = boost::system::error_code();
  714. return 0;
  715. }
  716. // Read some data.
  717. for (;;)
  718. {
  719. // Try to complete the operation without blocking.
  720. signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
  721. // Check if operation succeeded.
  722. if (bytes > 0)
  723. return bytes;
  724. // Check for EOF.
  725. if ((state & stream_oriented) && bytes == 0)
  726. {
  727. ec = boost::asio::error::eof;
  728. return 0;
  729. }
  730. // Operation failed.
  731. if ((state & user_set_non_blocking)
  732. || (ec != boost::asio::error::would_block
  733. && ec != boost::asio::error::try_again))
  734. return 0;
  735. // Wait for socket to become ready.
  736. if (socket_ops::poll_read(s, 0, -1, ec) < 0)
  737. return 0;
  738. }
  739. }
  740. #if defined(BOOST_ASIO_HAS_IOCP)
  741. void complete_iocp_recv(state_type state,
  742. const weak_cancel_token_type& cancel_token, bool all_empty,
  743. boost::system::error_code& ec, size_t bytes_transferred)
  744. {
  745. // Map non-portable errors to their portable counterparts.
  746. if (ec.value() == ERROR_NETNAME_DELETED)
  747. {
  748. if (cancel_token.expired())
  749. ec = boost::asio::error::operation_aborted;
  750. else
  751. ec = boost::asio::error::connection_reset;
  752. }
  753. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  754. {
  755. ec = boost::asio::error::connection_refused;
  756. }
  757. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  758. {
  759. ec.assign(0, ec.category());
  760. }
  761. // Check for connection closed.
  762. else if (!ec && bytes_transferred == 0
  763. && (state & stream_oriented) != 0
  764. && !all_empty)
  765. {
  766. ec = boost::asio::error::eof;
  767. }
  768. }
  769. #else // defined(BOOST_ASIO_HAS_IOCP)
  770. bool non_blocking_recv(socket_type s,
  771. buf* bufs, size_t count, int flags, bool is_stream,
  772. boost::system::error_code& ec, size_t& bytes_transferred)
  773. {
  774. for (;;)
  775. {
  776. // Read some data.
  777. signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
  778. // Check for end of stream.
  779. if (is_stream && bytes == 0)
  780. {
  781. ec = boost::asio::error::eof;
  782. return true;
  783. }
  784. // Retry operation if interrupted by signal.
  785. if (ec == boost::asio::error::interrupted)
  786. continue;
  787. // Check if we need to run the operation again.
  788. if (ec == boost::asio::error::would_block
  789. || ec == boost::asio::error::try_again)
  790. return false;
  791. // Operation is complete.
  792. if (bytes >= 0)
  793. {
  794. ec = boost::system::error_code();
  795. bytes_transferred = bytes;
  796. }
  797. else
  798. bytes_transferred = 0;
  799. return true;
  800. }
  801. }
  802. #endif // defined(BOOST_ASIO_HAS_IOCP)
  803. signed_size_type recvfrom(socket_type s, buf* bufs, size_t count,
  804. int flags, socket_addr_type* addr, std::size_t* addrlen,
  805. boost::system::error_code& ec)
  806. {
  807. clear_last_error();
  808. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  809. // Receive some data.
  810. DWORD recv_buf_count = static_cast<DWORD>(count);
  811. DWORD bytes_transferred = 0;
  812. DWORD recv_flags = flags;
  813. int tmp_addrlen = (int)*addrlen;
  814. int result = error_wrapper(::WSARecvFrom(s, bufs, recv_buf_count,
  815. &bytes_transferred, &recv_flags, addr, &tmp_addrlen, 0, 0), ec);
  816. *addrlen = (std::size_t)tmp_addrlen;
  817. if (ec.value() == ERROR_NETNAME_DELETED)
  818. ec = boost::asio::error::connection_reset;
  819. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  820. ec = boost::asio::error::connection_refused;
  821. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  822. ec.assign(0, ec.category());
  823. if (result != 0)
  824. return socket_error_retval;
  825. ec = boost::system::error_code();
  826. return bytes_transferred;
  827. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  828. msghdr msg = msghdr();
  829. init_msghdr_msg_name(msg.msg_name, addr);
  830. msg.msg_namelen = static_cast<int>(*addrlen);
  831. msg.msg_iov = bufs;
  832. msg.msg_iovlen = static_cast<int>(count);
  833. signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
  834. *addrlen = msg.msg_namelen;
  835. if (result >= 0)
  836. ec = boost::system::error_code();
  837. return result;
  838. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  839. }
  840. size_t sync_recvfrom(socket_type s, state_type state, buf* bufs,
  841. size_t count, int flags, socket_addr_type* addr,
  842. std::size_t* addrlen, boost::system::error_code& ec)
  843. {
  844. if (s == invalid_socket)
  845. {
  846. ec = boost::asio::error::bad_descriptor;
  847. return 0;
  848. }
  849. // Read some data.
  850. for (;;)
  851. {
  852. // Try to complete the operation without blocking.
  853. signed_size_type bytes = socket_ops::recvfrom(
  854. s, bufs, count, flags, addr, addrlen, ec);
  855. // Check if operation succeeded.
  856. if (bytes >= 0)
  857. return bytes;
  858. // Operation failed.
  859. if ((state & user_set_non_blocking)
  860. || (ec != boost::asio::error::would_block
  861. && ec != boost::asio::error::try_again))
  862. return 0;
  863. // Wait for socket to become ready.
  864. if (socket_ops::poll_read(s, 0, -1, ec) < 0)
  865. return 0;
  866. }
  867. }
  868. #if defined(BOOST_ASIO_HAS_IOCP)
  869. void complete_iocp_recvfrom(
  870. const weak_cancel_token_type& cancel_token,
  871. boost::system::error_code& ec)
  872. {
  873. // Map non-portable errors to their portable counterparts.
  874. if (ec.value() == ERROR_NETNAME_DELETED)
  875. {
  876. if (cancel_token.expired())
  877. ec = boost::asio::error::operation_aborted;
  878. else
  879. ec = boost::asio::error::connection_reset;
  880. }
  881. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  882. {
  883. ec = boost::asio::error::connection_refused;
  884. }
  885. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  886. {
  887. ec.assign(0, ec.category());
  888. }
  889. }
  890. #else // defined(BOOST_ASIO_HAS_IOCP)
  891. bool non_blocking_recvfrom(socket_type s,
  892. buf* bufs, size_t count, int flags,
  893. socket_addr_type* addr, std::size_t* addrlen,
  894. boost::system::error_code& ec, size_t& bytes_transferred)
  895. {
  896. for (;;)
  897. {
  898. // Read some data.
  899. signed_size_type bytes = socket_ops::recvfrom(
  900. s, bufs, count, flags, addr, addrlen, ec);
  901. // Retry operation if interrupted by signal.
  902. if (ec == boost::asio::error::interrupted)
  903. continue;
  904. // Check if we need to run the operation again.
  905. if (ec == boost::asio::error::would_block
  906. || ec == boost::asio::error::try_again)
  907. return false;
  908. // Operation is complete.
  909. if (bytes >= 0)
  910. {
  911. ec = boost::system::error_code();
  912. bytes_transferred = bytes;
  913. }
  914. else
  915. bytes_transferred = 0;
  916. return true;
  917. }
  918. }
  919. #endif // defined(BOOST_ASIO_HAS_IOCP)
  920. signed_size_type recvmsg(socket_type s, buf* bufs, size_t count,
  921. int in_flags, int& out_flags, boost::system::error_code& ec)
  922. {
  923. clear_last_error();
  924. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  925. out_flags = 0;
  926. return socket_ops::recv(s, bufs, count, in_flags, ec);
  927. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  928. msghdr msg = msghdr();
  929. msg.msg_iov = bufs;
  930. msg.msg_iovlen = static_cast<int>(count);
  931. signed_size_type result = error_wrapper(::recvmsg(s, &msg, in_flags), ec);
  932. if (result >= 0)
  933. {
  934. ec = boost::system::error_code();
  935. out_flags = msg.msg_flags;
  936. }
  937. else
  938. out_flags = 0;
  939. return result;
  940. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  941. }
  942. size_t sync_recvmsg(socket_type s, state_type state,
  943. buf* bufs, size_t count, int in_flags, int& out_flags,
  944. boost::system::error_code& ec)
  945. {
  946. if (s == invalid_socket)
  947. {
  948. ec = boost::asio::error::bad_descriptor;
  949. return 0;
  950. }
  951. // Read some data.
  952. for (;;)
  953. {
  954. // Try to complete the operation without blocking.
  955. signed_size_type bytes = socket_ops::recvmsg(
  956. s, bufs, count, in_flags, out_flags, ec);
  957. // Check if operation succeeded.
  958. if (bytes >= 0)
  959. return bytes;
  960. // Operation failed.
  961. if ((state & user_set_non_blocking)
  962. || (ec != boost::asio::error::would_block
  963. && ec != boost::asio::error::try_again))
  964. return 0;
  965. // Wait for socket to become ready.
  966. if (socket_ops::poll_read(s, 0, -1, ec) < 0)
  967. return 0;
  968. }
  969. }
  970. #if defined(BOOST_ASIO_HAS_IOCP)
  971. void complete_iocp_recvmsg(
  972. const weak_cancel_token_type& cancel_token,
  973. boost::system::error_code& ec)
  974. {
  975. // Map non-portable errors to their portable counterparts.
  976. if (ec.value() == ERROR_NETNAME_DELETED)
  977. {
  978. if (cancel_token.expired())
  979. ec = boost::asio::error::operation_aborted;
  980. else
  981. ec = boost::asio::error::connection_reset;
  982. }
  983. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  984. {
  985. ec = boost::asio::error::connection_refused;
  986. }
  987. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  988. {
  989. ec.assign(0, ec.category());
  990. }
  991. }
  992. #else // defined(BOOST_ASIO_HAS_IOCP)
  993. bool non_blocking_recvmsg(socket_type s,
  994. buf* bufs, size_t count, int in_flags, int& out_flags,
  995. boost::system::error_code& ec, size_t& bytes_transferred)
  996. {
  997. for (;;)
  998. {
  999. // Read some data.
  1000. signed_size_type bytes = socket_ops::recvmsg(
  1001. s, bufs, count, in_flags, out_flags, ec);
  1002. // Retry operation if interrupted by signal.
  1003. if (ec == boost::asio::error::interrupted)
  1004. continue;
  1005. // Check if we need to run the operation again.
  1006. if (ec == boost::asio::error::would_block
  1007. || ec == boost::asio::error::try_again)
  1008. return false;
  1009. // Operation is complete.
  1010. if (bytes >= 0)
  1011. {
  1012. ec = boost::system::error_code();
  1013. bytes_transferred = bytes;
  1014. }
  1015. else
  1016. bytes_transferred = 0;
  1017. return true;
  1018. }
  1019. }
  1020. #endif // defined(BOOST_ASIO_HAS_IOCP)
  1021. signed_size_type send(socket_type s, const buf* bufs, size_t count,
  1022. int flags, boost::system::error_code& ec)
  1023. {
  1024. clear_last_error();
  1025. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1026. // Send the data.
  1027. DWORD send_buf_count = static_cast<DWORD>(count);
  1028. DWORD bytes_transferred = 0;
  1029. DWORD send_flags = flags;
  1030. int result = error_wrapper(::WSASend(s, const_cast<buf*>(bufs),
  1031. send_buf_count, &bytes_transferred, send_flags, 0, 0), ec);
  1032. if (ec.value() == ERROR_NETNAME_DELETED)
  1033. ec = boost::asio::error::connection_reset;
  1034. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  1035. ec = boost::asio::error::connection_refused;
  1036. if (result != 0)
  1037. return socket_error_retval;
  1038. ec = boost::system::error_code();
  1039. return bytes_transferred;
  1040. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1041. msghdr msg = msghdr();
  1042. msg.msg_iov = const_cast<buf*>(bufs);
  1043. msg.msg_iovlen = static_cast<int>(count);
  1044. #if defined(__linux__)
  1045. flags |= MSG_NOSIGNAL;
  1046. #endif // defined(__linux__)
  1047. signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
  1048. if (result >= 0)
  1049. ec = boost::system::error_code();
  1050. return result;
  1051. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1052. }
  1053. size_t sync_send(socket_type s, state_type state, const buf* bufs,
  1054. size_t count, int flags, bool all_empty, boost::system::error_code& ec)
  1055. {
  1056. if (s == invalid_socket)
  1057. {
  1058. ec = boost::asio::error::bad_descriptor;
  1059. return 0;
  1060. }
  1061. // A request to write 0 bytes to a stream is a no-op.
  1062. if (all_empty && (state & stream_oriented))
  1063. {
  1064. ec = boost::system::error_code();
  1065. return 0;
  1066. }
  1067. // Read some data.
  1068. for (;;)
  1069. {
  1070. // Try to complete the operation without blocking.
  1071. signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
  1072. // Check if operation succeeded.
  1073. if (bytes >= 0)
  1074. return bytes;
  1075. // Operation failed.
  1076. if ((state & user_set_non_blocking)
  1077. || (ec != boost::asio::error::would_block
  1078. && ec != boost::asio::error::try_again))
  1079. return 0;
  1080. // Wait for socket to become ready.
  1081. if (socket_ops::poll_write(s, 0, -1, ec) < 0)
  1082. return 0;
  1083. }
  1084. }
  1085. #if defined(BOOST_ASIO_HAS_IOCP)
  1086. void complete_iocp_send(
  1087. const weak_cancel_token_type& cancel_token,
  1088. boost::system::error_code& ec)
  1089. {
  1090. // Map non-portable errors to their portable counterparts.
  1091. if (ec.value() == ERROR_NETNAME_DELETED)
  1092. {
  1093. if (cancel_token.expired())
  1094. ec = boost::asio::error::operation_aborted;
  1095. else
  1096. ec = boost::asio::error::connection_reset;
  1097. }
  1098. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  1099. {
  1100. ec = boost::asio::error::connection_refused;
  1101. }
  1102. }
  1103. #else // defined(BOOST_ASIO_HAS_IOCP)
  1104. bool non_blocking_send(socket_type s,
  1105. const buf* bufs, size_t count, int flags,
  1106. boost::system::error_code& ec, size_t& bytes_transferred)
  1107. {
  1108. for (;;)
  1109. {
  1110. // Write some data.
  1111. signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
  1112. // Retry operation if interrupted by signal.
  1113. if (ec == boost::asio::error::interrupted)
  1114. continue;
  1115. // Check if we need to run the operation again.
  1116. if (ec == boost::asio::error::would_block
  1117. || ec == boost::asio::error::try_again)
  1118. return false;
  1119. // Operation is complete.
  1120. if (bytes >= 0)
  1121. {
  1122. ec = boost::system::error_code();
  1123. bytes_transferred = bytes;
  1124. }
  1125. else
  1126. bytes_transferred = 0;
  1127. return true;
  1128. }
  1129. }
  1130. #endif // defined(BOOST_ASIO_HAS_IOCP)
  1131. signed_size_type sendto(socket_type s, const buf* bufs, size_t count,
  1132. int flags, const socket_addr_type* addr, std::size_t addrlen,
  1133. boost::system::error_code& ec)
  1134. {
  1135. clear_last_error();
  1136. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1137. // Send the data.
  1138. DWORD send_buf_count = static_cast<DWORD>(count);
  1139. DWORD bytes_transferred = 0;
  1140. int result = error_wrapper(::WSASendTo(s, const_cast<buf*>(bufs),
  1141. send_buf_count, &bytes_transferred, flags, addr,
  1142. static_cast<int>(addrlen), 0, 0), ec);
  1143. if (ec.value() == ERROR_NETNAME_DELETED)
  1144. ec = boost::asio::error::connection_reset;
  1145. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  1146. ec = boost::asio::error::connection_refused;
  1147. if (result != 0)
  1148. return socket_error_retval;
  1149. ec = boost::system::error_code();
  1150. return bytes_transferred;
  1151. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1152. msghdr msg = msghdr();
  1153. init_msghdr_msg_name(msg.msg_name, addr);
  1154. msg.msg_namelen = static_cast<int>(addrlen);
  1155. msg.msg_iov = const_cast<buf*>(bufs);
  1156. msg.msg_iovlen = static_cast<int>(count);
  1157. #if defined(__linux__)
  1158. flags |= MSG_NOSIGNAL;
  1159. #endif // defined(__linux__)
  1160. signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
  1161. if (result >= 0)
  1162. ec = boost::system::error_code();
  1163. return result;
  1164. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1165. }
  1166. size_t sync_sendto(socket_type s, state_type state, const buf* bufs,
  1167. size_t count, int flags, const socket_addr_type* addr,
  1168. std::size_t addrlen, boost::system::error_code& ec)
  1169. {
  1170. if (s == invalid_socket)
  1171. {
  1172. ec = boost::asio::error::bad_descriptor;
  1173. return 0;
  1174. }
  1175. // Write some data.
  1176. for (;;)
  1177. {
  1178. // Try to complete the operation without blocking.
  1179. signed_size_type bytes = socket_ops::sendto(
  1180. s, bufs, count, flags, addr, addrlen, ec);
  1181. // Check if operation succeeded.
  1182. if (bytes >= 0)
  1183. return bytes;
  1184. // Operation failed.
  1185. if ((state & user_set_non_blocking)
  1186. || (ec != boost::asio::error::would_block
  1187. && ec != boost::asio::error::try_again))
  1188. return 0;
  1189. // Wait for socket to become ready.
  1190. if (socket_ops::poll_write(s, 0, -1, ec) < 0)
  1191. return 0;
  1192. }
  1193. }
  1194. #if !defined(BOOST_ASIO_HAS_IOCP)
  1195. bool non_blocking_sendto(socket_type s,
  1196. const buf* bufs, size_t count, int flags,
  1197. const socket_addr_type* addr, std::size_t addrlen,
  1198. boost::system::error_code& ec, size_t& bytes_transferred)
  1199. {
  1200. for (;;)
  1201. {
  1202. // Write some data.
  1203. signed_size_type bytes = socket_ops::sendto(
  1204. s, bufs, count, flags, addr, addrlen, ec);
  1205. // Retry operation if interrupted by signal.
  1206. if (ec == boost::asio::error::interrupted)
  1207. continue;
  1208. // Check if we need to run the operation again.
  1209. if (ec == boost::asio::error::would_block
  1210. || ec == boost::asio::error::try_again)
  1211. return false;
  1212. // Operation is complete.
  1213. if (bytes >= 0)
  1214. {
  1215. ec = boost::system::error_code();
  1216. bytes_transferred = bytes;
  1217. }
  1218. else
  1219. bytes_transferred = 0;
  1220. return true;
  1221. }
  1222. }
  1223. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  1224. socket_type socket(int af, int type, int protocol,
  1225. boost::system::error_code& ec)
  1226. {
  1227. clear_last_error();
  1228. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1229. socket_type s = error_wrapper(::WSASocketW(af, type, protocol, 0, 0,
  1230. WSA_FLAG_OVERLAPPED), ec);
  1231. if (s == invalid_socket)
  1232. return s;
  1233. if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1234. {
  1235. // Try to enable the POSIX default behaviour of having IPV6_V6ONLY set to
  1236. // false. This will only succeed on Windows Vista and later versions of
  1237. // Windows, where a dual-stack IPv4/v6 implementation is available.
  1238. DWORD optval = 0;
  1239. ::setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
  1240. reinterpret_cast<const char*>(&optval), sizeof(optval));
  1241. }
  1242. ec = boost::system::error_code();
  1243. return s;
  1244. #elif defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
  1245. socket_type s = error_wrapper(::socket(af, type, protocol), ec);
  1246. if (s == invalid_socket)
  1247. return s;
  1248. int optval = 1;
  1249. int result = error_wrapper(::setsockopt(s,
  1250. SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
  1251. if (result != 0)
  1252. {
  1253. ::close(s);
  1254. return invalid_socket;
  1255. }
  1256. return s;
  1257. #else
  1258. int s = error_wrapper(::socket(af, type, protocol), ec);
  1259. if (s >= 0)
  1260. ec = boost::system::error_code();
  1261. return s;
  1262. #endif
  1263. }
  1264. template <typename SockLenType>
  1265. inline int call_setsockopt(SockLenType msghdr::*,
  1266. socket_type s, int level, int optname,
  1267. const void* optval, std::size_t optlen)
  1268. {
  1269. return ::setsockopt(s, level, optname,
  1270. (const char*)optval, (SockLenType)optlen);
  1271. }
  1272. int setsockopt(socket_type s, state_type& state, int level, int optname,
  1273. const void* optval, std::size_t optlen, boost::system::error_code& ec)
  1274. {
  1275. if (s == invalid_socket)
  1276. {
  1277. ec = boost::asio::error::bad_descriptor;
  1278. return socket_error_retval;
  1279. }
  1280. if (level == custom_socket_option_level && optname == always_fail_option)
  1281. {
  1282. ec = boost::asio::error::invalid_argument;
  1283. return socket_error_retval;
  1284. }
  1285. if (level == custom_socket_option_level
  1286. && optname == enable_connection_aborted_option)
  1287. {
  1288. if (optlen != sizeof(int))
  1289. {
  1290. ec = boost::asio::error::invalid_argument;
  1291. return socket_error_retval;
  1292. }
  1293. if (*static_cast<const int*>(optval))
  1294. state |= enable_connection_aborted;
  1295. else
  1296. state &= ~enable_connection_aborted;
  1297. ec = boost::system::error_code();
  1298. return 0;
  1299. }
  1300. if (level == SOL_SOCKET && optname == SO_LINGER)
  1301. state |= user_set_linger;
  1302. #if defined(__BORLANDC__)
  1303. // Mysteriously, using the getsockopt and setsockopt functions directly with
  1304. // Borland C++ results in incorrect values being set and read. The bug can be
  1305. // worked around by using function addresses resolved with GetProcAddress.
  1306. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  1307. {
  1308. typedef int (WSAAPI *sso_t)(SOCKET, int, int, const char*, int);
  1309. if (sso_t sso = (sso_t)::GetProcAddress(winsock_module, "setsockopt"))
  1310. {
  1311. clear_last_error();
  1312. return error_wrapper(sso(s, level, optname,
  1313. reinterpret_cast<const char*>(optval),
  1314. static_cast<int>(optlen)), ec);
  1315. }
  1316. }
  1317. ec = boost::asio::error::fault;
  1318. return socket_error_retval;
  1319. #else // defined(__BORLANDC__)
  1320. clear_last_error();
  1321. int result = error_wrapper(call_setsockopt(&msghdr::msg_namelen,
  1322. s, level, optname, optval, optlen), ec);
  1323. if (result == 0)
  1324. {
  1325. ec = boost::system::error_code();
  1326. #if defined(__MACH__) && defined(__APPLE__) \
  1327. || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  1328. // To implement portable behaviour for SO_REUSEADDR with UDP sockets we
  1329. // need to also set SO_REUSEPORT on BSD-based platforms.
  1330. if ((state & datagram_oriented)
  1331. && level == SOL_SOCKET && optname == SO_REUSEADDR)
  1332. {
  1333. call_setsockopt(&msghdr::msg_namelen, s,
  1334. SOL_SOCKET, SO_REUSEPORT, optval, optlen);
  1335. }
  1336. #endif
  1337. }
  1338. return result;
  1339. #endif // defined(__BORLANDC__)
  1340. }
  1341. template <typename SockLenType>
  1342. inline int call_getsockopt(SockLenType msghdr::*,
  1343. socket_type s, int level, int optname,
  1344. void* optval, std::size_t* optlen)
  1345. {
  1346. SockLenType tmp_optlen = (SockLenType)*optlen;
  1347. int result = ::getsockopt(s, level, optname, (char*)optval, &tmp_optlen);
  1348. *optlen = (std::size_t)tmp_optlen;
  1349. return result;
  1350. }
  1351. int getsockopt(socket_type s, state_type state, int level, int optname,
  1352. void* optval, size_t* optlen, boost::system::error_code& ec)
  1353. {
  1354. if (s == invalid_socket)
  1355. {
  1356. ec = boost::asio::error::bad_descriptor;
  1357. return socket_error_retval;
  1358. }
  1359. if (level == custom_socket_option_level && optname == always_fail_option)
  1360. {
  1361. ec = boost::asio::error::invalid_argument;
  1362. return socket_error_retval;
  1363. }
  1364. if (level == custom_socket_option_level
  1365. && optname == enable_connection_aborted_option)
  1366. {
  1367. if (*optlen != sizeof(int))
  1368. {
  1369. ec = boost::asio::error::invalid_argument;
  1370. return socket_error_retval;
  1371. }
  1372. *static_cast<int*>(optval) = (state & enable_connection_aborted) ? 1 : 0;
  1373. ec = boost::system::error_code();
  1374. return 0;
  1375. }
  1376. #if defined(__BORLANDC__)
  1377. // Mysteriously, using the getsockopt and setsockopt functions directly with
  1378. // Borland C++ results in incorrect values being set and read. The bug can be
  1379. // worked around by using function addresses resolved with GetProcAddress.
  1380. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  1381. {
  1382. typedef int (WSAAPI *gso_t)(SOCKET, int, int, char*, int*);
  1383. if (gso_t gso = (gso_t)::GetProcAddress(winsock_module, "getsockopt"))
  1384. {
  1385. clear_last_error();
  1386. int tmp_optlen = static_cast<int>(*optlen);
  1387. int result = error_wrapper(gso(s, level, optname,
  1388. reinterpret_cast<char*>(optval), &tmp_optlen), ec);
  1389. *optlen = static_cast<size_t>(tmp_optlen);
  1390. if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
  1391. && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
  1392. {
  1393. // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are
  1394. // only supported on Windows Vista and later. To simplify program logic
  1395. // we will fake success of getting this option and specify that the
  1396. // value is non-zero (i.e. true). This corresponds to the behavior of
  1397. // IPv6 sockets on Windows platforms pre-Vista.
  1398. *static_cast<DWORD*>(optval) = 1;
  1399. ec = boost::system::error_code();
  1400. }
  1401. return result;
  1402. }
  1403. }
  1404. ec = boost::asio::error::fault;
  1405. return socket_error_retval;
  1406. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1407. clear_last_error();
  1408. int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
  1409. s, level, optname, optval, optlen), ec);
  1410. if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
  1411. && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
  1412. {
  1413. // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are only
  1414. // supported on Windows Vista and later. To simplify program logic we will
  1415. // fake success of getting this option and specify that the value is
  1416. // non-zero (i.e. true). This corresponds to the behavior of IPv6 sockets
  1417. // on Windows platforms pre-Vista.
  1418. *static_cast<DWORD*>(optval) = 1;
  1419. ec = boost::system::error_code();
  1420. }
  1421. if (result == 0)
  1422. ec = boost::system::error_code();
  1423. return result;
  1424. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1425. clear_last_error();
  1426. int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
  1427. s, level, optname, optval, optlen), ec);
  1428. #if defined(__linux__)
  1429. if (result == 0 && level == SOL_SOCKET && *optlen == sizeof(int)
  1430. && (optname == SO_SNDBUF || optname == SO_RCVBUF))
  1431. {
  1432. // On Linux, setting SO_SNDBUF or SO_RCVBUF to N actually causes the kernel
  1433. // to set the buffer size to N*2. Linux puts additional stuff into the
  1434. // buffers so that only about half is actually available to the application.
  1435. // The retrieved value is divided by 2 here to make it appear as though the
  1436. // correct value has been set.
  1437. *static_cast<int*>(optval) /= 2;
  1438. }
  1439. #endif // defined(__linux__)
  1440. if (result == 0)
  1441. ec = boost::system::error_code();
  1442. return result;
  1443. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1444. }
  1445. template <typename SockLenType>
  1446. inline int call_getpeername(SockLenType msghdr::*,
  1447. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  1448. {
  1449. SockLenType tmp_addrlen = (SockLenType)*addrlen;
  1450. int result = ::getpeername(s, addr, &tmp_addrlen);
  1451. *addrlen = (std::size_t)tmp_addrlen;
  1452. return result;
  1453. }
  1454. int getpeername(socket_type s, socket_addr_type* addr,
  1455. std::size_t* addrlen, bool cached, boost::system::error_code& ec)
  1456. {
  1457. if (s == invalid_socket)
  1458. {
  1459. ec = boost::asio::error::bad_descriptor;
  1460. return socket_error_retval;
  1461. }
  1462. #if defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP) \
  1463. || defined(__CYGWIN__)
  1464. if (cached)
  1465. {
  1466. // Check if socket is still connected.
  1467. DWORD connect_time = 0;
  1468. size_t connect_time_len = sizeof(connect_time);
  1469. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_CONNECT_TIME,
  1470. &connect_time, &connect_time_len, ec) == socket_error_retval)
  1471. {
  1472. return socket_error_retval;
  1473. }
  1474. if (connect_time == 0xFFFFFFFF)
  1475. {
  1476. ec = boost::asio::error::not_connected;
  1477. return socket_error_retval;
  1478. }
  1479. // The cached value is still valid.
  1480. ec = boost::system::error_code();
  1481. return 0;
  1482. }
  1483. #else // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
  1484. // || defined(__CYGWIN__)
  1485. (void)cached;
  1486. #endif // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
  1487. // || defined(__CYGWIN__)
  1488. clear_last_error();
  1489. int result = error_wrapper(call_getpeername(
  1490. &msghdr::msg_namelen, s, addr, addrlen), ec);
  1491. if (result == 0)
  1492. ec = boost::system::error_code();
  1493. return result;
  1494. }
  1495. template <typename SockLenType>
  1496. inline int call_getsockname(SockLenType msghdr::*,
  1497. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  1498. {
  1499. SockLenType tmp_addrlen = (SockLenType)*addrlen;
  1500. int result = ::getsockname(s, addr, &tmp_addrlen);
  1501. *addrlen = (std::size_t)tmp_addrlen;
  1502. return result;
  1503. }
  1504. int getsockname(socket_type s, socket_addr_type* addr,
  1505. std::size_t* addrlen, boost::system::error_code& ec)
  1506. {
  1507. if (s == invalid_socket)
  1508. {
  1509. ec = boost::asio::error::bad_descriptor;
  1510. return socket_error_retval;
  1511. }
  1512. clear_last_error();
  1513. int result = error_wrapper(call_getsockname(
  1514. &msghdr::msg_namelen, s, addr, addrlen), ec);
  1515. if (result == 0)
  1516. ec = boost::system::error_code();
  1517. return result;
  1518. }
  1519. int ioctl(socket_type s, state_type& state, int cmd,
  1520. ioctl_arg_type* arg, boost::system::error_code& ec)
  1521. {
  1522. if (s == invalid_socket)
  1523. {
  1524. ec = boost::asio::error::bad_descriptor;
  1525. return socket_error_retval;
  1526. }
  1527. clear_last_error();
  1528. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1529. int result = error_wrapper(::ioctlsocket(s, cmd, arg), ec);
  1530. #elif defined(__MACH__) && defined(__APPLE__) \
  1531. || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  1532. int result = error_wrapper(::ioctl(s,
  1533. static_cast<unsigned int>(cmd), arg), ec);
  1534. #else
  1535. int result = error_wrapper(::ioctl(s, cmd, arg), ec);
  1536. #endif
  1537. if (result >= 0)
  1538. {
  1539. ec = boost::system::error_code();
  1540. // When updating the non-blocking mode we always perform the ioctl syscall,
  1541. // even if the flags would otherwise indicate that the socket is already in
  1542. // the correct state. This ensures that the underlying socket is put into
  1543. // the state that has been requested by the user. If the ioctl syscall was
  1544. // successful then we need to update the flags to match.
  1545. if (cmd == static_cast<int>(FIONBIO))
  1546. {
  1547. if (*arg)
  1548. {
  1549. state |= user_set_non_blocking;
  1550. }
  1551. else
  1552. {
  1553. // Clearing the non-blocking mode always overrides any internally-set
  1554. // non-blocking flag. Any subsequent asynchronous operations will need
  1555. // to re-enable non-blocking I/O.
  1556. state &= ~(user_set_non_blocking | internal_non_blocking);
  1557. }
  1558. }
  1559. }
  1560. return result;
  1561. }
  1562. int select(int nfds, fd_set* readfds, fd_set* writefds,
  1563. fd_set* exceptfds, timeval* timeout, boost::system::error_code& ec)
  1564. {
  1565. clear_last_error();
  1566. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1567. if (!readfds && !writefds && !exceptfds && timeout)
  1568. {
  1569. DWORD milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  1570. if (milliseconds == 0)
  1571. milliseconds = 1; // Force context switch.
  1572. ::Sleep(milliseconds);
  1573. ec = boost::system::error_code();
  1574. return 0;
  1575. }
  1576. // The select() call allows timeout values measured in microseconds, but the
  1577. // system clock (as wrapped by boost::posix_time::microsec_clock) typically
  1578. // has a resolution of 10 milliseconds. This can lead to a spinning select
  1579. // reactor, meaning increased CPU usage, when waiting for the earliest
  1580. // scheduled timeout if it's less than 10 milliseconds away. To avoid a tight
  1581. // spin we'll use a minimum timeout of 1 millisecond.
  1582. if (timeout && timeout->tv_sec == 0
  1583. && timeout->tv_usec > 0 && timeout->tv_usec < 1000)
  1584. timeout->tv_usec = 1000;
  1585. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1586. #if defined(__hpux) && defined(__SELECT)
  1587. timespec ts;
  1588. ts.tv_sec = timeout ? timeout->tv_sec : 0;
  1589. ts.tv_nsec = timeout ? timeout->tv_usec * 1000 : 0;
  1590. return error_wrapper(::pselect(nfds, readfds,
  1591. writefds, exceptfds, timeout ? &ts : 0, 0), ec);
  1592. #else
  1593. int result = error_wrapper(::select(nfds, readfds,
  1594. writefds, exceptfds, timeout), ec);
  1595. if (result >= 0)
  1596. ec = boost::system::error_code();
  1597. return result;
  1598. #endif
  1599. }
  1600. int poll_read(socket_type s, state_type state,
  1601. int msec, boost::system::error_code& ec)
  1602. {
  1603. if (s == invalid_socket)
  1604. {
  1605. ec = boost::asio::error::bad_descriptor;
  1606. return socket_error_retval;
  1607. }
  1608. #if defined(BOOST_ASIO_WINDOWS) \
  1609. || defined(__CYGWIN__) \
  1610. || defined(__SYMBIAN32__)
  1611. fd_set fds;
  1612. FD_ZERO(&fds);
  1613. FD_SET(s, &fds);
  1614. timeval timeout_obj;
  1615. timeval* timeout;
  1616. if (state & user_set_non_blocking)
  1617. {
  1618. timeout_obj.tv_sec = 0;
  1619. timeout_obj.tv_usec = 0;
  1620. timeout = &timeout_obj;
  1621. }
  1622. else if (msec >= 0)
  1623. {
  1624. timeout_obj.tv_sec = msec / 1000;
  1625. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1626. timeout = &timeout_obj;
  1627. }
  1628. else
  1629. timeout = 0;
  1630. clear_last_error();
  1631. int result = error_wrapper(::select(s + 1, &fds, 0, 0, timeout), ec);
  1632. #else // defined(BOOST_ASIO_WINDOWS)
  1633. // || defined(__CYGWIN__)
  1634. // || defined(__SYMBIAN32__)
  1635. pollfd fds;
  1636. fds.fd = s;
  1637. fds.events = POLLIN;
  1638. fds.revents = 0;
  1639. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1640. clear_last_error();
  1641. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1642. #endif // defined(BOOST_ASIO_WINDOWS)
  1643. // || defined(__CYGWIN__)
  1644. // || defined(__SYMBIAN32__)
  1645. if (result == 0)
  1646. ec = (state & user_set_non_blocking)
  1647. ? boost::asio::error::would_block : boost::system::error_code();
  1648. else if (result > 0)
  1649. ec = boost::system::error_code();
  1650. return result;
  1651. }
  1652. int poll_write(socket_type s, state_type state,
  1653. int msec, boost::system::error_code& ec)
  1654. {
  1655. if (s == invalid_socket)
  1656. {
  1657. ec = boost::asio::error::bad_descriptor;
  1658. return socket_error_retval;
  1659. }
  1660. #if defined(BOOST_ASIO_WINDOWS) \
  1661. || defined(__CYGWIN__) \
  1662. || defined(__SYMBIAN32__)
  1663. fd_set fds;
  1664. FD_ZERO(&fds);
  1665. FD_SET(s, &fds);
  1666. timeval timeout_obj;
  1667. timeval* timeout;
  1668. if (state & user_set_non_blocking)
  1669. {
  1670. timeout_obj.tv_sec = 0;
  1671. timeout_obj.tv_usec = 0;
  1672. timeout = &timeout_obj;
  1673. }
  1674. else if (msec >= 0)
  1675. {
  1676. timeout_obj.tv_sec = msec / 1000;
  1677. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1678. timeout = &timeout_obj;
  1679. }
  1680. else
  1681. timeout = 0;
  1682. clear_last_error();
  1683. int result = error_wrapper(::select(s + 1, 0, &fds, 0, timeout), ec);
  1684. #else // defined(BOOST_ASIO_WINDOWS)
  1685. // || defined(__CYGWIN__)
  1686. // || defined(__SYMBIAN32__)
  1687. pollfd fds;
  1688. fds.fd = s;
  1689. fds.events = POLLOUT;
  1690. fds.revents = 0;
  1691. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1692. clear_last_error();
  1693. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1694. #endif // defined(BOOST_ASIO_WINDOWS)
  1695. // || defined(__CYGWIN__)
  1696. // || defined(__SYMBIAN32__)
  1697. if (result == 0)
  1698. ec = (state & user_set_non_blocking)
  1699. ? boost::asio::error::would_block : boost::system::error_code();
  1700. else if (result > 0)
  1701. ec = boost::system::error_code();
  1702. return result;
  1703. }
  1704. int poll_error(socket_type s, state_type state,
  1705. int msec, boost::system::error_code& ec)
  1706. {
  1707. if (s == invalid_socket)
  1708. {
  1709. ec = boost::asio::error::bad_descriptor;
  1710. return socket_error_retval;
  1711. }
  1712. #if defined(BOOST_ASIO_WINDOWS) \
  1713. || defined(__CYGWIN__) \
  1714. || defined(__SYMBIAN32__)
  1715. fd_set fds;
  1716. FD_ZERO(&fds);
  1717. FD_SET(s, &fds);
  1718. timeval timeout_obj;
  1719. timeval* timeout;
  1720. if (state & user_set_non_blocking)
  1721. {
  1722. timeout_obj.tv_sec = 0;
  1723. timeout_obj.tv_usec = 0;
  1724. timeout = &timeout_obj;
  1725. }
  1726. else if (msec >= 0)
  1727. {
  1728. timeout_obj.tv_sec = msec / 1000;
  1729. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1730. timeout = &timeout_obj;
  1731. }
  1732. else
  1733. timeout = 0;
  1734. clear_last_error();
  1735. int result = error_wrapper(::select(s + 1, 0, 0, &fds, timeout), ec);
  1736. #else // defined(BOOST_ASIO_WINDOWS)
  1737. // || defined(__CYGWIN__)
  1738. // || defined(__SYMBIAN32__)
  1739. pollfd fds;
  1740. fds.fd = s;
  1741. fds.events = POLLPRI | POLLERR | POLLHUP;
  1742. fds.revents = 0;
  1743. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1744. clear_last_error();
  1745. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1746. #endif // defined(BOOST_ASIO_WINDOWS)
  1747. // || defined(__CYGWIN__)
  1748. // || defined(__SYMBIAN32__)
  1749. if (result == 0)
  1750. ec = (state & user_set_non_blocking)
  1751. ? boost::asio::error::would_block : boost::system::error_code();
  1752. else if (result > 0)
  1753. ec = boost::system::error_code();
  1754. return result;
  1755. }
  1756. int poll_connect(socket_type s, int msec, boost::system::error_code& ec)
  1757. {
  1758. if (s == invalid_socket)
  1759. {
  1760. ec = boost::asio::error::bad_descriptor;
  1761. return socket_error_retval;
  1762. }
  1763. #if defined(BOOST_ASIO_WINDOWS) \
  1764. || defined(__CYGWIN__) \
  1765. || defined(__SYMBIAN32__)
  1766. fd_set write_fds;
  1767. FD_ZERO(&write_fds);
  1768. FD_SET(s, &write_fds);
  1769. fd_set except_fds;
  1770. FD_ZERO(&except_fds);
  1771. FD_SET(s, &except_fds);
  1772. timeval timeout_obj;
  1773. timeval* timeout;
  1774. if (msec >= 0)
  1775. {
  1776. timeout_obj.tv_sec = msec / 1000;
  1777. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1778. timeout = &timeout_obj;
  1779. }
  1780. else
  1781. timeout = 0;
  1782. clear_last_error();
  1783. int result = error_wrapper(::select(
  1784. s + 1, 0, &write_fds, &except_fds, timeout), ec);
  1785. if (result >= 0)
  1786. ec = boost::system::error_code();
  1787. return result;
  1788. #else // defined(BOOST_ASIO_WINDOWS)
  1789. // || defined(__CYGWIN__)
  1790. // || defined(__SYMBIAN32__)
  1791. pollfd fds;
  1792. fds.fd = s;
  1793. fds.events = POLLOUT;
  1794. fds.revents = 0;
  1795. clear_last_error();
  1796. int result = error_wrapper(::poll(&fds, 1, msec), ec);
  1797. if (result >= 0)
  1798. ec = boost::system::error_code();
  1799. return result;
  1800. #endif // defined(BOOST_ASIO_WINDOWS)
  1801. // || defined(__CYGWIN__)
  1802. // || defined(__SYMBIAN32__)
  1803. }
  1804. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1805. const char* inet_ntop(int af, const void* src, char* dest, size_t length,
  1806. unsigned long scope_id, boost::system::error_code& ec)
  1807. {
  1808. clear_last_error();
  1809. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1810. using namespace std; // For sprintf.
  1811. const unsigned char* bytes = static_cast<const unsigned char*>(src);
  1812. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1813. {
  1814. sprintf_s(dest, length, "%u.%u.%u.%u",
  1815. bytes[0], bytes[1], bytes[2], bytes[3]);
  1816. return dest;
  1817. }
  1818. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1819. {
  1820. size_t n = 0, b = 0, z = 0;
  1821. while (n < length && b < 16)
  1822. {
  1823. if (bytes[b] == 0 && bytes[b + 1] == 0 && z == 0)
  1824. {
  1825. do b += 2; while (b < 16 && bytes[b] == 0 && bytes[b + 1] == 0);
  1826. n += sprintf_s(dest + n, length - n, ":%s", b < 16 ? "" : ":"), ++z;
  1827. }
  1828. else
  1829. {
  1830. n += sprintf_s(dest + n, length - n, "%s%x", b ? ":" : "",
  1831. (static_cast<u_long_type>(bytes[b]) << 8) | bytes[b + 1]);
  1832. b += 2;
  1833. }
  1834. }
  1835. if (scope_id)
  1836. n += sprintf_s(dest + n, length - n, "%%%lu", scope_id);
  1837. return dest;
  1838. }
  1839. else
  1840. {
  1841. ec = boost::asio::error::address_family_not_supported;
  1842. return 0;
  1843. }
  1844. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1845. using namespace std; // For memcpy.
  1846. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  1847. {
  1848. ec = boost::asio::error::address_family_not_supported;
  1849. return 0;
  1850. }
  1851. union
  1852. {
  1853. socket_addr_type base;
  1854. sockaddr_storage_type storage;
  1855. sockaddr_in4_type v4;
  1856. sockaddr_in6_type v6;
  1857. } address;
  1858. DWORD address_length;
  1859. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1860. {
  1861. address_length = sizeof(sockaddr_in4_type);
  1862. address.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  1863. address.v4.sin_port = 0;
  1864. memcpy(&address.v4.sin_addr, src, sizeof(in4_addr_type));
  1865. }
  1866. else // AF_INET6
  1867. {
  1868. address_length = sizeof(sockaddr_in6_type);
  1869. address.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  1870. address.v6.sin6_port = 0;
  1871. address.v6.sin6_flowinfo = 0;
  1872. address.v6.sin6_scope_id = scope_id;
  1873. memcpy(&address.v6.sin6_addr, src, sizeof(in6_addr_type));
  1874. }
  1875. DWORD string_length = static_cast<DWORD>(length);
  1876. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  1877. LPWSTR string_buffer = (LPWSTR)_alloca(length * sizeof(WCHAR));
  1878. int result = error_wrapper(::WSAAddressToStringW(&address.base,
  1879. address_length, 0, string_buffer, &string_length), ec);
  1880. ::WideCharToMultiByte(CP_ACP, 0, string_buffer, -1,
  1881. dest, static_cast<int>(length), 0, 0);
  1882. #else
  1883. int result = error_wrapper(::WSAAddressToStringA(
  1884. &address.base, address_length, 0, dest, &string_length), ec);
  1885. #endif
  1886. // Windows may set error code on success.
  1887. if (result != socket_error_retval)
  1888. ec = boost::system::error_code();
  1889. // Windows may not set an error code on failure.
  1890. else if (result == socket_error_retval && !ec)
  1891. ec = boost::asio::error::invalid_argument;
  1892. return result == socket_error_retval ? 0 : dest;
  1893. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1894. const char* result = error_wrapper(::inet_ntop(
  1895. af, src, dest, static_cast<int>(length)), ec);
  1896. if (result == 0 && !ec)
  1897. ec = boost::asio::error::invalid_argument;
  1898. if (result != 0 && af == BOOST_ASIO_OS_DEF(AF_INET6) && scope_id != 0)
  1899. {
  1900. using namespace std; // For strcat and sprintf.
  1901. char if_name[IF_NAMESIZE + 1] = "%";
  1902. const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
  1903. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  1904. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  1905. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  1906. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  1907. if ((!is_link_local && !is_multicast_link_local)
  1908. || if_indextoname(static_cast<unsigned>(scope_id), if_name + 1) == 0)
  1909. sprintf(if_name + 1, "%lu", scope_id);
  1910. strcat(dest, if_name);
  1911. }
  1912. return result;
  1913. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1914. }
  1915. int inet_pton(int af, const char* src, void* dest,
  1916. unsigned long* scope_id, boost::system::error_code& ec)
  1917. {
  1918. clear_last_error();
  1919. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1920. using namespace std; // For sscanf.
  1921. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1922. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1923. {
  1924. unsigned int b0, b1, b2, b3;
  1925. if (sscanf_s(src, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) != 4)
  1926. {
  1927. ec = boost::asio::error::invalid_argument;
  1928. return -1;
  1929. }
  1930. if (b0 > 255 || b1 > 255 || b2 > 255 || b3 > 255)
  1931. {
  1932. ec = boost::asio::error::invalid_argument;
  1933. return -1;
  1934. }
  1935. bytes[0] = static_cast<unsigned char>(b0);
  1936. bytes[1] = static_cast<unsigned char>(b1);
  1937. bytes[2] = static_cast<unsigned char>(b2);
  1938. bytes[3] = static_cast<unsigned char>(b3);
  1939. ec = boost::system::error_code();
  1940. return 1;
  1941. }
  1942. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1943. {
  1944. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1945. std::memset(bytes, 0, 16);
  1946. unsigned char back_bytes[16] = { 0 };
  1947. int num_front_bytes = 0, num_back_bytes = 0;
  1948. const char* p = src;
  1949. enum { fword, fcolon, bword, scope, done } state = fword;
  1950. unsigned long current_word = 0;
  1951. while (state != done)
  1952. {
  1953. if (current_word > 0xFFFF)
  1954. {
  1955. ec = boost::asio::error::invalid_argument;
  1956. return -1;
  1957. }
  1958. switch (state)
  1959. {
  1960. case fword:
  1961. if (*p >= '0' && *p <= '9')
  1962. current_word = current_word * 16 + *p++ - '0';
  1963. else if (*p >= 'a' && *p <= 'f')
  1964. current_word = current_word * 16 + *p++ - 'a' + 10;
  1965. else if (*p >= 'A' && *p <= 'F')
  1966. current_word = current_word * 16 + *p++ - 'A' + 10;
  1967. else
  1968. {
  1969. if (num_front_bytes == 16)
  1970. {
  1971. ec = boost::asio::error::invalid_argument;
  1972. return -1;
  1973. }
  1974. bytes[num_front_bytes++] = (current_word >> 8) & 0xFF;
  1975. bytes[num_front_bytes++] = current_word & 0xFF;
  1976. current_word = 0;
  1977. if (*p == ':')
  1978. state = fcolon, ++p;
  1979. else if (*p == '%')
  1980. state = scope, ++p;
  1981. else if (*p == 0)
  1982. state = done;
  1983. else
  1984. {
  1985. ec = boost::asio::error::invalid_argument;
  1986. return -1;
  1987. }
  1988. }
  1989. break;
  1990. case fcolon:
  1991. if (*p == ':')
  1992. state = bword, ++p;
  1993. else
  1994. state = fword;
  1995. break;
  1996. case bword:
  1997. if (*p >= '0' && *p <= '9')
  1998. current_word = current_word * 16 + *p++ - '0';
  1999. else if (*p >= 'a' && *p <= 'f')
  2000. current_word = current_word * 16 + *p++ - 'a' + 10;
  2001. else if (*p >= 'A' && *p <= 'F')
  2002. current_word = current_word * 16 + *p++ - 'A' + 10;
  2003. else
  2004. {
  2005. if (num_front_bytes + num_back_bytes == 16)
  2006. {
  2007. ec = boost::asio::error::invalid_argument;
  2008. return -1;
  2009. }
  2010. back_bytes[num_back_bytes++] = (current_word >> 8) & 0xFF;
  2011. back_bytes[num_back_bytes++] = current_word & 0xFF;
  2012. current_word = 0;
  2013. if (*p == ':')
  2014. state = bword, ++p;
  2015. else if (*p == '%')
  2016. state = scope, ++p;
  2017. else if (*p == 0)
  2018. state = done;
  2019. else
  2020. {
  2021. ec = boost::asio::error::invalid_argument;
  2022. return -1;
  2023. }
  2024. }
  2025. break;
  2026. case scope:
  2027. if (*p >= '0' && *p <= '9')
  2028. current_word = current_word * 10 + *p++ - '0';
  2029. else if (*p == 0)
  2030. *scope_id = current_word, state = done;
  2031. else
  2032. {
  2033. ec = boost::asio::error::invalid_argument;
  2034. return -1;
  2035. }
  2036. break;
  2037. default:
  2038. break;
  2039. }
  2040. }
  2041. for (int i = 0; i < num_back_bytes; ++i)
  2042. bytes[16 - num_back_bytes + i] = back_bytes[i];
  2043. ec = boost::system::error_code();
  2044. return 1;
  2045. }
  2046. else
  2047. {
  2048. ec = boost::asio::error::address_family_not_supported;
  2049. return -1;
  2050. }
  2051. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2052. using namespace std; // For memcpy and strcmp.
  2053. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  2054. {
  2055. ec = boost::asio::error::address_family_not_supported;
  2056. return -1;
  2057. }
  2058. union
  2059. {
  2060. socket_addr_type base;
  2061. sockaddr_storage_type storage;
  2062. sockaddr_in4_type v4;
  2063. sockaddr_in6_type v6;
  2064. } address;
  2065. int address_length = sizeof(sockaddr_storage_type);
  2066. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  2067. int num_wide_chars = static_cast<int>(strlen(src)) + 1;
  2068. LPWSTR wide_buffer = (LPWSTR)_alloca(num_wide_chars * sizeof(WCHAR));
  2069. ::MultiByteToWideChar(CP_ACP, 0, src, -1, wide_buffer, num_wide_chars);
  2070. int result = error_wrapper(::WSAStringToAddressW(
  2071. wide_buffer, af, 0, &address.base, &address_length), ec);
  2072. #else
  2073. int result = error_wrapper(::WSAStringToAddressA(
  2074. const_cast<char*>(src), af, 0, &address.base, &address_length), ec);
  2075. #endif
  2076. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  2077. {
  2078. if (result != socket_error_retval)
  2079. {
  2080. memcpy(dest, &address.v4.sin_addr, sizeof(in4_addr_type));
  2081. ec = boost::system::error_code();
  2082. }
  2083. else if (strcmp(src, "255.255.255.255") == 0)
  2084. {
  2085. static_cast<in4_addr_type*>(dest)->s_addr = INADDR_NONE;
  2086. ec = boost::system::error_code();
  2087. }
  2088. }
  2089. else // AF_INET6
  2090. {
  2091. if (result != socket_error_retval)
  2092. {
  2093. memcpy(dest, &address.v6.sin6_addr, sizeof(in6_addr_type));
  2094. if (scope_id)
  2095. *scope_id = address.v6.sin6_scope_id;
  2096. ec = boost::system::error_code();
  2097. }
  2098. }
  2099. // Windows may not set an error code on failure.
  2100. if (result == socket_error_retval && !ec)
  2101. ec = boost::asio::error::invalid_argument;
  2102. if (result != socket_error_retval)
  2103. ec = boost::system::error_code();
  2104. return result == socket_error_retval ? -1 : 1;
  2105. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2106. using namespace std; // For strchr, memcpy and atoi.
  2107. // On some platforms, inet_pton fails if an address string contains a scope
  2108. // id. Detect and remove the scope id before passing the string to inet_pton.
  2109. const bool is_v6 = (af == BOOST_ASIO_OS_DEF(AF_INET6));
  2110. const char* if_name = is_v6 ? strchr(src, '%') : 0;
  2111. char src_buf[max_addr_v6_str_len + 1];
  2112. const char* src_ptr = src;
  2113. if (if_name != 0)
  2114. {
  2115. if (if_name - src > max_addr_v6_str_len)
  2116. {
  2117. ec = boost::asio::error::invalid_argument;
  2118. return 0;
  2119. }
  2120. memcpy(src_buf, src, if_name - src);
  2121. src_buf[if_name - src] = 0;
  2122. src_ptr = src_buf;
  2123. }
  2124. int result = error_wrapper(::inet_pton(af, src_ptr, dest), ec);
  2125. if (result <= 0 && !ec)
  2126. ec = boost::asio::error::invalid_argument;
  2127. if (result > 0 && is_v6 && scope_id)
  2128. {
  2129. using namespace std; // For strchr and atoi.
  2130. *scope_id = 0;
  2131. if (if_name != 0)
  2132. {
  2133. in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
  2134. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  2135. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  2136. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  2137. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  2138. if (is_link_local || is_multicast_link_local)
  2139. *scope_id = if_nametoindex(if_name + 1);
  2140. if (*scope_id == 0)
  2141. *scope_id = atoi(if_name + 1);
  2142. }
  2143. }
  2144. return result;
  2145. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2146. }
  2147. int gethostname(char* name, int namelen, boost::system::error_code& ec)
  2148. {
  2149. clear_last_error();
  2150. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2151. try
  2152. {
  2153. using namespace Windows::Foundation::Collections;
  2154. using namespace Windows::Networking;
  2155. using namespace Windows::Networking::Connectivity;
  2156. IVectorView<HostName^>^ hostnames = NetworkInformation::GetHostNames();
  2157. for (unsigned i = 0; i < hostnames->Size; ++i)
  2158. {
  2159. HostName^ hostname = hostnames->GetAt(i);
  2160. if (hostname->Type == HostNameType::DomainName)
  2161. {
  2162. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  2163. std::string raw_name = converter.to_bytes(hostname->RawName->Data());
  2164. if (namelen > 0 && raw_name.size() < static_cast<std::size_t>(namelen))
  2165. {
  2166. strcpy_s(name, namelen, raw_name.c_str());
  2167. return 0;
  2168. }
  2169. }
  2170. }
  2171. return -1;
  2172. }
  2173. catch (Platform::Exception^ e)
  2174. {
  2175. ec = boost::system::error_code(e->HResult,
  2176. boost::system::system_category());
  2177. return -1;
  2178. }
  2179. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2180. int result = error_wrapper(::gethostname(name, namelen), ec);
  2181. # if defined(BOOST_ASIO_WINDOWS)
  2182. if (result == 0)
  2183. ec = boost::system::error_code();
  2184. # endif // defined(BOOST_ASIO_WINDOWS)
  2185. return result;
  2186. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2187. }
  2188. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2189. #if !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2190. // The following functions are only needed for emulation of getaddrinfo and
  2191. // getnameinfo.
  2192. inline boost::system::error_code translate_netdb_error(int error)
  2193. {
  2194. switch (error)
  2195. {
  2196. case 0:
  2197. return boost::system::error_code();
  2198. case HOST_NOT_FOUND:
  2199. return boost::asio::error::host_not_found;
  2200. case TRY_AGAIN:
  2201. return boost::asio::error::host_not_found_try_again;
  2202. case NO_RECOVERY:
  2203. return boost::asio::error::no_recovery;
  2204. case NO_DATA:
  2205. return boost::asio::error::no_data;
  2206. default:
  2207. BOOST_ASIO_ASSERT(false);
  2208. return boost::asio::error::invalid_argument;
  2209. }
  2210. }
  2211. inline hostent* gethostbyaddr(const char* addr, int length, int af,
  2212. hostent* result, char* buffer, int buflength, boost::system::error_code& ec)
  2213. {
  2214. clear_last_error();
  2215. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2216. (void)(buffer);
  2217. (void)(buflength);
  2218. hostent* retval = error_wrapper(::gethostbyaddr(addr, length, af), ec);
  2219. if (!retval)
  2220. return 0;
  2221. ec = boost::system::error_code();
  2222. *result = *retval;
  2223. return retval;
  2224. #elif defined(__sun) || defined(__QNX__)
  2225. int error = 0;
  2226. hostent* retval = error_wrapper(::gethostbyaddr_r(addr, length, af, result,
  2227. buffer, buflength, &error), ec);
  2228. if (error)
  2229. ec = translate_netdb_error(error);
  2230. return retval;
  2231. #elif defined(__MACH__) && defined(__APPLE__)
  2232. (void)(buffer);
  2233. (void)(buflength);
  2234. int error = 0;
  2235. hostent* retval = error_wrapper(::getipnodebyaddr(
  2236. addr, length, af, &error), ec);
  2237. if (error)
  2238. ec = translate_netdb_error(error);
  2239. if (!retval)
  2240. return 0;
  2241. *result = *retval;
  2242. return retval;
  2243. #else
  2244. hostent* retval = 0;
  2245. int error = 0;
  2246. error_wrapper(::gethostbyaddr_r(addr, length, af, result, buffer,
  2247. buflength, &retval, &error), ec);
  2248. if (error)
  2249. ec = translate_netdb_error(error);
  2250. return retval;
  2251. #endif
  2252. }
  2253. inline hostent* gethostbyname(const char* name, int af, struct hostent* result,
  2254. char* buffer, int buflength, int ai_flags, boost::system::error_code& ec)
  2255. {
  2256. clear_last_error();
  2257. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2258. (void)(buffer);
  2259. (void)(buflength);
  2260. (void)(ai_flags);
  2261. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2262. {
  2263. ec = boost::asio::error::address_family_not_supported;
  2264. return 0;
  2265. }
  2266. hostent* retval = error_wrapper(::gethostbyname(name), ec);
  2267. if (!retval)
  2268. return 0;
  2269. ec = boost::system::error_code();
  2270. *result = *retval;
  2271. return result;
  2272. #elif defined(__sun) || defined(__QNX__)
  2273. (void)(ai_flags);
  2274. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2275. {
  2276. ec = boost::asio::error::address_family_not_supported;
  2277. return 0;
  2278. }
  2279. int error = 0;
  2280. hostent* retval = error_wrapper(::gethostbyname_r(name, result, buffer,
  2281. buflength, &error), ec);
  2282. if (error)
  2283. ec = translate_netdb_error(error);
  2284. return retval;
  2285. #elif defined(__MACH__) && defined(__APPLE__)
  2286. (void)(buffer);
  2287. (void)(buflength);
  2288. int error = 0;
  2289. hostent* retval = error_wrapper(::getipnodebyname(
  2290. name, af, ai_flags, &error), ec);
  2291. if (error)
  2292. ec = translate_netdb_error(error);
  2293. if (!retval)
  2294. return 0;
  2295. *result = *retval;
  2296. return retval;
  2297. #else
  2298. (void)(ai_flags);
  2299. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2300. {
  2301. ec = boost::asio::error::address_family_not_supported;
  2302. return 0;
  2303. }
  2304. hostent* retval = 0;
  2305. int error = 0;
  2306. error_wrapper(::gethostbyname_r(name, result,
  2307. buffer, buflength, &retval, &error), ec);
  2308. if (error)
  2309. ec = translate_netdb_error(error);
  2310. return retval;
  2311. #endif
  2312. }
  2313. inline void freehostent(hostent* h)
  2314. {
  2315. #if defined(__MACH__) && defined(__APPLE__)
  2316. if (h)
  2317. ::freehostent(h);
  2318. #else
  2319. (void)(h);
  2320. #endif
  2321. }
  2322. // Emulation of getaddrinfo based on implementation in:
  2323. // Stevens, W. R., UNIX Network Programming Vol. 1, 2nd Ed., Prentice-Hall 1998.
  2324. struct gai_search
  2325. {
  2326. const char* host;
  2327. int family;
  2328. };
  2329. inline int gai_nsearch(const char* host,
  2330. const addrinfo_type* hints, gai_search (&search)[2])
  2331. {
  2332. int search_count = 0;
  2333. if (host == 0 || host[0] == '\0')
  2334. {
  2335. if (hints->ai_flags & AI_PASSIVE)
  2336. {
  2337. // No host and AI_PASSIVE implies wildcard bind.
  2338. switch (hints->ai_family)
  2339. {
  2340. case BOOST_ASIO_OS_DEF(AF_INET):
  2341. search[search_count].host = "0.0.0.0";
  2342. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2343. ++search_count;
  2344. break;
  2345. case BOOST_ASIO_OS_DEF(AF_INET6):
  2346. search[search_count].host = "0::0";
  2347. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2348. ++search_count;
  2349. break;
  2350. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2351. search[search_count].host = "0::0";
  2352. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2353. ++search_count;
  2354. search[search_count].host = "0.0.0.0";
  2355. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2356. ++search_count;
  2357. break;
  2358. default:
  2359. break;
  2360. }
  2361. }
  2362. else
  2363. {
  2364. // No host and not AI_PASSIVE means connect to local host.
  2365. switch (hints->ai_family)
  2366. {
  2367. case BOOST_ASIO_OS_DEF(AF_INET):
  2368. search[search_count].host = "localhost";
  2369. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2370. ++search_count;
  2371. break;
  2372. case BOOST_ASIO_OS_DEF(AF_INET6):
  2373. search[search_count].host = "localhost";
  2374. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2375. ++search_count;
  2376. break;
  2377. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2378. search[search_count].host = "localhost";
  2379. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2380. ++search_count;
  2381. search[search_count].host = "localhost";
  2382. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2383. ++search_count;
  2384. break;
  2385. default:
  2386. break;
  2387. }
  2388. }
  2389. }
  2390. else
  2391. {
  2392. // Host is specified.
  2393. switch (hints->ai_family)
  2394. {
  2395. case BOOST_ASIO_OS_DEF(AF_INET):
  2396. search[search_count].host = host;
  2397. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2398. ++search_count;
  2399. break;
  2400. case BOOST_ASIO_OS_DEF(AF_INET6):
  2401. search[search_count].host = host;
  2402. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2403. ++search_count;
  2404. break;
  2405. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2406. search[search_count].host = host;
  2407. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2408. ++search_count;
  2409. search[search_count].host = host;
  2410. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2411. ++search_count;
  2412. break;
  2413. default:
  2414. break;
  2415. }
  2416. }
  2417. return search_count;
  2418. }
  2419. template <typename T>
  2420. inline T* gai_alloc(std::size_t size = sizeof(T))
  2421. {
  2422. using namespace std;
  2423. T* p = static_cast<T*>(::operator new(size, std::nothrow));
  2424. if (p)
  2425. memset(p, 0, size);
  2426. return p;
  2427. }
  2428. inline void gai_free(void* p)
  2429. {
  2430. ::operator delete(p);
  2431. }
  2432. inline void gai_strcpy(char* target, const char* source, std::size_t max_size)
  2433. {
  2434. using namespace std;
  2435. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2436. strcpy_s(target, max_size, source);
  2437. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2438. *target = 0;
  2439. if (max_size > 0)
  2440. strncat(target, source, max_size - 1);
  2441. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2442. }
  2443. enum { gai_clone_flag = 1 << 30 };
  2444. inline int gai_aistruct(addrinfo_type*** next, const addrinfo_type* hints,
  2445. const void* addr, int family)
  2446. {
  2447. using namespace std;
  2448. addrinfo_type* ai = gai_alloc<addrinfo_type>();
  2449. if (ai == 0)
  2450. return EAI_MEMORY;
  2451. ai->ai_next = 0;
  2452. **next = ai;
  2453. *next = &ai->ai_next;
  2454. ai->ai_canonname = 0;
  2455. ai->ai_socktype = hints->ai_socktype;
  2456. if (ai->ai_socktype == 0)
  2457. ai->ai_flags |= gai_clone_flag;
  2458. ai->ai_protocol = hints->ai_protocol;
  2459. ai->ai_family = family;
  2460. switch (ai->ai_family)
  2461. {
  2462. case BOOST_ASIO_OS_DEF(AF_INET):
  2463. {
  2464. sockaddr_in4_type* sinptr = gai_alloc<sockaddr_in4_type>();
  2465. if (sinptr == 0)
  2466. return EAI_MEMORY;
  2467. sinptr->sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  2468. memcpy(&sinptr->sin_addr, addr, sizeof(in4_addr_type));
  2469. ai->ai_addr = reinterpret_cast<sockaddr*>(sinptr);
  2470. ai->ai_addrlen = sizeof(sockaddr_in4_type);
  2471. break;
  2472. }
  2473. case BOOST_ASIO_OS_DEF(AF_INET6):
  2474. {
  2475. sockaddr_in6_type* sin6ptr = gai_alloc<sockaddr_in6_type>();
  2476. if (sin6ptr == 0)
  2477. return EAI_MEMORY;
  2478. sin6ptr->sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  2479. memcpy(&sin6ptr->sin6_addr, addr, sizeof(in6_addr_type));
  2480. ai->ai_addr = reinterpret_cast<sockaddr*>(sin6ptr);
  2481. ai->ai_addrlen = sizeof(sockaddr_in6_type);
  2482. break;
  2483. }
  2484. default:
  2485. break;
  2486. }
  2487. return 0;
  2488. }
  2489. inline addrinfo_type* gai_clone(addrinfo_type* ai)
  2490. {
  2491. using namespace std;
  2492. addrinfo_type* new_ai = gai_alloc<addrinfo_type>();
  2493. if (new_ai == 0)
  2494. return new_ai;
  2495. new_ai->ai_next = ai->ai_next;
  2496. ai->ai_next = new_ai;
  2497. new_ai->ai_flags = 0;
  2498. new_ai->ai_family = ai->ai_family;
  2499. new_ai->ai_socktype = ai->ai_socktype;
  2500. new_ai->ai_protocol = ai->ai_protocol;
  2501. new_ai->ai_canonname = 0;
  2502. new_ai->ai_addrlen = ai->ai_addrlen;
  2503. new_ai->ai_addr = gai_alloc<sockaddr>(ai->ai_addrlen);
  2504. memcpy(new_ai->ai_addr, ai->ai_addr, ai->ai_addrlen);
  2505. return new_ai;
  2506. }
  2507. inline int gai_port(addrinfo_type* aihead, int port, int socktype)
  2508. {
  2509. int num_found = 0;
  2510. for (addrinfo_type* ai = aihead; ai; ai = ai->ai_next)
  2511. {
  2512. if (ai->ai_flags & gai_clone_flag)
  2513. {
  2514. if (ai->ai_socktype != 0)
  2515. {
  2516. ai = gai_clone(ai);
  2517. if (ai == 0)
  2518. return -1;
  2519. // ai now points to newly cloned entry.
  2520. }
  2521. }
  2522. else if (ai->ai_socktype != socktype)
  2523. {
  2524. // Ignore if mismatch on socket type.
  2525. continue;
  2526. }
  2527. ai->ai_socktype = socktype;
  2528. switch (ai->ai_family)
  2529. {
  2530. case BOOST_ASIO_OS_DEF(AF_INET):
  2531. {
  2532. sockaddr_in4_type* sinptr =
  2533. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  2534. sinptr->sin_port = port;
  2535. ++num_found;
  2536. break;
  2537. }
  2538. case BOOST_ASIO_OS_DEF(AF_INET6):
  2539. {
  2540. sockaddr_in6_type* sin6ptr =
  2541. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  2542. sin6ptr->sin6_port = port;
  2543. ++num_found;
  2544. break;
  2545. }
  2546. default:
  2547. break;
  2548. }
  2549. }
  2550. return num_found;
  2551. }
  2552. inline int gai_serv(addrinfo_type* aihead,
  2553. const addrinfo_type* hints, const char* serv)
  2554. {
  2555. using namespace std;
  2556. int num_found = 0;
  2557. if (
  2558. #if defined(AI_NUMERICSERV)
  2559. (hints->ai_flags & AI_NUMERICSERV) ||
  2560. #endif
  2561. isdigit(static_cast<unsigned char>(serv[0])))
  2562. {
  2563. int port = htons(atoi(serv));
  2564. if (hints->ai_socktype)
  2565. {
  2566. // Caller specifies socket type.
  2567. int rc = gai_port(aihead, port, hints->ai_socktype);
  2568. if (rc < 0)
  2569. return EAI_MEMORY;
  2570. num_found += rc;
  2571. }
  2572. else
  2573. {
  2574. // Caller does not specify socket type.
  2575. int rc = gai_port(aihead, port, SOCK_STREAM);
  2576. if (rc < 0)
  2577. return EAI_MEMORY;
  2578. num_found += rc;
  2579. rc = gai_port(aihead, port, SOCK_DGRAM);
  2580. if (rc < 0)
  2581. return EAI_MEMORY;
  2582. num_found += rc;
  2583. }
  2584. }
  2585. else
  2586. {
  2587. // Try service name with TCP first, then UDP.
  2588. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
  2589. {
  2590. servent* sptr = getservbyname(serv, "tcp");
  2591. if (sptr != 0)
  2592. {
  2593. int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
  2594. if (rc < 0)
  2595. return EAI_MEMORY;
  2596. num_found += rc;
  2597. }
  2598. }
  2599. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
  2600. {
  2601. servent* sptr = getservbyname(serv, "udp");
  2602. if (sptr != 0)
  2603. {
  2604. int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
  2605. if (rc < 0)
  2606. return EAI_MEMORY;
  2607. num_found += rc;
  2608. }
  2609. }
  2610. }
  2611. if (num_found == 0)
  2612. {
  2613. if (hints->ai_socktype == 0)
  2614. {
  2615. // All calls to getservbyname() failed.
  2616. return EAI_NONAME;
  2617. }
  2618. else
  2619. {
  2620. // Service not supported for socket type.
  2621. return EAI_SERVICE;
  2622. }
  2623. }
  2624. return 0;
  2625. }
  2626. inline int gai_echeck(const char* host, const char* service,
  2627. int flags, int family, int socktype, int protocol)
  2628. {
  2629. (void)(flags);
  2630. (void)(protocol);
  2631. // Host or service must be specified.
  2632. if (host == 0 || host[0] == '\0')
  2633. if (service == 0 || service[0] == '\0')
  2634. return EAI_NONAME;
  2635. // Check combination of family and socket type.
  2636. switch (family)
  2637. {
  2638. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2639. break;
  2640. case BOOST_ASIO_OS_DEF(AF_INET):
  2641. case BOOST_ASIO_OS_DEF(AF_INET6):
  2642. if (service != 0 && service[0] != '\0')
  2643. if (socktype != 0 && socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
  2644. return EAI_SOCKTYPE;
  2645. break;
  2646. default:
  2647. return EAI_FAMILY;
  2648. }
  2649. return 0;
  2650. }
  2651. inline void freeaddrinfo_emulation(addrinfo_type* aihead)
  2652. {
  2653. addrinfo_type* ai = aihead;
  2654. while (ai)
  2655. {
  2656. gai_free(ai->ai_addr);
  2657. gai_free(ai->ai_canonname);
  2658. addrinfo_type* ainext = ai->ai_next;
  2659. gai_free(ai);
  2660. ai = ainext;
  2661. }
  2662. }
  2663. inline int getaddrinfo_emulation(const char* host, const char* service,
  2664. const addrinfo_type* hintsp, addrinfo_type** result)
  2665. {
  2666. // Set up linked list of addrinfo structures.
  2667. addrinfo_type* aihead = 0;
  2668. addrinfo_type** ainext = &aihead;
  2669. char* canon = 0;
  2670. // Supply default hints if not specified by caller.
  2671. addrinfo_type hints = addrinfo_type();
  2672. hints.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
  2673. if (hintsp)
  2674. hints = *hintsp;
  2675. // If the resolution is not specifically for AF_INET6, remove the AI_V4MAPPED
  2676. // and AI_ALL flags.
  2677. #if defined(AI_V4MAPPED)
  2678. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2679. hints.ai_flags &= ~AI_V4MAPPED;
  2680. #endif
  2681. #if defined(AI_ALL)
  2682. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2683. hints.ai_flags &= ~AI_ALL;
  2684. #endif
  2685. // Basic error checking.
  2686. int rc = gai_echeck(host, service, hints.ai_flags, hints.ai_family,
  2687. hints.ai_socktype, hints.ai_protocol);
  2688. if (rc != 0)
  2689. {
  2690. freeaddrinfo_emulation(aihead);
  2691. return rc;
  2692. }
  2693. gai_search search[2];
  2694. int search_count = gai_nsearch(host, &hints, search);
  2695. for (gai_search* sptr = search; sptr < search + search_count; ++sptr)
  2696. {
  2697. // Check for IPv4 dotted decimal string.
  2698. in4_addr_type inaddr;
  2699. boost::system::error_code ec;
  2700. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET),
  2701. sptr->host, &inaddr, 0, ec) == 1)
  2702. {
  2703. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2704. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET))
  2705. {
  2706. freeaddrinfo_emulation(aihead);
  2707. gai_free(canon);
  2708. return EAI_FAMILY;
  2709. }
  2710. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET))
  2711. {
  2712. rc = gai_aistruct(&ainext, &hints, &inaddr, BOOST_ASIO_OS_DEF(AF_INET));
  2713. if (rc != 0)
  2714. {
  2715. freeaddrinfo_emulation(aihead);
  2716. gai_free(canon);
  2717. return rc;
  2718. }
  2719. }
  2720. continue;
  2721. }
  2722. // Check for IPv6 hex string.
  2723. in6_addr_type in6addr;
  2724. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6),
  2725. sptr->host, &in6addr, 0, ec) == 1)
  2726. {
  2727. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2728. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2729. {
  2730. freeaddrinfo_emulation(aihead);
  2731. gai_free(canon);
  2732. return EAI_FAMILY;
  2733. }
  2734. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET6))
  2735. {
  2736. rc = gai_aistruct(&ainext, &hints, &in6addr,
  2737. BOOST_ASIO_OS_DEF(AF_INET6));
  2738. if (rc != 0)
  2739. {
  2740. freeaddrinfo_emulation(aihead);
  2741. gai_free(canon);
  2742. return rc;
  2743. }
  2744. }
  2745. continue;
  2746. }
  2747. // Look up hostname.
  2748. hostent hent;
  2749. char hbuf[8192] = "";
  2750. hostent* hptr = socket_ops::gethostbyname(sptr->host,
  2751. sptr->family, &hent, hbuf, sizeof(hbuf), hints.ai_flags, ec);
  2752. if (hptr == 0)
  2753. {
  2754. if (search_count == 2)
  2755. {
  2756. // Failure is OK if there are multiple searches.
  2757. continue;
  2758. }
  2759. freeaddrinfo_emulation(aihead);
  2760. gai_free(canon);
  2761. if (ec == boost::asio::error::host_not_found)
  2762. return EAI_NONAME;
  2763. if (ec == boost::asio::error::host_not_found_try_again)
  2764. return EAI_AGAIN;
  2765. if (ec == boost::asio::error::no_recovery)
  2766. return EAI_FAIL;
  2767. if (ec == boost::asio::error::no_data)
  2768. return EAI_NONAME;
  2769. return EAI_NONAME;
  2770. }
  2771. // Check for address family mismatch if one was specified.
  2772. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2773. && hints.ai_family != hptr->h_addrtype)
  2774. {
  2775. freeaddrinfo_emulation(aihead);
  2776. gai_free(canon);
  2777. socket_ops::freehostent(hptr);
  2778. return EAI_FAMILY;
  2779. }
  2780. // Save canonical name first time.
  2781. if (host != 0 && host[0] != '\0' && hptr->h_name && hptr->h_name[0]
  2782. && (hints.ai_flags & AI_CANONNAME) && canon == 0)
  2783. {
  2784. std::size_t canon_len = strlen(hptr->h_name) + 1;
  2785. canon = gai_alloc<char>(canon_len);
  2786. if (canon == 0)
  2787. {
  2788. freeaddrinfo_emulation(aihead);
  2789. socket_ops::freehostent(hptr);
  2790. return EAI_MEMORY;
  2791. }
  2792. gai_strcpy(canon, hptr->h_name, canon_len);
  2793. }
  2794. // Create an addrinfo structure for each returned address.
  2795. for (char** ap = hptr->h_addr_list; *ap; ++ap)
  2796. {
  2797. rc = gai_aistruct(&ainext, &hints, *ap, hptr->h_addrtype);
  2798. if (rc != 0)
  2799. {
  2800. freeaddrinfo_emulation(aihead);
  2801. gai_free(canon);
  2802. socket_ops::freehostent(hptr);
  2803. return EAI_FAMILY;
  2804. }
  2805. }
  2806. socket_ops::freehostent(hptr);
  2807. }
  2808. // Check if we found anything.
  2809. if (aihead == 0)
  2810. {
  2811. gai_free(canon);
  2812. return EAI_NONAME;
  2813. }
  2814. // Return canonical name in first entry.
  2815. if (host != 0 && host[0] != '\0' && (hints.ai_flags & AI_CANONNAME))
  2816. {
  2817. if (canon)
  2818. {
  2819. aihead->ai_canonname = canon;
  2820. canon = 0;
  2821. }
  2822. else
  2823. {
  2824. std::size_t canonname_len = strlen(search[0].host) + 1;
  2825. aihead->ai_canonname = gai_alloc<char>(canonname_len);
  2826. if (aihead->ai_canonname == 0)
  2827. {
  2828. freeaddrinfo_emulation(aihead);
  2829. return EAI_MEMORY;
  2830. }
  2831. gai_strcpy(aihead->ai_canonname, search[0].host, canonname_len);
  2832. }
  2833. }
  2834. gai_free(canon);
  2835. // Process the service name.
  2836. if (service != 0 && service[0] != '\0')
  2837. {
  2838. rc = gai_serv(aihead, &hints, service);
  2839. if (rc != 0)
  2840. {
  2841. freeaddrinfo_emulation(aihead);
  2842. return rc;
  2843. }
  2844. }
  2845. // Return result to caller.
  2846. *result = aihead;
  2847. return 0;
  2848. }
  2849. inline boost::system::error_code getnameinfo_emulation(
  2850. const socket_addr_type* sa, std::size_t salen, char* host,
  2851. std::size_t hostlen, char* serv, std::size_t servlen, int flags,
  2852. boost::system::error_code& ec)
  2853. {
  2854. using namespace std;
  2855. const char* addr;
  2856. size_t addr_len;
  2857. unsigned short port;
  2858. switch (sa->sa_family)
  2859. {
  2860. case BOOST_ASIO_OS_DEF(AF_INET):
  2861. if (salen != sizeof(sockaddr_in4_type))
  2862. {
  2863. return ec = boost::asio::error::invalid_argument;
  2864. }
  2865. addr = reinterpret_cast<const char*>(
  2866. &reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_addr);
  2867. addr_len = sizeof(in4_addr_type);
  2868. port = reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_port;
  2869. break;
  2870. case BOOST_ASIO_OS_DEF(AF_INET6):
  2871. if (salen != sizeof(sockaddr_in6_type))
  2872. {
  2873. return ec = boost::asio::error::invalid_argument;
  2874. }
  2875. addr = reinterpret_cast<const char*>(
  2876. &reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_addr);
  2877. addr_len = sizeof(in6_addr_type);
  2878. port = reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_port;
  2879. break;
  2880. default:
  2881. return ec = boost::asio::error::address_family_not_supported;
  2882. }
  2883. if (host && hostlen > 0)
  2884. {
  2885. if (flags & NI_NUMERICHOST)
  2886. {
  2887. if (socket_ops::inet_ntop(sa->sa_family, addr, host, hostlen, 0, ec) == 0)
  2888. {
  2889. return ec;
  2890. }
  2891. }
  2892. else
  2893. {
  2894. hostent hent;
  2895. char hbuf[8192] = "";
  2896. hostent* hptr = socket_ops::gethostbyaddr(addr,
  2897. static_cast<int>(addr_len), sa->sa_family,
  2898. &hent, hbuf, sizeof(hbuf), ec);
  2899. if (hptr && hptr->h_name && hptr->h_name[0] != '\0')
  2900. {
  2901. if (flags & NI_NOFQDN)
  2902. {
  2903. char* dot = strchr(hptr->h_name, '.');
  2904. if (dot)
  2905. {
  2906. *dot = 0;
  2907. }
  2908. }
  2909. gai_strcpy(host, hptr->h_name, hostlen);
  2910. socket_ops::freehostent(hptr);
  2911. }
  2912. else
  2913. {
  2914. socket_ops::freehostent(hptr);
  2915. if (flags & NI_NAMEREQD)
  2916. {
  2917. return ec = boost::asio::error::host_not_found;
  2918. }
  2919. if (socket_ops::inet_ntop(sa->sa_family,
  2920. addr, host, hostlen, 0, ec) == 0)
  2921. {
  2922. return ec;
  2923. }
  2924. }
  2925. }
  2926. }
  2927. if (serv && servlen > 0)
  2928. {
  2929. if (flags & NI_NUMERICSERV)
  2930. {
  2931. if (servlen < 6)
  2932. {
  2933. return ec = boost::asio::error::no_buffer_space;
  2934. }
  2935. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2936. sprintf_s(serv, servlen, "%u", ntohs(port));
  2937. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2938. sprintf(serv, "%u", ntohs(port));
  2939. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2940. }
  2941. else
  2942. {
  2943. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2944. static ::pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  2945. ::pthread_mutex_lock(&mutex);
  2946. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2947. servent* sptr = ::getservbyport(port, (flags & NI_DGRAM) ? "udp" : 0);
  2948. if (sptr && sptr->s_name && sptr->s_name[0] != '\0')
  2949. {
  2950. gai_strcpy(serv, sptr->s_name, servlen);
  2951. }
  2952. else
  2953. {
  2954. if (servlen < 6)
  2955. {
  2956. return ec = boost::asio::error::no_buffer_space;
  2957. }
  2958. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2959. sprintf_s(serv, servlen, "%u", ntohs(port));
  2960. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2961. sprintf(serv, "%u", ntohs(port));
  2962. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2963. }
  2964. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2965. ::pthread_mutex_unlock(&mutex);
  2966. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2967. }
  2968. }
  2969. ec = boost::system::error_code();
  2970. return ec;
  2971. }
  2972. #endif // !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2973. inline boost::system::error_code translate_addrinfo_error(int error)
  2974. {
  2975. switch (error)
  2976. {
  2977. case 0:
  2978. return boost::system::error_code();
  2979. case EAI_AGAIN:
  2980. return boost::asio::error::host_not_found_try_again;
  2981. case EAI_BADFLAGS:
  2982. return boost::asio::error::invalid_argument;
  2983. case EAI_FAIL:
  2984. return boost::asio::error::no_recovery;
  2985. case EAI_FAMILY:
  2986. return boost::asio::error::address_family_not_supported;
  2987. case EAI_MEMORY:
  2988. return boost::asio::error::no_memory;
  2989. case EAI_NONAME:
  2990. #if defined(EAI_ADDRFAMILY)
  2991. case EAI_ADDRFAMILY:
  2992. #endif
  2993. #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
  2994. case EAI_NODATA:
  2995. #endif
  2996. return boost::asio::error::host_not_found;
  2997. case EAI_SERVICE:
  2998. return boost::asio::error::service_not_found;
  2999. case EAI_SOCKTYPE:
  3000. return boost::asio::error::socket_type_not_supported;
  3001. default: // Possibly the non-portable EAI_SYSTEM.
  3002. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3003. return boost::system::error_code(
  3004. WSAGetLastError(), boost::asio::error::get_system_category());
  3005. #else
  3006. return boost::system::error_code(
  3007. errno, boost::asio::error::get_system_category());
  3008. #endif
  3009. }
  3010. }
  3011. boost::system::error_code getaddrinfo(const char* host,
  3012. const char* service, const addrinfo_type& hints,
  3013. addrinfo_type** result, boost::system::error_code& ec)
  3014. {
  3015. host = (host && *host) ? host : 0;
  3016. service = (service && *service) ? service : 0;
  3017. clear_last_error();
  3018. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3019. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3020. // Building for Windows XP, Windows Server 2003, or later.
  3021. int error = ::getaddrinfo(host, service, &hints, result);
  3022. return ec = translate_addrinfo_error(error);
  3023. # else
  3024. // Building for Windows 2000 or earlier.
  3025. typedef int (WSAAPI *gai_t)(const char*,
  3026. const char*, const addrinfo_type*, addrinfo_type**);
  3027. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3028. {
  3029. if (gai_t gai = (gai_t)::GetProcAddress(winsock_module, "getaddrinfo"))
  3030. {
  3031. int error = gai(host, service, &hints, result);
  3032. return ec = translate_addrinfo_error(error);
  3033. }
  3034. }
  3035. int error = getaddrinfo_emulation(host, service, &hints, result);
  3036. return ec = translate_addrinfo_error(error);
  3037. # endif
  3038. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3039. int error = getaddrinfo_emulation(host, service, &hints, result);
  3040. return ec = translate_addrinfo_error(error);
  3041. #else
  3042. int error = ::getaddrinfo(host, service, &hints, result);
  3043. #if defined(__MACH__) && defined(__APPLE__)
  3044. using namespace std; // For isdigit and atoi.
  3045. if (error == 0 && service && isdigit(static_cast<unsigned char>(service[0])))
  3046. {
  3047. u_short_type port = host_to_network_short(atoi(service));
  3048. for (addrinfo_type* ai = *result; ai; ai = ai->ai_next)
  3049. {
  3050. switch (ai->ai_family)
  3051. {
  3052. case BOOST_ASIO_OS_DEF(AF_INET):
  3053. {
  3054. sockaddr_in4_type* sinptr =
  3055. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  3056. if (sinptr->sin_port == 0)
  3057. sinptr->sin_port = port;
  3058. break;
  3059. }
  3060. case BOOST_ASIO_OS_DEF(AF_INET6):
  3061. {
  3062. sockaddr_in6_type* sin6ptr =
  3063. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  3064. if (sin6ptr->sin6_port == 0)
  3065. sin6ptr->sin6_port = port;
  3066. break;
  3067. }
  3068. default:
  3069. break;
  3070. }
  3071. }
  3072. }
  3073. #endif
  3074. return ec = translate_addrinfo_error(error);
  3075. #endif
  3076. }
  3077. boost::system::error_code background_getaddrinfo(
  3078. const weak_cancel_token_type& cancel_token, const char* host,
  3079. const char* service, const addrinfo_type& hints,
  3080. addrinfo_type** result, boost::system::error_code& ec)
  3081. {
  3082. if (cancel_token.expired())
  3083. ec = boost::asio::error::operation_aborted;
  3084. else
  3085. socket_ops::getaddrinfo(host, service, hints, result, ec);
  3086. return ec;
  3087. }
  3088. void freeaddrinfo(addrinfo_type* ai)
  3089. {
  3090. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3091. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3092. // Building for Windows XP, Windows Server 2003, or later.
  3093. ::freeaddrinfo(ai);
  3094. # else
  3095. // Building for Windows 2000 or earlier.
  3096. typedef int (WSAAPI *fai_t)(addrinfo_type*);
  3097. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3098. {
  3099. if (fai_t fai = (fai_t)::GetProcAddress(winsock_module, "freeaddrinfo"))
  3100. {
  3101. fai(ai);
  3102. return;
  3103. }
  3104. }
  3105. freeaddrinfo_emulation(ai);
  3106. # endif
  3107. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3108. freeaddrinfo_emulation(ai);
  3109. #else
  3110. ::freeaddrinfo(ai);
  3111. #endif
  3112. }
  3113. boost::system::error_code getnameinfo(const socket_addr_type* addr,
  3114. std::size_t addrlen, char* host, std::size_t hostlen,
  3115. char* serv, std::size_t servlen, int flags, boost::system::error_code& ec)
  3116. {
  3117. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3118. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3119. // Building for Windows XP, Windows Server 2003, or later.
  3120. clear_last_error();
  3121. int error = ::getnameinfo(addr, static_cast<socklen_t>(addrlen),
  3122. host, static_cast<DWORD>(hostlen),
  3123. serv, static_cast<DWORD>(servlen), flags);
  3124. return ec = translate_addrinfo_error(error);
  3125. # else
  3126. // Building for Windows 2000 or earlier.
  3127. typedef int (WSAAPI *gni_t)(const socket_addr_type*,
  3128. int, char*, DWORD, char*, DWORD, int);
  3129. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3130. {
  3131. if (gni_t gni = (gni_t)::GetProcAddress(winsock_module, "getnameinfo"))
  3132. {
  3133. clear_last_error();
  3134. int error = gni(addr, static_cast<int>(addrlen),
  3135. host, static_cast<DWORD>(hostlen),
  3136. serv, static_cast<DWORD>(servlen), flags);
  3137. return ec = translate_addrinfo_error(error);
  3138. }
  3139. }
  3140. clear_last_error();
  3141. return getnameinfo_emulation(addr, addrlen,
  3142. host, hostlen, serv, servlen, flags, ec);
  3143. # endif
  3144. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3145. using namespace std; // For memcpy.
  3146. sockaddr_storage_type tmp_addr;
  3147. memcpy(&tmp_addr, addr, addrlen);
  3148. addr = reinterpret_cast<socket_addr_type*>(&tmp_addr);
  3149. clear_last_error();
  3150. return getnameinfo_emulation(addr, addrlen,
  3151. host, hostlen, serv, servlen, flags, ec);
  3152. #else
  3153. clear_last_error();
  3154. int error = ::getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
  3155. return ec = translate_addrinfo_error(error);
  3156. #endif
  3157. }
  3158. boost::system::error_code sync_getnameinfo(
  3159. const socket_addr_type* addr, std::size_t addrlen,
  3160. char* host, std::size_t hostlen, char* serv,
  3161. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3162. {
  3163. // First try resolving with the service name. If that fails try resolving
  3164. // but allow the service to be returned as a number.
  3165. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3166. socket_ops::getnameinfo(addr, addrlen, host,
  3167. hostlen, serv, servlen, flags, ec);
  3168. if (ec)
  3169. {
  3170. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3171. serv, servlen, flags | NI_NUMERICSERV, ec);
  3172. }
  3173. return ec;
  3174. }
  3175. boost::system::error_code background_getnameinfo(
  3176. const weak_cancel_token_type& cancel_token,
  3177. const socket_addr_type* addr, std::size_t addrlen,
  3178. char* host, std::size_t hostlen, char* serv,
  3179. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3180. {
  3181. if (cancel_token.expired())
  3182. {
  3183. ec = boost::asio::error::operation_aborted;
  3184. }
  3185. else
  3186. {
  3187. // First try resolving with the service name. If that fails try resolving
  3188. // but allow the service to be returned as a number.
  3189. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3190. socket_ops::getnameinfo(addr, addrlen, host,
  3191. hostlen, serv, servlen, flags, ec);
  3192. if (ec)
  3193. {
  3194. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3195. serv, servlen, flags | NI_NUMERICSERV, ec);
  3196. }
  3197. }
  3198. return ec;
  3199. }
  3200. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3201. u_long_type network_to_host_long(u_long_type value)
  3202. {
  3203. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3204. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3205. u_long_type result = (static_cast<u_long_type>(value_p[0]) << 24)
  3206. | (static_cast<u_long_type>(value_p[1]) << 16)
  3207. | (static_cast<u_long_type>(value_p[2]) << 8)
  3208. | static_cast<u_long_type>(value_p[3]);
  3209. return result;
  3210. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3211. return ntohl(value);
  3212. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3213. }
  3214. u_long_type host_to_network_long(u_long_type value)
  3215. {
  3216. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3217. u_long_type result;
  3218. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3219. result_p[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
  3220. result_p[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
  3221. result_p[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3222. result_p[3] = static_cast<unsigned char>(value & 0xFF);
  3223. return result;
  3224. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3225. return htonl(value);
  3226. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3227. }
  3228. u_short_type network_to_host_short(u_short_type value)
  3229. {
  3230. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3231. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3232. u_short_type result = (static_cast<u_short_type>(value_p[0]) << 8)
  3233. | static_cast<u_short_type>(value_p[1]);
  3234. return result;
  3235. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3236. return ntohs(value);
  3237. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3238. }
  3239. u_short_type host_to_network_short(u_short_type value)
  3240. {
  3241. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3242. u_short_type result;
  3243. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3244. result_p[0] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3245. result_p[1] = static_cast<unsigned char>(value & 0xFF);
  3246. return result;
  3247. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3248. return htons(value);
  3249. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3250. }
  3251. } // namespace socket_ops
  3252. } // namespace detail
  3253. } // namespace asio
  3254. } // namespace boost
  3255. #include <boost/asio/detail/pop_options.hpp>
  3256. #endif // BOOST_ASIO_DETAIL_SOCKET_OPS_IPP