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> |
| 44 | |
| 45 | #include <vppinfra/clib.h> |
| 46 | #include <vppinfra/error.h> |
| 47 | #include <vppinfra/format.h> |
| 48 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | typedef struct _socket_t |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | /* File descriptor. */ |
| 52 | i32 fd; |
| 53 | |
| 54 | /* Config string for socket HOST:PORT or just HOST. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | char *config; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
| 57 | u32 flags; |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 58 | #define CLIB_SOCKET_F_IS_SERVER (1 << 0) |
| 59 | #define CLIB_SOCKET_F_IS_CLIENT (0 << 0) |
| 60 | #define CLIB_SOCKET_F_RX_END_OF_FILE (1 << 2) |
| 61 | #define CLIB_SOCKET_F_NON_BLOCKING_CONNECT (1 << 3) |
| 62 | #define CLIB_SOCKET_F_ALLOW_GROUP_WRITE (1 << 4) |
| 63 | #define CLIB_SOCKET_F_SEQPACKET (1 << 5) |
| 64 | #define CLIB_SOCKET_F_PASSCRED (1 << 6) |
Florin Coras | 57e0af9 | 2021-05-26 10:21:10 -0700 | [diff] [blame] | 65 | #define CLIB_SOCKET_F_BLOCKING (1 << 7) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | /* Transmit buffer. Holds data waiting to be written. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 68 | u8 *tx_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
| 70 | /* Receive buffer. Holds data read from socket. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | u8 *rx_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | /* Peer socket we are connected to. */ |
| 74 | struct sockaddr_in peer; |
| 75 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 76 | /* Credentials, populated if CLIB_SOCKET_F_PASSCRED is set */ |
| 77 | pid_t pid; |
| 78 | uid_t uid; |
| 79 | gid_t gid; |
| 80 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 81 | clib_error_t *(*write_func) (struct _socket_t * sock); |
| 82 | clib_error_t *(*read_func) (struct _socket_t * sock, int min_bytes); |
| 83 | clib_error_t *(*close_func) (struct _socket_t * sock); |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 84 | clib_error_t *(*recvmsg_func) (struct _socket_t * s, void *msg, int msglen, |
| 85 | int fds[], int num_fds); |
| 86 | clib_error_t *(*sendmsg_func) (struct _socket_t * s, void *msg, int msglen, |
| 87 | int fds[], int num_fds); |
| 88 | uword private_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | } clib_socket_t; |
| 90 | |
| 91 | /* socket config format is host:port. |
| 92 | Unspecified port causes a free one to be chosen starting |
| 93 | from IPPORT_USERRESERVED (5000). */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 94 | clib_error_t *clib_socket_init (clib_socket_t * socket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 96 | clib_error_t *clib_socket_init_netns (clib_socket_t *socket, u8 *namespace); |
| 97 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | clib_error_t *clib_socket_accept (clib_socket_t * server, |
| 99 | clib_socket_t * client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 101 | always_inline uword |
| 102 | clib_socket_is_server (clib_socket_t * sock) |
| 103 | { |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 104 | return (sock->flags & CLIB_SOCKET_F_IS_SERVER) != 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 105 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 107 | always_inline uword |
| 108 | clib_socket_is_client (clib_socket_t * s) |
| 109 | { |
| 110 | return !clib_socket_is_server (s); |
| 111 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 113 | always_inline uword |
| 114 | clib_socket_is_connected (clib_socket_t * sock) |
| 115 | { |
| 116 | return sock->fd > 0; |
| 117 | } |
| 118 | |
| 119 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | always_inline int |
| 121 | clib_socket_rx_end_of_file (clib_socket_t * s) |
| 122 | { |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 123 | return s->flags & CLIB_SOCKET_F_RX_END_OF_FILE; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 124 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
| 126 | always_inline void * |
| 127 | clib_socket_tx_add (clib_socket_t * s, int n_bytes) |
| 128 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 129 | u8 *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | vec_add2 (s->tx_buffer, result, n_bytes); |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | clib_socket_tx_add_va_formatted (clib_socket_t * s, char *fmt, va_list * va) |
| 136 | { |
| 137 | s->tx_buffer = va_format (s->tx_buffer, fmt, va); |
| 138 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
| 140 | always_inline clib_error_t * |
| 141 | clib_socket_tx (clib_socket_t * s) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | { |
| 143 | return s->write_func (s); |
| 144 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | |
| 146 | always_inline clib_error_t * |
| 147 | clib_socket_rx (clib_socket_t * s, int n_bytes) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 148 | { |
| 149 | return s->read_func (s, n_bytes); |
| 150 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 152 | always_inline clib_error_t * |
| 153 | clib_socket_sendmsg (clib_socket_t * s, void *msg, int msglen, |
| 154 | int fds[], int num_fds) |
| 155 | { |
| 156 | return s->sendmsg_func (s, msg, msglen, fds, num_fds); |
| 157 | } |
| 158 | |
| 159 | always_inline clib_error_t * |
| 160 | clib_socket_recvmsg (clib_socket_t * s, void *msg, int msglen, |
| 161 | int fds[], int num_fds) |
| 162 | { |
| 163 | return s->recvmsg_func (s, msg, msglen, fds, num_fds); |
| 164 | } |
| 165 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 167 | clib_socket_free (clib_socket_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | { |
| 169 | vec_free (s->tx_buffer); |
| 170 | vec_free (s->rx_buffer); |
| 171 | if (clib_mem_is_heap_object (s->config)) |
| 172 | vec_free (s->config); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 173 | clib_memset (s, 0, sizeof (s[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | always_inline clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 177 | clib_socket_close (clib_socket_t * sock) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 179 | clib_error_t *err; |
| 180 | err = (*sock->close_func) (sock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | return err; |
| 182 | } |
| 183 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 184 | void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
| 186 | #endif /* _clib_included_socket_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * fd.io coding-style-patch-verification: ON |
| 190 | * |
| 191 | * Local Variables: |
| 192 | * eval: (c-set-style "gnu") |
| 193 | * End: |
| 194 | */ |