api: string type to convert to vector
[vpp.git] / src / plugins / http_static / http_static.c
1 /*
2  * http_static.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <http_static/http_static.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25 #include <vlibapi/api_types_inlines.h>
26
27 /* define message IDs */
28 #include <http_static/http_static_msg_enum.h>
29
30 #include <vpp/api/types.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <http_static/http_static_all_api_h.h>
35 #undef vl_typedefs
36
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <http_static/http_static_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <http_static/http_static_all_api_h.h>
46 #undef vl_printfun
47
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <http_static/http_static_all_api_h.h>
51 #undef vl_api_version
52
53 #define REPLY_MSG_ID_BASE hmp->msg_id_base
54 #include <vlibapi/api_helper_macros.h>
55
56 http_static_main_t http_static_main;
57
58 /* List of message types that this plugin understands */
59
60 #define foreach_http_static_plugin_api_msg                           \
61 _(HTTP_STATIC_ENABLE, http_static_enable)
62
63 /* API message handler */
64 static void vl_api_http_static_enable_t_handler
65   (vl_api_http_static_enable_t * mp)
66 {
67   vl_api_http_static_enable_reply_t *rmp;
68   http_static_main_t *hmp = &http_static_main;
69   int rv;
70   u8 *www_root = 0;
71   u8 *uri = 0;
72
73   char *p = (char *) &mp->www_root;
74   www_root = vl_api_from_api_to_vec ((vl_api_string_t *) p);
75   p += vl_api_string_len ((vl_api_string_t *) p) + sizeof (vl_api_string_t);
76   uri = vl_api_from_api_to_vec ((vl_api_string_t *) p);
77
78   rv = http_static_server_enable_api
79     (ntohl (mp->fifo_size),
80      ntohl (mp->cache_size_limit),
81      ntohl (mp->prealloc_fifos),
82      ntohl (mp->private_segment_size), www_root, uri);
83
84   vec_free (www_root);
85   vec_free (uri);
86   REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
87 }
88
89 /* Set up the API message handling tables */
90 static clib_error_t *
91 http_static_plugin_api_hookup (vlib_main_t * vm)
92 {
93   http_static_main_t *hmp = &http_static_main;
94 #define _(N,n)                                                  \
95     vl_msg_api_set_handlers((VL_API_##N + hmp->msg_id_base),     \
96                            #n,                                  \
97                            vl_api_##n##_t_handler,              \
98                            vl_noop_handler,                     \
99                            vl_api_##n##_t_endian,               \
100                            vl_api_##n##_t_print,                \
101                            sizeof(vl_api_##n##_t), 1);
102   foreach_http_static_plugin_api_msg;
103 #undef _
104
105   return 0;
106 }
107
108 #define vl_msg_name_crc_list
109 #include <http_static/http_static_all_api_h.h>
110 #undef vl_msg_name_crc_list
111
112 static void
113 setup_message_id_table (http_static_main_t * hmp, api_main_t * am)
114 {
115 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + hmp->msg_id_base);
116   foreach_vl_msg_name_crc_http_static;
117 #undef _
118 }
119
120 static clib_error_t *
121 http_static_init (vlib_main_t * vm)
122 {
123   http_static_main_t *hmp = &http_static_main;
124   clib_error_t *error = 0;
125   u8 *name;
126
127   hmp->vlib_main = vm;
128   hmp->vnet_main = vnet_get_main ();
129
130   name = format (0, "http_static_%08x%c", api_version, 0);
131
132   /* Ask for a correctly-sized block of API message decode slots */
133   hmp->msg_id_base = vl_msg_api_get_msg_ids
134     ((char *) name, VL_MSG_FIRST_AVAILABLE);
135
136   error = http_static_plugin_api_hookup (vm);
137
138   /* Add our API messages to the global name_crc hash table */
139   setup_message_id_table (hmp, &api_main);
140
141   vec_free (name);
142
143   return error;
144 }
145
146 VLIB_INIT_FUNCTION (http_static_init);
147
148 /* *INDENT-OFF* */
149 VLIB_PLUGIN_REGISTER () =
150 {
151   .version = VPP_BUILD_VER,
152   .description = "HTTP Static Server"
153 };
154 /* *INDENT-ON* */
155
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "gnu")
161  * End:
162  */