socket_ops.ipp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577
  1. //
  2. // detail/impl/socket_ops.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2020 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__) || defined(__EMSCRIPTEN__)
  293. int flags = ::fcntl(s, F_GETFL, 0);
  294. if (flags >= 0)
  295. ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
  296. # else // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  297. ioctl_arg_type arg = 0;
  298. ::ioctl(s, FIONBIO, &arg);
  299. # endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
  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__) || defined(__EMSCRIPTEN__)
  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__) || defined(__EMSCRIPTEN__)
  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. result = 0;
  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. result = 0;
  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__) \
  1328. || defined(__OpenBSD__) || defined(__QNX__)
  1329. // To implement portable behaviour for SO_REUSEADDR with UDP sockets we
  1330. // need to also set SO_REUSEPORT on BSD-based platforms.
  1331. if ((state & datagram_oriented)
  1332. && level == SOL_SOCKET && optname == SO_REUSEADDR)
  1333. {
  1334. call_setsockopt(&msghdr::msg_namelen, s,
  1335. SOL_SOCKET, SO_REUSEPORT, optval, optlen);
  1336. }
  1337. #endif
  1338. }
  1339. return result;
  1340. #endif // defined(__BORLANDC__)
  1341. }
  1342. template <typename SockLenType>
  1343. inline int call_getsockopt(SockLenType msghdr::*,
  1344. socket_type s, int level, int optname,
  1345. void* optval, std::size_t* optlen)
  1346. {
  1347. SockLenType tmp_optlen = (SockLenType)*optlen;
  1348. int result = ::getsockopt(s, level, optname, (char*)optval, &tmp_optlen);
  1349. *optlen = (std::size_t)tmp_optlen;
  1350. return result;
  1351. }
  1352. int getsockopt(socket_type s, state_type state, int level, int optname,
  1353. void* optval, size_t* optlen, boost::system::error_code& ec)
  1354. {
  1355. if (s == invalid_socket)
  1356. {
  1357. ec = boost::asio::error::bad_descriptor;
  1358. return socket_error_retval;
  1359. }
  1360. if (level == custom_socket_option_level && optname == always_fail_option)
  1361. {
  1362. ec = boost::asio::error::invalid_argument;
  1363. return socket_error_retval;
  1364. }
  1365. if (level == custom_socket_option_level
  1366. && optname == enable_connection_aborted_option)
  1367. {
  1368. if (*optlen != sizeof(int))
  1369. {
  1370. ec = boost::asio::error::invalid_argument;
  1371. return socket_error_retval;
  1372. }
  1373. *static_cast<int*>(optval) = (state & enable_connection_aborted) ? 1 : 0;
  1374. ec = boost::system::error_code();
  1375. return 0;
  1376. }
  1377. #if defined(__BORLANDC__)
  1378. // Mysteriously, using the getsockopt and setsockopt functions directly with
  1379. // Borland C++ results in incorrect values being set and read. The bug can be
  1380. // worked around by using function addresses resolved with GetProcAddress.
  1381. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  1382. {
  1383. typedef int (WSAAPI *gso_t)(SOCKET, int, int, char*, int*);
  1384. if (gso_t gso = (gso_t)::GetProcAddress(winsock_module, "getsockopt"))
  1385. {
  1386. clear_last_error();
  1387. int tmp_optlen = static_cast<int>(*optlen);
  1388. int result = error_wrapper(gso(s, level, optname,
  1389. reinterpret_cast<char*>(optval), &tmp_optlen), ec);
  1390. *optlen = static_cast<size_t>(tmp_optlen);
  1391. if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
  1392. && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
  1393. {
  1394. // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are
  1395. // only supported on Windows Vista and later. To simplify program logic
  1396. // we will fake success of getting this option and specify that the
  1397. // value is non-zero (i.e. true). This corresponds to the behavior of
  1398. // IPv6 sockets on Windows platforms pre-Vista.
  1399. *static_cast<DWORD*>(optval) = 1;
  1400. ec = boost::system::error_code();
  1401. }
  1402. return result;
  1403. }
  1404. }
  1405. ec = boost::asio::error::fault;
  1406. return socket_error_retval;
  1407. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1408. clear_last_error();
  1409. int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
  1410. s, level, optname, optval, optlen), ec);
  1411. if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
  1412. && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
  1413. {
  1414. // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are only
  1415. // supported on Windows Vista and later. To simplify program logic we will
  1416. // fake success of getting this option and specify that the value is
  1417. // non-zero (i.e. true). This corresponds to the behavior of IPv6 sockets
  1418. // on Windows platforms pre-Vista.
  1419. *static_cast<DWORD*>(optval) = 1;
  1420. ec = boost::system::error_code();
  1421. }
  1422. if (result == 0)
  1423. ec = boost::system::error_code();
  1424. return result;
  1425. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1426. clear_last_error();
  1427. int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
  1428. s, level, optname, optval, optlen), ec);
  1429. #if defined(__linux__)
  1430. if (result == 0 && level == SOL_SOCKET && *optlen == sizeof(int)
  1431. && (optname == SO_SNDBUF || optname == SO_RCVBUF))
  1432. {
  1433. // On Linux, setting SO_SNDBUF or SO_RCVBUF to N actually causes the kernel
  1434. // to set the buffer size to N*2. Linux puts additional stuff into the
  1435. // buffers so that only about half is actually available to the application.
  1436. // The retrieved value is divided by 2 here to make it appear as though the
  1437. // correct value has been set.
  1438. *static_cast<int*>(optval) /= 2;
  1439. }
  1440. #endif // defined(__linux__)
  1441. if (result == 0)
  1442. ec = boost::system::error_code();
  1443. return result;
  1444. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1445. }
  1446. template <typename SockLenType>
  1447. inline int call_getpeername(SockLenType msghdr::*,
  1448. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  1449. {
  1450. SockLenType tmp_addrlen = (SockLenType)*addrlen;
  1451. int result = ::getpeername(s, addr, &tmp_addrlen);
  1452. *addrlen = (std::size_t)tmp_addrlen;
  1453. return result;
  1454. }
  1455. int getpeername(socket_type s, socket_addr_type* addr,
  1456. std::size_t* addrlen, bool cached, boost::system::error_code& ec)
  1457. {
  1458. if (s == invalid_socket)
  1459. {
  1460. ec = boost::asio::error::bad_descriptor;
  1461. return socket_error_retval;
  1462. }
  1463. #if defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP) \
  1464. || defined(__CYGWIN__)
  1465. if (cached)
  1466. {
  1467. // Check if socket is still connected.
  1468. DWORD connect_time = 0;
  1469. size_t connect_time_len = sizeof(connect_time);
  1470. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_CONNECT_TIME,
  1471. &connect_time, &connect_time_len, ec) == socket_error_retval)
  1472. {
  1473. return socket_error_retval;
  1474. }
  1475. if (connect_time == 0xFFFFFFFF)
  1476. {
  1477. ec = boost::asio::error::not_connected;
  1478. return socket_error_retval;
  1479. }
  1480. // The cached value is still valid.
  1481. ec = boost::system::error_code();
  1482. return 0;
  1483. }
  1484. #else // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
  1485. // || defined(__CYGWIN__)
  1486. (void)cached;
  1487. #endif // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
  1488. // || defined(__CYGWIN__)
  1489. clear_last_error();
  1490. int result = error_wrapper(call_getpeername(
  1491. &msghdr::msg_namelen, s, addr, addrlen), ec);
  1492. if (result == 0)
  1493. ec = boost::system::error_code();
  1494. return result;
  1495. }
  1496. template <typename SockLenType>
  1497. inline int call_getsockname(SockLenType msghdr::*,
  1498. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  1499. {
  1500. SockLenType tmp_addrlen = (SockLenType)*addrlen;
  1501. int result = ::getsockname(s, addr, &tmp_addrlen);
  1502. *addrlen = (std::size_t)tmp_addrlen;
  1503. return result;
  1504. }
  1505. int getsockname(socket_type s, socket_addr_type* addr,
  1506. std::size_t* addrlen, boost::system::error_code& ec)
  1507. {
  1508. if (s == invalid_socket)
  1509. {
  1510. ec = boost::asio::error::bad_descriptor;
  1511. return socket_error_retval;
  1512. }
  1513. clear_last_error();
  1514. int result = error_wrapper(call_getsockname(
  1515. &msghdr::msg_namelen, s, addr, addrlen), ec);
  1516. if (result == 0)
  1517. ec = boost::system::error_code();
  1518. return result;
  1519. }
  1520. int ioctl(socket_type s, state_type& state, int cmd,
  1521. ioctl_arg_type* arg, boost::system::error_code& ec)
  1522. {
  1523. if (s == invalid_socket)
  1524. {
  1525. ec = boost::asio::error::bad_descriptor;
  1526. return socket_error_retval;
  1527. }
  1528. clear_last_error();
  1529. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1530. int result = error_wrapper(::ioctlsocket(s, cmd, arg), ec);
  1531. #elif defined(__MACH__) && defined(__APPLE__) \
  1532. || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  1533. int result = error_wrapper(::ioctl(s,
  1534. static_cast<unsigned int>(cmd), arg), ec);
  1535. #else
  1536. int result = error_wrapper(::ioctl(s, cmd, arg), ec);
  1537. #endif
  1538. if (result >= 0)
  1539. {
  1540. ec = boost::system::error_code();
  1541. // When updating the non-blocking mode we always perform the ioctl syscall,
  1542. // even if the flags would otherwise indicate that the socket is already in
  1543. // the correct state. This ensures that the underlying socket is put into
  1544. // the state that has been requested by the user. If the ioctl syscall was
  1545. // successful then we need to update the flags to match.
  1546. if (cmd == static_cast<int>(FIONBIO))
  1547. {
  1548. if (*arg)
  1549. {
  1550. state |= user_set_non_blocking;
  1551. }
  1552. else
  1553. {
  1554. // Clearing the non-blocking mode always overrides any internally-set
  1555. // non-blocking flag. Any subsequent asynchronous operations will need
  1556. // to re-enable non-blocking I/O.
  1557. state &= ~(user_set_non_blocking | internal_non_blocking);
  1558. }
  1559. }
  1560. }
  1561. return result;
  1562. }
  1563. int select(int nfds, fd_set* readfds, fd_set* writefds,
  1564. fd_set* exceptfds, timeval* timeout, boost::system::error_code& ec)
  1565. {
  1566. clear_last_error();
  1567. #if defined(__EMSCRIPTEN__)
  1568. exceptfds = 0;
  1569. #endif // defined(__EMSCRIPTEN__)
  1570. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1571. if (!readfds && !writefds && !exceptfds && timeout)
  1572. {
  1573. DWORD milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  1574. if (milliseconds == 0)
  1575. milliseconds = 1; // Force context switch.
  1576. ::Sleep(milliseconds);
  1577. ec = boost::system::error_code();
  1578. return 0;
  1579. }
  1580. // The select() call allows timeout values measured in microseconds, but the
  1581. // system clock (as wrapped by boost::posix_time::microsec_clock) typically
  1582. // has a resolution of 10 milliseconds. This can lead to a spinning select
  1583. // reactor, meaning increased CPU usage, when waiting for the earliest
  1584. // scheduled timeout if it's less than 10 milliseconds away. To avoid a tight
  1585. // spin we'll use a minimum timeout of 1 millisecond.
  1586. if (timeout && timeout->tv_sec == 0
  1587. && timeout->tv_usec > 0 && timeout->tv_usec < 1000)
  1588. timeout->tv_usec = 1000;
  1589. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1590. #if defined(__hpux) && defined(__SELECT)
  1591. timespec ts;
  1592. ts.tv_sec = timeout ? timeout->tv_sec : 0;
  1593. ts.tv_nsec = timeout ? timeout->tv_usec * 1000 : 0;
  1594. return error_wrapper(::pselect(nfds, readfds,
  1595. writefds, exceptfds, timeout ? &ts : 0, 0), ec);
  1596. #else
  1597. int result = error_wrapper(::select(nfds, readfds,
  1598. writefds, exceptfds, timeout), ec);
  1599. if (result >= 0)
  1600. ec = boost::system::error_code();
  1601. return result;
  1602. #endif
  1603. }
  1604. int poll_read(socket_type s, state_type state,
  1605. int msec, boost::system::error_code& ec)
  1606. {
  1607. if (s == invalid_socket)
  1608. {
  1609. ec = boost::asio::error::bad_descriptor;
  1610. return socket_error_retval;
  1611. }
  1612. #if defined(BOOST_ASIO_WINDOWS) \
  1613. || defined(__CYGWIN__) \
  1614. || defined(__SYMBIAN32__)
  1615. fd_set fds;
  1616. FD_ZERO(&fds);
  1617. FD_SET(s, &fds);
  1618. timeval timeout_obj;
  1619. timeval* timeout;
  1620. if (state & user_set_non_blocking)
  1621. {
  1622. timeout_obj.tv_sec = 0;
  1623. timeout_obj.tv_usec = 0;
  1624. timeout = &timeout_obj;
  1625. }
  1626. else if (msec >= 0)
  1627. {
  1628. timeout_obj.tv_sec = msec / 1000;
  1629. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1630. timeout = &timeout_obj;
  1631. }
  1632. else
  1633. timeout = 0;
  1634. clear_last_error();
  1635. int result = error_wrapper(::select(s + 1, &fds, 0, 0, timeout), ec);
  1636. #else // defined(BOOST_ASIO_WINDOWS)
  1637. // || defined(__CYGWIN__)
  1638. // || defined(__SYMBIAN32__)
  1639. pollfd fds;
  1640. fds.fd = s;
  1641. fds.events = POLLIN;
  1642. fds.revents = 0;
  1643. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1644. clear_last_error();
  1645. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1646. #endif // defined(BOOST_ASIO_WINDOWS)
  1647. // || defined(__CYGWIN__)
  1648. // || defined(__SYMBIAN32__)
  1649. if (result == 0)
  1650. ec = (state & user_set_non_blocking)
  1651. ? boost::asio::error::would_block : boost::system::error_code();
  1652. else if (result > 0)
  1653. ec = boost::system::error_code();
  1654. return result;
  1655. }
  1656. int poll_write(socket_type s, state_type state,
  1657. int msec, boost::system::error_code& ec)
  1658. {
  1659. if (s == invalid_socket)
  1660. {
  1661. ec = boost::asio::error::bad_descriptor;
  1662. return socket_error_retval;
  1663. }
  1664. #if defined(BOOST_ASIO_WINDOWS) \
  1665. || defined(__CYGWIN__) \
  1666. || defined(__SYMBIAN32__)
  1667. fd_set fds;
  1668. FD_ZERO(&fds);
  1669. FD_SET(s, &fds);
  1670. timeval timeout_obj;
  1671. timeval* timeout;
  1672. if (state & user_set_non_blocking)
  1673. {
  1674. timeout_obj.tv_sec = 0;
  1675. timeout_obj.tv_usec = 0;
  1676. timeout = &timeout_obj;
  1677. }
  1678. else if (msec >= 0)
  1679. {
  1680. timeout_obj.tv_sec = msec / 1000;
  1681. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1682. timeout = &timeout_obj;
  1683. }
  1684. else
  1685. timeout = 0;
  1686. clear_last_error();
  1687. int result = error_wrapper(::select(s + 1, 0, &fds, 0, timeout), ec);
  1688. #else // defined(BOOST_ASIO_WINDOWS)
  1689. // || defined(__CYGWIN__)
  1690. // || defined(__SYMBIAN32__)
  1691. pollfd fds;
  1692. fds.fd = s;
  1693. fds.events = POLLOUT;
  1694. fds.revents = 0;
  1695. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1696. clear_last_error();
  1697. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1698. #endif // defined(BOOST_ASIO_WINDOWS)
  1699. // || defined(__CYGWIN__)
  1700. // || defined(__SYMBIAN32__)
  1701. if (result == 0)
  1702. ec = (state & user_set_non_blocking)
  1703. ? boost::asio::error::would_block : boost::system::error_code();
  1704. else if (result > 0)
  1705. ec = boost::system::error_code();
  1706. return result;
  1707. }
  1708. int poll_error(socket_type s, state_type state,
  1709. int msec, boost::system::error_code& ec)
  1710. {
  1711. if (s == invalid_socket)
  1712. {
  1713. ec = boost::asio::error::bad_descriptor;
  1714. return socket_error_retval;
  1715. }
  1716. #if defined(BOOST_ASIO_WINDOWS) \
  1717. || defined(__CYGWIN__) \
  1718. || defined(__SYMBIAN32__)
  1719. fd_set fds;
  1720. FD_ZERO(&fds);
  1721. FD_SET(s, &fds);
  1722. timeval timeout_obj;
  1723. timeval* timeout;
  1724. if (state & user_set_non_blocking)
  1725. {
  1726. timeout_obj.tv_sec = 0;
  1727. timeout_obj.tv_usec = 0;
  1728. timeout = &timeout_obj;
  1729. }
  1730. else if (msec >= 0)
  1731. {
  1732. timeout_obj.tv_sec = msec / 1000;
  1733. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1734. timeout = &timeout_obj;
  1735. }
  1736. else
  1737. timeout = 0;
  1738. clear_last_error();
  1739. int result = error_wrapper(::select(s + 1, 0, 0, &fds, timeout), ec);
  1740. #else // defined(BOOST_ASIO_WINDOWS)
  1741. // || defined(__CYGWIN__)
  1742. // || defined(__SYMBIAN32__)
  1743. pollfd fds;
  1744. fds.fd = s;
  1745. fds.events = POLLPRI | POLLERR | POLLHUP;
  1746. fds.revents = 0;
  1747. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1748. clear_last_error();
  1749. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1750. #endif // defined(BOOST_ASIO_WINDOWS)
  1751. // || defined(__CYGWIN__)
  1752. // || defined(__SYMBIAN32__)
  1753. if (result == 0)
  1754. ec = (state & user_set_non_blocking)
  1755. ? boost::asio::error::would_block : boost::system::error_code();
  1756. else if (result > 0)
  1757. ec = boost::system::error_code();
  1758. return result;
  1759. }
  1760. int poll_connect(socket_type s, int msec, boost::system::error_code& ec)
  1761. {
  1762. if (s == invalid_socket)
  1763. {
  1764. ec = boost::asio::error::bad_descriptor;
  1765. return socket_error_retval;
  1766. }
  1767. #if defined(BOOST_ASIO_WINDOWS) \
  1768. || defined(__CYGWIN__) \
  1769. || defined(__SYMBIAN32__)
  1770. fd_set write_fds;
  1771. FD_ZERO(&write_fds);
  1772. FD_SET(s, &write_fds);
  1773. fd_set except_fds;
  1774. FD_ZERO(&except_fds);
  1775. FD_SET(s, &except_fds);
  1776. timeval timeout_obj;
  1777. timeval* timeout;
  1778. if (msec >= 0)
  1779. {
  1780. timeout_obj.tv_sec = msec / 1000;
  1781. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1782. timeout = &timeout_obj;
  1783. }
  1784. else
  1785. timeout = 0;
  1786. clear_last_error();
  1787. int result = error_wrapper(::select(
  1788. s + 1, 0, &write_fds, &except_fds, timeout), ec);
  1789. if (result >= 0)
  1790. ec = boost::system::error_code();
  1791. return result;
  1792. #else // defined(BOOST_ASIO_WINDOWS)
  1793. // || defined(__CYGWIN__)
  1794. // || defined(__SYMBIAN32__)
  1795. pollfd fds;
  1796. fds.fd = s;
  1797. fds.events = POLLOUT;
  1798. fds.revents = 0;
  1799. clear_last_error();
  1800. int result = error_wrapper(::poll(&fds, 1, msec), ec);
  1801. if (result >= 0)
  1802. ec = boost::system::error_code();
  1803. return result;
  1804. #endif // defined(BOOST_ASIO_WINDOWS)
  1805. // || defined(__CYGWIN__)
  1806. // || defined(__SYMBIAN32__)
  1807. }
  1808. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1809. const char* inet_ntop(int af, const void* src, char* dest, size_t length,
  1810. unsigned long scope_id, boost::system::error_code& ec)
  1811. {
  1812. clear_last_error();
  1813. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1814. using namespace std; // For sprintf.
  1815. const unsigned char* bytes = static_cast<const unsigned char*>(src);
  1816. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1817. {
  1818. sprintf_s(dest, length, "%u.%u.%u.%u",
  1819. bytes[0], bytes[1], bytes[2], bytes[3]);
  1820. return dest;
  1821. }
  1822. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1823. {
  1824. size_t n = 0, b = 0, z = 0;
  1825. while (n < length && b < 16)
  1826. {
  1827. if (bytes[b] == 0 && bytes[b + 1] == 0 && z == 0)
  1828. {
  1829. do b += 2; while (b < 16 && bytes[b] == 0 && bytes[b + 1] == 0);
  1830. n += sprintf_s(dest + n, length - n, ":%s", b < 16 ? "" : ":"), ++z;
  1831. }
  1832. else
  1833. {
  1834. n += sprintf_s(dest + n, length - n, "%s%x", b ? ":" : "",
  1835. (static_cast<u_long_type>(bytes[b]) << 8) | bytes[b + 1]);
  1836. b += 2;
  1837. }
  1838. }
  1839. if (scope_id)
  1840. n += sprintf_s(dest + n, length - n, "%%%lu", scope_id);
  1841. return dest;
  1842. }
  1843. else
  1844. {
  1845. ec = boost::asio::error::address_family_not_supported;
  1846. return 0;
  1847. }
  1848. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1849. using namespace std; // For memcpy.
  1850. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  1851. {
  1852. ec = boost::asio::error::address_family_not_supported;
  1853. return 0;
  1854. }
  1855. union
  1856. {
  1857. socket_addr_type base;
  1858. sockaddr_storage_type storage;
  1859. sockaddr_in4_type v4;
  1860. sockaddr_in6_type v6;
  1861. } address;
  1862. DWORD address_length;
  1863. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1864. {
  1865. address_length = sizeof(sockaddr_in4_type);
  1866. address.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  1867. address.v4.sin_port = 0;
  1868. memcpy(&address.v4.sin_addr, src, sizeof(in4_addr_type));
  1869. }
  1870. else // AF_INET6
  1871. {
  1872. address_length = sizeof(sockaddr_in6_type);
  1873. address.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  1874. address.v6.sin6_port = 0;
  1875. address.v6.sin6_flowinfo = 0;
  1876. address.v6.sin6_scope_id = scope_id;
  1877. memcpy(&address.v6.sin6_addr, src, sizeof(in6_addr_type));
  1878. }
  1879. DWORD string_length = static_cast<DWORD>(length);
  1880. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  1881. LPWSTR string_buffer = (LPWSTR)_alloca(length * sizeof(WCHAR));
  1882. int result = error_wrapper(::WSAAddressToStringW(&address.base,
  1883. address_length, 0, string_buffer, &string_length), ec);
  1884. ::WideCharToMultiByte(CP_ACP, 0, string_buffer, -1,
  1885. dest, static_cast<int>(length), 0, 0);
  1886. #else
  1887. int result = error_wrapper(::WSAAddressToStringA(
  1888. &address.base, address_length, 0, dest, &string_length), ec);
  1889. #endif
  1890. // Windows may set error code on success.
  1891. if (result != socket_error_retval)
  1892. ec = boost::system::error_code();
  1893. // Windows may not set an error code on failure.
  1894. else if (result == socket_error_retval && !ec)
  1895. ec = boost::asio::error::invalid_argument;
  1896. return result == socket_error_retval ? 0 : dest;
  1897. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1898. const char* result = error_wrapper(::inet_ntop(
  1899. af, src, dest, static_cast<int>(length)), ec);
  1900. if (result == 0 && !ec)
  1901. ec = boost::asio::error::invalid_argument;
  1902. if (result != 0 && af == BOOST_ASIO_OS_DEF(AF_INET6) && scope_id != 0)
  1903. {
  1904. using namespace std; // For strcat and sprintf.
  1905. char if_name[(IF_NAMESIZE > 21 ? IF_NAMESIZE : 21) + 1] = "%";
  1906. const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
  1907. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  1908. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  1909. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  1910. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  1911. if ((!is_link_local && !is_multicast_link_local)
  1912. || if_indextoname(static_cast<unsigned>(scope_id), if_name + 1) == 0)
  1913. sprintf(if_name + 1, "%lu", scope_id);
  1914. strcat(dest, if_name);
  1915. }
  1916. return result;
  1917. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1918. }
  1919. int inet_pton(int af, const char* src, void* dest,
  1920. unsigned long* scope_id, boost::system::error_code& ec)
  1921. {
  1922. clear_last_error();
  1923. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1924. using namespace std; // For sscanf.
  1925. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1926. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1927. {
  1928. unsigned int b0, b1, b2, b3;
  1929. if (sscanf_s(src, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) != 4)
  1930. {
  1931. ec = boost::asio::error::invalid_argument;
  1932. return -1;
  1933. }
  1934. if (b0 > 255 || b1 > 255 || b2 > 255 || b3 > 255)
  1935. {
  1936. ec = boost::asio::error::invalid_argument;
  1937. return -1;
  1938. }
  1939. bytes[0] = static_cast<unsigned char>(b0);
  1940. bytes[1] = static_cast<unsigned char>(b1);
  1941. bytes[2] = static_cast<unsigned char>(b2);
  1942. bytes[3] = static_cast<unsigned char>(b3);
  1943. ec = boost::system::error_code();
  1944. return 1;
  1945. }
  1946. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1947. {
  1948. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1949. std::memset(bytes, 0, 16);
  1950. unsigned char back_bytes[16] = { 0 };
  1951. int num_front_bytes = 0, num_back_bytes = 0;
  1952. const char* p = src;
  1953. enum { fword, fcolon, bword, scope, done } state = fword;
  1954. unsigned long current_word = 0;
  1955. while (state != done)
  1956. {
  1957. if (current_word > 0xFFFF)
  1958. {
  1959. ec = boost::asio::error::invalid_argument;
  1960. return -1;
  1961. }
  1962. switch (state)
  1963. {
  1964. case fword:
  1965. if (*p >= '0' && *p <= '9')
  1966. current_word = current_word * 16 + *p++ - '0';
  1967. else if (*p >= 'a' && *p <= 'f')
  1968. current_word = current_word * 16 + *p++ - 'a' + 10;
  1969. else if (*p >= 'A' && *p <= 'F')
  1970. current_word = current_word * 16 + *p++ - 'A' + 10;
  1971. else
  1972. {
  1973. if (num_front_bytes == 16)
  1974. {
  1975. ec = boost::asio::error::invalid_argument;
  1976. return -1;
  1977. }
  1978. bytes[num_front_bytes++] = (current_word >> 8) & 0xFF;
  1979. bytes[num_front_bytes++] = current_word & 0xFF;
  1980. current_word = 0;
  1981. if (*p == ':')
  1982. state = fcolon, ++p;
  1983. else if (*p == '%')
  1984. state = scope, ++p;
  1985. else if (*p == 0)
  1986. state = done;
  1987. else
  1988. {
  1989. ec = boost::asio::error::invalid_argument;
  1990. return -1;
  1991. }
  1992. }
  1993. break;
  1994. case fcolon:
  1995. if (*p == ':')
  1996. state = bword, ++p;
  1997. else
  1998. state = fword;
  1999. break;
  2000. case bword:
  2001. if (*p >= '0' && *p <= '9')
  2002. current_word = current_word * 16 + *p++ - '0';
  2003. else if (*p >= 'a' && *p <= 'f')
  2004. current_word = current_word * 16 + *p++ - 'a' + 10;
  2005. else if (*p >= 'A' && *p <= 'F')
  2006. current_word = current_word * 16 + *p++ - 'A' + 10;
  2007. else
  2008. {
  2009. if (num_front_bytes + num_back_bytes == 16)
  2010. {
  2011. ec = boost::asio::error::invalid_argument;
  2012. return -1;
  2013. }
  2014. back_bytes[num_back_bytes++] = (current_word >> 8) & 0xFF;
  2015. back_bytes[num_back_bytes++] = current_word & 0xFF;
  2016. current_word = 0;
  2017. if (*p == ':')
  2018. state = bword, ++p;
  2019. else if (*p == '%')
  2020. state = scope, ++p;
  2021. else if (*p == 0)
  2022. state = done;
  2023. else
  2024. {
  2025. ec = boost::asio::error::invalid_argument;
  2026. return -1;
  2027. }
  2028. }
  2029. break;
  2030. case scope:
  2031. if (*p >= '0' && *p <= '9')
  2032. current_word = current_word * 10 + *p++ - '0';
  2033. else if (*p == 0)
  2034. *scope_id = current_word, state = done;
  2035. else
  2036. {
  2037. ec = boost::asio::error::invalid_argument;
  2038. return -1;
  2039. }
  2040. break;
  2041. default:
  2042. break;
  2043. }
  2044. }
  2045. for (int i = 0; i < num_back_bytes; ++i)
  2046. bytes[16 - num_back_bytes + i] = back_bytes[i];
  2047. ec = boost::system::error_code();
  2048. return 1;
  2049. }
  2050. else
  2051. {
  2052. ec = boost::asio::error::address_family_not_supported;
  2053. return -1;
  2054. }
  2055. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2056. using namespace std; // For memcpy and strcmp.
  2057. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  2058. {
  2059. ec = boost::asio::error::address_family_not_supported;
  2060. return -1;
  2061. }
  2062. union
  2063. {
  2064. socket_addr_type base;
  2065. sockaddr_storage_type storage;
  2066. sockaddr_in4_type v4;
  2067. sockaddr_in6_type v6;
  2068. } address;
  2069. int address_length = sizeof(sockaddr_storage_type);
  2070. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  2071. int num_wide_chars = static_cast<int>(strlen(src)) + 1;
  2072. LPWSTR wide_buffer = (LPWSTR)_alloca(num_wide_chars * sizeof(WCHAR));
  2073. ::MultiByteToWideChar(CP_ACP, 0, src, -1, wide_buffer, num_wide_chars);
  2074. int result = error_wrapper(::WSAStringToAddressW(
  2075. wide_buffer, af, 0, &address.base, &address_length), ec);
  2076. #else
  2077. int result = error_wrapper(::WSAStringToAddressA(
  2078. const_cast<char*>(src), af, 0, &address.base, &address_length), ec);
  2079. #endif
  2080. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  2081. {
  2082. if (result != socket_error_retval)
  2083. {
  2084. memcpy(dest, &address.v4.sin_addr, sizeof(in4_addr_type));
  2085. ec = boost::system::error_code();
  2086. }
  2087. else if (strcmp(src, "255.255.255.255") == 0)
  2088. {
  2089. static_cast<in4_addr_type*>(dest)->s_addr = INADDR_NONE;
  2090. ec = boost::system::error_code();
  2091. }
  2092. }
  2093. else // AF_INET6
  2094. {
  2095. if (result != socket_error_retval)
  2096. {
  2097. memcpy(dest, &address.v6.sin6_addr, sizeof(in6_addr_type));
  2098. if (scope_id)
  2099. *scope_id = address.v6.sin6_scope_id;
  2100. ec = boost::system::error_code();
  2101. }
  2102. }
  2103. // Windows may not set an error code on failure.
  2104. if (result == socket_error_retval && !ec)
  2105. ec = boost::asio::error::invalid_argument;
  2106. if (result != socket_error_retval)
  2107. ec = boost::system::error_code();
  2108. return result == socket_error_retval ? -1 : 1;
  2109. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2110. using namespace std; // For strchr, memcpy and atoi.
  2111. // On some platforms, inet_pton fails if an address string contains a scope
  2112. // id. Detect and remove the scope id before passing the string to inet_pton.
  2113. const bool is_v6 = (af == BOOST_ASIO_OS_DEF(AF_INET6));
  2114. const char* if_name = is_v6 ? strchr(src, '%') : 0;
  2115. char src_buf[max_addr_v6_str_len + 1];
  2116. const char* src_ptr = src;
  2117. if (if_name != 0)
  2118. {
  2119. if (if_name - src > max_addr_v6_str_len)
  2120. {
  2121. ec = boost::asio::error::invalid_argument;
  2122. return 0;
  2123. }
  2124. memcpy(src_buf, src, if_name - src);
  2125. src_buf[if_name - src] = 0;
  2126. src_ptr = src_buf;
  2127. }
  2128. int result = error_wrapper(::inet_pton(af, src_ptr, dest), ec);
  2129. if (result <= 0 && !ec)
  2130. ec = boost::asio::error::invalid_argument;
  2131. if (result > 0 && is_v6 && scope_id)
  2132. {
  2133. using namespace std; // For strchr and atoi.
  2134. *scope_id = 0;
  2135. if (if_name != 0)
  2136. {
  2137. in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
  2138. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  2139. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  2140. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  2141. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  2142. if (is_link_local || is_multicast_link_local)
  2143. *scope_id = if_nametoindex(if_name + 1);
  2144. if (*scope_id == 0)
  2145. *scope_id = atoi(if_name + 1);
  2146. }
  2147. }
  2148. return result;
  2149. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2150. }
  2151. int gethostname(char* name, int namelen, boost::system::error_code& ec)
  2152. {
  2153. clear_last_error();
  2154. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2155. try
  2156. {
  2157. using namespace Windows::Foundation::Collections;
  2158. using namespace Windows::Networking;
  2159. using namespace Windows::Networking::Connectivity;
  2160. IVectorView<HostName^>^ hostnames = NetworkInformation::GetHostNames();
  2161. for (unsigned i = 0; i < hostnames->Size; ++i)
  2162. {
  2163. HostName^ hostname = hostnames->GetAt(i);
  2164. if (hostname->Type == HostNameType::DomainName)
  2165. {
  2166. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  2167. std::string raw_name = converter.to_bytes(hostname->RawName->Data());
  2168. if (namelen > 0 && raw_name.size() < static_cast<std::size_t>(namelen))
  2169. {
  2170. strcpy_s(name, namelen, raw_name.c_str());
  2171. return 0;
  2172. }
  2173. }
  2174. }
  2175. return -1;
  2176. }
  2177. catch (Platform::Exception^ e)
  2178. {
  2179. ec = boost::system::error_code(e->HResult,
  2180. boost::system::system_category());
  2181. return -1;
  2182. }
  2183. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2184. int result = error_wrapper(::gethostname(name, namelen), ec);
  2185. # if defined(BOOST_ASIO_WINDOWS)
  2186. if (result == 0)
  2187. ec = boost::system::error_code();
  2188. # endif // defined(BOOST_ASIO_WINDOWS)
  2189. return result;
  2190. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2191. }
  2192. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2193. #if !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2194. // The following functions are only needed for emulation of getaddrinfo and
  2195. // getnameinfo.
  2196. inline boost::system::error_code translate_netdb_error(int error)
  2197. {
  2198. switch (error)
  2199. {
  2200. case 0:
  2201. return boost::system::error_code();
  2202. case HOST_NOT_FOUND:
  2203. return boost::asio::error::host_not_found;
  2204. case TRY_AGAIN:
  2205. return boost::asio::error::host_not_found_try_again;
  2206. case NO_RECOVERY:
  2207. return boost::asio::error::no_recovery;
  2208. case NO_DATA:
  2209. return boost::asio::error::no_data;
  2210. default:
  2211. BOOST_ASIO_ASSERT(false);
  2212. return boost::asio::error::invalid_argument;
  2213. }
  2214. }
  2215. inline hostent* gethostbyaddr(const char* addr, int length, int af,
  2216. hostent* result, char* buffer, int buflength, boost::system::error_code& ec)
  2217. {
  2218. clear_last_error();
  2219. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2220. (void)(buffer);
  2221. (void)(buflength);
  2222. hostent* retval = error_wrapper(::gethostbyaddr(addr, length, af), ec);
  2223. if (!retval)
  2224. return 0;
  2225. ec = boost::system::error_code();
  2226. *result = *retval;
  2227. return retval;
  2228. #elif defined(__sun) || defined(__QNX__)
  2229. int error = 0;
  2230. hostent* retval = error_wrapper(::gethostbyaddr_r(addr, length, af, result,
  2231. buffer, buflength, &error), ec);
  2232. if (error)
  2233. ec = translate_netdb_error(error);
  2234. return retval;
  2235. #elif defined(__MACH__) && defined(__APPLE__)
  2236. (void)(buffer);
  2237. (void)(buflength);
  2238. int error = 0;
  2239. hostent* retval = error_wrapper(::getipnodebyaddr(
  2240. addr, length, af, &error), ec);
  2241. if (error)
  2242. ec = translate_netdb_error(error);
  2243. if (!retval)
  2244. return 0;
  2245. *result = *retval;
  2246. return retval;
  2247. #else
  2248. hostent* retval = 0;
  2249. int error = 0;
  2250. error_wrapper(::gethostbyaddr_r(addr, length, af, result, buffer,
  2251. buflength, &retval, &error), ec);
  2252. if (error)
  2253. ec = translate_netdb_error(error);
  2254. return retval;
  2255. #endif
  2256. }
  2257. inline hostent* gethostbyname(const char* name, int af, struct hostent* result,
  2258. char* buffer, int buflength, int ai_flags, boost::system::error_code& ec)
  2259. {
  2260. clear_last_error();
  2261. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2262. (void)(buffer);
  2263. (void)(buflength);
  2264. (void)(ai_flags);
  2265. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2266. {
  2267. ec = boost::asio::error::address_family_not_supported;
  2268. return 0;
  2269. }
  2270. hostent* retval = error_wrapper(::gethostbyname(name), ec);
  2271. if (!retval)
  2272. return 0;
  2273. ec = boost::system::error_code();
  2274. *result = *retval;
  2275. return result;
  2276. #elif defined(__sun) || defined(__QNX__)
  2277. (void)(ai_flags);
  2278. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2279. {
  2280. ec = boost::asio::error::address_family_not_supported;
  2281. return 0;
  2282. }
  2283. int error = 0;
  2284. hostent* retval = error_wrapper(::gethostbyname_r(name, result, buffer,
  2285. buflength, &error), ec);
  2286. if (error)
  2287. ec = translate_netdb_error(error);
  2288. return retval;
  2289. #elif defined(__MACH__) && defined(__APPLE__)
  2290. (void)(buffer);
  2291. (void)(buflength);
  2292. int error = 0;
  2293. hostent* retval = error_wrapper(::getipnodebyname(
  2294. name, af, ai_flags, &error), ec);
  2295. if (error)
  2296. ec = translate_netdb_error(error);
  2297. if (!retval)
  2298. return 0;
  2299. *result = *retval;
  2300. return retval;
  2301. #else
  2302. (void)(ai_flags);
  2303. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2304. {
  2305. ec = boost::asio::error::address_family_not_supported;
  2306. return 0;
  2307. }
  2308. hostent* retval = 0;
  2309. int error = 0;
  2310. error_wrapper(::gethostbyname_r(name, result,
  2311. buffer, buflength, &retval, &error), ec);
  2312. if (error)
  2313. ec = translate_netdb_error(error);
  2314. return retval;
  2315. #endif
  2316. }
  2317. inline void freehostent(hostent* h)
  2318. {
  2319. #if defined(__MACH__) && defined(__APPLE__)
  2320. if (h)
  2321. ::freehostent(h);
  2322. #else
  2323. (void)(h);
  2324. #endif
  2325. }
  2326. // Emulation of getaddrinfo based on implementation in:
  2327. // Stevens, W. R., UNIX Network Programming Vol. 1, 2nd Ed., Prentice-Hall 1998.
  2328. struct gai_search
  2329. {
  2330. const char* host;
  2331. int family;
  2332. };
  2333. inline int gai_nsearch(const char* host,
  2334. const addrinfo_type* hints, gai_search (&search)[2])
  2335. {
  2336. int search_count = 0;
  2337. if (host == 0 || host[0] == '\0')
  2338. {
  2339. if (hints->ai_flags & AI_PASSIVE)
  2340. {
  2341. // No host and AI_PASSIVE implies wildcard bind.
  2342. switch (hints->ai_family)
  2343. {
  2344. case BOOST_ASIO_OS_DEF(AF_INET):
  2345. search[search_count].host = "0.0.0.0";
  2346. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2347. ++search_count;
  2348. break;
  2349. case BOOST_ASIO_OS_DEF(AF_INET6):
  2350. search[search_count].host = "0::0";
  2351. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2352. ++search_count;
  2353. break;
  2354. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2355. search[search_count].host = "0::0";
  2356. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2357. ++search_count;
  2358. search[search_count].host = "0.0.0.0";
  2359. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2360. ++search_count;
  2361. break;
  2362. default:
  2363. break;
  2364. }
  2365. }
  2366. else
  2367. {
  2368. // No host and not AI_PASSIVE means connect to local host.
  2369. switch (hints->ai_family)
  2370. {
  2371. case BOOST_ASIO_OS_DEF(AF_INET):
  2372. search[search_count].host = "localhost";
  2373. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2374. ++search_count;
  2375. break;
  2376. case BOOST_ASIO_OS_DEF(AF_INET6):
  2377. search[search_count].host = "localhost";
  2378. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2379. ++search_count;
  2380. break;
  2381. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2382. search[search_count].host = "localhost";
  2383. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2384. ++search_count;
  2385. search[search_count].host = "localhost";
  2386. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2387. ++search_count;
  2388. break;
  2389. default:
  2390. break;
  2391. }
  2392. }
  2393. }
  2394. else
  2395. {
  2396. // Host is specified.
  2397. switch (hints->ai_family)
  2398. {
  2399. case BOOST_ASIO_OS_DEF(AF_INET):
  2400. search[search_count].host = host;
  2401. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2402. ++search_count;
  2403. break;
  2404. case BOOST_ASIO_OS_DEF(AF_INET6):
  2405. search[search_count].host = host;
  2406. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2407. ++search_count;
  2408. break;
  2409. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2410. search[search_count].host = host;
  2411. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2412. ++search_count;
  2413. search[search_count].host = host;
  2414. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2415. ++search_count;
  2416. break;
  2417. default:
  2418. break;
  2419. }
  2420. }
  2421. return search_count;
  2422. }
  2423. template <typename T>
  2424. inline T* gai_alloc(std::size_t size = sizeof(T))
  2425. {
  2426. using namespace std;
  2427. T* p = static_cast<T*>(::operator new(size, std::nothrow));
  2428. if (p)
  2429. memset(p, 0, size);
  2430. return p;
  2431. }
  2432. inline void gai_free(void* p)
  2433. {
  2434. ::operator delete(p);
  2435. }
  2436. inline void gai_strcpy(char* target, const char* source, std::size_t max_size)
  2437. {
  2438. using namespace std;
  2439. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2440. strcpy_s(target, max_size, source);
  2441. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2442. *target = 0;
  2443. if (max_size > 0)
  2444. strncat(target, source, max_size - 1);
  2445. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2446. }
  2447. enum { gai_clone_flag = 1 << 30 };
  2448. inline int gai_aistruct(addrinfo_type*** next, const addrinfo_type* hints,
  2449. const void* addr, int family)
  2450. {
  2451. using namespace std;
  2452. addrinfo_type* ai = gai_alloc<addrinfo_type>();
  2453. if (ai == 0)
  2454. return EAI_MEMORY;
  2455. ai->ai_next = 0;
  2456. **next = ai;
  2457. *next = &ai->ai_next;
  2458. ai->ai_canonname = 0;
  2459. ai->ai_socktype = hints->ai_socktype;
  2460. if (ai->ai_socktype == 0)
  2461. ai->ai_flags |= gai_clone_flag;
  2462. ai->ai_protocol = hints->ai_protocol;
  2463. ai->ai_family = family;
  2464. switch (ai->ai_family)
  2465. {
  2466. case BOOST_ASIO_OS_DEF(AF_INET):
  2467. {
  2468. sockaddr_in4_type* sinptr = gai_alloc<sockaddr_in4_type>();
  2469. if (sinptr == 0)
  2470. return EAI_MEMORY;
  2471. sinptr->sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  2472. memcpy(&sinptr->sin_addr, addr, sizeof(in4_addr_type));
  2473. ai->ai_addr = reinterpret_cast<sockaddr*>(sinptr);
  2474. ai->ai_addrlen = sizeof(sockaddr_in4_type);
  2475. break;
  2476. }
  2477. case BOOST_ASIO_OS_DEF(AF_INET6):
  2478. {
  2479. sockaddr_in6_type* sin6ptr = gai_alloc<sockaddr_in6_type>();
  2480. if (sin6ptr == 0)
  2481. return EAI_MEMORY;
  2482. sin6ptr->sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  2483. memcpy(&sin6ptr->sin6_addr, addr, sizeof(in6_addr_type));
  2484. ai->ai_addr = reinterpret_cast<sockaddr*>(sin6ptr);
  2485. ai->ai_addrlen = sizeof(sockaddr_in6_type);
  2486. break;
  2487. }
  2488. default:
  2489. break;
  2490. }
  2491. return 0;
  2492. }
  2493. inline addrinfo_type* gai_clone(addrinfo_type* ai)
  2494. {
  2495. using namespace std;
  2496. addrinfo_type* new_ai = gai_alloc<addrinfo_type>();
  2497. if (new_ai == 0)
  2498. return new_ai;
  2499. new_ai->ai_next = ai->ai_next;
  2500. ai->ai_next = new_ai;
  2501. new_ai->ai_flags = 0;
  2502. new_ai->ai_family = ai->ai_family;
  2503. new_ai->ai_socktype = ai->ai_socktype;
  2504. new_ai->ai_protocol = ai->ai_protocol;
  2505. new_ai->ai_canonname = 0;
  2506. new_ai->ai_addrlen = ai->ai_addrlen;
  2507. new_ai->ai_addr = gai_alloc<sockaddr>(ai->ai_addrlen);
  2508. memcpy(new_ai->ai_addr, ai->ai_addr, ai->ai_addrlen);
  2509. return new_ai;
  2510. }
  2511. inline int gai_port(addrinfo_type* aihead, int port, int socktype)
  2512. {
  2513. int num_found = 0;
  2514. for (addrinfo_type* ai = aihead; ai; ai = ai->ai_next)
  2515. {
  2516. if (ai->ai_flags & gai_clone_flag)
  2517. {
  2518. if (ai->ai_socktype != 0)
  2519. {
  2520. ai = gai_clone(ai);
  2521. if (ai == 0)
  2522. return -1;
  2523. // ai now points to newly cloned entry.
  2524. }
  2525. }
  2526. else if (ai->ai_socktype != socktype)
  2527. {
  2528. // Ignore if mismatch on socket type.
  2529. continue;
  2530. }
  2531. ai->ai_socktype = socktype;
  2532. switch (ai->ai_family)
  2533. {
  2534. case BOOST_ASIO_OS_DEF(AF_INET):
  2535. {
  2536. sockaddr_in4_type* sinptr =
  2537. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  2538. sinptr->sin_port = port;
  2539. ++num_found;
  2540. break;
  2541. }
  2542. case BOOST_ASIO_OS_DEF(AF_INET6):
  2543. {
  2544. sockaddr_in6_type* sin6ptr =
  2545. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  2546. sin6ptr->sin6_port = port;
  2547. ++num_found;
  2548. break;
  2549. }
  2550. default:
  2551. break;
  2552. }
  2553. }
  2554. return num_found;
  2555. }
  2556. inline int gai_serv(addrinfo_type* aihead,
  2557. const addrinfo_type* hints, const char* serv)
  2558. {
  2559. using namespace std;
  2560. int num_found = 0;
  2561. if (
  2562. #if defined(AI_NUMERICSERV)
  2563. (hints->ai_flags & AI_NUMERICSERV) ||
  2564. #endif
  2565. isdigit(static_cast<unsigned char>(serv[0])))
  2566. {
  2567. int port = htons(atoi(serv));
  2568. if (hints->ai_socktype)
  2569. {
  2570. // Caller specifies socket type.
  2571. int rc = gai_port(aihead, port, hints->ai_socktype);
  2572. if (rc < 0)
  2573. return EAI_MEMORY;
  2574. num_found += rc;
  2575. }
  2576. else
  2577. {
  2578. // Caller does not specify socket type.
  2579. int rc = gai_port(aihead, port, SOCK_STREAM);
  2580. if (rc < 0)
  2581. return EAI_MEMORY;
  2582. num_found += rc;
  2583. rc = gai_port(aihead, port, SOCK_DGRAM);
  2584. if (rc < 0)
  2585. return EAI_MEMORY;
  2586. num_found += rc;
  2587. }
  2588. }
  2589. else
  2590. {
  2591. // Try service name with TCP first, then UDP.
  2592. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
  2593. {
  2594. servent* sptr = getservbyname(serv, "tcp");
  2595. if (sptr != 0)
  2596. {
  2597. int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
  2598. if (rc < 0)
  2599. return EAI_MEMORY;
  2600. num_found += rc;
  2601. }
  2602. }
  2603. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
  2604. {
  2605. servent* sptr = getservbyname(serv, "udp");
  2606. if (sptr != 0)
  2607. {
  2608. int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
  2609. if (rc < 0)
  2610. return EAI_MEMORY;
  2611. num_found += rc;
  2612. }
  2613. }
  2614. }
  2615. if (num_found == 0)
  2616. {
  2617. if (hints->ai_socktype == 0)
  2618. {
  2619. // All calls to getservbyname() failed.
  2620. return EAI_NONAME;
  2621. }
  2622. else
  2623. {
  2624. // Service not supported for socket type.
  2625. return EAI_SERVICE;
  2626. }
  2627. }
  2628. return 0;
  2629. }
  2630. inline int gai_echeck(const char* host, const char* service,
  2631. int flags, int family, int socktype, int protocol)
  2632. {
  2633. (void)(flags);
  2634. (void)(protocol);
  2635. // Host or service must be specified.
  2636. if (host == 0 || host[0] == '\0')
  2637. if (service == 0 || service[0] == '\0')
  2638. return EAI_NONAME;
  2639. // Check combination of family and socket type.
  2640. switch (family)
  2641. {
  2642. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2643. break;
  2644. case BOOST_ASIO_OS_DEF(AF_INET):
  2645. case BOOST_ASIO_OS_DEF(AF_INET6):
  2646. if (service != 0 && service[0] != '\0')
  2647. if (socktype != 0 && socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
  2648. return EAI_SOCKTYPE;
  2649. break;
  2650. default:
  2651. return EAI_FAMILY;
  2652. }
  2653. return 0;
  2654. }
  2655. inline void freeaddrinfo_emulation(addrinfo_type* aihead)
  2656. {
  2657. addrinfo_type* ai = aihead;
  2658. while (ai)
  2659. {
  2660. gai_free(ai->ai_addr);
  2661. gai_free(ai->ai_canonname);
  2662. addrinfo_type* ainext = ai->ai_next;
  2663. gai_free(ai);
  2664. ai = ainext;
  2665. }
  2666. }
  2667. inline int getaddrinfo_emulation(const char* host, const char* service,
  2668. const addrinfo_type* hintsp, addrinfo_type** result)
  2669. {
  2670. // Set up linked list of addrinfo structures.
  2671. addrinfo_type* aihead = 0;
  2672. addrinfo_type** ainext = &aihead;
  2673. char* canon = 0;
  2674. // Supply default hints if not specified by caller.
  2675. addrinfo_type hints = addrinfo_type();
  2676. hints.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
  2677. if (hintsp)
  2678. hints = *hintsp;
  2679. // If the resolution is not specifically for AF_INET6, remove the AI_V4MAPPED
  2680. // and AI_ALL flags.
  2681. #if defined(AI_V4MAPPED)
  2682. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2683. hints.ai_flags &= ~AI_V4MAPPED;
  2684. #endif
  2685. #if defined(AI_ALL)
  2686. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2687. hints.ai_flags &= ~AI_ALL;
  2688. #endif
  2689. // Basic error checking.
  2690. int rc = gai_echeck(host, service, hints.ai_flags, hints.ai_family,
  2691. hints.ai_socktype, hints.ai_protocol);
  2692. if (rc != 0)
  2693. {
  2694. freeaddrinfo_emulation(aihead);
  2695. return rc;
  2696. }
  2697. gai_search search[2];
  2698. int search_count = gai_nsearch(host, &hints, search);
  2699. for (gai_search* sptr = search; sptr < search + search_count; ++sptr)
  2700. {
  2701. // Check for IPv4 dotted decimal string.
  2702. in4_addr_type inaddr;
  2703. boost::system::error_code ec;
  2704. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET),
  2705. sptr->host, &inaddr, 0, ec) == 1)
  2706. {
  2707. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2708. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET))
  2709. {
  2710. freeaddrinfo_emulation(aihead);
  2711. gai_free(canon);
  2712. return EAI_FAMILY;
  2713. }
  2714. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET))
  2715. {
  2716. rc = gai_aistruct(&ainext, &hints, &inaddr, BOOST_ASIO_OS_DEF(AF_INET));
  2717. if (rc != 0)
  2718. {
  2719. freeaddrinfo_emulation(aihead);
  2720. gai_free(canon);
  2721. return rc;
  2722. }
  2723. }
  2724. continue;
  2725. }
  2726. // Check for IPv6 hex string.
  2727. in6_addr_type in6addr;
  2728. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6),
  2729. sptr->host, &in6addr, 0, ec) == 1)
  2730. {
  2731. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2732. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2733. {
  2734. freeaddrinfo_emulation(aihead);
  2735. gai_free(canon);
  2736. return EAI_FAMILY;
  2737. }
  2738. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET6))
  2739. {
  2740. rc = gai_aistruct(&ainext, &hints, &in6addr,
  2741. BOOST_ASIO_OS_DEF(AF_INET6));
  2742. if (rc != 0)
  2743. {
  2744. freeaddrinfo_emulation(aihead);
  2745. gai_free(canon);
  2746. return rc;
  2747. }
  2748. }
  2749. continue;
  2750. }
  2751. // Look up hostname.
  2752. hostent hent;
  2753. char hbuf[8192] = "";
  2754. hostent* hptr = socket_ops::gethostbyname(sptr->host,
  2755. sptr->family, &hent, hbuf, sizeof(hbuf), hints.ai_flags, ec);
  2756. if (hptr == 0)
  2757. {
  2758. if (search_count == 2)
  2759. {
  2760. // Failure is OK if there are multiple searches.
  2761. continue;
  2762. }
  2763. freeaddrinfo_emulation(aihead);
  2764. gai_free(canon);
  2765. if (ec == boost::asio::error::host_not_found)
  2766. return EAI_NONAME;
  2767. if (ec == boost::asio::error::host_not_found_try_again)
  2768. return EAI_AGAIN;
  2769. if (ec == boost::asio::error::no_recovery)
  2770. return EAI_FAIL;
  2771. if (ec == boost::asio::error::no_data)
  2772. return EAI_NONAME;
  2773. return EAI_NONAME;
  2774. }
  2775. // Check for address family mismatch if one was specified.
  2776. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2777. && hints.ai_family != hptr->h_addrtype)
  2778. {
  2779. freeaddrinfo_emulation(aihead);
  2780. gai_free(canon);
  2781. socket_ops::freehostent(hptr);
  2782. return EAI_FAMILY;
  2783. }
  2784. // Save canonical name first time.
  2785. if (host != 0 && host[0] != '\0' && hptr->h_name && hptr->h_name[0]
  2786. && (hints.ai_flags & AI_CANONNAME) && canon == 0)
  2787. {
  2788. std::size_t canon_len = strlen(hptr->h_name) + 1;
  2789. canon = gai_alloc<char>(canon_len);
  2790. if (canon == 0)
  2791. {
  2792. freeaddrinfo_emulation(aihead);
  2793. socket_ops::freehostent(hptr);
  2794. return EAI_MEMORY;
  2795. }
  2796. gai_strcpy(canon, hptr->h_name, canon_len);
  2797. }
  2798. // Create an addrinfo structure for each returned address.
  2799. for (char** ap = hptr->h_addr_list; *ap; ++ap)
  2800. {
  2801. rc = gai_aistruct(&ainext, &hints, *ap, hptr->h_addrtype);
  2802. if (rc != 0)
  2803. {
  2804. freeaddrinfo_emulation(aihead);
  2805. gai_free(canon);
  2806. socket_ops::freehostent(hptr);
  2807. return EAI_FAMILY;
  2808. }
  2809. }
  2810. socket_ops::freehostent(hptr);
  2811. }
  2812. // Check if we found anything.
  2813. if (aihead == 0)
  2814. {
  2815. gai_free(canon);
  2816. return EAI_NONAME;
  2817. }
  2818. // Return canonical name in first entry.
  2819. if (host != 0 && host[0] != '\0' && (hints.ai_flags & AI_CANONNAME))
  2820. {
  2821. if (canon)
  2822. {
  2823. aihead->ai_canonname = canon;
  2824. canon = 0;
  2825. }
  2826. else
  2827. {
  2828. std::size_t canonname_len = strlen(search[0].host) + 1;
  2829. aihead->ai_canonname = gai_alloc<char>(canonname_len);
  2830. if (aihead->ai_canonname == 0)
  2831. {
  2832. freeaddrinfo_emulation(aihead);
  2833. return EAI_MEMORY;
  2834. }
  2835. gai_strcpy(aihead->ai_canonname, search[0].host, canonname_len);
  2836. }
  2837. }
  2838. gai_free(canon);
  2839. // Process the service name.
  2840. if (service != 0 && service[0] != '\0')
  2841. {
  2842. rc = gai_serv(aihead, &hints, service);
  2843. if (rc != 0)
  2844. {
  2845. freeaddrinfo_emulation(aihead);
  2846. return rc;
  2847. }
  2848. }
  2849. // Return result to caller.
  2850. *result = aihead;
  2851. return 0;
  2852. }
  2853. inline boost::system::error_code getnameinfo_emulation(
  2854. const socket_addr_type* sa, std::size_t salen, char* host,
  2855. std::size_t hostlen, char* serv, std::size_t servlen, int flags,
  2856. boost::system::error_code& ec)
  2857. {
  2858. using namespace std;
  2859. const char* addr;
  2860. size_t addr_len;
  2861. unsigned short port;
  2862. switch (sa->sa_family)
  2863. {
  2864. case BOOST_ASIO_OS_DEF(AF_INET):
  2865. if (salen != sizeof(sockaddr_in4_type))
  2866. {
  2867. return ec = boost::asio::error::invalid_argument;
  2868. }
  2869. addr = reinterpret_cast<const char*>(
  2870. &reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_addr);
  2871. addr_len = sizeof(in4_addr_type);
  2872. port = reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_port;
  2873. break;
  2874. case BOOST_ASIO_OS_DEF(AF_INET6):
  2875. if (salen != sizeof(sockaddr_in6_type))
  2876. {
  2877. return ec = boost::asio::error::invalid_argument;
  2878. }
  2879. addr = reinterpret_cast<const char*>(
  2880. &reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_addr);
  2881. addr_len = sizeof(in6_addr_type);
  2882. port = reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_port;
  2883. break;
  2884. default:
  2885. return ec = boost::asio::error::address_family_not_supported;
  2886. }
  2887. if (host && hostlen > 0)
  2888. {
  2889. if (flags & NI_NUMERICHOST)
  2890. {
  2891. if (socket_ops::inet_ntop(sa->sa_family, addr, host, hostlen, 0, ec) == 0)
  2892. {
  2893. return ec;
  2894. }
  2895. }
  2896. else
  2897. {
  2898. hostent hent;
  2899. char hbuf[8192] = "";
  2900. hostent* hptr = socket_ops::gethostbyaddr(addr,
  2901. static_cast<int>(addr_len), sa->sa_family,
  2902. &hent, hbuf, sizeof(hbuf), ec);
  2903. if (hptr && hptr->h_name && hptr->h_name[0] != '\0')
  2904. {
  2905. if (flags & NI_NOFQDN)
  2906. {
  2907. char* dot = strchr(hptr->h_name, '.');
  2908. if (dot)
  2909. {
  2910. *dot = 0;
  2911. }
  2912. }
  2913. gai_strcpy(host, hptr->h_name, hostlen);
  2914. socket_ops::freehostent(hptr);
  2915. }
  2916. else
  2917. {
  2918. socket_ops::freehostent(hptr);
  2919. if (flags & NI_NAMEREQD)
  2920. {
  2921. return ec = boost::asio::error::host_not_found;
  2922. }
  2923. if (socket_ops::inet_ntop(sa->sa_family,
  2924. addr, host, hostlen, 0, ec) == 0)
  2925. {
  2926. return ec;
  2927. }
  2928. }
  2929. }
  2930. }
  2931. if (serv && servlen > 0)
  2932. {
  2933. if (flags & NI_NUMERICSERV)
  2934. {
  2935. if (servlen < 6)
  2936. {
  2937. return ec = boost::asio::error::no_buffer_space;
  2938. }
  2939. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2940. sprintf_s(serv, servlen, "%u", ntohs(port));
  2941. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2942. sprintf(serv, "%u", ntohs(port));
  2943. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2944. }
  2945. else
  2946. {
  2947. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2948. static ::pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  2949. ::pthread_mutex_lock(&mutex);
  2950. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2951. servent* sptr = ::getservbyport(port, (flags & NI_DGRAM) ? "udp" : 0);
  2952. if (sptr && sptr->s_name && sptr->s_name[0] != '\0')
  2953. {
  2954. gai_strcpy(serv, sptr->s_name, servlen);
  2955. }
  2956. else
  2957. {
  2958. if (servlen < 6)
  2959. {
  2960. return ec = boost::asio::error::no_buffer_space;
  2961. }
  2962. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2963. sprintf_s(serv, servlen, "%u", ntohs(port));
  2964. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2965. sprintf(serv, "%u", ntohs(port));
  2966. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2967. }
  2968. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2969. ::pthread_mutex_unlock(&mutex);
  2970. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2971. }
  2972. }
  2973. ec = boost::system::error_code();
  2974. return ec;
  2975. }
  2976. #endif // !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2977. inline boost::system::error_code translate_addrinfo_error(int error)
  2978. {
  2979. switch (error)
  2980. {
  2981. case 0:
  2982. return boost::system::error_code();
  2983. case EAI_AGAIN:
  2984. return boost::asio::error::host_not_found_try_again;
  2985. case EAI_BADFLAGS:
  2986. return boost::asio::error::invalid_argument;
  2987. case EAI_FAIL:
  2988. return boost::asio::error::no_recovery;
  2989. case EAI_FAMILY:
  2990. return boost::asio::error::address_family_not_supported;
  2991. case EAI_MEMORY:
  2992. return boost::asio::error::no_memory;
  2993. case EAI_NONAME:
  2994. #if defined(EAI_ADDRFAMILY)
  2995. case EAI_ADDRFAMILY:
  2996. #endif
  2997. #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
  2998. case EAI_NODATA:
  2999. #endif
  3000. return boost::asio::error::host_not_found;
  3001. case EAI_SERVICE:
  3002. return boost::asio::error::service_not_found;
  3003. case EAI_SOCKTYPE:
  3004. return boost::asio::error::socket_type_not_supported;
  3005. default: // Possibly the non-portable EAI_SYSTEM.
  3006. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3007. return boost::system::error_code(
  3008. WSAGetLastError(), boost::asio::error::get_system_category());
  3009. #else
  3010. return boost::system::error_code(
  3011. errno, boost::asio::error::get_system_category());
  3012. #endif
  3013. }
  3014. }
  3015. boost::system::error_code getaddrinfo(const char* host,
  3016. const char* service, const addrinfo_type& hints,
  3017. addrinfo_type** result, boost::system::error_code& ec)
  3018. {
  3019. host = (host && *host) ? host : 0;
  3020. service = (service && *service) ? service : 0;
  3021. clear_last_error();
  3022. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3023. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3024. // Building for Windows XP, Windows Server 2003, or later.
  3025. int error = ::getaddrinfo(host, service, &hints, result);
  3026. return ec = translate_addrinfo_error(error);
  3027. # else
  3028. // Building for Windows 2000 or earlier.
  3029. typedef int (WSAAPI *gai_t)(const char*,
  3030. const char*, const addrinfo_type*, addrinfo_type**);
  3031. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3032. {
  3033. if (gai_t gai = (gai_t)::GetProcAddress(winsock_module, "getaddrinfo"))
  3034. {
  3035. int error = gai(host, service, &hints, result);
  3036. return ec = translate_addrinfo_error(error);
  3037. }
  3038. }
  3039. int error = getaddrinfo_emulation(host, service, &hints, result);
  3040. return ec = translate_addrinfo_error(error);
  3041. # endif
  3042. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3043. int error = getaddrinfo_emulation(host, service, &hints, result);
  3044. return ec = translate_addrinfo_error(error);
  3045. #else
  3046. int error = ::getaddrinfo(host, service, &hints, result);
  3047. #if defined(__MACH__) && defined(__APPLE__)
  3048. using namespace std; // For isdigit and atoi.
  3049. if (error == 0 && service && isdigit(static_cast<unsigned char>(service[0])))
  3050. {
  3051. u_short_type port = host_to_network_short(atoi(service));
  3052. for (addrinfo_type* ai = *result; ai; ai = ai->ai_next)
  3053. {
  3054. switch (ai->ai_family)
  3055. {
  3056. case BOOST_ASIO_OS_DEF(AF_INET):
  3057. {
  3058. sockaddr_in4_type* sinptr =
  3059. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  3060. if (sinptr->sin_port == 0)
  3061. sinptr->sin_port = port;
  3062. break;
  3063. }
  3064. case BOOST_ASIO_OS_DEF(AF_INET6):
  3065. {
  3066. sockaddr_in6_type* sin6ptr =
  3067. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  3068. if (sin6ptr->sin6_port == 0)
  3069. sin6ptr->sin6_port = port;
  3070. break;
  3071. }
  3072. default:
  3073. break;
  3074. }
  3075. }
  3076. }
  3077. #endif
  3078. return ec = translate_addrinfo_error(error);
  3079. #endif
  3080. }
  3081. boost::system::error_code background_getaddrinfo(
  3082. const weak_cancel_token_type& cancel_token, const char* host,
  3083. const char* service, const addrinfo_type& hints,
  3084. addrinfo_type** result, boost::system::error_code& ec)
  3085. {
  3086. if (cancel_token.expired())
  3087. ec = boost::asio::error::operation_aborted;
  3088. else
  3089. socket_ops::getaddrinfo(host, service, hints, result, ec);
  3090. return ec;
  3091. }
  3092. void freeaddrinfo(addrinfo_type* ai)
  3093. {
  3094. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3095. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3096. // Building for Windows XP, Windows Server 2003, or later.
  3097. ::freeaddrinfo(ai);
  3098. # else
  3099. // Building for Windows 2000 or earlier.
  3100. typedef int (WSAAPI *fai_t)(addrinfo_type*);
  3101. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3102. {
  3103. if (fai_t fai = (fai_t)::GetProcAddress(winsock_module, "freeaddrinfo"))
  3104. {
  3105. fai(ai);
  3106. return;
  3107. }
  3108. }
  3109. freeaddrinfo_emulation(ai);
  3110. # endif
  3111. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3112. freeaddrinfo_emulation(ai);
  3113. #else
  3114. ::freeaddrinfo(ai);
  3115. #endif
  3116. }
  3117. boost::system::error_code getnameinfo(const socket_addr_type* addr,
  3118. std::size_t addrlen, char* host, std::size_t hostlen,
  3119. char* serv, std::size_t servlen, int flags, boost::system::error_code& ec)
  3120. {
  3121. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3122. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3123. // Building for Windows XP, Windows Server 2003, or later.
  3124. clear_last_error();
  3125. int error = ::getnameinfo(addr, static_cast<socklen_t>(addrlen),
  3126. host, static_cast<DWORD>(hostlen),
  3127. serv, static_cast<DWORD>(servlen), flags);
  3128. return ec = translate_addrinfo_error(error);
  3129. # else
  3130. // Building for Windows 2000 or earlier.
  3131. typedef int (WSAAPI *gni_t)(const socket_addr_type*,
  3132. int, char*, DWORD, char*, DWORD, int);
  3133. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3134. {
  3135. if (gni_t gni = (gni_t)::GetProcAddress(winsock_module, "getnameinfo"))
  3136. {
  3137. clear_last_error();
  3138. int error = gni(addr, static_cast<int>(addrlen),
  3139. host, static_cast<DWORD>(hostlen),
  3140. serv, static_cast<DWORD>(servlen), flags);
  3141. return ec = translate_addrinfo_error(error);
  3142. }
  3143. }
  3144. clear_last_error();
  3145. return getnameinfo_emulation(addr, addrlen,
  3146. host, hostlen, serv, servlen, flags, ec);
  3147. # endif
  3148. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3149. using namespace std; // For memcpy.
  3150. sockaddr_storage_type tmp_addr;
  3151. memcpy(&tmp_addr, addr, addrlen);
  3152. addr = reinterpret_cast<socket_addr_type*>(&tmp_addr);
  3153. clear_last_error();
  3154. return getnameinfo_emulation(addr, addrlen,
  3155. host, hostlen, serv, servlen, flags, ec);
  3156. #else
  3157. clear_last_error();
  3158. int error = ::getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
  3159. return ec = translate_addrinfo_error(error);
  3160. #endif
  3161. }
  3162. boost::system::error_code sync_getnameinfo(
  3163. const socket_addr_type* addr, std::size_t addrlen,
  3164. char* host, std::size_t hostlen, char* serv,
  3165. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3166. {
  3167. // First try resolving with the service name. If that fails try resolving
  3168. // but allow the service to be returned as a number.
  3169. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3170. socket_ops::getnameinfo(addr, addrlen, host,
  3171. hostlen, serv, servlen, flags, ec);
  3172. if (ec)
  3173. {
  3174. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3175. serv, servlen, flags | NI_NUMERICSERV, ec);
  3176. }
  3177. return ec;
  3178. }
  3179. boost::system::error_code background_getnameinfo(
  3180. const weak_cancel_token_type& cancel_token,
  3181. const socket_addr_type* addr, std::size_t addrlen,
  3182. char* host, std::size_t hostlen, char* serv,
  3183. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3184. {
  3185. if (cancel_token.expired())
  3186. {
  3187. ec = boost::asio::error::operation_aborted;
  3188. }
  3189. else
  3190. {
  3191. // First try resolving with the service name. If that fails try resolving
  3192. // but allow the service to be returned as a number.
  3193. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3194. socket_ops::getnameinfo(addr, addrlen, host,
  3195. hostlen, serv, servlen, flags, ec);
  3196. if (ec)
  3197. {
  3198. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3199. serv, servlen, flags | NI_NUMERICSERV, ec);
  3200. }
  3201. }
  3202. return ec;
  3203. }
  3204. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3205. u_long_type network_to_host_long(u_long_type value)
  3206. {
  3207. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3208. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3209. u_long_type result = (static_cast<u_long_type>(value_p[0]) << 24)
  3210. | (static_cast<u_long_type>(value_p[1]) << 16)
  3211. | (static_cast<u_long_type>(value_p[2]) << 8)
  3212. | static_cast<u_long_type>(value_p[3]);
  3213. return result;
  3214. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3215. return ntohl(value);
  3216. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3217. }
  3218. u_long_type host_to_network_long(u_long_type value)
  3219. {
  3220. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3221. u_long_type result;
  3222. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3223. result_p[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
  3224. result_p[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
  3225. result_p[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3226. result_p[3] = static_cast<unsigned char>(value & 0xFF);
  3227. return result;
  3228. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3229. return htonl(value);
  3230. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3231. }
  3232. u_short_type network_to_host_short(u_short_type value)
  3233. {
  3234. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3235. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3236. u_short_type result = (static_cast<u_short_type>(value_p[0]) << 8)
  3237. | static_cast<u_short_type>(value_p[1]);
  3238. return result;
  3239. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3240. return ntohs(value);
  3241. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3242. }
  3243. u_short_type host_to_network_short(u_short_type value)
  3244. {
  3245. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3246. u_short_type result;
  3247. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3248. result_p[0] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3249. result_p[1] = static_cast<unsigned char>(value & 0xFF);
  3250. return result;
  3251. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3252. return htons(value);
  3253. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3254. }
  3255. } // namespace socket_ops
  3256. } // namespace detail
  3257. } // namespace asio
  3258. } // namespace boost
  3259. #include <boost/asio/detail/pop_options.hpp>
  3260. #endif // BOOST_ASIO_DETAIL_SOCKET_OPS_IPP