From 1708998a11c72e4775889979d91780d66912fb14 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Wed, 7 Apr 2021 21:24:55 +0200 Subject: [PATCH] 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 --- src/Cedar/Admin.c | 6 +- src/Cedar/CM.c | 2 +- src/Cedar/Client.c | 6 +- src/Cedar/EM.c | 2 +- src/Cedar/Hub.c | 14 +- src/Cedar/IPC.c | 82 +++++---- src/Cedar/NativeStack.c | 18 +- src/Cedar/Proto.c | 38 +--- src/Cedar/Proto_IKE.c | 24 +-- src/Cedar/Proto_IkePacket.c | 18 +- src/Cedar/Proto_OpenVPN.c | 2 +- src/Cedar/Proto_Win7.c | 4 +- src/Cedar/Protocol.c | 31 ++-- src/Cedar/SM.c | 2 +- src/Cedar/Virtual.c | 15 +- src/Cedar/WinUi.c | 2 +- src/Mayaqua/Network.c | 349 +++++++++++++++--------------------- src/Mayaqua/Network.h | 20 +-- src/Mayaqua/Pack.c | 39 +--- src/Mayaqua/Proxy.c | 18 +- src/Mayaqua/TcpIp.c | 6 +- 21 files changed, 295 insertions(+), 403 deletions(-) diff --git a/src/Cedar/Admin.c b/src/Cedar/Admin.c index 41fac8ef..298a9881 100644 --- a/src/Cedar/Admin.c +++ b/src/Cedar/Admin.c @@ -5431,7 +5431,7 @@ UINT StGetSessionStatus(ADMIN *a, RPC_SESSION_STATUS *t) t->ClientIp = IPToUINT(&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); @@ -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 (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 ReleaseHub(h); @@ -15370,7 +15370,7 @@ UINT AdminAccept(CONNECTION *c, PACK *p) 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 if (StrLen(hubname) != 0) diff --git a/src/Cedar/CM.c b/src/Cedar/CM.c index 69c7c665..3d311cf9 100644 --- a/src/Cedar/CM.c +++ b/src/Cedar/CM.c @@ -10041,7 +10041,7 @@ bool CmIsEnabled(HWND hWnd, UINT id) } case CMD_SHORTCUT: // Create a shortcut - if (cm->Client->Rpc->Sock->RemoteIP.addr[0] != 127) + if (IsLocalHostIP(&cm->Client->Rpc->Sock->RemoteIP) == false) { return false; } diff --git a/src/Cedar/Client.c b/src/Cedar/Client.c index 3d775477..1ce3fe83 100644 --- a/src/Cedar/Client.c +++ b/src/Cedar/Client.c @@ -1535,7 +1535,7 @@ void CnListenerProc(THREAD *thread, void *param) AddRef(s->ref); NoticeThreadInit(thread); - if (s->LocalIP.addr[0] == 127) + if (IsLocalHostIP(&s->LocalIP)) { p = RecvPack(s); @@ -5110,7 +5110,7 @@ void CiRpcAccepted(CLIENT *c, SOCK *s) 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, // 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, // identify whether this connection is from remote - if (s->RemoteIP.addr[0] != 127) + if (IsLocalHostIP(&s->RemoteIP) == false) { retcode = 2; } diff --git a/src/Cedar/EM.c b/src/Cedar/EM.c index 87640238..17c5c1f3 100644 --- a/src/Cedar/EM.c +++ b/src/Cedar/EM.c @@ -1156,7 +1156,7 @@ void EMMain(RPC *r) 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 MsgBox(NULL, MB_ICONINFORMATION, _UU("EM_WPCAP_REMOTE")); diff --git a/src/Cedar/Hub.c b/src/Cedar/Hub.c index 71ed6913..17d0b40e 100644 --- a/src/Cedar/Hub.c +++ b/src/Cedar/Hub.c @@ -6752,11 +6752,13 @@ bool IsHubIpAddress(IP *ip) 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; } @@ -6810,11 +6812,7 @@ void GenHubIpAddress(IP *ip, char *name) Sha0(hash, tmp2, StrLen(tmp2)); - Zero(ip, sizeof(IP)); - ip->addr[0] = 172; - ip->addr[1] = 31; - ip->addr[2] = hash[0] % 254 + 1; - ip->addr[3] = hash[1] % 254 + 1; + SetIP(ip, 172, 31, hash[0] % 254 + 1, hash[0] % 254 + 1); } // Generate a MAC address for the Virtual HUB diff --git a/src/Cedar/IPC.c b/src/Cedar/IPC.c index 54c6661b..162f7f00 100644 --- a/src/Cedar/IPC.c +++ b/src/Cedar/IPC.c @@ -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; } diff --git a/src/Cedar/NativeStack.c b/src/Cedar/NativeStack.c index 627c7207..0a1eabaa 100644 --- a/src/Cedar/NativeStack.c +++ b/src/Cedar/NativeStack.c @@ -683,30 +683,26 @@ UINT GenerateDummyMark(PRAND *p) // Generate a dummy IP void GenerateDummyIp(PRAND *p, IP *ip) { - UINT i; if (p == NULL || ip == NULL) { 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; - while (true) + BYTE v = 0; + while (v == 0 || v > 254) { 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 diff --git a/src/Cedar/Proto.c b/src/Cedar/Proto.c index 96c07533..182cc183 100644 --- a/src/Cedar/Proto.c +++ b/src/Cedar/Proto.c @@ -134,47 +134,21 @@ UINT ProtoSessionHash(void *p) } ip = &session->SrcIp; - if (IsIP6(ip)) + for (BYTE i = 0; i < sizeof(ip->address); ++i) { - UINT i; - for (i = 0; i < sizeof(ip->ipv6_addr); ++i) - { - ret += ip->ipv6_addr[i]; - } - - ret += ip->ipv6_scope_id; - } - else - { - UINT i; - for (i = 0; i < sizeof(ip->addr); ++i) - { - ret += ip->addr[i]; - } + ret += ip->address[i]; } + ret += ip->ipv6_scope_id; ret += session->SrcPort; ip = &session->DstIp; - if (IsIP6(ip)) + for (BYTE i = 0; i < sizeof(ip->address); ++i) { - UINT i; - for (i = 0; i < sizeof(ip->ipv6_addr); ++i) - { - ret += ip->ipv6_addr[i]; - } - - ret += ip->ipv6_scope_id; - } - else - { - UINT i; - for (i = 0; i < sizeof(ip->addr); ++i) - { - ret += ip->addr[i]; - } + ret += ip->address[i]; } + ret += ip->ipv6_scope_id; ret += session->DstPort; return ret; diff --git a/src/Cedar/Proto_IKE.c b/src/Cedar/Proto_IKE.c index 852d37ec..beaf09ab 100644 --- a/src/Cedar/Proto_IKE.c +++ b/src/Cedar/Proto_IKE.c @@ -143,8 +143,8 @@ void IPsecSendPacketByIPsecSa(IKE_SERVER *ike, IPSECSA *sa, UCHAR *data, UINT da h.PayloadLength = Endian16(data_size); h.NextHeader = protocol_id; h.HopLimit = 64; - Copy(h.SrcAddress.Value, c->TunnelModeServerIP.ipv6_addr, 16); - Copy(h.DestAddress.Value, c->TunnelModeClientIP.ipv6_addr, 16); + Copy(h.SrcAddress.Value, c->TunnelModeServerIP.address, sizeof(h.SrcAddress.Value)); + Copy(h.DestAddress.Value, c->TunnelModeClientIP.address, sizeof(h.DestAddress.Value)); 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) { - u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TransportModeServerIP.ipv6_addr, - (IPV6_ADDR *)c->TransportModeClientIP.ipv6_addr, + u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TransportModeServerIP.address, + (IPV6_ADDR *)c->TransportModeClientIP.address, IP_PROTO_UDP, u, udp_size, 0); } else { - u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TunnelModeServerIP.ipv6_addr, - (IPV6_ADDR *)c->TunnelModeClientIP.ipv6_addr, + u->Checksum = CalcChecksumForIPv6((IPV6_ADDR *)c->TunnelModeServerIP.address, + (IPV6_ADDR *)c->TunnelModeClientIP.address, IP_PROTO_UDP, u, udp_size, 0); @@ -2907,12 +2907,12 @@ void ProcIkeAggressiveModePacketRecv(IKE_SERVER *ike, UDPPACKET *p, IKE_PACKET * if (IsIP6(&sa->IkeClient->ServerIP)) { // 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 { // 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 @@ -3411,12 +3411,12 @@ void ProcIkeMainModePacketRecv(IKE_SERVER *ike, UDPPACKET *p, IKE_PACKET *header if (IsIP6(&sa->IkeClient->ServerIP)) { // 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 { // 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 @@ -3687,11 +3687,11 @@ BUF *IkeCalcNatDetectHash(IKE_SERVER *ike, IKE_HASH *hash, UINT64 initiator_cook if (IsIP6(ip)) { - WriteBuf(b, ip->ipv6_addr, sizeof(ip->ipv6_addr)); + WriteBuf(b, ip->address, sizeof(ip->address)); } else { - WriteBuf(b, ip->addr, sizeof(ip->addr)); + WriteBuf(b, IPV4(ip->address), IPV4_SIZE); } us = Endian16((USHORT)port); diff --git a/src/Cedar/Proto_IkePacket.c b/src/Cedar/Proto_IkePacket.c index f7fcd73c..2b160dc7 100644 --- a/src/Cedar/Proto_IkePacket.c +++ b/src/Cedar/Proto_IkePacket.c @@ -382,11 +382,11 @@ BUF *IkeBuildNatOaPayload(IKE_PACKET_NAT_OA_PAYLOAD *t) if (IsIP6(&t->IpAddress)) { - WriteBuf(ret, t->IpAddress.ipv6_addr, 16); + WriteBuf(ret, t->IpAddress.address, sizeof(t->IpAddress.address)); } else { - WriteBuf(ret, t->IpAddress.addr, 4); + WriteBuf(ret, IPV4(t->IpAddress.address), IPV4_SIZE); } return ret; @@ -1233,8 +1233,8 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b) return false; } - Zero(&ip, sizeof(ip)); - Zero(&subnet, sizeof(subnet)); + ZeroIP4(&ip); + ZeroIP4(&subnet); // Convert to string Zero(t->StrData, sizeof(t->StrData)); @@ -1247,9 +1247,9 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b) break; 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); } @@ -1265,12 +1265,12 @@ bool IkeParseIdPayload(IKE_PACKET_ID_PAYLOAD *t, BUF *b) break; case IKE_ID_IPV4_ADDR_SUBNET: - if (t->IdData->Size == 8) + if (t->IdData->Size == IPV4_SIZE * 2) { char ipstr[MAX_SIZE]; char subnetstr[MAX_SIZE]; - Copy(ip.addr, t->IdData->Buf, 4); - Copy(subnet.addr, ((UCHAR *)t->IdData->Buf) + 4, 4); + Copy(IPV4(ip.address), t->IdData->Buf, IPV4_SIZE); + Copy(IPV4(subnet.address), ((BYTE *)t->IdData->Buf) + IPV4_SIZE, IPV4_SIZE); IPToStr(ipstr, sizeof(ipstr), &ip); MaskToStr(subnetstr, sizeof(subnetstr), &subnet); diff --git a/src/Cedar/Proto_OpenVPN.c b/src/Cedar/Proto_OpenVPN.c index 816f390f..a1779710 100644 --- a/src/Cedar/Proto_OpenVPN.c +++ b/src/Cedar/Proto_OpenVPN.c @@ -2916,7 +2916,7 @@ int OvsCompareSessionList(void *p1, void *p2) return 0; } - i = CmpIpAddr(&s1->Protocol, &s2->Protocol); + i = Cmp(&s1->Protocol, &s2->Protocol, sizeof(s1->Protocol)); if (i != 0) { return i; diff --git a/src/Cedar/Proto_Win7.c b/src/Cedar/Proto_Win7.c index ad78a771..98397503 100644 --- a/src/Cedar/Proto_Win7.c +++ b/src/Cedar/Proto_Win7.c @@ -155,12 +155,12 @@ void IPsecWin7UpdateHostIPAddressList(IPSEC_WIN7 *w) if (IsIP4(ip)) { a.IpVersion = 4; - Copy(a.IpAddress.IPv4Address, ip->addr, 4); + Copy(a.IpAddress.IPv4Address, IPV4(ip->address), sizeof(a.IpAddress.IPv4Address)); } else { 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)); diff --git a/src/Cedar/Protocol.c b/src/Cedar/Protocol.c index 416f8142..c3b22f98 100644 --- a/src/Cedar/Protocol.c +++ b/src/Cedar/Protocol.c @@ -2117,7 +2117,7 @@ bool ServerAccept(CONNECTION *c) if (is_empty_password) { 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) { @@ -3834,7 +3834,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c) } 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 info->ClientPort = Endian32(c->FirstSock->LocalPort); @@ -3850,7 +3850,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c) } else { - Copy(info->ServerIpAddress6, ip.ipv6_addr, sizeof(info->ServerIpAddress6)); + Copy(info->ServerIpAddress6, ip.address, sizeof(info->ServerIpAddress6)); } } // Server port number @@ -3868,7 +3868,7 @@ void CreateNodeInfo(NODE_INFO *info, CONNECTION *c) } 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); @@ -5674,25 +5674,18 @@ bool ClientUploadAuth(CONNECTION *c) // UDP acceleration function using flag if (o->NoUdpAcceleration == false && c->Session->UdpAccel != NULL) { - IP my_ip; - - Zero(&my_ip, sizeof(my_ip)); - PackAddBool(p, "use_udp_acceleration", true); PackAddInt(p, "udp_acceleration_version", c->Session->UdpAccel->Version); - Copy(&my_ip, &c->Session->UdpAccel->MyIp, sizeof(IP)); - if (IsLocalHostIP(&my_ip)) + IP my_ip; + if (IsLocalHostIP(&c->Session->UdpAccel->MyIp) == false) { - if (IsIP4(&my_ip)) - { - ZeroIP4(&my_ip); - } - else - { - ZeroIP6(&my_ip); - } + Copy(&my_ip, &c->Session->UdpAccel->MyIp, sizeof(my_ip)); + } + else + { + Zero(&my_ip, sizeof(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) { diff --git a/src/Cedar/SM.c b/src/Cedar/SM.c index ce645826..13f89797 100644 --- a/src/Cedar/SM.c +++ b/src/Cedar/SM.c @@ -8267,7 +8267,7 @@ void SmBridgeDlg(HWND hWnd, SM_SERVER *s) 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 MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_REMOTE")); diff --git a/src/Cedar/Virtual.c b/src/Cedar/Virtual.c index e61f4594..90597602 100644 --- a/src/Cedar/Virtual.c +++ b/src/Cedar/Virtual.c @@ -2255,15 +2255,14 @@ bool NnParseDnsResponsePacket(UCHAR *data, UINT size, IP *ret_ip) if (r != NULL) { - if (tp == 0x0001 && cl == 0x0001 && r->Size == 4) + if (tp == 0x0001 && cl == 0x0001 && r->Size == IPV4_SIZE) { ret = true; if (ret_ip != NULL) { - Zero(ret_ip, sizeof(IP)); - - Copy(ret_ip->addr, r->Buf, 4); + ZeroIP4(ret_ip); + Copy(IPV4(ret_ip->address), r->Buf, IPV4_SIZE); } } @@ -3708,10 +3707,10 @@ bool ArpaToIP(IP *ip, char *str) { // Convert the token [0, 1, 2, 3] to IP UINT i; - Zero(ip, sizeof(IP)); - for (i = 0; i < 4; i++) + ZeroIP4(ip); + 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; } @@ -5536,7 +5535,7 @@ void VirtualTcpReceived(VH *v, UINT src_ip, UINT dest_ip, void *data, UINT size, } UINTToIP(&ip1, src_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 return; diff --git a/src/Cedar/WinUi.c b/src/Cedar/WinUi.c index 5c3802df..b2b633a1 100644 --- a/src/Cedar/WinUi.c +++ b/src/Cedar/WinUi.c @@ -1207,7 +1207,7 @@ void NicInfoRefresh(HWND hWnd, UI_NICINFO *info) { 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; } diff --git a/src/Mayaqua/Network.c b/src/Mayaqua/Network.c index 85ed4747..f233bd7f 100644 --- a/src/Mayaqua/Network.c +++ b/src/Mayaqua/Network.c @@ -4185,7 +4185,7 @@ void RUDPGetRegisterHostNameByIP(char *dst, UINT size, IP *ip) { UCHAR hash[SHA1_SIZE]; - Sha1(hash, ip->addr, 4); + Sha1(hash, IPV4(ip->address), IPV4_SIZE); BinToStr(tmp, sizeof(tmp), hash, 2); } else @@ -6476,20 +6476,21 @@ bool IsSubnetMask6(IP *a) // Generate a local address from the MAC address void GenerateEui64LocalAddress(IP *a, UCHAR *mac) { - UCHAR tmp[8]; // Validate arguments if (a == NULL || mac == NULL) { return; } + Zero(a, sizeof(IP)); + + UCHAR tmp[8]; GenerateEui64Address6(tmp, mac); - ZeroIP6(a); - a->ipv6_addr[0] = 0xfe; - a->ipv6_addr[1] = 0x80; + a->address[0] = 0xfe; + a->address[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 @@ -6620,7 +6621,7 @@ UINT GetIPAddrType6(IP *ip) return 0; } - if (ip->ipv6_addr[0] == 0xff) + if (ip->address[0] == 0xff) { IP all_node, all_router; @@ -6630,20 +6631,20 @@ UINT GetIPAddrType6(IP *ip) 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; } - 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; } else { - if (ip->ipv6_addr[1] == 0x02 && ip->ipv6_addr[2] == 0 && ip->ipv6_addr[3] == 0 && - ip->ipv6_addr[4] == 0 && ip->ipv6_addr[5] == 0 && ip->ipv6_addr[6] == 0 && - ip->ipv6_addr[7] == 0 && ip->ipv6_addr[8] == 0 && ip->ipv6_addr[9] == 0 && - ip->ipv6_addr[10] == 0 && ip->ipv6_addr[11] == 0x01 && ip->ipv6_addr[12] == 0xff) + if (ip->address[1] == 0x02 && ip->address[2] == 0 && ip->address[3] == 0 && + ip->address[4] == 0 && ip->address[5] == 0 && ip->address[6] == 0 && + ip->address[7] == 0 && ip->address[8] == 0 && ip->address[9] == 0 && + ip->address[10] == 0 && ip->address[11] == 0x01 && ip->address[12] == 0xff) { ret |= IPV6_ADDR_SOLICIATION_MULTICAST; } @@ -6653,7 +6654,7 @@ UINT GetIPAddrType6(IP *ip) { 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; } @@ -6661,7 +6662,7 @@ UINT GetIPAddrType6(IP *ip) { ret |= IPV6_ADDR_GLOBAL_UNICAST; - if (IsZero(&ip->ipv6_addr, 16)) + if (IsZero(&ip->address, sizeof(ip->address))) { ret |= IPV6_ADDR_ZERO; } @@ -6671,7 +6672,7 @@ UINT GetIPAddrType6(IP *ip) 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; } @@ -6691,9 +6692,9 @@ void GetLoopbackAddress6(IP *ip) return; } - ZeroIP6(ip); + Zero(ip, sizeof(IP)); - ip->ipv6_addr[15] = 0x01; + ip->address[15] = 0x01; } // All-nodes multicast address @@ -6705,11 +6706,11 @@ void GetAllNodeMulticaseAddress6(IP *ip) return; } - ZeroIP6(ip); + Zero(ip, sizeof(IP)); - ip->ipv6_addr[0] = 0xff; - ip->ipv6_addr[1] = 0x02; - ip->ipv6_addr[15] = 0x01; + ip->address[0] = 0xff; + ip->address[1] = 0x02; + ip->address[15] = 0x01; } // All-routers multicast address @@ -6721,44 +6722,40 @@ void GetAllRouterMulticastAddress6(IP *ip) return; } - ZeroIP6(ip); + Zero(ip, sizeof(IP)); - ip->ipv6_addr[0] = 0xff; - ip->ipv6_addr[1] = 0x02; - ip->ipv6_addr[15] = 0x02; + ip->address[0] = 0xff; + ip->address[1] = 0x02; + ip->address[15] = 0x02; } // Logical operation of the IPv4 address void IPAnd4(IP *dst, IP *a, IP *b) { - UINT i; // Validate arguments if (dst == NULL || a == NULL || b == NULL || IsIP4(a) == false || IsIP4(b) == false) { - Zero(dst, sizeof(IP)); + ZeroIP4(dst); return; } - i = IPToUINT(a) & IPToUINT(b); - - UINTToIP(dst, i); + UINTToIP(dst, IPToUINT(a) & IPToUINT(b)); } // Logical operation of the IPv6 address void IPAnd6(IP *dst, IP *a, IP *b) { - UINT i; + Zero(dst, sizeof(IP)); + // Validate arguments if (dst == NULL || IsIP6(a) == false || IsIP6(b) == false) { - ZeroIP6(dst); return; } - ZeroIP6(dst); - for (i = 0; i < 16; i++) + for (BYTE i = 0; i < sizeof(dst->address); ++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; 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) { - a.ipv6_addr[z] = 0xff; + a.address[z] = 0xff; } 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++) { - Copy(&values[i], &a.ipv6_addr[i * 2], sizeof(USHORT)); + Copy(&values[i], &a.address[i * 2], sizeof(USHORT)); values[i] = Endian16(values[i]); } @@ -6934,7 +6931,7 @@ bool StrToIP6(IP *ip, char *str) return false; } - ZeroIP6(&a); + Zero(&a, sizeof(a)); StrCpy(tmp, sizeof(tmp), str); Trim(tmp); @@ -7006,8 +7003,8 @@ bool StrToIP6(IP *ip, char *str) IPItemStrToChars6(chars, str); - a.ipv6_addr[k++] = chars[0]; - a.ipv6_addr[k++] = chars[1]; + a.address[k++] = chars[0]; + a.address[k++] = chars[1]; } } @@ -7163,18 +7160,9 @@ void ZeroIP4(IP *ip) } Zero(ip, sizeof(IP)); -} -// Create an IPv6 address of all zero -void ZeroIP6(IP *ip) -{ - // Validate arguments - if (ip == NULL) - { - return; - } - - SetIP6(ip, NULL); + ip->address[10] = 0xff; + ip->address[11] = 0xff; } // Get the IP address of the localhost @@ -7185,9 +7173,10 @@ void GetLocalHostIP6(IP *ip) { return; } - ZeroIP6(ip); - ip->ipv6_addr[15] = 1; + Zero(ip, sizeof(IP)); + + ip->address[15] = 1; } void GetLocalHostIP4(IP *ip) { @@ -7235,7 +7224,7 @@ bool IsLocalHostIP4(IP *ip) return false; } - if (ip->addr[0] == 127) + if (IPV4(ip->address)[0] == 127) { return true; } @@ -7289,9 +7278,9 @@ bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip) 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; @@ -7301,45 +7290,20 @@ bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip) void SetIP6(IP *ip, UCHAR *value) { // Validate arguments - if (ip == NULL) + if (ip == NULL || value == NULL) { return; } Zero(ip, sizeof(IP)); - ip->addr[0] = 192; - ip->addr[1] = 0; - ip->addr[2] = 2; - ip->addr[3] = 254; - - if (value != NULL) + for (BYTE i = 0; i < sizeof(ip->address); ++i) { - UINT i; - - for (i = 0; i < 16; i++) - { - ip->ipv6_addr[i] = value[i]; - } + ip->address[i] = value[i]; } } -// Check whether the specified address is a IPv6 address -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; -} +// Check whether the specified address is IPv4 bool IsIP4(IP *ip) { // Validate arguments @@ -7348,7 +7312,17 @@ bool IsIP4(IP *ip) 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 @@ -7361,7 +7335,6 @@ void CopyIP(IP *dst, IP *src) // Identify whether the IP address is a normal unicast address bool IsValidUnicastIPAddress4(IP *ip) { - UINT i; // Validate arguments if (IsIP4(ip) == false) { @@ -7373,17 +7346,18 @@ bool IsValidUnicastIPAddress4(IP *ip) 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 return false; } - /// 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; } @@ -7694,10 +7668,6 @@ bool NormalizeMacAddress(char *dst, UINT size, char *src) // Identify whether the IP address is empty bool IsZeroIP(IP *ip) -{ - return IsZeroIp(ip); -} -bool IsZeroIp(IP *ip) { // Validate arguments if (ip == NULL) @@ -7705,14 +7675,17 @@ bool IsZeroIp(IP *ip) 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) { @@ -8079,10 +8052,7 @@ bool UnixGetDefaultDns(IP *ip) return true; } - ip->addr[0] = 127; - ip->addr[1] = 0; - ip->addr[2] = 0; - ip->addr[3] = 1; + GetLocalHostIP4(ip); b = ReadDump("/etc/resolv.conf"); if (b != NULL) @@ -10498,10 +10468,7 @@ ROUTE_ENTRY *GetBestRouteEntryFromRouteTableEx(ROUTE_TABLE *table, IP *ip, UINT ret = ZeroMallocFast(sizeof(ROUTE_ENTRY)); Copy(&ret->DestIP, ip, sizeof(IP)); - ret->DestMask.addr[0] = 255; - ret->DestMask.addr[1] = 255; - ret->DestMask.addr[2] = 255; - ret->DestMask.addr[3] = 255; + SetIP(&ret->DestMask, 255, 255, 255, 255); Copy(&ret->GatewayIP, &tmp->GatewayIP, sizeof(IP)); ret->InterfaceID = tmp->InterfaceID; 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); - if ((dest_addr->addr[0] == 255 && dest_addr->addr[1] == 255 && - dest_addr->addr[2] == 255 && dest_addr->addr[3] == 255) || - (dest_addr->addr[0] >= 224 && dest_addr->addr[0] <= 239) - || broadcast) + const BYTE *ipv4 = IPV4(dest_addr->address); + if ((ipv4[0] == 255 && ipv4[1] == 255 && ipv4[2] == 255 && ipv4[3] == 255) || + (ipv4[0] >= 224 && ipv4[0] <= 239) || + broadcast) { if (sock->UdpBroadcast == false) { @@ -15041,18 +15008,17 @@ SOCK *NewSock() // Convert the IP to UINT UINT IPToUINT(IP *ip) { - UCHAR *b; - UINT i, value = 0; // Validate arguments if (ip == NULL) { return 0; } - b = (UCHAR *)&value; - for (i = 0; i < 4; i++) + UINT value; + + for (BYTE i = 0; i < IPV4_SIZE; ++i) { - b[i] = ip->addr[i]; + ((BYTE *)&value)[i] = IPV4(ip->address)[i]; } return value; @@ -15061,8 +15027,6 @@ UINT IPToUINT(IP *ip) // Convert UINT to IP void UINTToIP(IP *ip, UINT value) { - UCHAR *b; - UINT i; // Validate arguments if (ip == NULL) { @@ -15071,10 +15035,9 @@ void UINTToIP(IP *ip, UINT value) ZeroIP4(ip); - b = (UCHAR *)&value; - for (i = 0; i < 4; i++) + for (BYTE i = 0; i < IPV4_SIZE; ++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; } - Zero(ip, sizeof(IP)); - ip->addr[0] = a1; - ip->addr[1] = a2; - ip->addr[2] = a3; - ip->addr[3] = a4; + ZeroIP4(ip); + + ip->address[12] = a1; + ip->address[13] = a2; + ip->address[14] = a3; + ip->address[15] = 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); - ZeroIP6(ip6); + Zero(ip6, sizeof(IP)); ok_a = ok_b = false; @@ -16216,29 +16180,16 @@ void IPToStr(char *str, UINT size, IP *ip) } 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 bool StrToIP(IP *ip, char *str) { TOKEN_LIST *token; char *tmp; - UINT i; // Validate arguments if (ip == NULL || str == NULL) { @@ -16250,7 +16201,7 @@ bool StrToIP(IP *ip, char *str) return true; } - Zero(ip, sizeof(IP)); + ZeroIP4(ip); tmp = CopyStr(str); Trim(tmp); @@ -16262,7 +16213,7 @@ bool StrToIP(IP *ip, char *str) FreeToken(token); return false; } - for (i = 0; i < 4; i++) + for (BYTE i = 0; i < IPV4_SIZE; ++i) { char *s = token->Token[i]; if (s[0] < '0' || s[0] > '9' || @@ -16272,10 +16223,10 @@ bool StrToIP(IP *ip, char *str) 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); @@ -16315,26 +16266,23 @@ void IPToInAddr(struct in_addr *addr, IP *ip) { UINT i; // Validate arguments - if (addr == NULL || ip == NULL) + if (addr == NULL || IsIP4(ip) == false) { return; } 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++) - { - ((UCHAR *)addr)[i] = ip->addr[i]; - } + ((BYTE *)addr)[i] = ipv4[i]; } } // Convert the IP to the in6_addr void IPToInAddr6(struct in6_addr *addr, IP *ip) { - UINT i; // Validate arguments if (addr == NULL || ip == NULL) { @@ -16343,19 +16291,33 @@ void IPToInAddr6(struct in6_addr *addr, IP *ip) Zero(addr, sizeof(struct in6_addr)); - if (IsIP6(ip)) + for (BYTE i = 0; i < sizeof(ip->address); ++i) { - for (i = 0; i < 16; i++) - { - ((UCHAR *)addr)[i] = ip->ipv6_addr[i]; - } + ((BYTE *)addr)[i] = ip->address[i]; } } // Convert the in_addr to the IP 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 if (ip == NULL || addr == NULL) { @@ -16364,26 +16326,9 @@ void InAddrToIP(IP *ip, struct in_addr *addr) 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]; - } -} - -// 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]; + ip->address[i] = ((UCHAR *)addr)[i]; } } @@ -16871,37 +16816,44 @@ bool IsIPMyHost(IP *ip) bool IsIPPrivate(IP *ip) { // Validate arguments - if (ip == NULL) + if (IsIP4(ip) == 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; } - 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; } } - 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; } - 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; } - 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; } @@ -16909,12 +16861,7 @@ bool IsIPPrivate(IP *ip) if (g_private_ip_list != NULL) { - if (IsIP4(ip)) - { - UINT ip4 = IPToUINT(ip); - - return IsOnPrivateIPFile(ip4); - } + return IsOnPrivateIPFile(IPToUINT(ip)); } return false; @@ -17036,7 +16983,7 @@ bool IsIPAddressInSameLocalNetwork(IP *a) if (IsIP4(p)) { - if (IsZeroIp(p) == false && p->addr[0] != 127) + if (IsZeroIp(p) == false && IsLocalHostIP4(a) == false) { if (IsInSameNetwork4Standard(p, a)) { @@ -17077,7 +17024,7 @@ void GetCurrentGlobalIPGuess(IP *ip, bool ipv6) 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)); } @@ -17092,7 +17039,7 @@ void GetCurrentGlobalIPGuess(IP *ip, bool ipv6) 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)); } @@ -18396,7 +18343,7 @@ LIST *GetHostIPAddressListInternal() GetLocalHostIP6(&local6); ZeroIP4(&any4); - ZeroIP6(&any6); + Zero(&any6, sizeof(any6)); Zero(hostname, sizeof(hostname)); diff --git a/src/Mayaqua/Network.h b/src/Mayaqua/Network.h index dcdc9211..5fbffd36 100644 --- a/src/Mayaqua/Network.h +++ b/src/Mayaqua/Network.h @@ -85,16 +85,18 @@ struct DYN_VALUE // IP address struct IP { - UCHAR addr[4]; // IPv4 address, (meaning that 192.0.2.254 = IPv6) - UCHAR ipv6_addr[16]; // IPv6 address - UINT ipv6_scope_id; // IPv6 scope ID + BYTE address[16]; // IP address (RFC 3493 format used for IPv4) + UINT ipv6_scope_id; // IPv6 scope ID }; -// Size when comparing the IP structures only in the address part -#define SIZE_OF_IP_FOR_ADDR (sizeof(UCHAR) * 20) +// Pointer to the beginning of the IPv4 address +#define IPV4(address) (&address[12]) +#define IPV4_SIZE (4) -// Compare the IP address part -#define CmpIpAddr(ip1, ip2) (Cmp((ip1), (ip2), SIZE_OF_IP_FOR_ADDR)) +#define CmpIpAddr(ip1, ip2) (Cmp((ip1)->address, (ip2)->address, sizeof((ip1)->address))) + +#define IsIP6(ip) (IsIP4(ip) == false) +#define IsZeroIp(ip) (IsZeroIP(ip)) // IPv6 address (different format) struct IPV6_ADDR @@ -1084,7 +1086,6 @@ bool StrToIP(IP *ip, char *str); UINT StrToIP32(char *str); UINT UniStrToIP32(wchar_t *str); 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 IPToStr4or6(char *str, UINT size, UINT ip_4_uint, UCHAR *ip_6_bytes); 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 IsHostIPAddress4(IP *ip); bool IsHostIPAddress32(UINT ip); -bool IsZeroIp(IP *ip); bool IsZeroIP(IP *ip); bool IsZeroIP6Addr(IPV6_ADDR *addr); UINT IntToSubnetMask32(UINT i); @@ -1270,7 +1270,6 @@ SOCKET_TIMEOUT_PARAM *NewSocketTimeout(SOCK *sock); void FreeSocketTimeout(SOCKET_TIMEOUT_PARAM *ttp); void CopyIP(IP *dst, IP *src); -bool IsIP6(IP *ip); bool IsIP4(IP *ip); void IPv6AddrToIP(IP *ip, IPV6_ADDR *addr); bool IPToIPv6Addr(IPV6_ADDR *addr, IP *ip); @@ -1280,7 +1279,6 @@ void GetLocalHostIP4(IP *ip); bool IsLocalHostIP6(IP *ip); bool IsLocalHostIP4(IP *ip); bool IsLocalHostIP(IP *ip); -void ZeroIP6(IP *ip); void ZeroIP4(IP *ip); bool CheckIPItemStr6(char *str); void IPItemStrToChars6(UCHAR *chars, char *str); diff --git a/src/Mayaqua/Pack.c b/src/Mayaqua/Pack.c index f1ebbd9b..2e871fe2 100644 --- a/src/Mayaqua/Pack.c +++ b/src/Mayaqua/Pack.c @@ -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) { UINT i; - bool b = false; char tmp[MAX_PATH]; ELEMENT *e; // Validate arguments @@ -1196,44 +1195,20 @@ void PackAddIpEx2(PACK *p, char *name, IP *ip, UINT index, UINT total, bool is_s is_single = false; } - b = IsIP6(ip); - 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) e->JsonHint_IsIP = true; Format(tmp, sizeof(tmp), "%s@ipv6_array", name); - if (b) - { - 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) 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; - } + e = PackAddDataEx(p, tmp, ip->address, sizeof(ip->address), 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); - if (b) - { - e = PackAddIntEx(p, tmp, ip->ipv6_scope_id, index, total); - if (e != NULL && is_single) e->JsonHint_IsArray = false; - 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; - } + e = PackAddIntEx(p, tmp, ip->ipv6_scope_id, index, total); + if (e != NULL && is_single) e->JsonHint_IsArray = false; + if (e != NULL) e->JsonHint_IsIP = true; i = IPToUINT(ip); diff --git a/src/Mayaqua/Proxy.c b/src/Mayaqua/Proxy.c index 84ffd89d..ad599f77 100644 --- a/src/Mayaqua/Proxy.c +++ b/src/Mayaqua/Proxy.c @@ -384,27 +384,27 @@ UINT ProxySocks5Connect(PROXY_PARAM_OUT *out, PROXY_PARAM_IN *in, volatile bool StrToIP(&target_ip, in->TargetHostname); // 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); tmp = 3; - WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (hostname) - WriteBuf(b, &dest_length, sizeof(dest_length)); // Destination hostname length - WriteBuf(b, in->TargetHostname, dest_length); // Destination hostname + WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (hostname) + WriteBuf(b, &dest_length, sizeof(dest_length)); // Destination hostname length + WriteBuf(b, in->TargetHostname, dest_length); // Destination hostname } else { if (IsIP6(&target_ip)) { tmp = 4; - WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv6) - WriteBuf(b, target_ip.ipv6_addr, sizeof(target_ip.ipv6_addr)); // Destination IPv6 address + WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv6) + WriteBuf(b, target_ip.address, sizeof(target_ip.address)); // Destination IPv6 address } else { tmp = 1; - WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv4) - WriteBuf(b, target_ip.addr, sizeof(target_ip.addr)); // Destination IPv4 address + WriteBuf(b, &tmp, sizeof(tmp)); // Destination type (IPv4) + 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)); target_port = Endian16(in->TargetPort); 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); ret = SendAll(s, b->Buf, b->Size, false); diff --git a/src/Mayaqua/TcpIp.c b/src/Mayaqua/TcpIp.c index 175f933e..b550dc90 100644 --- a/src/Mayaqua/TcpIp.c +++ b/src/Mayaqua/TcpIp.c @@ -3861,7 +3861,7 @@ void DhcpParseClasslessRouteData(DHCP_CLASSLESS_ROUTE_TABLE *t, void *data, UINT UCHAR c; UINT subnet_mask_len; UINT data_len; - UCHAR tmp[4]; + BYTE tmp[IPV4_SIZE]; IP ip; IP mask; IP gateway; @@ -3889,8 +3889,8 @@ void DhcpParseClasslessRouteData(DHCP_CLASSLESS_ROUTE_TABLE *t, void *data, UINT } // IP address body - Zero(&ip, sizeof(IP)); - Copy(ip.addr, tmp, data_len); + ZeroIP4(&ip); + Copy(IPV4(ip.address), tmp, sizeof(tmp)); Zero(&mask, sizeof(mask)); IntToSubnetMask4(&mask, subnet_mask_len);