Skip to content

Commit f6b4c34

Browse files
[libc] Add functions to send/recv messages (#106467)
This patch adds the necessary functions to send and receive messages over a socket. Those functions are: recv, recvfrom, recvmsg, send, sendto, sendmsg, and socketpair for testing.
1 parent c3fe727 commit f6b4c34

37 files changed

+1422
-84
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,14 @@ 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.socketpair
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
10461052
)
10471053
endif()
10481054

libc/hdr/types/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,30 @@ add_proxy_header_library(
199199
libc.include.setjmp
200200
)
201201

202+
203+
add_proxy_header_library(
204+
struct_msghdr
205+
HDRS
206+
struct_msghdr.h
207+
FULL_BUILD_DEPENDS
208+
libc.include.llvm-libc-types.struct_msghdr
209+
libc.include.sys_socket
210+
)
211+
212+
add_proxy_header_library(
213+
struct_sockaddr
214+
HDRS
215+
struct_sockaddr.h
216+
FULL_BUILD_DEPENDS
217+
libc.include.llvm-libc-types.struct_sockaddr
218+
libc.include.sys_socket
219+
)
220+
221+
add_proxy_header_library(
222+
socklen_t
223+
HDRS
224+
socklen_t.h
225+
FULL_BUILD_DEPENDS
226+
libc.include.llvm-libc-types.socklen_t
227+
libc.include.sys_socket
228+
)

libc/hdr/types/socklen_t.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for socklen_t -----------------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_SOCKLEN_T_H
9+
#define LLVM_LIBC_HDR_TYPES_SOCKLEN_T_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/socklen_t.h"
14+
15+
#else
16+
17+
#include <signal.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_SOCKLEN_T_H

libc/hdr/types/ssize_t.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Proxy for ssize_t -------------------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_SSIZE_T_H
9+
#define LLVM_LIBC_HDR_TYPES_SSIZE_T_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/ssize_t.h"
14+
15+
#else
16+
17+
#define __need_ssize_t
18+
#include <stddef.h>
19+
#undef __need_ssize_t
20+
21+
#endif // LIBC_FULL_BUILD
22+
23+
#endif // LLVM_LIBC_HDR_TYPES_SSIZE_T_H

libc/hdr/types/struct_msghdr.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for 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+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_MSGHDR_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_MSGHDR_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_msghdr.h"
14+
15+
#else
16+
17+
#include <sys/socket.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_MSGHDR_H

libc/hdr/types/struct_sockaddr.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct sockaddr ----------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_SOCKADDR_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_SOCKADDR_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_sockaddr.h"
14+
15+
#else
16+
17+
#include <sys/socket.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SOCKADDR_H

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 "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 "size_t.h"
13+
#include "socklen_t.h"
14+
#include "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: 92 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
@@ -24,3 +107,12 @@ functions:
24107
- type: int
25108
- type: int
26109
- type: int
110+
- name: socketpair
111+
standards:
112+
- posix
113+
return_type: int
114+
arguments:
115+
- type: int
116+
- type: int
117+
- type: int
118+
- type: int*

0 commit comments

Comments
 (0)