vcom_socket.h revision a7221052
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_socket_h
17#define included_vcom_socket_h
18
19#include <libvcl-ldpreload/vcom_glibc_socket.h>
20#include <vppinfra/types.h>
21
22#define INVALID_SESSION_ID (~0)
23#define INVALID_FD (~0)
24
25typedef enum
26{
27  SOCKET_TYPE_UNBOUND = 0,
28  SOCKET_TYPE_KERNEL_BOUND,
29  SOCKET_TYPE_VPPCOM_BOUND
30} vcom_socket_type_t;
31
32typedef struct
33{
34  /* file descriptor -
35   * fd 0, 1, 2 have special meaning and are reserved,
36   * -1 denote invalid fd */
37  i32 fd;
38
39  /* session id - -1 denote invalid sid */
40  i32 sid;
41
42  /* socket type */
43  vcom_socket_type_t type;
44
45  /* vcom socket attributes here */
46
47} vcom_socket_t;
48
49static inline char *
50vcom_socket_type_str (vcom_socket_type_t t)
51{
52  switch (t)
53    {
54    case SOCKET_TYPE_UNBOUND:
55      return "SOCKET_TYPE_UNBOUND";
56
57    case SOCKET_TYPE_KERNEL_BOUND:
58      return "SOCKET_TYPE_KERNEL_BOUND";
59
60    case SOCKET_TYPE_VPPCOM_BOUND:
61      return "SOCKET_TYPE_VPPCOM_BOUND";
62
63    default:
64      return "SOCKET_TYPE_UNKNOWN";
65    }
66}
67
68static inline int
69vcom_socket_type_is_vppcom_bound (vcom_socket_type_t t)
70{
71  return t == SOCKET_TYPE_VPPCOM_BOUND;
72}
73
74static inline void
75vsocket_init (vcom_socket_t * vsock)
76{
77  vsock->fd = INVALID_FD;
78  vsock->sid = INVALID_SESSION_ID;
79  vsock->type = SOCKET_TYPE_UNBOUND;
80  /* vcom socket attributes init here */
81}
82
83static inline void
84vsocket_set (vcom_socket_t * vsock, i32 fd, i32 sid, vcom_socket_type_t type)
85{
86  vsock->fd = fd;
87  vsock->sid = sid;
88  vsock->type = type;
89  /* vcom socket attributes set here */
90}
91
92static inline int
93vsocket_is_vppcom_bound (vcom_socket_t * vsock)
94{
95  return vcom_socket_type_is_vppcom_bound (vsock->type);
96}
97
98int vcom_socket_open_socket (int domain, int type, int protocol);
99
100int vcom_socket_close_socket (int fd);
101
102int vcom_socket_main_init (void);
103
104void vcom_socket_main_destroy (void);
105
106void vcom_socket_main_show (void);
107
108int vcom_socket_is_vcom_fd (int fd);
109
110int vcom_socket_close (int __fd);
111
112ssize_t vcom_socket_read (int __fd, void *__buf, size_t __nbytes);
113
114ssize_t vcom_socket_write (int __fd, const void *__buf, size_t __n);
115
116int vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap);
117
118int
119vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
120		    fd_set * __restrict vcom_writefds,
121		    fd_set * __restrict vcom_exceptfds,
122		    struct timeval *__restrict timeout);
123
124
125int vcom_socket_socket (int __domain, int __type, int __protocol);
126
127int
128vcom_socket_socketpair (int __domain, int __type, int __protocol,
129			int __fds[2]);
130
131int vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
132
133int
134vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
135			 socklen_t * __restrict __len);
136
137int
138vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
139
140int
141vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
142			 socklen_t * __restrict __len);
143
144ssize_t
145vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags);
146
147ssize_t vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags);
148
149/*
150 * RETURN   1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
151 * 0 otherwise
152 * */
153int vcom_socket_is_connection_mode_socket (int __fd);
154
155ssize_t
156vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
157		    int __flags, __CONST_SOCKADDR_ARG __addr,
158		    socklen_t __addr_len);
159
160ssize_t
161vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
162		      int __flags, __SOCKADDR_ARG __addr,
163		      socklen_t * __restrict __addr_len);
164
165ssize_t
166vcom_socket_sendmsg (int __fd, const struct msghdr *__message, int __flags);
167
168#ifdef __USE_GNU
169int
170vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
171		      unsigned int __vlen, int __flags);
172#endif
173
174ssize_t vcom_socket_recvmsg (int __fd, struct msghdr *__message, int __flags);
175
176#ifdef __USE_GNU
177int
178vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
179		      unsigned int __vlen, int __flags,
180		      struct timespec *__tmo);
181#endif
182
183int
184vcom_socket_getsockopt (int __fd, int __level, int __optname,
185			void *__restrict __optval,
186			socklen_t * __restrict __optlen);
187
188int
189vcom_socket_setsockopt (int __fd, int __level, int __optname,
190			const void *__optval, socklen_t __optlen);
191
192int vcom_socket_listen (int __fd, int __n);
193
194int
195vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
196		    socklen_t * __restrict __addr_len);
197
198#ifdef __USE_GNU
199int
200vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
201		     socklen_t * __restrict __addr_len, int __flags);
202#endif
203
204int vcom_socket_shutdown (int __fd, int __how);
205
206#endif /* included_vcom_socket_h */
207
208/*
209 * fd.io coding-style-patch-verification: ON
210 *
211 * Local Variables:
212 * eval: (c-set-style "gnu")
213 * End:
214 */
215