@@ -28,7 +28,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
28
29
29
#define PORT_ANY -1
30
30
31
- typedef enum {NA_LOOPBACK = 1 , NA_BROADCAST , NA_IP , NA_IPX , NA_BROADCAST_IPX , NA_IP6 , NA_MULTICAST_IP6 } netadrtype_t ;
31
+ typedef enum netadrtype_e
32
+ {
33
+ NA_UNDEFINED = 0 ,
34
+ NA_LOOPBACK ,
35
+ NA_BROADCAST ,
36
+ NA_IP ,
37
+ NA_IPX ,
38
+ NA_BROADCAST_IPX ,
39
+ NA_IP6 ,
40
+ NA_MULTICAST_IP6
41
+ } netadrtype_t ;
32
42
33
43
/*
34
44
Original Quake-2 structure:
@@ -46,29 +56,60 @@ typedef struct
46
56
#pragma pack( push, 1 )
47
57
typedef struct netadr_s
48
58
{
59
+ // the reason we do this evil thing, is that when this struct contains IPv6
60
+ // address the `type` is 2-byte wide, but when it doesn't `type` must 4-byte
61
+ // wide _and_ ip6_0 must be zeroed, to keep it binary compatible.
62
+ #if XASH_LITTLE_ENDIAN
63
+ uint16_t type ;
64
+ uint8_t ip6_0 [2 ];
65
+ #elif XASH_BIG_ENDIAN
66
+ uint8_t ip6_0 [2 ];
67
+ uint16_t type ;
68
+ #else
69
+ #error
70
+ #endif
71
+
49
72
union
50
73
{
51
74
// IPv6 struct
75
+ uint8_t ip6_1 [14 ];
52
76
struct
53
77
{
54
- uint16_t type6 ;
55
- uint8_t ip6 [16 ];
56
- };
57
- struct
58
- {
59
- uint32_t type ; // must be netadrtype_t but will break with short enums
60
78
union
61
79
{
62
- uint8_t ip [4 ];
63
- uint32_t ip4 ; // for easier conversions
80
+ uint8_t ip [4 ];
81
+ uint32_t ip4 ; // for easier conversions
64
82
};
65
83
uint8_t ipx [10 ];
66
84
};
67
85
};
68
- uint16_t port ;
86
+ uint16_t port ;
69
87
} netadr_t ;
70
88
#pragma pack( pop )
71
89
90
+ static inline netadrtype_t NET_NetadrType ( const netadr_t * a )
91
+ {
92
+ if ( a -> type == NA_IP6 || a -> type == NA_MULTICAST_IP6 )
93
+ return (netadrtype_t )a -> type ;
94
+
95
+ if ( a -> ip6_0 [0 ] || a -> ip6_0 [1 ] )
96
+ return NA_UNDEFINED ;
97
+
98
+ return (netadrtype_t )a -> type ;
99
+ }
100
+
101
+ static inline void NET_NetadrSetType ( netadr_t * a , netadrtype_t type )
102
+ {
103
+ if ( type == NA_IP6 || type == NA_MULTICAST_IP6 )
104
+ {
105
+ a -> type = type ;
106
+ return ;
107
+ }
108
+
109
+ a -> ip6_0 [0 ] = a -> ip6_0 [1 ] = 0 ;
110
+ a -> type = type ;
111
+ }
112
+
72
113
STATIC_CHECK_SIZEOF ( netadr_t , 20 , 20 );
73
114
74
115
#endif // NET_ADR_H
0 commit comments