Skip to content

Commit d97e1d0

Browse files
[libc] Add functions to send/recv messages
This patch adds the necessary functions to send and receive messages over a socket. Those functions are: accept, bind, connect, listen, recv, recvfrom, recvmsg, send, sendto, and sendmsg.
1 parent 419c534 commit d97e1d0

33 files changed

+1026
-5
lines changed

libc/config/linux/api.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ def SysSocketAPI : PublicAPI<"sys/socket.h"> {
193193
"socklen_t",
194194
"struct sockaddr",
195195
"struct sockaddr_un",
196+
"struct msghdr",
197+
"struct iovec",
198+
"size_t",
199+
"ssize_t",
196200
];
197201
}
198202

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,17 @@ if(LLVM_LIBC_FULL_BUILD)
10411041
libc.src.sys.select.select
10421042

10431043
# sys/socket.h entrypoints
1044-
libc.src.sys.socket.bind
10451044
libc.src.sys.socket.socket
1045+
libc.src.sys.socket.bind
1046+
libc.src.sys.socket.send
1047+
libc.src.sys.socket.sendto
1048+
libc.src.sys.socket.sendmsg
1049+
libc.src.sys.socket.recv
1050+
libc.src.sys.socket.recvfrom
1051+
libc.src.sys.socket.recvmsg
1052+
libc.src.sys.socket.connect
1053+
libc.src.sys.socket.listen
1054+
libc.src.sys.socket.accept
10461055
)
10471056
endif()
10481057

libc/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ add_header_macro(
593593
.llvm-libc-macros.sys_socket_macros
594594
.llvm-libc-types.sa_family_t
595595
.llvm-libc-types.socklen_t
596+
.llvm-libc-types.struct_iovec
597+
.llvm-libc-types.struct_msghdr
596598
.llvm-libc-types.struct_sockaddr
597599
.llvm-libc-types.struct_sockaddr_un
598600
)

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ add_header(
117117
add_header(wint_t HDR wint_t.h)
118118
add_header(sa_family_t HDR sa_family_t.h)
119119
add_header(socklen_t HDR socklen_t.h)
120-
add_header(struct_sockaddr_un HDR struct_sockaddr_un.h)
121-
add_header(struct_sockaddr HDR struct_sockaddr.h)
120+
add_header(struct_sockaddr_un HDR struct_sockaddr_un.h DEPENDS .sa_family_t)
121+
add_header(struct_sockaddr HDR struct_sockaddr.h DEPENDS .sa_family_t)
122+
add_header(struct_iovec HDR struct_iovec.h DEPENDS .size_t)
123+
add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
122124
add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
123125
add_header(ACTION HDR ACTION.h)
124126
add_header(ENTRY HDR ENTRY.h)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Definition of struct iovec ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_STRUCT_IOVEC_H__
10+
#define __LLVM_LIBC_TYPES_STRUCT_IOVEC_H__
11+
12+
#include <llvm-libc-types/size_t.h>
13+
14+
struct iovec {
15+
void *iov_base;
16+
size_t iov_len;
17+
};
18+
19+
#endif // __LLVM_LIBC_TYPES_STRUCT_IOVEC_H__
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Definition of struct msghdr ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_STRUCT_MSGHDR_H__
10+
#define __LLVM_LIBC_TYPES_STRUCT_MSGHDR_H__
11+
12+
#include <llvm-libc-types/size_t.h>
13+
#include <llvm-libc-types/socklen_t.h>
14+
#include <llvm-libc-types/struct_iovec.h>
15+
16+
struct msghdr {
17+
void *msg_name; /* Optional address */
18+
socklen_t msg_namelen; /* Size of address */
19+
struct iovec *msg_iov; /* Scatter/gather array */
20+
size_t msg_iovlen; /* # elements in msg_iov */
21+
void *msg_control; /* Ancillary data, see below */
22+
size_t msg_controllen; /* Ancillary data buffer len */
23+
int msg_flags; /* Flags (unused) */
24+
};
25+
26+
#endif // __LLVM_LIBC_TYPES_STRUCT_MSGHDR_H__

libc/newhdrgen/yaml/sys/socket.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@ types:
55
- type_name: struct_sockaddr
66
- type_name: socklen_t
77
- type_name: sa_family_t
8+
- type_name: struct_msghdr
9+
- type_name: struct_iovec
10+
- type_name: size_t
11+
- type_name: ssize_t
812
enums: []
913
objects: []
1014
functions:
15+
- name: accept
16+
standards:
17+
- POSIX
18+
return_type: int
19+
arguments:
20+
- type: int
21+
- type: sockaddr *__restrict
22+
- type: socklen_t *__restrict
1123
- name: bind
1224
standards:
1325
- POSIX
@@ -16,6 +28,77 @@ functions:
1628
- type: int
1729
- type: const struct sockaddr *
1830
- type: socklen_t
31+
- name: connect
32+
standards:
33+
- POSIX
34+
return_type: int
35+
arguments:
36+
- type: int
37+
- type: const struct sockaddr *
38+
- type: socklen_t
39+
- name: listen
40+
standards:
41+
- POSIX
42+
return_type: int
43+
arguments:
44+
- type: int
45+
- type: int
46+
- name: recv
47+
standards:
48+
- POSIX
49+
return_type: ssize_t
50+
arguments:
51+
- type: int
52+
- type: const void *
53+
- type: size_t
54+
- type: int
55+
- name: recvfrom
56+
standards:
57+
- POSIX
58+
return_type: ssize_t
59+
arguments:
60+
- type: int
61+
- type: const void*
62+
- type: size_t
63+
- type: int
64+
- type: const struct sockaddr *
65+
- type: socklen_t
66+
- name: recvmsg
67+
standards:
68+
- POSIX
69+
return_type: ssize_t
70+
arguments:
71+
- type: int
72+
- type: const struct msghdr *
73+
- type: int
74+
- name: send
75+
standards:
76+
- POSIX
77+
return_type: ssize_t
78+
arguments:
79+
- type: int
80+
- type: const void*
81+
- type: size_t
82+
- type: int
83+
- name: sendmsg
84+
standards:
85+
- POSIX
86+
return_type: ssize_t
87+
arguments:
88+
- type: int
89+
- type: const struct msghdr *
90+
- type: int
91+
- name: sendto
92+
standards:
93+
- POSIX
94+
return_type: ssize_t
95+
arguments:
96+
- type: int
97+
- type: const void *
98+
- type: size_t
99+
- type: int
100+
- type: const struct sockaddr *
101+
- type: socklen_t
19102
- name: socket
20103
standards:
21104
- POSIX

libc/spec/posix.td

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ def StructSockAddr : NamedType<"struct sockaddr">;
8585
def StructSockAddrPtr : PtrType<StructSockAddr>;
8686
def ConstStructSockAddrPtr : ConstType<StructSockAddrPtr>;
8787

88+
def StructMsghdr : NamedType<"struct msghdr">;
89+
def StructMsghdrPtr : PtrType<StructMsghdr>;
90+
def ConstStructMsghdrPtr : ConstType<StructMsghdrPtr>;
91+
92+
def StructIovec : NamedType<"struct iovec">;
93+
def StructIovecPtr : PtrType<StructIovec>;
94+
def ConstStructIovecPtr : ConstType<StructIovecPtr>;
95+
8896
def StructSockAddrUn : NamedType<"struct sockaddr_un">;
8997

9098
def StructStatvfs : NamedType<"struct statvfs">;
@@ -1711,10 +1719,14 @@ def POSIX : StandardSpec<"POSIX"> {
17111719
Macro<"SOCK_PACKET">,
17121720
], // Macros
17131721
[
1722+
SizeTType,
1723+
SSizeTType,
17141724
SAFamilyType,
17151725
StructSockAddr,
17161726
StructSockAddrUn,
17171727
SocklenType,
1728+
StructIovec,
1729+
StructMsghdr,
17181730
], // Types
17191731
[], // Enumerations
17201732
[
@@ -1728,6 +1740,25 @@ def POSIX : StandardSpec<"POSIX"> {
17281740
RetValSpec<IntType>,
17291741
[ArgSpec<IntType>, ArgSpec<ConstStructSockAddrPtr>, ArgSpec<SocklenType>]
17301742
>,
1743+
FunctionSpec<
1744+
"send",
1745+
RetValSpec<SSizeTType>,
1746+
[ArgSpec<IntType>, ArgSpec<ConstVoidPtr>, ArgSpec<SizeTType>, ArgSpec<IntType>]
1747+
>,
1748+
FunctionSpec<
1749+
"sendto",
1750+
RetValSpec<SSizeTType>,
1751+
[
1752+
ArgSpec<IntType>, ArgSpec<ConstVoidPtr>, ArgSpec<SizeTType>,
1753+
ArgSpec<IntType>, ArgSpec<ConstStructSockAddrPtr>,
1754+
ArgSpec<SocklenType>
1755+
]
1756+
>,
1757+
FunctionSpec<
1758+
"sendmsg",
1759+
RetValSpec<SSizeTType>,
1760+
[ArgSpec<IntType>, ArgSpec<ConstStructMsghdrPtr>, ArgSpec<IntType>]
1761+
>,
17311762
] // Functions
17321763
>;
17331764

libc/src/sys/socket/CMakeLists.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,66 @@ add_entrypoint_object(
1515
DEPENDS
1616
.${LIBC_TARGET_OS}.bind
1717
)
18+
19+
add_entrypoint_object(
20+
send
21+
ALIAS
22+
DEPENDS
23+
.${LIBC_TARGET_OS}.send
24+
)
25+
26+
add_entrypoint_object(
27+
sendto
28+
ALIAS
29+
DEPENDS
30+
.${LIBC_TARGET_OS}.sendto
31+
)
32+
33+
add_entrypoint_object(
34+
sendmsg
35+
ALIAS
36+
DEPENDS
37+
.${LIBC_TARGET_OS}.sendmsg
38+
)
39+
40+
add_entrypoint_object(
41+
recv
42+
ALIAS
43+
DEPENDS
44+
.${LIBC_TARGET_OS}.recv
45+
)
46+
47+
add_entrypoint_object(
48+
recvfrom
49+
ALIAS
50+
DEPENDS
51+
.${LIBC_TARGET_OS}.recvfrom
52+
)
53+
54+
add_entrypoint_object(
55+
recvmsg
56+
ALIAS
57+
DEPENDS
58+
.${LIBC_TARGET_OS}.recvmsg
59+
)
60+
61+
add_entrypoint_object(
62+
connect
63+
ALIAS
64+
DEPENDS
65+
.${LIBC_TARGET_OS}.connect
66+
)
67+
68+
add_entrypoint_object(
69+
accept
70+
ALIAS
71+
DEPENDS
72+
.${LIBC_TARGET_OS}.accept
73+
)
74+
75+
add_entrypoint_object(
76+
listen
77+
ALIAS
78+
DEPENDS
79+
.${LIBC_TARGET_OS}.listen
80+
)

libc/src/sys/socket/accept.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for accept ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_SOCKET_ACCEPT_H
10+
#define LLVM_LIBC_SRC_SYS_SOCKET_ACCEPT_H
11+
12+
#include <sys/socket.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int accept(int domain, sockaddr *__restrict address,
17+
socklen_t *__restrict address_len);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_SYS_SOCKET_ACCEPT_H

libc/src/sys/socket/connect.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for connect -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_SOCKET_RECV_H
10+
#define LLVM_LIBC_SRC_SYS_SOCKET_RECV_H
11+
12+
#include <sys/socket.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_SYS_SOCKET_RECV_H

0 commit comments

Comments
 (0)