+void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
+ delete tunnel->manager;
+ ms_free(tunnel);
+}
+
+static char *linphone_tunnel_config_to_string(const LinphoneTunnelConfig *tunnel_config) {
+ char *str = NULL;
+ if(linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config) != -1) {
+ str = ms_strdup_printf("%s:%d:%d:%d",
+ linphone_tunnel_config_get_host(tunnel_config),
+ linphone_tunnel_config_get_port(tunnel_config),
+ linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config),
+ linphone_tunnel_config_get_delay(tunnel_config));
+ } else {
+ str = ms_strdup_printf("%s:%d",
+ linphone_tunnel_config_get_host(tunnel_config),
+ linphone_tunnel_config_get_port(tunnel_config));
+ }
+ return str;
+}
+
+static LinphoneTunnelConfig *linphone_tunnel_config_from_string(const char *str) {
+ LinphoneTunnelConfig *tunnel_config = NULL;
+ char * dstr = ms_strdup(str);
+ const char *host = NULL;
+ int port = -1;
+ int remote_udp_mirror_port = -1;
+ int delay = -1;
+ int pos = 0;
+ char *pch;
+ pch = strtok(dstr, ":");
+ while(pch != NULL) {
+ switch(pos) {
+ case 0:
+ host = pch;
+ break;
+ case 1:
+ port = atoi(pch);
+ break;
+ case 2:
+ remote_udp_mirror_port = atoi(pch);
+ break;
+ case 3:
+ delay = atoi(pch);
+ break;
+ default:
+ // Abort
+ pos = 0;
+ break;
+
+ }
+ ++pos;
+ pch = strtok(NULL, ":");
+ }
+ if(pos >= 2) {
+ tunnel_config = linphone_tunnel_config_new();
+ linphone_tunnel_config_set_host(tunnel_config, host);
+ linphone_tunnel_config_set_port(tunnel_config, port);
+ }
+ if(pos >= 3) {
+ linphone_tunnel_config_set_remote_udp_mirror_port(tunnel_config, remote_udp_mirror_port);
+ }
+ if(pos == 4) {
+ linphone_tunnel_config_set_delay(tunnel_config, delay);
+ }
+ ms_free(dstr);
+ return tunnel_config;
+}
+
+
+static void linphone_tunnel_save_config(LinphoneTunnel *tunnel) {
+ MSList *elem = tunnel->config_list;
+ char *tmp = NULL, *old_tmp = NULL, *tc_str = NULL;
+ while(elem != NULL) {
+ LinphoneTunnelConfig *tunnel_config = (LinphoneTunnelConfig *)elem->data;
+ tc_str = linphone_tunnel_config_to_string(tunnel_config);
+ if(tmp != NULL) {
+ old_tmp = tmp;
+ tmp = ms_strdup_printf("%s %s", old_tmp, tc_str);
+ ms_free(old_tmp);
+ ms_free(tc_str);
+ } else {
+ tmp = tc_str;
+ }
+ elem = elem->next;
+ }
+ lp_config_set_string(config(tunnel), "tunnel", "server_addresses", tmp);
+ if(tmp != NULL) {
+ ms_free(tmp);
+ }