2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_vcom_glibc_socket_h
17 #define included_vcom_glibc_socket_h
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/select.h>
22 #include <arpa/inet.h>
25 #include <sys/epoll.h>
30 * Generic glibc fd api
34 * glibc APIs from <unistd.h>
37 /* Close the file descriptor FD.
39 This function is a cancellation point and therefore not marked with
41 extern int close (int __fd);
43 /* Read NBYTES into BUF from FD. Return the
44 number read, -1 for errors or 0 for EOF.
46 This function is a cancellation point and therefore not marked with
48 extern ssize_t __wur read (int __fd, void *__buf, size_t __nbytes);
50 /* Write N bytes of BUF to FD. Return the number written, or -1.
52 This function is a cancellation point and therefore not marked with
54 extern ssize_t __wur write (int __fd, const void *__buf, size_t __n);
58 * glibc APIs from <fcntl.h>
61 /* Do the file control operation described by CMD on FD.
62 The remaining arguments are interpreted depending on CMD.
64 This function is a cancellation point and therefore not marked with
66 extern int fcntl (int __fd, int __cmd, ...);
70 * glibc APIs from <sys/select.h>
73 /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
74 readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
75 (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
76 after waiting the interval specified therein. Returns the number of ready
77 descriptors, or -1 for errors.
80 This function is a cancellation point and therefore not marked with
83 select (int __nfds, fd_set * __restrict __readfds,
84 fd_set * __restrict __writefds,
85 fd_set * __restrict __exceptfds,
86 struct timeval *__restrict __timeout);
89 /* Same as above only that the TIMEOUT value is given with higher
90 resolution and a sigmask which is been set temporarily. This version
93 This function is a cancellation point and therefore not marked with
96 pselect (int __nfds, fd_set * __restrict __readfds,
97 fd_set * __restrict __writefds,
98 fd_set * __restrict __exceptfds,
99 const struct timespec *__restrict __timeout,
100 const __sigset_t * __restrict __sigmask);
106 * Socket specific glibc api
111 * glibc APIs from <sys/socket.h>
114 /* Create a new socket of type TYPE in domain DOMAIN, using
115 protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
116 Returns a file descriptor for the new socket, or -1 for errors. */
117 extern int __THROW socket (int __domain, int __type, int __protocol);
119 /* Create two new sockets, of type TYPE in domain DOMAIN and using
120 protocol PROTOCOL, which are connected to each other, and put file
121 descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
122 one will be chosen automatically. Returns 0 on success, -1 for errors. */
124 socketpair (int __domain, int __type, int __protocol, int __fds[2]);
126 /* Give the socket FD the local address ADDR (which is LEN bytes long). */
128 bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
130 /* Put the local address of FD into *ADDR and its length in *LEN. */
132 getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
134 /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
135 For connectionless socket types, just set the default address to send to
136 and the only address from which to accept transmissions.
137 Return 0 on success, -1 for errors.
139 This function is a cancellation point and therefore not marked with
141 extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
143 /* Put the address of the peer connected to socket FD into *ADDR
144 (which is *LEN bytes long), and its actual length into *LEN. */
146 getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
148 /* Send N bytes of BUF to socket FD. Returns the number sent or -1.
150 This function is a cancellation point and therefore not marked with
152 extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
154 /* Read N bytes into BUF from socket FD.
155 Returns the number read or -1 for errors.
157 This function is a cancellation point and therefore not marked with
159 extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
161 /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
162 ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
164 This function is a cancellation point and therefore not marked with
167 sendto (int __fd, const void *__buf, size_t __n,
168 int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len);
170 /* Read N bytes into BUF through socket FD.
171 If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
172 the sender, and store the actual size of the address in *ADDR_LEN.
173 Returns the number of bytes read or -1 for errors.
175 This function is a cancellation point and therefore not marked with
178 recvfrom (int __fd, void *__restrict __buf,
179 size_t __n, int __flags,
180 __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
182 /* Send a message described MESSAGE on socket FD.
183 Returns the number of bytes sent, or -1 for errors.
185 This function is a cancellation point and therefore not marked with
188 sendmsg (int __fd, const struct msghdr *__message, int __flags);
191 /* Send a VLEN messages as described by VMESSAGES to socket FD.
192 Returns the number of datagrams successfully written or -1 for errors.
194 This function is a cancellation point and therefore not marked with
197 sendmmsg (int __fd, struct mmsghdr *__vmessages,
198 unsigned int __vlen, int __flags);
201 /* Receive a message as described by MESSAGE from socket FD.
202 Returns the number of bytes read or -1 for errors.
204 This function is a cancellation point and therefore not marked with
206 extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
209 /* Receive up to VLEN messages as described by VMESSAGES from socket FD.
210 Returns the number of messages received or -1 for errors.
212 This function is a cancellation point and therefore not marked with
215 recvmmsg (int __fd, struct mmsghdr *__vmessages,
216 unsigned int __vlen, int __flags, struct timespec *__tmo);
220 /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
221 into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
222 actual length. Returns 0 on success, -1 for errors. */
224 getsockopt (int __fd, int __level, int __optname,
225 void *__restrict __optval, socklen_t * __restrict __optlen);
227 /* Set socket FD's option OPTNAME at protocol level LEVEL
228 to *OPTVAL (which is OPTLEN bytes long).
229 Returns 0 on success, -1 for errors. */
231 setsockopt (int __fd, int __level, int __optname,
232 const void *__optval, socklen_t __optlen);
234 /* Prepare to accept connections on socket FD.
235 N connection requests will be queued before further requests are refused.
236 Returns 0 on success, -1 for errors. */
237 extern int __THROW listen (int __fd, int __n);
239 /* Await a connection on socket FD.
240 When a connection arrives, open a new socket to communicate with it,
241 set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
242 peer and *ADDR_LEN to the address's actual length, and return the
243 new socket's descriptor, or -1 for errors.
245 This function is a cancellation point and therefore not marked with
248 accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
251 /* Similar to 'accept' but takes an additional parameter to specify flags.
253 This function is a cancellation point and therefore not marked with
255 /* TBD: implemented later */
257 accept4 (int __fd, __SOCKADDR_ARG __addr,
258 socklen_t * __restrict __addr_len, int __flags);
261 /* Shut down all or part of the connection open on socket FD.
262 HOW determines what to shut down:
263 SHUT_RD = No more receptions;
264 SHUT_WR = No more transmissions;
265 SHUT_RDWR = No more receptions or transmissions.
266 Returns 0 on success, -1 for errors. */
267 extern int __THROW shutdown (int __fd, int __how);
271 * glibc APIs from <sys/epoll.h>
274 /* Creates an epoll instance. Returns an fd for the new instance.
275 The "size" parameter is a hint specifying the number of file
276 descriptors to be associated with the new instance. The fd
277 returned by epoll_create() should be closed with close(). */
278 extern int __THROW epoll_create (int __size);
280 /* Same as epoll_create but with an FLAGS parameter. The unused SIZE
281 parameter has been dropped. */
282 extern int __THROW epoll_create1 (int __flags);
284 /* Manipulate an epoll instance "epfd". Returns 0 in case of success,
285 -1 in case of error ( the "errno" variable will contain the
286 specific error code ) The "op" parameter is one of the EPOLL_CTL_*
287 constants defined above. The "fd" parameter is the target of the
288 operation. The "event" parameter describes which events the caller
289 is interested in and any associated user data. */
291 epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event);
293 #define EP_INT_MAX ((int)(~0U>>1))
294 #define EP_MAX_EVENTS (EP_INT_MAX / sizeof(struct epoll_event))
296 /* Wait for events on an epoll instance "epfd". Returns the number of
297 triggered events returned in "events" buffer. Or -1 in case of
298 error with the "errno" variable set to the specific error code. The
299 "events" parameter is a buffer that will contain triggered
300 events. The "maxevents" is the maximum number of events to be
301 returned ( usually size of "events" ). The "timeout" parameter
302 specifies the maximum wait time in milliseconds (-1 == infinite).
304 This function is a cancellation point and therefore not marked with
307 epoll_wait (int __epfd, struct epoll_event *__events,
308 int __maxevents, int __timeout);
310 /* Same as epoll_wait, but the thread's signal mask is temporarily
311 and atomically replaced with the one provided as parameter.
313 This function is a cancellation point and therefore not marked with
316 epoll_pwait (int __epfd, struct epoll_event *__events,
317 int __maxevents, int __timeout, const __sigset_t * __ss);
319 /* Poll the file descriptors described by the NFDS structures starting at
320 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
321 an event to occur; if TIMEOUT is -1, block until an event occurs.
322 Returns the number of file descriptors with events, zero if timed out,
325 This function is a cancellation point and therefore not marked with
327 extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
330 /* Like poll, but before waiting the threads signal mask is replaced
331 with that specified in the fourth parameter. For better usability,
332 the timeout value is specified using a TIMESPEC object.
334 This function is a cancellation point and therefore not marked with
336 extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
337 const struct timespec *__timeout, const __sigset_t * __ss);
341 #endif /* included_vcom_glibc_socket_h */
344 * fd.io coding-style-patch-verification: ON
347 * eval: (c-set-style "gnu")