/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include /* strchr */ #define __USE_GNU #include #include #include #include #include #include #include #include #include #include #include #include #include #include void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...) { va_list va; va_start (va, fmt); clib_socket_tx_add_va_formatted (s, fmt, &va); va_end (va); } /* Return and bind to an unused port. */ static word find_free_port (word sock) { word port; for (port = IPPORT_USERRESERVED; port < 1 << 16; port++) { struct sockaddr_in a; clib_memset (&a, 0, sizeof (a)); /* Warnings be gone */ a.sin_family = PF_INET; a.sin_addr.s_addr = INADDR_ANY; a.sin_port = htons (port); if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0) break; } return port < 1 << 16 ? port : -1; } /* Convert a config string to a struct sockaddr and length for use with bind or connect. */ static clib_error_t * socket_config (char *config, void *addr, socklen_t * addr_len, u32 ip4_default_address) { clib_error_t *error = 0; if (!config) config = ""; /* Anything that begins with a / is a local PF_LOCAL socket. */ if (config[0] == '/') { struct sockaddr_un *su = addr; su->sun_family = PF_LOCAL; clib_memcpy (&su->sun_path, config, clib_min (sizeof (su->sun_path), 1 + strlen (config))); *addr_len = sizeof (su[0]); } /* Hostname or hostname:port or port. */ else { char *host_name; int port = -1; struct sockaddr_in *sa = addr; host_name = 0; port = -1; if (config[0] != 0) { unformat_input_t i; unformat_init_string (&i, config, strlen (config)); if (unformat (&i, "%s:%d", &host_name, &port) || unformat (&i, "%s:0x%x", &host_name, &port)) ; else if (unformat (&i, "%s", &host_name)) ; else error = clib_error_return (0, "unknown input `%U'", format_unformat_error, &i); unformat_free (&i); if (error) goto done; } sa->sin_family = PF_INET; *addr_len = sizeof (sa[0]); if (port != -1) sa->sin_port = htons (port); else sa->sin_port = 0; if (host_name) { struct in_addr host_addr; /* Recognize localhost to avoid host lookup in most common cast. */ if (!strcmp (host_name, "localhost")) sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK); else if (inet_aton (host_name, &host_addr)) sa->sin_addr = host_addr; else if (host_name && strlen (host_name) > 0) { struct hostent *host = gethostbyname (host_name); if (!host) error = clib_error_return (0, "unknown host `%s'", config); else clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0], host->h_length); } else sa->sin_addr.s_addr = htonl (ip4_default_address); vec_free (host_name); if (error) goto done; } } done: return error; } static clib_error_t * default_socket_write (clib_socket_t * s) { clib_error_t *err = 0; word written = 0; word fd = 0; word tx_len; fd = s->fd; /* Map standard input to standard output. Typically, fd is a socket for which read/wr