1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#ifndef _SYS_SOCKET_H
17570af302Sopenharmony_ci#error "Never include this file directly; instead, include <sys/socket.h>"
18570af302Sopenharmony_ci#endif
19570af302Sopenharmony_ci
20570af302Sopenharmony_ci#include "fortify.h"
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#ifdef __cplusplus
23570af302Sopenharmony_ciextern "C" {
24570af302Sopenharmony_ci#endif
25570af302Sopenharmony_ci
26570af302Sopenharmony_cissize_t __sendto_chk(int, const void*, size_t, size_t, int, const struct sockaddr*,
27570af302Sopenharmony_ci    socklen_t);
28570af302Sopenharmony_cissize_t __recvfrom_chk(int, void*, size_t, size_t, int, struct sockaddr*,
29570af302Sopenharmony_ci    socklen_t*);
30570af302Sopenharmony_cissize_t __send_chk(int, const void*, size_t, size_t, int);
31570af302Sopenharmony_cissize_t __recv_chk(int, void*, size_t, size_t, int);
32570af302Sopenharmony_ci
33570af302Sopenharmony_ci
34570af302Sopenharmony_ci#ifdef __FORTIFY_COMPILATION
35570af302Sopenharmony_ci__DIAGNOSE_FORTIFY_INLINE
36570af302Sopenharmony_cissize_t recvfrom(int fd, void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags,
37570af302Sopenharmony_ci    struct sockaddr* src_addr, socklen_t* addr_len)
38570af302Sopenharmony_ci__DIAGNOSE_OVERLOAD
39570af302Sopenharmony_ci__DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len),
40570af302Sopenharmony_ci    "'recvfrom' " CALLED_WITH_SIZE_BIGGER_BUFFER)
41570af302Sopenharmony_ci{
42570af302Sopenharmony_ci#ifdef __FORTIFY_RUNTIME
43570af302Sopenharmony_ci    size_t bos = __DIAGNOSE_BOS0(buf);
44570af302Sopenharmony_ci
45570af302Sopenharmony_ci    if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) {
46570af302Sopenharmony_ci        return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
47570af302Sopenharmony_ci    }
48570af302Sopenharmony_ci#endif
49570af302Sopenharmony_ci    return __DIAGNOSE_CALL_BYPASSING_FORTIFY(recvfrom)(fd, buf, len, flags, src_addr, addr_len);
50570af302Sopenharmony_ci}
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci__DIAGNOSE_FORTIFY_INLINE
53570af302Sopenharmony_cissize_t sendto(int fd, const void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags,
54570af302Sopenharmony_ci    const struct sockaddr* dest_addr, socklen_t addr_len)
55570af302Sopenharmony_ci__DIAGNOSE_OVERLOAD
56570af302Sopenharmony_ci__DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len),
57570af302Sopenharmony_ci    "'sendto' " CALLED_WITH_SIZE_BIGGER_BUFFER)
58570af302Sopenharmony_ci{
59570af302Sopenharmony_ci#ifdef __FORTIFY_RUNTIME
60570af302Sopenharmony_ci    size_t bos = __DIAGNOSE_BOS0(buf);
61570af302Sopenharmony_ci
62570af302Sopenharmony_ci    if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) {
63570af302Sopenharmony_ci        return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len);
64570af302Sopenharmony_ci    }
65570af302Sopenharmony_ci#endif
66570af302Sopenharmony_ci    return __DIAGNOSE_CALL_BYPASSING_FORTIFY(sendto)(fd, buf, len, flags, dest_addr, addr_len);
67570af302Sopenharmony_ci}
68570af302Sopenharmony_ci
69570af302Sopenharmony_ci__DIAGNOSE_FORTIFY_INLINE
70570af302Sopenharmony_cissize_t recv(int socket, void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags)
71570af302Sopenharmony_ci__DIAGNOSE_OVERLOAD
72570af302Sopenharmony_ci__DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len),
73570af302Sopenharmony_ci    "'recv' " CALLED_WITH_SIZE_BIGGER_BUFFER)
74570af302Sopenharmony_ci{
75570af302Sopenharmony_ci#ifdef __FORTIFY_RUNTIME
76570af302Sopenharmony_ci    size_t bos = __DIAGNOSE_BOS0(buf);
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci    if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) {
79570af302Sopenharmony_ci        return __recv_chk(socket, buf, len, bos, flags);
80570af302Sopenharmony_ci    }
81570af302Sopenharmony_ci#endif
82570af302Sopenharmony_ci    return __DIAGNOSE_CALL_BYPASSING_FORTIFY(recv)(socket, buf, len, flags);
83570af302Sopenharmony_ci}
84570af302Sopenharmony_ci
85570af302Sopenharmony_ci__DIAGNOSE_FORTIFY_INLINE
86570af302Sopenharmony_cissize_t send(int socket, const void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags)
87570af302Sopenharmony_ci__DIAGNOSE_OVERLOAD
88570af302Sopenharmony_ci__DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len),
89570af302Sopenharmony_ci    "'send' " CALLED_WITH_SIZE_BIGGER_BUFFER)
90570af302Sopenharmony_ci{
91570af302Sopenharmony_ci#ifdef __FORTIFY_RUNTIME
92570af302Sopenharmony_ci    size_t bos = __DIAGNOSE_BOS0(buf);
93570af302Sopenharmony_ci
94570af302Sopenharmony_ci    if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) {
95570af302Sopenharmony_ci        return __send_chk(socket, buf, len, bos, flags);
96570af302Sopenharmony_ci    }
97570af302Sopenharmony_ci#endif
98570af302Sopenharmony_ci    return __DIAGNOSE_CALL_BYPASSING_FORTIFY(send)(socket, buf, len, flags);
99570af302Sopenharmony_ci}
100570af302Sopenharmony_ci#endif
101570af302Sopenharmony_ci
102570af302Sopenharmony_ci#ifdef __cplusplus
103570af302Sopenharmony_ci}
104570af302Sopenharmony_ci#endif