game-networking-sockets-sys 0.2.0

Rust bindings for Valve GameNetworkingSockets library.
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//========= Copyright Valve Corporation, All rights reserved. =================//

#include <functional>

#include <tier1/netadr.h>
#include <tier0/platform_sockets.h>

#include <vstdlib/strtools.h>
#include "ipv6text.h"

const byte k_ipv6Bytes_LinkLocalAllNodes[16] = { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; // ff02:1
const byte k_ipv6Bytes_Loopback[16]          = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; // ::1
const byte k_ipv6Bytes_Any[16]               = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // ::0


//---------------------------------------------------------------------------------
//
// CIPAddress
//
//---------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: Convert the given IP address to a string representation.
//			Optionally will include the given port if punPort is not null.
//-----------------------------------------------------------------------------
void CIPAddress::ToString( char *pchBuffer, uint32 unBufferSize, const uint16 *punPort ) const
{
	if (m_usType == k_EIPTypeLoopbackDeprecated)
	{
		V_strncpy (pchBuffer, "loopback", unBufferSize );
	}
	else if (m_usType == k_EIPTypeBroadcastDeprecated)
	{
		V_strncpy (pchBuffer, "broadcast", unBufferSize );
	}
	else if (m_usType == k_EIPTypeV4)
	{
		if ( !punPort )
		{
			V_snprintf(pchBuffer, unBufferSize, "%i.%i.%i.%i", m_IPv4Bytes.b1, m_IPv4Bytes.b2, m_IPv4Bytes.b3, m_IPv4Bytes.b4 );
		}
		else
		{
			V_snprintf(pchBuffer, unBufferSize, "%i.%i.%i.%i:%i", m_IPv4Bytes.b1, m_IPv4Bytes.b2, m_IPv4Bytes.b3, m_IPv4Bytes.b4, *punPort );
		}
	}
	else if (m_usType == k_EIPTypeV6)
	{
		// Format IP into a temp that we know is big enough
		char temp[ k_cchMaxIPV6AddrStringWithPort ];
		if ( !punPort )
			IPv6IPToString( temp, m_rgubIPv6 );
		else
			IPv6AddrToString( temp, m_rgubIPv6, *punPort, m_unIPv6Scope );

		// Now put into caller's buffer, and handle truncation
		V_strncpy( pchBuffer, temp, unBufferSize );
	}
	else
	{
		V_strncpy(pchBuffer, "unknown", unBufferSize );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Is this IP part of one of the reserved blocks?
//-----------------------------------------------------------------------------
bool CIPAddress::IsReservedAdr () const
{
	// The code below will be incorrect if we are a ipv4-mapped-to-ipv6 address
	// So - unmap ourselves to a temp CIPAddress and ask it
	if ( IsMappedIPv4() )
	{
		CIPAddress ipTemp = *this;
		ipTemp.BConvertMappedToIPv4();
		return ipTemp.IsReservedAdr();
	}

	switch ( m_usType )
	{
		case k_EIPTypeLoopbackDeprecated:
			return true;

		case k_EIPTypeBroadcastDeprecated:
			// Makes no sense to me, but this is what the old code did
			return false;

		case k_EIPTypeV4:
			if ( (m_IPv4Bytes.b1 == 10) ||									// 10.x.x.x is reserved
				(m_IPv4Bytes.b1 == 127) ||									// 127.x.x.x 
				(m_IPv4Bytes.b1 == 169 && m_IPv4Bytes.b2 == 254) ||			// 169.254.x.x is link-local ipv4
				(m_IPv4Bytes.b1 == 172 && m_IPv4Bytes.b2 >= 16 && m_IPv4Bytes.b2 <= 31) ||	// 172.16.x.x  - 172.31.x.x 
				(m_IPv4Bytes.b1 == 192 && m_IPv4Bytes.b2 >= 168) ) 					// 192.168.x.x
			{
				return true;
			}
			else
			{
				return false;
			}

		case k_EIPTypeV6:
			// Private addresses, fc00::/7
			// Range is fc00:: to fdff:ffff:etc
			if ( m_rgubIPv6[0] >= 0xFC && m_rgubIPv6[1] <= 0xFD )
			{
				return true;
			}
			
			// Link-local fe80::/10
			// Range is fe80:: to febf::
			if ( m_rgubIPv6[0] == 0xFE
				&& ( m_rgubIPv6[1] >= 0x80 && m_rgubIPv6[1] <= 0xBF ) )
			{
				return true;
			}
			
			return false;

		default:
			Assert( false );
	}
	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Does this object have an IP value set
//-----------------------------------------------------------------------------
bool CIPAddress::HasIP() const
{
	switch ( m_usType )
	{
		default:
			Assert( false );
			// FALLTHROUGH
		case k_EIPTypeInvalid:
			return false;
		case k_EIPTypeV4:
			if ( m_unIPv4 == 0 )
				return false;
			break;
		case k_EIPTypeV6:
			if ( m_ipv6Qword[0] == 0 && m_ipv6Qword[1] == 0 )
				return false;
			break;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Compare this IP address with another for equality
//-----------------------------------------------------------------------------
bool CIPAddress::operator==(const CIPAddress &a) const
{
	if ( a.m_usType != m_usType )
		return false;

	if ( m_usType == k_EIPTypeV4 )
	{
		if ( a.m_unIPv4 == m_unIPv4 )
			return true;
	}
	else if ( m_usType == k_EIPTypeV6 )
	{
		// NOTE: We are intentionally not comparing the scope here.
		//       The examples where comparing the scope breaks simple
		//       stuff in unexpected ways seems more common than examples
		//       where you need to compare the scope.  If you need to compare
		//       them, then do it yourself.

		if ( a.m_ipv6Qword[0] == m_ipv6Qword[0] && a.m_ipv6Qword[1] == m_ipv6Qword[1] )
			return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
// Purpose: Inequality comparison
//-----------------------------------------------------------------------------
bool CIPAddress::operator!=(const CIPAddress &netadr) const
{
	return !operator==(netadr);
}


//-----------------------------------------------------------------------------
// Purpose: For sorting lists/trees of IP addresses
//-----------------------------------------------------------------------------
bool CIPAddress::operator<(const CIPAddress &netadr) const
{
	if ( m_usType < netadr.m_usType ) return true;
	if ( m_usType > netadr.m_usType ) return false;

	// Types match
	if ( m_usType == k_EIPTypeV6 )
	{
		int c = memcmp( m_rgubIPv6, netadr.m_rgubIPv6, sizeof(m_rgubIPv6) );
		if ( c < 0 ) return true;
		if ( c > 0 ) return false;
	}
	else
	{
		if ( m_unIPv4 < netadr.m_unIPv4 ) return true;
		if ( m_unIPv4 > netadr.m_unIPv4 ) return false;
	}

	// equal
	return false;
}

unsigned int CIPAddress::GetHashKey( const CIPAddress &netadr )
{
	// See: boost::hash_combine
	size_t result;
	switch ( netadr.m_usType )
	{
		default:
			result = std::hash<int>{}( netadr.m_usType );
			break;

		case k_EIPTypeV4:
			result = std::hash<uint32>{}( netadr.m_unIPv4 );
			break;

		case k_EIPTypeV6:
			result = std::hash<uint64>{}( netadr.m_ipv6Qword[0] );
			result ^= std::hash<uint64>{}( netadr.m_ipv6Qword[1] ) + 0x9e3779b9 + (result << 6) + (result >> 2);
			break;
	}

	return (unsigned int)result;
}

//-----------------------------------------------------------------------------
// Purpose: Legacy - just returns HasIP()
//-----------------------------------------------------------------------------
bool CIPAddress::IsValid() const
{
	return HasIP();
}

//-----------------------------------------------------------------------------
// Purpose: Is this IP address a "loopback" special address, such as 127.0.0.1?
//-----------------------------------------------------------------------------
bool CIPAddress::IsLoopback() const
{
	// The code below will be incorrect if we are a ipv4-mapped-to-ipv6 address
	// So - unmap ourselves to a temp CIPAddress and ask it
	if ( IsMappedIPv4() )
	{
		CIPAddress ipTemp = *this;
		ipTemp.BConvertMappedToIPv4();
		return ipTemp.IsLoopback();
	}

	switch ( m_usType )
	{
		case k_EIPTypeInvalid:
		case k_EIPTypeBroadcastDeprecated:
			return false;
		case k_EIPTypeLoopbackDeprecated:
			return true;
		case k_EIPTypeV4:
			return ( m_unIPv4 & 0xff000000 ) == 0x7f000000; // 127.x.x.x
		case k_EIPTypeV6:
			return m_ipv6Qword[0] == 0 &&
			#ifdef VALVE_BIG_ENDIAN
				m_ipv6Qword[1] == 1;
			#else
				m_ipv6Qword[1] == 0x0100000000000000ull;
			#endif
	}
	Assert( false );
	return false;
}

//-----------------------------------------------------------------------------
// Purpose: Is this IP address a broadcast address?
//-----------------------------------------------------------------------------
bool CIPAddress::IsBroadcast() const
{
	// The code below will be incorrect if we are a ipv4-mapped-to-ipv6 address
	// So - unmap ourselves to a temp CIPAddress and return it
	if ( IsMappedIPv4() )
	{
		CIPAddress ipTemp = *this;
		ipTemp.BConvertMappedToIPv4();
		return ipTemp.IsBroadcast();
	}

	switch ( m_usType )
	{
		case k_EIPTypeInvalid:
		case k_EIPTypeLoopbackDeprecated:
			return false;
		case k_EIPTypeBroadcastDeprecated:
			return true;
		case k_EIPTypeV4:
			return m_unIPv4 == 0xffffffff; // 255.255.255.255
		case k_EIPTypeV6:
			// There might other IPs than could be construed as "broadcast",
			// but just check for the one used by SetIPV6Broadcast()
			return memcmp( m_rgubIPv6, k_ipv6Bytes_LinkLocalAllNodes, 16 ) == 0;
	}
	Assert( false );
	return false;
}

//-----------------------------------------------------------------------------
// Purpose: Get the ipv6 bytes (network byte order) of this address. Works even
//			if this is an m_IPv4Bytes address, it will return a "mapped" ipv6 address.
//-----------------------------------------------------------------------------
void CIPAddress::GetIPV6( byte *result ) const
{
	switch ( m_usType )
	{
		default:
			Assert( false );
			// FALLTHROUGH
		case k_EIPTypeInvalid:
			// ::
			memset( result, 0, 16 );
			break;

		case k_EIPTypeLoopbackDeprecated:
			// ::1
			memset( result, 0, 16 );
			result[15] = 1;
			return;

		case k_EIPTypeBroadcastDeprecated:
			memcpy( result, k_ipv6Bytes_LinkLocalAllNodes, 16 );
			break;

		case k_EIPTypeV4:
			// ::ffff:aabb.ccdd
			memset( result, 0, 10 );
			result[10] = 0xff;
			result[11] = 0xff;
			result[12] = m_IPv4Bytes.b1;
			result[13] = m_IPv4Bytes.b2;
			result[14] = m_IPv4Bytes.b3;
			result[15] = m_IPv4Bytes.b4;
			break;

		case k_EIPTypeV6:
			memcpy( result, m_rgubIPv6, 16 );
			break;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Is this an ipv6 address that is actually a mapped m_IPv4Bytes address
//-----------------------------------------------------------------------------
bool CIPAddress::IsMappedIPv4() const
{
	if ( m_usType != k_EIPTypeV6 )
		return false;
	if (
		m_ipv6Qword[0] != 0 // 0...7
		|| m_rgubIPv6[8] != 0
		|| m_rgubIPv6[9] != 0
		|| m_rgubIPv6[10] != 0xff
		|| m_rgubIPv6[11] != 0xff
	) {
		return false;
	}
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: For an "m_IPv4Bytes address mapped into ipv6 space", this will internally 
//			convert it to a plain m_IPv4Bytes address.
//-----------------------------------------------------------------------------
bool CIPAddress::BConvertMappedToIPv4()
{
	if ( !IsMappedIPv4() )
		return false;
	SetIPv4( m_rgubIPv6[12], m_rgubIPv6[13], m_rgubIPv6[14], m_rgubIPv6[15] );
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: For an m_IPv4Bytes address, this will internally convert it into a mapped
//			address in ipv6 space.
//-----------------------------------------------------------------------------
bool CIPAddress::BConvertIPv4ToMapped()
{
	if ( m_usType != k_EIPTypeV4 )
		return false;

	// Copy off IPv4 address, since it shares the same memory
	// as the IPv6 bytes.  And we don't want to write code that depends
	// on how the memory is laid out or try to be clever.
	uint8 b1 = m_IPv4Bytes.b1;
	uint8 b2 = m_IPv4Bytes.b2;
	uint8 b3 = m_IPv4Bytes.b3;
	uint8 b4 = m_IPv4Bytes.b4;

	m_usType = k_EIPTypeV6;

	// ::ffff:aabb.ccdd
	memset( m_rgubIPv6, 0, 10 );
	m_rgubIPv6[10] = 0xff;
	m_rgubIPv6[11] = 0xff;
	m_rgubIPv6[12] = b1;
	m_rgubIPv6[13] = b2;
	m_rgubIPv6[14] = b3;
	m_rgubIPv6[15] = b4;

	m_unIPv6Scope = 0;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Initialize the object from a sockaddr structure. May be m_IPv4Bytes or ipv6.
//-----------------------------------------------------------------------------
bool CIPAddress::SetFromSockadr(const void *addr, size_t addr_size, uint16 *punPort )
{
	Clear();
	const auto *s = (const sockaddr *)addr;
	if (!s || addr_size < sizeof(s->sa_family) )
	{
		Assert( false );
		return false;
	}
	switch ( (int)s->sa_family )
	{
		case AF_INET:
		{
			if ( addr_size < sizeof(sockaddr_in) )
			{
				Assert( false );
				return false;
			}
			const auto *sin = (const sockaddr_in *)addr;
			m_usType = k_EIPTypeV4;
			m_unIPv4 = BigDWord( sin->sin_addr.s_addr );
			if ( punPort )
				*punPort = BigWord( sin->sin_port );

			return true;
		}

#ifndef PLATFORM_NO_IPV6
		case AF_INET6:
		{
			if ( addr_size < sizeof(sockaddr_in6) )
			{
				Assert( false );
				return false;
			}
			const auto *sin6 = (const sockaddr_in6 *)addr;
			m_usType = k_EIPTypeV6;
			COMPILE_TIME_ASSERT( sizeof(sin6->sin6_addr) == sizeof(m_rgubIPv6) );
			memcpy( m_rgubIPv6, &sin6->sin6_addr, sizeof(m_rgubIPv6) );
			m_unIPv6Scope = sin6->sin6_scope_id;
			if ( punPort )
				*punPort = BigWord( sin6->sin6_port );

			return true;
		}
#endif
	}
	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Initialize from a string representation of either an m_IPv4Bytes or ipv6
//			address. Optionally can return the port number, if any was found 
//			in the string.
//-----------------------------------------------------------------------------
bool CIPAddress::SetFromString( const char *pch, uint16 *punPort )
{

	Clear();

	if ( punPort )
		*punPort = 0;

	if ( !pch || pch[0] == 0 )			// but let's not crash
		return false;

	if ( pch[0] >= '0' && pch[0] <= '9' && strchr( pch, '.' ) )
	{
		int n1, n2, n3, n4, n5 = 0;
		int nRes = sscanf( pch, "%d.%d.%d.%d:%d", &n1, &n2, &n3, &n4, &n5 );
		if ( nRes >= 4 )
		{
			// Make sure octets are in range 0...255 and port number is legit
			if ( ( ( n1 | n2 | n3 | n4 ) & ~0xff ) || (uint16)n5 != n5 )
				return false;

			SetIPv4( n1, n2, n3, n4 );
			if ( punPort )
			{
				*punPort = (uint16) n5;
			}
			return true;
		}
	}

	// IPv6?
	int tmpPort;
	uint32_t tmpScope;
	if ( ParseIPv6Addr( pch, m_rgubIPv6, &tmpPort, &tmpScope ) )
	{
		m_usType = k_EIPTypeV6;
		if ( tmpPort >= 0 && punPort )
			*punPort = (uint16)tmpPort;
        m_unIPv6Scope = tmpScope;
		return true;
	}

	//	clobber partial state possibly left by ParseIPv6Addr
	Clear();

	return false;
}
//---------------------------------------------------------------------------------
//
// CIPAndPort
//
//---------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: Convert the given IP+port combination to a string representation.
//-----------------------------------------------------------------------------
void CIPAndPort::ToString( char *pchBuffer, uint32 unBufferSize, bool baseOnly ) const
{
	CIPAddress::ToString( pchBuffer, unBufferSize, baseOnly ? nullptr : &m_usPort );
}


//-----------------------------------------------------------------------------
// Purpose: Has a port value been set
//-----------------------------------------------------------------------------
bool CIPAndPort::HasPort() const
{
	return ( m_usPort != 0 );
}

//-----------------------------------------------------------------------------
// Purpose: Legacy - only valid if both the IP and the Port are set
//-----------------------------------------------------------------------------
bool CIPAndPort::IsValid() const
{
	return HasIP() && HasPort();
}





//-----------------------------------------------------------------------------
// Purpose: Compare m_unIPv4+port combo for equality
//-----------------------------------------------------------------------------
bool CIPAndPort::CompareAdr(const CIPAndPort &a, bool onlyBase) const
{
	if ( CIPAddress::operator==(a) )
	{
		if ( onlyBase )
			return true;
		else
			return m_usPort == a.m_usPort;
	}
	else
	{
		return false;
	}
}


//-----------------------------------------------------------------------------
// Purpose: For sorting lists/trees of IP+port combinations
//-----------------------------------------------------------------------------
bool CIPAndPort::operator<(const CIPAndPort &netadr) const
{
	// If either address is IPv6, use sane behaviour
	if ( m_usType == k_EIPTypeV6 || netadr.m_usType == k_EIPTypeV6 )
	{
		if ( m_usType < netadr.m_usType ) return true;
		if ( m_usType > netadr.m_usType ) return false;
		Assert( m_usType == k_EIPTypeV6 && netadr.m_usType == k_EIPTypeV6 );
		int c = memcmp( m_rgubIPv6, netadr.m_rgubIPv6, sizeof(m_rgubIPv6) );
		if ( c < 0 ) return true;
		if ( c > 0 ) return false;
	}
	else
	{
		// Even if types differ, just compare the IP portion?
		// This seems wrong to me, but I'm scared to change it
		// in case I break existing behaviour
		if ( m_unIPv4 < netadr.m_unIPv4 ) return true;
		if ( m_unIPv4 > netadr.m_unIPv4 ) return false;
	}

	// port breaks the tie
	return m_usPort < netadr.m_usPort;
}

unsigned int CIPAndPort::GetHashKey( const CIPAndPort &netadr )
{
	// See: boost::hash_combine
	size_t result = CIPAddress::GetHashKey( netadr );
	if ( netadr.m_usType != k_EIPTypeInvalid )
	{
		result ^= std::hash<uint16>{}( netadr.m_usPort ) + 0x9e3779b9 + (result << 6) + (result >> 2);
	}

	return (unsigned int)result;
}



//-----------------------------------------------------------------------------
// Purpose: Convert this m_unIPv4+port to a 'sockaddr' structure
//-----------------------------------------------------------------------------
size_t CIPAndPort::ToSockadr(void *addr, size_t addr_size) const
{
	size_t struct_size = 0;
	memset( addr, 0, addr_size);

	switch ( m_usType )
	{
		default:
		case k_EIPTypeInvalid:
			Assert( false );
			break;

		case k_EIPTypeLoopbackDeprecated:
		{
			if ( addr_size < sizeof(sockaddr_in) )
			{
				AssertMsg( false, "Address too small!" );
				return struct_size;
			}
			auto *s = (struct sockaddr_in*)addr;
			s->sin_family = AF_INET;
			s->sin_addr.s_addr = BigDWord( INADDR_LOOPBACK );
			s->sin_port = BigWord( m_usPort );
			struct_size = sizeof(sockaddr_in);
		}
		break;

		case k_EIPTypeBroadcastDeprecated:
		{
			if ( addr_size < sizeof(sockaddr_in) )
			{
				AssertMsg( false, "Address too small!" );
				return struct_size;
			}
			auto *s = (struct sockaddr_in*)addr;
			s->sin_family = AF_INET;
			s->sin_addr.s_addr = BigDWord( INADDR_BROADCAST );
			s->sin_port = BigWord( m_usPort );
			struct_size = sizeof(sockaddr_in);
		}
		break;

		case k_EIPTypeV4:
		{
			if ( addr_size < sizeof(sockaddr_in) )
			{
				AssertMsg( false, "Address too small!" );
				return struct_size;
			}
			auto *s = (struct sockaddr_in*)addr;
			s->sin_family = AF_INET;
			s->sin_addr.s_addr = BigDWord( m_unIPv4 );
			s->sin_port = BigWord( m_usPort );
			struct_size = sizeof(sockaddr_in);
		}
		break;

#ifndef PLATFORM_NO_IPV6
		case k_EIPTypeV6:
		{
			if ( addr_size < sizeof(sockaddr_in6) )
			{
				AssertMsg( false, "Address too small!" );
				return struct_size;
			}
			auto *s = (struct sockaddr_in6*)addr;
			s->sin6_family = AF_INET6;
			COMPILE_TIME_ASSERT( sizeof(s->sin6_addr) == sizeof(m_rgubIPv6) );
			memcpy( &s->sin6_addr, m_rgubIPv6, sizeof(s->sin6_addr) );
			s->sin6_scope_id = m_unIPv6Scope;
			s->sin6_port = BigWord( m_usPort );
			struct_size = sizeof(sockaddr_in6);
		}
		break;
#endif // #ifndef PLATFORM_NO_IPV6
	}

	return struct_size;
}




//-----------------------------------------------------------------------------
// Purpose: Convert to an ipv6 sockaddr structure
//-----------------------------------------------------------------------------
#ifndef PLATFORM_NO_IPV6
void CIPAndPort::ToSockadrIPV6(void *addr, size_t addr_size) const
{
	memset( addr, 0, addr_size);
	if ( addr_size < sizeof(sockaddr_in6) )
	{
		AssertMsg( false, "Address too small!" );
		return;
	}
	auto *s = (struct sockaddr_in6*)addr;
	s->sin6_family = AF_INET6;
	GetIPV6( s->sin6_addr.s6_addr );
	if ( m_usType == k_EIPTypeV6 )
		s->sin6_scope_id = m_unIPv6Scope;
	s->sin6_port = BigWord( m_usPort );
}
#endif // PLATFORM_NO_IPV6