rob@76: /* rob@76: oscpack -- Open Sound Control (OSC) packet manipulation library rob@76: http://www.rossbencina.com/code/oscpack rob@76: rob@76: Copyright (c) 2004-2013 Ross Bencina rob@76: rob@76: Permission is hereby granted, free of charge, to any person obtaining rob@76: a copy of this software and associated documentation files rob@76: (the "Software"), to deal in the Software without restriction, rob@76: including without limitation the rights to use, copy, modify, merge, rob@76: publish, distribute, sublicense, and/or sell copies of the Software, rob@76: and to permit persons to whom the Software is furnished to do so, rob@76: subject to the following conditions: rob@76: rob@76: The above copyright notice and this permission notice shall be rob@76: included in all copies or substantial portions of the Software. rob@76: rob@76: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, rob@76: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF rob@76: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. rob@76: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR rob@76: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF rob@76: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION rob@76: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rob@76: */ rob@76: rob@76: /* rob@76: The text above constitutes the entire oscpack license; however, rob@76: the oscpack developer(s) also make the following non-binding requests: rob@76: rob@76: Any person wishing to distribute modifications to the Software is rob@76: requested to send the modifications to the original developer so that rob@76: they can be incorporated into the canonical version. It is also rob@76: requested that these non-binding requests be included whenever the rob@76: above license is reproduced. rob@76: */ rob@76: #include "ip/NetworkingUtils.h" rob@76: rob@76: #include // this must come first to prevent errors with MSVC7 rob@76: #include rob@76: rob@76: #include rob@76: rob@76: rob@76: static LONG initCount_ = 0; rob@76: static bool winsockInitialized_ = false; rob@76: rob@76: NetworkInitializer::NetworkInitializer() rob@76: { rob@76: if( InterlockedIncrement( &initCount_ ) == 1 ){ rob@76: // there is a race condition here if one thread tries to access rob@76: // the library while another is still initializing it. rob@76: // i can't think of an easy way to fix it so i'm telling you here rob@76: // incase you need to init the library from two threads at once. rob@76: // this is why the header file advises to instantiate one of these rob@76: // in main() so that the initialization happens globally rob@76: rob@76: // initialize winsock rob@76: WSAData wsaData; rob@76: int nCode = WSAStartup(MAKEWORD(1, 1), &wsaData); rob@76: if( nCode != 0 ){ rob@76: //std::cout << "WSAStartup() failed with error code " << nCode << "\n"; rob@76: }else{ rob@76: winsockInitialized_ = true; rob@76: } rob@76: } rob@76: } rob@76: rob@76: rob@76: NetworkInitializer::~NetworkInitializer() rob@76: { rob@76: if( InterlockedDecrement( &initCount_ ) == 0 ){ rob@76: if( winsockInitialized_ ){ rob@76: WSACleanup(); rob@76: winsockInitialized_ = false; rob@76: } rob@76: } rob@76: } rob@76: rob@76: rob@76: unsigned long GetHostByName( const char *name ) rob@76: { rob@76: NetworkInitializer networkInitializer; rob@76: rob@76: unsigned long result = 0; rob@76: rob@76: struct hostent *h = gethostbyname( name ); rob@76: if( h ){ rob@76: struct in_addr a; rob@76: std::memcpy( &a, h->h_addr_list[0], h->h_length ); rob@76: result = ntohl(a.s_addr); rob@76: } rob@76: rob@76: return result; rob@76: }