Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Tom Jones | 40c95f5 | 2024-03-20 09:52:16 +0000 | [diff] [blame] | 44 | #ifdef __FreeBSD__ |
| 45 | #include <errno.h> |
| 46 | #define EBADFD EBADF |
| 47 | #endif /* __FreeBSD__ */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
| 49 | #include <vppinfra/clib.h> |
| 50 | #include <vppinfra/error.h> |
| 51 | #include <vppinfra/format.h> |
| 52 | |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 53 | typedef enum |
| 54 | { |
| 55 | CLIB_SOCKET_TYPE_UNKNOWN = 0, |
| 56 | CLIB_SOCKET_TYPE_INET, |
| 57 | CLIB_SOCKET_TYPE_UNIX, |
| 58 | #if CLIB_LINUX |
| 59 | CLIB_SOCKET_TYPE_LINUX_ABSTRACT, |
| 60 | #endif |
| 61 | } clib_socket_type_t; |
| 62 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 63 | typedef struct _socket_t |
| 64 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | /* File descriptor. */ |
| 66 | i32 fd; |
| 67 | |
| 68 | /* Config string for socket HOST:PORT or just HOST. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | char *config; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 71 | union |
| 72 | { |
| 73 | struct |
| 74 | { |
| 75 | u32 is_server : 1; |
| 76 | u32 rx_end_of_file : 1; |
| 77 | u32 non_blocking_connect : 1; |
| 78 | u32 allow_group_write : 1; |
| 79 | u32 is_seqpacket : 1; |
| 80 | u32 passcred : 1; |
| 81 | u32 is_blocking : 1; |
| 82 | u32 local_only : 1; |
| 83 | }; |
| 84 | u32 flags; |
| 85 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | /* Transmit buffer. Holds data waiting to be written. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | u8 *tx_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
| 90 | /* Receive buffer. Holds data read from socket. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | u8 *rx_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
| 93 | /* Peer socket we are connected to. */ |
| 94 | struct sockaddr_in peer; |
| 95 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 96 | /* Credentials, populated if CLIB_SOCKET_F_PASSCRED is set */ |
| 97 | pid_t pid; |
| 98 | uid_t uid; |
| 99 | gid_t gid; |
| 100 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 101 | clib_error_t *(*write_func) (struct _socket_t * sock); |
| 102 | clib_error_t *(*read_func) (struct _socket_t * sock, int min_bytes); |
| 103 | clib_error_t *(*close_func) (struct _socket_t * sock); |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 104 | clib_error_t *(*recvmsg_func) (struct _socket_t * s, void *msg, int msglen, |
| 105 | int fds[], int num_fds); |
| 106 | clib_error_t *(*sendmsg_func) (struct _socket_t * s, void *msg, int msglen, |
| 107 | int fds[], int num_fds); |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 108 | clib_socket_type_t type; |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 109 | uword private_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | } clib_socket_t; |
| 111 | |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 112 | #define CLIB_SOCKET_FLAG(f) (((clib_socket_t){ .f = 1 }).flags) |
| 113 | #define CLIB_SOCKET_F_IS_CLIENT 0 |
| 114 | #define CLIB_SOCKET_F_IS_SERVER CLIB_SOCKET_FLAG (is_server) |
| 115 | #define CLIB_SOCKET_F_ALLOW_GROUP_WRITE CLIB_SOCKET_FLAG (allow_group_write) |
| 116 | #define CLIB_SOCKET_F_SEQPACKET CLIB_SOCKET_FLAG (is_seqpacket) |
| 117 | #define CLIB_SOCKET_F_PASSCRED CLIB_SOCKET_FLAG (passcred) |
| 118 | #define CLIB_SOCKET_F_BLOCKING CLIB_SOCKET_FLAG (is_blocking) |
| 119 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | /* socket config format is host:port. |
| 121 | Unspecified port causes a free one to be chosen starting |
| 122 | from IPPORT_USERRESERVED (5000). */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | clib_error_t *clib_socket_init (clib_socket_t * socket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 125 | clib_error_t *clib_socket_accept (clib_socket_t * server, |
| 126 | clib_socket_t * client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 128 | int clib_socket_prefix_is_valid (char *s); |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 129 | int clib_socket_prefix_get_type (char *s); |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 130 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | always_inline uword |
| 132 | clib_socket_is_server (clib_socket_t * sock) |
| 133 | { |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 134 | return sock->is_server; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 137 | always_inline uword |
| 138 | clib_socket_is_client (clib_socket_t * s) |
| 139 | { |
| 140 | return !clib_socket_is_server (s); |
| 141 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 143 | always_inline uword |
| 144 | clib_socket_is_connected (clib_socket_t * sock) |
| 145 | { |
| 146 | return sock->fd > 0; |
| 147 | } |
| 148 | |
| 149 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 150 | always_inline int |
| 151 | clib_socket_rx_end_of_file (clib_socket_t * s) |
| 152 | { |
Damjan Marion | 085757b | 2023-01-30 11:48:38 +0100 | [diff] [blame] | 153 | return s->rx_end_of_file; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 154 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
| 156 | always_inline void * |
| 157 | clib_socket_tx_add (clib_socket_t * s, int n_bytes) |
| 158 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 159 | u8 *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | vec_add2 (s->tx_buffer, result, n_bytes); |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 165 | clib_socket_tx_add_va_formatted (clib_socket_t * s, char *fmt, va_list * va) |
| 166 | { |
| 167 | s->tx_buffer = va_format (s->tx_buffer, fmt, va); |
| 168 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
| 170 | always_inline clib_error_t * |
| 171 | clib_socket_tx (clib_socket_t * s) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 172 | { |
| 173 | return s->write_func (s); |
| 174 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | always_inline clib_error_t * |
| 177 | clib_socket_rx (clib_socket_t * s, int n_bytes) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | { |
| 179 | return s->read_func (s, n_bytes); |
| 180 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 182 | always_inline clib_error_t * |
| 183 | clib_socket_sendmsg (clib_socket_t * s, void *msg, int msglen, |
| 184 | int fds[], int num_fds) |
| 185 | { |
| 186 | return s->sendmsg_func (s, msg, msglen, fds, num_fds); |
| 187 | } |
| 188 | |
| 189 | always_inline clib_error_t * |
| 190 | clib_socket_recvmsg (clib_socket_t * s, void *msg, int msglen, |
| 191 | int fds[], int num_fds) |
| 192 | { |
| 193 | return s->recvmsg_func (s, msg, msglen, fds, num_fds); |
| 194 | } |
| 195 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 197 | clib_socket_free (clib_socket_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | { |
| 199 | vec_free (s->tx_buffer); |
| 200 | vec_free (s->rx_buffer); |
| 201 | if (clib_mem_is_heap_object (s->config)) |
| 202 | vec_free (s->config); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 203 | clib_memset (s, 0, sizeof (s[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | always_inline clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 207 | clib_socket_close (clib_socket_t * sock) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 209 | clib_error_t *err; |
| 210 | err = (*sock->close_func) (sock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | return err; |
| 212 | } |
| 213 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 214 | void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
| 216 | #endif /* _clib_included_socket_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * fd.io coding-style-patch-verification: ON |
| 220 | * |
| 221 | * Local Variables: |
| 222 | * eval: (c-set-style "gnu") |
| 223 | * End: |
| 224 | */ |