1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef FILLP_UTILS_H
17 #define FILLP_UTILS_H
18 #include <time.h>
19 #include "fillp_os.h"
20 #include "opt.h"
21 #include "fillp_function.h"
22 #include "log.h"
23 #include "securec.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #define UTILS_FLAGS_SET(_flag, _f) ((_flag) |= (_f))
30 #define UTILS_FLAGS_GET(_flag, _f) ((_flag) & (_f))
31 #define UTILS_FLAGS_CLEAN(_flag, _f) ((_flag) &= ~(_f))
32 #define UTILS_FLAGS_RESET(_flag) ((_flag) &= 0u)
33 #define UTILS_FLAGS_CHECK(_flag, _f) (((_flag) & (_f)) == (_f))
34
35 #define UTILS_ARRAY_LEN(_a) (sizeof(_a) / sizeof((_a)[0]))
36
37 #define UTILS_MIN(a, b) ((a) < (b) ? (a) : (b))
38 #define UTILS_MAX(a, b) ((a) > (b) ? (a) : (b))
39
40 #define FILLP_UTILS_MS2US(ms) ((ms) * 1000)
41 #define FILLP_UTILS_US2MS(us) ((us) / 1000)
42 #define FILLP_UTILS_US2S(us) ((us) / 1000000)
43 #define FILLP_UTILS_S2US(s) ((s) * 1000000)
44
45 #define FILLP_UTILS_KBPS2BPS(kbps) ((kbps) * 1000)
46 #define FILLP_UTILS_BPS2KBPS(bps) ((bps) / 1000)
47 #define FILLP_UTILS_BIT2BYTE(bit) ((bit) >> 3)
48
49 #define UTILS_GET_ADDRPORT(addr) ((struct sockaddr_in*) (addr))->sin_port
50 #define IPV6_ADDR_LEN 16
51
UtilsIpv4AddrPortKey(FILLP_UINT16 port,struct sockaddr_in * remoteAddr)52 __inline static FILLP_UINT32 UtilsIpv4AddrPortKey(FILLP_UINT16 port, struct sockaddr_in *remoteAddr)
53 {
54 return (port ^ (remoteAddr)->sin_addr.s_addr);
55 }
56
UtilsIpv6AddrPortKey(FILLP_UINT16 port,struct sockaddr_in6 * remoteAddr)57 __inline static FILLP_UINT32 UtilsIpv6AddrPortKey(FILLP_UINT16 port, struct sockaddr_in6 *remoteAddr)
58 {
59 FILLP_INT i;
60 for (i = 0; i < IPV6_ADDR_LEN; i++) {
61 port ^= ((remoteAddr)->sin6_addr.s6_addr[i]);
62 }
63 return (FILLP_UINT32)port;
64 }
65
UtilsAddrHashKey(struct sockaddr_in * addr)66 __inline static FILLP_UINT32 UtilsAddrHashKey(struct sockaddr_in *addr)
67 {
68 FILLP_INT port = UTILS_GET_ADDRPORT(addr);
69 socklen_t addrLen = sizeof(struct sockaddr);
70
71 if (((struct sockaddr_in *)(void *)addr)->sin_family == AF_INET6) {
72 addrLen = sizeof(struct sockaddr_in6);
73 }
74
75 if (addrLen == sizeof(struct sockaddr)) {
76 return UtilsIpv4AddrPortKey((FILLP_UINT16)port, addr);
77 } else {
78 return UtilsIpv6AddrPortKey((FILLP_UINT16)port, (struct sockaddr_in6 *)addr);
79 }
80 }
81
UtilsIpv6AddrMatch(FILLP_CONST struct sockaddr_in6 * a,FILLP_CONST struct sockaddr_in6 * b)82 __inline static FILLP_BOOL UtilsIpv6AddrMatch(FILLP_CONST struct sockaddr_in6 *a, FILLP_CONST struct sockaddr_in6 *b)
83 {
84 FILLP_INT i;
85 for (i = 0; i < IPV6_ADDR_LEN; i++) {
86 if ((a)->sin6_addr.s6_addr[i] != (b)->sin6_addr.s6_addr[i]) {
87 return FILLP_FALSE;
88 }
89 }
90 return FILLP_TRUE;
91 }
92
93 #define UTILS_ADDR_FAMILY_MATCH(a, b) (((struct sockaddr_in *)(void *)(a))->sin_family == \
94 ((struct sockaddr_in *)(void *)(b))->sin_family)
95
96 #define UTILS_ADDR_PORT_MATCH(a, b) (((struct sockaddr_in *)(void *)(a))->sin_port == \
97 ((struct sockaddr_in *)(void *)(b))->sin_port)
98
99 #define UTILS_IPV4_ADDR_MATCH(a, b) ((a)->sin_addr.s_addr == (b)->sin_addr.s_addr && \
100 UTILS_ADDR_PORT_MATCH((a), (b)))
101
102 #define UTILS_IPV6_ADDR_MATCH(a, b) (UTILS_ADDR_PORT_MATCH((a), (b)) && \
103 UtilsIpv6AddrMatch((a), (b)))
104
UtilsAddrMatch(FILLP_CONST struct sockaddr_in * addrA,FILLP_CONST struct sockaddr_in * addrB)105 static __inline FILLP_BOOL UtilsAddrMatch(FILLP_CONST struct sockaddr_in *addrA, FILLP_CONST struct sockaddr_in *addrB)
106 {
107 if (!UTILS_ADDR_FAMILY_MATCH(addrA, addrB)) {
108 return FILLP_FALSE;
109 }
110
111 if (addrA->sin_family == AF_INET) {
112 return (FILLP_BOOL)UTILS_IPV4_ADDR_MATCH(addrA, addrB);
113 } else {
114 return (FILLP_BOOL)UTILS_IPV6_ADDR_MATCH((struct sockaddr_in6 *)addrA, (struct sockaddr_in6 *)addrB);
115 }
116 }
117
UtilsAddrCopy(struct sockaddr * dest,struct sockaddr * src)118 static __inline void UtilsAddrCopy(struct sockaddr *dest, struct sockaddr *src)
119 {
120 FillpErrorType err;
121 if (((struct sockaddr_in *)(void *)src)->sin_family == AF_INET) {
122 err = memcpy_s(dest, sizeof(struct sockaddr_in), src, sizeof(struct sockaddr_in));
123 } else {
124 err = memcpy_s(dest, sizeof(struct sockaddr_in6), src, sizeof(struct sockaddr_in6));
125 }
126 if (err != EOK) {
127 FILLP_LOGERR("utils_addr_copy memcpy_s failed : %d", err);
128 }
129 }
130
UtilsAddrValidLength(struct sockaddr_in * addr)131 static __inline size_t UtilsAddrValidLength(struct sockaddr_in *addr)
132 {
133 if (((struct sockaddr_in *)(void *)addr)->sin_family == AF_INET) {
134 return sizeof(struct sockaddr_in);
135 }
136
137 return sizeof(struct sockaddr_in6);
138 }
139
140 #ifdef __cplusplus
141 }
142 #endif
143
144 #endif /* FILLP_UTILS_H */