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, 2005 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 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <string.h> /* strchr */ |
| 40 | #define __USE_GNU |
Nathan Moos | 323418d | 2021-01-15 16:45:14 -0800 | [diff] [blame] | 41 | #define _GNU_SOURCE |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/socket.h> |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 44 | #include <sys/un.h> |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 45 | #include <sys/stat.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | #include <netinet/in.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <netdb.h> |
| 49 | #include <unistd.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #include <fcntl.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
| 52 | #include <vppinfra/mem.h> |
| 53 | #include <vppinfra/vec.h> |
| 54 | #include <vppinfra/socket.h> |
| 55 | #include <vppinfra/format.h> |
| 56 | #include <vppinfra/error.h> |
| 57 | |
Nathan Moos | 323418d | 2021-01-15 16:45:14 -0800 | [diff] [blame] | 58 | #ifndef __GLIBC__ |
| 59 | /* IPPORT_USERRESERVED is not part of musl libc. */ |
| 60 | #define IPPORT_USERRESERVED 5000 |
| 61 | #endif |
| 62 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 63 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
| 66 | va_list va; |
| 67 | va_start (va, fmt); |
| 68 | clib_socket_tx_add_va_formatted (s, fmt, &va); |
| 69 | va_end (va); |
| 70 | } |
| 71 | |
| 72 | /* Return and bind to an unused port. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | static word |
| 74 | find_free_port (word sock) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
| 76 | word port; |
| 77 | |
| 78 | for (port = IPPORT_USERRESERVED; port < 1 << 16; port++) |
| 79 | { |
| 80 | struct sockaddr_in a; |
| 81 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 82 | clib_memset (&a, 0, sizeof (a)); /* Warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
| 84 | a.sin_family = PF_INET; |
| 85 | a.sin_addr.s_addr = INADDR_ANY; |
| 86 | a.sin_port = htons (port); |
| 87 | |
| 88 | if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0) |
| 89 | break; |
| 90 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | return port < 1 << 16 ? port : -1; |
| 93 | } |
| 94 | |
| 95 | /* Convert a config string to a struct sockaddr and length for use |
| 96 | with bind or connect. */ |
| 97 | static clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | socket_config (char *config, |
| 99 | void *addr, socklen_t * addr_len, u32 ip4_default_address) |
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 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 103 | if (!config) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | config = ""; |
| 105 | |
| 106 | /* Anything that begins with a / is a local PF_LOCAL socket. */ |
| 107 | if (config[0] == '/') |
| 108 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | struct sockaddr_un *su = addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | su->sun_family = PF_LOCAL; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 111 | clib_memcpy (&su->sun_path, config, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 112 | clib_min (sizeof (su->sun_path), 1 + strlen (config))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | *addr_len = sizeof (su[0]); |
| 114 | } |
| 115 | |
| 116 | /* Hostname or hostname:port or port. */ |
| 117 | else |
| 118 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | char *host_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | int port = -1; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 121 | struct sockaddr_in *sa = addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
| 123 | host_name = 0; |
| 124 | port = -1; |
| 125 | if (config[0] != 0) |
| 126 | { |
| 127 | unformat_input_t i; |
| 128 | |
| 129 | unformat_init_string (&i, config, strlen (config)); |
| 130 | if (unformat (&i, "%s:%d", &host_name, &port) |
| 131 | || unformat (&i, "%s:0x%x", &host_name, &port)) |
| 132 | ; |
| 133 | else if (unformat (&i, "%s", &host_name)) |
| 134 | ; |
| 135 | else |
| 136 | error = clib_error_return (0, "unknown input `%U'", |
| 137 | format_unformat_error, &i); |
| 138 | unformat_free (&i); |
| 139 | |
| 140 | if (error) |
| 141 | goto done; |
| 142 | } |
| 143 | |
| 144 | sa->sin_family = PF_INET; |
| 145 | *addr_len = sizeof (sa[0]); |
| 146 | if (port != -1) |
| 147 | sa->sin_port = htons (port); |
| 148 | else |
| 149 | sa->sin_port = 0; |
| 150 | |
| 151 | if (host_name) |
| 152 | { |
| 153 | struct in_addr host_addr; |
| 154 | |
| 155 | /* Recognize localhost to avoid host lookup in most common cast. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 156 | if (!strcmp (host_name, "localhost")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 158 | |
| 159 | else if (inet_aton (host_name, &host_addr)) |
| 160 | sa->sin_addr = host_addr; |
| 161 | |
| 162 | else if (host_name && strlen (host_name) > 0) |
| 163 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | struct hostent *host = gethostbyname (host_name); |
| 165 | if (!host) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | error = clib_error_return (0, "unknown host `%s'", config); |
| 167 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 168 | clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0], |
| 169 | host->h_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | else |
| 173 | sa->sin_addr.s_addr = htonl (ip4_default_address); |
| 174 | |
| 175 | vec_free (host_name); |
| 176 | if (error) |
| 177 | goto done; |
| 178 | } |
| 179 | } |
| 180 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 181 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | return error; |
| 183 | } |
| 184 | |
| 185 | static clib_error_t * |
| 186 | default_socket_write (clib_socket_t * s) |
| 187 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 188 | clib_error_t *err = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | word written = 0; |
| 190 | word fd = 0; |
| 191 | word tx_len; |
| 192 | |
| 193 | fd = s->fd; |
| 194 | |
| 195 | /* Map standard input to standard output. |
| 196 | Typically, fd is a socket for which read/write both work. */ |
| 197 | if (fd == 0) |
| 198 | fd = 1; |
| 199 | |
| 200 | tx_len = vec_len (s->tx_buffer); |
| 201 | written = write (fd, s->tx_buffer, tx_len); |
| 202 | |
| 203 | /* Ignore certain errors. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 204 | if (written < 0 && !unix_error_is_fatal (errno)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | written = 0; |
| 206 | |
| 207 | /* A "real" error occurred. */ |
| 208 | if (written < 0) |
| 209 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 210 | err = clib_error_return_unix (0, "write %wd bytes (fd %d, '%s')", |
| 211 | tx_len, s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | vec_free (s->tx_buffer); |
| 213 | goto done; |
| 214 | } |
| 215 | |
| 216 | /* Reclaim the transmitted part of the tx buffer on successful writes. */ |
| 217 | else if (written > 0) |
| 218 | { |
| 219 | if (written == tx_len) |
| 220 | _vec_len (s->tx_buffer) = 0; |
| 221 | else |
| 222 | vec_delete (s->tx_buffer, written, 0); |
| 223 | } |
| 224 | |
| 225 | /* If a non-fatal error occurred AND |
| 226 | the buffer is full, then we must free it. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 227 | else if (written == 0 && tx_len > 64 * 1024) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | { |
| 229 | vec_free (s->tx_buffer); |
| 230 | } |
| 231 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 232 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | return err; |
| 234 | } |
| 235 | |
| 236 | static clib_error_t * |
| 237 | default_socket_read (clib_socket_t * sock, int n_bytes) |
| 238 | { |
| 239 | word fd, n_read; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | u8 *buf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
| 242 | /* RX side of socket is down once end of file is reached. */ |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 243 | if (sock->flags & CLIB_SOCKET_F_RX_END_OF_FILE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | |
| 246 | fd = sock->fd; |
| 247 | |
| 248 | n_bytes = clib_max (n_bytes, 4096); |
| 249 | vec_add2 (sock->rx_buffer, buf, n_bytes); |
| 250 | |
| 251 | if ((n_read = read (fd, buf, n_bytes)) < 0) |
| 252 | { |
| 253 | n_read = 0; |
| 254 | |
| 255 | /* Ignore certain errors. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 256 | if (!unix_error_is_fatal (errno)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | goto non_fatal; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 258 | |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 259 | return clib_error_return_unix (0, "read %d bytes (fd %d, '%s')", |
| 260 | n_bytes, sock->fd, sock->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 262 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | /* Other side closed the socket. */ |
| 264 | if (n_read == 0) |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 265 | sock->flags |= CLIB_SOCKET_F_RX_END_OF_FILE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 267 | non_fatal: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | _vec_len (sock->rx_buffer) += n_read - n_bytes; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 273 | static clib_error_t * |
| 274 | default_socket_close (clib_socket_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | { |
| 276 | if (close (s->fd) < 0) |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 277 | return clib_error_return_unix (0, "close (fd %d, %s)", s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 281 | static clib_error_t * |
| 282 | default_socket_sendmsg (clib_socket_t * s, void *msg, int msglen, |
| 283 | int fds[], int num_fds) |
| 284 | { |
| 285 | struct msghdr mh = { 0 }; |
| 286 | struct iovec iov[1]; |
Damjan Marion | 0215fb4 | 2018-10-30 13:04:19 +0100 | [diff] [blame] | 287 | char ctl[CMSG_SPACE (sizeof (int) * num_fds)]; |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 288 | int rv; |
| 289 | |
| 290 | iov[0].iov_base = msg; |
| 291 | iov[0].iov_len = msglen; |
| 292 | mh.msg_iov = iov; |
| 293 | mh.msg_iovlen = 1; |
| 294 | |
| 295 | if (num_fds > 0) |
| 296 | { |
| 297 | struct cmsghdr *cmsg; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 298 | clib_memset (&ctl, 0, sizeof (ctl)); |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 299 | mh.msg_control = ctl; |
| 300 | mh.msg_controllen = sizeof (ctl); |
| 301 | cmsg = CMSG_FIRSTHDR (&mh); |
| 302 | cmsg->cmsg_len = CMSG_LEN (sizeof (int) * num_fds); |
| 303 | cmsg->cmsg_level = SOL_SOCKET; |
| 304 | cmsg->cmsg_type = SCM_RIGHTS; |
| 305 | memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * num_fds); |
| 306 | } |
| 307 | rv = sendmsg (s->fd, &mh, 0); |
| 308 | if (rv < 0) |
| 309 | return clib_error_return_unix (0, "sendmsg"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static clib_error_t * |
| 315 | default_socket_recvmsg (clib_socket_t * s, void *msg, int msglen, |
| 316 | int fds[], int num_fds) |
| 317 | { |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 318 | #ifdef __linux__ |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 319 | char ctl[CMSG_SPACE (sizeof (int) * num_fds) + |
| 320 | CMSG_SPACE (sizeof (struct ucred))]; |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 321 | struct ucred *cr = 0; |
| 322 | #else |
| 323 | char ctl[CMSG_SPACE (sizeof (int) * num_fds)]; |
| 324 | #endif |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 325 | struct msghdr mh = { 0 }; |
| 326 | struct iovec iov[1]; |
| 327 | ssize_t size; |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 328 | struct cmsghdr *cmsg; |
| 329 | |
| 330 | iov[0].iov_base = msg; |
| 331 | iov[0].iov_len = msglen; |
| 332 | mh.msg_iov = iov; |
| 333 | mh.msg_iovlen = 1; |
| 334 | mh.msg_control = ctl; |
| 335 | mh.msg_controllen = sizeof (ctl); |
| 336 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 337 | clib_memset (ctl, 0, sizeof (ctl)); |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 338 | |
| 339 | /* receive the incoming message */ |
| 340 | size = recvmsg (s->fd, &mh, 0); |
| 341 | if (size != msglen) |
| 342 | { |
| 343 | return (size == 0) ? clib_error_return (0, "disconnected") : |
| 344 | clib_error_return_unix (0, "recvmsg: malformed message (fd %d, '%s')", |
| 345 | s->fd, s->config); |
| 346 | } |
| 347 | |
| 348 | cmsg = CMSG_FIRSTHDR (&mh); |
| 349 | while (cmsg) |
| 350 | { |
| 351 | if (cmsg->cmsg_level == SOL_SOCKET) |
| 352 | { |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 353 | #ifdef __linux__ |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 354 | if (cmsg->cmsg_type == SCM_CREDENTIALS) |
| 355 | { |
| 356 | cr = (struct ucred *) CMSG_DATA (cmsg); |
| 357 | s->uid = cr->uid; |
| 358 | s->gid = cr->gid; |
| 359 | s->pid = cr->pid; |
| 360 | } |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 361 | else |
| 362 | #endif |
| 363 | if (cmsg->cmsg_type == SCM_RIGHTS) |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 364 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 365 | clib_memcpy_fast (fds, CMSG_DATA (cmsg), |
| 366 | num_fds * sizeof (int)); |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | cmsg = CMSG_NXTHDR (&mh, cmsg); |
| 370 | } |
| 371 | return 0; |
| 372 | } |
| 373 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 374 | static void |
| 375 | socket_init_funcs (clib_socket_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 377 | if (!s->write_func) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | s->write_func = default_socket_write; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 379 | if (!s->read_func) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | s->read_func = default_socket_read; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 381 | if (!s->close_func) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | s->close_func = default_socket_close; |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 383 | if (!s->sendmsg_func) |
| 384 | s->sendmsg_func = default_socket_sendmsg; |
| 385 | if (!s->recvmsg_func) |
| 386 | s->recvmsg_func = default_socket_recvmsg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 389 | __clib_export clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | clib_socket_init (clib_socket_t * s) |
| 391 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 392 | union |
| 393 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | struct sockaddr sa; |
| 395 | struct sockaddr_un su; |
| 396 | } addr; |
| 397 | socklen_t addr_len = 0; |
Florin Coras | 42ddf69 | 2020-01-06 20:23:07 +0000 | [diff] [blame] | 398 | int socket_type, rv; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 399 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | word port; |
| 401 | |
| 402 | error = socket_config (s->config, &addr.sa, &addr_len, |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 403 | (s->flags & CLIB_SOCKET_F_IS_SERVER |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 404 | ? INADDR_LOOPBACK : INADDR_ANY)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | if (error) |
| 406 | goto done; |
| 407 | |
| 408 | socket_init_funcs (s); |
| 409 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 410 | socket_type = s->flags & CLIB_SOCKET_F_SEQPACKET ? |
| 411 | SOCK_SEQPACKET : SOCK_STREAM; |
| 412 | |
| 413 | s->fd = socket (addr.sa.sa_family, socket_type, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | if (s->fd < 0) |
| 415 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 416 | error = clib_error_return_unix (0, "socket (fd %d, '%s')", |
| 417 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | goto done; |
| 419 | } |
| 420 | |
| 421 | port = 0; |
| 422 | if (addr.sa.sa_family == PF_INET) |
| 423 | port = ((struct sockaddr_in *) &addr)->sin_port; |
| 424 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 425 | if (s->flags & CLIB_SOCKET_F_IS_SERVER) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | { |
| 427 | uword need_bind = 1; |
| 428 | |
| 429 | if (addr.sa.sa_family == PF_INET) |
| 430 | { |
| 431 | if (port == 0) |
| 432 | { |
| 433 | port = find_free_port (s->fd); |
| 434 | if (port < 0) |
| 435 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 436 | error = clib_error_return (0, "no free port (fd %d, '%s')", |
| 437 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | goto done; |
| 439 | } |
| 440 | need_bind = 0; |
| 441 | } |
| 442 | } |
| 443 | if (addr.sa.sa_family == PF_LOCAL) |
| 444 | unlink (((struct sockaddr_un *) &addr)->sun_path); |
| 445 | |
| 446 | /* Make address available for multiple users. */ |
| 447 | { |
| 448 | int v = 1; |
| 449 | if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0) |
| 450 | clib_unix_warning ("setsockopt SO_REUSEADDR fails"); |
| 451 | } |
| 452 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 453 | #if __linux__ |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 454 | if (addr.sa.sa_family == PF_LOCAL && s->flags & CLIB_SOCKET_F_PASSCRED) |
| 455 | { |
| 456 | int x = 1; |
| 457 | if (setsockopt (s->fd, SOL_SOCKET, SO_PASSCRED, &x, sizeof (x)) < 0) |
| 458 | { |
| 459 | error = clib_error_return_unix (0, "setsockopt (SO_PASSCRED, " |
| 460 | "fd %d, '%s')", s->fd, |
| 461 | s->config); |
| 462 | goto done; |
| 463 | } |
| 464 | } |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 465 | #endif |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 466 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 467 | if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 469 | error = clib_error_return_unix (0, "bind (fd %d, '%s')", |
| 470 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | goto done; |
| 472 | } |
| 473 | |
| 474 | if (listen (s->fd, 5) < 0) |
| 475 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 476 | error = clib_error_return_unix (0, "listen (fd %d, '%s')", |
| 477 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | goto done; |
| 479 | } |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 480 | if (addr.sa.sa_family == PF_LOCAL |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 481 | && s->flags & CLIB_SOCKET_F_ALLOW_GROUP_WRITE) |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 482 | { |
| 483 | struct stat st = { 0 }; |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 484 | if (stat (((struct sockaddr_un *) &addr)->sun_path, &st) < 0) |
| 485 | { |
| 486 | error = clib_error_return_unix (0, "stat (fd %d, '%s')", |
| 487 | s->fd, s->config); |
| 488 | goto done; |
| 489 | } |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 490 | st.st_mode |= S_IWGRP; |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 491 | if (chmod (((struct sockaddr_un *) &addr)->sun_path, st.st_mode) < |
| 492 | 0) |
| 493 | { |
| 494 | error = |
| 495 | clib_error_return_unix (0, "chmod (fd %d, '%s', mode %o)", |
| 496 | s->fd, s->config, st.st_mode); |
| 497 | goto done; |
| 498 | } |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 499 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | } |
| 501 | else |
| 502 | { |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 503 | if ((s->flags & CLIB_SOCKET_F_NON_BLOCKING_CONNECT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0) |
| 505 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 506 | error = clib_error_return_unix (0, "fcntl NONBLOCK (fd %d, '%s')", |
| 507 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | goto done; |
| 509 | } |
| 510 | |
Florin Coras | 42ddf69 | 2020-01-06 20:23:07 +0000 | [diff] [blame] | 511 | while ((rv = connect (s->fd, &addr.sa, addr_len)) < 0 |
| 512 | && errno == EAGAIN) |
| 513 | ; |
| 514 | if (rv < 0 && !((s->flags & CLIB_SOCKET_F_NON_BLOCKING_CONNECT) && |
| 515 | errno == EINPROGRESS)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 517 | error = clib_error_return_unix (0, "connect (fd %d, '%s')", |
| 518 | s->fd, s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | goto done; |
| 520 | } |
Florin Coras | 5fe9457 | 2021-05-24 08:58:15 -0700 | [diff] [blame^] | 521 | /* Connect was blocking so set fd to non-blocking now */ |
| 522 | if (!(s->flags & CLIB_SOCKET_F_NON_BLOCKING_CONNECT) && |
| 523 | fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0) |
| 524 | { |
| 525 | error = clib_error_return_unix (0, "fcntl NONBLOCK2 (fd %d, '%s')", |
| 526 | s->fd, s->config); |
| 527 | goto done; |
| 528 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return error; |
| 532 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 533 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | if (s->fd > 0) |
| 535 | close (s->fd); |
| 536 | return error; |
| 537 | } |
| 538 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 539 | __clib_export clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 540 | clib_socket_accept (clib_socket_t * server, clib_socket_t * client) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 542 | clib_error_t *err = 0; |
| 543 | socklen_t len = 0; |
| 544 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 545 | clib_memset (client, 0, sizeof (client[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
| 547 | /* Accept the new socket connection. */ |
| 548 | client->fd = accept (server->fd, 0, 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 549 | if (client->fd < 0) |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 550 | return clib_error_return_unix (0, "accept (fd %d, '%s')", |
| 551 | server->fd, server->config); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 552 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | /* Set the new socket to be non-blocking. */ |
| 554 | if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0) |
| 555 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 556 | err = clib_error_return_unix (0, "fcntl O_NONBLOCK (fd %d)", |
| 557 | client->fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | goto close_client; |
| 559 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 560 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | /* Get peer info. */ |
| 562 | len = sizeof (client->peer); |
| 563 | if (getpeername (client->fd, (struct sockaddr *) &client->peer, &len) < 0) |
| 564 | { |
Dave Wallace | 70ec09d | 2017-09-06 16:45:04 -0400 | [diff] [blame] | 565 | err = clib_error_return_unix (0, "getpeername (fd %d)", client->fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | goto close_client; |
| 567 | } |
| 568 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 569 | client->flags = CLIB_SOCKET_F_IS_CLIENT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | |
| 571 | socket_init_funcs (client); |
| 572 | return 0; |
| 573 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 574 | close_client: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | close (client->fd); |
| 576 | return err; |
| 577 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * fd.io coding-style-patch-verification: ON |
| 581 | * |
| 582 | * Local Variables: |
| 583 | * eval: (c-set-style "gnu") |
| 584 | * End: |
| 585 | */ |