123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573 |
- //
- // detail/impl/socket_ops.ipp
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
- #define BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #include <cctype>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
- #include <new>
- #include <boost/asio/detail/assert.hpp>
- #include <boost/asio/detail/socket_ops.hpp>
- #include <boost/asio/error.hpp>
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- # include <codecvt>
- # include <locale>
- # include <string>
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) \
- || defined(__MACH__) && defined(__APPLE__)
- # if defined(BOOST_ASIO_HAS_PTHREADS)
- # include <pthread.h>
- # endif // defined(BOOST_ASIO_HAS_PTHREADS)
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- // || defined(__MACH__) && defined(__APPLE__)
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- namespace socket_ops {
- #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- struct msghdr { int msg_namelen; };
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- #if defined(__hpux)
- // HP-UX doesn't declare these functions extern "C", so they are declared again
- // here to avoid linker errors about undefined symbols.
- extern "C" char* if_indextoname(unsigned int, char*);
- extern "C" unsigned int if_nametoindex(const char*);
- #endif // defined(__hpux)
- #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- inline void clear_last_error()
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- WSASetLastError(0);
- #else
- errno = 0;
- #endif
- }
- #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- template <typename ReturnType>
- inline ReturnType error_wrapper(ReturnType return_value,
- boost::system::error_code& ec)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- ec = boost::system::error_code(WSAGetLastError(),
- boost::asio::error::get_system_category());
- #else
- ec = boost::system::error_code(errno,
- boost::asio::error::get_system_category());
- #endif
- return return_value;
- }
- template <typename SockLenType>
- inline socket_type call_accept(SockLenType msghdr::*,
- socket_type s, socket_addr_type* addr, std::size_t* addrlen)
- {
- SockLenType tmp_addrlen = addrlen ? (SockLenType)*addrlen : 0;
- socket_type result = ::accept(s, addr, addrlen ? &tmp_addrlen : 0);
- if (addrlen)
- *addrlen = (std::size_t)tmp_addrlen;
- return result;
- }
- socket_type accept(socket_type s, socket_addr_type* addr,
- std::size_t* addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return invalid_socket;
- }
- clear_last_error();
- socket_type new_s = error_wrapper(call_accept(
- &msghdr::msg_namelen, s, addr, addrlen), ec);
- if (new_s == invalid_socket)
- return new_s;
- #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
- int optval = 1;
- int result = error_wrapper(::setsockopt(new_s,
- SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
- if (result != 0)
- {
- ::close(new_s);
- return invalid_socket;
- }
- #endif
- ec = boost::system::error_code();
- return new_s;
- }
- socket_type sync_accept(socket_type s, state_type state,
- socket_addr_type* addr, std::size_t* addrlen, boost::system::error_code& ec)
- {
- // Accept a socket.
- for (;;)
- {
- // Try to complete the operation without blocking.
- socket_type new_socket = socket_ops::accept(s, addr, addrlen, ec);
- // Check if operation succeeded.
- if (new_socket != invalid_socket)
- return new_socket;
- // Operation failed.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- {
- if (state & user_set_non_blocking)
- return invalid_socket;
- // Fall through to retry operation.
- }
- else if (ec == boost::asio::error::connection_aborted)
- {
- if (state & enable_connection_aborted)
- return invalid_socket;
- // Fall through to retry operation.
- }
- #if defined(EPROTO)
- else if (ec.value() == EPROTO)
- {
- if (state & enable_connection_aborted)
- return invalid_socket;
- // Fall through to retry operation.
- }
- #endif // defined(EPROTO)
- else
- return invalid_socket;
- // Wait for socket to become ready.
- if (socket_ops::poll_read(s, 0, -1, ec) < 0)
- return invalid_socket;
- }
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_accept(socket_type s,
- void* output_buffer, DWORD address_length,
- socket_addr_type* addr, std::size_t* addrlen,
- socket_type new_socket, boost::system::error_code& ec)
- {
- // Map non-portable errors to their portable counterparts.
- if (ec.value() == ERROR_NETNAME_DELETED)
- ec = boost::asio::error::connection_aborted;
- if (!ec)
- {
- // Get the address of the peer.
- if (addr && addrlen)
- {
- LPSOCKADDR local_addr = 0;
- int local_addr_length = 0;
- LPSOCKADDR remote_addr = 0;
- int remote_addr_length = 0;
- GetAcceptExSockaddrs(output_buffer, 0, address_length,
- address_length, &local_addr, &local_addr_length,
- &remote_addr, &remote_addr_length);
- if (static_cast<std::size_t>(remote_addr_length) > *addrlen)
- {
- ec = boost::asio::error::invalid_argument;
- }
- else
- {
- using namespace std; // For memcpy.
- memcpy(addr, remote_addr, remote_addr_length);
- *addrlen = static_cast<std::size_t>(remote_addr_length);
- }
- }
- // Need to set the SO_UPDATE_ACCEPT_CONTEXT option so that getsockname
- // and getpeername will work on the accepted socket.
- SOCKET update_ctx_param = s;
- socket_ops::state_type state = 0;
- socket_ops::setsockopt(new_socket, state,
- SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
- &update_ctx_param, sizeof(SOCKET), ec);
- }
- }
- #else // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_accept(socket_type s,
- state_type state, socket_addr_type* addr, std::size_t* addrlen,
- boost::system::error_code& ec, socket_type& new_socket)
- {
- for (;;)
- {
- // Accept the waiting connection.
- new_socket = socket_ops::accept(s, addr, addrlen, ec);
- // Check if operation succeeded.
- if (new_socket != invalid_socket)
- return true;
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Operation failed.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- {
- // Fall through to retry operation.
- }
- else if (ec == boost::asio::error::connection_aborted)
- {
- if (state & enable_connection_aborted)
- return true;
- // Fall through to retry operation.
- }
- #if defined(EPROTO)
- else if (ec.value() == EPROTO)
- {
- if (state & enable_connection_aborted)
- return true;
- // Fall through to retry operation.
- }
- #endif // defined(EPROTO)
- else
- return true;
- return false;
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- template <typename SockLenType>
- inline int call_bind(SockLenType msghdr::*,
- socket_type s, const socket_addr_type* addr, std::size_t addrlen)
- {
- return ::bind(s, addr, (SockLenType)addrlen);
- }
- int bind(socket_type s, const socket_addr_type* addr,
- std::size_t addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- int result = error_wrapper(call_bind(
- &msghdr::msg_namelen, s, addr, addrlen), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- int close(socket_type s, state_type& state,
- bool destruction, boost::system::error_code& ec)
- {
- int result = 0;
- if (s != invalid_socket)
- {
- // We don't want the destructor to block, so set the socket to linger in
- // the background. If the user doesn't like this behaviour then they need
- // to explicitly close the socket.
- if (destruction && (state & user_set_linger))
- {
- ::linger opt;
- opt.l_onoff = 0;
- opt.l_linger = 0;
- boost::system::error_code ignored_ec;
- socket_ops::setsockopt(s, state, SOL_SOCKET,
- SO_LINGER, &opt, sizeof(opt), ignored_ec);
- }
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- result = error_wrapper(::closesocket(s), ec);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- result = error_wrapper(::close(s), ec);
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- if (result != 0
- && (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again))
- {
- // According to UNIX Network Programming Vol. 1, it is possible for
- // close() to fail with EWOULDBLOCK under certain circumstances. What
- // isn't clear is the state of the descriptor after this error. The one
- // current OS where this behaviour is seen, Windows, says that the socket
- // remains open. Therefore we'll put the descriptor back into blocking
- // mode and have another attempt at closing it.
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- ioctl_arg_type arg = 0;
- ::ioctlsocket(s, FIONBIO, &arg);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- # if defined(__SYMBIAN32__)
- int flags = ::fcntl(s, F_GETFL, 0);
- if (flags >= 0)
- ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
- # else // defined(__SYMBIAN32__)
- ioctl_arg_type arg = 0;
- ::ioctl(s, FIONBIO, &arg);
- # endif // defined(__SYMBIAN32__)
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- state &= ~non_blocking;
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- result = error_wrapper(::closesocket(s), ec);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- result = error_wrapper(::close(s), ec);
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- }
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- bool set_user_non_blocking(socket_type s,
- state_type& state, bool value, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return false;
- }
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- ioctl_arg_type arg = (value ? 1 : 0);
- int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
- #elif defined(__SYMBIAN32__)
- int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
- if (result >= 0)
- {
- clear_last_error();
- int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
- result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
- }
- #else
- ioctl_arg_type arg = (value ? 1 : 0);
- int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
- #endif
- if (result >= 0)
- {
- ec = boost::system::error_code();
- if (value)
- state |= user_set_non_blocking;
- else
- {
- // Clearing the user-set non-blocking mode always overrides any
- // internally-set non-blocking flag. Any subsequent asynchronous
- // operations will need to re-enable non-blocking I/O.
- state &= ~(user_set_non_blocking | internal_non_blocking);
- }
- return true;
- }
- return false;
- }
- bool set_internal_non_blocking(socket_type s,
- state_type& state, bool value, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return false;
- }
- if (!value && (state & user_set_non_blocking))
- {
- // It does not make sense to clear the internal non-blocking flag if the
- // user still wants non-blocking behaviour. Return an error and let the
- // caller figure out whether to update the user-set non-blocking flag.
- ec = boost::asio::error::invalid_argument;
- return false;
- }
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- ioctl_arg_type arg = (value ? 1 : 0);
- int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
- #elif defined(__SYMBIAN32__)
- int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
- if (result >= 0)
- {
- clear_last_error();
- int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
- result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
- }
- #else
- ioctl_arg_type arg = (value ? 1 : 0);
- int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
- #endif
- if (result >= 0)
- {
- ec = boost::system::error_code();
- if (value)
- state |= internal_non_blocking;
- else
- state &= ~internal_non_blocking;
- return true;
- }
- return false;
- }
- int shutdown(socket_type s, int what, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- int result = error_wrapper(::shutdown(s, what), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- template <typename SockLenType>
- inline int call_connect(SockLenType msghdr::*,
- socket_type s, const socket_addr_type* addr, std::size_t addrlen)
- {
- return ::connect(s, addr, (SockLenType)addrlen);
- }
- int connect(socket_type s, const socket_addr_type* addr,
- std::size_t addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- int result = error_wrapper(call_connect(
- &msghdr::msg_namelen, s, addr, addrlen), ec);
- if (result == 0)
- ec = boost::system::error_code();
- #if defined(__linux__)
- else if (ec == boost::asio::error::try_again)
- ec = boost::asio::error::no_buffer_space;
- #endif // defined(__linux__)
- return result;
- }
- void sync_connect(socket_type s, const socket_addr_type* addr,
- std::size_t addrlen, boost::system::error_code& ec)
- {
- // Perform the connect operation.
- socket_ops::connect(s, addr, addrlen, ec);
- if (ec != boost::asio::error::in_progress
- && ec != boost::asio::error::would_block)
- {
- // The connect operation finished immediately.
- return;
- }
- // Wait for socket to become ready.
- if (socket_ops::poll_connect(s, -1, ec) < 0)
- return;
- // Get the error code from the connect operation.
- int connect_error = 0;
- size_t connect_error_len = sizeof(connect_error);
- if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
- &connect_error, &connect_error_len, ec) == socket_error_retval)
- return;
- // Return the result of the connect operation.
- ec = boost::system::error_code(connect_error,
- boost::asio::error::get_system_category());
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_connect(socket_type s, boost::system::error_code& ec)
- {
- // Map non-portable errors to their portable counterparts.
- switch (ec.value())
- {
- case ERROR_CONNECTION_REFUSED:
- ec = boost::asio::error::connection_refused;
- break;
- case ERROR_NETWORK_UNREACHABLE:
- ec = boost::asio::error::network_unreachable;
- break;
- case ERROR_HOST_UNREACHABLE:
- ec = boost::asio::error::host_unreachable;
- break;
- case ERROR_SEM_TIMEOUT:
- ec = boost::asio::error::timed_out;
- break;
- default:
- break;
- }
- if (!ec)
- {
- // Need to set the SO_UPDATE_CONNECT_CONTEXT option so that getsockname
- // and getpeername will work on the connected socket.
- socket_ops::state_type state = 0;
- const int so_update_connect_context = 0x7010;
- socket_ops::setsockopt(s, state, SOL_SOCKET,
- so_update_connect_context, 0, 0, ec);
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_connect(socket_type s, boost::system::error_code& ec)
- {
- // Check if the connect operation has finished. This is required since we may
- // get spurious readiness notifications from the reactor.
- #if defined(BOOST_ASIO_WINDOWS) \
- || defined(__CYGWIN__) \
- || defined(__SYMBIAN32__)
- fd_set write_fds;
- FD_ZERO(&write_fds);
- FD_SET(s, &write_fds);
- fd_set except_fds;
- FD_ZERO(&except_fds);
- FD_SET(s, &except_fds);
- timeval zero_timeout;
- zero_timeout.tv_sec = 0;
- zero_timeout.tv_usec = 0;
- int ready = ::select(s + 1, 0, &write_fds, &except_fds, &zero_timeout);
- #else // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- pollfd fds;
- fds.fd = s;
- fds.events = POLLOUT;
- fds.revents = 0;
- int ready = ::poll(&fds, 1, 0);
- #endif // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- if (ready == 0)
- {
- // The asynchronous connect operation is still in progress.
- return false;
- }
- // Get the error code from the connect operation.
- int connect_error = 0;
- size_t connect_error_len = sizeof(connect_error);
- if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
- &connect_error, &connect_error_len, ec) == 0)
- {
- if (connect_error)
- {
- ec = boost::system::error_code(connect_error,
- boost::asio::error::get_system_category());
- }
- else
- ec = boost::system::error_code();
- }
- return true;
- }
- int socketpair(int af, int type, int protocol,
- socket_type sv[2], boost::system::error_code& ec)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- (void)(af);
- (void)(type);
- (void)(protocol);
- (void)(sv);
- ec = boost::asio::error::operation_not_supported;
- return socket_error_retval;
- #else
- clear_last_error();
- int result = error_wrapper(::socketpair(af, type, protocol, sv), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- #endif
- }
- bool sockatmark(socket_type s, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return false;
- }
- #if defined(SIOCATMARK)
- ioctl_arg_type value = 0;
- # if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- int result = error_wrapper(::ioctlsocket(s, SIOCATMARK, &value), ec);
- # else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- int result = error_wrapper(::ioctl(s, SIOCATMARK, &value), ec);
- # endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- if (result == 0)
- ec = boost::system::error_code();
- # if defined(ENOTTY)
- if (ec.value() == ENOTTY)
- ec = boost::asio::error::not_socket;
- # endif // defined(ENOTTY)
- #else // defined(SIOCATMARK)
- int value = error_wrapper(::sockatmark(s), ec);
- if (value != -1)
- ec = boost::system::error_code();
- #endif // defined(SIOCATMARK)
- return ec ? false : value != 0;
- }
- size_t available(socket_type s, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- ioctl_arg_type value = 0;
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- int result = error_wrapper(::ioctlsocket(s, FIONREAD, &value), ec);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- int result = error_wrapper(::ioctl(s, FIONREAD, &value), ec);
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- if (result == 0)
- ec = boost::system::error_code();
- #if defined(ENOTTY)
- if (ec.value() == ENOTTY)
- ec = boost::asio::error::not_socket;
- #endif // defined(ENOTTY)
- return ec ? static_cast<size_t>(0) : static_cast<size_t>(value);
- }
- int listen(socket_type s, int backlog, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- int result = error_wrapper(::listen(s, backlog), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- inline void init_buf_iov_base(void*& base, void* addr)
- {
- base = addr;
- }
- template <typename T>
- inline void init_buf_iov_base(T& base, void* addr)
- {
- base = static_cast<T>(addr);
- }
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- typedef WSABUF buf;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- typedef iovec buf;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- void init_buf(buf& b, void* data, size_t size)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- b.buf = static_cast<char*>(data);
- b.len = static_cast<u_long>(size);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- init_buf_iov_base(b.iov_base, data);
- b.iov_len = size;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- void init_buf(buf& b, const void* data, size_t size)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- b.buf = static_cast<char*>(const_cast<void*>(data));
- b.len = static_cast<u_long>(size);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- init_buf_iov_base(b.iov_base, const_cast<void*>(data));
- b.iov_len = size;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- inline void init_msghdr_msg_name(void*& name, socket_addr_type* addr)
- {
- name = addr;
- }
- inline void init_msghdr_msg_name(void*& name, const socket_addr_type* addr)
- {
- name = const_cast<socket_addr_type*>(addr);
- }
- template <typename T>
- inline void init_msghdr_msg_name(T& name, socket_addr_type* addr)
- {
- name = reinterpret_cast<T>(addr);
- }
- template <typename T>
- inline void init_msghdr_msg_name(T& name, const socket_addr_type* addr)
- {
- name = reinterpret_cast<T>(const_cast<socket_addr_type*>(addr));
- }
- signed_size_type recv(socket_type s, buf* bufs, size_t count,
- int flags, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- // Receive some data.
- DWORD recv_buf_count = static_cast<DWORD>(count);
- DWORD bytes_transferred = 0;
- DWORD recv_flags = flags;
- int result = error_wrapper(::WSARecv(s, bufs,
- recv_buf_count, &bytes_transferred, &recv_flags, 0, 0), ec);
- if (ec.value() == ERROR_NETNAME_DELETED)
- ec = boost::asio::error::connection_reset;
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- ec = boost::asio::error::connection_refused;
- else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
- ec.assign(0, ec.category());
- if (result != 0)
- return socket_error_retval;
- ec = boost::system::error_code();
- return bytes_transferred;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- msghdr msg = msghdr();
- msg.msg_iov = bufs;
- msg.msg_iovlen = static_cast<int>(count);
- signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- size_t sync_recv(socket_type s, state_type state, buf* bufs,
- size_t count, int flags, bool all_empty, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // A request to read 0 bytes on a stream is a no-op.
- if (all_empty && (state & stream_oriented))
- {
- ec = boost::system::error_code();
- return 0;
- }
- // Read some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
- // Check if operation succeeded.
- if (bytes > 0)
- return bytes;
- // Check for EOF.
- if ((state & stream_oriented) && bytes == 0)
- {
- ec = boost::asio::error::eof;
- return 0;
- }
- // Operation failed.
- if ((state & user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for socket to become ready.
- if (socket_ops::poll_read(s, 0, -1, ec) < 0)
- return 0;
- }
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_recv(state_type state,
- const weak_cancel_token_type& cancel_token, bool all_empty,
- boost::system::error_code& ec, size_t bytes_transferred)
- {
- // Map non-portable errors to their portable counterparts.
- if (ec.value() == ERROR_NETNAME_DELETED)
- {
- if (cancel_token.expired())
- ec = boost::asio::error::operation_aborted;
- else
- ec = boost::asio::error::connection_reset;
- }
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- {
- ec = boost::asio::error::connection_refused;
- }
- else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
- {
- ec.assign(0, ec.category());
- }
- // Check for connection closed.
- else if (!ec && bytes_transferred == 0
- && (state & stream_oriented) != 0
- && !all_empty)
- {
- ec = boost::asio::error::eof;
- }
- }
- #else // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_recv(socket_type s,
- buf* bufs, size_t count, int flags, bool is_stream,
- boost::system::error_code& ec, size_t& bytes_transferred)
- {
- for (;;)
- {
- // Read some data.
- signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
- // Check for end of stream.
- if (is_stream && bytes == 0)
- {
- ec = boost::asio::error::eof;
- return true;
- }
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- // Operation is complete.
- if (bytes >= 0)
- {
- ec = boost::system::error_code();
- bytes_transferred = bytes;
- }
- else
- bytes_transferred = 0;
- return true;
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- signed_size_type recvfrom(socket_type s, buf* bufs, size_t count,
- int flags, socket_addr_type* addr, std::size_t* addrlen,
- boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- // Receive some data.
- DWORD recv_buf_count = static_cast<DWORD>(count);
- DWORD bytes_transferred = 0;
- DWORD recv_flags = flags;
- int tmp_addrlen = (int)*addrlen;
- int result = error_wrapper(::WSARecvFrom(s, bufs, recv_buf_count,
- &bytes_transferred, &recv_flags, addr, &tmp_addrlen, 0, 0), ec);
- *addrlen = (std::size_t)tmp_addrlen;
- if (ec.value() == ERROR_NETNAME_DELETED)
- ec = boost::asio::error::connection_reset;
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- ec = boost::asio::error::connection_refused;
- else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
- ec.assign(0, ec.category());
- if (result != 0)
- return socket_error_retval;
- ec = boost::system::error_code();
- return bytes_transferred;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- msghdr msg = msghdr();
- init_msghdr_msg_name(msg.msg_name, addr);
- msg.msg_namelen = static_cast<int>(*addrlen);
- msg.msg_iov = bufs;
- msg.msg_iovlen = static_cast<int>(count);
- signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
- *addrlen = msg.msg_namelen;
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- size_t sync_recvfrom(socket_type s, state_type state, buf* bufs,
- size_t count, int flags, socket_addr_type* addr,
- std::size_t* addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Read some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- signed_size_type bytes = socket_ops::recvfrom(
- s, bufs, count, flags, addr, addrlen, ec);
- // Check if operation succeeded.
- if (bytes >= 0)
- return bytes;
- // Operation failed.
- if ((state & user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for socket to become ready.
- if (socket_ops::poll_read(s, 0, -1, ec) < 0)
- return 0;
- }
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_recvfrom(
- const weak_cancel_token_type& cancel_token,
- boost::system::error_code& ec)
- {
- // Map non-portable errors to their portable counterparts.
- if (ec.value() == ERROR_NETNAME_DELETED)
- {
- if (cancel_token.expired())
- ec = boost::asio::error::operation_aborted;
- else
- ec = boost::asio::error::connection_reset;
- }
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- {
- ec = boost::asio::error::connection_refused;
- }
- else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
- {
- ec.assign(0, ec.category());
- }
- }
- #else // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_recvfrom(socket_type s,
- buf* bufs, size_t count, int flags,
- socket_addr_type* addr, std::size_t* addrlen,
- boost::system::error_code& ec, size_t& bytes_transferred)
- {
- for (;;)
- {
- // Read some data.
- signed_size_type bytes = socket_ops::recvfrom(
- s, bufs, count, flags, addr, addrlen, ec);
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- // Operation is complete.
- if (bytes >= 0)
- {
- ec = boost::system::error_code();
- bytes_transferred = bytes;
- }
- else
- bytes_transferred = 0;
- return true;
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- signed_size_type recvmsg(socket_type s, buf* bufs, size_t count,
- int in_flags, int& out_flags, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- out_flags = 0;
- return socket_ops::recv(s, bufs, count, in_flags, ec);
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- msghdr msg = msghdr();
- msg.msg_iov = bufs;
- msg.msg_iovlen = static_cast<int>(count);
- signed_size_type result = error_wrapper(::recvmsg(s, &msg, in_flags), ec);
- if (result >= 0)
- {
- ec = boost::system::error_code();
- out_flags = msg.msg_flags;
- }
- else
- out_flags = 0;
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- size_t sync_recvmsg(socket_type s, state_type state,
- buf* bufs, size_t count, int in_flags, int& out_flags,
- boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Read some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- signed_size_type bytes = socket_ops::recvmsg(
- s, bufs, count, in_flags, out_flags, ec);
- // Check if operation succeeded.
- if (bytes >= 0)
- return bytes;
- // Operation failed.
- if ((state & user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for socket to become ready.
- if (socket_ops::poll_read(s, 0, -1, ec) < 0)
- return 0;
- }
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_recvmsg(
- const weak_cancel_token_type& cancel_token,
- boost::system::error_code& ec)
- {
- // Map non-portable errors to their portable counterparts.
- if (ec.value() == ERROR_NETNAME_DELETED)
- {
- if (cancel_token.expired())
- ec = boost::asio::error::operation_aborted;
- else
- ec = boost::asio::error::connection_reset;
- }
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- {
- ec = boost::asio::error::connection_refused;
- }
- else if (ec.value() == WSAEMSGSIZE || ec.value() == ERROR_MORE_DATA)
- {
- ec.assign(0, ec.category());
- }
- }
- #else // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_recvmsg(socket_type s,
- buf* bufs, size_t count, int in_flags, int& out_flags,
- boost::system::error_code& ec, size_t& bytes_transferred)
- {
- for (;;)
- {
- // Read some data.
- signed_size_type bytes = socket_ops::recvmsg(
- s, bufs, count, in_flags, out_flags, ec);
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- // Operation is complete.
- if (bytes >= 0)
- {
- ec = boost::system::error_code();
- bytes_transferred = bytes;
- }
- else
- bytes_transferred = 0;
- return true;
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- signed_size_type send(socket_type s, const buf* bufs, size_t count,
- int flags, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- // Send the data.
- DWORD send_buf_count = static_cast<DWORD>(count);
- DWORD bytes_transferred = 0;
- DWORD send_flags = flags;
- int result = error_wrapper(::WSASend(s, const_cast<buf*>(bufs),
- send_buf_count, &bytes_transferred, send_flags, 0, 0), ec);
- if (ec.value() == ERROR_NETNAME_DELETED)
- ec = boost::asio::error::connection_reset;
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- ec = boost::asio::error::connection_refused;
- if (result != 0)
- return socket_error_retval;
- ec = boost::system::error_code();
- return bytes_transferred;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- msghdr msg = msghdr();
- msg.msg_iov = const_cast<buf*>(bufs);
- msg.msg_iovlen = static_cast<int>(count);
- #if defined(__linux__)
- flags |= MSG_NOSIGNAL;
- #endif // defined(__linux__)
- signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- size_t sync_send(socket_type s, state_type state, const buf* bufs,
- size_t count, int flags, bool all_empty, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // A request to write 0 bytes to a stream is a no-op.
- if (all_empty && (state & stream_oriented))
- {
- ec = boost::system::error_code();
- return 0;
- }
- // Read some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
- // Check if operation succeeded.
- if (bytes >= 0)
- return bytes;
- // Operation failed.
- if ((state & user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for socket to become ready.
- if (socket_ops::poll_write(s, 0, -1, ec) < 0)
- return 0;
- }
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- void complete_iocp_send(
- const weak_cancel_token_type& cancel_token,
- boost::system::error_code& ec)
- {
- // Map non-portable errors to their portable counterparts.
- if (ec.value() == ERROR_NETNAME_DELETED)
- {
- if (cancel_token.expired())
- ec = boost::asio::error::operation_aborted;
- else
- ec = boost::asio::error::connection_reset;
- }
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- {
- ec = boost::asio::error::connection_refused;
- }
- }
- #else // defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_send(socket_type s,
- const buf* bufs, size_t count, int flags,
- boost::system::error_code& ec, size_t& bytes_transferred)
- {
- for (;;)
- {
- // Write some data.
- signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- // Operation is complete.
- if (bytes >= 0)
- {
- ec = boost::system::error_code();
- bytes_transferred = bytes;
- }
- else
- bytes_transferred = 0;
- return true;
- }
- }
- #endif // defined(BOOST_ASIO_HAS_IOCP)
- signed_size_type sendto(socket_type s, const buf* bufs, size_t count,
- int flags, const socket_addr_type* addr, std::size_t addrlen,
- boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- // Send the data.
- DWORD send_buf_count = static_cast<DWORD>(count);
- DWORD bytes_transferred = 0;
- int result = error_wrapper(::WSASendTo(s, const_cast<buf*>(bufs),
- send_buf_count, &bytes_transferred, flags, addr,
- static_cast<int>(addrlen), 0, 0), ec);
- if (ec.value() == ERROR_NETNAME_DELETED)
- ec = boost::asio::error::connection_reset;
- else if (ec.value() == ERROR_PORT_UNREACHABLE)
- ec = boost::asio::error::connection_refused;
- if (result != 0)
- return socket_error_retval;
- ec = boost::system::error_code();
- return bytes_transferred;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- msghdr msg = msghdr();
- init_msghdr_msg_name(msg.msg_name, addr);
- msg.msg_namelen = static_cast<int>(addrlen);
- msg.msg_iov = const_cast<buf*>(bufs);
- msg.msg_iovlen = static_cast<int>(count);
- #if defined(__linux__)
- flags |= MSG_NOSIGNAL;
- #endif // defined(__linux__)
- signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- size_t sync_sendto(socket_type s, state_type state, const buf* bufs,
- size_t count, int flags, const socket_addr_type* addr,
- std::size_t addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Write some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- signed_size_type bytes = socket_ops::sendto(
- s, bufs, count, flags, addr, addrlen, ec);
- // Check if operation succeeded.
- if (bytes >= 0)
- return bytes;
- // Operation failed.
- if ((state & user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for socket to become ready.
- if (socket_ops::poll_write(s, 0, -1, ec) < 0)
- return 0;
- }
- }
- #if !defined(BOOST_ASIO_HAS_IOCP)
- bool non_blocking_sendto(socket_type s,
- const buf* bufs, size_t count, int flags,
- const socket_addr_type* addr, std::size_t addrlen,
- boost::system::error_code& ec, size_t& bytes_transferred)
- {
- for (;;)
- {
- // Write some data.
- signed_size_type bytes = socket_ops::sendto(
- s, bufs, count, flags, addr, addrlen, ec);
- // Retry operation if interrupted by signal.
- if (ec == boost::asio::error::interrupted)
- continue;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- // Operation is complete.
- if (bytes >= 0)
- {
- ec = boost::system::error_code();
- bytes_transferred = bytes;
- }
- else
- bytes_transferred = 0;
- return true;
- }
- }
- #endif // !defined(BOOST_ASIO_HAS_IOCP)
- socket_type socket(int af, int type, int protocol,
- boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- socket_type s = error_wrapper(::WSASocketW(af, type, protocol, 0, 0,
- WSA_FLAG_OVERLAPPED), ec);
- if (s == invalid_socket)
- return s;
- if (af == BOOST_ASIO_OS_DEF(AF_INET6))
- {
- // Try to enable the POSIX default behaviour of having IPV6_V6ONLY set to
- // false. This will only succeed on Windows Vista and later versions of
- // Windows, where a dual-stack IPv4/v6 implementation is available.
- DWORD optval = 0;
- ::setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
- reinterpret_cast<const char*>(&optval), sizeof(optval));
- }
- ec = boost::system::error_code();
- return s;
- #elif defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
- socket_type s = error_wrapper(::socket(af, type, protocol), ec);
- if (s == invalid_socket)
- return s;
- int optval = 1;
- int result = error_wrapper(::setsockopt(s,
- SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
- if (result != 0)
- {
- ::close(s);
- return invalid_socket;
- }
- return s;
- #else
- int s = error_wrapper(::socket(af, type, protocol), ec);
- if (s >= 0)
- ec = boost::system::error_code();
- return s;
- #endif
- }
- template <typename SockLenType>
- inline int call_setsockopt(SockLenType msghdr::*,
- socket_type s, int level, int optname,
- const void* optval, std::size_t optlen)
- {
- return ::setsockopt(s, level, optname,
- (const char*)optval, (SockLenType)optlen);
- }
- int setsockopt(socket_type s, state_type& state, int level, int optname,
- const void* optval, std::size_t optlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- if (level == custom_socket_option_level && optname == always_fail_option)
- {
- ec = boost::asio::error::invalid_argument;
- return socket_error_retval;
- }
- if (level == custom_socket_option_level
- && optname == enable_connection_aborted_option)
- {
- if (optlen != sizeof(int))
- {
- ec = boost::asio::error::invalid_argument;
- return socket_error_retval;
- }
- if (*static_cast<const int*>(optval))
- state |= enable_connection_aborted;
- else
- state &= ~enable_connection_aborted;
- ec = boost::system::error_code();
- return 0;
- }
- if (level == SOL_SOCKET && optname == SO_LINGER)
- state |= user_set_linger;
- #if defined(__BORLANDC__)
- // Mysteriously, using the getsockopt and setsockopt functions directly with
- // Borland C++ results in incorrect values being set and read. The bug can be
- // worked around by using function addresses resolved with GetProcAddress.
- if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
- {
- typedef int (WSAAPI *sso_t)(SOCKET, int, int, const char*, int);
- if (sso_t sso = (sso_t)::GetProcAddress(winsock_module, "setsockopt"))
- {
- clear_last_error();
- return error_wrapper(sso(s, level, optname,
- reinterpret_cast<const char*>(optval),
- static_cast<int>(optlen)), ec);
- }
- }
- ec = boost::asio::error::fault;
- return socket_error_retval;
- #else // defined(__BORLANDC__)
- clear_last_error();
- int result = error_wrapper(call_setsockopt(&msghdr::msg_namelen,
- s, level, optname, optval, optlen), ec);
- if (result == 0)
- {
- ec = boost::system::error_code();
- #if defined(__MACH__) && defined(__APPLE__) \
- || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
- // To implement portable behaviour for SO_REUSEADDR with UDP sockets we
- // need to also set SO_REUSEPORT on BSD-based platforms.
- if ((state & datagram_oriented)
- && level == SOL_SOCKET && optname == SO_REUSEADDR)
- {
- call_setsockopt(&msghdr::msg_namelen, s,
- SOL_SOCKET, SO_REUSEPORT, optval, optlen);
- }
- #endif
- }
- return result;
- #endif // defined(__BORLANDC__)
- }
- template <typename SockLenType>
- inline int call_getsockopt(SockLenType msghdr::*,
- socket_type s, int level, int optname,
- void* optval, std::size_t* optlen)
- {
- SockLenType tmp_optlen = (SockLenType)*optlen;
- int result = ::getsockopt(s, level, optname, (char*)optval, &tmp_optlen);
- *optlen = (std::size_t)tmp_optlen;
- return result;
- }
- int getsockopt(socket_type s, state_type state, int level, int optname,
- void* optval, size_t* optlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- if (level == custom_socket_option_level && optname == always_fail_option)
- {
- ec = boost::asio::error::invalid_argument;
- return socket_error_retval;
- }
- if (level == custom_socket_option_level
- && optname == enable_connection_aborted_option)
- {
- if (*optlen != sizeof(int))
- {
- ec = boost::asio::error::invalid_argument;
- return socket_error_retval;
- }
- *static_cast<int*>(optval) = (state & enable_connection_aborted) ? 1 : 0;
- ec = boost::system::error_code();
- return 0;
- }
- #if defined(__BORLANDC__)
- // Mysteriously, using the getsockopt and setsockopt functions directly with
- // Borland C++ results in incorrect values being set and read. The bug can be
- // worked around by using function addresses resolved with GetProcAddress.
- if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
- {
- typedef int (WSAAPI *gso_t)(SOCKET, int, int, char*, int*);
- if (gso_t gso = (gso_t)::GetProcAddress(winsock_module, "getsockopt"))
- {
- clear_last_error();
- int tmp_optlen = static_cast<int>(*optlen);
- int result = error_wrapper(gso(s, level, optname,
- reinterpret_cast<char*>(optval), &tmp_optlen), ec);
- *optlen = static_cast<size_t>(tmp_optlen);
- if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
- && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
- {
- // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are
- // only supported on Windows Vista and later. To simplify program logic
- // we will fake success of getting this option and specify that the
- // value is non-zero (i.e. true). This corresponds to the behavior of
- // IPv6 sockets on Windows platforms pre-Vista.
- *static_cast<DWORD*>(optval) = 1;
- ec = boost::system::error_code();
- }
- return result;
- }
- }
- ec = boost::asio::error::fault;
- return socket_error_retval;
- #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- clear_last_error();
- int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
- s, level, optname, optval, optlen), ec);
- if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
- && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
- {
- // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are only
- // supported on Windows Vista and later. To simplify program logic we will
- // fake success of getting this option and specify that the value is
- // non-zero (i.e. true). This corresponds to the behavior of IPv6 sockets
- // on Windows platforms pre-Vista.
- *static_cast<DWORD*>(optval) = 1;
- ec = boost::system::error_code();
- }
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- clear_last_error();
- int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
- s, level, optname, optval, optlen), ec);
- #if defined(__linux__)
- if (result == 0 && level == SOL_SOCKET && *optlen == sizeof(int)
- && (optname == SO_SNDBUF || optname == SO_RCVBUF))
- {
- // On Linux, setting SO_SNDBUF or SO_RCVBUF to N actually causes the kernel
- // to set the buffer size to N*2. Linux puts additional stuff into the
- // buffers so that only about half is actually available to the application.
- // The retrieved value is divided by 2 here to make it appear as though the
- // correct value has been set.
- *static_cast<int*>(optval) /= 2;
- }
- #endif // defined(__linux__)
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- template <typename SockLenType>
- inline int call_getpeername(SockLenType msghdr::*,
- socket_type s, socket_addr_type* addr, std::size_t* addrlen)
- {
- SockLenType tmp_addrlen = (SockLenType)*addrlen;
- int result = ::getpeername(s, addr, &tmp_addrlen);
- *addrlen = (std::size_t)tmp_addrlen;
- return result;
- }
- int getpeername(socket_type s, socket_addr_type* addr,
- std::size_t* addrlen, bool cached, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- #if defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP) \
- || defined(__CYGWIN__)
- if (cached)
- {
- // Check if socket is still connected.
- DWORD connect_time = 0;
- size_t connect_time_len = sizeof(connect_time);
- if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_CONNECT_TIME,
- &connect_time, &connect_time_len, ec) == socket_error_retval)
- {
- return socket_error_retval;
- }
- if (connect_time == 0xFFFFFFFF)
- {
- ec = boost::asio::error::not_connected;
- return socket_error_retval;
- }
- // The cached value is still valid.
- ec = boost::system::error_code();
- return 0;
- }
- #else // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
- // || defined(__CYGWIN__)
- (void)cached;
- #endif // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
- // || defined(__CYGWIN__)
- clear_last_error();
- int result = error_wrapper(call_getpeername(
- &msghdr::msg_namelen, s, addr, addrlen), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- template <typename SockLenType>
- inline int call_getsockname(SockLenType msghdr::*,
- socket_type s, socket_addr_type* addr, std::size_t* addrlen)
- {
- SockLenType tmp_addrlen = (SockLenType)*addrlen;
- int result = ::getsockname(s, addr, &tmp_addrlen);
- *addrlen = (std::size_t)tmp_addrlen;
- return result;
- }
- int getsockname(socket_type s, socket_addr_type* addr,
- std::size_t* addrlen, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- int result = error_wrapper(call_getsockname(
- &msghdr::msg_namelen, s, addr, addrlen), ec);
- if (result == 0)
- ec = boost::system::error_code();
- return result;
- }
- int ioctl(socket_type s, state_type& state, int cmd,
- ioctl_arg_type* arg, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- int result = error_wrapper(::ioctlsocket(s, cmd, arg), ec);
- #elif defined(__MACH__) && defined(__APPLE__) \
- || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
- int result = error_wrapper(::ioctl(s,
- static_cast<unsigned int>(cmd), arg), ec);
- #else
- int result = error_wrapper(::ioctl(s, cmd, arg), ec);
- #endif
- if (result >= 0)
- {
- ec = boost::system::error_code();
- // When updating the non-blocking mode we always perform the ioctl syscall,
- // even if the flags would otherwise indicate that the socket is already in
- // the correct state. This ensures that the underlying socket is put into
- // the state that has been requested by the user. If the ioctl syscall was
- // successful then we need to update the flags to match.
- if (cmd == static_cast<int>(FIONBIO))
- {
- if (*arg)
- {
- state |= user_set_non_blocking;
- }
- else
- {
- // Clearing the non-blocking mode always overrides any internally-set
- // non-blocking flag. Any subsequent asynchronous operations will need
- // to re-enable non-blocking I/O.
- state &= ~(user_set_non_blocking | internal_non_blocking);
- }
- }
- }
- return result;
- }
- int select(int nfds, fd_set* readfds, fd_set* writefds,
- fd_set* exceptfds, timeval* timeout, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- if (!readfds && !writefds && !exceptfds && timeout)
- {
- DWORD milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
- if (milliseconds == 0)
- milliseconds = 1; // Force context switch.
- ::Sleep(milliseconds);
- ec = boost::system::error_code();
- return 0;
- }
- // The select() call allows timeout values measured in microseconds, but the
- // system clock (as wrapped by boost::posix_time::microsec_clock) typically
- // has a resolution of 10 milliseconds. This can lead to a spinning select
- // reactor, meaning increased CPU usage, when waiting for the earliest
- // scheduled timeout if it's less than 10 milliseconds away. To avoid a tight
- // spin we'll use a minimum timeout of 1 millisecond.
- if (timeout && timeout->tv_sec == 0
- && timeout->tv_usec > 0 && timeout->tv_usec < 1000)
- timeout->tv_usec = 1000;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- #if defined(__hpux) && defined(__SELECT)
- timespec ts;
- ts.tv_sec = timeout ? timeout->tv_sec : 0;
- ts.tv_nsec = timeout ? timeout->tv_usec * 1000 : 0;
- return error_wrapper(::pselect(nfds, readfds,
- writefds, exceptfds, timeout ? &ts : 0, 0), ec);
- #else
- int result = error_wrapper(::select(nfds, readfds,
- writefds, exceptfds, timeout), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif
- }
- int poll_read(socket_type s, state_type state,
- int msec, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- #if defined(BOOST_ASIO_WINDOWS) \
- || defined(__CYGWIN__) \
- || defined(__SYMBIAN32__)
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(s, &fds);
- timeval timeout_obj;
- timeval* timeout;
- if (state & user_set_non_blocking)
- {
- timeout_obj.tv_sec = 0;
- timeout_obj.tv_usec = 0;
- timeout = &timeout_obj;
- }
- else if (msec >= 0)
- {
- timeout_obj.tv_sec = msec / 1000;
- timeout_obj.tv_usec = (msec % 1000) * 1000;
- timeout = &timeout_obj;
- }
- else
- timeout = 0;
- clear_last_error();
- int result = error_wrapper(::select(s + 1, &fds, 0, 0, timeout), ec);
- #else // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- pollfd fds;
- fds.fd = s;
- fds.events = POLLIN;
- fds.revents = 0;
- int timeout = (state & user_set_non_blocking) ? 0 : msec;
- clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, timeout), ec);
- #endif // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- if (result == 0)
- ec = (state & user_set_non_blocking)
- ? boost::asio::error::would_block : boost::system::error_code();
- else if (result > 0)
- ec = boost::system::error_code();
- return result;
- }
- int poll_write(socket_type s, state_type state,
- int msec, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- #if defined(BOOST_ASIO_WINDOWS) \
- || defined(__CYGWIN__) \
- || defined(__SYMBIAN32__)
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(s, &fds);
- timeval timeout_obj;
- timeval* timeout;
- if (state & user_set_non_blocking)
- {
- timeout_obj.tv_sec = 0;
- timeout_obj.tv_usec = 0;
- timeout = &timeout_obj;
- }
- else if (msec >= 0)
- {
- timeout_obj.tv_sec = msec / 1000;
- timeout_obj.tv_usec = (msec % 1000) * 1000;
- timeout = &timeout_obj;
- }
- else
- timeout = 0;
- clear_last_error();
- int result = error_wrapper(::select(s + 1, 0, &fds, 0, timeout), ec);
- #else // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- pollfd fds;
- fds.fd = s;
- fds.events = POLLOUT;
- fds.revents = 0;
- int timeout = (state & user_set_non_blocking) ? 0 : msec;
- clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, timeout), ec);
- #endif // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- if (result == 0)
- ec = (state & user_set_non_blocking)
- ? boost::asio::error::would_block : boost::system::error_code();
- else if (result > 0)
- ec = boost::system::error_code();
- return result;
- }
- int poll_error(socket_type s, state_type state,
- int msec, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- #if defined(BOOST_ASIO_WINDOWS) \
- || defined(__CYGWIN__) \
- || defined(__SYMBIAN32__)
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(s, &fds);
- timeval timeout_obj;
- timeval* timeout;
- if (state & user_set_non_blocking)
- {
- timeout_obj.tv_sec = 0;
- timeout_obj.tv_usec = 0;
- timeout = &timeout_obj;
- }
- else if (msec >= 0)
- {
- timeout_obj.tv_sec = msec / 1000;
- timeout_obj.tv_usec = (msec % 1000) * 1000;
- timeout = &timeout_obj;
- }
- else
- timeout = 0;
- clear_last_error();
- int result = error_wrapper(::select(s + 1, 0, 0, &fds, timeout), ec);
- #else // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- pollfd fds;
- fds.fd = s;
- fds.events = POLLPRI | POLLERR | POLLHUP;
- fds.revents = 0;
- int timeout = (state & user_set_non_blocking) ? 0 : msec;
- clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, timeout), ec);
- #endif // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- if (result == 0)
- ec = (state & user_set_non_blocking)
- ? boost::asio::error::would_block : boost::system::error_code();
- else if (result > 0)
- ec = boost::system::error_code();
- return result;
- }
- int poll_connect(socket_type s, int msec, boost::system::error_code& ec)
- {
- if (s == invalid_socket)
- {
- ec = boost::asio::error::bad_descriptor;
- return socket_error_retval;
- }
- #if defined(BOOST_ASIO_WINDOWS) \
- || defined(__CYGWIN__) \
- || defined(__SYMBIAN32__)
- fd_set write_fds;
- FD_ZERO(&write_fds);
- FD_SET(s, &write_fds);
- fd_set except_fds;
- FD_ZERO(&except_fds);
- FD_SET(s, &except_fds);
- timeval timeout_obj;
- timeval* timeout;
- if (msec >= 0)
- {
- timeout_obj.tv_sec = msec / 1000;
- timeout_obj.tv_usec = (msec % 1000) * 1000;
- timeout = &timeout_obj;
- }
- else
- timeout = 0;
- clear_last_error();
- int result = error_wrapper(::select(
- s + 1, 0, &write_fds, &except_fds, timeout), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #else // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- pollfd fds;
- fds.fd = s;
- fds.events = POLLOUT;
- fds.revents = 0;
- clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, msec), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS)
- // || defined(__CYGWIN__)
- // || defined(__SYMBIAN32__)
- }
- #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- const char* inet_ntop(int af, const void* src, char* dest, size_t length,
- unsigned long scope_id, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- using namespace std; // For sprintf.
- const unsigned char* bytes = static_cast<const unsigned char*>(src);
- if (af == BOOST_ASIO_OS_DEF(AF_INET))
- {
- sprintf_s(dest, length, "%u.%u.%u.%u",
- bytes[0], bytes[1], bytes[2], bytes[3]);
- return dest;
- }
- else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
- {
- size_t n = 0, b = 0, z = 0;
- while (n < length && b < 16)
- {
- if (bytes[b] == 0 && bytes[b + 1] == 0 && z == 0)
- {
- do b += 2; while (b < 16 && bytes[b] == 0 && bytes[b + 1] == 0);
- n += sprintf_s(dest + n, length - n, ":%s", b < 16 ? "" : ":"), ++z;
- }
- else
- {
- n += sprintf_s(dest + n, length - n, "%s%x", b ? ":" : "",
- (static_cast<u_long_type>(bytes[b]) << 8) | bytes[b + 1]);
- b += 2;
- }
- }
- if (scope_id)
- n += sprintf_s(dest + n, length - n, "%%%lu", scope_id);
- return dest;
- }
- else
- {
- ec = boost::asio::error::address_family_not_supported;
- return 0;
- }
- #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- using namespace std; // For memcpy.
- if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
- {
- ec = boost::asio::error::address_family_not_supported;
- return 0;
- }
- union
- {
- socket_addr_type base;
- sockaddr_storage_type storage;
- sockaddr_in4_type v4;
- sockaddr_in6_type v6;
- } address;
- DWORD address_length;
- if (af == BOOST_ASIO_OS_DEF(AF_INET))
- {
- address_length = sizeof(sockaddr_in4_type);
- address.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
- address.v4.sin_port = 0;
- memcpy(&address.v4.sin_addr, src, sizeof(in4_addr_type));
- }
- else // AF_INET6
- {
- address_length = sizeof(sockaddr_in6_type);
- address.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
- address.v6.sin6_port = 0;
- address.v6.sin6_flowinfo = 0;
- address.v6.sin6_scope_id = scope_id;
- memcpy(&address.v6.sin6_addr, src, sizeof(in6_addr_type));
- }
- DWORD string_length = static_cast<DWORD>(length);
- #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
- LPWSTR string_buffer = (LPWSTR)_alloca(length * sizeof(WCHAR));
- int result = error_wrapper(::WSAAddressToStringW(&address.base,
- address_length, 0, string_buffer, &string_length), ec);
- ::WideCharToMultiByte(CP_ACP, 0, string_buffer, -1,
- dest, static_cast<int>(length), 0, 0);
- #else
- int result = error_wrapper(::WSAAddressToStringA(
- &address.base, address_length, 0, dest, &string_length), ec);
- #endif
- // Windows may set error code on success.
- if (result != socket_error_retval)
- ec = boost::system::error_code();
- // Windows may not set an error code on failure.
- else if (result == socket_error_retval && !ec)
- ec = boost::asio::error::invalid_argument;
- return result == socket_error_retval ? 0 : dest;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- const char* result = error_wrapper(::inet_ntop(
- af, src, dest, static_cast<int>(length)), ec);
- if (result == 0 && !ec)
- ec = boost::asio::error::invalid_argument;
- if (result != 0 && af == BOOST_ASIO_OS_DEF(AF_INET6) && scope_id != 0)
- {
- using namespace std; // For strcat and sprintf.
- char if_name[IF_NAMESIZE + 1] = "%";
- const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
- bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
- && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
- bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
- && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
- if ((!is_link_local && !is_multicast_link_local)
- || if_indextoname(static_cast<unsigned>(scope_id), if_name + 1) == 0)
- sprintf(if_name + 1, "%lu", scope_id);
- strcat(dest, if_name);
- }
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- int inet_pton(int af, const char* src, void* dest,
- unsigned long* scope_id, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- using namespace std; // For sscanf.
- unsigned char* bytes = static_cast<unsigned char*>(dest);
- if (af == BOOST_ASIO_OS_DEF(AF_INET))
- {
- unsigned int b0, b1, b2, b3;
- if (sscanf_s(src, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) != 4)
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- if (b0 > 255 || b1 > 255 || b2 > 255 || b3 > 255)
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- bytes[0] = static_cast<unsigned char>(b0);
- bytes[1] = static_cast<unsigned char>(b1);
- bytes[2] = static_cast<unsigned char>(b2);
- bytes[3] = static_cast<unsigned char>(b3);
- ec = boost::system::error_code();
- return 1;
- }
- else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
- {
- unsigned char* bytes = static_cast<unsigned char*>(dest);
- std::memset(bytes, 0, 16);
- unsigned char back_bytes[16] = { 0 };
- int num_front_bytes = 0, num_back_bytes = 0;
- const char* p = src;
- enum { fword, fcolon, bword, scope, done } state = fword;
- unsigned long current_word = 0;
- while (state != done)
- {
- if (current_word > 0xFFFF)
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- switch (state)
- {
- case fword:
- if (*p >= '0' && *p <= '9')
- current_word = current_word * 16 + *p++ - '0';
- else if (*p >= 'a' && *p <= 'f')
- current_word = current_word * 16 + *p++ - 'a' + 10;
- else if (*p >= 'A' && *p <= 'F')
- current_word = current_word * 16 + *p++ - 'A' + 10;
- else
- {
- if (num_front_bytes == 16)
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- bytes[num_front_bytes++] = (current_word >> 8) & 0xFF;
- bytes[num_front_bytes++] = current_word & 0xFF;
- current_word = 0;
- if (*p == ':')
- state = fcolon, ++p;
- else if (*p == '%')
- state = scope, ++p;
- else if (*p == 0)
- state = done;
- else
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- }
- break;
- case fcolon:
- if (*p == ':')
- state = bword, ++p;
- else
- state = fword;
- break;
- case bword:
- if (*p >= '0' && *p <= '9')
- current_word = current_word * 16 + *p++ - '0';
- else if (*p >= 'a' && *p <= 'f')
- current_word = current_word * 16 + *p++ - 'a' + 10;
- else if (*p >= 'A' && *p <= 'F')
- current_word = current_word * 16 + *p++ - 'A' + 10;
- else
- {
- if (num_front_bytes + num_back_bytes == 16)
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- back_bytes[num_back_bytes++] = (current_word >> 8) & 0xFF;
- back_bytes[num_back_bytes++] = current_word & 0xFF;
- current_word = 0;
- if (*p == ':')
- state = bword, ++p;
- else if (*p == '%')
- state = scope, ++p;
- else if (*p == 0)
- state = done;
- else
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- }
- break;
- case scope:
- if (*p >= '0' && *p <= '9')
- current_word = current_word * 10 + *p++ - '0';
- else if (*p == 0)
- *scope_id = current_word, state = done;
- else
- {
- ec = boost::asio::error::invalid_argument;
- return -1;
- }
- break;
- default:
- break;
- }
- }
- for (int i = 0; i < num_back_bytes; ++i)
- bytes[16 - num_back_bytes + i] = back_bytes[i];
- ec = boost::system::error_code();
- return 1;
- }
- else
- {
- ec = boost::asio::error::address_family_not_supported;
- return -1;
- }
- #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- using namespace std; // For memcpy and strcmp.
- if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
- {
- ec = boost::asio::error::address_family_not_supported;
- return -1;
- }
- union
- {
- socket_addr_type base;
- sockaddr_storage_type storage;
- sockaddr_in4_type v4;
- sockaddr_in6_type v6;
- } address;
- int address_length = sizeof(sockaddr_storage_type);
- #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
- int num_wide_chars = static_cast<int>(strlen(src)) + 1;
- LPWSTR wide_buffer = (LPWSTR)_alloca(num_wide_chars * sizeof(WCHAR));
- ::MultiByteToWideChar(CP_ACP, 0, src, -1, wide_buffer, num_wide_chars);
- int result = error_wrapper(::WSAStringToAddressW(
- wide_buffer, af, 0, &address.base, &address_length), ec);
- #else
- int result = error_wrapper(::WSAStringToAddressA(
- const_cast<char*>(src), af, 0, &address.base, &address_length), ec);
- #endif
- if (af == BOOST_ASIO_OS_DEF(AF_INET))
- {
- if (result != socket_error_retval)
- {
- memcpy(dest, &address.v4.sin_addr, sizeof(in4_addr_type));
- ec = boost::system::error_code();
- }
- else if (strcmp(src, "255.255.255.255") == 0)
- {
- static_cast<in4_addr_type*>(dest)->s_addr = INADDR_NONE;
- ec = boost::system::error_code();
- }
- }
- else // AF_INET6
- {
- if (result != socket_error_retval)
- {
- memcpy(dest, &address.v6.sin6_addr, sizeof(in6_addr_type));
- if (scope_id)
- *scope_id = address.v6.sin6_scope_id;
- ec = boost::system::error_code();
- }
- }
- // Windows may not set an error code on failure.
- if (result == socket_error_retval && !ec)
- ec = boost::asio::error::invalid_argument;
- if (result != socket_error_retval)
- ec = boost::system::error_code();
- return result == socket_error_retval ? -1 : 1;
- #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- using namespace std; // For strchr, memcpy and atoi.
- // On some platforms, inet_pton fails if an address string contains a scope
- // id. Detect and remove the scope id before passing the string to inet_pton.
- const bool is_v6 = (af == BOOST_ASIO_OS_DEF(AF_INET6));
- const char* if_name = is_v6 ? strchr(src, '%') : 0;
- char src_buf[max_addr_v6_str_len + 1];
- const char* src_ptr = src;
- if (if_name != 0)
- {
- if (if_name - src > max_addr_v6_str_len)
- {
- ec = boost::asio::error::invalid_argument;
- return 0;
- }
- memcpy(src_buf, src, if_name - src);
- src_buf[if_name - src] = 0;
- src_ptr = src_buf;
- }
- int result = error_wrapper(::inet_pton(af, src_ptr, dest), ec);
- if (result <= 0 && !ec)
- ec = boost::asio::error::invalid_argument;
- if (result > 0 && is_v6 && scope_id)
- {
- using namespace std; // For strchr and atoi.
- *scope_id = 0;
- if (if_name != 0)
- {
- in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
- bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
- && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
- bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
- && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
- if (is_link_local || is_multicast_link_local)
- *scope_id = if_nametoindex(if_name + 1);
- if (*scope_id == 0)
- *scope_id = atoi(if_name + 1);
- }
- }
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- }
- int gethostname(char* name, int namelen, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- try
- {
- using namespace Windows::Foundation::Collections;
- using namespace Windows::Networking;
- using namespace Windows::Networking::Connectivity;
- IVectorView<HostName^>^ hostnames = NetworkInformation::GetHostNames();
- for (unsigned i = 0; i < hostnames->Size; ++i)
- {
- HostName^ hostname = hostnames->GetAt(i);
- if (hostname->Type == HostNameType::DomainName)
- {
- std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
- std::string raw_name = converter.to_bytes(hostname->RawName->Data());
- if (namelen > 0 && raw_name.size() < static_cast<std::size_t>(namelen))
- {
- strcpy_s(name, namelen, raw_name.c_str());
- return 0;
- }
- }
- }
- return -1;
- }
- catch (Platform::Exception^ e)
- {
- ec = boost::system::error_code(e->HResult,
- boost::system::system_category());
- return -1;
- }
- #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- int result = error_wrapper(::gethostname(name, namelen), ec);
- # if defined(BOOST_ASIO_WINDOWS)
- if (result == 0)
- ec = boost::system::error_code();
- # endif // defined(BOOST_ASIO_WINDOWS)
- return result;
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- }
- #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- #if !defined(BOOST_ASIO_HAS_GETADDRINFO)
- // The following functions are only needed for emulation of getaddrinfo and
- // getnameinfo.
- inline boost::system::error_code translate_netdb_error(int error)
- {
- switch (error)
- {
- case 0:
- return boost::system::error_code();
- case HOST_NOT_FOUND:
- return boost::asio::error::host_not_found;
- case TRY_AGAIN:
- return boost::asio::error::host_not_found_try_again;
- case NO_RECOVERY:
- return boost::asio::error::no_recovery;
- case NO_DATA:
- return boost::asio::error::no_data;
- default:
- BOOST_ASIO_ASSERT(false);
- return boost::asio::error::invalid_argument;
- }
- }
- inline hostent* gethostbyaddr(const char* addr, int length, int af,
- hostent* result, char* buffer, int buflength, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- (void)(buffer);
- (void)(buflength);
- hostent* retval = error_wrapper(::gethostbyaddr(addr, length, af), ec);
- if (!retval)
- return 0;
- ec = boost::system::error_code();
- *result = *retval;
- return retval;
- #elif defined(__sun) || defined(__QNX__)
- int error = 0;
- hostent* retval = error_wrapper(::gethostbyaddr_r(addr, length, af, result,
- buffer, buflength, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- return retval;
- #elif defined(__MACH__) && defined(__APPLE__)
- (void)(buffer);
- (void)(buflength);
- int error = 0;
- hostent* retval = error_wrapper(::getipnodebyaddr(
- addr, length, af, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- if (!retval)
- return 0;
- *result = *retval;
- return retval;
- #else
- hostent* retval = 0;
- int error = 0;
- error_wrapper(::gethostbyaddr_r(addr, length, af, result, buffer,
- buflength, &retval, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- return retval;
- #endif
- }
- inline hostent* gethostbyname(const char* name, int af, struct hostent* result,
- char* buffer, int buflength, int ai_flags, boost::system::error_code& ec)
- {
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- (void)(buffer);
- (void)(buflength);
- (void)(ai_flags);
- if (af != BOOST_ASIO_OS_DEF(AF_INET))
- {
- ec = boost::asio::error::address_family_not_supported;
- return 0;
- }
- hostent* retval = error_wrapper(::gethostbyname(name), ec);
- if (!retval)
- return 0;
- ec = boost::system::error_code();
- *result = *retval;
- return result;
- #elif defined(__sun) || defined(__QNX__)
- (void)(ai_flags);
- if (af != BOOST_ASIO_OS_DEF(AF_INET))
- {
- ec = boost::asio::error::address_family_not_supported;
- return 0;
- }
- int error = 0;
- hostent* retval = error_wrapper(::gethostbyname_r(name, result, buffer,
- buflength, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- return retval;
- #elif defined(__MACH__) && defined(__APPLE__)
- (void)(buffer);
- (void)(buflength);
- int error = 0;
- hostent* retval = error_wrapper(::getipnodebyname(
- name, af, ai_flags, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- if (!retval)
- return 0;
- *result = *retval;
- return retval;
- #else
- (void)(ai_flags);
- if (af != BOOST_ASIO_OS_DEF(AF_INET))
- {
- ec = boost::asio::error::address_family_not_supported;
- return 0;
- }
- hostent* retval = 0;
- int error = 0;
- error_wrapper(::gethostbyname_r(name, result,
- buffer, buflength, &retval, &error), ec);
- if (error)
- ec = translate_netdb_error(error);
- return retval;
- #endif
- }
- inline void freehostent(hostent* h)
- {
- #if defined(__MACH__) && defined(__APPLE__)
- if (h)
- ::freehostent(h);
- #else
- (void)(h);
- #endif
- }
- // Emulation of getaddrinfo based on implementation in:
- // Stevens, W. R., UNIX Network Programming Vol. 1, 2nd Ed., Prentice-Hall 1998.
- struct gai_search
- {
- const char* host;
- int family;
- };
- inline int gai_nsearch(const char* host,
- const addrinfo_type* hints, gai_search (&search)[2])
- {
- int search_count = 0;
- if (host == 0 || host[0] == '\0')
- {
- if (hints->ai_flags & AI_PASSIVE)
- {
- // No host and AI_PASSIVE implies wildcard bind.
- switch (hints->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- search[search_count].host = "0.0.0.0";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_INET6):
- search[search_count].host = "0::0";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_UNSPEC):
- search[search_count].host = "0::0";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- search[search_count].host = "0.0.0.0";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- default:
- break;
- }
- }
- else
- {
- // No host and not AI_PASSIVE means connect to local host.
- switch (hints->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- search[search_count].host = "localhost";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_INET6):
- search[search_count].host = "localhost";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_UNSPEC):
- search[search_count].host = "localhost";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- search[search_count].host = "localhost";
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- default:
- break;
- }
- }
- }
- else
- {
- // Host is specified.
- switch (hints->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- search[search_count].host = host;
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_INET6):
- search[search_count].host = host;
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- break;
- case BOOST_ASIO_OS_DEF(AF_UNSPEC):
- search[search_count].host = host;
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
- ++search_count;
- search[search_count].host = host;
- search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
- ++search_count;
- break;
- default:
- break;
- }
- }
- return search_count;
- }
- template <typename T>
- inline T* gai_alloc(std::size_t size = sizeof(T))
- {
- using namespace std;
- T* p = static_cast<T*>(::operator new(size, std::nothrow));
- if (p)
- memset(p, 0, size);
- return p;
- }
- inline void gai_free(void* p)
- {
- ::operator delete(p);
- }
- inline void gai_strcpy(char* target, const char* source, std::size_t max_size)
- {
- using namespace std;
- #if defined(BOOST_ASIO_HAS_SECURE_RTL)
- strcpy_s(target, max_size, source);
- #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
- *target = 0;
- if (max_size > 0)
- strncat(target, source, max_size - 1);
- #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
- }
- enum { gai_clone_flag = 1 << 30 };
- inline int gai_aistruct(addrinfo_type*** next, const addrinfo_type* hints,
- const void* addr, int family)
- {
- using namespace std;
- addrinfo_type* ai = gai_alloc<addrinfo_type>();
- if (ai == 0)
- return EAI_MEMORY;
- ai->ai_next = 0;
- **next = ai;
- *next = &ai->ai_next;
- ai->ai_canonname = 0;
- ai->ai_socktype = hints->ai_socktype;
- if (ai->ai_socktype == 0)
- ai->ai_flags |= gai_clone_flag;
- ai->ai_protocol = hints->ai_protocol;
- ai->ai_family = family;
- switch (ai->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- {
- sockaddr_in4_type* sinptr = gai_alloc<sockaddr_in4_type>();
- if (sinptr == 0)
- return EAI_MEMORY;
- sinptr->sin_family = BOOST_ASIO_OS_DEF(AF_INET);
- memcpy(&sinptr->sin_addr, addr, sizeof(in4_addr_type));
- ai->ai_addr = reinterpret_cast<sockaddr*>(sinptr);
- ai->ai_addrlen = sizeof(sockaddr_in4_type);
- break;
- }
- case BOOST_ASIO_OS_DEF(AF_INET6):
- {
- sockaddr_in6_type* sin6ptr = gai_alloc<sockaddr_in6_type>();
- if (sin6ptr == 0)
- return EAI_MEMORY;
- sin6ptr->sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
- memcpy(&sin6ptr->sin6_addr, addr, sizeof(in6_addr_type));
- ai->ai_addr = reinterpret_cast<sockaddr*>(sin6ptr);
- ai->ai_addrlen = sizeof(sockaddr_in6_type);
- break;
- }
- default:
- break;
- }
- return 0;
- }
- inline addrinfo_type* gai_clone(addrinfo_type* ai)
- {
- using namespace std;
- addrinfo_type* new_ai = gai_alloc<addrinfo_type>();
- if (new_ai == 0)
- return new_ai;
- new_ai->ai_next = ai->ai_next;
- ai->ai_next = new_ai;
- new_ai->ai_flags = 0;
- new_ai->ai_family = ai->ai_family;
- new_ai->ai_socktype = ai->ai_socktype;
- new_ai->ai_protocol = ai->ai_protocol;
- new_ai->ai_canonname = 0;
- new_ai->ai_addrlen = ai->ai_addrlen;
- new_ai->ai_addr = gai_alloc<sockaddr>(ai->ai_addrlen);
- memcpy(new_ai->ai_addr, ai->ai_addr, ai->ai_addrlen);
- return new_ai;
- }
- inline int gai_port(addrinfo_type* aihead, int port, int socktype)
- {
- int num_found = 0;
- for (addrinfo_type* ai = aihead; ai; ai = ai->ai_next)
- {
- if (ai->ai_flags & gai_clone_flag)
- {
- if (ai->ai_socktype != 0)
- {
- ai = gai_clone(ai);
- if (ai == 0)
- return -1;
- // ai now points to newly cloned entry.
- }
- }
- else if (ai->ai_socktype != socktype)
- {
- // Ignore if mismatch on socket type.
- continue;
- }
- ai->ai_socktype = socktype;
- switch (ai->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- {
- sockaddr_in4_type* sinptr =
- reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
- sinptr->sin_port = port;
- ++num_found;
- break;
- }
- case BOOST_ASIO_OS_DEF(AF_INET6):
- {
- sockaddr_in6_type* sin6ptr =
- reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
- sin6ptr->sin6_port = port;
- ++num_found;
- break;
- }
- default:
- break;
- }
- }
- return num_found;
- }
- inline int gai_serv(addrinfo_type* aihead,
- const addrinfo_type* hints, const char* serv)
- {
- using namespace std;
- int num_found = 0;
- if (
- #if defined(AI_NUMERICSERV)
- (hints->ai_flags & AI_NUMERICSERV) ||
- #endif
- isdigit(static_cast<unsigned char>(serv[0])))
- {
- int port = htons(atoi(serv));
- if (hints->ai_socktype)
- {
- // Caller specifies socket type.
- int rc = gai_port(aihead, port, hints->ai_socktype);
- if (rc < 0)
- return EAI_MEMORY;
- num_found += rc;
- }
- else
- {
- // Caller does not specify socket type.
- int rc = gai_port(aihead, port, SOCK_STREAM);
- if (rc < 0)
- return EAI_MEMORY;
- num_found += rc;
- rc = gai_port(aihead, port, SOCK_DGRAM);
- if (rc < 0)
- return EAI_MEMORY;
- num_found += rc;
- }
- }
- else
- {
- // Try service name with TCP first, then UDP.
- if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
- {
- servent* sptr = getservbyname(serv, "tcp");
- if (sptr != 0)
- {
- int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
- if (rc < 0)
- return EAI_MEMORY;
- num_found += rc;
- }
- }
- if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
- {
- servent* sptr = getservbyname(serv, "udp");
- if (sptr != 0)
- {
- int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
- if (rc < 0)
- return EAI_MEMORY;
- num_found += rc;
- }
- }
- }
- if (num_found == 0)
- {
- if (hints->ai_socktype == 0)
- {
- // All calls to getservbyname() failed.
- return EAI_NONAME;
- }
- else
- {
- // Service not supported for socket type.
- return EAI_SERVICE;
- }
- }
- return 0;
- }
- inline int gai_echeck(const char* host, const char* service,
- int flags, int family, int socktype, int protocol)
- {
- (void)(flags);
- (void)(protocol);
- // Host or service must be specified.
- if (host == 0 || host[0] == '\0')
- if (service == 0 || service[0] == '\0')
- return EAI_NONAME;
- // Check combination of family and socket type.
- switch (family)
- {
- case BOOST_ASIO_OS_DEF(AF_UNSPEC):
- break;
- case BOOST_ASIO_OS_DEF(AF_INET):
- case BOOST_ASIO_OS_DEF(AF_INET6):
- if (service != 0 && service[0] != '\0')
- if (socktype != 0 && socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
- return EAI_SOCKTYPE;
- break;
- default:
- return EAI_FAMILY;
- }
- return 0;
- }
- inline void freeaddrinfo_emulation(addrinfo_type* aihead)
- {
- addrinfo_type* ai = aihead;
- while (ai)
- {
- gai_free(ai->ai_addr);
- gai_free(ai->ai_canonname);
- addrinfo_type* ainext = ai->ai_next;
- gai_free(ai);
- ai = ainext;
- }
- }
- inline int getaddrinfo_emulation(const char* host, const char* service,
- const addrinfo_type* hintsp, addrinfo_type** result)
- {
- // Set up linked list of addrinfo structures.
- addrinfo_type* aihead = 0;
- addrinfo_type** ainext = &aihead;
- char* canon = 0;
- // Supply default hints if not specified by caller.
- addrinfo_type hints = addrinfo_type();
- hints.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
- if (hintsp)
- hints = *hintsp;
- // If the resolution is not specifically for AF_INET6, remove the AI_V4MAPPED
- // and AI_ALL flags.
- #if defined(AI_V4MAPPED)
- if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
- hints.ai_flags &= ~AI_V4MAPPED;
- #endif
- #if defined(AI_ALL)
- if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
- hints.ai_flags &= ~AI_ALL;
- #endif
- // Basic error checking.
- int rc = gai_echeck(host, service, hints.ai_flags, hints.ai_family,
- hints.ai_socktype, hints.ai_protocol);
- if (rc != 0)
- {
- freeaddrinfo_emulation(aihead);
- return rc;
- }
- gai_search search[2];
- int search_count = gai_nsearch(host, &hints, search);
- for (gai_search* sptr = search; sptr < search + search_count; ++sptr)
- {
- // Check for IPv4 dotted decimal string.
- in4_addr_type inaddr;
- boost::system::error_code ec;
- if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET),
- sptr->host, &inaddr, 0, ec) == 1)
- {
- if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
- && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET))
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- return EAI_FAMILY;
- }
- if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET))
- {
- rc = gai_aistruct(&ainext, &hints, &inaddr, BOOST_ASIO_OS_DEF(AF_INET));
- if (rc != 0)
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- return rc;
- }
- }
- continue;
- }
- // Check for IPv6 hex string.
- in6_addr_type in6addr;
- if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6),
- sptr->host, &in6addr, 0, ec) == 1)
- {
- if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
- && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- return EAI_FAMILY;
- }
- if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET6))
- {
- rc = gai_aistruct(&ainext, &hints, &in6addr,
- BOOST_ASIO_OS_DEF(AF_INET6));
- if (rc != 0)
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- return rc;
- }
- }
- continue;
- }
- // Look up hostname.
- hostent hent;
- char hbuf[8192] = "";
- hostent* hptr = socket_ops::gethostbyname(sptr->host,
- sptr->family, &hent, hbuf, sizeof(hbuf), hints.ai_flags, ec);
- if (hptr == 0)
- {
- if (search_count == 2)
- {
- // Failure is OK if there are multiple searches.
- continue;
- }
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- if (ec == boost::asio::error::host_not_found)
- return EAI_NONAME;
- if (ec == boost::asio::error::host_not_found_try_again)
- return EAI_AGAIN;
- if (ec == boost::asio::error::no_recovery)
- return EAI_FAIL;
- if (ec == boost::asio::error::no_data)
- return EAI_NONAME;
- return EAI_NONAME;
- }
- // Check for address family mismatch if one was specified.
- if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
- && hints.ai_family != hptr->h_addrtype)
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- socket_ops::freehostent(hptr);
- return EAI_FAMILY;
- }
- // Save canonical name first time.
- if (host != 0 && host[0] != '\0' && hptr->h_name && hptr->h_name[0]
- && (hints.ai_flags & AI_CANONNAME) && canon == 0)
- {
- std::size_t canon_len = strlen(hptr->h_name) + 1;
- canon = gai_alloc<char>(canon_len);
- if (canon == 0)
- {
- freeaddrinfo_emulation(aihead);
- socket_ops::freehostent(hptr);
- return EAI_MEMORY;
- }
- gai_strcpy(canon, hptr->h_name, canon_len);
- }
- // Create an addrinfo structure for each returned address.
- for (char** ap = hptr->h_addr_list; *ap; ++ap)
- {
- rc = gai_aistruct(&ainext, &hints, *ap, hptr->h_addrtype);
- if (rc != 0)
- {
- freeaddrinfo_emulation(aihead);
- gai_free(canon);
- socket_ops::freehostent(hptr);
- return EAI_FAMILY;
- }
- }
- socket_ops::freehostent(hptr);
- }
- // Check if we found anything.
- if (aihead == 0)
- {
- gai_free(canon);
- return EAI_NONAME;
- }
- // Return canonical name in first entry.
- if (host != 0 && host[0] != '\0' && (hints.ai_flags & AI_CANONNAME))
- {
- if (canon)
- {
- aihead->ai_canonname = canon;
- canon = 0;
- }
- else
- {
- std::size_t canonname_len = strlen(search[0].host) + 1;
- aihead->ai_canonname = gai_alloc<char>(canonname_len);
- if (aihead->ai_canonname == 0)
- {
- freeaddrinfo_emulation(aihead);
- return EAI_MEMORY;
- }
- gai_strcpy(aihead->ai_canonname, search[0].host, canonname_len);
- }
- }
- gai_free(canon);
- // Process the service name.
- if (service != 0 && service[0] != '\0')
- {
- rc = gai_serv(aihead, &hints, service);
- if (rc != 0)
- {
- freeaddrinfo_emulation(aihead);
- return rc;
- }
- }
- // Return result to caller.
- *result = aihead;
- return 0;
- }
- inline boost::system::error_code getnameinfo_emulation(
- const socket_addr_type* sa, std::size_t salen, char* host,
- std::size_t hostlen, char* serv, std::size_t servlen, int flags,
- boost::system::error_code& ec)
- {
- using namespace std;
- const char* addr;
- size_t addr_len;
- unsigned short port;
- switch (sa->sa_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- if (salen != sizeof(sockaddr_in4_type))
- {
- return ec = boost::asio::error::invalid_argument;
- }
- addr = reinterpret_cast<const char*>(
- &reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_addr);
- addr_len = sizeof(in4_addr_type);
- port = reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_port;
- break;
- case BOOST_ASIO_OS_DEF(AF_INET6):
- if (salen != sizeof(sockaddr_in6_type))
- {
- return ec = boost::asio::error::invalid_argument;
- }
- addr = reinterpret_cast<const char*>(
- &reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_addr);
- addr_len = sizeof(in6_addr_type);
- port = reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_port;
- break;
- default:
- return ec = boost::asio::error::address_family_not_supported;
- }
- if (host && hostlen > 0)
- {
- if (flags & NI_NUMERICHOST)
- {
- if (socket_ops::inet_ntop(sa->sa_family, addr, host, hostlen, 0, ec) == 0)
- {
- return ec;
- }
- }
- else
- {
- hostent hent;
- char hbuf[8192] = "";
- hostent* hptr = socket_ops::gethostbyaddr(addr,
- static_cast<int>(addr_len), sa->sa_family,
- &hent, hbuf, sizeof(hbuf), ec);
- if (hptr && hptr->h_name && hptr->h_name[0] != '\0')
- {
- if (flags & NI_NOFQDN)
- {
- char* dot = strchr(hptr->h_name, '.');
- if (dot)
- {
- *dot = 0;
- }
- }
- gai_strcpy(host, hptr->h_name, hostlen);
- socket_ops::freehostent(hptr);
- }
- else
- {
- socket_ops::freehostent(hptr);
- if (flags & NI_NAMEREQD)
- {
- return ec = boost::asio::error::host_not_found;
- }
- if (socket_ops::inet_ntop(sa->sa_family,
- addr, host, hostlen, 0, ec) == 0)
- {
- return ec;
- }
- }
- }
- }
- if (serv && servlen > 0)
- {
- if (flags & NI_NUMERICSERV)
- {
- if (servlen < 6)
- {
- return ec = boost::asio::error::no_buffer_space;
- }
- #if defined(BOOST_ASIO_HAS_SECURE_RTL)
- sprintf_s(serv, servlen, "%u", ntohs(port));
- #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
- sprintf(serv, "%u", ntohs(port));
- #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
- }
- else
- {
- #if defined(BOOST_ASIO_HAS_PTHREADS)
- static ::pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- ::pthread_mutex_lock(&mutex);
- #endif // defined(BOOST_ASIO_HAS_PTHREADS)
- servent* sptr = ::getservbyport(port, (flags & NI_DGRAM) ? "udp" : 0);
- if (sptr && sptr->s_name && sptr->s_name[0] != '\0')
- {
- gai_strcpy(serv, sptr->s_name, servlen);
- }
- else
- {
- if (servlen < 6)
- {
- return ec = boost::asio::error::no_buffer_space;
- }
- #if defined(BOOST_ASIO_HAS_SECURE_RTL)
- sprintf_s(serv, servlen, "%u", ntohs(port));
- #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
- sprintf(serv, "%u", ntohs(port));
- #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
- }
- #if defined(BOOST_ASIO_HAS_PTHREADS)
- ::pthread_mutex_unlock(&mutex);
- #endif // defined(BOOST_ASIO_HAS_PTHREADS)
- }
- }
- ec = boost::system::error_code();
- return ec;
- }
- #endif // !defined(BOOST_ASIO_HAS_GETADDRINFO)
- inline boost::system::error_code translate_addrinfo_error(int error)
- {
- switch (error)
- {
- case 0:
- return boost::system::error_code();
- case EAI_AGAIN:
- return boost::asio::error::host_not_found_try_again;
- case EAI_BADFLAGS:
- return boost::asio::error::invalid_argument;
- case EAI_FAIL:
- return boost::asio::error::no_recovery;
- case EAI_FAMILY:
- return boost::asio::error::address_family_not_supported;
- case EAI_MEMORY:
- return boost::asio::error::no_memory;
- case EAI_NONAME:
- #if defined(EAI_ADDRFAMILY)
- case EAI_ADDRFAMILY:
- #endif
- #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
- case EAI_NODATA:
- #endif
- return boost::asio::error::host_not_found;
- case EAI_SERVICE:
- return boost::asio::error::service_not_found;
- case EAI_SOCKTYPE:
- return boost::asio::error::socket_type_not_supported;
- default: // Possibly the non-portable EAI_SYSTEM.
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- return boost::system::error_code(
- WSAGetLastError(), boost::asio::error::get_system_category());
- #else
- return boost::system::error_code(
- errno, boost::asio::error::get_system_category());
- #endif
- }
- }
- boost::system::error_code getaddrinfo(const char* host,
- const char* service, const addrinfo_type& hints,
- addrinfo_type** result, boost::system::error_code& ec)
- {
- host = (host && *host) ? host : 0;
- service = (service && *service) ? service : 0;
- clear_last_error();
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- # if defined(BOOST_ASIO_HAS_GETADDRINFO)
- // Building for Windows XP, Windows Server 2003, or later.
- int error = ::getaddrinfo(host, service, &hints, result);
- return ec = translate_addrinfo_error(error);
- # else
- // Building for Windows 2000 or earlier.
- typedef int (WSAAPI *gai_t)(const char*,
- const char*, const addrinfo_type*, addrinfo_type**);
- if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
- {
- if (gai_t gai = (gai_t)::GetProcAddress(winsock_module, "getaddrinfo"))
- {
- int error = gai(host, service, &hints, result);
- return ec = translate_addrinfo_error(error);
- }
- }
- int error = getaddrinfo_emulation(host, service, &hints, result);
- return ec = translate_addrinfo_error(error);
- # endif
- #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
- int error = getaddrinfo_emulation(host, service, &hints, result);
- return ec = translate_addrinfo_error(error);
- #else
- int error = ::getaddrinfo(host, service, &hints, result);
- #if defined(__MACH__) && defined(__APPLE__)
- using namespace std; // For isdigit and atoi.
- if (error == 0 && service && isdigit(static_cast<unsigned char>(service[0])))
- {
- u_short_type port = host_to_network_short(atoi(service));
- for (addrinfo_type* ai = *result; ai; ai = ai->ai_next)
- {
- switch (ai->ai_family)
- {
- case BOOST_ASIO_OS_DEF(AF_INET):
- {
- sockaddr_in4_type* sinptr =
- reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
- if (sinptr->sin_port == 0)
- sinptr->sin_port = port;
- break;
- }
- case BOOST_ASIO_OS_DEF(AF_INET6):
- {
- sockaddr_in6_type* sin6ptr =
- reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
- if (sin6ptr->sin6_port == 0)
- sin6ptr->sin6_port = port;
- break;
- }
- default:
- break;
- }
- }
- }
- #endif
- return ec = translate_addrinfo_error(error);
- #endif
- }
- boost::system::error_code background_getaddrinfo(
- const weak_cancel_token_type& cancel_token, const char* host,
- const char* service, const addrinfo_type& hints,
- addrinfo_type** result, boost::system::error_code& ec)
- {
- if (cancel_token.expired())
- ec = boost::asio::error::operation_aborted;
- else
- socket_ops::getaddrinfo(host, service, hints, result, ec);
- return ec;
- }
- void freeaddrinfo(addrinfo_type* ai)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- # if defined(BOOST_ASIO_HAS_GETADDRINFO)
- // Building for Windows XP, Windows Server 2003, or later.
- ::freeaddrinfo(ai);
- # else
- // Building for Windows 2000 or earlier.
- typedef int (WSAAPI *fai_t)(addrinfo_type*);
- if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
- {
- if (fai_t fai = (fai_t)::GetProcAddress(winsock_module, "freeaddrinfo"))
- {
- fai(ai);
- return;
- }
- }
- freeaddrinfo_emulation(ai);
- # endif
- #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
- freeaddrinfo_emulation(ai);
- #else
- ::freeaddrinfo(ai);
- #endif
- }
- boost::system::error_code getnameinfo(const socket_addr_type* addr,
- std::size_t addrlen, char* host, std::size_t hostlen,
- char* serv, std::size_t servlen, int flags, boost::system::error_code& ec)
- {
- #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
- # if defined(BOOST_ASIO_HAS_GETADDRINFO)
- // Building for Windows XP, Windows Server 2003, or later.
- clear_last_error();
- int error = ::getnameinfo(addr, static_cast<socklen_t>(addrlen),
- host, static_cast<DWORD>(hostlen),
- serv, static_cast<DWORD>(servlen), flags);
- return ec = translate_addrinfo_error(error);
- # else
- // Building for Windows 2000 or earlier.
- typedef int (WSAAPI *gni_t)(const socket_addr_type*,
- int, char*, DWORD, char*, DWORD, int);
- if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
- {
- if (gni_t gni = (gni_t)::GetProcAddress(winsock_module, "getnameinfo"))
- {
- clear_last_error();
- int error = gni(addr, static_cast<int>(addrlen),
- host, static_cast<DWORD>(hostlen),
- serv, static_cast<DWORD>(servlen), flags);
- return ec = translate_addrinfo_error(error);
- }
- }
- clear_last_error();
- return getnameinfo_emulation(addr, addrlen,
- host, hostlen, serv, servlen, flags, ec);
- # endif
- #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
- using namespace std; // For memcpy.
- sockaddr_storage_type tmp_addr;
- memcpy(&tmp_addr, addr, addrlen);
- addr = reinterpret_cast<socket_addr_type*>(&tmp_addr);
- clear_last_error();
- return getnameinfo_emulation(addr, addrlen,
- host, hostlen, serv, servlen, flags, ec);
- #else
- clear_last_error();
- int error = ::getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
- return ec = translate_addrinfo_error(error);
- #endif
- }
- boost::system::error_code sync_getnameinfo(
- const socket_addr_type* addr, std::size_t addrlen,
- char* host, std::size_t hostlen, char* serv,
- std::size_t servlen, int sock_type, boost::system::error_code& ec)
- {
- // First try resolving with the service name. If that fails try resolving
- // but allow the service to be returned as a number.
- int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
- socket_ops::getnameinfo(addr, addrlen, host,
- hostlen, serv, servlen, flags, ec);
- if (ec)
- {
- socket_ops::getnameinfo(addr, addrlen, host, hostlen,
- serv, servlen, flags | NI_NUMERICSERV, ec);
- }
- return ec;
- }
- boost::system::error_code background_getnameinfo(
- const weak_cancel_token_type& cancel_token,
- const socket_addr_type* addr, std::size_t addrlen,
- char* host, std::size_t hostlen, char* serv,
- std::size_t servlen, int sock_type, boost::system::error_code& ec)
- {
- if (cancel_token.expired())
- {
- ec = boost::asio::error::operation_aborted;
- }
- else
- {
- // First try resolving with the service name. If that fails try resolving
- // but allow the service to be returned as a number.
- int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
- socket_ops::getnameinfo(addr, addrlen, host,
- hostlen, serv, servlen, flags, ec);
- if (ec)
- {
- socket_ops::getnameinfo(addr, addrlen, host, hostlen,
- serv, servlen, flags | NI_NUMERICSERV, ec);
- }
- }
- return ec;
- }
- #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
- u_long_type network_to_host_long(u_long_type value)
- {
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
- u_long_type result = (static_cast<u_long_type>(value_p[0]) << 24)
- | (static_cast<u_long_type>(value_p[1]) << 16)
- | (static_cast<u_long_type>(value_p[2]) << 8)
- | static_cast<u_long_type>(value_p[3]);
- return result;
- #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- return ntohl(value);
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- }
- u_long_type host_to_network_long(u_long_type value)
- {
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- u_long_type result;
- unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
- result_p[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
- result_p[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
- result_p[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
- result_p[3] = static_cast<unsigned char>(value & 0xFF);
- return result;
- #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- return htonl(value);
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- }
- u_short_type network_to_host_short(u_short_type value)
- {
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
- u_short_type result = (static_cast<u_short_type>(value_p[0]) << 8)
- | static_cast<u_short_type>(value_p[1]);
- return result;
- #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- return ntohs(value);
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- }
- u_short_type host_to_network_short(u_short_type value)
- {
- #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
- u_short_type result;
- unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
- result_p[0] = static_cast<unsigned char>((value >> 8) & 0xFF);
- result_p[1] = static_cast<unsigned char>(value & 0xFF);
- return result;
- #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- return htons(value);
- #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
- }
- } // namespace socket_ops
- } // namespace detail
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
|