1 /*
2 * Copyright (C) 2021 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 SYS_UTIL_H
17 #define SYS_UTIL_H
18
19 #include "nstackx_common_header.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 #define PATH_SEPARATOR '/'
26 #define INVALID_TID (pthread_t)(-1)
27
28 typedef uint64_t atomic_t;
29
30 #define NSTACKX_ATOM_FETCH(ptr) (*ptr)
31 #define NSTACKX_ATOM_SET(ptr, i) ((*ptr) = (i))
32 #define NSTACKX_ATOM_FETCH_INC(ptr) __sync_fetch_and_add((ptr), 1)
33 #define NSTACKX_ATOM_FETCH_DEC(ptr) __sync_fetch_and_sub((ptr), 1)
34 #define NSTACKX_ATOM_ADD_RETURN(ptr, i) __sync_add_and_fetch((ptr), i)
35 #define NSTACKX_ATOM_FETCH_ADD(ptr, val) __sync_fetch_and_add((ptr), (val))
36 #define NSTACKX_ATOM_FETCH_SUB(ptr, val) __sync_fetch_and_sub((ptr), (val))
37
GetErrno(void)38 static inline int32_t GetErrno(void)
39 {
40 return errno;
41 }
42
43 #define CloseSocketInner CloseDesc
44 #define gettid() (pid_t)syscall(__NR_gettid)
45 NSTACKX_EXPORT void CloseDesc(int32_t desc);
46 NSTACKX_EXPORT int32_t GetInterfaceList(struct ifconf *ifc, struct ifreq *buf, uint32_t size);
47 NSTACKX_EXPORT int32_t GetInterfaceIP(int32_t fd, struct ifreq *interface);
48 NSTACKX_EXPORT int32_t GetTargetInterface(const struct sockaddr_in *dstAddr, struct ifreq *localDev);
49
50 #ifdef __cplusplus
51 }
52 #endif
53
54 #endif // NSTACKX_UTIL_H
55