1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-11-23 01:49:53 +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

@ -5431,7 +5431,7 @@ UINT StGetSessionStatus(ADMIN *a, RPC_SESSION_STATUS *t)
t->ClientIp = IPToUINT(&s->Connection->ClientIp); t->ClientIp = IPToUINT(&s->Connection->ClientIp);
if (IsIP6(&s->Connection->ClientIp)) if (IsIP6(&s->Connection->ClientIp))
{ {
Copy(&t->ClientIp6, &s->Connection->ClientIp.ipv6_addr, sizeof(t->ClientIp6)); Copy(&t->ClientIp6, &s->Connection->ClientIp.address, sizeof(t->ClientIp6));
} }
CopyIP(&t->ClientIpAddress, &s->Connection->ClientIp); CopyIP(&t->ClientIpAddress, &s->Connection->ClientIp);
@ -9200,7 +9200,7 @@ UINT StSetHub(ADMIN *a, RPC_CREATE_HUB *t)
if (Cmp(t->HashedPassword, hash2, SHA1_SIZE) == 0 || Cmp(t->SecurePassword, hash1, SHA1_SIZE) == 0) if (Cmp(t->HashedPassword, hash2, SHA1_SIZE) == 0 || Cmp(t->SecurePassword, hash1, SHA1_SIZE) == 0)
{ {
if (a->ServerAdmin == false && a->Rpc->Sock->RemoteIP.addr[0] != 127) if (a->ServerAdmin == false && IsLocalHostIP(&a->Rpc->Sock->RemoteIP) == false)
{ {
// Refuse to set a blank password to hub admin from remote host // Refuse to set a blank password to hub admin from remote host
ReleaseHub(h); ReleaseHub(h);
@ -15370,7 +15370,7 @@ UINT AdminAccept(CONNECTION *c, PACK *p)
if (Cmp(secure_null_password, secure_password, SHA1_SIZE) == 0) if (Cmp(secure_null_password, secure_password, SHA1_SIZE) == 0)
{ {
if (sock->RemoteIP.addr[0] != 127) if (IsLocalHostIP(&sock->RemoteIP) == false)
{ {
// The client tried to use blank password for hub admin mode from remote // The client tried to use blank password for hub admin mode from remote
if (StrLen(hubname) != 0) if (StrLen(hubname) != 0)

View File

@ -10041,7 +10041,7 @@ bool CmIsEnabled(HWND hWnd, UINT id)
} }
case CMD_SHORTCUT: case CMD_SHORTCUT:
// Create a shortcut // Create a shortcut
if (cm->Client->Rpc->Sock->RemoteIP.addr[0] != 127) if (IsLocalHostIP(&cm->Client->Rpc->Sock->RemoteIP) == false)
{ {
return false; return false;
} }

View File

@ -1535,7 +1535,7 @@ void CnListenerProc(THREAD *thread, void *param)
AddRef(s->ref); AddRef(s->ref);
NoticeThreadInit(thread); NoticeThreadInit(thread);
if (s->LocalIP.addr[0] == 127) if (IsLocalHostIP(&s->LocalIP))
{ {
p = RecvPack(s); p = RecvPack(s);
@ -5110,7 +5110,7 @@ void CiRpcAccepted(CLIENT *c, SOCK *s)
retcode = 1; retcode = 1;
} }
if (c->PasswordRemoteOnly && s->RemoteIP.addr[0] == 127) if (c->PasswordRemoteOnly && IsLocalHostIP(&s->RemoteIP))
{ {
// If in a mode that requires a password only remote, // If in a mode that requires a password only remote,
// the password sent from localhost is considered to be always correct // the password sent from localhost is considered to be always correct
@ -5123,7 +5123,7 @@ void CiRpcAccepted(CLIENT *c, SOCK *s)
{ {
// If the remote control is prohibited, // If the remote control is prohibited,
// identify whether this connection is from remote // identify whether this connection is from remote
if (s->RemoteIP.addr[0] != 127) if (IsLocalHostIP(&s->RemoteIP) == false)
{ {
retcode = 2; retcode = 2;
} }

View File

@ -1156,7 +1156,7 @@ void EMMain(RPC *r)
if (t.IsWinPcapNeeded) if (t.IsWinPcapNeeded)
{ {
if (r->Sock->RemoteIP.addr[0] != 127) if (IsLocalHostIP(&r->Sock->RemoteIP) == false)
{ {
// WinPcap is required, but can not do anything because it is in remote management mode // WinPcap is required, but can not do anything because it is in remote management mode
MsgBox(NULL, MB_ICONINFORMATION, _UU("EM_WPCAP_REMOTE")); MsgBox(NULL, MB_ICONINFORMATION, _UU("EM_WPCAP_REMOTE"));

View File

@ -6752,11 +6752,13 @@ bool IsHubIpAddress(IP *ip)
return false; return false;
} }
if (ip->addr[0] == 172 && ip->addr[1] == 31) const BYTE *ipv4 = IPV4(ip->address);
if (ipv4[0] == 172 && ipv4[1] == 31)
{ {
if (ip->addr[2] >= 1 && ip->addr[2] <= 254) if (ipv4[2] >= 1 && ipv4[2] <= 254)
{ {
if (ip->addr[3] >= 1 && ip->addr[3] <= 254) if (ipv4[3] >= 1 && ipv4[3] <= 254)
{ {
return true; return true;
} }
@ -6810,11 +6812,7 @@ void GenHubIpAddress(IP *ip, char *name)
Sha0(hash, tmp2, StrLen(tmp2)); Sha0(hash, tmp2, StrLen(tmp2));
Zero(ip, sizeof(IP)); SetIP(ip, 172, 31, hash[0] % 254 + 1, hash[0] % 254 + 1);
ip->addr[0] = 172;
ip->addr[1] = 31;
ip->addr[2] = hash[0] % 254 + 1;
ip->addr[3] = hash[1] % 254 + 1;
} }
// Generate a MAC address for the Virtual HUB // Generate a MAC address for the Virtual HUB

View File

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

View File

@ -683,30 +683,26 @@ UINT GenerateDummyMark(PRAND *p)
// Generate a dummy IP // Generate a dummy IP
void GenerateDummyIp(PRAND *p, IP *ip) void GenerateDummyIp(PRAND *p, IP *ip)
{ {
UINT i;
if (p == NULL || ip == NULL) if (p == NULL || ip == NULL)
{ {
return; return;
} }
Zero(ip, sizeof(IP)); ZeroIP4(ip);
BYTE *ipv4 = IPV4(ip->address);
for (i = 1;i < 4;i++) for (BYTE i = 1; i < IPV4_SIZE; ++i)
{ {
UINT v = 0; BYTE v = 0;
while (true) while (v == 0 || v > 254)
{ {
v = PRandInt(p) % 256; v = PRandInt(p) % 256;
if (v >= 1 && v <= 254)
{
break;
}
} }
ip->addr[i] = (UCHAR)v; IPV4(ip->address)[i] = v;
} }
ip->addr[0] = 127; IPV4(ip->address)[0] = 127;
} }
// Search an entry // Search an entry

View File

@ -134,47 +134,21 @@ UINT ProtoSessionHash(void *p)
} }
ip = &session->SrcIp; ip = &session->SrcIp;
if (IsIP6(ip)) for (BYTE i = 0; i < sizeof(ip->address); ++i)
{ {
UINT i; ret += ip->address[i];
for (i = 0; i < sizeof(ip->ipv6_addr); ++i)
{
ret += ip->ipv6_addr[i];
} }
ret += ip->ipv6_scope_id; ret += ip->ipv6_scope_id;
}
else
{
UINT i;
for (i = 0; i < sizeof(ip->addr); ++i)
{
ret += ip->addr[i];
}
}
ret += session->SrcPort; ret += session->SrcPort;
ip = &session->DstIp; ip = &session->DstIp;
if (IsIP6(ip)) for (BYTE i = 0; i < sizeof(ip->address); ++i)
{ {
UINT i; ret += ip->address[i];
for (i = 0; i < sizeof(ip->ipv6_addr); ++i)
{
ret += ip->ipv6_addr[i];
} }
ret += ip->ipv6_scope_id; ret += ip->ipv6_scope_id;
}
else
{
UINT i;
for (i = 0; i < sizeof(ip->addr); ++i)
{
ret += ip->addr[i];
}
}
ret += session->DstPort; ret += session->DstPort;
return ret; return ret;

View File

@ -143,8 +143,8 @@ void IPsecSendPacketByIPsecSa(IKE_SERVER *ike, IPSECSA *sa, UCHAR *data, UINT da
h.PayloadLength = Endian16(data_size); h.PayloadLength = Endian16(data_size);
h.NextHeader = protocol_id; h.NextHeader = protocol_id;
h.HopLimit = 64; h.HopLimit = 64;
Copy(h.SrcAddress.Value, c->TunnelModeServerIP.ipv6_addr, 16); Copy(h.SrcAddress.Value, c->TunnelModeServerIP.address, sizeof(h.SrcAddress.Value));
Copy(h.DestAddress.Value, c->TunnelModeClientIP.ipv6_addr, 16); Copy(h.DestAddress.Value, c->TunnelModeClientIP.address, sizeof(h.DestAddress.Value));
WriteBuf(b, &h, sizeof(IPV6_HEADER)); WriteBuf(b, &h, sizeof(IPV6_HEADER));
@ -359,16 +359,16 @@ void IPsecSendUdpPacket(IKE_SERVER *ike, IKE_CLIENT *c, UINT src_port, UINT dst_
{ {
if (IsIPsecSaTunnelMode(c->CurrentIpSecSaSend) == false) if (IsIPsecSaTunnelMode(c->CurrentIpSecSaSend) == false)
{ {
u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TransportModeServerIP.ipv6_addr, u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TransportModeServerIP.address,
(IPV6_ADDR *)c->TransportModeClientIP.ipv6_addr, (IPV6_ADDR *)c->TransportModeClientIP.address,
IP_PROTO_UDP, IP_PROTO_UDP,
u, u,
udp_size, 0); udp_size, 0);
} }
else else
{ {
u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TunnelModeServerIP.ipv6_addr, u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TunnelModeServerIP.address,
(IPV6_ADDR *)c->TunnelModeClientIP.ipv6_addr, (IPV6_ADDR *)c->TunnelModeClientIP.address,
IP_PROTO_UDP, IP_PROTO_UDP,
u, u,
udp_size, 0); udp_size, 0);
@ -2907,12 +2907,12 @@ void ProcIkeAggressiveModePacketRecv(IKE_SERVER *ike, UDPPACKET *p, IKE_PACKET *
if (IsIP6(&sa->IkeClient->ServerIP)) if (IsIP6(&sa->IkeClient->ServerIP))
{ {
// IPv6 address // IPv6 address
my_id_payload = IkeNewIdPayload(IKE_ID_IPV6_ADDR, 0, 0, sa->IkeClient->ServerIP.ipv6_addr, 16); my_id_payload = IkeNewIdPayload(IKE_ID_IPV6_ADDR, 0, 0, sa->IkeClient->ServerIP.address, 16);
} }
else else
{ {
// IPv4 address // IPv4 address
my_id_payload = IkeNewIdPayload(IKE_ID_IPV4_ADDR, 0, 0, sa->IkeClient->ServerIP.addr, 4); my_id_payload = IkeNewIdPayload(IKE_ID_IPV4_ADDR, 0, 0, IPV4(sa->IkeClient->ServerIP.address), IPV4_SIZE);
} }
// Build the ID payload tentatively // Build the ID payload tentatively
@ -3411,12 +3411,12 @@ void ProcIkeMainModePacketRecv(IKE_SERVER *ike, UDPPACKET *p, IKE_PACKET *header
if (IsIP6(&sa->IkeClient->ServerIP)) if (IsIP6(&sa->IkeClient->ServerIP))
{ {
// IPv6 address // IPv6 address
my_id_payload = IkeNewIdPayload(IKE_ID_IPV6_ADDR, 0, 0, sa->IkeClient->ServerIP.ipv6_addr, 16); my_id_payload = IkeNewIdPayload(IKE_ID_IPV6_ADDR, 0, 0, sa->IkeClient->ServerIP.address, 16);
} }
else else
{ {
// IPv4 address // IPv4 address
my_id_payload = IkeNewIdPayload(IKE_ID_IPV4_ADDR, 0, 0, sa->IkeClient->ServerIP.addr, 4); my_id_payload = IkeNewIdPayload(IKE_ID_IPV4_ADDR, 0, 0, IPV4(sa->IkeClient->ServerIP.address), IPV4_SIZE);
} }
// Build the ID payload tentatively // Build the ID payload tentatively
@ -3687,11 +3687,11 @@ BUF *IkeCalcNatDetectHash(IKE_SERVER *ike, IKE_HASH *hash, UINT64 initiator_cook
if (IsIP6(ip)) if (IsIP6(ip))
{ {
WriteBuf(b, ip->ipv6_addr, sizeof(ip->ipv6_addr)); WriteBuf(b, ip->address, sizeof(ip->address));
} }
else else
{ {
WriteBuf(b, ip->addr, sizeof(ip->addr)); WriteBuf(b, IPV4(ip->address), IPV4_SIZE);
} }
us = Endian16((USHORT)port); us = Endian16((USHORT)port);

View File

@ -382,11 +382,11 @@ BUF *IkeBuildNatOaPayload(IKE_PACKET_NAT_OA_PAYLOAD *t)
if (IsIP6(&t->IpAddress)) if (IsIP6(&t->IpAddress))
{ {
WriteBuf(ret, t->IpAddress.ipv6_addr, 16); WriteBuf(ret, t->IpAddress.address, sizeof(t->IpAddress.address));
} }
else else
{ {
WriteBuf(ret, t->IpAddress.addr, 4); WriteBuf(ret, IPV4(t->IpAddress.address), IPV4_SIZE);
} }
return ret; return ret;
@ -1233,8 +1233,8 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b)
return false; return false;
} }
Zero(&ip, sizeof(ip)); ZeroIP4(&ip);
Zero(&subnet, sizeof(subnet)); ZeroIP4(&subnet);
// Convert to string // Convert to string
Zero(t->StrData, sizeof(t->StrData)); Zero(t->StrData, sizeof(t->StrData));
@ -1247,9 +1247,9 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b)
break; break;
case IKE_ID_IPV4_ADDR: case IKE_ID_IPV4_ADDR:
if (t->IdData->Size == 4) if (t->IdData->Size == IPV4_SIZE)
{ {
Copy(ip.addr, t->IdData->Buf, 4); Copy(IPV4(ip.address), t->IdData->Buf, IPV4_SIZE);
IPToStr(t->StrData, sizeof(t->StrData), &ip); IPToStr(t->StrData, sizeof(t->StrData), &ip);
} }
@ -1265,12 +1265,12 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b)
break; break;
case IKE_ID_IPV4_ADDR_SUBNET: case IKE_ID_IPV4_ADDR_SUBNET:
if (t->IdData->Size == 8) if (t->IdData->Size == IPV4_SIZE * 2)
{ {
char ipstr[MAX_SIZE]; char ipstr[MAX_SIZE];
char subnetstr[MAX_SIZE]; char subnetstr[MAX_SIZE];
Copy(ip.addr, t->IdData->Buf, 4); Copy(IPV4(ip.address), t->IdData->Buf, IPV4_SIZE);
Copy(subnet.addr, ((UCHAR *)t->IdData->Buf) + 4, 4); Copy(IPV4(subnet.address), ((BYTE *)t->IdData->Buf) + IPV4_SIZE, IPV4_SIZE);
IPToStr(ipstr, sizeof(ipstr), &ip); IPToStr(ipstr, sizeof(ipstr), &ip);
MaskToStr(subnetstr, sizeof(subnetstr), &subnet); MaskToStr(subnetstr, sizeof(subnetstr), &subnet);

View File

@ -2916,7 +2916,7 @@ int OvsCompareSessionList(void *p1, void *p2)
return 0; return 0;
} }
i = CmpIpAddr(&s1->Protocol, &s2->Protocol); i = Cmp(&s1->Protocol, &s2->Protocol, sizeof(s1->Protocol));
if (i != 0) if (i != 0)
{ {
return i; return i;

View File

@ -155,12 +155,12 @@ void IPsecWin7UpdateHostIPAddressList(IPSEC_WIN7 *w)
if (IsIP4(ip)) if (IsIP4(ip))
{ {
a.IpVersion = 4; a.IpVersion = 4;
Copy(a.IpAddress.IPv4Address, ip->addr, 4); Copy(a.IpAddress.IPv4Address, IPV4(ip->address), sizeof(a.IpAddress.IPv4Address));
} }
else else
{ {
a.IpVersion = 6; a.IpVersion = 6;
Copy(a.IpAddress.IPv6Address, ip->ipv6_addr, 16); Copy(a.IpAddress.IPv6Address, ip->address, sizeof(a.IpAddress.IPv6Address));
} }
WriteBuf(buf, &a, sizeof(WFP_LOCAL_IP)); WriteBuf(buf, &a, sizeof(WFP_LOCAL_IP));

View File

@ -2117,7 +2117,7 @@ bool ServerAccept(CONNECTION *c)
if (is_empty_password) if (is_empty_password)
{ {
const SOCK *s = c->FirstSock; const SOCK *s = c->FirstSock;
if (s != NULL && s->RemoteIP.addr[0] != 127) if (s != NULL && IsLocalHostIP(&s->RemoteIP) == false)
{ {
if (StrCmpi(username, ADMINISTRATOR_USERNAME) == 0 || GetHubAdminOption(hub, "deny_empty_password") != 0) if (StrCmpi(username, ADMINISTRATOR_USERNAME) == 0 || GetHubAdminOption(hub, "deny_empty_password") != 0)
{ {
@ -3834,7 +3834,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c)
} }
else else
{ {
Copy(info->ClientIpAddress6, c->FirstSock->LocalIP.ipv6_addr, sizeof(info->ClientIpAddress6)); Copy(info->ClientIpAddress6, c->FirstSock->LocalIP.address, sizeof(info->ClientIpAddress6));
} }
// Client port number // Client port number
info->ClientPort = Endian32(c->FirstSock->LocalPort); info->ClientPort = Endian32(c->FirstSock->LocalPort);
@ -3850,7 +3850,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c)
} }
else else
{ {
Copy(info->ServerIpAddress6, ip.ipv6_addr, sizeof(info->ServerIpAddress6)); Copy(info->ServerIpAddress6, ip.address, sizeof(info->ServerIpAddress6));
} }
} }
// Server port number // Server port number
@ -3868,7 +3868,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c)
} }
else else
{ {
Copy(&info->ProxyIpAddress6, c->FirstSock->RemoteIP.ipv6_addr, sizeof(info->ProxyIpAddress6)); Copy(&info->ProxyIpAddress6, c->FirstSock->RemoteIP.address, sizeof(info->ProxyIpAddress6));
} }
info->ProxyPort = Endian32(c->FirstSock->RemotePort); info->ProxyPort = Endian32(c->FirstSock->RemotePort);
@ -5674,25 +5674,18 @@ bool ClientUploadAuth(CONNECTION *c)
// UDP acceleration function using flag // UDP acceleration function using flag
if (o->NoUdpAcceleration == false && c->Session->UdpAccel != NULL) if (o->NoUdpAcceleration == false && c->Session->UdpAccel != NULL)
{ {
IP my_ip;
Zero(&my_ip, sizeof(my_ip));
PackAddBool(p, "use_udp_acceleration", true); PackAddBool(p, "use_udp_acceleration", true);
PackAddInt(p, "udp_acceleration_version", c->Session->UdpAccel->Version); PackAddInt(p, "udp_acceleration_version", c->Session->UdpAccel->Version);
Copy(&my_ip, &c->Session->UdpAccel->MyIp, sizeof(IP)); IP my_ip;
if (IsLocalHostIP(&my_ip)) if (IsLocalHostIP(&c->Session->UdpAccel->MyIp) == false)
{ {
if (IsIP4(&my_ip)) Copy(&my_ip, &c->Session->UdpAccel->MyIp, sizeof(my_ip));
{
ZeroIP4(&my_ip);
} }
else else
{ {
ZeroIP6(&my_ip); Zero(&my_ip, sizeof(my_ip));
}
} }
PackAddIp(p, "udp_acceleration_client_ip", &my_ip); PackAddIp(p, "udp_acceleration_client_ip", &my_ip);
@ -6058,7 +6051,7 @@ bool ServerDownloadSignature(CONNECTION *c, char **error_detail_str)
} }
if (c->FirstSock->RemoteIP.addr[0] == 127) if (IsLocalHostIP(&c->FirstSock->RemoteIP))
{ {
if (StrCmpi(h->Target, HTTP_SAITAMA) == 0) if (StrCmpi(h->Target, HTTP_SAITAMA) == 0)
{ {

View File

@ -8267,7 +8267,7 @@ void SmBridgeDlg(HWND hWnd, SM_SERVER *s)
if (t.IsWinPcapNeeded) if (t.IsWinPcapNeeded)
{ {
if (s->Rpc->Sock->RemoteIP.addr[0] != 127) if (IsLocalHostIP(&s->Rpc->Sock->RemoteIP) == false)
{ {
// WinPcap is required, but can not do anything because it is in remote control mode // WinPcap is required, but can not do anything because it is in remote control mode
MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_REMOTE")); MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_REMOTE"));

View File

@ -2255,15 +2255,14 @@ bool NnParseDnsResponsePacket(UCHAR *data, UINT size, IP *ret_ip)
if (r != NULL) if (r != NULL)
{ {
if (tp == 0x0001 && cl == 0x0001 && r->Size == 4) if (tp == 0x0001 && cl == 0x0001 && r->Size == IPV4_SIZE)
{ {
ret = true; ret = true;
if (ret_ip != NULL) if (ret_ip != NULL)
{ {
Zero(ret_ip, sizeof(IP)); ZeroIP4(ret_ip);
Copy(IPV4(ret_ip->address), r->Buf, IPV4_SIZE);
Copy(ret_ip->addr, r->Buf, 4);
} }
} }
@ -3708,10 +3707,10 @@ bool ArpaToIP(IP *ip, char *str)
{ {
// Convert the token [0, 1, 2, 3] to IP // Convert the token [0, 1, 2, 3] to IP
UINT i; UINT i;
Zero(ip, sizeof(IP)); ZeroIP4(ip);
for (i = 0; i < 4; i++) for (i = 0; i < IPV4_SIZE; ++i)
{ {
ip->addr[i] = (UCHAR)ToInt(token->Token[3 - i]); IPV4(ip->address)[i] = (UCHAR)ToInt(token->Token[3 - i]);
} }
ret = true; ret = true;
} }
@ -5536,7 +5535,7 @@ void VirtualTcpReceived(VH *v, UINT src_ip, UINT dest_ip, void *data, UINT size,
} }
UINTToIP(&ip1, src_ip); UINTToIP(&ip1, src_ip);
UINTToIP(&ip2, dest_ip); UINTToIP(&ip2, dest_ip);
if (ip1.addr[0] == 127 || ip2.addr[0] == 127) if (IsLocalHostIP4(&ip1) || IsLocalHostIP4(&ip2))
{ {
// Loopback IP address can not be specified // Loopback IP address can not be specified
return; return;

View File

@ -1207,7 +1207,7 @@ void NicInfoRefresh(HWND hWnd, UI_NICINFO *info)
{ {
Copy(&ip, &a->IpAddresses[i], sizeof(IP)); Copy(&ip, &a->IpAddresses[i], sizeof(IP));
if (!(ip.addr[0] == 169 && ip.addr[1] == 254)) if (!(IPV4(ip.address)[0] == 169 && IPV4(ip.address)[1] == 254))
{ {
has_ip = true; has_ip = true;
} }

View File

@ -4185,7 +4185,7 @@ void RUDPGetRegisterHostNameByIP(char *dst, UINT size, IP *ip)
{ {
UCHAR hash[SHA1_SIZE]; UCHAR hash[SHA1_SIZE];
Sha1(hash, ip->addr, 4); Sha1(hash, IPV4(ip->address), IPV4_SIZE);
BinToStr(tmp, sizeof(tmp), hash, 2); BinToStr(tmp, sizeof(tmp), hash, 2);
} }
else else
@ -6476,20 +6476,21 @@ bool IsSubnetMask6(IP *a)
// Generate a local address from the MAC address // Generate a local address from the MAC address
void GenerateEui64LocalAddress(IP *a, UCHAR *mac) void GenerateEui64LocalAddress(IP *a, UCHAR *mac)
{ {
UCHAR tmp[8];
// Validate arguments // Validate arguments
if (a == NULL || mac == NULL) if (a == NULL || mac == NULL)
{ {
return; return;
} }
Zero(a, sizeof(IP));
UCHAR tmp[8];
GenerateEui64Address6(tmp, mac); GenerateEui64Address6(tmp, mac);
ZeroIP6(a); a->address[0] = 0xfe;
a->ipv6_addr[0] = 0xfe; a->address[1] = 0x80;
a->ipv6_addr[1] = 0x80;
Copy(&a->ipv6_addr[8], tmp, 8); Copy(&a->address[8], tmp, sizeof(tmp));
} }
// Generate the EUI-64 address from the MAC address // Generate the EUI-64 address from the MAC address
@ -6620,7 +6621,7 @@ UINT GetIPAddrType6(IP *ip)
return 0; return 0;
} }
if (ip->ipv6_addr[0] == 0xff) if (ip->address[0] == 0xff)
{ {
IP all_node, all_router; IP all_node, all_router;
@ -6630,20 +6631,20 @@ UINT GetIPAddrType6(IP *ip)
ret |= IPV6_ADDR_MULTICAST; ret |= IPV6_ADDR_MULTICAST;
if (Cmp(ip->ipv6_addr, all_node.ipv6_addr, 16) == 0) if (CmpIpAddr(ip, &all_node) == 0)
{ {
ret |= IPV6_ADDR_ALL_NODE_MULTICAST; ret |= IPV6_ADDR_ALL_NODE_MULTICAST;
} }
else if (Cmp(ip->ipv6_addr, all_router.ipv6_addr, 16) == 0) else if (CmpIpAddr(ip, &all_router) == 0)
{ {
ret |= IPV6_ADDR_ALL_ROUTER_MULTICAST; ret |= IPV6_ADDR_ALL_ROUTER_MULTICAST;
} }
else else
{ {
if (ip->ipv6_addr[1] == 0x02 && ip->ipv6_addr[2] == 0 && ip->ipv6_addr[3] == 0 && if (ip->address[1] == 0x02 && ip->address[2] == 0 && ip->address[3] == 0 &&
ip->ipv6_addr[4] == 0 && ip->ipv6_addr[5] == 0 && ip->ipv6_addr[6] == 0 && ip->address[4] == 0 && ip->address[5] == 0 && ip->address[6] == 0 &&
ip->ipv6_addr[7] == 0 && ip->ipv6_addr[8] == 0 && ip->ipv6_addr[9] == 0 && ip->address[7] == 0 && ip->address[8] == 0 && ip->address[9] == 0 &&
ip->ipv6_addr[10] == 0 && ip->ipv6_addr[11] == 0x01 && ip->ipv6_addr[12] == 0xff) ip->address[10] == 0 && ip->address[11] == 0x01 && ip->address[12] == 0xff)
{ {
ret |= IPV6_ADDR_SOLICIATION_MULTICAST; ret |= IPV6_ADDR_SOLICIATION_MULTICAST;
} }
@ -6653,7 +6654,7 @@ UINT GetIPAddrType6(IP *ip)
{ {
ret |= IPV6_ADDR_UNICAST; ret |= IPV6_ADDR_UNICAST;
if (ip->ipv6_addr[0] == 0xfe && (ip->ipv6_addr[1] & 0xc0) == 0x80) if (ip->address[0] == 0xfe && (ip->address[1] & 0xc0) == 0x80)
{ {
ret |= IPV6_ADDR_LOCAL_UNICAST; ret |= IPV6_ADDR_LOCAL_UNICAST;
} }
@ -6661,7 +6662,7 @@ UINT GetIPAddrType6(IP *ip)
{ {
ret |= IPV6_ADDR_GLOBAL_UNICAST; ret |= IPV6_ADDR_GLOBAL_UNICAST;
if (IsZero(&ip->ipv6_addr, 16)) if (IsZero(&ip->address, sizeof(ip->address)))
{ {
ret |= IPV6_ADDR_ZERO; ret |= IPV6_ADDR_ZERO;
} }
@ -6671,7 +6672,7 @@ UINT GetIPAddrType6(IP *ip)
GetLoopbackAddress6(&loopback); GetLoopbackAddress6(&loopback);
if (Cmp(ip->ipv6_addr, loopback.ipv6_addr, 16) == 0) if (Cmp(ip->address, loopback.address, sizeof(ip->address)) == 0)
{ {
ret |= IPV6_ADDR_LOOPBACK; ret |= IPV6_ADDR_LOOPBACK;
} }
@ -6691,9 +6692,9 @@ void GetLoopbackAddress6(IP *ip)
return; return;
} }
ZeroIP6(ip); Zero(ip, sizeof(IP));
ip->ipv6_addr[15] = 0x01; ip->address[15] = 0x01;
} }
// All-nodes multicast address // All-nodes multicast address
@ -6705,11 +6706,11 @@ void GetAllNodeMulticaseAddress6(IP *ip)
return; return;
} }
ZeroIP6(ip); Zero(ip, sizeof(IP));
ip->ipv6_addr[0] = 0xff; ip->address[0] = 0xff;
ip->ipv6_addr[1] = 0x02; ip->address[1] = 0x02;
ip->ipv6_addr[15] = 0x01; ip->address[15] = 0x01;
} }
// All-routers multicast address // All-routers multicast address
@ -6721,44 +6722,40 @@ void GetAllRouterMulticastAddress6(IP *ip)
return; return;
} }
ZeroIP6(ip); Zero(ip, sizeof(IP));
ip->ipv6_addr[0] = 0xff; ip->address[0] = 0xff;
ip->ipv6_addr[1] = 0x02; ip->address[1] = 0x02;
ip->ipv6_addr[15] = 0x02; ip->address[15] = 0x02;
} }
// Logical operation of the IPv4 address // Logical operation of the IPv4 address
void IPAnd4(IP *dst, IP *a, IP *b) void IPAnd4(IP *dst, IP *a, IP *b)
{ {
UINT i;
// Validate arguments // Validate arguments
if (dst == NULL || a == NULL || b == NULL || IsIP4(a) == false || IsIP4(b) == false) if (dst == NULL || a == NULL || b == NULL || IsIP4(a) == false || IsIP4(b) == false)
{ {
Zero(dst, sizeof(IP)); ZeroIP4(dst);
return; return;
} }
i = IPToUINT(a) & IPToUINT(b); UINTToIP(dst, IPToUINT(a) & IPToUINT(b));
UINTToIP(dst, i);
} }
// Logical operation of the IPv6 address // Logical operation of the IPv6 address
void IPAnd6(IP *dst, IP *a, IP *b) void IPAnd6(IP *dst, IP *a, IP *b)
{ {
UINT i; Zero(dst, sizeof(IP));
// Validate arguments // Validate arguments
if (dst == NULL || IsIP6(a) == false || IsIP6(b) == false) if (dst == NULL || IsIP6(a) == false || IsIP6(b) == false)
{ {
ZeroIP6(dst);
return; return;
} }
ZeroIP6(dst); for (BYTE i = 0; i < sizeof(dst->address); ++i)
for (i = 0; i < 16; i++)
{ {
dst->ipv6_addr[i] = a->ipv6_addr[i] & b->ipv6_addr[i]; dst->address[i] = a->address[i] & b->address[i];
} }
} }
@ -6770,17 +6767,17 @@ void IntToSubnetMask6(IP *ip, UINT i)
UINT z; UINT z;
IP a; IP a;
ZeroIP6(&a); Zero(&a, sizeof(IP));
for (z = 0; z < 16; z++) for (z = 0; z < sizeof(a.address); ++z)
{ {
if (z < j) if (z < j)
{ {
a.ipv6_addr[z] = 0xff; a.address[z] = 0xff;
} }
else if (z == j) else if (z == j)
{ {
a.ipv6_addr[z] = ~(0xff >> k); a.address[z] = ~(0xff >> k);
} }
} }
@ -6837,7 +6834,7 @@ void IPToStr6Inner(char *str, IP *ip)
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
Copy(&values[i], &a.ipv6_addr[i * 2], sizeof(USHORT)); Copy(&values[i], &a.address[i * 2], sizeof(USHORT));
values[i] = Endian16(values[i]); values[i] = Endian16(values[i]);
} }
@ -6934,7 +6931,7 @@ bool StrToIP6(IP *ip, char *str)
return false; return false;
} }
ZeroIP6(&a); Zero(&a, sizeof(a));
StrCpy(tmp, sizeof(tmp), str); StrCpy(tmp, sizeof(tmp), str);
Trim(tmp); Trim(tmp);
@ -7006,8 +7003,8 @@ bool StrToIP6(IP *ip, char *str)
IPItemStrToChars6(chars, str); IPItemStrToChars6(chars, str);
a.ipv6_addr[k++] = chars[0]; a.address[k++] = chars[0];
a.ipv6_addr[k++] = chars[1]; a.address[k++] = chars[1];
} }
} }
@ -7163,18 +7160,9 @@ void ZeroIP4(IP *ip)
} }
Zero(ip, sizeof(IP)); Zero(ip, sizeof(IP));
}
// Create an IPv6 address of all zero ip->address[10] = 0xff;
void ZeroIP6(IP *ip) ip->address[11] = 0xff;
{
// Validate arguments
if (ip == NULL)
{
return;
}
SetIP6(ip, NULL);
} }
// Get the IP address of the localhost // Get the IP address of the localhost
@ -7185,9 +7173,10 @@ void GetLocalHostIP6(IP *ip)
{ {
return; return;
} }
ZeroIP6(ip);
ip->ipv6_addr[15] = 1; Zero(ip, sizeof(IP));
ip->address[15] = 1;
} }
void GetLocalHostIP4(IP *ip) void GetLocalHostIP4(IP *ip)
{ {
@ -7235,7 +7224,7 @@ bool IsLocalHostIP4(IP *ip)
return false; return false;
} }
if (ip->addr[0] == 127) if (IPV4(ip->address)[0] == 127)
{ {
return true; return true;
} }
@ -7289,9 +7278,9 @@ bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip)
return false; return false;
} }
for (i = 0; i < 16; i++) for (i = 0; i < sizeof(addr->Value); ++i)
{ {
addr->Value[i] = ip->ipv6_addr[i]; addr->Value[i] = ip->address[i];
} }
return true; return true;
@ -7301,45 +7290,20 @@ bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip)
void SetIP6(IP *ip, UCHAR *value) void SetIP6(IP *ip, UCHAR *value)
{ {
// Validate arguments // Validate arguments
if (ip == NULL) if (ip == NULL || value == NULL)
{ {
return; return;
} }
Zero(ip, sizeof(IP)); Zero(ip, sizeof(IP));
ip->addr[0] = 192; for (BYTE i = 0; i < sizeof(ip->address); ++i)
ip->addr[1] = 0;
ip->addr[2] = 2;
ip->addr[3] = 254;
if (value != NULL)
{ {
UINT i; ip->address[i] = value[i];
for (i = 0; i < 16; i++)
{
ip->ipv6_addr[i] = value[i];
}
} }
} }
// Check whether the specified address is a IPv6 address // Check whether the specified address is IPv4
bool IsIP6(IP *ip)
{
// Validate arguments
if (ip == NULL)
{
return false;
}
if (ip->addr[0] == 192 && ip->addr[1] == 0 && ip->addr[2] == 2 && ip->addr[3] == 254)
{
return true;
}
return false;
}
bool IsIP4(IP *ip) bool IsIP4(IP *ip)
{ {
// Validate arguments // Validate arguments
@ -7348,7 +7312,17 @@ bool IsIP4(IP *ip)
return false; return false;
} }
return (IsIP6(ip) ? false : true); if (IsZero(ip->address, 10) == false)
{
return false;
}
if (ip->address[10] != 0xff || ip->address[11] != 0xff)
{
return false;
}
return true;
} }
// Copy the IP address // Copy the IP address
@ -7361,7 +7335,6 @@ void CopyIP(IP *dst, IP *src)
// Identify whether the IP address is a normal unicast address // Identify whether the IP address is a normal unicast address
bool IsValidUnicastIPAddress4(IP *ip) bool IsValidUnicastIPAddress4(IP *ip)
{ {
UINT i;
// Validate arguments // Validate arguments
if (IsIP4(ip) == false) if (IsIP4(ip) == false)
{ {
@ -7373,17 +7346,18 @@ bool IsValidUnicastIPAddress4(IP *ip)
return false; return false;
} }
if (ip->addr[0] >= 224 && ip->addr[0] <= 239) const BYTE *ipv4 = IPV4(ip->address);
if (ipv4[0] >= 224 && ipv4[0] <= 239)
{ {
// IPv4 Multicast // IPv4 Multicast
return false; return false;
} }
/// TODO: this is kinda incorrect, but for the correct parsing we need the netmask anyway /// TODO: this is kinda incorrect, but for the correct parsing we need the netmask anyway
for (i = 0; i < 4; i++) for (BYTE i = 0; i < IPV4_SIZE; ++i)
{ {
if (ip->addr[i] != 255) if (ipv4[i] != 255)
{ {
return true; return true;
} }
@ -7694,10 +7668,6 @@ bool NormalizeMacAddress(char *dst, UINT size, char *src)
// Identify whether the IP address is empty // Identify whether the IP address is empty
bool IsZeroIP(IP *ip) bool IsZeroIP(IP *ip)
{
return IsZeroIp(ip);
}
bool IsZeroIp(IP *ip)
{ {
// Validate arguments // Validate arguments
if (ip == NULL) if (ip == NULL)
@ -7705,14 +7675,17 @@ bool IsZeroIp(IP *ip)
return true; return true;
} }
if (IsIP6(ip) == false) if (IsZero(ip->address, sizeof(ip->address)))
{ {
return IsZero(ip->addr, sizeof(ip->addr)); return true;
} }
else
if (IsIP4(ip))
{ {
return IsZero(ip->ipv6_addr, sizeof(ip->ipv6_addr)); return IsZero(IPV4(ip->address), IPV4_SIZE);
} }
return false;
} }
bool IsZeroIP6Addr(IPV6_ADDR *addr) bool IsZeroIP6Addr(IPV6_ADDR *addr)
{ {
@ -8079,10 +8052,7 @@ bool UnixGetDefaultDns(IP *ip)
return true; return true;
} }
ip->addr[0] = 127; GetLocalHostIP4(ip);
ip->addr[1] = 0;
ip->addr[2] = 0;
ip->addr[3] = 1;
b = ReadDump("/etc/resolv.conf"); b = ReadDump("/etc/resolv.conf");
if (b != NULL) if (b != NULL)
@ -10498,10 +10468,7 @@ ROUTE_ENTRY *GetBestRouteEntryFromRouteTableEx(ROUTE_TABLE *table, IP *ip, UINT
ret = ZeroMallocFast(sizeof(ROUTE_ENTRY)); ret = ZeroMallocFast(sizeof(ROUTE_ENTRY));
Copy(&ret->DestIP, ip, sizeof(IP)); Copy(&ret->DestIP, ip, sizeof(IP));
ret->DestMask.addr[0] = 255; SetIP(&ret->DestMask, 255, 255, 255, 255);
ret->DestMask.addr[1] = 255;
ret->DestMask.addr[2] = 255;
ret->DestMask.addr[3] = 255;
Copy(&ret->GatewayIP, &tmp->GatewayIP, sizeof(IP)); Copy(&ret->GatewayIP, &tmp->GatewayIP, sizeof(IP));
ret->InterfaceID = tmp->InterfaceID; ret->InterfaceID = tmp->InterfaceID;
ret->LocalRouting = tmp->LocalRouting; ret->LocalRouting = tmp->LocalRouting;
@ -10978,10 +10945,10 @@ UINT SendToEx(SOCK *sock, IP *dest_addr, UINT dest_port, void *data, UINT size,
} }
IPToInAddr(&addr.sin_addr, dest_addr); IPToInAddr(&addr.sin_addr, dest_addr);
if ((dest_addr->addr[0] == 255 && dest_addr->addr[1] == 255 && const BYTE *ipv4 = IPV4(dest_addr->address);
dest_addr->addr[2] == 255 && dest_addr->addr[3] == 255) || if ((ipv4[0] == 255 && ipv4[1] == 255 && ipv4[2] == 255 && ipv4[3] == 255) ||
(dest_addr->addr[0] >= 224 && dest_addr->addr[0] <= 239) (ipv4[0] >= 224 && ipv4[0] <= 239) ||
|| broadcast) broadcast)
{ {
if (sock->UdpBroadcast == false) if (sock->UdpBroadcast == false)
{ {
@ -15041,18 +15008,17 @@ SOCK *NewSock()
// Convert the IP to UINT // Convert the IP to UINT
UINT IPToUINT(IP *ip) UINT IPToUINT(IP *ip)
{ {
UCHAR *b;
UINT i, value = 0;
// Validate arguments // Validate arguments
if (ip == NULL) if (ip == NULL)
{ {
return 0; return 0;
} }
b = (UCHAR *)&value; UINT value;
for (i = 0; i < 4; i++)
for (BYTE i = 0; i < IPV4_SIZE; ++i)
{ {
b[i] = ip->addr[i]; ((BYTE *)&value)[i] = IPV4(ip->address)[i];
} }
return value; return value;
@ -15061,8 +15027,6 @@ UINT IPToUINT(IP *ip)
// Convert UINT to IP // Convert UINT to IP
void UINTToIP(IP *ip, UINT value) void UINTToIP(IP *ip, UINT value)
{ {
UCHAR *b;
UINT i;
// Validate arguments // Validate arguments
if (ip == NULL) if (ip == NULL)
{ {
@ -15071,10 +15035,9 @@ void UINTToIP(IP *ip, UINT value)
ZeroIP4(ip); ZeroIP4(ip);
b = (UCHAR *)&value; for (BYTE i = 0; i < IPV4_SIZE; ++i)
for (i = 0; i < 4; i++)
{ {
ip->addr[i] = b[i]; IPV4(ip->address)[i] = ((BYTE *)&value)[i];
} }
} }
@ -15558,11 +15521,12 @@ void SetIP(IP *ip, UCHAR a1, UCHAR a2, UCHAR a3, UCHAR a4)
return; return;
} }
Zero(ip, sizeof(IP)); ZeroIP4(ip);
ip->addr[0] = a1;
ip->addr[1] = a2; ip->address[12] = a1;
ip->addr[2] = a3; ip->address[13] = a2;
ip->addr[3] = a4; ip->address[14] = a3;
ip->address[15] = a4;
} }
UINT SetIP32(UCHAR a1, UCHAR a2, UCHAR a3, UCHAR a4) UINT SetIP32(UCHAR a1, UCHAR a2, UCHAR a3, UCHAR a4)
{ {
@ -15586,7 +15550,7 @@ bool GetIP46Ex(IP *ip4, IP *ip6, char *hostname, UINT timeout, bool *cancel)
} }
ZeroIP4(ip4); ZeroIP4(ip4);
ZeroIP6(ip6); Zero(ip6, sizeof(IP));
ok_a = ok_b = false; ok_a = ok_b = false;
@ -16216,29 +16180,16 @@ void IPToStr(char *str, UINT size, IP *ip)
} }
else else
{ {
IPToStr4(str, size, ip); const BYTE *ipv4 = IPV4(ip->address);
Format(str, size, "%hhu.%hhu.%hhu.%hhu", ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
} }
} }
// Convert the IPv4 to a string
void IPToStr4(char *str, UINT size, IP *ip)
{
// Validate arguments
if (str == NULL || ip == NULL)
{
return;
}
// Conversion
snprintf(str, size != 0 ? size : 64, "%u.%u.%u.%u", ip->addr[0], ip->addr[1], ip->addr[2], ip->addr[3]);
}
// Convert the string to an IP // Convert the string to an IP
bool StrToIP(IP *ip, char *str) bool StrToIP(IP *ip, char *str)
{ {
TOKEN_LIST *token; TOKEN_LIST *token;
char *tmp; char *tmp;
UINT i;
// Validate arguments // Validate arguments
if (ip == NULL || str == NULL) if (ip == NULL || str == NULL)
{ {
@ -16250,7 +16201,7 @@ bool StrToIP(IP *ip, char *str)
return true; return true;
} }
Zero(ip, sizeof(IP)); ZeroIP4(ip);
tmp = CopyStr(str); tmp = CopyStr(str);
Trim(tmp); Trim(tmp);
@ -16262,7 +16213,7 @@ bool StrToIP(IP *ip, char *str)
FreeToken(token); FreeToken(token);
return false; return false;
} }
for (i = 0; i < 4; i++) for (BYTE i = 0; i < IPV4_SIZE; ++i)
{ {
char *s = token->Token[i]; char *s = token->Token[i];
if (s[0] < '0' || s[0] > '9' || if (s[0] < '0' || s[0] > '9' ||
@ -16272,10 +16223,10 @@ bool StrToIP(IP *ip, char *str)
return false; return false;
} }
} }
Zero(ip, sizeof(IP));
for (i = 0; i < 4; i++) for (BYTE i = 0; i < IPV4_SIZE; ++i)
{ {
ip->addr[i] = (UCHAR)ToInt(token->Token[i]); IPV4(ip->address)[i] = (BYTE)ToInt(token->Token[i]);
} }
FreeToken(token); FreeToken(token);
@ -16315,26 +16266,23 @@ void IPToInAddr(struct in_addr *addr, IP *ip)
{ {
UINT i; UINT i;
// Validate arguments // Validate arguments
if (addr == NULL || ip == NULL) if (addr == NULL || IsIP4(ip) == false)
{ {
return; return;
} }
Zero(addr, sizeof(struct in_addr)); Zero(addr, sizeof(struct in_addr));
if (IsIP6(ip) == false) const BYTE *ipv4 = IPV4(ip->address);
for (i = 0; i < IPV4_SIZE; ++i)
{ {
for (i = 0; i < 4; i++) ((BYTE *)addr)[i] = ipv4[i];
{
((UCHAR *)addr)[i] = ip->addr[i];
}
} }
} }
// Convert the IP to the in6_addr // Convert the IP to the in6_addr
void IPToInAddr6(struct in6_addr *addr, IP *ip) void IPToInAddr6(struct in6_addr *addr, IP *ip)
{ {
UINT i;
// Validate arguments // Validate arguments
if (addr == NULL || ip == NULL) if (addr == NULL || ip == NULL)
{ {
@ -16343,19 +16291,33 @@ void IPToInAddr6(struct in6_addr *addr, IP *ip)
Zero(addr, sizeof(struct in6_addr)); Zero(addr, sizeof(struct in6_addr));
if (IsIP6(ip)) for (BYTE i = 0; i < sizeof(ip->address); ++i)
{ {
for (i = 0; i < 16; i++) ((BYTE *)addr)[i] = ip->address[i];
{
((UCHAR *)addr)[i] = ip->ipv6_addr[i];
}
} }
} }
// Convert the in_addr to the IP // Convert the in_addr to the IP
void InAddrToIP(IP *ip, struct in_addr *addr) void InAddrToIP(IP *ip, struct in_addr *addr)
{ {
UINT i; if (ip == NULL || addr == NULL)
{
return;
}
ZeroIP4(ip);
BYTE *ipv4 = IPV4(ip->address);
for (BYTE i = 0; i < IPV4_SIZE; ++i)
{
ipv4[i] = ((UCHAR *)addr)[i];
}
}
// Convert the in6_addr to the IP
void InAddrToIP6(IP *ip, struct in6_addr *addr)
{
// Validate arguments // Validate arguments
if (ip == NULL || addr == NULL) if (ip == NULL || addr == NULL)
{ {
@ -16364,26 +16326,9 @@ void InAddrToIP(IP *ip, struct in_addr *addr)
Zero(ip, sizeof(IP)); Zero(ip, sizeof(IP));
for (i = 0; i < 4; i++) for (BYTE i = 0; i < sizeof(ip->address); ++i)
{ {
ip->addr[i] = ((UCHAR *)addr)[i]; ip->address[i] = ((UCHAR *)addr)[i];
}
}
// Convert the in6_addr to the IP
void InAddrToIP6(IP *ip, struct in6_addr *addr)
{
UINT i;
// Validate arguments
if (ip == NULL || addr == NULL)
{
return;
}
ZeroIP6(ip);
for (i = 0; i < 16; i++)
{
ip->ipv6_addr[i] = ((UCHAR *)addr)[i];
} }
} }
@ -16871,37 +16816,44 @@ bool IsIPMyHost(IP *ip)
bool IsIPPrivate(IP *ip) bool IsIPPrivate(IP *ip)
{ {
// Validate arguments // Validate arguments
if (ip == NULL) if (IsIP4(ip) == false)
{ {
return false; return false;
} }
if (ip->addr[0] == 10) const BYTE *ipv4 = IPV4(ip->address);
// RFC 1918 defines 10.0.0.0/8
if (ipv4[0] == 10)
{ {
return true; return true;
} }
if (ip->addr[0] == 172) // RFC 1918 defines 172.16.0.0/12
if (ipv4[0] == 172)
{ {
if (ip->addr[1] >= 16 && ip->addr[1] <= 31) if (ipv4[1] >= 16 && ipv4[1] <= 31)
{ {
return true; return true;
} }
} }
if (ip->addr[0] == 192 && ip->addr[1] == 168) // RFC 1918 defines 192.168.0.0/16
if (ipv4[0] == 192 && ipv4[1] == 168)
{ {
return true; return true;
} }
if (ip->addr[0] == 169 && ip->addr[1] == 254) // RFC 3927 defines 169.254.0.0/16
if (ipv4[0] == 169 && ipv4[1] == 254)
{ {
return true; return true;
} }
if (ip->addr[0] == 100) // RFC 6598 defines 100.64.0.0/10
if (ipv4[0] == 100)
{ {
if (ip->addr[1] >= 64 && ip->addr[1] <= 127) if (ipv4[1] >= 64 && ipv4[1] <= 127)
{ {
return true; return true;
} }
@ -16909,12 +16861,7 @@ bool IsIPPrivate(IP *ip)
if (g_private_ip_list != NULL) if (g_private_ip_list != NULL)
{ {
if (IsIP4(ip)) return IsOnPrivateIPFile(IPToUINT(ip));
{
UINT ip4 = IPToUINT(ip);
return IsOnPrivateIPFile(ip4);
}
} }
return false; return false;
@ -17036,7 +16983,7 @@ bool IsIPAddressInSameLocalNetwork(IP *a)
if (IsIP4(p)) if (IsIP4(p))
{ {
if (IsZeroIp(p) == false && p->addr[0] != 127) if (IsZeroIp(p) == false && IsLocalHostIP4(a) == false)
{ {
if (IsInSameNetwork4Standard(p, a)) if (IsInSameNetwork4Standard(p, a))
{ {
@ -17077,7 +17024,7 @@ void GetCurrentGlobalIPGuess(IP *ip, bool ipv6)
if (IsIP4(p)) if (IsIP4(p))
{ {
if (IsZeroIp(p) == false && IsIPPrivate(p) == false && p->addr[0] != 127) if (IsZeroIp(p) == false && IsIPPrivate(p) == false && IsLocalHostIP4(p) == false)
{ {
Copy(ip, p, sizeof(IP)); Copy(ip, p, sizeof(IP));
} }
@ -17092,7 +17039,7 @@ void GetCurrentGlobalIPGuess(IP *ip, bool ipv6)
if (IsIP4(p)) if (IsIP4(p))
{ {
if (IsZeroIp(p) == false && IsIPPrivate(p) && p->addr[0] != 127) if (IsZeroIp(p) == false && IsIPPrivate(p) && IsLocalHostIP4(p) == false)
{ {
Copy(ip, p, sizeof(IP)); Copy(ip, p, sizeof(IP));
} }
@ -18396,7 +18343,7 @@ LIST *GetHostIPAddressListInternal()
GetLocalHostIP6(&local6); GetLocalHostIP6(&local6);
ZeroIP4(&any4); ZeroIP4(&any4);
ZeroIP6(&any6); Zero(&any6, sizeof(any6));
Zero(hostname, sizeof(hostname)); Zero(hostname, sizeof(hostname));

View File

@ -85,16 +85,18 @@ struct DYN_VALUE
// IP address // IP address
struct IP struct IP
{ {
UCHAR addr[4]; // IPv4 address, (meaning that 192.0.2.254 = IPv6) BYTE address[16]; // IP address (RFC 3493 format used for IPv4)
UCHAR ipv6_addr[16]; // IPv6 address
UINT ipv6_scope_id; // IPv6 scope ID UINT ipv6_scope_id; // IPv6 scope ID
}; };
// Size when comparing the IP structures only in the address part // Pointer to the beginning of the IPv4 address
#define SIZE_OF_IP_FOR_ADDR (sizeof(UCHAR) * 20) #define IPV4(address) (&address[12])
#define IPV4_SIZE (4)
// Compare the IP address part #define CmpIpAddr(ip1, ip2) (Cmp((ip1)->address, (ip2)->address, sizeof((ip1)->address)))
#define CmpIpAddr(ip1, ip2) (Cmp((ip1), (ip2), SIZE_OF_IP_FOR_ADDR))
#define IsIP6(ip) (IsIP4(ip) == false)
#define IsZeroIp(ip) (IsZeroIP(ip))
// IPv6 address (different format) // IPv6 address (different format)
struct IPV6_ADDR struct IPV6_ADDR
@ -1084,7 +1086,6 @@ bool StrToIP(IP *ip, char *str);
UINT StrToIP32(char *str); UINT StrToIP32(char *str);
UINT UniStrToIP32(wchar_t *str); UINT UniStrToIP32(wchar_t *str);
void IPToStr(char *str, UINT size, IP *ip); void IPToStr(char *str, UINT size, IP *ip);
void IPToStr4(char *str, UINT size, IP *ip);
void IPToStr32(char *str, UINT size, UINT ip); void IPToStr32(char *str, UINT size, UINT ip);
void IPToStr4or6(char *str, UINT size, UINT ip_4_uint, UCHAR *ip_6_bytes); void IPToStr4or6(char *str, UINT size, UINT ip_4_uint, UCHAR *ip_6_bytes);
void IPToUniStr(wchar_t *str, UINT size, IP *ip); void IPToUniStr(wchar_t *str, UINT size, IP *ip);
@ -1223,7 +1224,6 @@ bool IsNetworkAddress4(IP *ip, IP *mask);
bool IsNetworkAddress32(UINT ip, UINT mask); bool IsNetworkAddress32(UINT ip, UINT mask);
bool IsHostIPAddress4(IP *ip); bool IsHostIPAddress4(IP *ip);
bool IsHostIPAddress32(UINT ip); bool IsHostIPAddress32(UINT ip);
bool IsZeroIp(IP *ip);
bool IsZeroIP(IP *ip); bool IsZeroIP(IP *ip);
bool IsZeroIP6Addr(IPV6_ADDR *addr); bool IsZeroIP6Addr(IPV6_ADDR *addr);
UINT IntToSubnetMask32(UINT i); UINT IntToSubnetMask32(UINT i);
@ -1270,7 +1270,6 @@ SOCKET_TIMEOUT_PARAM *NewSocketTimeout(SOCK *sock);
void FreeSocketTimeout(SOCKET_TIMEOUT_PARAM *ttp); void FreeSocketTimeout(SOCKET_TIMEOUT_PARAM *ttp);
void CopyIP(IP *dst, IP *src); void CopyIP(IP *dst, IP *src);
bool IsIP6(IP *ip);
bool IsIP4(IP *ip); bool IsIP4(IP *ip);
void IPv6AddrToIP(IP *ip, IPV6_ADDR *addr); void IPv6AddrToIP(IP *ip, IPV6_ADDR *addr);
bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip); bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip);
@ -1280,7 +1279,6 @@ void GetLocalHostIP4(IP *ip);
bool IsLocalHostIP6(IP *ip); bool IsLocalHostIP6(IP *ip);
bool IsLocalHostIP4(IP *ip); bool IsLocalHostIP4(IP *ip);
bool IsLocalHostIP(IP *ip); bool IsLocalHostIP(IP *ip);
void ZeroIP6(IP *ip);
void ZeroIP4(IP *ip); void ZeroIP4(IP *ip);
bool CheckIPItemStr6(char *str); bool CheckIPItemStr6(char *str);
void IPItemStrToChars6(UCHAR *chars, char *str); void IPItemStrToChars6(UCHAR *chars, char *str);

View File

@ -1183,7 +1183,6 @@ void PackAddIpEx(PACK *p, char *name, IP *ip, UINT index, UINT total)
void PackAddIpEx2(PACK *p, char *name, IP *ip, UINT index, UINT total, bool is_single) void PackAddIpEx2(PACK *p, char *name, IP *ip, UINT index, UINT total, bool is_single)
{ {
UINT i; UINT i;
bool b = false;
char tmp[MAX_PATH]; char tmp[MAX_PATH];
ELEMENT *e; ELEMENT *e;
// Validate arguments // Validate arguments
@ -1196,44 +1195,20 @@ void PackAddIpEx2(PACK *p, char *name, IP *ip, UINT index, UINT total, bool is_s
is_single = false; is_single = false;
} }
b = IsIP6(ip);
Format(tmp, sizeof(tmp), "%s@ipv6_bool", name); Format(tmp, sizeof(tmp), "%s@ipv6_bool", name);
e = PackAddBoolEx(p, tmp, b, index, total); e = PackAddBoolEx(p, tmp, IsIP6(ip), index, total);
if (e != NULL && is_single) e->JsonHint_IsArray = false; if (e != NULL && is_single) e->JsonHint_IsArray = false;
if (e != NULL) e->JsonHint_IsIP = true; if (e != NULL) e->JsonHint_IsIP = true;
Format(tmp, sizeof(tmp), "%s@ipv6_array", name); Format(tmp, sizeof(tmp), "%s@ipv6_array", name);
if (b) e = PackAddDataEx(p, tmp, ip->address, sizeof(ip->address), index, total);
{
e = PackAddDataEx(p, tmp, ip->ipv6_addr, sizeof(ip->ipv6_addr), index, total);
if (e != NULL && is_single) e->JsonHint_IsArray = false; if (e != NULL && is_single) e->JsonHint_IsArray = false;
if (e != NULL) e->JsonHint_IsIP = true; if (e != NULL) e->JsonHint_IsIP = true;
}
else
{
UCHAR dummy[16];
Zero(dummy, sizeof(dummy));
e = PackAddDataEx(p, tmp, dummy, sizeof(dummy), index, total);
if (e != NULL && is_single) e->JsonHint_IsArray = false;
if (e != NULL) e->JsonHint_IsIP = true;
}
Format(tmp, sizeof(tmp), "%s@ipv6_scope_id", name); Format(tmp, sizeof(tmp), "%s@ipv6_scope_id", name);
if (b)
{
e = PackAddIntEx(p, tmp, ip->ipv6_scope_id, index, total); e = PackAddIntEx(p, tmp, ip->ipv6_scope_id, index, total);
if (e != NULL && is_single) e->JsonHint_IsArray = false; if (e != NULL && is_single) e->JsonHint_IsArray = false;
if (e != NULL) e->JsonHint_IsIP = true; if (e != NULL) e->JsonHint_IsIP = true;
}
else
{
e = PackAddIntEx(p, tmp, 0, index, total);
if (e != NULL && is_single) e->JsonHint_IsArray = false;
if (e != NULL) e->JsonHint_IsIP = true;
}
i = IPToUINT(ip); i = IPToUINT(ip);

View File

@ -384,7 +384,7 @@ UINT ProxySocks5Connect(PROXY_PARAM_OUT *out, PROXY_PARAM_IN *in, volatile bool
StrToIP(&target_ip, in->TargetHostname); StrToIP(&target_ip, in->TargetHostname);
// If the IP structure doesn't contain an IP address, the string should be an hostname // If the IP structure doesn't contain an IP address, the string should be an hostname
if (IsZeroIp(&target_ip)) if (IsZeroIP(&target_ip))
{ {
UCHAR dest_length = StrLen(in->TargetHostname); UCHAR dest_length = StrLen(in->TargetHostname);
tmp = 3; tmp = 3;
@ -398,13 +398,13 @@ UINT ProxySocks5Connect(PROXY_PARAM_OUT *out, PROXY_PARAM_IN *in, volatile bool
{ {
tmp = 4; tmp = 4;
WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv6) WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv6)
WriteBuf(b, target_ip.ipv6_addr, sizeof(target_ip.ipv6_addr)); // Destination IPv6 address WriteBuf(b, target_ip.address, sizeof(target_ip.address)); // Destination IPv6 address
} }
else else
{ {
tmp = 1; tmp = 1;
WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv4) WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv4)
WriteBuf(b, target_ip.addr, sizeof(target_ip.addr)); // Destination IPv4 address WriteBuf(b, IPV4(target_ip.address), IPV4_SIZE); // Destination IPv4 address
} }
} }
@ -573,7 +573,7 @@ UINT ProxySocks4Connect(PROXY_PARAM_OUT *out, PROXY_PARAM_IN *in, volatile bool
WriteBuf(b, &tmp, sizeof(tmp)); WriteBuf(b, &tmp, sizeof(tmp));
target_port = Endian16(in->TargetPort); target_port = Endian16(in->TargetPort);
WriteBuf(b, &target_port, sizeof(target_port)); WriteBuf(b, &target_port, sizeof(target_port));
WriteBuf(b, target_ip.addr, sizeof(target_ip.addr)); WriteBuf(b, IPV4(target_ip.address), IPV4_SIZE);
WriteBuf(b, in->Username, StrLen(in->Username) + 1); WriteBuf(b, in->Username, StrLen(in->Username) + 1);
ret = SendAll(s, b->Buf, b->Size, false); ret = SendAll(s, b->Buf, b->Size, false);

View File

@ -3861,7 +3861,7 @@ void DhcpParseClasslessRouteData(DHCP_CLASSLESS_ROUTE_TABLE *t, void *data, UINT
UCHAR c; UCHAR c;
UINT subnet_mask_len; UINT subnet_mask_len;
UINT data_len; UINT data_len;
UCHAR tmp[4]; BYTE tmp[IPV4_SIZE];
IP ip; IP ip;
IP mask; IP mask;
IP gateway; IP gateway;
@ -3889,8 +3889,8 @@ void DhcpParseClasslessRouteData(DHCP_CLASSLESS_ROUTE_TABLE *t, void *data, UINT
} }
// IP address body // IP address body
Zero(&ip, sizeof(IP)); ZeroIP4(&ip);
Copy(ip.addr, tmp, data_len); Copy(IPV4(ip.address), tmp, sizeof(tmp));
Zero(&mask, sizeof(mask)); Zero(&mask, sizeof(mask));
IntToSubnetMask4(&mask, subnet_mask_len); IntToSubnetMask4(&mask, subnet_mask_len);