rob@76
|
1 /*
|
rob@76
|
2 oscpack -- Open Sound Control (OSC) packet manipulation library
|
rob@76
|
3 http://www.rossbencina.com/code/oscpack
|
rob@76
|
4
|
rob@76
|
5 Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
|
rob@76
|
6
|
rob@76
|
7 Permission is hereby granted, free of charge, to any person obtaining
|
rob@76
|
8 a copy of this software and associated documentation files
|
rob@76
|
9 (the "Software"), to deal in the Software without restriction,
|
rob@76
|
10 including without limitation the rights to use, copy, modify, merge,
|
rob@76
|
11 publish, distribute, sublicense, and/or sell copies of the Software,
|
rob@76
|
12 and to permit persons to whom the Software is furnished to do so,
|
rob@76
|
13 subject to the following conditions:
|
rob@76
|
14
|
rob@76
|
15 The above copyright notice and this permission notice shall be
|
rob@76
|
16 included in all copies or substantial portions of the Software.
|
rob@76
|
17
|
rob@76
|
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
rob@76
|
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
rob@76
|
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
rob@76
|
21 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
rob@76
|
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
rob@76
|
23 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
rob@76
|
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
rob@76
|
25 */
|
rob@76
|
26
|
rob@76
|
27 /*
|
rob@76
|
28 The text above constitutes the entire oscpack license; however,
|
rob@76
|
29 the oscpack developer(s) also make the following non-binding requests:
|
rob@76
|
30
|
rob@76
|
31 Any person wishing to distribute modifications to the Software is
|
rob@76
|
32 requested to send the modifications to the original developer so that
|
rob@76
|
33 they can be incorporated into the canonical version. It is also
|
rob@76
|
34 requested that these non-binding requests be included whenever the
|
rob@76
|
35 above license is reproduced.
|
rob@76
|
36 */
|
rob@76
|
37 #include "ip/NetworkingUtils.h"
|
rob@76
|
38
|
rob@76
|
39 #include <winsock2.h> // this must come first to prevent errors with MSVC7
|
rob@76
|
40 #include <windows.h>
|
rob@76
|
41
|
rob@76
|
42 #include <cstring>
|
rob@76
|
43
|
rob@76
|
44
|
rob@76
|
45 static LONG initCount_ = 0;
|
rob@76
|
46 static bool winsockInitialized_ = false;
|
rob@76
|
47
|
rob@76
|
48 NetworkInitializer::NetworkInitializer()
|
rob@76
|
49 {
|
rob@76
|
50 if( InterlockedIncrement( &initCount_ ) == 1 ){
|
rob@76
|
51 // there is a race condition here if one thread tries to access
|
rob@76
|
52 // the library while another is still initializing it.
|
rob@76
|
53 // i can't think of an easy way to fix it so i'm telling you here
|
rob@76
|
54 // incase you need to init the library from two threads at once.
|
rob@76
|
55 // this is why the header file advises to instantiate one of these
|
rob@76
|
56 // in main() so that the initialization happens globally
|
rob@76
|
57
|
rob@76
|
58 // initialize winsock
|
rob@76
|
59 WSAData wsaData;
|
rob@76
|
60 int nCode = WSAStartup(MAKEWORD(1, 1), &wsaData);
|
rob@76
|
61 if( nCode != 0 ){
|
rob@76
|
62 //std::cout << "WSAStartup() failed with error code " << nCode << "\n";
|
rob@76
|
63 }else{
|
rob@76
|
64 winsockInitialized_ = true;
|
rob@76
|
65 }
|
rob@76
|
66 }
|
rob@76
|
67 }
|
rob@76
|
68
|
rob@76
|
69
|
rob@76
|
70 NetworkInitializer::~NetworkInitializer()
|
rob@76
|
71 {
|
rob@76
|
72 if( InterlockedDecrement( &initCount_ ) == 0 ){
|
rob@76
|
73 if( winsockInitialized_ ){
|
rob@76
|
74 WSACleanup();
|
rob@76
|
75 winsockInitialized_ = false;
|
rob@76
|
76 }
|
rob@76
|
77 }
|
rob@76
|
78 }
|
rob@76
|
79
|
rob@76
|
80
|
rob@76
|
81 unsigned long GetHostByName( const char *name )
|
rob@76
|
82 {
|
rob@76
|
83 NetworkInitializer networkInitializer;
|
rob@76
|
84
|
rob@76
|
85 unsigned long result = 0;
|
rob@76
|
86
|
rob@76
|
87 struct hostent *h = gethostbyname( name );
|
rob@76
|
88 if( h ){
|
rob@76
|
89 struct in_addr a;
|
rob@76
|
90 std::memcpy( &a, h->h_addr_list[0], h->h_length );
|
rob@76
|
91 result = ntohl(a.s_addr);
|
rob@76
|
92 }
|
rob@76
|
93
|
rob@76
|
94 return result;
|
rob@76
|
95 }
|