Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * sockclnt_vlib.c |
| 4 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 20 | #include <sys/types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
| 22 | #include <unistd.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <vppinfra/byte_order.h> |
| 26 | #include <netdb.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #include <vlibmemory/api.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 33 | |
| 34 | #include <vlibmemory/vl_memory_msg_enum.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 36 | #define vl_typedefs /* define message structures */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 37 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | #undef vl_typedefs |
| 39 | |
| 40 | /* instantiate all the print functions we know about */ |
| 41 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 42 | #define vl_printfun |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 43 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | #undef vl_printfun |
| 45 | |
| 46 | /* instantiate all the endian swap functions we know about */ |
| 47 | #define vl_endianfun |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 48 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | #undef vl_endianfun |
| 50 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 51 | static void |
| 52 | vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 54 | vl_api_registration_t *rp = socket_main.current_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 56 | rp->server_handle = mp->handle; |
| 57 | rp->server_index = mp->index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 60 | static void |
| 61 | vl_api_sockclnt_delete_reply_t_handler (vl_api_sockclnt_delete_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 63 | clib_file_main_t *fm = &file_main; |
| 64 | clib_file_t *uf = socket_main.current_uf; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 65 | vl_api_registration_t *rp = socket_main.current_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 67 | clib_file_del (fm, uf); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 68 | vl_socket_free_registration_index (rp->vl_api_registration_pool_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 71 | u32 |
| 72 | sockclnt_open_index (char *client_name, char *hostname, int port) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 74 | vl_api_registration_t *rp; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 75 | clib_file_main_t *fm = &file_main; |
| 76 | clib_file_t template = { 0 }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 77 | int sockfd; |
| 78 | int one = 1; |
| 79 | int rv; |
| 80 | struct sockaddr_in serv_addr; |
| 81 | struct hostent *server; |
| 82 | vl_api_sockclnt_create_t *mp; |
| 83 | char my_hostname[64]; |
| 84 | |
| 85 | server = gethostbyname (hostname); |
| 86 | if (server == NULL) |
| 87 | { |
| 88 | clib_warning ("Couldn't translate server name %s", hostname); |
| 89 | return ~0; |
| 90 | } |
| 91 | |
| 92 | /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ |
| 93 | sockfd = socket (AF_INET, SOCK_STREAM, 0); |
| 94 | |
| 95 | if (sockfd < 0) |
| 96 | { |
| 97 | clib_unix_warning ("socket"); |
| 98 | return ~0; |
| 99 | } |
| 100 | |
| 101 | bzero ((char *) &serv_addr, sizeof (serv_addr)); |
| 102 | serv_addr.sin_family = AF_INET; |
| 103 | bcopy ((char *) server->h_addr, |
| 104 | (char *) &serv_addr.sin_addr.s_addr, server->h_length); |
| 105 | serv_addr.sin_port = htons (port); |
| 106 | |
| 107 | if (connect (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0) |
| 108 | { |
| 109 | clib_unix_warning ("Connect failure to (%s, %d)", hostname, port); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 110 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 111 | return ~0; |
| 112 | } |
| 113 | |
| 114 | rv = ioctl (sockfd, FIONBIO, &one); |
| 115 | if (rv < 0) |
| 116 | { |
| 117 | clib_unix_warning ("FIONBIO"); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 118 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 119 | return ~0; |
| 120 | } |
| 121 | |
| 122 | pool_get (socket_main.registration_pool, rp); |
| 123 | memset (rp, 0, sizeof (*rp)); |
| 124 | rp->registration_type = REGISTRATION_TYPE_SOCKET_CLIENT; |
| 125 | rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; |
| 126 | |
| 127 | template.read_function = vl_socket_read_ready; |
| 128 | template.write_function = vl_socket_write_ready; |
| 129 | template.file_descriptor = sockfd; |
| 130 | template.private_data = rp - socket_main.registration_pool; |
| 131 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 132 | rp->clib_file_index = clib_file_add (fm, &template); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 133 | rp->name = format (0, "%s:%d", hostname, port); |
| 134 | |
| 135 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 136 | mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_CREATE); |
| 137 | mp->context = rp - socket_main.registration_pool; |
| 138 | |
| 139 | if (gethostname (my_hostname, sizeof (my_hostname)) < 0) |
| 140 | { |
| 141 | clib_unix_warning ("gethostname"); |
| 142 | strncpy (my_hostname, "unknown!", sizeof (my_hostname) - 1); |
| 143 | } |
| 144 | strncpy ((char *) mp->name, my_hostname, sizeof (mp->name) - 1); |
| 145 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 146 | vl_api_send_msg (rp, (u8 *) mp); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 147 | return rp - socket_main.registration_pool; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | sockclnt_close_index (u32 index) |
| 152 | { |
| 153 | vl_api_sockclnt_delete_t *mp; |
| 154 | vl_api_registration_t *rp; |
| 155 | |
| 156 | /* Don't crash / assert if fed garbage */ |
| 157 | if (pool_is_free_index (socket_main.registration_pool, index)) |
| 158 | { |
| 159 | clib_warning ("registration_pool index %d already free", index); |
| 160 | return; |
| 161 | } |
| 162 | rp = pool_elt_at_index (socket_main.registration_pool, index); |
| 163 | |
| 164 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 165 | mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_DELETE); |
| 166 | mp->handle = rp->server_handle; |
| 167 | mp->index = rp->server_index; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 168 | vl_api_send_msg (rp, (u8 *) mp); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | vl_api_registration_t * |
| 172 | sockclnt_get_registration (u32 index) |
| 173 | { |
| 174 | return pool_elt_at_index (socket_main.registration_pool, index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Both rx and tx msgs MUST be initialized, or we'll have |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 179 | * precisely no idea how many bytes to write into the API trace... |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | */ |
| 181 | #define foreach_sockclnt_api_msg \ |
| 182 | _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 183 | _(SOCKCLNT_DELETE_REPLY, sockclnt_delete_reply) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
| 185 | |
| 186 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 187 | sockclnt_vlib_api_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
| 189 | #define _(N,n) \ |
| 190 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 191 | vl_api_##n##_t_handler, \ |
| 192 | vl_noop_handler, \ |
| 193 | vl_api_##n##_t_endian, \ |
| 194 | vl_api_##n##_t_print, \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 195 | sizeof(vl_api_##n##_t), 1); |
| 196 | foreach_sockclnt_api_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | #undef _ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 198 | return 0; |
| 199 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 201 | VLIB_API_INIT_FUNCTION (sockclnt_vlib_api_init); |
| 202 | |
| 203 | /* |
| 204 | * fd.io coding-style-patch-verification: ON |
| 205 | * |
| 206 | * Local Variables: |
| 207 | * eval: (c-set-style "gnu") |
| 208 | * End: |
| 209 | */ |