1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10 /**************************************************************************/
11 /**************************************************************************/
12 /** */
13 /** NetX Component */
14 /** */
15 /** Internet Protocol version 6 Default Router Table (IPv6 router) */
16 /** */
17 /**************************************************************************/
18 /**************************************************************************/
19 #define NX_SOURCE_CODE
20
21
22 #include "nx_api.h"
23 #include "nx_ipv6.h"
24
25 #ifdef FEATURE_NX_IPV6
26
27 /**************************************************************************/
28 /* */
29 /* FUNCTION RELEASE */
30 /* */
31 /* _nxd_ipv6_find_max_prefix_length PORTABLE C */
32 /* 6.1 */
33 /* AUTHOR */
34 /* */
35 /* Yuxin Zhou, Microsoft Corporation */
36 /* */
37 /* DESCRIPTION */
38 /* */
39 /* This function finds the longest matching prefix between two IPv6 */
40 /* addresses. */
41 /* */
42 /* INPUT */
43 /* */
44 /* addr1 IPv6 address 1 */
45 /* addr2 IPv6 address 2 */
46 /* max_length Maximum length to match */
47 /* */
48 /* OUTPUT */
49 /* */
50 /* Number of matching bits */
51 /* */
52 /* CALLS */
53 /* */
54 /* None */
55 /* */
56 /* CALLED BY */
57 /* */
58 /* _nx_ipv6_source_selection */
59 /* */
60 /* NOTE */
61 /* */
62 /* */
63 /* RELEASE HISTORY */
64 /* */
65 /* DATE NAME DESCRIPTION */
66 /* */
67 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
68 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
69 /* resulting in version 6.1 */
70 /* */
71 /**************************************************************************/
_nxd_ipv6_find_max_prefix_length(ULONG * addr1,ULONG * addr2,UINT max_length)72 UINT _nxd_ipv6_find_max_prefix_length(ULONG *addr1, ULONG *addr2, UINT max_length)
73 {
74 UINT length = 0;
75 UINT i, j, bit, time;
76
77 for (i = 0; i < 4; i++)
78 {
79 if (addr1[i] == addr2[i])
80 {
81 length += 32;
82 }
83 /* Length shall not exceed max_length. Stop compare. */
84 else if (length + 31 < max_length)
85 {
86 break;
87 }
88 else
89 {
90 bit = 16;
91 time = 16;
92 for (j = 0; j < 5; j++)
93 {
94 time = time / 2;
95 if (addr1[i] >> bit == addr2[i] >> bit)
96 {
97 bit -= time;
98 if (time == 0)
99 {
100 length += (32 - bit);
101 }
102 }
103 else if (j == 4)
104 {
105 length += (31 - bit);
106 break;
107 }
108 else
109 {
110 bit += time;
111 }
112 }
113 break;
114 }
115 }
116
117
118 return(length);
119 }
120
121 #endif /* FEATURE_NX_IPV6 */
122
123