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