socket_ops.ipp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574
  1. //
  2. // detail/impl/socket_ops.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
  11. #define BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cctype>
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <cstring>
  20. #include <cerrno>
  21. #include <new>
  22. #include <boost/asio/detail/assert.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/error.hpp>
  25. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  26. # include <codecvt>
  27. # include <locale>
  28. # include <string>
  29. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  30. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) \
  31. || defined(__MACH__) && defined(__APPLE__)
  32. # if defined(BOOST_ASIO_HAS_PTHREADS)
  33. # include <pthread.h>
  34. # endif // defined(BOOST_ASIO_HAS_PTHREADS)
  35. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  36. // || defined(__MACH__) && defined(__APPLE__)
  37. #include <boost/asio/detail/push_options.hpp>
  38. namespace boost {
  39. namespace asio {
  40. namespace detail {
  41. namespace socket_ops {
  42. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  43. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  44. struct msghdr { int msg_namelen; };
  45. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  46. #if defined(__hpux)
  47. // HP-UX doesn't declare these functions extern "C", so they are declared again
  48. // here to avoid linker errors about undefined symbols.
  49. extern "C" char* if_indextoname(unsigned int, char*);
  50. extern "C" unsigned int if_nametoindex(const char*);
  51. #endif // defined(__hpux)
  52. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  53. inline void clear_last_error()
  54. {
  55. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  56. WSASetLastError(0);
  57. #else
  58. errno = 0;
  59. #endif
  60. }
  61. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  62. template <typename ReturnType>
  63. inline ReturnType error_wrapper(ReturnType return_value,
  64. boost::system::error_code& ec)
  65. {
  66. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  67. ec = boost::system::error_code(WSAGetLastError(),
  68. boost::asio::error::get_system_category());
  69. #else
  70. ec = boost::system::error_code(errno,
  71. boost::asio::error::get_system_category());
  72. #endif
  73. return return_value;
  74. }
  75. template <typename SockLenType>
  76. inline socket_type call_accept(SockLenType msghdr::*,
  77. socket_type s, socket_addr_type* addr, std::size_t* addrlen)
  78. {
  79. SockLenType tmp_addrlen = addrlen ? (SockLenType)*addrlen : 0;
  80. socket_type result = ::accept(s, addr, addrlen ? &tmp_addrlen : 0);
  81. if (addrlen)
  82. *addrlen = (std::size_t)tmp_addrlen;
  83. return result;
  84. }
  85. socket_type accept(socket_type s, socket_addr_type* addr,
  86. std::size_t* addrlen, boost::system::error_code& ec)
  87. {
  88. if (s == invalid_socket)
  89. {
  90. ec = boost::asio::error::bad_descriptor;
  91. return invalid_socket;
  92. }
  93. clear_last_error();
  94. socket_type new_s = error_wrapper(call_accept(
  95. &msghdr::msg_namelen, s, addr, addrlen), ec);
  96. if (new_s == invalid_socket)
  97. return new_s;
  98. #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
  99. int optval = 1;
  100. int result = error_wrapper(::setsockopt(new_s,
  101. SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
  102. if (result != 0)
  103. {
  104. ::close(new_s);
  105. return invalid_socket;
  106. }
  107. #endif
  108. ec = boost::system::error_code();
  109. return new_s;
  110. }
  111. socket_type sync_accept(socket_type s, state_type state,
  112. socket_addr_type* addr, std::size_t* addrlen, boost::system::error_code& ec)
  113. {
  114. // Accept a socket.
  115. for (;;)
  116. {
  117. // Try to complete the operation without blocking.
  118. socket_type new_socket = socket_ops::accept(s, addr, addrlen, ec);
  119. // Check if operation succeeded.
  120. if (new_socket != invalid_socket)
  121. return new_socket;
  122. // Operation failed.
  123. if (ec == boost::asio::error::would_block
  124. || ec == boost::asio::error::try_again)
  125. {
  126. if (state & user_set_non_blocking)
  127. return invalid_socket;
  128. // Fall through to retry operation.
  129. }
  130. else if (ec == boost::asio::error::connection_aborted)
  131. {
  132. if (state & enable_connection_aborted)
  133. return invalid_socket;
  134. // Fall through to retry operation.
  135. }
  136. #if defined(EPROTO)
  137. else if (ec.value() == EPROTO)
  138. {
  139. if (state & enable_connection_aborted)
  140. return invalid_socket;
  141. // Fall through to retry operation.
  142. }
  143. #endif // defined(EPROTO)
  144. else
  145. return invalid_socket;
  146. // Wait for socket to become ready.
  147. if (socket_ops::poll_read(s, 0, -1, ec) < 0)
  148. return invalid_socket;
  149. }
  150. }
  151. #if defined(BOOST_ASIO_HAS_IOCP)
  152. void complete_iocp_accept(socket_type s,
  153. void* output_buffer, DWORD address_length,
  154. socket_addr_type* addr, std::size_t* addrlen,
  155. socket_type new_socket, boost::system::error_code& ec)
  156. {
  157. // Map non-portable errors to their portable counterparts.
  158. if (ec.value() == ERROR_NETNAME_DELETED)
  159. ec = boost::asio::error::connection_aborted;
  160. if (!ec)
  161. {
  162. // Get the address of the peer.
  163. if (addr && addrlen)
  164. {
  165. LPSOCKADDR local_addr = 0;
  166. int local_addr_length = 0;
  167. LPSOCKADDR remote_addr = 0;
  168. int remote_addr_length = 0;
  169. GetAcceptExSockaddrs(output_buffer, 0, address_length,
  170. address_length, &local_addr, &local_addr_length,
  171. &remote_addr, &remote_addr_length);
  172. if (static_cast<std::size_t>(remote_addr_length) > *addrlen)
  173. {
  174. ec = boost::asio::error::invalid_argument;
  175. }
  176. else
  177. {
  178. using namespace std; // For memcpy.
  179. memcpy(addr, remote_addr, remote_addr_length);
  180. *addrlen = static_cast<std::size_t>(remote_addr_length);
  181. }
  182. }
  183. // Need to set the SO_UPDATE_ACCEPT_CONTEXT option so that getsockname
  184. // and getpeername will work on the accepted socket.
  185. SOCKET update_ctx_param = s;
  186. socket_ops::state_type state = 0;
  187. socket_ops::setsockopt(new_socket, state,
  188. SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
  189. &update_ctx_param, sizeof(SOCKET), ec);
  190. }
  191. }
  192. #else // defined(BOOST_ASIO_HAS_IOCP)
  193. bool non_blocking_accept(socket_type s,
  194. state_type state, socket_addr_type* addr, std::size_t* addrlen,
  195. boost::system::error_code& ec, socket_type& new_socket)
  196. {
  197. for (;;)
  198. {
  199. // Accept the waiting connection.
  200. new_socket = socket_ops::accept(s, addr, addrlen, ec);
  201. // Check if operation succeeded.
  202. if (new_socket != invalid_socket)
  203. return true;
  204. // Retry operation if interrupted by signal.
  205. if (ec == boost::asio::error::interrupted)
  206. continue;
  207. // Operation failed.
  208. if (ec == boost::asio::error::would_block
  209. || ec == boost::asio::error::try_again)
  210. {
  211. // Fall through to retry operation.
  212. }
  213. else if (ec == boost::asio::error::connection_aborted)
  214. {
  215. if (state & enable_connection_aborted)
  216. return true;
  217. // Fall through to retry operation.
  218. }
  219. #if defined(EPROTO)
  220. else if (ec.value() == EPROTO)
  221. {
  222. if (state & enable_connection_aborted)
  223. return true;
  224. // Fall through to retry operation.
  225. }
  226. #endif // defined(EPROTO)
  227. else
  228. return true;
  229. return false;
  230. }
  231. }
  232. #endif // defined(BOOST_ASIO_HAS_IOCP)
  233. template <typename SockLenType>
  234. inline int call_bind(SockLenType msghdr::*,
  235. socket_type s, const socket_addr_type* addr, std::size_t addrlen)
  236. {
  237. return ::bind(s, addr, (SockLenType)addrlen);
  238. }
  239. int bind(socket_type s, const socket_addr_type* addr,
  240. std::size_t addrlen, boost::system::error_code& ec)
  241. {
  242. if (s == invalid_socket)
  243. {
  244. ec = boost::asio::error::bad_descriptor;
  245. return socket_error_retval;
  246. }
  247. clear_last_error();
  248. int result = error_wrapper(call_bind(
  249. &msghdr::msg_namelen, s, addr, addrlen), ec);
  250. if (result == 0)
  251. ec = boost::system::error_code();
  252. return result;
  253. }
  254. int close(socket_type s, state_type& state,
  255. bool destruction, boost::system::error_code& ec)
  256. {
  257. int result = 0;
  258. if (s != invalid_socket)
  259. {
  260. // We don't want the destructor to block, so set the socket to linger in
  261. // the background. If the user doesn't like this behaviour then they need
  262. // to explicitly close the socket.
  263. if (destruction && (state & user_set_linger))
  264. {
  265. ::linger opt;
  266. opt.l_onoff = 0;
  267. opt.l_linger = 0;
  268. boost::system::error_code ignored_ec;
  269. socket_ops::setsockopt(s, state, SOL_SOCKET,
  270. SO_LINGER, &opt, sizeof(opt), ignored_ec);
  271. }
  272. clear_last_error();
  273. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  274. result = error_wrapper(::closesocket(s), ec);
  275. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  276. result = error_wrapper(::close(s), ec);
  277. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  278. if (result != 0
  279. && (ec == boost::asio::error::would_block
  280. || ec == boost::asio::error::try_again))
  281. {
  282. // According to UNIX Network Programming Vol. 1, it is possible for
  283. // close() to fail with EWOULDBLOCK under certain circumstances. What
  284. // isn't clear is the state of the descriptor after this error. The one
  285. // current OS where this behaviour is seen, Windows, says that the socket
  286. // remains open. Therefore we'll put the descriptor back into blocking
  287. // mode and have another attempt at closing it.
  288. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  289. ioctl_arg_type arg = 0;
  290. ::ioctlsocket(s, FIONBIO, &arg);
  291. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  292. # if defined(__SYMBIAN32__)
  293. int flags = ::fcntl(s, F_GETFL, 0);
  294. if (flags >= 0)
  295. ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
  296. # else // defined(__SYMBIAN32__)
  297. ioctl_arg_type arg = 0;
  298. ::ioctl(s, FIONBIO, &arg);
  299. # endif // defined(__SYMBIAN32__)
  300. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  301. state &= ~non_blocking;
  302. clear_last_error();
  303. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  304. result = error_wrapper(::closesocket(s), ec);
  305. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  306. result = error_wrapper(::close(s), ec);
  307. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  308. }
  309. }
  310. if (result == 0)
  311. ec = boost::system::error_code();
  312. return result;
  313. }
  314. bool set_user_non_blocking(socket_type s,
  315. state_type& state, bool value, boost::system::error_code& ec)
  316. {
  317. if (s == invalid_socket)
  318. {
  319. ec = boost::asio::error::bad_descriptor;
  320. return false;
  321. }
  322. clear_last_error();
  323. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  324. ioctl_arg_type arg = (value ? 1 : 0);
  325. int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
  326. #elif defined(__SYMBIAN32__)
  327. int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
  328. if (result >= 0)
  329. {
  330. clear_last_error();
  331. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  332. result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
  333. }
  334. #else
  335. ioctl_arg_type arg = (value ? 1 : 0);
  336. int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
  337. #endif
  338. if (result >= 0)
  339. {
  340. ec = boost::system::error_code();
  341. if (value)
  342. state |= user_set_non_blocking;
  343. else
  344. {
  345. // Clearing the user-set non-blocking mode always overrides any
  346. // internally-set non-blocking flag. Any subsequent asynchronous
  347. // operations will need to re-enable non-blocking I/O.
  348. state &= ~(user_set_non_blocking | internal_non_blocking);
  349. }
  350. return true;
  351. }
  352. return false;
  353. }
  354. bool set_internal_non_blocking(socket_type s,
  355. state_type& state, bool value, boost::system::error_code& ec)
  356. {
  357. if (s == invalid_socket)
  358. {
  359. ec = boost::asio::error::bad_descriptor;
  360. return false;
  361. }
  362. if (!value && (state & user_set_non_blocking))
  363. {
  364. // It does not make sense to clear the internal non-blocking flag if the
  365. // user still wants non-blocking behaviour. Return an error and let the
  366. // caller figure out whether to update the user-set non-blocking flag.
  367. ec = boost::asio::error::invalid_argument;
  368. return false;
  369. }
  370. clear_last_error();
  371. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  372. ioctl_arg_type arg = (value ? 1 : 0);
  373. int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
  374. #elif defined(__SYMBIAN32__)
  375. int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
  376. if (result >= 0)
  377. {
  378. clear_last_error();
  379. int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
  380. result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
  381. }
  382. #else
  383. ioctl_arg_type arg = (value ? 1 : 0);
  384. int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
  385. #endif
  386. if (result >= 0)
  387. {
  388. ec = boost::system::error_code();
  389. if (value)
  390. state |= internal_non_blocking;
  391. else
  392. state &= ~internal_non_blocking;
  393. return true;
  394. }
  395. return false;
  396. }
  397. int shutdown(socket_type s, int what, boost::system::error_code& ec)
  398. {
  399. if (s == invalid_socket)
  400. {
  401. ec = boost::asio::error::bad_descriptor;
  402. return socket_error_retval;
  403. }
  404. clear_last_error();
  405. int result = error_wrapper(::shutdown(s, what), ec);
  406. if (result == 0)
  407. ec = boost::system::error_code();
  408. return result;
  409. }
  410. template <typename SockLenType>
  411. inline int call_connect(SockLenType msghdr::*,
  412. socket_type s, const socket_addr_type* addr, std::size_t addrlen)
  413. {
  414. return ::connect(s, addr, (SockLenType)addrlen);
  415. }
  416. int connect(socket_type s, const socket_addr_type* addr,
  417. std::size_t addrlen, boost::system::error_code& ec)
  418. {
  419. if (s == invalid_socket)
  420. {
  421. ec = boost::asio::error::bad_descriptor;
  422. return socket_error_retval;
  423. }
  424. clear_last_error();
  425. int result = error_wrapper(call_connect(
  426. &msghdr::msg_namelen, s, addr, addrlen), ec);
  427. if (result == 0)
  428. ec = boost::system::error_code();
  429. #if defined(__linux__)
  430. else if (ec == boost::asio::error::try_again)
  431. ec = boost::asio::error::no_buffer_space;
  432. #endif // defined(__linux__)
  433. return result;
  434. }
  435. void sync_connect(socket_type s, const socket_addr_type* addr,
  436. std::size_t addrlen, boost::system::error_code& ec)
  437. {
  438. // Perform the connect operation.
  439. socket_ops::connect(s, addr, addrlen, ec);
  440. if (ec != boost::asio::error::in_progress
  441. && ec != boost::asio::error::would_block)
  442. {
  443. // The connect operation finished immediately.
  444. return;
  445. }
  446. // Wait for socket to become ready.
  447. if (socket_ops::poll_connect(s, -1, ec) < 0)
  448. return;
  449. // Get the error code from the connect operation.
  450. int connect_error = 0;
  451. size_t connect_error_len = sizeof(connect_error);
  452. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
  453. &connect_error, &connect_error_len, ec) == socket_error_retval)
  454. return;
  455. // Return the result of the connect operation.
  456. ec = boost::system::error_code(connect_error,
  457. boost::asio::error::get_system_category());
  458. }
  459. #if defined(BOOST_ASIO_HAS_IOCP)
  460. void complete_iocp_connect(socket_type s, boost::system::error_code& ec)
  461. {
  462. // Map non-portable errors to their portable counterparts.
  463. switch (ec.value())
  464. {
  465. case ERROR_CONNECTION_REFUSED:
  466. ec = boost::asio::error::connection_refused;
  467. break;
  468. case ERROR_NETWORK_UNREACHABLE:
  469. ec = boost::asio::error::network_unreachable;
  470. break;
  471. case ERROR_HOST_UNREACHABLE:
  472. ec = boost::asio::error::host_unreachable;
  473. break;
  474. case ERROR_SEM_TIMEOUT:
  475. ec = boost::asio::error::timed_out;
  476. break;
  477. default:
  478. break;
  479. }
  480. if (!ec)
  481. {
  482. // Need to set the SO_UPDATE_CONNECT_CONTEXT option so that getsockname
  483. // and getpeername will work on the connected socket.
  484. socket_ops::state_type state = 0;
  485. const int so_update_connect_context = 0x7010;
  486. socket_ops::setsockopt(s, state, SOL_SOCKET,
  487. so_update_connect_context, 0, 0, ec);
  488. }
  489. }
  490. #endif // defined(BOOST_ASIO_HAS_IOCP)
  491. bool non_blocking_connect(socket_type s, boost::system::error_code& ec)
  492. {
  493. // Check if the connect operation has finished. This is required since we may
  494. // get spurious readiness notifications from the reactor.
  495. #if defined(BOOST_ASIO_WINDOWS) \
  496. || defined(__CYGWIN__) \
  497. || defined(__SYMBIAN32__)
  498. fd_set write_fds;
  499. FD_ZERO(&write_fds);
  500. FD_SET(s, &write_fds);
  501. fd_set except_fds;
  502. FD_ZERO(&except_fds);
  503. FD_SET(s, &except_fds);
  504. timeval zero_timeout;
  505. zero_timeout.tv_sec = 0;
  506. zero_timeout.tv_usec = 0;
  507. int ready = ::select(s + 1, 0, &write_fds, &except_fds, &zero_timeout);
  508. #else // defined(BOOST_ASIO_WINDOWS)
  509. // || defined(__CYGWIN__)
  510. // || defined(__SYMBIAN32__)
  511. pollfd fds;
  512. fds.fd = s;
  513. fds.events = POLLOUT;
  514. fds.revents = 0;
  515. int ready = ::poll(&fds, 1, 0);
  516. #endif // defined(BOOST_ASIO_WINDOWS)
  517. // || defined(__CYGWIN__)
  518. // || defined(__SYMBIAN32__)
  519. if (ready == 0)
  520. {
  521. // The asynchronous connect operation is still in progress.
  522. return false;
  523. }
  524. // Get the error code from the connect operation.
  525. int connect_error = 0;
  526. size_t connect_error_len = sizeof(connect_error);
  527. if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
  528. &connect_error, &connect_error_len, ec) == 0)
  529. {
  530. if (connect_error)
  531. {
  532. ec = boost::system::error_code(connect_error,
  533. boost::asio::error::get_system_category());
  534. }
  535. else
  536. ec = boost::system::error_code();
  537. }
  538. return true;
  539. }
  540. int socketpair(int af, int type, int protocol,
  541. socket_type sv[2], boost::system::error_code& ec)
  542. {
  543. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  544. (void)(af);
  545. (void)(type);
  546. (void)(protocol);
  547. (void)(sv);
  548. ec = boost::asio::error::operation_not_supported;
  549. return socket_error_retval;
  550. #else
  551. clear_last_error();
  552. int result = error_wrapper(::socketpair(af, type, protocol, sv), ec);
  553. if (result == 0)
  554. ec = boost::system::error_code();
  555. return result;
  556. #endif
  557. }
  558. bool sockatmark(socket_type s, boost::system::error_code& ec)
  559. {
  560. if (s == invalid_socket)
  561. {
  562. ec = boost::asio::error::bad_descriptor;
  563. return false;
  564. }
  565. #if defined(SIOCATMARK)
  566. ioctl_arg_type value = 0;
  567. # if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  568. int result = error_wrapper(::ioctlsocket(s, SIOCATMARK, &value), ec);
  569. # else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  570. int result = error_wrapper(::ioctl(s, SIOCATMARK, &value), ec);
  571. # endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  572. if (result == 0)
  573. ec = boost::system::error_code();
  574. # if defined(ENOTTY)
  575. if (ec.value() == ENOTTY)
  576. ec = boost::asio::error::not_socket;
  577. # endif // defined(ENOTTY)
  578. #else // defined(SIOCATMARK)
  579. int value = error_wrapper(::sockatmark(s), ec);
  580. if (value != -1)
  581. ec = boost::system::error_code();
  582. #endif // defined(SIOCATMARK)
  583. return ec ? false : value != 0;
  584. }
  585. size_t available(socket_type s, boost::system::error_code& ec)
  586. {
  587. if (s == invalid_socket)
  588. {
  589. ec = boost::asio::error::bad_descriptor;
  590. return 0;
  591. }
  592. ioctl_arg_type value = 0;
  593. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  594. int result = error_wrapper(::ioctlsocket(s, FIONREAD, &value), ec);
  595. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  596. int result = error_wrapper(::ioctl(s, FIONREAD, &value), ec);
  597. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  598. if (result == 0)
  599. ec = boost::system::error_code();
  600. #if defined(ENOTTY)
  601. if (ec.value() == ENOTTY)
  602. ec = boost::asio::error::not_socket;
  603. #endif // defined(ENOTTY)
  604. return ec ? static_cast<size_t>(0) : static_cast<size_t>(value);
  605. }
  606. int listen(socket_type s, int backlog, boost::system::error_code& ec)
  607. {
  608. if (s == invalid_socket)
  609. {
  610. ec = boost::asio::error::bad_descriptor;
  611. return socket_error_retval;
  612. }
  613. clear_last_error();
  614. int result = error_wrapper(::listen(s, backlog), ec);
  615. if (result == 0)
  616. ec = boost::system::error_code();
  617. return result;
  618. }
  619. inline void init_buf_iov_base(void*& base, void* addr)
  620. {
  621. base = addr;
  622. }
  623. template <typename T>
  624. inline void init_buf_iov_base(T& base, void* addr)
  625. {
  626. base = static_cast<T>(addr);
  627. }
  628. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  629. typedef WSABUF buf;
  630. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  631. typedef iovec buf;
  632. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  633. void init_buf(buf& b, void* data, size_t size)
  634. {
  635. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  636. b.buf = static_cast<char*>(data);
  637. b.len = static_cast<u_long>(size);
  638. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  639. init_buf_iov_base(b.iov_base, data);
  640. b.iov_len = size;
  641. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  642. }
  643. void init_buf(buf& b, const void* data, size_t size)
  644. {
  645. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  646. b.buf = static_cast<char*>(const_cast<void*>(data));
  647. b.len = static_cast<u_long>(size);
  648. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  649. init_buf_iov_base(b.iov_base, const_cast<void*>(data));
  650. b.iov_len = size;
  651. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  652. }
  653. inline void init_msghdr_msg_name(void*& name, socket_addr_type* addr)
  654. {
  655. name = addr;
  656. }
  657. inline void init_msghdr_msg_name(void*& name, const socket_addr_type* addr)
  658. {
  659. name = const_cast<socket_addr_type*>(addr);
  660. }
  661. template <typename T>
  662. inline void init_msghdr_msg_name(T& name, socket_addr_type* addr)
  663. {
  664. name = reinterpret_cast<T>(addr);
  665. }
  666. template <typename T>
  667. inline void init_msghdr_msg_name(T& name, const socket_addr_type* addr)
  668. {
  669. name = reinterpret_cast<T>(const_cast<socket_addr_type*>(addr));
  670. }
  671. signed_size_type recv(socket_type s, buf* bufs, size_t count,
  672. int flags, boost::system::error_code& ec)
  673. {
  674. clear_last_error();
  675. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  676. // Receive some data.
  677. DWORD recv_buf_count = static_cast<DWORD>(count);
  678. DWORD bytes_transferred = 0;
  679. DWORD recv_flags = flags;
  680. int result = error_wrapper(::WSARecv(s, bufs,
  681. recv_buf_count, &bytes_transferred, &recv_flags, 0, 0), ec);
  682. if (ec.value() == ERROR_NETNAME_DELETED)
  683. ec = boost::asio::error::connection_reset;
  684. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  685. ec = boost::asio::error::connection_refused;
  686. else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
  687. 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(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1568. if (!readfds && !writefds && !exceptfds && timeout)
  1569. {
  1570. DWORD milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  1571. if (milliseconds == 0)
  1572. milliseconds = 1; // Force context switch.
  1573. ::Sleep(milliseconds);
  1574. ec = boost::system::error_code();
  1575. return 0;
  1576. }
  1577. // The select() call allows timeout values measured in microseconds, but the
  1578. // system clock (as wrapped by boost::posix_time::microsec_clock) typically
  1579. // has a resolution of 10 milliseconds. This can lead to a spinning select
  1580. // reactor, meaning increased CPU usage, when waiting for the earliest
  1581. // scheduled timeout if it's less than 10 milliseconds away. To avoid a tight
  1582. // spin we'll use a minimum timeout of 1 millisecond.
  1583. if (timeout && timeout->tv_sec == 0
  1584. && timeout->tv_usec > 0 && timeout->tv_usec < 1000)
  1585. timeout->tv_usec = 1000;
  1586. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1587. #if defined(__hpux) && defined(__SELECT)
  1588. timespec ts;
  1589. ts.tv_sec = timeout ? timeout->tv_sec : 0;
  1590. ts.tv_nsec = timeout ? timeout->tv_usec * 1000 : 0;
  1591. return error_wrapper(::pselect(nfds, readfds,
  1592. writefds, exceptfds, timeout ? &ts : 0, 0), ec);
  1593. #else
  1594. int result = error_wrapper(::select(nfds, readfds,
  1595. writefds, exceptfds, timeout), ec);
  1596. if (result >= 0)
  1597. ec = boost::system::error_code();
  1598. return result;
  1599. #endif
  1600. }
  1601. int poll_read(socket_type s, state_type state,
  1602. int msec, boost::system::error_code& ec)
  1603. {
  1604. if (s == invalid_socket)
  1605. {
  1606. ec = boost::asio::error::bad_descriptor;
  1607. return socket_error_retval;
  1608. }
  1609. #if defined(BOOST_ASIO_WINDOWS) \
  1610. || defined(__CYGWIN__) \
  1611. || defined(__SYMBIAN32__)
  1612. fd_set fds;
  1613. FD_ZERO(&fds);
  1614. FD_SET(s, &fds);
  1615. timeval timeout_obj;
  1616. timeval* timeout;
  1617. if (state & user_set_non_blocking)
  1618. {
  1619. timeout_obj.tv_sec = 0;
  1620. timeout_obj.tv_usec = 0;
  1621. timeout = &timeout_obj;
  1622. }
  1623. else if (msec >= 0)
  1624. {
  1625. timeout_obj.tv_sec = msec / 1000;
  1626. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1627. timeout = &timeout_obj;
  1628. }
  1629. else
  1630. timeout = 0;
  1631. clear_last_error();
  1632. int result = error_wrapper(::select(s + 1, &fds, 0, 0, timeout), ec);
  1633. #else // defined(BOOST_ASIO_WINDOWS)
  1634. // || defined(__CYGWIN__)
  1635. // || defined(__SYMBIAN32__)
  1636. pollfd fds;
  1637. fds.fd = s;
  1638. fds.events = POLLIN;
  1639. fds.revents = 0;
  1640. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1641. clear_last_error();
  1642. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1643. #endif // defined(BOOST_ASIO_WINDOWS)
  1644. // || defined(__CYGWIN__)
  1645. // || defined(__SYMBIAN32__)
  1646. if (result == 0)
  1647. ec = (state & user_set_non_blocking)
  1648. ? boost::asio::error::would_block : boost::system::error_code();
  1649. else if (result > 0)
  1650. ec = boost::system::error_code();
  1651. return result;
  1652. }
  1653. int poll_write(socket_type s, state_type state,
  1654. int msec, boost::system::error_code& ec)
  1655. {
  1656. if (s == invalid_socket)
  1657. {
  1658. ec = boost::asio::error::bad_descriptor;
  1659. return socket_error_retval;
  1660. }
  1661. #if defined(BOOST_ASIO_WINDOWS) \
  1662. || defined(__CYGWIN__) \
  1663. || defined(__SYMBIAN32__)
  1664. fd_set fds;
  1665. FD_ZERO(&fds);
  1666. FD_SET(s, &fds);
  1667. timeval timeout_obj;
  1668. timeval* timeout;
  1669. if (state & user_set_non_blocking)
  1670. {
  1671. timeout_obj.tv_sec = 0;
  1672. timeout_obj.tv_usec = 0;
  1673. timeout = &timeout_obj;
  1674. }
  1675. else if (msec >= 0)
  1676. {
  1677. timeout_obj.tv_sec = msec / 1000;
  1678. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1679. timeout = &timeout_obj;
  1680. }
  1681. else
  1682. timeout = 0;
  1683. clear_last_error();
  1684. int result = error_wrapper(::select(s + 1, 0, &fds, 0, timeout), ec);
  1685. #else // defined(BOOST_ASIO_WINDOWS)
  1686. // || defined(__CYGWIN__)
  1687. // || defined(__SYMBIAN32__)
  1688. pollfd fds;
  1689. fds.fd = s;
  1690. fds.events = POLLOUT;
  1691. fds.revents = 0;
  1692. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1693. clear_last_error();
  1694. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1695. #endif // defined(BOOST_ASIO_WINDOWS)
  1696. // || defined(__CYGWIN__)
  1697. // || defined(__SYMBIAN32__)
  1698. if (result == 0)
  1699. ec = (state & user_set_non_blocking)
  1700. ? boost::asio::error::would_block : boost::system::error_code();
  1701. else if (result > 0)
  1702. ec = boost::system::error_code();
  1703. return result;
  1704. }
  1705. int poll_error(socket_type s, state_type state,
  1706. int msec, boost::system::error_code& ec)
  1707. {
  1708. if (s == invalid_socket)
  1709. {
  1710. ec = boost::asio::error::bad_descriptor;
  1711. return socket_error_retval;
  1712. }
  1713. #if defined(BOOST_ASIO_WINDOWS) \
  1714. || defined(__CYGWIN__) \
  1715. || defined(__SYMBIAN32__)
  1716. fd_set fds;
  1717. FD_ZERO(&fds);
  1718. FD_SET(s, &fds);
  1719. timeval timeout_obj;
  1720. timeval* timeout;
  1721. if (state & user_set_non_blocking)
  1722. {
  1723. timeout_obj.tv_sec = 0;
  1724. timeout_obj.tv_usec = 0;
  1725. timeout = &timeout_obj;
  1726. }
  1727. else if (msec >= 0)
  1728. {
  1729. timeout_obj.tv_sec = msec / 1000;
  1730. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1731. timeout = &timeout_obj;
  1732. }
  1733. else
  1734. timeout = 0;
  1735. clear_last_error();
  1736. int result = error_wrapper(::select(s + 1, 0, 0, &fds, timeout), ec);
  1737. #else // defined(BOOST_ASIO_WINDOWS)
  1738. // || defined(__CYGWIN__)
  1739. // || defined(__SYMBIAN32__)
  1740. pollfd fds;
  1741. fds.fd = s;
  1742. fds.events = POLLPRI | POLLERR | POLLHUP;
  1743. fds.revents = 0;
  1744. int timeout = (state & user_set_non_blocking) ? 0 : msec;
  1745. clear_last_error();
  1746. int result = error_wrapper(::poll(&fds, 1, timeout), ec);
  1747. #endif // defined(BOOST_ASIO_WINDOWS)
  1748. // || defined(__CYGWIN__)
  1749. // || defined(__SYMBIAN32__)
  1750. if (result == 0)
  1751. ec = (state & user_set_non_blocking)
  1752. ? boost::asio::error::would_block : boost::system::error_code();
  1753. else if (result > 0)
  1754. ec = boost::system::error_code();
  1755. return result;
  1756. }
  1757. int poll_connect(socket_type s, int msec, boost::system::error_code& ec)
  1758. {
  1759. if (s == invalid_socket)
  1760. {
  1761. ec = boost::asio::error::bad_descriptor;
  1762. return socket_error_retval;
  1763. }
  1764. #if defined(BOOST_ASIO_WINDOWS) \
  1765. || defined(__CYGWIN__) \
  1766. || defined(__SYMBIAN32__)
  1767. fd_set write_fds;
  1768. FD_ZERO(&write_fds);
  1769. FD_SET(s, &write_fds);
  1770. fd_set except_fds;
  1771. FD_ZERO(&except_fds);
  1772. FD_SET(s, &except_fds);
  1773. timeval timeout_obj;
  1774. timeval* timeout;
  1775. if (msec >= 0)
  1776. {
  1777. timeout_obj.tv_sec = msec / 1000;
  1778. timeout_obj.tv_usec = (msec % 1000) * 1000;
  1779. timeout = &timeout_obj;
  1780. }
  1781. else
  1782. timeout = 0;
  1783. clear_last_error();
  1784. int result = error_wrapper(::select(
  1785. s + 1, 0, &write_fds, &except_fds, timeout), ec);
  1786. if (result >= 0)
  1787. ec = boost::system::error_code();
  1788. return result;
  1789. #else // defined(BOOST_ASIO_WINDOWS)
  1790. // || defined(__CYGWIN__)
  1791. // || defined(__SYMBIAN32__)
  1792. pollfd fds;
  1793. fds.fd = s;
  1794. fds.events = POLLOUT;
  1795. fds.revents = 0;
  1796. clear_last_error();
  1797. int result = error_wrapper(::poll(&fds, 1, msec), ec);
  1798. if (result >= 0)
  1799. ec = boost::system::error_code();
  1800. return result;
  1801. #endif // defined(BOOST_ASIO_WINDOWS)
  1802. // || defined(__CYGWIN__)
  1803. // || defined(__SYMBIAN32__)
  1804. }
  1805. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1806. const char* inet_ntop(int af, const void* src, char* dest, size_t length,
  1807. unsigned long scope_id, boost::system::error_code& ec)
  1808. {
  1809. clear_last_error();
  1810. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1811. using namespace std; // For sprintf.
  1812. const unsigned char* bytes = static_cast<const unsigned char*>(src);
  1813. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1814. {
  1815. sprintf_s(dest, length, "%u.%u.%u.%u",
  1816. bytes[0], bytes[1], bytes[2], bytes[3]);
  1817. return dest;
  1818. }
  1819. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1820. {
  1821. size_t n = 0, b = 0, z = 0;
  1822. while (n < length && b < 16)
  1823. {
  1824. if (bytes[b] == 0 && bytes[b + 1] == 0 && z == 0)
  1825. {
  1826. do b += 2; while (b < 16 && bytes[b] == 0 && bytes[b + 1] == 0);
  1827. n += sprintf_s(dest + n, length - n, ":%s", b < 16 ? "" : ":"), ++z;
  1828. }
  1829. else
  1830. {
  1831. n += sprintf_s(dest + n, length - n, "%s%x", b ? ":" : "",
  1832. (static_cast<u_long_type>(bytes[b]) << 8) | bytes[b + 1]);
  1833. b += 2;
  1834. }
  1835. }
  1836. if (scope_id)
  1837. n += sprintf_s(dest + n, length - n, "%%%lu", scope_id);
  1838. return dest;
  1839. }
  1840. else
  1841. {
  1842. ec = boost::asio::error::address_family_not_supported;
  1843. return 0;
  1844. }
  1845. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1846. using namespace std; // For memcpy.
  1847. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  1848. {
  1849. ec = boost::asio::error::address_family_not_supported;
  1850. return 0;
  1851. }
  1852. union
  1853. {
  1854. socket_addr_type base;
  1855. sockaddr_storage_type storage;
  1856. sockaddr_in4_type v4;
  1857. sockaddr_in6_type v6;
  1858. } address;
  1859. DWORD address_length;
  1860. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1861. {
  1862. address_length = sizeof(sockaddr_in4_type);
  1863. address.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  1864. address.v4.sin_port = 0;
  1865. memcpy(&address.v4.sin_addr, src, sizeof(in4_addr_type));
  1866. }
  1867. else // AF_INET6
  1868. {
  1869. address_length = sizeof(sockaddr_in6_type);
  1870. address.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  1871. address.v6.sin6_port = 0;
  1872. address.v6.sin6_flowinfo = 0;
  1873. address.v6.sin6_scope_id = scope_id;
  1874. memcpy(&address.v6.sin6_addr, src, sizeof(in6_addr_type));
  1875. }
  1876. DWORD string_length = static_cast<DWORD>(length);
  1877. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  1878. LPWSTR string_buffer = (LPWSTR)_alloca(length * sizeof(WCHAR));
  1879. int result = error_wrapper(::WSAAddressToStringW(&address.base,
  1880. address_length, 0, string_buffer, &string_length), ec);
  1881. ::WideCharToMultiByte(CP_ACP, 0, string_buffer, -1,
  1882. dest, static_cast<int>(length), 0, 0);
  1883. #else
  1884. int result = error_wrapper(::WSAAddressToStringA(
  1885. &address.base, address_length, 0, dest, &string_length), ec);
  1886. #endif
  1887. // Windows may set error code on success.
  1888. if (result != socket_error_retval)
  1889. ec = boost::system::error_code();
  1890. // Windows may not set an error code on failure.
  1891. else if (result == socket_error_retval && !ec)
  1892. ec = boost::asio::error::invalid_argument;
  1893. return result == socket_error_retval ? 0 : dest;
  1894. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1895. const char* result = error_wrapper(::inet_ntop(
  1896. af, src, dest, static_cast<int>(length)), ec);
  1897. if (result == 0 && !ec)
  1898. ec = boost::asio::error::invalid_argument;
  1899. if (result != 0 && af == BOOST_ASIO_OS_DEF(AF_INET6) && scope_id != 0)
  1900. {
  1901. using namespace std; // For strcat and sprintf.
  1902. char if_name[(IF_NAMESIZE > 21 ? IF_NAMESIZE : 21) + 1] = "%";
  1903. const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
  1904. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  1905. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  1906. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  1907. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  1908. if ((!is_link_local && !is_multicast_link_local)
  1909. || if_indextoname(static_cast<unsigned>(scope_id), if_name + 1) == 0)
  1910. sprintf(if_name + 1, "%lu", scope_id);
  1911. strcat(dest, if_name);
  1912. }
  1913. return result;
  1914. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  1915. }
  1916. int inet_pton(int af, const char* src, void* dest,
  1917. unsigned long* scope_id, boost::system::error_code& ec)
  1918. {
  1919. clear_last_error();
  1920. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1921. using namespace std; // For sscanf.
  1922. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1923. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  1924. {
  1925. unsigned int b0, b1, b2, b3;
  1926. if (sscanf_s(src, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) != 4)
  1927. {
  1928. ec = boost::asio::error::invalid_argument;
  1929. return -1;
  1930. }
  1931. if (b0 > 255 || b1 > 255 || b2 > 255 || b3 > 255)
  1932. {
  1933. ec = boost::asio::error::invalid_argument;
  1934. return -1;
  1935. }
  1936. bytes[0] = static_cast<unsigned char>(b0);
  1937. bytes[1] = static_cast<unsigned char>(b1);
  1938. bytes[2] = static_cast<unsigned char>(b2);
  1939. bytes[3] = static_cast<unsigned char>(b3);
  1940. ec = boost::system::error_code();
  1941. return 1;
  1942. }
  1943. else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
  1944. {
  1945. unsigned char* bytes = static_cast<unsigned char*>(dest);
  1946. std::memset(bytes, 0, 16);
  1947. unsigned char back_bytes[16] = { 0 };
  1948. int num_front_bytes = 0, num_back_bytes = 0;
  1949. const char* p = src;
  1950. enum { fword, fcolon, bword, scope, done } state = fword;
  1951. unsigned long current_word = 0;
  1952. while (state != done)
  1953. {
  1954. if (current_word > 0xFFFF)
  1955. {
  1956. ec = boost::asio::error::invalid_argument;
  1957. return -1;
  1958. }
  1959. switch (state)
  1960. {
  1961. case fword:
  1962. if (*p >= '0' && *p <= '9')
  1963. current_word = current_word * 16 + *p++ - '0';
  1964. else if (*p >= 'a' && *p <= 'f')
  1965. current_word = current_word * 16 + *p++ - 'a' + 10;
  1966. else if (*p >= 'A' && *p <= 'F')
  1967. current_word = current_word * 16 + *p++ - 'A' + 10;
  1968. else
  1969. {
  1970. if (num_front_bytes == 16)
  1971. {
  1972. ec = boost::asio::error::invalid_argument;
  1973. return -1;
  1974. }
  1975. bytes[num_front_bytes++] = (current_word >> 8) & 0xFF;
  1976. bytes[num_front_bytes++] = current_word & 0xFF;
  1977. current_word = 0;
  1978. if (*p == ':')
  1979. state = fcolon, ++p;
  1980. else if (*p == '%')
  1981. state = scope, ++p;
  1982. else if (*p == 0)
  1983. state = done;
  1984. else
  1985. {
  1986. ec = boost::asio::error::invalid_argument;
  1987. return -1;
  1988. }
  1989. }
  1990. break;
  1991. case fcolon:
  1992. if (*p == ':')
  1993. state = bword, ++p;
  1994. else
  1995. state = fword;
  1996. break;
  1997. case bword:
  1998. if (*p >= '0' && *p <= '9')
  1999. current_word = current_word * 16 + *p++ - '0';
  2000. else if (*p >= 'a' && *p <= 'f')
  2001. current_word = current_word * 16 + *p++ - 'a' + 10;
  2002. else if (*p >= 'A' && *p <= 'F')
  2003. current_word = current_word * 16 + *p++ - 'A' + 10;
  2004. else
  2005. {
  2006. if (num_front_bytes + num_back_bytes == 16)
  2007. {
  2008. ec = boost::asio::error::invalid_argument;
  2009. return -1;
  2010. }
  2011. back_bytes[num_back_bytes++] = (current_word >> 8) & 0xFF;
  2012. back_bytes[num_back_bytes++] = current_word & 0xFF;
  2013. current_word = 0;
  2014. if (*p == ':')
  2015. state = bword, ++p;
  2016. else if (*p == '%')
  2017. state = scope, ++p;
  2018. else if (*p == 0)
  2019. state = done;
  2020. else
  2021. {
  2022. ec = boost::asio::error::invalid_argument;
  2023. return -1;
  2024. }
  2025. }
  2026. break;
  2027. case scope:
  2028. if (*p >= '0' && *p <= '9')
  2029. current_word = current_word * 10 + *p++ - '0';
  2030. else if (*p == 0)
  2031. *scope_id = current_word, state = done;
  2032. else
  2033. {
  2034. ec = boost::asio::error::invalid_argument;
  2035. return -1;
  2036. }
  2037. break;
  2038. default:
  2039. break;
  2040. }
  2041. }
  2042. for (int i = 0; i < num_back_bytes; ++i)
  2043. bytes[16 - num_back_bytes + i] = back_bytes[i];
  2044. ec = boost::system::error_code();
  2045. return 1;
  2046. }
  2047. else
  2048. {
  2049. ec = boost::asio::error::address_family_not_supported;
  2050. return -1;
  2051. }
  2052. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2053. using namespace std; // For memcpy and strcmp.
  2054. if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
  2055. {
  2056. ec = boost::asio::error::address_family_not_supported;
  2057. return -1;
  2058. }
  2059. union
  2060. {
  2061. socket_addr_type base;
  2062. sockaddr_storage_type storage;
  2063. sockaddr_in4_type v4;
  2064. sockaddr_in6_type v6;
  2065. } address;
  2066. int address_length = sizeof(sockaddr_storage_type);
  2067. #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
  2068. int num_wide_chars = static_cast<int>(strlen(src)) + 1;
  2069. LPWSTR wide_buffer = (LPWSTR)_alloca(num_wide_chars * sizeof(WCHAR));
  2070. ::MultiByteToWideChar(CP_ACP, 0, src, -1, wide_buffer, num_wide_chars);
  2071. int result = error_wrapper(::WSAStringToAddressW(
  2072. wide_buffer, af, 0, &address.base, &address_length), ec);
  2073. #else
  2074. int result = error_wrapper(::WSAStringToAddressA(
  2075. const_cast<char*>(src), af, 0, &address.base, &address_length), ec);
  2076. #endif
  2077. if (af == BOOST_ASIO_OS_DEF(AF_INET))
  2078. {
  2079. if (result != socket_error_retval)
  2080. {
  2081. memcpy(dest, &address.v4.sin_addr, sizeof(in4_addr_type));
  2082. ec = boost::system::error_code();
  2083. }
  2084. else if (strcmp(src, "255.255.255.255") == 0)
  2085. {
  2086. static_cast<in4_addr_type*>(dest)->s_addr = INADDR_NONE;
  2087. ec = boost::system::error_code();
  2088. }
  2089. }
  2090. else // AF_INET6
  2091. {
  2092. if (result != socket_error_retval)
  2093. {
  2094. memcpy(dest, &address.v6.sin6_addr, sizeof(in6_addr_type));
  2095. if (scope_id)
  2096. *scope_id = address.v6.sin6_scope_id;
  2097. ec = boost::system::error_code();
  2098. }
  2099. }
  2100. // Windows may not set an error code on failure.
  2101. if (result == socket_error_retval && !ec)
  2102. ec = boost::asio::error::invalid_argument;
  2103. if (result != socket_error_retval)
  2104. ec = boost::system::error_code();
  2105. return result == socket_error_retval ? -1 : 1;
  2106. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2107. using namespace std; // For strchr, memcpy and atoi.
  2108. // On some platforms, inet_pton fails if an address string contains a scope
  2109. // id. Detect and remove the scope id before passing the string to inet_pton.
  2110. const bool is_v6 = (af == BOOST_ASIO_OS_DEF(AF_INET6));
  2111. const char* if_name = is_v6 ? strchr(src, '%') : 0;
  2112. char src_buf[max_addr_v6_str_len + 1];
  2113. const char* src_ptr = src;
  2114. if (if_name != 0)
  2115. {
  2116. if (if_name - src > max_addr_v6_str_len)
  2117. {
  2118. ec = boost::asio::error::invalid_argument;
  2119. return 0;
  2120. }
  2121. memcpy(src_buf, src, if_name - src);
  2122. src_buf[if_name - src] = 0;
  2123. src_ptr = src_buf;
  2124. }
  2125. int result = error_wrapper(::inet_pton(af, src_ptr, dest), ec);
  2126. if (result <= 0 && !ec)
  2127. ec = boost::asio::error::invalid_argument;
  2128. if (result > 0 && is_v6 && scope_id)
  2129. {
  2130. using namespace std; // For strchr and atoi.
  2131. *scope_id = 0;
  2132. if (if_name != 0)
  2133. {
  2134. in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
  2135. bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
  2136. && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
  2137. bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
  2138. && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
  2139. if (is_link_local || is_multicast_link_local)
  2140. *scope_id = if_nametoindex(if_name + 1);
  2141. if (*scope_id == 0)
  2142. *scope_id = atoi(if_name + 1);
  2143. }
  2144. }
  2145. return result;
  2146. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2147. }
  2148. int gethostname(char* name, int namelen, boost::system::error_code& ec)
  2149. {
  2150. clear_last_error();
  2151. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2152. try
  2153. {
  2154. using namespace Windows::Foundation::Collections;
  2155. using namespace Windows::Networking;
  2156. using namespace Windows::Networking::Connectivity;
  2157. IVectorView<HostName^>^ hostnames = NetworkInformation::GetHostNames();
  2158. for (unsigned i = 0; i < hostnames->Size; ++i)
  2159. {
  2160. HostName^ hostname = hostnames->GetAt(i);
  2161. if (hostname->Type == HostNameType::DomainName)
  2162. {
  2163. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  2164. std::string raw_name = converter.to_bytes(hostname->RawName->Data());
  2165. if (namelen > 0 && raw_name.size() < static_cast<std::size_t>(namelen))
  2166. {
  2167. strcpy_s(name, namelen, raw_name.c_str());
  2168. return 0;
  2169. }
  2170. }
  2171. }
  2172. return -1;
  2173. }
  2174. catch (Platform::Exception^ e)
  2175. {
  2176. ec = boost::system::error_code(e->HResult,
  2177. boost::system::system_category());
  2178. return -1;
  2179. }
  2180. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2181. int result = error_wrapper(::gethostname(name, namelen), ec);
  2182. # if defined(BOOST_ASIO_WINDOWS)
  2183. if (result == 0)
  2184. ec = boost::system::error_code();
  2185. # endif // defined(BOOST_ASIO_WINDOWS)
  2186. return result;
  2187. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2188. }
  2189. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  2190. #if !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2191. // The following functions are only needed for emulation of getaddrinfo and
  2192. // getnameinfo.
  2193. inline boost::system::error_code translate_netdb_error(int error)
  2194. {
  2195. switch (error)
  2196. {
  2197. case 0:
  2198. return boost::system::error_code();
  2199. case HOST_NOT_FOUND:
  2200. return boost::asio::error::host_not_found;
  2201. case TRY_AGAIN:
  2202. return boost::asio::error::host_not_found_try_again;
  2203. case NO_RECOVERY:
  2204. return boost::asio::error::no_recovery;
  2205. case NO_DATA:
  2206. return boost::asio::error::no_data;
  2207. default:
  2208. BOOST_ASIO_ASSERT(false);
  2209. return boost::asio::error::invalid_argument;
  2210. }
  2211. }
  2212. inline hostent* gethostbyaddr(const char* addr, int length, int af,
  2213. hostent* result, char* buffer, int buflength, boost::system::error_code& ec)
  2214. {
  2215. clear_last_error();
  2216. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2217. (void)(buffer);
  2218. (void)(buflength);
  2219. hostent* retval = error_wrapper(::gethostbyaddr(addr, length, af), ec);
  2220. if (!retval)
  2221. return 0;
  2222. ec = boost::system::error_code();
  2223. *result = *retval;
  2224. return retval;
  2225. #elif defined(__sun) || defined(__QNX__)
  2226. int error = 0;
  2227. hostent* retval = error_wrapper(::gethostbyaddr_r(addr, length, af, result,
  2228. buffer, buflength, &error), ec);
  2229. if (error)
  2230. ec = translate_netdb_error(error);
  2231. return retval;
  2232. #elif defined(__MACH__) && defined(__APPLE__)
  2233. (void)(buffer);
  2234. (void)(buflength);
  2235. int error = 0;
  2236. hostent* retval = error_wrapper(::getipnodebyaddr(
  2237. addr, length, af, &error), ec);
  2238. if (error)
  2239. ec = translate_netdb_error(error);
  2240. if (!retval)
  2241. return 0;
  2242. *result = *retval;
  2243. return retval;
  2244. #else
  2245. hostent* retval = 0;
  2246. int error = 0;
  2247. error_wrapper(::gethostbyaddr_r(addr, length, af, result, buffer,
  2248. buflength, &retval, &error), ec);
  2249. if (error)
  2250. ec = translate_netdb_error(error);
  2251. return retval;
  2252. #endif
  2253. }
  2254. inline hostent* gethostbyname(const char* name, int af, struct hostent* result,
  2255. char* buffer, int buflength, int ai_flags, boost::system::error_code& ec)
  2256. {
  2257. clear_last_error();
  2258. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  2259. (void)(buffer);
  2260. (void)(buflength);
  2261. (void)(ai_flags);
  2262. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2263. {
  2264. ec = boost::asio::error::address_family_not_supported;
  2265. return 0;
  2266. }
  2267. hostent* retval = error_wrapper(::gethostbyname(name), ec);
  2268. if (!retval)
  2269. return 0;
  2270. ec = boost::system::error_code();
  2271. *result = *retval;
  2272. return result;
  2273. #elif defined(__sun) || defined(__QNX__)
  2274. (void)(ai_flags);
  2275. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2276. {
  2277. ec = boost::asio::error::address_family_not_supported;
  2278. return 0;
  2279. }
  2280. int error = 0;
  2281. hostent* retval = error_wrapper(::gethostbyname_r(name, result, buffer,
  2282. buflength, &error), ec);
  2283. if (error)
  2284. ec = translate_netdb_error(error);
  2285. return retval;
  2286. #elif defined(__MACH__) && defined(__APPLE__)
  2287. (void)(buffer);
  2288. (void)(buflength);
  2289. int error = 0;
  2290. hostent* retval = error_wrapper(::getipnodebyname(
  2291. name, af, ai_flags, &error), ec);
  2292. if (error)
  2293. ec = translate_netdb_error(error);
  2294. if (!retval)
  2295. return 0;
  2296. *result = *retval;
  2297. return retval;
  2298. #else
  2299. (void)(ai_flags);
  2300. if (af != BOOST_ASIO_OS_DEF(AF_INET))
  2301. {
  2302. ec = boost::asio::error::address_family_not_supported;
  2303. return 0;
  2304. }
  2305. hostent* retval = 0;
  2306. int error = 0;
  2307. error_wrapper(::gethostbyname_r(name, result,
  2308. buffer, buflength, &retval, &error), ec);
  2309. if (error)
  2310. ec = translate_netdb_error(error);
  2311. return retval;
  2312. #endif
  2313. }
  2314. inline void freehostent(hostent* h)
  2315. {
  2316. #if defined(__MACH__) && defined(__APPLE__)
  2317. if (h)
  2318. ::freehostent(h);
  2319. #else
  2320. (void)(h);
  2321. #endif
  2322. }
  2323. // Emulation of getaddrinfo based on implementation in:
  2324. // Stevens, W. R., UNIX Network Programming Vol. 1, 2nd Ed., Prentice-Hall 1998.
  2325. struct gai_search
  2326. {
  2327. const char* host;
  2328. int family;
  2329. };
  2330. inline int gai_nsearch(const char* host,
  2331. const addrinfo_type* hints, gai_search (&search)[2])
  2332. {
  2333. int search_count = 0;
  2334. if (host == 0 || host[0] == '\0')
  2335. {
  2336. if (hints->ai_flags & AI_PASSIVE)
  2337. {
  2338. // No host and AI_PASSIVE implies wildcard bind.
  2339. switch (hints->ai_family)
  2340. {
  2341. case BOOST_ASIO_OS_DEF(AF_INET):
  2342. search[search_count].host = "0.0.0.0";
  2343. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2344. ++search_count;
  2345. break;
  2346. case BOOST_ASIO_OS_DEF(AF_INET6):
  2347. search[search_count].host = "0::0";
  2348. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2349. ++search_count;
  2350. break;
  2351. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2352. search[search_count].host = "0::0";
  2353. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2354. ++search_count;
  2355. search[search_count].host = "0.0.0.0";
  2356. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2357. ++search_count;
  2358. break;
  2359. default:
  2360. break;
  2361. }
  2362. }
  2363. else
  2364. {
  2365. // No host and not AI_PASSIVE means connect to local host.
  2366. switch (hints->ai_family)
  2367. {
  2368. case BOOST_ASIO_OS_DEF(AF_INET):
  2369. search[search_count].host = "localhost";
  2370. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2371. ++search_count;
  2372. break;
  2373. case BOOST_ASIO_OS_DEF(AF_INET6):
  2374. search[search_count].host = "localhost";
  2375. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2376. ++search_count;
  2377. break;
  2378. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2379. search[search_count].host = "localhost";
  2380. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2381. ++search_count;
  2382. search[search_count].host = "localhost";
  2383. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2384. ++search_count;
  2385. break;
  2386. default:
  2387. break;
  2388. }
  2389. }
  2390. }
  2391. else
  2392. {
  2393. // Host is specified.
  2394. switch (hints->ai_family)
  2395. {
  2396. case BOOST_ASIO_OS_DEF(AF_INET):
  2397. search[search_count].host = host;
  2398. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2399. ++search_count;
  2400. break;
  2401. case BOOST_ASIO_OS_DEF(AF_INET6):
  2402. search[search_count].host = host;
  2403. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2404. ++search_count;
  2405. break;
  2406. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2407. search[search_count].host = host;
  2408. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
  2409. ++search_count;
  2410. search[search_count].host = host;
  2411. search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
  2412. ++search_count;
  2413. break;
  2414. default:
  2415. break;
  2416. }
  2417. }
  2418. return search_count;
  2419. }
  2420. template <typename T>
  2421. inline T* gai_alloc(std::size_t size = sizeof(T))
  2422. {
  2423. using namespace std;
  2424. T* p = static_cast<T*>(::operator new(size, std::nothrow));
  2425. if (p)
  2426. memset(p, 0, size);
  2427. return p;
  2428. }
  2429. inline void gai_free(void* p)
  2430. {
  2431. ::operator delete(p);
  2432. }
  2433. inline void gai_strcpy(char* target, const char* source, std::size_t max_size)
  2434. {
  2435. using namespace std;
  2436. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2437. strcpy_s(target, max_size, source);
  2438. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2439. *target = 0;
  2440. if (max_size > 0)
  2441. strncat(target, source, max_size - 1);
  2442. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2443. }
  2444. enum { gai_clone_flag = 1 << 30 };
  2445. inline int gai_aistruct(addrinfo_type*** next, const addrinfo_type* hints,
  2446. const void* addr, int family)
  2447. {
  2448. using namespace std;
  2449. addrinfo_type* ai = gai_alloc<addrinfo_type>();
  2450. if (ai == 0)
  2451. return EAI_MEMORY;
  2452. ai->ai_next = 0;
  2453. **next = ai;
  2454. *next = &ai->ai_next;
  2455. ai->ai_canonname = 0;
  2456. ai->ai_socktype = hints->ai_socktype;
  2457. if (ai->ai_socktype == 0)
  2458. ai->ai_flags |= gai_clone_flag;
  2459. ai->ai_protocol = hints->ai_protocol;
  2460. ai->ai_family = family;
  2461. switch (ai->ai_family)
  2462. {
  2463. case BOOST_ASIO_OS_DEF(AF_INET):
  2464. {
  2465. sockaddr_in4_type* sinptr = gai_alloc<sockaddr_in4_type>();
  2466. if (sinptr == 0)
  2467. return EAI_MEMORY;
  2468. sinptr->sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  2469. memcpy(&sinptr->sin_addr, addr, sizeof(in4_addr_type));
  2470. ai->ai_addr = reinterpret_cast<sockaddr*>(sinptr);
  2471. ai->ai_addrlen = sizeof(sockaddr_in4_type);
  2472. break;
  2473. }
  2474. case BOOST_ASIO_OS_DEF(AF_INET6):
  2475. {
  2476. sockaddr_in6_type* sin6ptr = gai_alloc<sockaddr_in6_type>();
  2477. if (sin6ptr == 0)
  2478. return EAI_MEMORY;
  2479. sin6ptr->sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  2480. memcpy(&sin6ptr->sin6_addr, addr, sizeof(in6_addr_type));
  2481. ai->ai_addr = reinterpret_cast<sockaddr*>(sin6ptr);
  2482. ai->ai_addrlen = sizeof(sockaddr_in6_type);
  2483. break;
  2484. }
  2485. default:
  2486. break;
  2487. }
  2488. return 0;
  2489. }
  2490. inline addrinfo_type* gai_clone(addrinfo_type* ai)
  2491. {
  2492. using namespace std;
  2493. addrinfo_type* new_ai = gai_alloc<addrinfo_type>();
  2494. if (new_ai == 0)
  2495. return new_ai;
  2496. new_ai->ai_next = ai->ai_next;
  2497. ai->ai_next = new_ai;
  2498. new_ai->ai_flags = 0;
  2499. new_ai->ai_family = ai->ai_family;
  2500. new_ai->ai_socktype = ai->ai_socktype;
  2501. new_ai->ai_protocol = ai->ai_protocol;
  2502. new_ai->ai_canonname = 0;
  2503. new_ai->ai_addrlen = ai->ai_addrlen;
  2504. new_ai->ai_addr = gai_alloc<sockaddr>(ai->ai_addrlen);
  2505. memcpy(new_ai->ai_addr, ai->ai_addr, ai->ai_addrlen);
  2506. return new_ai;
  2507. }
  2508. inline int gai_port(addrinfo_type* aihead, int port, int socktype)
  2509. {
  2510. int num_found = 0;
  2511. for (addrinfo_type* ai = aihead; ai; ai = ai->ai_next)
  2512. {
  2513. if (ai->ai_flags & gai_clone_flag)
  2514. {
  2515. if (ai->ai_socktype != 0)
  2516. {
  2517. ai = gai_clone(ai);
  2518. if (ai == 0)
  2519. return -1;
  2520. // ai now points to newly cloned entry.
  2521. }
  2522. }
  2523. else if (ai->ai_socktype != socktype)
  2524. {
  2525. // Ignore if mismatch on socket type.
  2526. continue;
  2527. }
  2528. ai->ai_socktype = socktype;
  2529. switch (ai->ai_family)
  2530. {
  2531. case BOOST_ASIO_OS_DEF(AF_INET):
  2532. {
  2533. sockaddr_in4_type* sinptr =
  2534. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  2535. sinptr->sin_port = port;
  2536. ++num_found;
  2537. break;
  2538. }
  2539. case BOOST_ASIO_OS_DEF(AF_INET6):
  2540. {
  2541. sockaddr_in6_type* sin6ptr =
  2542. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  2543. sin6ptr->sin6_port = port;
  2544. ++num_found;
  2545. break;
  2546. }
  2547. default:
  2548. break;
  2549. }
  2550. }
  2551. return num_found;
  2552. }
  2553. inline int gai_serv(addrinfo_type* aihead,
  2554. const addrinfo_type* hints, const char* serv)
  2555. {
  2556. using namespace std;
  2557. int num_found = 0;
  2558. if (
  2559. #if defined(AI_NUMERICSERV)
  2560. (hints->ai_flags & AI_NUMERICSERV) ||
  2561. #endif
  2562. isdigit(static_cast<unsigned char>(serv[0])))
  2563. {
  2564. int port = htons(atoi(serv));
  2565. if (hints->ai_socktype)
  2566. {
  2567. // Caller specifies socket type.
  2568. int rc = gai_port(aihead, port, hints->ai_socktype);
  2569. if (rc < 0)
  2570. return EAI_MEMORY;
  2571. num_found += rc;
  2572. }
  2573. else
  2574. {
  2575. // Caller does not specify socket type.
  2576. int rc = gai_port(aihead, port, SOCK_STREAM);
  2577. if (rc < 0)
  2578. return EAI_MEMORY;
  2579. num_found += rc;
  2580. rc = gai_port(aihead, port, SOCK_DGRAM);
  2581. if (rc < 0)
  2582. return EAI_MEMORY;
  2583. num_found += rc;
  2584. }
  2585. }
  2586. else
  2587. {
  2588. // Try service name with TCP first, then UDP.
  2589. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
  2590. {
  2591. servent* sptr = getservbyname(serv, "tcp");
  2592. if (sptr != 0)
  2593. {
  2594. int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
  2595. if (rc < 0)
  2596. return EAI_MEMORY;
  2597. num_found += rc;
  2598. }
  2599. }
  2600. if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
  2601. {
  2602. servent* sptr = getservbyname(serv, "udp");
  2603. if (sptr != 0)
  2604. {
  2605. int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
  2606. if (rc < 0)
  2607. return EAI_MEMORY;
  2608. num_found += rc;
  2609. }
  2610. }
  2611. }
  2612. if (num_found == 0)
  2613. {
  2614. if (hints->ai_socktype == 0)
  2615. {
  2616. // All calls to getservbyname() failed.
  2617. return EAI_NONAME;
  2618. }
  2619. else
  2620. {
  2621. // Service not supported for socket type.
  2622. return EAI_SERVICE;
  2623. }
  2624. }
  2625. return 0;
  2626. }
  2627. inline int gai_echeck(const char* host, const char* service,
  2628. int flags, int family, int socktype, int protocol)
  2629. {
  2630. (void)(flags);
  2631. (void)(protocol);
  2632. // Host or service must be specified.
  2633. if (host == 0 || host[0] == '\0')
  2634. if (service == 0 || service[0] == '\0')
  2635. return EAI_NONAME;
  2636. // Check combination of family and socket type.
  2637. switch (family)
  2638. {
  2639. case BOOST_ASIO_OS_DEF(AF_UNSPEC):
  2640. break;
  2641. case BOOST_ASIO_OS_DEF(AF_INET):
  2642. case BOOST_ASIO_OS_DEF(AF_INET6):
  2643. if (service != 0 && service[0] != '\0')
  2644. if (socktype != 0 && socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
  2645. return EAI_SOCKTYPE;
  2646. break;
  2647. default:
  2648. return EAI_FAMILY;
  2649. }
  2650. return 0;
  2651. }
  2652. inline void freeaddrinfo_emulation(addrinfo_type* aihead)
  2653. {
  2654. addrinfo_type* ai = aihead;
  2655. while (ai)
  2656. {
  2657. gai_free(ai->ai_addr);
  2658. gai_free(ai->ai_canonname);
  2659. addrinfo_type* ainext = ai->ai_next;
  2660. gai_free(ai);
  2661. ai = ainext;
  2662. }
  2663. }
  2664. inline int getaddrinfo_emulation(const char* host, const char* service,
  2665. const addrinfo_type* hintsp, addrinfo_type** result)
  2666. {
  2667. // Set up linked list of addrinfo structures.
  2668. addrinfo_type* aihead = 0;
  2669. addrinfo_type** ainext = &aihead;
  2670. char* canon = 0;
  2671. // Supply default hints if not specified by caller.
  2672. addrinfo_type hints = addrinfo_type();
  2673. hints.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
  2674. if (hintsp)
  2675. hints = *hintsp;
  2676. // If the resolution is not specifically for AF_INET6, remove the AI_V4MAPPED
  2677. // and AI_ALL flags.
  2678. #if defined(AI_V4MAPPED)
  2679. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2680. hints.ai_flags &= ~AI_V4MAPPED;
  2681. #endif
  2682. #if defined(AI_ALL)
  2683. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2684. hints.ai_flags &= ~AI_ALL;
  2685. #endif
  2686. // Basic error checking.
  2687. int rc = gai_echeck(host, service, hints.ai_flags, hints.ai_family,
  2688. hints.ai_socktype, hints.ai_protocol);
  2689. if (rc != 0)
  2690. {
  2691. freeaddrinfo_emulation(aihead);
  2692. return rc;
  2693. }
  2694. gai_search search[2];
  2695. int search_count = gai_nsearch(host, &hints, search);
  2696. for (gai_search* sptr = search; sptr < search + search_count; ++sptr)
  2697. {
  2698. // Check for IPv4 dotted decimal string.
  2699. in4_addr_type inaddr;
  2700. boost::system::error_code ec;
  2701. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET),
  2702. sptr->host, &inaddr, 0, ec) == 1)
  2703. {
  2704. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2705. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET))
  2706. {
  2707. freeaddrinfo_emulation(aihead);
  2708. gai_free(canon);
  2709. return EAI_FAMILY;
  2710. }
  2711. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET))
  2712. {
  2713. rc = gai_aistruct(&ainext, &hints, &inaddr, BOOST_ASIO_OS_DEF(AF_INET));
  2714. if (rc != 0)
  2715. {
  2716. freeaddrinfo_emulation(aihead);
  2717. gai_free(canon);
  2718. return rc;
  2719. }
  2720. }
  2721. continue;
  2722. }
  2723. // Check for IPv6 hex string.
  2724. in6_addr_type in6addr;
  2725. if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6),
  2726. sptr->host, &in6addr, 0, ec) == 1)
  2727. {
  2728. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2729. && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
  2730. {
  2731. freeaddrinfo_emulation(aihead);
  2732. gai_free(canon);
  2733. return EAI_FAMILY;
  2734. }
  2735. if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET6))
  2736. {
  2737. rc = gai_aistruct(&ainext, &hints, &in6addr,
  2738. BOOST_ASIO_OS_DEF(AF_INET6));
  2739. if (rc != 0)
  2740. {
  2741. freeaddrinfo_emulation(aihead);
  2742. gai_free(canon);
  2743. return rc;
  2744. }
  2745. }
  2746. continue;
  2747. }
  2748. // Look up hostname.
  2749. hostent hent;
  2750. char hbuf[8192] = "";
  2751. hostent* hptr = socket_ops::gethostbyname(sptr->host,
  2752. sptr->family, &hent, hbuf, sizeof(hbuf), hints.ai_flags, ec);
  2753. if (hptr == 0)
  2754. {
  2755. if (search_count == 2)
  2756. {
  2757. // Failure is OK if there are multiple searches.
  2758. continue;
  2759. }
  2760. freeaddrinfo_emulation(aihead);
  2761. gai_free(canon);
  2762. if (ec == boost::asio::error::host_not_found)
  2763. return EAI_NONAME;
  2764. if (ec == boost::asio::error::host_not_found_try_again)
  2765. return EAI_AGAIN;
  2766. if (ec == boost::asio::error::no_recovery)
  2767. return EAI_FAIL;
  2768. if (ec == boost::asio::error::no_data)
  2769. return EAI_NONAME;
  2770. return EAI_NONAME;
  2771. }
  2772. // Check for address family mismatch if one was specified.
  2773. if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
  2774. && hints.ai_family != hptr->h_addrtype)
  2775. {
  2776. freeaddrinfo_emulation(aihead);
  2777. gai_free(canon);
  2778. socket_ops::freehostent(hptr);
  2779. return EAI_FAMILY;
  2780. }
  2781. // Save canonical name first time.
  2782. if (host != 0 && host[0] != '\0' && hptr->h_name && hptr->h_name[0]
  2783. && (hints.ai_flags & AI_CANONNAME) && canon == 0)
  2784. {
  2785. std::size_t canon_len = strlen(hptr->h_name) + 1;
  2786. canon = gai_alloc<char>(canon_len);
  2787. if (canon == 0)
  2788. {
  2789. freeaddrinfo_emulation(aihead);
  2790. socket_ops::freehostent(hptr);
  2791. return EAI_MEMORY;
  2792. }
  2793. gai_strcpy(canon, hptr->h_name, canon_len);
  2794. }
  2795. // Create an addrinfo structure for each returned address.
  2796. for (char** ap = hptr->h_addr_list; *ap; ++ap)
  2797. {
  2798. rc = gai_aistruct(&ainext, &hints, *ap, hptr->h_addrtype);
  2799. if (rc != 0)
  2800. {
  2801. freeaddrinfo_emulation(aihead);
  2802. gai_free(canon);
  2803. socket_ops::freehostent(hptr);
  2804. return EAI_FAMILY;
  2805. }
  2806. }
  2807. socket_ops::freehostent(hptr);
  2808. }
  2809. // Check if we found anything.
  2810. if (aihead == 0)
  2811. {
  2812. gai_free(canon);
  2813. return EAI_NONAME;
  2814. }
  2815. // Return canonical name in first entry.
  2816. if (host != 0 && host[0] != '\0' && (hints.ai_flags & AI_CANONNAME))
  2817. {
  2818. if (canon)
  2819. {
  2820. aihead->ai_canonname = canon;
  2821. canon = 0;
  2822. }
  2823. else
  2824. {
  2825. std::size_t canonname_len = strlen(search[0].host) + 1;
  2826. aihead->ai_canonname = gai_alloc<char>(canonname_len);
  2827. if (aihead->ai_canonname == 0)
  2828. {
  2829. freeaddrinfo_emulation(aihead);
  2830. return EAI_MEMORY;
  2831. }
  2832. gai_strcpy(aihead->ai_canonname, search[0].host, canonname_len);
  2833. }
  2834. }
  2835. gai_free(canon);
  2836. // Process the service name.
  2837. if (service != 0 && service[0] != '\0')
  2838. {
  2839. rc = gai_serv(aihead, &hints, service);
  2840. if (rc != 0)
  2841. {
  2842. freeaddrinfo_emulation(aihead);
  2843. return rc;
  2844. }
  2845. }
  2846. // Return result to caller.
  2847. *result = aihead;
  2848. return 0;
  2849. }
  2850. inline boost::system::error_code getnameinfo_emulation(
  2851. const socket_addr_type* sa, std::size_t salen, char* host,
  2852. std::size_t hostlen, char* serv, std::size_t servlen, int flags,
  2853. boost::system::error_code& ec)
  2854. {
  2855. using namespace std;
  2856. const char* addr;
  2857. size_t addr_len;
  2858. unsigned short port;
  2859. switch (sa->sa_family)
  2860. {
  2861. case BOOST_ASIO_OS_DEF(AF_INET):
  2862. if (salen != sizeof(sockaddr_in4_type))
  2863. {
  2864. return ec = boost::asio::error::invalid_argument;
  2865. }
  2866. addr = reinterpret_cast<const char*>(
  2867. &reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_addr);
  2868. addr_len = sizeof(in4_addr_type);
  2869. port = reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_port;
  2870. break;
  2871. case BOOST_ASIO_OS_DEF(AF_INET6):
  2872. if (salen != sizeof(sockaddr_in6_type))
  2873. {
  2874. return ec = boost::asio::error::invalid_argument;
  2875. }
  2876. addr = reinterpret_cast<const char*>(
  2877. &reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_addr);
  2878. addr_len = sizeof(in6_addr_type);
  2879. port = reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_port;
  2880. break;
  2881. default:
  2882. return ec = boost::asio::error::address_family_not_supported;
  2883. }
  2884. if (host && hostlen > 0)
  2885. {
  2886. if (flags & NI_NUMERICHOST)
  2887. {
  2888. if (socket_ops::inet_ntop(sa->sa_family, addr, host, hostlen, 0, ec) == 0)
  2889. {
  2890. return ec;
  2891. }
  2892. }
  2893. else
  2894. {
  2895. hostent hent;
  2896. char hbuf[8192] = "";
  2897. hostent* hptr = socket_ops::gethostbyaddr(addr,
  2898. static_cast<int>(addr_len), sa->sa_family,
  2899. &hent, hbuf, sizeof(hbuf), ec);
  2900. if (hptr && hptr->h_name && hptr->h_name[0] != '\0')
  2901. {
  2902. if (flags & NI_NOFQDN)
  2903. {
  2904. char* dot = strchr(hptr->h_name, '.');
  2905. if (dot)
  2906. {
  2907. *dot = 0;
  2908. }
  2909. }
  2910. gai_strcpy(host, hptr->h_name, hostlen);
  2911. socket_ops::freehostent(hptr);
  2912. }
  2913. else
  2914. {
  2915. socket_ops::freehostent(hptr);
  2916. if (flags & NI_NAMEREQD)
  2917. {
  2918. return ec = boost::asio::error::host_not_found;
  2919. }
  2920. if (socket_ops::inet_ntop(sa->sa_family,
  2921. addr, host, hostlen, 0, ec) == 0)
  2922. {
  2923. return ec;
  2924. }
  2925. }
  2926. }
  2927. }
  2928. if (serv && servlen > 0)
  2929. {
  2930. if (flags & NI_NUMERICSERV)
  2931. {
  2932. if (servlen < 6)
  2933. {
  2934. return ec = boost::asio::error::no_buffer_space;
  2935. }
  2936. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2937. sprintf_s(serv, servlen, "%u", ntohs(port));
  2938. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2939. sprintf(serv, "%u", ntohs(port));
  2940. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2941. }
  2942. else
  2943. {
  2944. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2945. static ::pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  2946. ::pthread_mutex_lock(&mutex);
  2947. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2948. servent* sptr = ::getservbyport(port, (flags & NI_DGRAM) ? "udp" : 0);
  2949. if (sptr && sptr->s_name && sptr->s_name[0] != '\0')
  2950. {
  2951. gai_strcpy(serv, sptr->s_name, servlen);
  2952. }
  2953. else
  2954. {
  2955. if (servlen < 6)
  2956. {
  2957. return ec = boost::asio::error::no_buffer_space;
  2958. }
  2959. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  2960. sprintf_s(serv, servlen, "%u", ntohs(port));
  2961. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2962. sprintf(serv, "%u", ntohs(port));
  2963. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  2964. }
  2965. #if defined(BOOST_ASIO_HAS_PTHREADS)
  2966. ::pthread_mutex_unlock(&mutex);
  2967. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  2968. }
  2969. }
  2970. ec = boost::system::error_code();
  2971. return ec;
  2972. }
  2973. #endif // !defined(BOOST_ASIO_HAS_GETADDRINFO)
  2974. inline boost::system::error_code translate_addrinfo_error(int error)
  2975. {
  2976. switch (error)
  2977. {
  2978. case 0:
  2979. return boost::system::error_code();
  2980. case EAI_AGAIN:
  2981. return boost::asio::error::host_not_found_try_again;
  2982. case EAI_BADFLAGS:
  2983. return boost::asio::error::invalid_argument;
  2984. case EAI_FAIL:
  2985. return boost::asio::error::no_recovery;
  2986. case EAI_FAMILY:
  2987. return boost::asio::error::address_family_not_supported;
  2988. case EAI_MEMORY:
  2989. return boost::asio::error::no_memory;
  2990. case EAI_NONAME:
  2991. #if defined(EAI_ADDRFAMILY)
  2992. case EAI_ADDRFAMILY:
  2993. #endif
  2994. #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
  2995. case EAI_NODATA:
  2996. #endif
  2997. return boost::asio::error::host_not_found;
  2998. case EAI_SERVICE:
  2999. return boost::asio::error::service_not_found;
  3000. case EAI_SOCKTYPE:
  3001. return boost::asio::error::socket_type_not_supported;
  3002. default: // Possibly the non-portable EAI_SYSTEM.
  3003. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3004. return boost::system::error_code(
  3005. WSAGetLastError(), boost::asio::error::get_system_category());
  3006. #else
  3007. return boost::system::error_code(
  3008. errno, boost::asio::error::get_system_category());
  3009. #endif
  3010. }
  3011. }
  3012. boost::system::error_code getaddrinfo(const char* host,
  3013. const char* service, const addrinfo_type& hints,
  3014. addrinfo_type** result, boost::system::error_code& ec)
  3015. {
  3016. host = (host && *host) ? host : 0;
  3017. service = (service && *service) ? service : 0;
  3018. clear_last_error();
  3019. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3020. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3021. // Building for Windows XP, Windows Server 2003, or later.
  3022. int error = ::getaddrinfo(host, service, &hints, result);
  3023. return ec = translate_addrinfo_error(error);
  3024. # else
  3025. // Building for Windows 2000 or earlier.
  3026. typedef int (WSAAPI *gai_t)(const char*,
  3027. const char*, const addrinfo_type*, addrinfo_type**);
  3028. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3029. {
  3030. if (gai_t gai = (gai_t)::GetProcAddress(winsock_module, "getaddrinfo"))
  3031. {
  3032. int error = gai(host, service, &hints, result);
  3033. return ec = translate_addrinfo_error(error);
  3034. }
  3035. }
  3036. int error = getaddrinfo_emulation(host, service, &hints, result);
  3037. return ec = translate_addrinfo_error(error);
  3038. # endif
  3039. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3040. int error = getaddrinfo_emulation(host, service, &hints, result);
  3041. return ec = translate_addrinfo_error(error);
  3042. #else
  3043. int error = ::getaddrinfo(host, service, &hints, result);
  3044. #if defined(__MACH__) && defined(__APPLE__)
  3045. using namespace std; // For isdigit and atoi.
  3046. if (error == 0 && service && isdigit(static_cast<unsigned char>(service[0])))
  3047. {
  3048. u_short_type port = host_to_network_short(atoi(service));
  3049. for (addrinfo_type* ai = *result; ai; ai = ai->ai_next)
  3050. {
  3051. switch (ai->ai_family)
  3052. {
  3053. case BOOST_ASIO_OS_DEF(AF_INET):
  3054. {
  3055. sockaddr_in4_type* sinptr =
  3056. reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
  3057. if (sinptr->sin_port == 0)
  3058. sinptr->sin_port = port;
  3059. break;
  3060. }
  3061. case BOOST_ASIO_OS_DEF(AF_INET6):
  3062. {
  3063. sockaddr_in6_type* sin6ptr =
  3064. reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
  3065. if (sin6ptr->sin6_port == 0)
  3066. sin6ptr->sin6_port = port;
  3067. break;
  3068. }
  3069. default:
  3070. break;
  3071. }
  3072. }
  3073. }
  3074. #endif
  3075. return ec = translate_addrinfo_error(error);
  3076. #endif
  3077. }
  3078. boost::system::error_code background_getaddrinfo(
  3079. const weak_cancel_token_type& cancel_token, const char* host,
  3080. const char* service, const addrinfo_type& hints,
  3081. addrinfo_type** result, boost::system::error_code& ec)
  3082. {
  3083. if (cancel_token.expired())
  3084. ec = boost::asio::error::operation_aborted;
  3085. else
  3086. socket_ops::getaddrinfo(host, service, hints, result, ec);
  3087. return ec;
  3088. }
  3089. void freeaddrinfo(addrinfo_type* ai)
  3090. {
  3091. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3092. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3093. // Building for Windows XP, Windows Server 2003, or later.
  3094. ::freeaddrinfo(ai);
  3095. # else
  3096. // Building for Windows 2000 or earlier.
  3097. typedef int (WSAAPI *fai_t)(addrinfo_type*);
  3098. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3099. {
  3100. if (fai_t fai = (fai_t)::GetProcAddress(winsock_module, "freeaddrinfo"))
  3101. {
  3102. fai(ai);
  3103. return;
  3104. }
  3105. }
  3106. freeaddrinfo_emulation(ai);
  3107. # endif
  3108. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3109. freeaddrinfo_emulation(ai);
  3110. #else
  3111. ::freeaddrinfo(ai);
  3112. #endif
  3113. }
  3114. boost::system::error_code getnameinfo(const socket_addr_type* addr,
  3115. std::size_t addrlen, char* host, std::size_t hostlen,
  3116. char* serv, std::size_t servlen, int flags, boost::system::error_code& ec)
  3117. {
  3118. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  3119. # if defined(BOOST_ASIO_HAS_GETADDRINFO)
  3120. // Building for Windows XP, Windows Server 2003, or later.
  3121. clear_last_error();
  3122. int error = ::getnameinfo(addr, static_cast<socklen_t>(addrlen),
  3123. host, static_cast<DWORD>(hostlen),
  3124. serv, static_cast<DWORD>(servlen), flags);
  3125. return ec = translate_addrinfo_error(error);
  3126. # else
  3127. // Building for Windows 2000 or earlier.
  3128. typedef int (WSAAPI *gni_t)(const socket_addr_type*,
  3129. int, char*, DWORD, char*, DWORD, int);
  3130. if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
  3131. {
  3132. if (gni_t gni = (gni_t)::GetProcAddress(winsock_module, "getnameinfo"))
  3133. {
  3134. clear_last_error();
  3135. int error = gni(addr, static_cast<int>(addrlen),
  3136. host, static_cast<DWORD>(hostlen),
  3137. serv, static_cast<DWORD>(servlen), flags);
  3138. return ec = translate_addrinfo_error(error);
  3139. }
  3140. }
  3141. clear_last_error();
  3142. return getnameinfo_emulation(addr, addrlen,
  3143. host, hostlen, serv, servlen, flags, ec);
  3144. # endif
  3145. #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
  3146. using namespace std; // For memcpy.
  3147. sockaddr_storage_type tmp_addr;
  3148. memcpy(&tmp_addr, addr, addrlen);
  3149. addr = reinterpret_cast<socket_addr_type*>(&tmp_addr);
  3150. clear_last_error();
  3151. return getnameinfo_emulation(addr, addrlen,
  3152. host, hostlen, serv, servlen, flags, ec);
  3153. #else
  3154. clear_last_error();
  3155. int error = ::getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
  3156. return ec = translate_addrinfo_error(error);
  3157. #endif
  3158. }
  3159. boost::system::error_code sync_getnameinfo(
  3160. const socket_addr_type* addr, std::size_t addrlen,
  3161. char* host, std::size_t hostlen, char* serv,
  3162. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3163. {
  3164. // First try resolving with the service name. If that fails try resolving
  3165. // but allow the service to be returned as a number.
  3166. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3167. socket_ops::getnameinfo(addr, addrlen, host,
  3168. hostlen, serv, servlen, flags, ec);
  3169. if (ec)
  3170. {
  3171. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3172. serv, servlen, flags | NI_NUMERICSERV, ec);
  3173. }
  3174. return ec;
  3175. }
  3176. boost::system::error_code background_getnameinfo(
  3177. const weak_cancel_token_type& cancel_token,
  3178. const socket_addr_type* addr, std::size_t addrlen,
  3179. char* host, std::size_t hostlen, char* serv,
  3180. std::size_t servlen, int sock_type, boost::system::error_code& ec)
  3181. {
  3182. if (cancel_token.expired())
  3183. {
  3184. ec = boost::asio::error::operation_aborted;
  3185. }
  3186. else
  3187. {
  3188. // First try resolving with the service name. If that fails try resolving
  3189. // but allow the service to be returned as a number.
  3190. int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
  3191. socket_ops::getnameinfo(addr, addrlen, host,
  3192. hostlen, serv, servlen, flags, ec);
  3193. if (ec)
  3194. {
  3195. socket_ops::getnameinfo(addr, addrlen, host, hostlen,
  3196. serv, servlen, flags | NI_NUMERICSERV, ec);
  3197. }
  3198. }
  3199. return ec;
  3200. }
  3201. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3202. u_long_type network_to_host_long(u_long_type value)
  3203. {
  3204. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3205. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3206. u_long_type result = (static_cast<u_long_type>(value_p[0]) << 24)
  3207. | (static_cast<u_long_type>(value_p[1]) << 16)
  3208. | (static_cast<u_long_type>(value_p[2]) << 8)
  3209. | static_cast<u_long_type>(value_p[3]);
  3210. return result;
  3211. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3212. return ntohl(value);
  3213. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3214. }
  3215. u_long_type host_to_network_long(u_long_type value)
  3216. {
  3217. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3218. u_long_type result;
  3219. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3220. result_p[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
  3221. result_p[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
  3222. result_p[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3223. result_p[3] = static_cast<unsigned char>(value & 0xFF);
  3224. return result;
  3225. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3226. return htonl(value);
  3227. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3228. }
  3229. u_short_type network_to_host_short(u_short_type value)
  3230. {
  3231. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3232. unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
  3233. u_short_type result = (static_cast<u_short_type>(value_p[0]) << 8)
  3234. | static_cast<u_short_type>(value_p[1]);
  3235. return result;
  3236. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3237. return ntohs(value);
  3238. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3239. }
  3240. u_short_type host_to_network_short(u_short_type value)
  3241. {
  3242. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3243. u_short_type result;
  3244. unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
  3245. result_p[0] = static_cast<unsigned char>((value >> 8) & 0xFF);
  3246. result_p[1] = static_cast<unsigned char>(value & 0xFF);
  3247. return result;
  3248. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3249. return htons(value);
  3250. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  3251. }
  3252. } // namespace socket_ops
  3253. } // namespace detail
  3254. } // namespace asio
  3255. } // namespace boost
  3256. #include <boost/asio/detail/pop_options.hpp>
  3257. #endif // BOOST_ASIO_DETAIL_SOCKET_OPS_IPP