vcom_glibc_socket.h revision 20574915
1/*
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:
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 included_vcom_glibc_socket_h
17#define included_vcom_glibc_socket_h
18
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <sys/select.h>
22#include <arpa/inet.h>
23#include <fcntl.h>
24
25#include <sys/epoll.h>
26#include <poll.h>
27
28/*
29 *
30 * Generic glibc fd api
31 *
32 */
33/*
34 * glibc APIs from <unistd.h>
35 */
36
37/* Close the file descriptor FD.
38
39   This function is a cancellation point and therefore not marked with
40   __THROW.  */
41extern int close (int __fd);
42
43/* Read NBYTES into BUF from FD.  Return the
44   number read, -1 for errors or 0 for EOF.
45
46   This function is a cancellation point and therefore not marked with
47   __THROW.  */
48extern ssize_t __wur read (int __fd, void *__buf, size_t __nbytes);
49
50/* Write N bytes of BUF to FD.  Return the number written, or -1.
51
52   This function is a cancellation point and therefore not marked with
53   __THROW.  */
54extern ssize_t __wur write (int __fd, const void *__buf, size_t __n);
55
56
57/*
58 * glibc APIs from <fcntl.h>
59 */
60
61/* Do the file control operation described by CMD on FD.
62   The remaining arguments are interpreted depending on CMD.
63
64   This function is a cancellation point and therefore not marked with
65   __THROW.  */
66extern int fcntl (int __fd, int __cmd, ...);
67
68
69/*
70 * glibc APIs from <sys/select.h>
71 */
72
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.
78
79
80   This function is a cancellation point and therefore not marked with
81   __THROW.  */
82extern int
83select (int __nfds, fd_set * __restrict __readfds,
84        fd_set * __restrict __writefds,
85        fd_set * __restrict __exceptfds,
86        struct timeval *__restrict __timeout);
87
88#ifdef __USE_XOPEN2K
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
91   should be used.
92
93   This function is a cancellation point and therefore not marked with
94   __THROW.  */
95extern int
96pselect (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);
101#endif
102
103
104/*
105 *
106 * Socket specific glibc api
107 *
108 */
109
110/*
111 * glibc APIs from <sys/socket.h>
112 */
113
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.  */
117extern int __THROW socket (int __domain, int __type, int __protocol);
118
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.  */
123extern int __THROW
124socketpair (int __domain, int __type, int __protocol, int __fds[2]);
125
126/* Give the socket FD the local address ADDR (which is LEN bytes long).  */
127extern int __THROW
128bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
129
130/* Put the local address of FD into *ADDR and its length in *LEN.  */
131extern int __THROW
132getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
133
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.
138
139   This function is a cancellation point and therefore not marked with
140   __THROW.  */
141extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
142
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.  */
145extern int __THROW
146getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
147
148/* Send N bytes of BUF to socket FD.  Returns the number sent or -1.
149
150   This function is a cancellation point and therefore not marked with
151   __THROW.  */
152extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
153
154/* Read N bytes into BUF from socket FD.
155   Returns the number read or -1 for errors.
156
157   This function is a cancellation point and therefore not marked with
158   __THROW.  */
159extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
160
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.
163
164   This function is a cancellation point and therefore not marked with
165   __THROW.  */
166extern ssize_t
167sendto (int __fd, const void *__buf, size_t __n,
168        int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len);
169
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.
174
175   This function is a cancellation point and therefore not marked with
176   __THROW.  */
177extern ssize_t
178recvfrom (int __fd, void *__restrict __buf,
179          size_t __n, int __flags,
180          __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
181
182/* Send a message described MESSAGE on socket FD.
183   Returns the number of bytes sent, or -1 for errors.
184
185   This function is a cancellation point and therefore not marked with
186   __THROW.  */
187extern ssize_t
188sendmsg (int __fd, const struct msghdr *__message, int __flags);
189
190#ifdef __USE_GNU
191/* Send a VLEN messages as described by VMESSAGES to socket FD.
192   Returns the number of datagrams successfully written or -1 for errors.
193
194   This function is a cancellation point and therefore not marked with
195   __THROW.  */
196extern int
197sendmmsg (int __fd, struct mmsghdr *__vmessages,
198          unsigned int __vlen, int __flags);
199#endif
200
201/* Receive a message as described by MESSAGE from socket FD.
202   Returns the number of bytes read or -1 for errors.
203
204   This function is a cancellation point and therefore not marked with
205   __THROW.  */
206extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
207
208#ifdef __USE_GNU
209/* Receive up to VLEN messages as described by VMESSAGES from socket FD.
210   Returns the number of messages received or -1 for errors.
211
212   This function is a cancellation point and therefore not marked with
213   __THROW.  */
214extern int
215recvmmsg (int __fd, struct mmsghdr *__vmessages,
216          unsigned int __vlen, int __flags, struct timespec *__tmo);
217#endif
218
219
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.  */
223extern int __THROW
224getsockopt (int __fd, int __level, int __optname,
225            void *__restrict __optval, socklen_t * __restrict __optlen);
226
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.  */
230extern int __THROW
231setsockopt (int __fd, int __level, int __optname,
232            const void *__optval, socklen_t __optlen);
233
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.  */
237extern int __THROW listen (int __fd, int __n);
238
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.
244
245   This function is a cancellation point and therefore not marked with
246   __THROW.  */
247extern int
248accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
249
250#ifdef __USE_GNU
251/* Similar to 'accept' but takes an additional parameter to specify flags.
252
253   This function is a cancellation point and therefore not marked with
254   __THROW.  */
255     /* TBD: implemented later */
256extern int
257accept4 (int __fd, __SOCKADDR_ARG __addr,
258         socklen_t * __restrict __addr_len, int __flags);
259#endif
260
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.  */
267extern int __THROW shutdown (int __fd, int __how);
268
269
270/*
271 * glibc APIs from <sys/epoll.h>
272 */
273
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().  */
278extern int __THROW
279epoll_create (int __size);
280
281/* Same as epoll_create but with an FLAGS parameter.  The unused SIZE
282   parameter has been dropped.  */
283extern int __THROW
284epoll_create1 (int __flags);
285
286/* Manipulate an epoll instance "epfd". Returns 0 in case of success,
287   -1 in case of error ( the "errno" variable will contain the
288   specific error code ) The "op" parameter is one of the EPOLL_CTL_*
289   constants defined above. The "fd" parameter is the target of the
290   operation. The "event" parameter describes which events the caller
291   is interested in and any associated user data.  */
292extern int __THROW
293epoll_ctl (int __epfd, int __op, int __fd,
294           struct epoll_event *__event);
295
296#define EP_INT_MAX ((int)(~0U>>1))
297#define EP_MAX_EVENTS (EP_INT_MAX / sizeof(struct epoll_event))
298
299/* Wait for events on an epoll instance "epfd". Returns the number of
300   triggered events returned in "events" buffer. Or -1 in case of
301   error with the "errno" variable set to the specific error code. The
302   "events" parameter is a buffer that will contain triggered
303   events. The "maxevents" is the maximum number of events to be
304   returned ( usually size of "events" ). The "timeout" parameter
305   specifies the maximum wait time in milliseconds (-1 == infinite).
306
307   This function is a cancellation point and therefore not marked with
308   __THROW.  */
309extern int
310epoll_wait (int __epfd, struct epoll_event *__events,
311            int __maxevents, int __timeout);
312
313/* Same as epoll_wait, but the thread's signal mask is temporarily
314   and atomically replaced with the one provided as parameter.
315
316   This function is a cancellation point and therefore not marked with
317   __THROW.  */
318extern int
319epoll_pwait (int __epfd, struct epoll_event *__events,
320             int __maxevents, int __timeout,
321             const __sigset_t *__ss);
322
323/* Poll the file descriptors described by the NFDS structures starting at
324   FDS.  If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
325   an event to occur; if TIMEOUT is -1, block until an event occurs.
326   Returns the number of file descriptors with events, zero if timed out,
327   or -1 for errors.
328
329   This function is a cancellation point and therefore not marked with
330   __THROW.  */
331extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
332
333#ifdef __USE_GNU
334/* Like poll, but before waiting the threads signal mask is replaced
335   with that specified in the fourth parameter.  For better usability,
336   the timeout value is specified using a TIMESPEC object.
337
338   This function is a cancellation point and therefore not marked with
339   __THROW.  */
340extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
341                  const struct timespec *__timeout,
342                  const __sigset_t *__ss);
343#endif
344
345
346#endif /* included_vcom_glibc_socket_h */
347
348/*
349 * fd.io coding-style-patch-verification: ON
350 *
351 * Local Variables:
352 * eval: (c-set-style "gnu")
353 * End:
354 */
355