vcom_glibc_socket.h revision eedb87c1
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/*
27 *
28 * Generic glibc fd api
29 *
30 */
31/*
32 * glibc APIs from <unistd.h>
33 */
34
35/* Close the file descriptor FD.
36
37   This function is a cancellation point and therefore not marked with
38   __THROW.  */
39extern int close (int __fd);
40
41/* Read NBYTES into BUF from FD.  Return the
42   number read, -1 for errors or 0 for EOF.
43
44   This function is a cancellation point and therefore not marked with
45   __THROW.  */
46extern ssize_t __wur read (int __fd, void *__buf, size_t __nbytes);
47
48/* Write N bytes of BUF to FD.  Return the number written, or -1.
49
50   This function is a cancellation point and therefore not marked with
51   __THROW.  */
52extern ssize_t __wur write (int __fd, const void *__buf, size_t __n);
53
54
55/*
56 * glibc APIs from <fcntl.h>
57 */
58
59/* Do the file control operation described by CMD on FD.
60   The remaining arguments are interpreted depending on CMD.
61
62   This function is a cancellation point and therefore not marked with
63   __THROW.  */
64extern int fcntl (int __fd, int __cmd, ...);
65
66
67/*
68 * glibc APIs from <sys/select.h>
69 */
70
71/* Check the first NFDS descriptors each in READFDS (if not NULL) for read
72   readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
73   (if not NULL) for exceptional conditions.  If TIMEOUT is not NULL, time out
74   after waiting the interval specified therein.  Returns the number of ready
75   descriptors, or -1 for errors.
76
77
78   This function is a cancellation point and therefore not marked with
79   __THROW.  */
80extern int
81select (int __nfds, fd_set * __restrict __readfds,
82        fd_set * __restrict __writefds,
83        fd_set * __restrict __exceptfds,
84        struct timeval *__restrict __timeout);
85
86#ifdef __USE_XOPEN2K
87/* Same as above only that the TIMEOUT value is given with higher
88   resolution and a sigmask which is been set temporarily.  This version
89   should be used.
90
91   This function is a cancellation point and therefore not marked with
92   __THROW.  */
93extern int
94pselect (int __nfds, fd_set * __restrict __readfds,
95         fd_set * __restrict __writefds,
96         fd_set * __restrict __exceptfds,
97         const struct timespec *__restrict __timeout,
98         const __sigset_t * __restrict __sigmask);
99#endif
100
101
102/*
103 *
104 * Socket specific glibc api
105 *
106 */
107
108/*
109 * glibc APIs from <sys/socket.h>
110 */
111
112/* Create a new socket of type TYPE in domain DOMAIN, using
113   protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
114   Returns a file descriptor for the new socket, or -1 for errors.  */
115extern int __THROW socket (int __domain, int __type, int __protocol);
116
117/* Create two new sockets, of type TYPE in domain DOMAIN and using
118   protocol PROTOCOL, which are connected to each other, and put file
119   descriptors for them in FDS[0] and FDS[1].  If PROTOCOL is zero,
120   one will be chosen automatically.  Returns 0 on success, -1 for errors.  */
121extern int __THROW
122socketpair (int __domain, int __type, int __protocol, int __fds[2]);
123
124/* Give the socket FD the local address ADDR (which is LEN bytes long).  */
125extern int __THROW
126bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
127
128/* Put the local address of FD into *ADDR and its length in *LEN.  */
129extern int __THROW
130getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
131
132/* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
133   For connectionless socket types, just set the default address to send to
134   and the only address from which to accept transmissions.
135   Return 0 on success, -1 for errors.
136
137   This function is a cancellation point and therefore not marked with
138   __THROW.  */
139extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
140
141/* Put the address of the peer connected to socket FD into *ADDR
142   (which is *LEN bytes long), and its actual length into *LEN.  */
143extern int __THROW
144getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len);
145
146/* Send N bytes of BUF to socket FD.  Returns the number sent or -1.
147
148   This function is a cancellation point and therefore not marked with
149   __THROW.  */
150extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
151
152/* Read N bytes into BUF from socket FD.
153   Returns the number read or -1 for errors.
154
155   This function is a cancellation point and therefore not marked with
156   __THROW.  */
157extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
158
159/* Send N bytes of BUF on socket FD to peer at address ADDR (which is
160   ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
161
162   This function is a cancellation point and therefore not marked with
163   __THROW.  */
164extern ssize_t
165sendto (int __fd, const void *__buf, size_t __n,
166        int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len);
167
168/* Read N bytes into BUF through socket FD.
169   If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
170   the sender, and store the actual size of the address in *ADDR_LEN.
171   Returns the number of bytes read or -1 for errors.
172
173   This function is a cancellation point and therefore not marked with
174   __THROW.  */
175extern ssize_t
176recvfrom (int __fd, void *__restrict __buf,
177          size_t __n, int __flags,
178          __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
179
180/* Send a message described MESSAGE on socket FD.
181   Returns the number of bytes sent, or -1 for errors.
182
183   This function is a cancellation point and therefore not marked with
184   __THROW.  */
185extern ssize_t
186sendmsg (int __fd, const struct msghdr *__message, int __flags);
187
188#ifdef __USE_GNU
189/* Send a VLEN messages as described by VMESSAGES to socket FD.
190   Returns the number of datagrams successfully written or -1 for errors.
191
192   This function is a cancellation point and therefore not marked with
193   __THROW.  */
194extern int
195sendmmsg (int __fd, struct mmsghdr *__vmessages,
196          unsigned int __vlen, int __flags);
197#endif
198
199/* Receive a message as described by MESSAGE from socket FD.
200   Returns the number of bytes read or -1 for errors.
201
202   This function is a cancellation point and therefore not marked with
203   __THROW.  */
204extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
205
206#ifdef __USE_GNU
207/* Receive up to VLEN messages as described by VMESSAGES from socket FD.
208   Returns the number of messages received or -1 for errors.
209
210   This function is a cancellation point and therefore not marked with
211   __THROW.  */
212extern int
213recvmmsg (int __fd, struct mmsghdr *__vmessages,
214          unsigned int __vlen, int __flags, struct timespec *__tmo);
215#endif
216
217
218/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
219   into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
220   actual length.  Returns 0 on success, -1 for errors.  */
221extern int __THROW
222getsockopt (int __fd, int __level, int __optname,
223            void *__restrict __optval, socklen_t * __restrict __optlen);
224
225/* Set socket FD's option OPTNAME at protocol level LEVEL
226   to *OPTVAL (which is OPTLEN bytes long).
227   Returns 0 on success, -1 for errors.  */
228extern int __THROW
229setsockopt (int __fd, int __level, int __optname,
230            const void *__optval, socklen_t __optlen);
231
232/* Prepare to accept connections on socket FD.
233   N connection requests will be queued before further requests are refused.
234   Returns 0 on success, -1 for errors.  */
235extern int __THROW listen (int __fd, int __n);
236
237/* Await a connection on socket FD.
238   When a connection arrives, open a new socket to communicate with it,
239   set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
240   peer and *ADDR_LEN to the address's actual length, and return the
241   new socket's descriptor, or -1 for errors.
242
243   This function is a cancellation point and therefore not marked with
244   __THROW.  */
245extern int
246accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
247
248#ifdef __USE_GNU
249/* Similar to 'accept' but takes an additional parameter to specify flags.
250
251   This function is a cancellation point and therefore not marked with
252   __THROW.  */
253     /* TBD: implemented later */
254extern int
255accept4 (int __fd, __SOCKADDR_ARG __addr,
256         socklen_t * __restrict __addr_len, int __flags);
257#endif
258
259/* Shut down all or part of the connection open on socket FD.
260   HOW determines what to shut down:
261     SHUT_RD   = No more receptions;
262     SHUT_WR   = No more transmissions;
263     SHUT_RDWR = No more receptions or transmissions.
264   Returns 0 on success, -1 for errors.  */
265extern int __THROW shutdown (int __fd, int __how);
266
267
268/*
269 * glibc APIs from <sys/epoll.h>
270 */
271
272/* Creates an epoll instance.  Returns an fd for the new instance.
273   The "size" parameter is a hint specifying the number of file
274   descriptors to be associated with the new instance.  The fd
275   returned by epoll_create() should be closed with close().  */
276extern int __THROW
277epoll_create (int __size);
278
279/* Same as epoll_create but with an FLAGS parameter.  The unused SIZE
280   parameter has been dropped.  */
281extern int __THROW
282epoll_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.  */
290extern int __THROW
291epoll_ctl (int __epfd, int __op, int __fd,
292           struct epoll_event *__event);
293
294#define EP_INT_MAX ((int)(~0U>>1))
295#define EP_MAX_EVENTS (EP_INT_MAX / sizeof(struct epoll_event))
296
297/* Wait for events on an epoll instance "epfd". Returns the number of
298   triggered events returned in "events" buffer. Or -1 in case of
299   error with the "errno" variable set to the specific error code. The
300   "events" parameter is a buffer that will contain triggered
301   events. The "maxevents" is the maximum number of events to be
302   returned ( usually size of "events" ). The "timeout" parameter
303   specifies the maximum wait time in milliseconds (-1 == infinite).
304
305   This function is a cancellation point and therefore not marked with
306   __THROW.  */
307extern int
308epoll_wait (int __epfd, struct epoll_event *__events,
309            int __maxevents, int __timeout);
310
311/* Same as epoll_wait, but the thread's signal mask is temporarily
312   and atomically replaced with the one provided as parameter.
313
314   This function is a cancellation point and therefore not marked with
315   __THROW.  */
316extern int
317epoll_pwait (int __epfd, struct epoll_event *__events,
318             int __maxevents, int __timeout,
319             const __sigset_t *__ss);
320
321#endif /* included_vcom_glibc_socket_h */
322
323/*
324 * fd.io coding-style-patch-verification: ON
325 *
326 * Local Variables:
327 * eval: (c-set-style "gnu")
328 * End:
329 */
330