5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/socket.h>
31 #include <arpa/inet.h>
35 #include <connman/log.h>
37 #include "vici-client.h"
40 #define VICI_REQUEST_TIMEOUT 5000
44 VICI_SECTION_START = 1,
52 enum vici_packet_type {
54 VICI_CMD_RESPONSE = 1,
56 VICI_EVENT_REGISTER = 3,
57 VICI_EVENT_UNREGISTER = 4,
58 VICI_EVENT_CONFIRM = 5,
59 VICI_EVENT_UNKNOWN = 6,
63 static const char *vici_cmd_str[] = {
77 unsigned int allocated;
84 unsigned int rcv_pkt_size;
94 unsigned int rcv_pkt_size;
97 vici_request_reply_cb reply_cb;
98 vici_event_cb event_cb;
99 void *reply_user_data;
100 void *event_user_data;
103 struct _VICISection {
107 GHashTable *subsection;
110 static void remove_list(gpointer data)
115 g_slist_free_full((GSList *)data, g_free);
118 void vici_destroy_section(VICISection* section)
120 g_free(section->name);
121 g_hash_table_destroy(section->kvs);
122 g_hash_table_destroy(section->kvls);
123 g_hash_table_destroy(section->subsection);
127 static void free_section(gpointer data)
129 VICISection* section = (VICISection*)data;
130 vici_destroy_section(section);
133 VICISection* vici_create_section(const char* name)
135 VICISection* section;
137 section = g_try_new0(VICISection, 1);
139 connman_error("Failed to create section");
144 section->name = g_strdup(name);
145 section->kvs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
146 section->kvls = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, remove_list);
147 section->subsection = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_section);
151 int add_subsection(const char* name, VICISection* child, VICISection* section)
153 if (section == NULL || name == NULL || child == NULL) {
154 connman_error("invalid parameter");
158 g_hash_table_insert(section->subsection, g_strdup(name), child);
162 static int add_kvl_to_section(const char* key, const char* value, VICISection* section)
165 if (section == NULL || key == NULL || value == NULL) {
166 connman_error("invalid parameter");
170 list = g_hash_table_lookup(section->kvls, key);
172 list = g_slist_alloc();
174 list = g_slist_prepend(list, g_strdup(value));
175 g_hash_table_replace(section->kvls, g_strdup(key), list);
179 static int add_kv_to_section(const char* key, const char* value, VICISection* section)
181 if (section == NULL || key == NULL || value == NULL) {
182 connman_error("invalid parameter");
186 g_hash_table_insert(section->kvs, g_strdup(key), g_strdup(value));
190 static VICISection* get_subsection(VICISection* section, const char* name)
192 VICISection* sub = g_hash_table_lookup(section->subsection, name);
194 sub = vici_create_section(name);
195 add_subsection(name, sub, section);
200 int vici_add_kv(VICISection* section, const char* key,
201 const char* value, const char* subsection)
203 VICISection* target = section;
204 DBG("key: %s, value: %s, subsection: %s", key, value, subsection);
206 if (section == NULL || key == NULL) {
207 connman_error("invalid parameter");
212 target = get_subsection(section, subsection);
214 add_kv_to_section(key, value, target);
218 int vici_add_kvl(VICISection* section, const char* key,
219 const char* value, const char* subsection)
221 VICISection* target = section;
223 DBG("key: %s, value: %s, subsection: %s", key, value, subsection);
224 if (section == NULL || key == NULL) {
225 connman_error("invalid parameter");
230 target = get_subsection(section, subsection);
232 if (g_strcmp0(subsection, "children") == 0)
233 target = get_subsection(target, "net");
235 add_kvl_to_section(key, value, target);
239 static void add_list_to_section(char *key, GSList *list, VICISection *section)
241 if (section == NULL || key == NULL || list == NULL)
244 g_hash_table_insert(section->kvls, g_strdup(key), g_slist_copy_deep(list, (GCopyFunc)g_strdup, NULL));
248 int vici_add_list(VICISection* section, char *key, GSList *list, const char* subsection)
250 VICISection* target = section;
252 DBG("key: %s, subsection: %s", key, subsection);
253 if (section == NULL || key == NULL) {
254 connman_error("invalid parameter");
259 target = get_subsection(section, subsection);
261 if (g_strcmp0(subsection, "children") == 0)
262 target = get_subsection(target, "net");
264 add_list_to_section(key, list, target);
268 static char *load_cert_from_path(const char *path)
273 size_t file_size = 0;
274 char *file_buff = NULL;
276 fp = fopen(path, "rb");
278 connman_error("fopen failed");
284 connman_error("fp is not a valid stream");
290 file_size = st.st_size;
291 file_buff = g_try_malloc0(sizeof(char)*st.st_size);
292 if (file_buff == NULL) {
293 connman_error("g_try_malloc0 failed\n");
298 if (fread(file_buff, 1, file_size, fp) != file_size) {
299 connman_error("file size not matched\n");
308 int vici_add_cert_kv(VICISection *section, const char *key,
309 const char *value, const char *subsection)
315 DBG("value is null");
319 cert = load_cert_from_path(value);
323 ret = vici_add_kv(section, key, (const char *)cert, subsection);
328 int vici_add_cert_kvl(VICISection *section, const char *key,
329 const char *value, const char *subsection)
334 cert = load_cert_from_path(value);
338 ret = vici_add_kvl(section, key, (const char *)cert, subsection);
343 static void *add_element(struct request *r, enum vici_element type,
349 if (r->used + size + 1 > r->allocated) {
350 newlen = r->allocated;
351 while (newlen < r->used + size + 1) {
354 new = realloc(r->sndbuf, newlen);
360 r->allocated = newlen;
362 r->sndbuf[r->used++] = type;
363 ret = r->sndbuf + r->used;
368 static void section_start(struct request *r, const char *name)
374 pos = add_element(r, VICI_SECTION_START, 1 + nlen);
377 memcpy(pos + 1, name, nlen);
381 static void section_end(struct request *r)
383 add_element(r, VICI_SECTION_END, 0);
386 static void key_value(struct request *r, const char *name,
387 const void *buf, unsigned int buflen)
394 pos = add_element(r, VICI_KEY_VALUE, 1 + nlen + sizeof(vlen) + buflen);
397 memcpy(pos + 1, name, nlen);
398 vlen = htons(buflen);
399 memcpy(pos + 1 + nlen, &vlen, sizeof(vlen));
400 memcpy(pos + 1 + nlen + sizeof(vlen), buf, buflen);
405 static void list_start(struct request *r, const char *name)
411 pos = add_element(r, VICI_LIST_START, 1 + nlen);
414 memcpy(pos + 1, name, nlen);
418 static void list_item(struct request *r, const void *buf,
424 pos = add_element(r, VICI_LIST_ITEM, sizeof(vlen) + buflen);
426 vlen = htons(buflen);
427 memcpy(pos, &vlen, sizeof(vlen));
428 memcpy(pos + sizeof(vlen), buf, buflen);
432 static void list_end(struct request *r)
434 add_element(r, VICI_LIST_END, 0);
437 static void destroy_vici_request(gpointer data)
439 struct request *req = (struct request *)data;
447 static int create_vici_request(enum vici_packet_type type, VICIClientCmd cmd,
450 struct request *req = NULL;
452 if (cmd >= VICI_CMD_MAX || !rp)
455 req = g_try_new0(struct request, 1);
457 connman_error("g_try_new0 failed");
462 req->used += strlen(vici_cmd_str[cmd]);
463 req->allocated = MIN(32, req->used);
464 req->sndbuf = g_try_new0(char, req->allocated);
466 connman_error("g_try_new0 failed");
471 req->sndbuf[0] = type;
472 req->sndbuf[1] = req->used - 2; /* except for type and name length */
473 memcpy(req->sndbuf + 2, vici_cmd_str[cmd], req->used - 2);
474 req->hdr_len = req->used;
482 static void write_section_kvs(VICISection *section, struct request *req)
487 if (section == NULL || req == NULL)
490 g_hash_table_iter_init (&iter, section->kvs);
491 while (g_hash_table_iter_next (&iter, &key, &value)) {
494 key_value(req, (const char*)key, (const void *)value, strlen((char *)value));
500 static void write_list_item(gpointer data, gpointer user_data)
502 struct request *req = NULL;
505 if (!data || !user_data)
508 value = (char *)data;
509 req = (struct request *)user_data;
510 list_item(req, value, strlen(value));
515 static void write_section_kvls(VICISection *section, struct request *req)
520 if (section == NULL || req == NULL)
523 g_hash_table_iter_init (&iter, section->kvls);
524 while (g_hash_table_iter_next (&iter, &key, &value)) {
528 list_start(req, key);
529 g_slist_foreach((GSList *)value, (GFunc)write_list_item, (gpointer)req);
536 static void write_section(struct request *req, VICISection *section)
541 if (req == NULL || section == NULL)
545 section_start(req, section->name);
547 write_section_kvs(section, req);
548 write_section_kvls(section, req);
550 g_hash_table_iter_init(&iter, section->subsection);
551 while (g_hash_table_iter_next (&iter, &key, &value)) {
554 write_section(req, (VICISection *)value);
562 static int check_socket(int sock)
568 p_fd.events = POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL;
569 res = poll((struct pollfd *) &p_fd, 1, 1);
572 connman_error("Polling error from socket\n");
574 } else if (res == 0) {
575 connman_error( "poll timeout. socket is busy\n");
579 if (p_fd.revents & POLLERR) {
580 connman_error("Error! POLLERR from socket[%d]\n", sock);
582 } else if (p_fd.revents & POLLHUP) {
583 connman_error("Error! POLLHUP from socket[%d]\n", sock);
585 } else if (p_fd.revents & POLLNVAL) {
586 connman_error("Error! POLLNVAL from socket[%d]\n", sock);
588 } else if (p_fd.revents & POLLIN) {
590 } else if (p_fd.revents & POLLOUT) {
595 connman_error("Unknown poll event [%d]\n", p_fd.revents);
599 static int write_socket(int sock, char *data, int data_len)
602 int left_len = data_len;
606 if (sock < SOCK_FD_MIN || !data || data_len < 0)
609 res = check_socket(sock);
617 wbytes = write(sock, ptr, left_len);
619 connman_error("Failed to write data into socket[%d].\n", sock);
621 }else if (wbytes < left_len) {
624 } else if (wbytes == left_len) {
627 connman_error("Unknown error occurred.\n");
638 int send_vici_command(struct request *req, VICIClient *vici_client)
640 unsigned int size = 0;
644 if (req == NULL || vici_client == NULL) {
645 connman_error("request is NULL\n");
648 sock_fd = vici_client->client_sock_fd;
650 size = htonl(req->used);
651 res = write_socket(sock_fd, (char *)&size, sizeof(size));
653 connman_error("failed to send size with network byte order\n");
657 res = write_socket(sock_fd, req->sndbuf, req->used);
659 connman_error("failed to send pkt\n");
663 if(req->cmd != VICI_CMD_REGISTER_CHILD_UPDOWN)
664 vici_client->request_list = g_slist_append(vici_client->request_list, req);
669 static void print_vici_element(int elem_type, char *value, int sections)
675 case VICI_SECTION_START:
676 for (i = 0; i < sections - 1; i++)
678 DBG("%s = {\n", value);
680 case VICI_SECTION_END:
681 for (i = 0; i < sections; i++)
686 for (i = 0; i < sections; i++)
690 case VICI_LIST_START:
691 for (i = 0; i < sections; i++)
693 DBG("%s = [", value);
707 static void debug_vici_message(char *buf, unsigned int size)
710 unsigned int pos = 0;
715 if (buf == NULL || size == 0)
724 case VICI_SECTION_START:
728 g_strlcpy(temp, (const gchar *)&buf[pos], len + 1);
733 case VICI_SECTION_END:
745 g_strlcpy(temp, (const gchar *)&buf[pos], key_len + 1);
747 pos += (key_len + 1);
748 value_len = buf[pos];
750 g_strlcpy(temp + key_len + 1, (const gchar *)&buf[pos], value_len + 1);
752 len = key_len + 1 + value_len;
755 case VICI_LIST_START:
759 g_strlcpy(temp, (const gchar *)&buf[pos], len + 1);
768 g_strlcpy(temp, (const gchar *)&buf[pos], len + 1);
777 print_vici_element(type, temp, sections);
782 static unsigned int extract_key_value(char *buf, unsigned int pos, char **key, char **value)
789 *key = g_strndup((const gchar *)&buf[pos], key_len);
791 value_len = buf[pos];
793 *value = g_strndup((const gchar *)&buf[pos], value_len);
798 static gboolean extract_request_result(char *buf, unsigned int size, char **err)
800 gboolean success = FALSE;
801 unsigned int pos = 0;
809 if (type == VICI_KEY_VALUE) {
812 pos = extract_key_value(buf, pos, &key, &value);
813 DBG("pos : %d size : %d\n", pos, size);
815 /* TODO :remove this after debug */
816 DBG("key : %s value : %s\n", key, value);
817 if (g_strcmp0(key, "success") == 0)
818 (g_strcmp0(value, "yes") == 0)?(success = TRUE):(success = FALSE);
820 if (g_strcmp0(key, "errmsg"))
821 *err = g_strdup(value);
829 static int handle_vici_result(gboolean success, int cmd, char * err)
838 case VICI_CMD_LOAD_CONN:
841 case VICI_CMD_LOAD_SHARED:
844 case VICI_CMD_LOAD_CERT:
847 case VICI_CMD_LOAD_AUTH:
850 case VICI_CMD_LOAD_KEY:
853 case VICI_CMD_INITIATE:
856 case VICI_CMD_TERMINATE:
863 DBG(" %s failed with %d!\n", vici_cmd_str[cmd], ret);
867 static int process_vici_response(struct request * req)
870 gboolean success = FALSE;
876 if (!req->rcvbuf || req->rcvbuf[0] != VICI_CMD_RESPONSE)
879 //TODO: remove below when there's no further problem.
880 debug_vici_message(req->rcvbuf, req->rcv_pkt_size);
882 success = extract_request_result(req->rcvbuf, req->rcv_pkt_size, &err);
883 ret = handle_vici_result(success, req->cmd, err);
888 int vici_send_request(VICIClient *vici_client, VICIClientCmd cmd, VICISection *root)
890 struct request *req = NULL;
893 DBG("%s", vici_cmd_str[cmd]);
894 ret = create_vici_request(VICI_CMD_REQUEST, cmd, &req);
896 connman_error("error on create_request\n");
900 write_section(req, root);
901 //TODO: remove below when there's no further problem.
902 debug_vici_message(req->sndbuf + req->hdr_len - 1, req->used - req->hdr_len + 1);
904 ret = send_vici_command(req, vici_client);
906 destroy_vici_request(req);
907 connman_error("error on send_command\n");
914 int vici_set_event_cb(VICIClient *vici_client, vici_event_cb cb, gpointer user_data)
916 struct request *req = NULL;
919 DBG("%s",vici_cmd_str[VICI_EVENT_CHILD_UP]);
920 ret = create_vici_request(VICI_EVENT_REGISTER, VICI_CMD_REGISTER_CHILD_UPDOWN, &req);
922 connman_error("error on create_request\n");
926 ret = send_vici_command(req, vici_client);
928 connman_error("error on send_command\n");
931 destroy_vici_request(req);
932 vici_client->event_cb = cb;
933 vici_client->event_user_data = user_data;
939 static int get_socket_from_source(GIOChannel *source, GIOCondition condition)
943 sock = g_io_channel_unix_get_fd(source);
944 if (sock < SOCK_FD_MIN)
947 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) || (condition & G_IO_NVAL)) {
948 connman_error("G_IO_ERR/G_IO_HUP/G_IO_NVAL received sock [%d] condition [%d]\n", sock, condition);
949 //TODO: handle the breaking socket
955 static int read_socket(int sock, char *data, unsigned int data_len)
958 int total_rbytes = 0;
960 if (sock < SOCK_FD_MIN || !data || data_len <= 0)
963 while (data_len > 0) {
965 rbytes = read(sock, data, data_len);
969 total_rbytes += rbytes;
977 static int recv_vici_pkt(int sock, struct _VICIClient *vici_client)
982 if (vici_client->rcv_pkt_size == 0) {
983 unsigned int pkt_size = 0;
984 if (read_socket(sock, (char *)&pkt_size, sizeof(pkt_size)) < 0)
987 vici_client->rcv_pkt_size = ntohl(pkt_size);
988 /* TODO :REMOVE THIS AFTER DEBUG */
989 DBG("rcv_pkt_size [%d] will be recved\n", vici_client->rcv_pkt_size);
992 DBG("rcv_pkt_size [%d] is recved\n", vici_client->rcv_pkt_size);
994 buf = g_try_malloc0(vici_client->rcv_pkt_size);
998 if (read_socket(sock, buf, vici_client->rcv_pkt_size) < 0) {
1002 vici_client->rcvbuf = buf;
1008 static struct request *pop_vici_request(VICIClient *vici_client)
1010 GSList *list = NULL;
1015 list = vici_client->request_list;
1022 static void process_vici_reply(VICIClient *vici_client)
1024 struct request *req;
1030 /* get first request */
1031 req = pop_vici_request(vici_client);
1035 req->rcvbuf = vici_client->rcvbuf;
1036 req->rcv_pkt_size = vici_client->rcv_pkt_size;
1038 ret = process_vici_response(req);
1039 vici_client->request_list = g_slist_remove(vici_client->request_list, req);
1040 destroy_vici_request(req);
1042 /* TODO :remove this after debug */
1043 DBG("left request reply : %d", g_slist_length(vici_client->request_list));
1045 if (ret != 0 || g_slist_length(vici_client->request_list) == 0)
1046 vici_client->reply_cb(ret, vici_client->reply_user_data);
1050 static int extract_event_name(char *buf, unsigned int size, char *temp)
1054 name_len = buf[pos];
1056 DBG("event len: %d", name_len);
1057 while(pos < size && pos - 2 < name_len) {
1058 temp[pos - 2] = buf[pos];
1062 DBG("event name: %s", temp);
1066 static char *vici_get_value(char *buf, unsigned int pos, unsigned int size, char *search_key)
1071 while (pos < size) {
1075 if (type == VICI_KEY_VALUE) {
1078 pos = extract_key_value(buf, pos, &key, &value);
1079 if (g_strcmp0(search_key, key) == 0) {
1091 static void process_child_updown(VICIClient *vici_client,char *buf, unsigned int size)
1095 state = vici_get_value(buf, 0, size, "state");
1096 if (g_strcmp0(state, "ESTABLISHED") == 0) {
1098 vici_client->event_cb(VICI_EVENT_CHILD_UP, vici_client->event_user_data);
1099 } else if (g_strcmp0(state, "DELETING") == 0) {
1101 vici_client->event_cb(VICI_EVENT_CHILD_DOWN, vici_client->event_user_data);
1103 DBG("Unknown event");
1109 static void process_vici_event(VICIClient *vici_client)
1112 unsigned int size = 0;
1113 unsigned int pos = 0;
1114 char temp[256] = {0,};
1115 if (!vici_client || !(vici_client->rcvbuf) || vici_client->rcv_pkt_size == 0)
1118 buf = vici_client->rcvbuf;
1119 size = vici_client->rcv_pkt_size;
1121 pos = extract_event_name(buf, size, temp);
1122 /* TODO: remove below after debug */
1124 if (g_strcmp0(temp, "child-updown") == 0)
1125 process_child_updown(vici_client, buf + pos -1, size - pos);
1128 static void process_vici_packet(VICIClient *vici_client, char *buf)
1131 if (!vici_client || !buf)
1134 if (buf[0] == VICI_CMD_RESPONSE) {
1135 DBG("VICI_CMD_RESPONSE\n");
1136 process_vici_reply(vici_client);
1137 } else if (buf[0] == VICI_EVENT_CONFIRM) {
1138 DBG("VICI_EVENT_CONFIRM\n");
1139 } else if (buf[0] == VICI_EVENT) {
1141 process_vici_event(vici_client);
1143 DBG("Not handled [%u]", buf[0]);
1148 static gboolean process_vici_msg(GIOChannel *source,
1149 GIOCondition condition,
1152 VICIClient *vici_client = NULL;
1155 vici_client = (VICIClient *)user_data;
1159 sock = get_socket_from_source(source, condition);
1164 if(recv_vici_pkt(sock, vici_client) < 0)
1167 if (!vici_client->rcvbuf) {
1171 process_vici_packet(vici_client, vici_client->rcvbuf);
1172 g_free(vici_client->rcvbuf);
1173 vici_client->rcvbuf = NULL;
1174 vici_client->rcv_pkt_size = 0;
1179 static int str_to_socket_addr(const char *uri, struct sockaddr_un *addr)
1181 memset(addr, 0, sizeof(*addr));
1182 addr->sun_family = AF_UNIX;
1183 strncpy(addr->sun_path, uri, sizeof(addr->sun_path));
1185 addr->sun_path[sizeof(addr->sun_path)-1] = '\0';
1187 return offsetof(struct sockaddr_un, sun_path) + strlen(addr->sun_path);
1190 static int connect_socket(const char *uri)
1192 struct sockaddr_un addr;
1195 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1197 connman_error("socket() failed");
1201 len = str_to_socket_addr(uri, &addr);
1203 connman_error("str_to_socket_addr failed");
1208 if (connect(fd, (struct sockaddr*)&addr, len) < 0) {
1209 connman_error("connect failed. errno %d/%s", errno, strerror(errno));
1217 static int initialize_vici_source(VICIClient *vici_client)
1219 GIOChannel *vici_channel;
1224 vici_client->client_sock_fd = connect_socket(VICI_DEFAULT_URI);
1225 if (vici_client->client_sock_fd < 0) {
1226 connman_error("connect_socket failed");
1230 vici_channel = g_io_channel_unix_new(vici_client->client_sock_fd);
1231 if (!vici_channel) {
1232 connman_error("g_io_channel_unix_new failed");
1233 close(vici_client->client_sock_fd);
1237 vici_client->client_watch = g_io_add_watch_full(vici_channel,
1239 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1240 (GIOFunc)process_vici_msg,
1241 (gpointer)vici_client,
1243 g_io_channel_unref(vici_channel);
1247 int vici_initialize(VICIClient **vici_client)
1251 *vici_client = g_try_new0(VICIClient, 1);
1252 if (!*vici_client) {
1253 connman_error("out of memory");
1257 ret = initialize_vici_source(*vici_client);
1259 g_free(*vici_client);
1267 void vici_set_request_reply_cb(VICIClient *vici_client, vici_request_reply_cb reply_cb, gpointer user_data)
1269 vici_client->reply_cb = reply_cb;
1270 vici_client->reply_user_data = user_data;
1273 int vici_deinitialize(VICIClient *vici_client)
1275 if (vici_client->client_watch > 0) {
1276 g_source_remove(vici_client->client_watch);
1277 vici_client->client_watch = 0;
1280 close(vici_client->client_sock_fd);
1281 g_slist_free_full(vici_client->request_list, destroy_vici_request);
1282 g_free(vici_client->rcvbuf);
1283 g_free(vici_client);