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