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 | * socksvr_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 <netinet/in.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <vppinfra/byte_order.h> |
| 25 | |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include <vlibsocket/api.h> |
| 30 | #include <vlibmemory/api.h> |
| 31 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 32 | #include <vlibsocket/vl_socket_msg_enum.h> /* enumerate all vlib messages */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 34 | #define vl_typedefs /* define message structures */ |
| 35 | #include <vlibsocket/vl_socket_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | #undef vl_typedefs |
| 37 | |
| 38 | /* instantiate all the print functions we know about */ |
| 39 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 40 | #define vl_printfun |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 41 | #include <vlibsocket/vl_socket_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #undef vl_printfun |
| 43 | |
| 44 | /* instantiate all the endian swap functions we know about */ |
| 45 | #define vl_endianfun |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 46 | #include <vlibsocket/vl_socket_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | #undef vl_endianfun |
| 48 | |
| 49 | socket_main_t socket_main; |
| 50 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 51 | void |
| 52 | dump_socket_clients (vlib_main_t * vm, api_main_t * am) |
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 *reg; |
| 55 | socket_main_t *sm = &socket_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 56 | clib_file_main_t *fm = &file_main; |
| 57 | clib_file_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 59 | /* |
| 60 | * Must have at least one active client, not counting the |
| 61 | * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket |
| 62 | */ |
| 63 | if (pool_elts (sm->registration_pool) < 2) |
| 64 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 66 | vlib_cli_output (vm, "TCP socket clients"); |
| 67 | vlib_cli_output (vm, "%16s %8s", "Name", "Fildesc"); |
| 68 | /* *INDENT-OFF* */ |
| 69 | pool_foreach (reg, sm->registration_pool, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | ({ |
| 71 | if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 72 | f = pool_elt_at_index (fm->file_pool, reg->clib_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | vlib_cli_output (vm, "%16s %8d", |
| 74 | reg->name, f->file_descriptor); |
| 75 | } |
| 76 | })); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 77 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 80 | void |
| 81 | vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 83 | u32 nbytes = 4; /* for the length... */ |
| 84 | u16 msg_id = ntohs (*(u16 *) elem); |
| 85 | u32 msg_length; |
| 86 | u32 tmp; |
| 87 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 89 | ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 91 | if (msg_id >= vec_len (am->api_trace_cfg)) |
| 92 | { |
| 93 | clib_warning ("id out of range: %d", msg_id); |
| 94 | vl_msg_api_free ((void *) elem); |
| 95 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 98 | msg_length = am->api_trace_cfg[msg_id].size; |
| 99 | nbytes += msg_length; |
| 100 | tmp = clib_host_to_net_u32 (nbytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 102 | vl_socket_add_pending_output (rp->clib_file_index |
| 103 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 104 | rp->vl_api_registration_pool_index |
| 105 | + socket_main.registration_pool, |
| 106 | (u8 *) & tmp, sizeof (tmp)); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 107 | vl_socket_add_pending_output (rp->clib_file_index |
| 108 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 109 | rp->vl_api_registration_pool_index |
| 110 | + socket_main.registration_pool, |
| 111 | elem, msg_length); |
| 112 | vl_msg_api_free ((void *) elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 115 | void |
| 116 | vl_socket_api_send_with_data (vl_api_registration_t * rp, |
| 117 | u8 * elem, u8 * data_vector) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 119 | u32 nbytes = 4; /* for the length... */ |
| 120 | u16 msg_id = ntohs (*(u16 *) elem); |
| 121 | u32 msg_length; |
| 122 | u32 tmp; |
| 123 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 125 | ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); |
| 126 | |
| 127 | if (msg_id >= vec_len (am->api_trace_cfg)) |
| 128 | { |
| 129 | clib_warning ("id out of range: %d", msg_id); |
| 130 | vec_free (data_vector); |
| 131 | vl_msg_api_free ((void *) elem); |
| 132 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 135 | msg_length = am->api_trace_cfg[msg_id].size; |
| 136 | nbytes += msg_length; |
| 137 | nbytes += vec_len (data_vector); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 139 | /* Length in network byte order */ |
| 140 | tmp = clib_host_to_net_u32 (nbytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 142 | vl_socket_add_pending_output (rp->clib_file_index |
| 143 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 144 | rp->vl_api_registration_pool_index |
| 145 | + socket_main.registration_pool, |
| 146 | (u8 *) & tmp, sizeof (tmp)); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 147 | vl_socket_add_pending_output (rp->clib_file_index |
| 148 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 149 | rp->vl_api_registration_pool_index |
| 150 | + socket_main.registration_pool, |
| 151 | elem, msg_length); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 152 | vl_socket_add_pending_output (rp->clib_file_index |
| 153 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 154 | rp->vl_api_registration_pool_index |
| 155 | + socket_main.registration_pool, |
| 156 | data_vector, vec_len (data_vector)); |
| 157 | vl_msg_api_free ((void *) elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static inline void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 161 | vl_socket_api_send_with_length_internal (vl_api_registration_t * rp, |
| 162 | u8 * elem, u32 msg_length, int free) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 164 | u32 nbytes = 4; /* for the length... */ |
| 165 | u16 msg_id = ntohs (*(u16 *) elem); |
| 166 | u32 tmp; |
| 167 | api_main_t *am = &api_main; |
| 168 | |
| 169 | ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); |
| 170 | |
| 171 | if (msg_id >= vec_len (am->api_trace_cfg)) |
| 172 | { |
| 173 | clib_warning ("id out of range: %d", msg_id); |
| 174 | if (free) |
| 175 | vl_msg_api_free ((void *) elem); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | nbytes += msg_length; |
| 180 | |
| 181 | /* Length in network byte order */ |
| 182 | tmp = clib_host_to_net_u32 (nbytes); |
| 183 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 184 | vl_socket_add_pending_output (rp->clib_file_index |
| 185 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 186 | rp->vl_api_registration_pool_index |
| 187 | + socket_main.registration_pool, |
| 188 | (u8 *) & tmp, sizeof (tmp)); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 189 | vl_socket_add_pending_output (rp->clib_file_index |
| 190 | + file_main.file_pool, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 191 | rp->vl_api_registration_pool_index |
| 192 | + socket_main.registration_pool, |
| 193 | elem, msg_length); |
| 194 | if (free) |
| 195 | vl_msg_api_free ((void *) elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 198 | void |
| 199 | vl_socket_api_send_with_length (vl_api_registration_t * rp, |
| 200 | u8 * elem, u32 msg_length) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 202 | vl_socket_api_send_with_length_internal (rp, elem, msg_length, |
| 203 | 1 /* free */ ); |
| 204 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 206 | void |
| 207 | vl_socket_api_send_with_length_no_free (vl_api_registration_t * rp, |
| 208 | u8 * elem, u32 msg_length) |
| 209 | { |
| 210 | vl_socket_api_send_with_length_internal (rp, elem, msg_length, |
| 211 | 0 /* free */ ); |
| 212 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 214 | void |
| 215 | vl_free_socket_registration_index (u32 pool_index) |
| 216 | { |
| 217 | vl_api_registration_t *rp; |
| 218 | if (pool_is_free_index (socket_main.registration_pool, pool_index)) |
| 219 | { |
| 220 | clib_warning ("main pool index %d already free", pool_index); |
| 221 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 223 | rp = pool_elt_at_index (socket_main.registration_pool, pool_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 225 | ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE); |
| 226 | vec_free (rp->name); |
| 227 | vec_free (rp->unprocessed_input); |
| 228 | vec_free (rp->output_vector); |
| 229 | rp->registration_type = REGISTRATION_TYPE_FREE; |
| 230 | pool_put (socket_main.registration_pool, rp); |
| 231 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 233 | static inline void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 234 | socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 235 | i8 * input_v) |
| 236 | { |
| 237 | u8 *the_msg = (u8 *) (input_v + sizeof (u32)); |
| 238 | socket_main.current_uf = uf; |
| 239 | socket_main.current_rp = rp; |
| 240 | vl_msg_api_socket_handler (the_msg); |
| 241 | socket_main.current_uf = 0; |
| 242 | socket_main.current_rp = 0; |
| 243 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 245 | clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 246 | vl_socket_read_ready (clib_file_t * uf) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 247 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 248 | clib_file_main_t *fm = &file_main; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 249 | vl_api_registration_t *rp; |
| 250 | int n; |
| 251 | i8 *msg_buffer = 0; |
| 252 | u32 msg_len; |
| 253 | u32 save_input_buffer_length = vec_len (socket_main.input_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 255 | rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 257 | n = read (uf->file_descriptor, socket_main.input_buffer, |
| 258 | vec_len (socket_main.input_buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 260 | if (n <= 0 && errno != EAGAIN) |
| 261 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 262 | clib_file_del (fm, uf); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 263 | |
| 264 | if (!pool_is_free (socket_main.registration_pool, rp)) |
| 265 | { |
| 266 | u32 index = rp - socket_main.registration_pool; |
| 267 | vl_free_socket_registration_index (index); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | clib_warning ("client index %d already free?", |
| 272 | rp->vl_api_registration_pool_index); |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | _vec_len (socket_main.input_buffer) = n; |
| 278 | |
| 279 | /* |
| 280 | * Look for bugs here. This code is tricky because |
| 281 | * data read from a stream socket does honor message |
| 282 | * boundaries. In the case of a long message (>4K bytes) |
| 283 | * we have to do (at least) 2 reads, etc. |
| 284 | */ |
| 285 | do |
| 286 | { |
| 287 | if (vec_len (rp->unprocessed_input)) |
| 288 | { |
| 289 | vec_append (rp->unprocessed_input, socket_main.input_buffer); |
| 290 | msg_buffer = rp->unprocessed_input; |
| 291 | msg_len = rp->unprocessed_msg_length; |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | msg_buffer = socket_main.input_buffer; |
| 296 | msg_len = 0; |
| 297 | } |
| 298 | |
| 299 | if (msg_len == 0) |
| 300 | { |
| 301 | /* Length may be split across two reads */ |
| 302 | if (vec_len (msg_buffer) < sizeof (u32)) |
| 303 | goto save_and_split; |
| 304 | |
| 305 | /* total length, including msg_len itself, in network byte order */ |
| 306 | msg_len = clib_net_to_host_u32 (*((u32 *) msg_buffer)); |
| 307 | } |
| 308 | |
| 309 | /* Happens if the client sent msg_len == 0 */ |
| 310 | if (msg_len == 0) |
| 311 | { |
| 312 | clib_warning ("msg_len == 0"); |
| 313 | goto turf_it; |
| 314 | } |
| 315 | |
| 316 | /* We don't have the entire message yet. */ |
| 317 | if (msg_len > vec_len (msg_buffer)) |
| 318 | { |
| 319 | save_and_split: |
| 320 | /* |
| 321 | * if we were using the shared input buffer, |
| 322 | * save the fragment. |
| 323 | */ |
| 324 | if (msg_buffer == socket_main.input_buffer) |
| 325 | { |
| 326 | ASSERT (vec_len (rp->unprocessed_input) == 0); |
| 327 | vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1); |
| 328 | clib_memcpy (rp->unprocessed_input, msg_buffer, |
| 329 | vec_len (msg_buffer)); |
| 330 | _vec_len (rp->unprocessed_input) = vec_len (msg_buffer); |
| 331 | } |
| 332 | _vec_len (socket_main.input_buffer) = save_input_buffer_length; |
| 333 | rp->unprocessed_msg_length = msg_len; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | socket_process_msg (uf, rp, msg_buffer); |
| 338 | if (n > msg_len) |
| 339 | vec_delete (msg_buffer, msg_len, 0); |
| 340 | else |
| 341 | _vec_len (msg_buffer) = 0; |
| 342 | n -= msg_len; |
| 343 | msg_len = 0; |
| 344 | rp->unprocessed_msg_length = 0; |
| 345 | } |
| 346 | while (n > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | |
| 348 | turf_it: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 349 | _vec_len (socket_main.input_buffer) = save_input_buffer_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 351 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 355 | vl_socket_add_pending_output (clib_file_t * uf, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 356 | vl_api_registration_t * rp, |
| 357 | u8 * buffer, uword buffer_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 359 | clib_file_main_t *fm = &file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 361 | vec_add (rp->output_vector, buffer, buffer_bytes); |
| 362 | if (vec_len (rp->output_vector) > 0) |
| 363 | { |
| 364 | int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); |
| 365 | uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
| 366 | if (!skip_update) |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 367 | fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 372 | socket_del_pending_output (clib_file_t * uf, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 373 | vl_api_registration_t * rp, uword n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 375 | clib_file_main_t *fm = &file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 377 | vec_delete (rp->output_vector, n_bytes, 0); |
| 378 | if (vec_len (rp->output_vector) <= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 380 | int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); |
| 381 | uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
| 382 | if (!skip_update) |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 383 | fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 387 | clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 388 | vl_socket_write_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 390 | clib_file_main_t *fm = &file_main; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 391 | vl_api_registration_t *rp; |
| 392 | int n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 394 | rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 396 | /* Flush output vector. */ |
| 397 | n = write (uf->file_descriptor, |
| 398 | rp->output_vector, vec_len (rp->output_vector)); |
| 399 | |
| 400 | if (n < 0) |
| 401 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | #if DEBUG > 2 |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 403 | clib_warning ("write error, close the file...\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | #endif |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 405 | clib_file_del (fm, uf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 407 | vl_free_socket_registration_index (rp - socket_main.registration_pool); |
| 408 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 411 | else if (n > 0) |
| 412 | socket_del_pending_output (uf, rp, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 414 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 417 | clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 418 | vl_socket_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 419 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 420 | vl_api_registration_t *rp; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 421 | clib_file_main_t *fm = &file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 423 | rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 424 | clib_file_del (fm, uf); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 425 | vl_free_socket_registration_index (rp - socket_main.registration_pool); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 427 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 430 | void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 431 | socksvr_file_add (clib_file_main_t * fm, int fd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 433 | vl_api_registration_t *rp; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 434 | clib_file_t template = { 0 }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 435 | |
| 436 | pool_get (socket_main.registration_pool, rp); |
| 437 | memset (rp, 0, sizeof (*rp)); |
| 438 | |
| 439 | template.read_function = vl_socket_read_ready; |
| 440 | template.write_function = vl_socket_write_ready; |
| 441 | template.error_function = vl_socket_error_ready; |
| 442 | template.file_descriptor = fd; |
| 443 | template.private_data = rp - socket_main.registration_pool; |
| 444 | |
| 445 | rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER; |
| 446 | rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 447 | rp->clib_file_index = clib_file_add (fm, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 450 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 451 | socksvr_accept_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 453 | clib_file_main_t *fm = &file_main; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 454 | struct sockaddr_in client_addr; |
| 455 | int client_fd; |
| 456 | int client_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 458 | client_len = sizeof (client_addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 460 | /* |
| 461 | * Supposedly acquires the non-blocking attrib from the |
| 462 | * server socket. |
| 463 | */ |
| 464 | client_fd = accept (uf->file_descriptor, |
| 465 | (struct sockaddr *) &client_addr, |
| 466 | (socklen_t *) & client_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 468 | if (client_fd < 0) |
| 469 | return clib_error_return_unix (0, "socksvr_accept_ready: accept"); |
| 470 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 471 | socksvr_file_add (fm, client_fd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 472 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 475 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 476 | socksvr_bogus_write (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 478 | clib_warning ("why am I here?"); |
| 479 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | * vl_api_sockclnt_create_t_handler |
| 484 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 485 | void |
| 486 | vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 488 | vl_api_registration_t *regp; |
| 489 | vl_api_sockclnt_create_reply_t *rp; |
| 490 | int rv = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 491 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 492 | regp = socket_main.current_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 494 | ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 496 | regp->name = format (0, "%s%c", mp->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 498 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 499 | rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY); |
| 500 | rp->handle = (uword) regp; |
| 501 | rp->index = (uword) regp->vl_api_registration_pool_index; |
| 502 | rp->context = mp->context; |
| 503 | rp->response = htonl (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 505 | vl_msg_api_send (regp, (u8 *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | * vl_api_sockclnt_delete_t_handler |
| 510 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 511 | void |
| 512 | vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 514 | vl_api_registration_t *regp; |
| 515 | vl_api_sockclnt_delete_reply_t *rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 517 | if (!pool_is_free_index (socket_main.registration_pool, mp->index)) |
| 518 | { |
| 519 | regp = pool_elt_at_index (socket_main.registration_pool, mp->index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 521 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 522 | rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY); |
| 523 | rp->handle = mp->handle; |
| 524 | rp->response = htonl (1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 526 | vl_msg_api_send (regp, (u8 *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 528 | clib_file_del (&file_main, file_main.file_pool + regp->clib_file_index); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 529 | |
| 530 | vl_free_socket_registration_index (mp->index); |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | clib_warning ("unknown client ID %d", mp->index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | #define foreach_vlib_api_msg \ |
| 539 | _(SOCKCLNT_CREATE, sockclnt_create) \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 540 | _(SOCKCLNT_DELETE, sockclnt_delete) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 542 | static clib_error_t * |
| 543 | socksvr_api_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 545 | clib_file_main_t *fm = &file_main; |
| 546 | clib_file_t template = { 0 }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 547 | int sockfd; |
| 548 | int one = 1; |
| 549 | int rv; |
| 550 | struct sockaddr_in serv_addr; |
| 551 | vl_api_registration_t *rp; |
| 552 | u16 portno; |
| 553 | u32 bind_address; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 554 | |
| 555 | #define _(N,n) \ |
| 556 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 557 | vl_api_##n##_t_handler, \ |
| 558 | vl_noop_handler, \ |
| 559 | vl_api_##n##_t_endian, \ |
| 560 | vl_api_##n##_t_print, \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 561 | sizeof(vl_api_##n##_t), 1); |
| 562 | foreach_vlib_api_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | #undef _ |
| 564 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 565 | vec_resize (socket_main.input_buffer, 4096); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 567 | /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ |
| 568 | sockfd = socket (AF_INET, SOCK_STREAM, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 569 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 570 | if (sockfd < 0) |
| 571 | { |
| 572 | return clib_error_return_unix (0, "socket"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 575 | rv = ioctl (sockfd, FIONBIO, &one); |
| 576 | if (rv < 0) |
| 577 | { |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 578 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 579 | return clib_error_return_unix (0, "FIONBIO"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 582 | rv = setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)); |
| 583 | if (rv < 0) |
| 584 | { |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 585 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 586 | return clib_error_return_unix (0, "SO_REUSEADDR"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 589 | bzero ((char *) &serv_addr, sizeof (serv_addr)); |
| 590 | serv_addr.sin_family = AF_INET; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 591 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 592 | if (socket_main.bind_address) |
| 593 | bind_address = socket_main.bind_address; |
| 594 | else |
| 595 | bind_address = INADDR_LOOPBACK; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 597 | if (socket_main.portno) |
| 598 | portno = socket_main.portno; |
| 599 | else |
| 600 | portno = SOCKSVR_DEFAULT_PORT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 602 | serv_addr.sin_port = clib_host_to_net_u16 (portno); |
| 603 | serv_addr.sin_addr.s_addr = clib_host_to_net_u32 (bind_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 605 | if (bind (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) |
| 606 | { |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 607 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 608 | return clib_error_return_unix (0, "bind"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 611 | rv = listen (sockfd, 5); |
| 612 | if (rv < 0) |
| 613 | { |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 614 | close (sockfd); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 615 | return clib_error_return_unix (0, "listen"); |
| 616 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 618 | pool_get (socket_main.registration_pool, rp); |
| 619 | memset (rp, 0, sizeof (*rp)); |
| 620 | |
| 621 | rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN; |
| 622 | |
| 623 | template.read_function = socksvr_accept_ready; |
| 624 | template.write_function = socksvr_bogus_write; |
| 625 | template.file_descriptor = sockfd; |
| 626 | template.private_data = rp - socket_main.registration_pool; |
| 627 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 628 | rp->clib_file_index = clib_file_add (fm, &template); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 629 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static clib_error_t * |
| 633 | socket_exit (vlib_main_t * vm) |
| 634 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 635 | clib_file_main_t *fm = &file_main; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 636 | vl_api_registration_t *rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 637 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 638 | /* Defensive driving in case something wipes out early */ |
| 639 | if (socket_main.registration_pool) |
| 640 | { |
| 641 | u32 index; |
| 642 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | pool_foreach (rp, socket_main.registration_pool, ({ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 644 | clib_file_del (fm, fm->file_pool + rp->clib_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | index = rp->vl_api_registration_pool_index; |
| 646 | vl_free_socket_registration_index (index); |
| 647 | })); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 648 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 651 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit); |
| 655 | |
| 656 | static clib_error_t * |
| 657 | socksvr_config (vlib_main_t * vm, unformat_input_t * input) |
| 658 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 659 | int portno; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 661 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 662 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 663 | if (unformat (input, "port %d", &portno)) |
| 664 | { |
| 665 | socket_main.portno = portno; |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | return clib_error_return (0, "unknown input '%U'", |
| 670 | format_unformat_error, input); |
| 671 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 673 | return socksvr_api_init (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr"); |
| 677 | |
| 678 | /* argument in host byte order */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 679 | void |
| 680 | socksvr_set_port (u16 port) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 681 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 682 | socket_main.portno = port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* argument in host byte order */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 686 | void |
| 687 | socksvr_set_bind_address (u32 bind_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 689 | socket_main.bind_address = bind_address; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | clib_error_t * |
| 693 | vlibsocket_init (vlib_main_t * vm) |
| 694 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 695 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | VLIB_INIT_FUNCTION (vlibsocket_init); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * fd.io coding-style-patch-verification: ON |
| 702 | * |
| 703 | * Local Variables: |
| 704 | * eval: (c-set-style "gnu") |
| 705 | * End: |
| 706 | */ |