FD.io VPP  v16.06
Vector Packet Processing
sockclnt_vlib.c
Go to the documentation of this file.
1 /*
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 
20 #include <sys/types.h>
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>
32 #include <vlibsocket/api.h>
33 
35 
36 #define vl_typedefs /* define message structures */
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
44 #undef vl_printfun
45 
46 /* instantiate all the endian swap functions we know about */
47 #define vl_endianfun
49 #undef vl_endianfun
50 
53 {
55 
56  rp->server_handle = mp->handle;
57  rp->server_index = mp->index;
58 }
59 
62 {
63  unix_main_t *um = &unix_main;
66 
67  unix_file_del (um, uf);
69 }
70 
71 u32 sockclnt_open_index (char *client_name, char *hostname, int port)
72 {
74  unix_main_t * um = &unix_main;
75  unix_file_t template = {0};
76  int sockfd;
77  int one = 1;
78  int rv;
79  struct sockaddr_in serv_addr;
80  struct hostent *server;
82  char my_hostname[64];
83 
84  server = gethostbyname(hostname);
85  if (server == NULL) {
86  clib_warning("Couldn't translate server name %s", hostname);
87  return ~0;
88  }
89 
90  /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */
91  sockfd = socket(AF_INET, SOCK_STREAM, 0);
92 
93  if (sockfd < 0) {
94  clib_unix_warning ("socket");
95  return ~0;
96  }
97 
98  bzero((char *) &serv_addr, sizeof(serv_addr));
99  serv_addr.sin_family = AF_INET;
100  bcopy((char *)server->h_addr,
101  (char *)&serv_addr.sin_addr.s_addr,
102  server->h_length);
103  serv_addr.sin_port = htons(port);
104 
105  if (connect(sockfd,(const void *)&serv_addr,sizeof(serv_addr)) < 0) {
106  clib_unix_warning("Connect failure to (%s, %d)",
107  hostname, port);
108  return ~0;
109  }
110 
111  rv = ioctl (sockfd, FIONBIO, &one);
112  if (rv < 0) {
113  clib_unix_warning ("FIONBIO");
114  return ~0;
115  }
116 
118  memset(rp, 0, sizeof(*rp));
121 
122  template.read_function = vl_socket_read_ready;
123  template.write_function = vl_socket_write_ready;
124  template.file_descriptor = sockfd;
125  template.private_data = rp - socket_main.registration_pool;
126 
127  rp->unix_file_index = unix_file_add (um, &template);
128  rp->name = format(0, "%s:%d", hostname, port);
129 
130  mp = vl_msg_api_alloc (sizeof (*mp));
131  mp->_vl_msg_id = ntohs(VL_API_SOCKCLNT_CREATE);
133 
134  if (gethostname(my_hostname, sizeof (my_hostname)) < 0) {
135  clib_unix_warning("gethostname");
136  strncpy (my_hostname, "unknown!", sizeof(my_hostname)-1);
137  }
138  strncpy ((char *)mp->name, my_hostname, sizeof (mp->name)-1);
139 
140  vl_msg_api_send (rp, (u8 *)mp);
141  return rp - socket_main.registration_pool;
142 }
143 
145 {
148 
149  /* Don't crash / assert if fed garbage */
151  clib_warning ("registration_pool index %d already free", index);
152  return;
153  }
155 
156  mp = vl_msg_api_alloc (sizeof (*mp));
157  mp->_vl_msg_id = ntohs(VL_API_SOCKCLNT_DELETE);
158  mp->handle = rp->server_handle;
159  mp->index = rp->server_index;
160  vl_msg_api_send (rp, (u8 *)mp);
161 }
162 
164 {
166 }
167 
168 /*
169  * Both rx and tx msgs MUST be initialized, or we'll have
170  * precisely no idea how many bytes to write into the API trace...
171  */
172 #define foreach_sockclnt_api_msg \
173 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
174 _(SOCKCLNT_DELETE_REPLY, sockclnt_delete_reply)
175 
176 
177 static clib_error_t *
179 {
180 #define _(N,n) \
181  vl_msg_api_set_handlers(VL_API_##N, #n, \
182  vl_api_##n##_t_handler, \
183  vl_noop_handler, \
184  vl_api_##n##_t_endian, \
185  vl_api_##n##_t_print, \
186  sizeof(vl_api_##n##_t), 1);
188 #undef _
189  return 0;
190 }
191 
u32 unix_file_index
Definition: api.h:54
static void vl_api_sockclnt_delete_reply_t_handler(vl_api_sockclnt_delete_reply_t *mp)
Definition: sockclnt_vlib.c:60
static void vl_api_sockclnt_create_reply_t_handler(vl_api_sockclnt_create_reply_t *mp)
Definition: sockclnt_vlib.c:51
VLIB_API_INIT_FUNCTION(sockclnt_vlib_api_init)
vl_api_registration_t * registration_pool
Definition: api.h:36
#define foreach_sockclnt_api_msg
#define NULL
Definition: clib.h:55
vl_api_registration_t * current_rp
Definition: api.h:42
always_inline uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:131
void sockclnt_close_index(u32 index)
u32 server_index
Definition: api.h:61
#define pool_get(P, E)
Definition: pool.h:186
clib_error_t * vl_socket_read_ready(struct unix_file *uf)
Definition: socksvr_vlib.c:234
socket_main_t socket_main
Definition: api.h:48
#define clib_warning(format, args...)
Definition: error.h:59
vl_registration_type_t registration_type
Definition: api.h:38
static clib_error_t * sockclnt_vlib_api_init(vlib_main_t *vm)
#define pool_elt_at_index(p, i)
Definition: pool.h:346
vl_api_registration_t * sockclnt_get_registration(u32 index)
unix_file_t * current_uf
Definition: api.h:43
void * vl_msg_api_alloc(int nbytes)
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define pool_is_free_index(P, I)
Definition: pool.h:197
always_inline void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:141
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
void vl_free_socket_registration_index(u32 pool_index)
Definition: socksvr_vlib.c:206
unix_main_t unix_main
Definition: main.c:57
clib_error_t * vl_socket_write_ready(struct unix_file *uf)
Definition: socksvr_vlib.c:363
void vl_msg_api_send(vl_api_registration_t *rp, u8 *elem)
Definition: memory_vlib.c:90
unsigned char u8
Definition: types.h:56
u32 sockclnt_open_index(char *client_name, char *hostname, int port)
Definition: sockclnt_vlib.c:71
Definition: unix.h:49
u32 vl_api_registration_pool_index
Definition: api.h:41
u32 server_handle
Definition: api.h:60