-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnman_proxy_utils.c
252 lines (230 loc) · 9.14 KB
/
connman_proxy_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2019, Munez Bokkapatna Nayakwady <[email protected]>, All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author(s) : Munez BN <[email protected]>
* File : connman_proxy_utils.c
* Description : Utility functions like printing variant or service
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "connman_proxy.h"
#include "connman_proxy_internal.h"
#define MAX_DEPTH_LEVEL 10
char *s_depth_tab_str = "\t\t\t\t\t\t\t\t\t\t";
/**** Static ****/
/**** Hidden ****/
void
connman_proxy_util_notify_error_cb(connman_proxy_handler_t *connman_proxy_handler, connman_proxy_error_type_t error_code)
{
connman_return_if_invalid_arg(NULL == connman_proxy_handler);
if(connman_proxy_handler->cb && connman_proxy_handler->cb->on_update)
{
connman_proxy_update_cb_data_t *notify_data = (connman_proxy_update_cb_data_t*) malloc(sizeof(connman_proxy_update_cb_data_t));
if(NULL == notify_data)
return;
notify_data->notify_type = CONNMAN_PROXY_NOTIFY_ERROR;
notify_data->data.error_code = error_code;
connman_proxy_handler->cb->on_update(notify_data, connman_proxy_handler->cb->cookie);
}
}
void
connman_proxy_util_notify_connman_service_cb(connman_proxy_handler_t *connman_proxy_handler, gboolean available)
{
connman_return_if_invalid_arg(NULL == connman_proxy_handler);
if(connman_proxy_handler->cb && connman_proxy_handler->cb->on_update)
{
connman_proxy_update_cb_data_t *notify_data = (connman_proxy_update_cb_data_t*) malloc(sizeof(connman_proxy_update_cb_data_t));
if(NULL == notify_data)
return;
notify_data->notify_type = CONNMAN_PROXY_NOTIFY_CONNMAN_SERVICE_UPDATE;
notify_data->data.service_available = available;
connman_proxy_handler->cb->on_update(notify_data, connman_proxy_handler->cb->cookie);
}
}
void
connman_proxy_util_print_array_of_string(GVariant *res)
{
GVariantIter iter;
gchar *value;
g_variant_iter_init (&iter, res);
while (g_variant_iter_next (&iter, "s", &value))
{
CONNMAN_LOG_USER("\t%s\n", value);
g_free (value);
}
}
void
connman_proxy_util_print_custom(GVariant *res)
{
if(g_strcmp0(g_variant_get_type_string (res), "(sv)") == 0 )
{
GVariant *val = NULL;
gchar *string = NULL;
g_variant_get (res, "(sv)", &string, &val);
connman_proxy_util_print_g_variant(string, val);
g_free (string);
g_variant_unref (val);
}
else
{
CONNMAN_LOG_WARNING("%-12s : Unknown Property of type '%s'\n", "test", g_variant_get_type_string (res));
}
}
void
connman_proxy_util_print_array_of_dict(GVariant *res)
{
GVariantIter iter;
GVariant *value;
gchar *key;
g_variant_iter_init (&iter, res);
while (g_variant_iter_next (&iter, "{sv}", &key, &value))
{
connman_proxy_util_print_g_variant(key, value);
/*must free data for ourselves*/
g_variant_unref (value);
g_free (key);
}
}
void
connman_proxy_g_free(gpointer data, gpointer user_data)
{
CONNMAN_PROXY_UNUSED(user_data);
g_free(data);
}
/**** Global ****/
CP_EXPORT void
connman_proxy_util_print_services(connman_proxy_service_info_t *service)
{
CONNMAN_LOG_USER("\n************* Service %s *************\n", service->obj_path);
CONNMAN_LOG_USER( "\t\tService Name : %s\n"
"\t\tName : %s\n"
"\t\tType : %s\n"
"\t\tSignale Strength : %hhu\n"
"\t\tState : %s\n"
"\t\tFavourite : %s\n"
"\t\tImmutable : %s\n"
"\t\tAutoconnect : %s\n"
"\t\tMDNS Enabled : %s\n"
"\t\tIPV4\n"
"\t\t\tMethod :%s\n"
"\t\t\tAddress :%s\n"
"\t\t\tNetMask :%s\n"
"\t\t\tGateWay :%s\n"
"\t\tEthernet\n"
"\t\t\tMethod :%s\n"
"\t\t\tInterface :%s\n"
"\t\t\tMac Address :%s\n"
"\t\t\tMTU :%hu\n"
"\t\tNetwork Proxy\n"
"\t\t\tMethod :%s\n"
"\t\t\tURL :%s\n"
,
service->service_name, service->name ? service->name : "Hidden", service->type, service->signal_strength, service->state,
service->favorite ? "Yes": "No", service->immutable ? "Yes": "No", service->autoconnect ? "Yes": "No", service->mdns ? "Yes": "No",
service->ipv4.method, service->ipv4.address, service->ipv4.netmask, service->ipv4.gateway,
service->eth.method, service->eth.interface, service->eth.address, service->eth.mtu,
service->proxy.method, service->proxy.url ? service->proxy.url : "");
/* To print PROXY/DNS/NTP/DOMAIN servers*/
{
GSList *tmp = NULL;
int i = 0;
for (i = 0, tmp = service->proxy.servers; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tPROXY %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("\t\tExcludes\n");
for (i = 0, tmp = service->proxy.exclude; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tEXCLUDE %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("\t\tSecurity\n");
for (i = 0, tmp = service->security; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tSecurity %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("\t\tDNS\n");
for (i = 0, tmp = service->nameservers; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tDNS %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("\t\tTime Servers\n");
for (i = 0, tmp = service->timeservers; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tNTS %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("\t\tDomains\n");
for (i = 0, tmp = service->domains; tmp; tmp = g_slist_next (tmp), i++)
{
CONNMAN_LOG_USER("\t\t\tDOMAIN %d - %s\n", i, (char *)tmp->data);
}
CONNMAN_LOG_USER("************* End *************\n");
}
}
CP_EXPORT void
connman_proxy_util_print_g_variant(char *name, GVariant *val)
{
#if defined(CONNMAN_LOG_LEVEL) && (CONNMAN_LOG_LEVEL > CONN_LOG_INFO)
static uint8_t depth_level = 0;
if(depth_level > MAX_DEPTH_LEVEL)
{
CONNMAN_LOG_WARNING("Print MAX_DEPTH_LEVEL is %d and we have reached to depth %d.. \n", MAX_DEPTH_LEVEL, depth_level);
return;
}
if(g_variant_type_equal(G_VARIANT_TYPE_BOOLEAN, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%-12s : %s\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name, g_variant_get_boolean (val) ? "Yes" : "No");
}
else if(g_variant_type_equal(G_VARIANT_TYPE_STRING, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%-12s : %s\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name, g_variant_get_string (val, NULL));
}
else if(g_variant_type_equal(G_VARIANT_TYPE_BYTE, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%-12s : %"PRIu8"\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name, g_variant_get_byte (val));
}
else if(g_variant_type_equal(G_VARIANT_TYPE_UINT16, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%-12s : %"PRIu16"\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name, g_variant_get_uint16 (val));
}
else if(g_variant_type_equal(G_VARIANT_TYPE_STRING_ARRAY, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%s {\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name);
depth_level++;
connman_proxy_util_print_array_of_string(val);
depth_level--;
CONNMAN_LOG_USER ("%s}\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level));
}
else if(g_variant_type_equal(G_VARIANT_TYPE_VARDICT, g_variant_get_type(val)))
{
CONNMAN_LOG_USER ("%s%s {\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level), name);
depth_level++;
connman_proxy_util_print_array_of_dict(val);
depth_level--;
CONNMAN_LOG_USER ("%s}\n", s_depth_tab_str+(MAX_DEPTH_LEVEL-depth_level));
}
else
{
connman_proxy_util_print_custom(val);
}
#endif /* CONNMAN_LOG_LEVEL > CONN_LOG_ERROR */
return;
}