blob: 132e89f2871202cb6c01d4c3709a846df0fbff8e [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef _clib_included_socket_h
39#define _clib_included_socket_h
40
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44
45#include <vppinfra/clib.h>
46#include <vppinfra/error.h>
47#include <vppinfra/format.h>
48
Damjan Marion085757b2023-01-30 11:48:38 +010049typedef enum
50{
51 CLIB_SOCKET_TYPE_UNKNOWN = 0,
52 CLIB_SOCKET_TYPE_INET,
53 CLIB_SOCKET_TYPE_UNIX,
54#if CLIB_LINUX
55 CLIB_SOCKET_TYPE_LINUX_ABSTRACT,
56#endif
57} clib_socket_type_t;
58
Dave Barachc3799992016-08-15 11:12:27 -040059typedef struct _socket_t
60{
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 /* File descriptor. */
62 i32 fd;
63
64 /* Config string for socket HOST:PORT or just HOST. */
Dave Barachc3799992016-08-15 11:12:27 -040065 char *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Damjan Marion085757b2023-01-30 11:48:38 +010067 union
68 {
69 struct
70 {
71 u32 is_server : 1;
72 u32 rx_end_of_file : 1;
73 u32 non_blocking_connect : 1;
74 u32 allow_group_write : 1;
75 u32 is_seqpacket : 1;
76 u32 passcred : 1;
77 u32 is_blocking : 1;
78 u32 local_only : 1;
79 };
80 u32 flags;
81 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 /* Transmit buffer. Holds data waiting to be written. */
Dave Barachc3799992016-08-15 11:12:27 -040084 u8 *tx_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 /* Receive buffer. Holds data read from socket. */
Dave Barachc3799992016-08-15 11:12:27 -040087 u8 *rx_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89 /* Peer socket we are connected to. */
90 struct sockaddr_in peer;
91
Damjan Marionf49921f2017-09-11 16:52:11 +020092 /* Credentials, populated if CLIB_SOCKET_F_PASSCRED is set */
93 pid_t pid;
94 uid_t uid;
95 gid_t gid;
96
Dave Barachc3799992016-08-15 11:12:27 -040097 clib_error_t *(*write_func) (struct _socket_t * sock);
98 clib_error_t *(*read_func) (struct _socket_t * sock, int min_bytes);
99 clib_error_t *(*close_func) (struct _socket_t * sock);
Damjan Marionf49921f2017-09-11 16:52:11 +0200100 clib_error_t *(*recvmsg_func) (struct _socket_t * s, void *msg, int msglen,
101 int fds[], int num_fds);
102 clib_error_t *(*sendmsg_func) (struct _socket_t * s, void *msg, int msglen,
103 int fds[], int num_fds);
Damjan Marion085757b2023-01-30 11:48:38 +0100104 clib_socket_type_t type;
Damjan Marionf49921f2017-09-11 16:52:11 +0200105 uword private_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106} clib_socket_t;
107
Damjan Marion085757b2023-01-30 11:48:38 +0100108#define CLIB_SOCKET_FLAG(f) (((clib_socket_t){ .f = 1 }).flags)
109#define CLIB_SOCKET_F_IS_CLIENT 0
110#define CLIB_SOCKET_F_IS_SERVER CLIB_SOCKET_FLAG (is_server)
111#define CLIB_SOCKET_F_ALLOW_GROUP_WRITE CLIB_SOCKET_FLAG (allow_group_write)
112#define CLIB_SOCKET_F_SEQPACKET CLIB_SOCKET_FLAG (is_seqpacket)
113#define CLIB_SOCKET_F_PASSCRED CLIB_SOCKET_FLAG (passcred)
114#define CLIB_SOCKET_F_BLOCKING CLIB_SOCKET_FLAG (is_blocking)
115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116/* socket config format is host:port.
117 Unspecified port causes a free one to be chosen starting
118 from IPPORT_USERRESERVED (5000). */
Dave Barachc3799992016-08-15 11:12:27 -0400119clib_error_t *clib_socket_init (clib_socket_t * socket);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200121clib_error_t *clib_socket_init_netns (clib_socket_t *socket, u8 *namespace);
122
Dave Barachc3799992016-08-15 11:12:27 -0400123clib_error_t *clib_socket_accept (clib_socket_t * server,
124 clib_socket_t * client);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Damjan Marion085757b2023-01-30 11:48:38 +0100126int clib_socket_prefix_is_valid (char *s);
127
Dave Barachc3799992016-08-15 11:12:27 -0400128always_inline uword
129clib_socket_is_server (clib_socket_t * sock)
130{
Damjan Marion085757b2023-01-30 11:48:38 +0100131 return sock->is_server;
Dave Barachc3799992016-08-15 11:12:27 -0400132}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Dave Barachc3799992016-08-15 11:12:27 -0400134always_inline uword
135clib_socket_is_client (clib_socket_t * s)
136{
137 return !clib_socket_is_server (s);
138}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Damjan Marionf49921f2017-09-11 16:52:11 +0200140always_inline uword
141clib_socket_is_connected (clib_socket_t * sock)
142{
143 return sock->fd > 0;
144}
145
146
Dave Barachc3799992016-08-15 11:12:27 -0400147always_inline int
148clib_socket_rx_end_of_file (clib_socket_t * s)
149{
Damjan Marion085757b2023-01-30 11:48:38 +0100150 return s->rx_end_of_file;
Dave Barachc3799992016-08-15 11:12:27 -0400151}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153always_inline void *
154clib_socket_tx_add (clib_socket_t * s, int n_bytes)
155{
Dave Barachc3799992016-08-15 11:12:27 -0400156 u8 *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 vec_add2 (s->tx_buffer, result, n_bytes);
158 return result;
159}
160
161always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400162clib_socket_tx_add_va_formatted (clib_socket_t * s, char *fmt, va_list * va)
163{
164 s->tx_buffer = va_format (s->tx_buffer, fmt, va);
165}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
167always_inline clib_error_t *
168clib_socket_tx (clib_socket_t * s)
Dave Barachc3799992016-08-15 11:12:27 -0400169{
170 return s->write_func (s);
171}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173always_inline clib_error_t *
174clib_socket_rx (clib_socket_t * s, int n_bytes)
Dave Barachc3799992016-08-15 11:12:27 -0400175{
176 return s->read_func (s, n_bytes);
177}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Damjan Marionf49921f2017-09-11 16:52:11 +0200179always_inline clib_error_t *
180clib_socket_sendmsg (clib_socket_t * s, void *msg, int msglen,
181 int fds[], int num_fds)
182{
183 return s->sendmsg_func (s, msg, msglen, fds, num_fds);
184}
185
186always_inline clib_error_t *
187clib_socket_recvmsg (clib_socket_t * s, void *msg, int msglen,
188 int fds[], int num_fds)
189{
190 return s->recvmsg_func (s, msg, msglen, fds, num_fds);
191}
192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400194clib_socket_free (clib_socket_t * s)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195{
196 vec_free (s->tx_buffer);
197 vec_free (s->rx_buffer);
198 if (clib_mem_is_heap_object (s->config))
199 vec_free (s->config);
Dave Barachb7b92992018-10-17 10:38:51 -0400200 clib_memset (s, 0, sizeof (s[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201}
202
203always_inline clib_error_t *
Dave Barachc3799992016-08-15 11:12:27 -0400204clib_socket_close (clib_socket_t * sock)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205{
Dave Barachc3799992016-08-15 11:12:27 -0400206 clib_error_t *err;
207 err = (*sock->close_func) (sock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 return err;
209}
210
Dave Barachc3799992016-08-15 11:12:27 -0400211void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213#endif /* _clib_included_socket_h */
Dave Barachc3799992016-08-15 11:12:27 -0400214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */