1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2025-07-10 01:34:58 +03:00

Change IP structure so that IPv4 addresses are stored in RFC3493 format

In addition to saving 4 bytes for each instantiation, this change makes IP-related operations faster and clearer.

https://tools.ietf.org/html/rfc3493.html#section-3.7
This commit is contained in:
Davide Beatrici
2021-04-07 21:24:55 +02:00
parent 01663f836d
commit 1708998a11
21 changed files with 295 additions and 403 deletions

View File

@ -425,14 +425,14 @@ IPC *NewIPC(CEDAR *cedar, char *client_name, char *postfix, char *hubname, char
info.ServerIpAddress = IPToUINT(&s->RemoteIP);
info.ServerPort = Endian32(s->RemotePort);
StrCpy(info.HubName, sizeof(info.HubName), hubname);
Copy(info.UniqueId, unique, 16);
Copy(info.UniqueId, unique, sizeof(info.UniqueId));
if (IsIP6(&s->LocalIP))
{
Copy(info.ClientIpAddress6, s->LocalIP.ipv6_addr, 16);
Copy(info.ClientIpAddress6, s->LocalIP.address, sizeof(info.ClientIpAddress6));
}
if (IsIP6(&s->RemoteIP))
{
Copy(info.ServerIpAddress6, s->RemoteIP.ipv6_addr, 16);
Copy(info.ServerIpAddress6, s->RemoteIP.address, sizeof(info.ServerIpAddress6));
}
OutRpcNodeInfo(p, &info);
@ -488,7 +488,16 @@ IPC *NewIPC(CEDAR *cedar, char *client_name, char *postfix, char *hubname, char
{
UINTToIP(&ipc->DefaultGateway, hub->Option->DefaultGateway);
UINTToIP(&ipc->SubnetMask, hub->Option->DefaultSubnet);
GetBroadcastAddress4(&ipc->BroadcastAddress, &ipc->DefaultGateway, &ipc->SubnetMask);
}
else
{
ZeroIP4(&ipc->DefaultGateway);
ZeroIP4(&ipc->SubnetMask);
ZeroIP4(&ipc->BroadcastAddress);
}
ZeroIP4(&ipc->ClientIPAddress);
MacToStr(macstr, sizeof(macstr), ipc->MacAddress);
@ -1401,23 +1410,23 @@ void IPCProcessL3EventsEx(IPC *ipc, UINT64 now)
{
ok = true;
}
else if (ip_dst.addr[0] == 255 && ip_dst.addr[1] == 255 &&
ip_dst.addr[2] == 255 && ip_dst.addr[3] == 255)
{
ok = true;
}
else if (ip_dst.addr[0] >= 224 && ip_dst.addr[0] <= 239)
{
ok = true;
}
else
{
if (CmpIpAddr(&ipc->BroadcastAddress, &ip_dst) == 0)
const BYTE *ipv4 = IPV4(ip_dst.address);
if (ipv4[0] == 255 && ipv4[1] == 255 && ipv4[2] == 255 && ipv4[3] == 255)
{
ok = true;
}
if (IsZeroIP(&ipc->ClientIPAddress))
else if (ipv4[0] >= 224 && ipv4[1] <= 239)
{
ok = true;
}
else if (CmpIpAddr(&ipc->BroadcastAddress, &ip_dst) == 0)
{
ok = true;
}
else if (IsZeroIP(&ipc->ClientIPAddress))
{
// Client IP address is undetermined
ok = true;
@ -1663,17 +1672,20 @@ void IPCSendIPv4(IPC *ipc, void *data, UINT size)
// Local Broadcast
is_broadcast = true;
}
if (ip_dst.addr[0] == 255 && ip_dst.addr[1] == 255 && ip_dst.addr[2] == 255 && ip_dst.addr[3] == 255)
else
{
// Global Broadcast
is_broadcast = true;
}
const BYTE *ipv4 = IPV4(ip_dst.address);
if (ip_dst.addr[0] >= 224 && ip_dst.addr[0] <= 239)
{
// IPv4 Multicast
is_broadcast = true;
if (ipv4[0] == 255 && ipv4[1] == 255 && ipv4[2] == 255 && ipv4[3] == 255)
{
// Global Broadcast
is_broadcast = true;
}
else if (ipv4[0] >= 224 && ipv4[0] <= 239)
{
// IPv4 Multicast
is_broadcast = true;
}
}
if (is_broadcast)
@ -2204,10 +2216,10 @@ bool IPCIPv6CheckExistingLinkLocal(IPC *ipc, UINT64 eui)
t.Name = ipc->HubName;
// Construct link local from eui
ZeroIP6(&i.Ip);
i.Ip.ipv6_addr[0] = 0xFE;
i.Ip.ipv6_addr[1] = 0x80;
Copy(&i.Ip.ipv6_addr[8], &eui, sizeof(UINT64));
Zero(&i.Ip, sizeof(i.Ip));
i.Ip.address[0] = 0xfe;
i.Ip.address[1] = 0x80;
Copy(&i.Ip.address[8], &eui, sizeof(eui));
h = Search(ipc->Cedar->HubList, &t);
@ -2235,7 +2247,7 @@ void IPCIPv6AddRouterPrefixes(IPC *ipc, ICMPV6_OPTION_LIST *recvPrefix, UCHAR *m
for (j = 0; j < LIST_NUM(ipc->IPv6RouterAdvs); j++)
{
IPC_IPV6_ROUTER_ADVERTISEMENT *existingRA = LIST_DATA(ipc->IPv6RouterAdvs, j);
if (Cmp(&recvPrefix->Prefix[i]->Prefix, &existingRA->RoutedPrefix.ipv6_addr, sizeof(IPV6_ADDR)) == 0)
if (Cmp(&recvPrefix->Prefix[i]->Prefix, &existingRA->RoutedPrefix.address, sizeof(IPV6_ADDR)) == 0)
{
foundPrefix = true;
break;
@ -2321,7 +2333,7 @@ UINT64 IPCIPv6GetServerEui(IPC *ipc)
// Generate the MAC address from the multicast address
destMacAddress[0] = 0x33;
destMacAddress[1] = 0x33;
Copy(&destMacAddress[2], &destIP.ipv6_addr[12], sizeof(UINT));
Copy(&destMacAddress[2], &destIP.address[12], sizeof(UINT));
IPToIPv6Addr(&destV6, &destIP);
@ -2355,7 +2367,7 @@ UINT64 IPCIPv6GetServerEui(IPC *ipc)
if (LIST_NUM(ipc->IPv6RouterAdvs) > 0)
{
IPC_IPV6_ROUTER_ADVERTISEMENT *ra = LIST_DATA(ipc->IPv6RouterAdvs, 0);
Copy(&ipc->IPv6ServerEUI, &ra->RouterAddress.ipv6_addr[8], sizeof(UINT64));
Copy(&ipc->IPv6ServerEUI, &ra->RouterAddress.address[8], sizeof(ipc->IPv6ServerEUI));
}
// If it is still not defined, let's just generate something random
@ -2408,10 +2420,10 @@ void IPCIPv6Send(IPC *ipc, void *data, UINT size)
// Constructing multicast MAC address based on destination IP address, then just fire and forget
destMac[0] = 0x33;
destMac[1] = 0x33;
destMac[2] = destAddr.ipv6_addr[12];
destMac[3] = destAddr.ipv6_addr[13];
destMac[4] = destAddr.ipv6_addr[14];
destMac[5] = destAddr.ipv6_addr[15];
destMac[2] = destAddr.address[12];
destMac[3] = destAddr.address[13];
destMac[4] = destAddr.address[14];
destMac[5] = destAddr.address[15];
IPCIPv6SendWithDestMacAddr(ipc, data, size, destMac);
return;
}