Initial push of vcl-ldpreload to extras
[vpp.git] / extras / vcl-ldpreload / src / libvcl-ldpreload / vcom_glibc_socket.h
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.  */
41 extern 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.  */
48 extern 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.  */
54 extern 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.  */
66 extern 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.  */
82 extern int
83 select (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.  */
95 extern int
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);
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.  */
117 extern 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.  */
123 extern int __THROW
124 socketpair (int __domain, int __type, int __protocol, int __fds[2]);
125
126 /* Give the socket FD the local address ADDR (which is LEN bytes long).  */
127 extern int __THROW
128 bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
129
130 /* Put the local address of FD into *ADDR and its length in *LEN.  */
131 extern int __THROW
132 getsockname (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.  */
141 extern 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.  */
145 extern int __THROW
146 getpeername (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.  */
152 extern 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.  */
159 extern 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.  */
166 extern ssize_t
167 sendto (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.  */
177 extern ssize_t
178 recvfrom (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.  */
187 extern ssize_t
188 sendmsg (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.  */
196 extern int
197 sendmmsg (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.  */
206 extern 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.  */
214 extern int
215 recvmmsg (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.  */
223 extern int __THROW
224 getsockopt (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.  */
230 extern int __THROW
231 setsockopt (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.  */
237 extern 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.  */
247 extern int
248 accept (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 */
256 extern int
257 accept4 (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.  */
267 extern 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().  */
278 extern int __THROW epoll_create (int __size);
279
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);
283
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.  */
290 extern int __THROW
291 epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event);
292
293 #define EP_INT_MAX ((int)(~0U>>1))
294 #define EP_MAX_EVENTS (EP_INT_MAX / sizeof(struct epoll_event))
295
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).
303
304    This function is a cancellation point and therefore not marked with
305    __THROW.  */
306 extern int
307 epoll_wait (int __epfd, struct epoll_event *__events,
308             int __maxevents, int __timeout);
309
310 /* Same as epoll_wait, but the thread's signal mask is temporarily
311    and atomically replaced with the one provided as parameter.
312
313    This function is a cancellation point and therefore not marked with
314    __THROW.  */
315 extern int
316 epoll_pwait (int __epfd, struct epoll_event *__events,
317              int __maxevents, int __timeout, const __sigset_t * __ss);
318
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,
323    or -1 for errors.
324
325    This function is a cancellation point and therefore not marked with
326    __THROW.  */
327 extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
328
329 #ifdef __USE_GNU
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.
333
334    This function is a cancellation point and therefore not marked with
335    __THROW.  */
336 extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
337                   const struct timespec *__timeout, const __sigset_t * __ss);
338 #endif
339
340
341 #endif /* included_vcom_glibc_socket_h */
342
343 /*
344  * fd.io coding-style-patch-verification: ON
345  *
346  * Local Variables:
347  * eval: (c-set-style "gnu")
348  * End:
349  */