~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/network/win32/HostAddressWin32.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2001-December-14     Jason Rohrer
 
5
 * Created.
 
6
 * Has not been compiled or tested under windows.
 
7
 *
 
8
 * 2002-March-25    Jason Rohrer
 
9
 * Added function for conversion to numerical addresses.
 
10
 *
 
11
 * 2002-March-26    Jason Rohrer
 
12
 * Changed to use strdup.
 
13
 * Fixed problem on some platforms where local address does
 
14
 * not include domain name.
 
15
 * Fixed so that numerical address is actually retrieved by
 
16
 * getNumericalAddress().
 
17
 *
 
18
 * 2002-April-6    Jason Rohrer
 
19
 * Replaced use of strdup.
 
20
 *
 
21
 * 2002-April-7    Jason Rohrer
 
22
 * Changed to return numerical address from getLocalAddress,
 
23
 * since getdomainname() call not available on Win32.
 
24
 *
 
25
 * 2002-April-8   Jason Rohrer
 
26
 * Changed to init socket framework before getting local address.
 
27
 * Changed to check if address is numerical before
 
28
 * performing a lookup on it.
 
29
 *
 
30
 * 2004-January-4   Jason Rohrer
 
31
 * Added use of network function locks.
 
32
 *
 
33
 * 2008-November-2   Jason Rohrer
 
34
 * Added framework init in getNumericalAddress.
 
35
 */
 
36
 
 
37
 
 
38
 
 
39
#include "minorGems/network/HostAddress.h"
 
40
#include "minorGems/network/NetworkFunctionLocks.h"
 
41
#include "minorGems/util/stringUtils.h"
 
42
 
 
43
#include <winsock.h>
 
44
#include <windows.h>
 
45
 
 
46
#include <string.h>
 
47
 
 
48
 
 
49
#include "minorGems/network/Socket.h"
 
50
 
 
51
 
 
52
 
 
53
HostAddress *HostAddress::getLocalAddress() {
 
54
        
 
55
    // first make sure sockets are initialized
 
56
    if( !Socket::isFrameworkInitialized() ) {
 
57
                
 
58
                // try to init the framework
 
59
                
 
60
                int error = Socket::initSocketFramework();
 
61
                
 
62
                if( error == -1 ) {
 
63
                        
 
64
                        printf( "initializing network socket framework failed\n" );
 
65
                        return NULL;
 
66
                        }
 
67
                }
 
68
        
 
69
        
 
70
        
 
71
        
 
72
        int bufferLength = 200;
 
73
        char *buffer = new char[ bufferLength ];
 
74
 
 
75
        gethostname( buffer, bufferLength );
 
76
 
 
77
    char *name = stringDuplicate( buffer );
 
78
 
 
79
        delete [] buffer;
 
80
 
 
81
 
 
82
    // this class will destroy name for us
 
83
    HostAddress *nameAddress = new HostAddress( name, 0 );
 
84
 
 
85
    HostAddress *fullAddress = nameAddress->getNumericalAddress();
 
86
 
 
87
    delete nameAddress;
 
88
 
 
89
    
 
90
    return fullAddress;    
 
91
        }
 
92
 
 
93
 
 
94
 
 
95
HostAddress *HostAddress::getNumericalAddress() {
 
96
 
 
97
    // make sure we're not already numerical
 
98
    if( this->isNumerical() ) {
 
99
        return this->copy();
 
100
        }
 
101
 
 
102
 
 
103
    if( !Socket::isFrameworkInitialized() ) {
 
104
                
 
105
                // try to init the framework
 
106
                
 
107
                int error = Socket::initSocketFramework();
 
108
                
 
109
                if( error == -1 ) {
 
110
                        
 
111
                        printf( "initializing network socket framework failed\n" );
 
112
                        return NULL;
 
113
                        }
 
114
                }
 
115
 
 
116
    // need to use two function locks here
 
117
 
 
118
    // first, lock for gethostbyname
 
119
    NetworkFunctionLocks::mGetHostByNameLock.lock();
 
120
    struct hostent *host = gethostbyname( mAddressString );
 
121
 
 
122
    if (host != NULL) {
 
123
 
 
124
        // this line adapted from the
 
125
        // Unix Socket FAQ
 
126
        struct in_addr *inetAddress = (struct in_addr *) *host->h_addr_list;
 
127
 
 
128
 
 
129
        // now lock for inet_ntoa
 
130
        NetworkFunctionLocks::mInet_ntoaLock.lock();
 
131
        // the returned string is statically allocated, so copy it
 
132
        char *inetAddressString = stringDuplicate( inet_ntoa( *inetAddress ) );
 
133
 
 
134
        NetworkFunctionLocks::mInet_ntoaLock.unlock();
 
135
 
 
136
        
 
137
        // done with returned hostent now, so unlock first lock
 
138
        NetworkFunctionLocks::mGetHostByNameLock.unlock();
 
139
        
 
140
        return new HostAddress( inetAddressString, mPort );
 
141
        }
 
142
    else {
 
143
 
 
144
        // unlock first lock before returning
 
145
        NetworkFunctionLocks::mGetHostByNameLock.unlock();
 
146
        
 
147
        return NULL;
 
148
        }
 
149
 
 
150
    }
 
151
 
 
152
 
 
153
 
 
154
char HostAddress::isNumerical() {
 
155
 
 
156
    unsigned long returnedValue = inet_addr( mAddressString );
 
157
 
 
158
 
 
159
    if( returnedValue == INADDR_NONE ) {
 
160
        return false;
 
161
        }
 
162
    else {
 
163
        return true;
 
164
        }
 
165
 
 
166
    }
 
167
 
 
168