Process Hacker
phnet.h
Go to the documentation of this file.
1 #ifndef _PH_PHNET_H
2 #define _PH_PHNET_H
3 
4 #include <inaddr.h>
5 #include <in6addr.h>
6 
7 #define PH_IPV4_NETWORK_TYPE 0x1
8 #define PH_IPV6_NETWORK_TYPE 0x2
9 #define PH_NETWORK_TYPE_MASK 0x3
10 
11 #define PH_TCP_PROTOCOL_TYPE 0x10
12 #define PH_UDP_PROTOCOL_TYPE 0x20
13 #define PH_PROTOCOL_TYPE_MASK 0x30
14 
15 #define PH_NO_NETWORK_PROTOCOL 0x0
16 #define PH_TCP4_NETWORK_PROTOCOL (PH_IPV4_NETWORK_TYPE | PH_TCP_PROTOCOL_TYPE)
17 #define PH_TCP6_NETWORK_PROTOCOL (PH_IPV6_NETWORK_TYPE | PH_TCP_PROTOCOL_TYPE)
18 #define PH_UDP4_NETWORK_PROTOCOL (PH_IPV4_NETWORK_TYPE | PH_UDP_PROTOCOL_TYPE)
19 #define PH_UDP6_NETWORK_PROTOCOL (PH_IPV6_NETWORK_TYPE | PH_UDP_PROTOCOL_TYPE)
20 
21 typedef struct _PH_IP_ADDRESS
22 {
23  ULONG Type;
24  union
25  {
26  ULONG Ipv4;
27  struct in_addr InAddr;
28  UCHAR Ipv6[16];
29  struct in6_addr In6Addr;
30  };
32 
33 FORCEINLINE BOOLEAN PhEqualIpAddress(
34  _In_ PPH_IP_ADDRESS Address1,
35  _In_ PPH_IP_ADDRESS Address2
36  )
37 {
38  if ((Address1->Type | Address2->Type) == 0) // don't check addresses if both are invalid
39  return TRUE;
40  if (Address1->Type != Address2->Type)
41  return FALSE;
42 
43  if (Address1->Type == PH_IPV4_NETWORK_TYPE)
44  {
45  return Address1->Ipv4 == Address2->Ipv4;
46  }
47  else
48  {
49 #ifdef _WIN64
50  return
51  *(PULONG64)(Address1->Ipv6) == *(PULONG64)(Address2->Ipv6) &&
52  *(PULONG64)(Address1->Ipv6 + 8) == *(PULONG64)(Address2->Ipv6 + 8);
53 #else
54  return
55  *(PULONG)(Address1->Ipv6) == *(PULONG)(Address2->Ipv6) &&
56  *(PULONG)(Address1->Ipv6 + 4) == *(PULONG)(Address2->Ipv6 + 4) &&
57  *(PULONG)(Address1->Ipv6 + 8) == *(PULONG)(Address2->Ipv6 + 8) &&
58  *(PULONG)(Address1->Ipv6 + 12) == *(PULONG)(Address2->Ipv6 + 12);
59 #endif
60  }
61 }
62 
63 FORCEINLINE ULONG PhHashIpAddress(
64  _In_ PPH_IP_ADDRESS Address
65  )
66 {
67  ULONG hash = 0;
68 
69  if (Address->Type == 0)
70  return 0;
71 
72  hash = Address->Type | (Address->Type << 16);
73 
74  if (Address->Type == PH_IPV4_NETWORK_TYPE)
75  {
76  hash ^= Address->Ipv4;
77  }
78  else
79  {
80  hash += *(PULONG)(Address->Ipv6);
81  hash ^= *(PULONG)(Address->Ipv6 + 4);
82  hash += *(PULONG)(Address->Ipv6 + 8);
83  hash ^= *(PULONG)(Address->Ipv6 + 12);
84  }
85 
86  return hash;
87 }
88 
89 FORCEINLINE BOOLEAN PhIsNullIpAddress(
90  _In_ PPH_IP_ADDRESS Address
91  )
92 {
93  if (Address->Type == 0)
94  {
95  return TRUE;
96  }
97  else if (Address->Type == PH_IPV4_NETWORK_TYPE)
98  {
99  return Address->Ipv4 == 0;
100  }
101  else if (Address->Type == PH_IPV6_NETWORK_TYPE)
102  {
103 #ifdef _WIN64
104  return (*(PULONG64)(Address->Ipv6) | *(PULONG64)(Address->Ipv6 + 8)) == 0;
105 #else
106  return (*(PULONG)(Address->Ipv6) | *(PULONG)(Address->Ipv6 + 4) |
107  *(PULONG)(Address->Ipv6 + 8) | *(PULONG)(Address->Ipv6 + 12)) == 0;
108 #endif
109  }
110  else
111  {
112  return TRUE;
113  }
114 }
115 
116 typedef struct _PH_IP_ENDPOINT
117 {
119  ULONG Port;
121 
122 FORCEINLINE BOOLEAN PhEqualIpEndpoint(
123  _In_ PPH_IP_ENDPOINT Endpoint1,
124  _In_ PPH_IP_ENDPOINT Endpoint2
125  )
126 {
127  return
128  PhEqualIpAddress(&Endpoint1->Address, &Endpoint2->Address) &&
129  Endpoint1->Port == Endpoint2->Port;
130 }
131 
132 FORCEINLINE ULONG PhHashIpEndpoint(
133  _In_ PPH_IP_ENDPOINT Endpoint
134  )
135 {
136  return PhHashIpAddress(&Endpoint->Address) ^ Endpoint->Port;
137 }
138 
139 #endif