From nobody Sat Nov 2 16:21:40 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 148773909052229.22559395405301; Tue, 21 Feb 2017 20:51:30 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 89D2782270; Tue, 21 Feb 2017 20:51:05 -0800 (PST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 02F1982223 for ; Tue, 21 Feb 2017 20:51:01 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Feb 2017 20:51:01 -0800 Received: from ray-dev.ccr.corp.intel.com ([10.239.9.25]) by orsmga001.jf.intel.com with ESMTP; 21 Feb 2017 20:51:00 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,192,1484035200"; d="scan'208";a="1100867186" From: Ruiyu Ni To: edk2-devel@lists.01.org Date: Wed, 22 Feb 2017 12:50:47 +0800 Message-Id: <20170222045047.558308-12-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.9.0.windows.1 In-Reply-To: <20170222045047.558308-1-ruiyu.ni@intel.com> References: <20170222045047.558308-1-ruiyu.ni@intel.com> Subject: [edk2] [PATCH 11/11] MdeModulePkg/NetLib: Use StrToIpv4/6Address in BaseLib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Siyuan Fu MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Cc: Siyuan Fu --- MdeModulePkg/Include/Library/NetLib.h | 5 +- MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 382 +++----------------------= ---- 2 files changed, 32 insertions(+), 355 deletions(-) diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/L= ibrary/NetLib.h index 09ead09..6773ed5 100644 --- a/MdeModulePkg/Include/Library/NetLib.h +++ b/MdeModulePkg/Include/Library/NetLib.h @@ -2,7 +2,7 @@ This library is only intended to be used by UEFI network stack modules. It provides basic functions for the UEFI network stack. =20 -Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD = License which accompanies this distribution. The full text of the license may be = found at
@@ -1391,7 +1391,6 @@ NetLibAsciiStrToIp6 ( =20 @retval EFI_SUCCESS Converted to an IPv4 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formatted or Ip4Address= is NULL. - @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to la= ck of resources. =20 **/ EFI_STATUS @@ -1410,7 +1409,6 @@ NetLibStrToIp4 ( =20 @retval EFI_SUCCESS Converted to an IPv6 address successfully. @retval EFI_INVALID_PARAMETER The string is malformatted or Ip6Address = is NULL. - @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to a = lack of resources. =20 **/ EFI_STATUS @@ -1431,7 +1429,6 @@ NetLibStrToIp6 ( =20 @retval EFI_SUCCESS Converted to an IPv6 address successfull= y. @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip6Address= is NULL. - @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to a = lack of resources. =20 **/ EFI_STATUS diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Libr= ary/DxeNetLib/DxeNetLib.c index 0a7117c..37b89f5 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -1,7 +1,7 @@ /** @file Network library. =20 -Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.
(C) Copyright 2015 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD = License @@ -2709,63 +2709,15 @@ NetLibAsciiStrToIp4 ( OUT EFI_IPv4_ADDRESS *Ip4Address ) { - UINT8 Index; - CHAR8 *Ip4Str; - CHAR8 *TempStr; - UINTN NodeVal; + RETURN_STATUS Status; + CHAR8 *EndPointer; =20 - if ((String =3D=3D NULL) || (Ip4Address =3D=3D NULL)) { + Status =3D AsciiStrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer !=3D '\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - Ip4Str =3D (CHAR8 *) String; - - for (Index =3D 0; Index < 4; Index++) { - TempStr =3D Ip4Str; - - while ((*Ip4Str !=3D '\0') && (*Ip4Str !=3D '.')) { - if (Index !=3D 3 && !NET_IS_DIGIT (*Ip4Str)) { - return EFI_INVALID_PARAMETER; - } - =20 - // - // Allow the IPv4 with prefix case, e.g. 192.168.10.10/24=20 - // - if (Index =3D=3D 3 && !NET_IS_DIGIT (*Ip4Str) && *Ip4Str !=3D '/') { - return EFI_INVALID_PARAMETER; - } - =20 - Ip4Str++; - } - - // - // The IPv4 address is X.X.X.X - // - if (*Ip4Str =3D=3D '.') { - if (Index =3D=3D 3) { - return EFI_INVALID_PARAMETER; - } - } else { - if (Index !=3D 3) { - return EFI_INVALID_PARAMETER; - } - } - - // - // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at= the - // first character that is not a valid decimal character, '.' or '\0' = here. - // - NodeVal =3D AsciiStrDecimalToUintn (TempStr); - if (NodeVal > 0xFF) { - return EFI_INVALID_PARAMETER; - } - - Ip4Address->Addr[Index] =3D (UINT8) NodeVal; - - Ip4Str++; - } - - return EFI_SUCCESS; } =20 =20 @@ -2787,193 +2739,15 @@ NetLibAsciiStrToIp6 ( OUT EFI_IPv6_ADDRESS *Ip6Address ) { - UINT8 Index; - CHAR8 *Ip6Str; - CHAR8 *TempStr; - CHAR8 *TempStr2; - UINT8 NodeCnt; - UINT8 TailNodeCnt; - UINT8 AllowedCnt; - UINTN NodeVal; - BOOLEAN Short; - BOOLEAN Update; - BOOLEAN LeadZero; - UINT8 LeadZeroCnt; - UINT8 Cnt; - - if ((String =3D=3D NULL) || (Ip6Address =3D=3D NULL)) { - return EFI_INVALID_PARAMETER; - } - - Ip6Str =3D (CHAR8 *) String; - AllowedCnt =3D 6; - LeadZeroCnt =3D 0; - - // - // An IPv6 address leading with : looks strange. - // - if (*Ip6Str =3D=3D ':') { - if (*(Ip6Str + 1) !=3D ':') { - return EFI_INVALID_PARAMETER; - } else { - AllowedCnt =3D 7; - } - } + RETURN_STATUS Status; + CHAR8 *EndPointer; =20 - ZeroMem (Ip6Address, sizeof (EFI_IPv6_ADDRESS)); - - NodeCnt =3D 0; - TailNodeCnt =3D 0; - Short =3D FALSE; - Update =3D FALSE; - LeadZero =3D FALSE; - - for (Index =3D 0; Index < 15; Index =3D (UINT8) (Index + 2)) { - TempStr =3D Ip6Str; - - while ((*Ip6Str !=3D '\0') && (*Ip6Str !=3D ':')) { - if (Index !=3D 14 && !NET_IS_HEX (*Ip6Str)) { - return EFI_INVALID_PARAMETER; - } - =20 - // - // Allow the IPv6 with prefix case, e.g. 2000:aaaa::10/24=20 - // - if (Index =3D=3D 14 && !NET_IS_HEX (*Ip6Str) && *Ip6Str !=3D '/') { - return EFI_INVALID_PARAMETER; - } - =20 - Ip6Str++; - } - - if ((*Ip6Str =3D=3D '\0') && (Index !=3D 14)) { - return EFI_INVALID_PARAMETER; - } - - if (*Ip6Str =3D=3D ':') { - if (*(Ip6Str + 1) =3D=3D ':') { - if ((NodeCnt > 6) ||=20 - ((*(Ip6Str + 2) !=3D '\0') && (AsciiStrHexToUintn (Ip6Str + 2)= =3D=3D 0))) { - // - // ::0 looks strange. report error to user. - // - return EFI_INVALID_PARAMETER; - } - if ((NodeCnt =3D=3D 6) && (*(Ip6Str + 2) !=3D '\0') &&=20 - (AsciiStrHexToUintn (Ip6Str + 2) !=3D 0)) { - return EFI_INVALID_PARAMETER; - } - - // - // Skip the abbreviation part of IPv6 address. - // - TempStr2 =3D Ip6Str + 2; - while ((*TempStr2 !=3D '\0')) { - if (*TempStr2 =3D=3D ':') { - if (*(TempStr2 + 1) =3D=3D ':') { - // - // :: can only appear once in IPv6 address. - // - return EFI_INVALID_PARAMETER; - } - - TailNodeCnt++; - if (TailNodeCnt >=3D (AllowedCnt - NodeCnt)) { - // - // :: indicates one or more groups of 16 bits of zeros. - // - return EFI_INVALID_PARAMETER; - } - } - - TempStr2++; - } - - Short =3D TRUE; - Update =3D TRUE; - - Ip6Str =3D Ip6Str + 2; - } else { - if (*(Ip6Str + 1) =3D=3D '\0') { - return EFI_INVALID_PARAMETER; - } - Ip6Str++; - NodeCnt++; - if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) { - // - // There are more than 8 groups of 16 bits of zeros. - // - return EFI_INVALID_PARAMETER; - } - } - } - - // - // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the= first - // character that is not a valid hexadecimal character, ':' or '\0' he= re. - // - NodeVal =3D AsciiStrHexToUintn (TempStr); - if ((NodeVal > 0xFFFF) || (Index > 14)) { - return EFI_INVALID_PARAMETER; - } - if (NodeVal !=3D 0) { - if ((*TempStr =3D=3D '0') &&=20 - ((*(TempStr + 2) =3D=3D ':') || (*(TempStr + 3) =3D=3D ':') ||=20 - (*(TempStr + 2) =3D=3D '\0') || (*(TempStr + 3) =3D=3D '\0'))) { - return EFI_INVALID_PARAMETER; - } - if ((*TempStr =3D=3D '0') && (*(TempStr + 4) !=3D '\0') &&=20 - (*(TempStr + 4) !=3D ':')) {=20 - return EFI_INVALID_PARAMETER; - } - } else { - if (((*TempStr =3D=3D '0') && (*(TempStr + 1) =3D=3D '0') &&=20 - ((*(TempStr + 2) =3D=3D ':') || (*(TempStr + 2) =3D=3D '\0'))) || - ((*TempStr =3D=3D '0') && (*(TempStr + 1) =3D=3D '0') && (*(Tem= pStr + 2) =3D=3D '0') &&=20 - ((*(TempStr + 3) =3D=3D ':') || (*(TempStr + 3) =3D=3D '\0')))) { - return EFI_INVALID_PARAMETER; - } - } - - Cnt =3D 0; - while ((TempStr[Cnt] !=3D ':') && (TempStr[Cnt] !=3D '\0')) { - Cnt++;=20 - } - if (LeadZeroCnt =3D=3D 0) { - if ((Cnt =3D=3D 4) && (*TempStr =3D=3D '0')) { - LeadZero =3D TRUE; - LeadZeroCnt++; - } - if ((Cnt !=3D 0) && (Cnt < 4)) { - LeadZero =3D FALSE; - LeadZeroCnt++; - } - } else { - if ((Cnt =3D=3D 4) && (*TempStr =3D=3D '0') && !LeadZero) { - return EFI_INVALID_PARAMETER; - } - if ((Cnt !=3D 0) && (Cnt < 4) && LeadZero) { - return EFI_INVALID_PARAMETER; - } - }=20 - - Ip6Address->Addr[Index] =3D (UINT8) (NodeVal >> 8); - Ip6Address->Addr[Index + 1] =3D (UINT8) (NodeVal & 0xFF); - - // - // Skip the groups of zeros by :: - // - if (Short && Update) { - Index =3D (UINT8) (16 - (TailNodeCnt + 2) * 2); - Update =3D FALSE; - } - } - - if ((!Short && Index !=3D 16) || (*Ip6Str !=3D '\0')) { + Status =3D AsciiStrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer !=3D '\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - return EFI_SUCCESS; } =20 =20 @@ -2985,7 +2759,6 @@ NetLibAsciiStrToIp6 ( =20 @retval EFI_SUCCESS Convert to IPv4 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address = is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack= of resource. =20 **/ EFI_STATUS @@ -2995,27 +2768,15 @@ NetLibStrToIp4 ( OUT EFI_IPv4_ADDRESS *Ip4Address ) { - CHAR8 *Ip4Str; - UINTN StringSize; - EFI_STATUS Status; + RETURN_STATUS Status; + CHAR16 *EndPointer; =20 - if ((String =3D=3D NULL) || (Ip4Address =3D=3D NULL)) { + Status =3D StrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer !=3D L'\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - StringSize =3D StrLen (String) + 1; - Ip4Str =3D (CHAR8 *) AllocatePool (StringSize * sizeof (CHAR8)); - if (Ip4Str =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; - } - - UnicodeStrToAsciiStrS (String, Ip4Str, StringSize); - - Status =3D NetLibAsciiStrToIp4 (Ip4Str, Ip4Address); - - FreePool (Ip4Str); - - return Status; } =20 =20 @@ -3028,7 +2789,6 @@ NetLibStrToIp4 ( =20 @retval EFI_SUCCESS Convert to IPv6 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address = is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack= of resource. =20 **/ EFI_STATUS @@ -3038,27 +2798,15 @@ NetLibStrToIp6 ( OUT EFI_IPv6_ADDRESS *Ip6Address ) { - CHAR8 *Ip6Str; - UINTN StringSize; - EFI_STATUS Status; + RETURN_STATUS Status; + CHAR16 *EndPointer; =20 - if ((String =3D=3D NULL) || (Ip6Address =3D=3D NULL)) { + Status =3D StrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer !=3D L'\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - StringSize =3D StrLen (String) + 1; - Ip6Str =3D (CHAR8 *) AllocatePool (StringSize * sizeof (CHAR8)); - if (Ip6Str =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; - } - - UnicodeStrToAsciiStrS (String, Ip6Str, StringSize); - - Status =3D NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); - - FreePool (Ip6Str); - - return Status; } =20 /** @@ -3072,7 +2820,6 @@ NetLibStrToIp6 ( =20 @retval EFI_SUCCESS Convert to IPv6 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address = is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack= of resource. =20 **/ EFI_STATUS @@ -3083,82 +2830,15 @@ NetLibStrToIp6andPrefix ( OUT UINT8 *PrefixLength ) { - CHAR8 *Ip6Str; - UINTN StringSize; - CHAR8 *PrefixStr; - CHAR8 *TempStr; - EFI_STATUS Status; - UINT8 Length; - - if ((String =3D=3D NULL) || (Ip6Address =3D=3D NULL) || (PrefixLength = =3D=3D NULL)) { - return EFI_INVALID_PARAMETER; - } - - StringSize =3D StrLen (String) + 1; - Ip6Str =3D (CHAR8 *) AllocatePool (StringSize * sizeof (CHAR8)); - if (Ip6Str =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; - } - - UnicodeStrToAsciiStrS (String, Ip6Str, StringSize); - - // - // Get the sub string describing prefix length. - // - TempStr =3D Ip6Str; - while (*TempStr !=3D '\0' && (*TempStr !=3D '/')) { - TempStr++; - } + RETURN_STATUS Status; + CHAR16 *EndPointer; =20 - if (*TempStr =3D=3D '/') { - PrefixStr =3D TempStr + 1; + Status =3D StrToIpv6Address (String, &EndPointer, Ip6Address, PrefixLeng= th); + if (RETURN_ERROR (Status) || (*EndPointer !=3D L'\0')) { + return EFI_INVALID_PARAMETER; } else { - PrefixStr =3D NULL; - } - - // - // Get the sub string describing IPv6 address and convert it. - // - *TempStr =3D '\0'; - - Status =3D NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); - if (EFI_ERROR (Status)) { - goto Exit; - } - - // - // If input string doesn't indicate the prefix length, return 0xff. - // - Length =3D 0xFF; - - // - // Convert the string to prefix length - // - if (PrefixStr !=3D NULL) { - - Status =3D EFI_INVALID_PARAMETER; - Length =3D 0; - while (*PrefixStr !=3D '\0') { - if (NET_IS_DIGIT (*PrefixStr)) { - Length =3D (UINT8) (Length * 10 + (*PrefixStr - '0')); - if (Length > IP6_PREFIX_MAX) { - goto Exit; - } - } else { - goto Exit; - } - - PrefixStr++; - } + return EFI_SUCCESS; } - - *PrefixLength =3D Length; - Status =3D EFI_SUCCESS; - -Exit: - - FreePool (Ip6Str); - return Status; } =20 /** --=20 2.9.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel