20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <netinet/in.h> 23 #include <sys/ioctl.h> 39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) 69 if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
70 f = pool_elt_at_index (um->file_pool, reg->unix_file_index);
71 vlib_cli_output (vm,
"%16s %8d",
72 reg->name, f->file_descriptor);
81 u16 msg_id = ntohs(*(
u16 *)elem);
96 tmp = clib_host_to_net_u32 (nbytes);
113 u8 *elem,
u8 *data_vector)
116 u16 msg_id = ntohs(*(
u16 *)elem);
131 nbytes += msg_length;
132 nbytes +=
vec_len(data_vector);
135 tmp = clib_host_to_net_u32(nbytes);
152 data_vector,
vec_len(data_vector));
158 u8 *elem,
u32 msg_length,
int free)
161 u16 msg_id = ntohs(*(
u16 *)elem);
174 nbytes += msg_length;
177 tmp = clib_host_to_net_u32(nbytes);
195 u8 *elem,
u32 msg_length)
201 u8 *elem,
u32 msg_length)
210 clib_warning (
"main pool index %d already free", pool_index);
226 u8 *the_msg = (
u8 *)(input_v+
sizeof(
u32));
248 if (n <= 0 && errno != EAGAIN) {
285 msg_len = clib_net_to_host_u32 (*((
u32 *)msg_buffer));
295 if (msg_len >
vec_len(msg_buffer)) {
307 _vec_len(socket_main.
input_buffer) = save_input_buffer_length;
316 _vec_len(msg_buffer) = 0;
323 _vec_len(socket_main.
input_buffer) = save_input_buffer_length;
409 memset(rp, 0,
sizeof(*rp));
414 template.file_descriptor = fd;
425 struct sockaddr_in client_addr;
429 client_len =
sizeof(client_addr);
436 (
struct sockaddr *)&client_addr,
437 (socklen_t *)&client_len);
468 rp->_vl_msg_id = htons(VL_API_SOCKCLNT_CREATE_REPLY);
489 rp->_vl_msg_id = htons(VL_API_SOCKCLNT_DELETE_REPLY);
504 #define foreach_vlib_api_msg \ 505 _(SOCKCLNT_CREATE, sockclnt_create) \ 506 _(SOCKCLNT_DELETE, sockclnt_delete) 515 struct sockaddr_in serv_addr;
521 vl_msg_api_set_handlers(VL_API_##N, #n, \ 522 vl_api_##n##_t_handler, \ 524 vl_api_##n##_t_endian, \ 525 vl_api_##n##_t_print, \ 526 sizeof(vl_api_##n##_t), 1); 533 sockfd = socket(AF_INET, SOCK_STREAM, 0);
539 rv = ioctl (sockfd, FIONBIO, &one);
544 rv = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one));
549 bzero((
char *) &serv_addr,
sizeof(serv_addr));
550 serv_addr.sin_family = AF_INET;
555 bind_address = INADDR_LOOPBACK;
558 portno = socket_main.
portno;
562 serv_addr.sin_port = clib_host_to_net_u16 (portno);
563 serv_addr.sin_addr.s_addr = clib_host_to_net_u32(bind_address);
565 if (bind(sockfd, (
struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
569 rv = listen(sockfd,5);
575 memset(rp, 0,
sizeof(*rp));
581 template.file_descriptor = sockfd;
598 unix_file_del (um, um->file_pool + rp->unix_file_index);
599 index = rp->vl_api_registration_pool_index;
600 vl_free_socket_registration_index (index);
616 if (
unformat (input,
"port %d", &portno)) {
617 socket_main.
portno = portno;
631 socket_main.
portno = port;
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
void dump_socket_clients(vlib_main_t *vm, api_main_t *am)
static clib_error_t * socksvr_config(vlib_main_t *vm, unformat_input_t *input)
clib_error_t * vl_socket_error_ready(unix_file_t *uf)
static clib_error_t * socksvr_bogus_write(unix_file_t *uf)
clib_error_t * vl_socket_write_ready(unix_file_t *uf)
void vl_socket_add_pending_output(unix_file_t *uf, vl_api_registration_t *rp, u8 *buffer, uword buffer_bytes)
vl_api_registration_t * registration_pool
void vl_socket_api_send_with_data(vl_api_registration_t *rp, u8 *elem, u8 *data_vector)
static clib_error_t * socket_exit(vlib_main_t *vm)
vl_api_registration_t * current_rp
always_inline uword unix_file_add(unix_main_t *um, unix_file_t *template)
void socksvr_file_add(unix_main_t *um, int fd)
void socksvr_set_port(u16 port)
#define SOCKSVR_DEFAULT_PORT
#define pool_is_free(P, E)
socket_main_t socket_main
static void socket_process_msg(unix_file_t *uf, vl_api_registration_t *rp, i8 *input_v)
clib_error_t * vl_socket_read_ready(unix_file_t *uf)
trace_cfg_t * api_trace_cfg
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
#define pool_foreach(VAR, POOL, BODY)
#define VLIB_INIT_FUNCTION(x)
void vl_msg_api_free(void *)
void vl_free_socket_registration_index(u32 pool_index)
always_inline uword pool_elts(void *v)
#define clib_warning(format, args...)
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
void vl_socket_api_send(vl_api_registration_t *rp, u8 *elem)
static void socket_del_pending_output(unix_file_t *uf, vl_api_registration_t *rp, uword n_bytes)
vl_registration_type_t registration_type
#define pool_elt_at_index(p, i)
#define clib_error_return_unix(e, args...)
#define VLIB_CONFIG_FUNCTION(x, n,...)
u32 unprocessed_msg_length
void vl_socket_api_send_with_length_no_free(vl_api_registration_t *rp, u8 *elem, u32 msg_length)
static clib_error_t * socksvr_api_init(vlib_main_t *vm)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
void * vl_msg_api_alloc(int nbytes)
void socksvr_set_bind_address(u32 bind_address)
void vl_api_sockclnt_create_t_handler(vl_api_sockclnt_create_t *mp)
#define vec_free(V)
Free vector's memory (no header).
#define foreach_vlib_api_msg
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
clib_error_t * vlibsocket_init(vlib_main_t *vm)
#define clib_memcpy(a, b, c)
#define pool_is_free_index(P, I)
static clib_error_t * socksvr_accept_ready(unix_file_t *uf)
always_inline void unix_file_del(unix_main_t *um, unix_file_t *f)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
#define vec_append(v1, v2)
Append v2 after v1.
void vl_msg_api_send(vl_api_registration_t *rp, u8 *elem)
void vl_socket_api_send_with_length(vl_api_registration_t *rp, u8 *elem, u32 msg_length)
void vl_msg_api_socket_handler(void *the_msg)
#define UNIX_FILE_DATA_AVAILABLE_TO_WRITE
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 vl_api_registration_pool_index
void(* file_update)(unix_file_t *file, unix_file_update_type_t update_type)
#define clib_error_return(e, args...)
static void vl_socket_api_send_with_length_internal(vl_api_registration_t *rp, u8 *elem, u32 msg_length, int free)
void vl_api_sockclnt_delete_t_handler(vl_api_sockclnt_delete_t *mp)