-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetapi.cpp
282 lines (244 loc) · 6.52 KB
/
netapi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include "winsock.h"
typedef int socklen_t;
#elif POSIX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#else
#error "implement me"
#endif
#include "netapi.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#pragma warning(disable: 4706) // warning C4706: assignment within conditional expression
//-----------------------------------------------------------------------------
// Purpose: Implements INetAPI
//-----------------------------------------------------------------------------
class CNetAPI : public INetAPI
{
public:
virtual void NetAdrToSockAddr( netadr_t *a, struct sockaddr *s );
virtual void SockAddrToNetAdr( struct sockaddr *s, netadr_t *a );
virtual char *AdrToString( netadr_t *a );
virtual bool StringToAdr( const char *s, netadr_t *a );
virtual void GetSocketAddress( int socket, netadr_t *a );
virtual bool CompareAdr( netadr_t *a, netadr_t *b );
virtual void GetLocalIP(netadr_t *a);
};
// Expose interface
static CNetAPI g_NetAPI;
INetAPI *net = ( INetAPI * )&g_NetAPI;
//-----------------------------------------------------------------------------
// Purpose:
// Input : *a -
// *s -
//-----------------------------------------------------------------------------
void CNetAPI::NetAdrToSockAddr (netadr_t *a, struct sockaddr *s)
{
memset (s, 0, sizeof(*s));
if (a->type == NA_BROADCAST)
{
((struct sockaddr_in *)s)->sin_family = AF_INET;
((struct sockaddr_in *)s)->sin_port = a->port;
((struct sockaddr_in *)s)->sin_addr.s_addr = INADDR_BROADCAST;
}
else if (a->type == NA_IP)
{
((struct sockaddr_in *)s)->sin_family = AF_INET;
((struct sockaddr_in *)s)->sin_addr.s_addr = *(int *)&a->ip;
((struct sockaddr_in *)s)->sin_port = a->port;
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *s -
// *a -
//-----------------------------------------------------------------------------
void CNetAPI::SockAddrToNetAdr( struct sockaddr *s, netadr_t *a )
{
if (s->sa_family == AF_INET)
{
a->type = NA_IP;
*(int *)&a->ip = ((struct sockaddr_in *)s)->sin_addr.s_addr;
a->port = ((struct sockaddr_in *)s)->sin_port;
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *a -
// Output : char
//-----------------------------------------------------------------------------
char *CNetAPI::AdrToString( netadr_t *a )
{
static char s[64];
memset(s, 0, 64);
if ( a )
{
if ( a->type == NA_LOOPBACK )
{
sprintf (s, "loopback");
}
else if ( a->type == NA_IP )
{
sprintf(s, "%i.%i.%i.%i:%i", a->ip[0], a->ip[1], a->ip[2], a->ip[3], ntohs( a->port ) );
}
}
return s;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *s -
// *sadr -
// Output : static bool
//-----------------------------------------------------------------------------
static bool StringToSockaddr( const char *s, struct sockaddr *sadr )
{
struct hostent *h;
char *colon;
char copy[128];
struct sockaddr_in *p;
memset (sadr, 0, sizeof(*sadr));
p = ( struct sockaddr_in * )sadr;
p->sin_family = AF_INET;
p->sin_port = 0;
strcpy (copy, s);
// strip off a trailing :port if present
for ( colon = copy ; *colon ; colon++ )
{
if (*colon == ':')
{
// terminate
*colon = 0;
// Start at next character
p->sin_port = htons( ( short )atoi( colon + 1 ) );
}
}
// Numeric IP, no DNS
if ( copy[0] >= '0' && copy[0] <= '9' && strstr( copy, "." ) )
{
*(int *)&p->sin_addr = inet_addr( copy );
}
else
{
// DNS it
if ( !( h = gethostbyname( copy ) ) )
{
return false;
}
// Use first result
*(int *)&p->sin_addr = *(int *)h->h_addr_list[0];
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *s -
// *a -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CNetAPI::StringToAdr( const char *s, netadr_t *a )
{
struct sockaddr sadr;
if ( !strcmp ( s, "localhost" ) )
{
memset ( a, 0, sizeof( *a ) );
a->type = NA_LOOPBACK;
return true;
}
if ( !StringToSockaddr (s, &sadr) )
{
return false;
}
SockAddrToNetAdr( &sadr, a );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Lookup the IP address for the specified IP socket
// Input : socket -
// *a -
//-----------------------------------------------------------------------------
#ifdef _LINUX
typedef socklen_t SOCKLEN;
#else
typedef int SOCKLEN;
#endif
void CNetAPI::GetSocketAddress( int socket, netadr_t *a )
{
char buff[512];
struct sockaddr_in address;
#ifdef POSIX
socklen_t namelen;
#else
int namelen;
#endif
// int net_error = 0;
memset( a, 0, sizeof( *a ) );
gethostname(buff, 512);
// Ensure that it doesn't overrun the buffer
buff[512-1] = 0;
StringToAdr(buff, a );
namelen = sizeof(address);
if ( getsockname( socket, (struct sockaddr *)&address, (socklen_t *)&namelen) == 0 )
{
a->port = address.sin_port;
}
}
//-----------------------------------------------------------------------------
// Purpose: Full IP address compare
// Input : *a -
// *b -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CNetAPI::CompareAdr( netadr_t *a, netadr_t *b )
{
if ( a->type != b->type )
{
return false;
}
if ( a->type == NA_LOOPBACK )
{
return true;
}
if ( a->type == NA_IP &&
a->ip[0] == b->ip[0] &&
a->ip[1] == b->ip[1] &&
a->ip[2] == b->ip[2] &&
a->ip[3] == b->ip[3] &&
a->port == b->port )
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Full IP address compare
// Input : *a -
// *b -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
void CNetAPI::GetLocalIP(netadr_t *a)
{
char s[64];
if(!::gethostname(s,64))
{
struct hostent *localip = ::gethostbyname(s);
if(localip)
{
a->type=NA_IP;
a->port=0;
memcpy(a->ip,localip->h_addr_list[0],4);
}
}
}