/
gsupplicant.c
964 lines (889 loc) · 27.9 KB
/
gsupplicant.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/*
* Copyright (C) 2015-2019 Jolla Ltd.
* Copyright (C) 2015-2019 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "gsupplicant.h"
#include "gsupplicant_dbus.h"
#include "gsupplicant_util_p.h"
#include "gsupplicant_error.h"
#include "gsupplicant_log.h"
#include <gutil_strv.h>
#include <gutil_misc.h>
/* Generated headers */
#include "fi.w1.wpa_supplicant1.h"
/* Log module */
GLOG_MODULE_DEFINE("gsupplicant");
/* Object definition */
enum gsupplicant_proxy_handler_id {
PROXY_INTERFACE_ADDED,
PROXY_INTERFACE_REMOVED,
PROXY_NOTIFY_NAME_OWNER,
PROXY_NOTIFY_CAPABILITIES,
PROXY_NOTIFY_EAP_METHODS,
PROXY_NOTIFY_INTERFACES,
PROXY_HANDLER_COUNT
};
struct gsupplicant_priv {
GDBusConnection* bus;
FiW1Wpa_supplicant1* proxy;
guint32 pending_signals;
GStrV* interfaces;
gulong proxy_handler_id[PROXY_HANDLER_COUNT];
};
typedef GObjectClass GSupplicantClass;
G_DEFINE_TYPE(GSupplicant, gsupplicant, G_TYPE_OBJECT)
#define GSUPPLICANT_TYPE (gsupplicant_get_type())
#define GSUPPLICANT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
GSUPPLICANT_TYPE, GSupplicant))
typedef union gsupplicant_call_func_union {
GCallback cb;
GSupplicantResultFunc fn_void;
GSupplicantStringResultFunc fn_string;
} GSupplicantCallFuncUnion;
typedef
void
(*GSupplicantCallFinishFunc)(
GSupplicant* supplicant,
GCancellable* cancel,
GAsyncResult* result,
GSupplicantCallFuncUnion fn,
void* data);
typedef struct gsupplicant_call {
GSupplicant* supplicant;
GCancellable* cancel;
gulong cancel_id;
GSupplicantCallFinishFunc finish;
GSupplicantCallFuncUnion fn;
void* data;
} GSupplicantCall;
/* Supplicant properties */
#define GSUPPLICANT_PROPERTIES_(p) \
p(VALID,valid) \
p(CAPABILITIES,capabilities) \
p(EAP_METHODS,eap-methods) \
p(INTERFACES,interfaces)
/* Supplicant signals */
typedef enum gsupplicant_signal {
#define SIGNAL_ENUM_(P,p) SIGNAL_##P##_CHANGED,
GSUPPLICANT_PROPERTIES_(SIGNAL_ENUM_)
#undef SIGNAL_ENUM_
SIGNAL_PROPERTY_CHANGED,
SIGNAL_COUNT
} GSUPPLICANT_SIGNAL;
#define SIGNAL_BIT(name) (1 << SIGNAL_##name##_CHANGED)
/* Assert that we have covered all publicly defined properties */
G_STATIC_ASSERT((int)SIGNAL_PROPERTY_CHANGED ==
((int)GSUPPLICANT_PROPERTY_COUNT-1));
#define SIGNAL_PROPERTY_CHANGED_NAME "property-changed"
#define SIGNAL_PROPERTY_CHANGED_DETAIL "%x"
#define SIGNAL_PROPERTY_CHANGED_DETAIL_MAX_LEN (8)
static GQuark gsupplicant_property_quarks[SIGNAL_PROPERTY_CHANGED];
static guint gsupplicant_signals[SIGNAL_COUNT];
static const char* gsupplicant_signame[SIGNAL_COUNT] = {
#define SIGNAL_NAME_(P,p) #p "-changed",
GSUPPLICANT_PROPERTIES_(SIGNAL_NAME_)
#undef SIGNAL_NAME_
SIGNAL_PROPERTY_CHANGED_NAME
};
G_STATIC_ASSERT(G_N_ELEMENTS(gsupplicant_signame) == SIGNAL_COUNT);
/* Proxy properties */
#define PROXY_PROPERTY_NAME_DEBUG_LEVEL "DebugLevel"
#define PROXY_PROPERTY_NAME_DEBUG_SHOW_KEYS "DebugShowKeys"
#define PROXY_PROPERTY_NAME_DEBUG_TIMESTAMP "DebugTimestamp"
#define PROXY_PROPERTY_NAME_CAPABILITIES "Capabilities"
#define PROXY_PROPERTY_NAME_EAP_METHODS "EapMethods"
#define PROXY_PROPERTY_NAME_INTERFACES "Interfaces"
/* Capabilities */
static const GSupNameIntPair gsupplicant_caps [] = {
{ "ap", GSUPPLICANT_CAPS_AP },
{ "ibss-rsn", GSUPPLICANT_CAPS_IBSS_RSN },
{ "p2p", GSUPPLICANT_CAPS_P2P },
{ "interworking", GSUPPLICANT_CAPS_INTERWORKING }
};
/* EAP methods */
static const GSupNameIntPair gsupplicant_eap_methods [] = {
{ "MD5", GSUPPLICANT_EAP_METHOD_MD5 },
{ "TLS", GSUPPLICANT_EAP_METHOD_TLS },
{ "MSCHAPV2", GSUPPLICANT_EAP_METHOD_MSCHAPV2 },
{ "PEAP", GSUPPLICANT_EAP_METHOD_PEAP },
{ "TTLS", GSUPPLICANT_EAP_METHOD_TTLS },
{ "GTC", GSUPPLICANT_EAP_METHOD_GTC },
{ "OTP", GSUPPLICANT_EAP_METHOD_OTP },
{ "SIM", GSUPPLICANT_EAP_METHOD_SIM },
{ "LEAP", GSUPPLICANT_EAP_METHOD_LEAP },
{ "PSK", GSUPPLICANT_EAP_METHOD_PSK },
{ "AKA", GSUPPLICANT_EAP_METHOD_AKA },
{ "FAST", GSUPPLICANT_EAP_METHOD_FAST },
{ "PAX", GSUPPLICANT_EAP_METHOD_PAX },
{ "SAKE", GSUPPLICANT_EAP_METHOD_SAKE },
{ "GPSK", GSUPPLICANT_EAP_METHOD_GPSK },
{ "WSC", GSUPPLICANT_EAP_METHOD_WSC },
{ "IKEV2", GSUPPLICANT_EAP_METHOD_IKEV2 },
{ "TNC", GSUPPLICANT_EAP_METHOD_TNC },
{ "PWD", GSUPPLICANT_EAP_METHOD_PWD }
};
static const GSupNameIntPair gsupplicant_cipher_suites [] = {
{ "none", GSUPPLICANT_CIPHER_NONE },
{ "ccmp", GSUPPLICANT_CIPHER_CCMP },
{ "tkip", GSUPPLICANT_CIPHER_TKIP },
{ "wep104", GSUPPLICANT_CIPHER_WEP104 },
{ "wep40", GSUPPLICANT_CIPHER_WEP40 },
{ "aes128cmac", GSUPPLICANT_CIPHER_AES128_CMAC }
};
static const GSupNameIntPair gsupplicant_keymgmt_suites [] = {
{ "none", GSUPPLICANT_KEYMGMT_NONE },
{ "wpa-psk", GSUPPLICANT_KEYMGMT_WPA_PSK },
{ "wpa-ft-psk", GSUPPLICANT_KEYMGMT_WPA_FT_PSK },
{ "wpa-psk-sha256", GSUPPLICANT_KEYMGMT_WPA_PSK_SHA256 },
{ "wpa-eap", GSUPPLICANT_KEYMGMT_WPA_EAP },
{ "wpa-ft-eap", GSUPPLICANT_KEYMGMT_WPA_FT_EAP },
{ "wpa-eap-sha256", GSUPPLICANT_KEYMGMT_WPA_EAP_SHA256 },
{ "ieee8021x", GSUPPLICANT_KEYMGMT_IEEE8021X },
{ "wpa-none", GSUPPLICANT_KEYMGMT_WPA_NONE },
{ "wps", GSUPPLICANT_KEYMGMT_WPS }
};
/*==========================================================================*
* Implementation
*==========================================================================*/
static
void
gsupplicant_call_cancelled(
GCancellable* cancel,
gpointer data)
{
GSupplicantCall* call = data;
GASSERT(call->supplicant);
GASSERT(call->cancel == cancel);
gsupplicant_unref(call->supplicant);
call->supplicant = NULL;
}
static
void
gsupplicant_call_finished(
GObject* proxy,
GAsyncResult* result,
gpointer data)
{
GSupplicantCall* call = data;
g_cancellable_disconnect(call->cancel, call->cancel_id);
if (!g_cancellable_is_cancelled(call->cancel)) {
GASSERT(call->supplicant);
call->finish(call->supplicant, call->cancel, result,
call->fn, call->data);
} else {
/* Generic cleanup */
GVariant* var = g_dbus_proxy_call_finish(G_DBUS_PROXY(proxy),
result, NULL);
if (var) {
g_variant_unref(var);
}
}
gsupplicant_unref(call->supplicant);
g_object_unref(call->cancel);
g_slice_free(GSupplicantCall, call);
}
static
GSupplicantCall*
gsupplicant_call_new(
GSupplicant* supplicant,
GSupplicantCallFinishFunc finish,
GCallback cb,
void* data)
{
GSupplicantCall* call = g_slice_new0(GSupplicantCall);
call->cancel = g_cancellable_new();
call->cancel_id = g_cancellable_connect(call->cancel,
G_CALLBACK(gsupplicant_call_cancelled), call, NULL);
call->supplicant = gsupplicant_ref(supplicant);
call->finish = finish;
call->fn.cb = cb;
call->data = data;
return call;
}
static
void
gsupplicant_call_finish_void(
GSupplicant* supplicant,
GCancellable* cancel,
GAsyncResult* result,
GSupplicantCallFuncUnion fn,
void* data)
{
GError* error = NULL;
GDBusProxy* proxy = G_DBUS_PROXY(supplicant->priv->proxy);
GVariant* var = g_dbus_proxy_call_finish(proxy, result, &error);
if (var) {
g_variant_unref(var);
}
if (fn.fn_void) {
fn.fn_void(supplicant, cancel, error, data);
}
if (error) {
g_error_free(error);
}
}
static
void
gsupplicant_call_finish_string(
GSupplicant* supplicant,
GCancellable* cancel,
GAsyncResult* result,
gboolean (*get_result)(
FiW1Wpa_supplicant1* proxy,
gchar** str,
GAsyncResult* res,
GError** error),
GSupplicantStringResultFunc fn,
void* data)
{
char* str = NULL;
GError* error = NULL;
GSupplicantPriv* priv = supplicant->priv;
get_result(priv->proxy, &str, result, &error);
if (fn) {
fn(supplicant, cancel, error, str, data);
}
if (error) {
g_error_free(error);
}
g_free(str);
}
static
void
gsupplicant_call_finish_create_interface(
GSupplicant* supplicant,
GCancellable* cancel,
GAsyncResult* result,
GSupplicantCallFuncUnion fn,
void* data)
{
gsupplicant_call_finish_string(supplicant, cancel, result,
fi_w1_wpa_supplicant1_call_create_interface_finish, fn.fn_string, data);
}
static
void
gsupplicant_call_finish_get_interface(
GSupplicant* supplicant,
GCancellable* cancel,
GAsyncResult* result,
GSupplicantCallFuncUnion fn,
void* data)
{
gsupplicant_call_finish_string(supplicant, cancel, result,
fi_w1_wpa_supplicant1_call_get_interface_finish, fn.fn_string, data);
}
static inline
GSUPPLICANT_PROPERTY
gsupplicant_property_from_signal(
GSUPPLICANT_SIGNAL sig)
{
switch (sig) {
#define SIGNAL_PROPERTY_MAP_(P,p) \
case SIGNAL_##P##_CHANGED: return GSUPPLICANT_PROPERTY_##P;
GSUPPLICANT_PROPERTIES_(SIGNAL_PROPERTY_MAP_)
#undef SIGNAL_PROPERTY_MAP_
default: /* unreachable */ return GSUPPLICANT_PROPERTY_ANY;
}
}
static
void
gsupplicant_signal_property_change(
GSupplicant* self,
GSUPPLICANT_SIGNAL sig,
GSUPPLICANT_PROPERTY prop)
{
GSupplicantPriv* priv = self->priv;
GASSERT(prop > GSUPPLICANT_PROPERTY_ANY);
GASSERT(prop < GSUPPLICANT_PROPERTY_COUNT);
GASSERT(sig < G_N_ELEMENTS(gsupplicant_property_quarks));
if (!gsupplicant_property_quarks[sig]) {
char buf[SIGNAL_PROPERTY_CHANGED_DETAIL_MAX_LEN + 1];
snprintf(buf, sizeof(buf), SIGNAL_PROPERTY_CHANGED_DETAIL, prop);
buf[sizeof(buf)-1] = 0;
gsupplicant_property_quarks[sig] = g_quark_from_string(buf);
}
priv->pending_signals &= ~(1 << sig);
g_signal_emit(self, gsupplicant_signals[sig], 0);
g_signal_emit(self, gsupplicant_signals[SIGNAL_PROPERTY_CHANGED],
gsupplicant_property_quarks[sig], prop);
}
static
void
gsupplicant_emit_pending_signals(
GSupplicant* self)
{
GSupplicantPriv* priv = self->priv;
GSUPPLICANT_SIGNAL sig;
gboolean valid_changed;
/* Handlers could drop their references to us */
gsupplicant_ref(self);
/* VALID is the last one to be emitted if we BECOME valid */
if ((priv->pending_signals & SIGNAL_BIT(VALID)) && self->valid) {
priv->pending_signals &= ~SIGNAL_BIT(VALID);
valid_changed = TRUE;
} else {
valid_changed = FALSE;
}
/* Emit the signals. Not that in case if valid has become FALSE, then
* VALID is emitted first, otherwise it's emitted last */
for (sig = SIGNAL_VALID_CHANGED;
sig < SIGNAL_PROPERTY_CHANGED && priv->pending_signals;
sig++) {
if (priv->pending_signals & (1 << sig)) {
gsupplicant_signal_property_change(self, sig,
gsupplicant_property_from_signal(sig));
}
}
/* Then emit VALID if valid has become TRUE */
if (valid_changed) {
gsupplicant_signal_property_change(self,
SIGNAL_VALID_CHANGED, GSUPPLICANT_PROPERTY_VALID);
}
/* And release the temporary reference */
gsupplicant_unref(self);
}
static
guint
gsupplicant_convert_to_bitmask(
const gchar* const* values,
const GSupNameIntPair* list,
size_t count)
{
guint mask = 0;
if (values) {
const gchar* const* name = values;
while (*name) {
const GSupNameIntPair* pair =
gsupplicant_name_int_find_name(*name, list, count);
if (pair) mask |= pair->value;
name++;
}
}
return mask;
}
static
guint
gsupplicant_get_capabilities(
GSupplicant* self)
{
return self->valid ? gsupplicant_convert_to_bitmask(
fi_w1_wpa_supplicant1_get_capabilities(self->priv->proxy),
gsupplicant_caps, G_N_ELEMENTS(gsupplicant_caps)) : 0;
}
static
guint
gsupplicant_get_eap_methods(
GSupplicant* self)
{
return self->valid ? gsupplicant_convert_to_bitmask(
fi_w1_wpa_supplicant1_get_eap_methods(self->priv->proxy),
gsupplicant_eap_methods, G_N_ELEMENTS(gsupplicant_eap_methods)) : 0;
}
static
void
gsupplicant_update_name_owner(
GSupplicant* self)
{
GSupplicantPriv* priv = self->priv;
char* owner = g_dbus_proxy_get_name_owner(G_DBUS_PROXY(priv->proxy));
const gboolean valid = (owner != NULL);
g_free(owner);
if (self->valid != valid) {
self->valid = valid;
priv->pending_signals |= SIGNAL_BIT(VALID);
}
}
static
void
gsupplicant_update_interfaces(
GSupplicant* self)
{
GSupplicantPriv* priv = self->priv;
gchar** interfaces = (char**)(self->valid ?
fi_w1_wpa_supplicant1_get_interfaces(priv->proxy) : NULL);
/* If the stub is generated by gdbus-codegen < 2.56 then the getting
* returns shallow copy, i.e. the return result should be released
* with g_free(), but the individual strings must not be modified. */
if (!gutil_strv_equal((const GStrV*)interfaces, priv->interfaces)) {
#if STRV_GETTERS_RETURN_SHALLOW_COPY
GStrV* ptr;
for (ptr = interfaces; *ptr; ptr++) {
*ptr = g_strdup(*ptr);
}
#else
interfaces = g_strdupv(interfaces);
#endif
g_strfreev(priv->interfaces);
self->interfaces = priv->interfaces = interfaces;
priv->pending_signals |= SIGNAL_BIT(INTERFACES);
}
#if STRV_GETTERS_RETURN_SHALLOW_COPY
else {
g_free(interfaces);
}
#endif
}
static
void
gsupplicant_update_capabilities(
GSupplicant* self)
{
const guint caps = gsupplicant_get_capabilities(self);
if (self->caps != caps) {
self->caps = caps;
self->priv->pending_signals |= SIGNAL_BIT(CAPABILITIES);
}
}
static
void
gsupplicant_update_eap_methods(
GSupplicant* self)
{
const guint methods = gsupplicant_get_eap_methods(self);
if (self->eap_methods != methods) {
self->eap_methods = methods;
self->priv->pending_signals |= SIGNAL_BIT(EAP_METHODS);
}
}
static
void
gsupplicant_notify_name_owner(
FiW1Wpa_supplicant1* proxy,
GParamSpec* param,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
gsupplicant_update_name_owner(self);
gsupplicant_emit_pending_signals(self);
}
static
void
gsupplicant_notify_capabilities(
FiW1Wpa_supplicant1* proxy,
GParamSpec* param,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
gsupplicant_update_capabilities(self);
gsupplicant_emit_pending_signals(self);
}
static
void
gsupplicant_notify_eap_methods(
FiW1Wpa_supplicant1* proxy,
GParamSpec* param,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
gsupplicant_update_eap_methods(self);
gsupplicant_emit_pending_signals(self);
}
static
void
gsupplicant_notify_interfaces(
FiW1Wpa_supplicant1* proxy,
GParamSpec* param,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
gsupplicant_update_interfaces(self);
gsupplicant_emit_pending_signals(self);
}
static
void
gsupplicant_proxy_interface_added(
GDBusProxy* proxy,
const char* path,
GVariant* properties,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
GSupplicantPriv* priv = self->priv;
GDEBUG("Interface added: %s", path);
if (!gutil_strv_contains(priv->interfaces, path)) {
self->interfaces = priv->interfaces =
gutil_strv_add(priv->interfaces, path);
priv->pending_signals |= SIGNAL_BIT(INTERFACES);
gsupplicant_emit_pending_signals(self);
}
}
static
void
gsupplicant_proxy_interface_removed(
GDBusProxy* proxy,
const char* path,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
GSupplicantPriv* priv = self->priv;
const int pos = gutil_strv_find(priv->interfaces, path);
GDEBUG("Interface removed: %s", path);
if (pos >= 0) {
self->interfaces = priv->interfaces =
gutil_strv_remove_at(priv->interfaces, pos, TRUE);
priv->pending_signals |= SIGNAL_BIT(INTERFACES);
gsupplicant_emit_pending_signals(self);
}
}
static
void
gsupplicant_proxy_created(
GObject* bus,
GAsyncResult* result,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
GSupplicantPriv* priv = self->priv;
GError* error = NULL;
FiW1Wpa_supplicant1* proxy = fi_w1_wpa_supplicant1_proxy_new_finish(
result, &error);
if (proxy) {
GASSERT(!priv->proxy);
GASSERT(!self->valid);
priv->proxy = proxy;
priv->proxy_handler_id[PROXY_INTERFACE_ADDED] =
g_signal_connect(proxy, "interface-added",
G_CALLBACK(gsupplicant_proxy_interface_added), self);
priv->proxy_handler_id[PROXY_INTERFACE_REMOVED] =
g_signal_connect(proxy, "interface-removed",
G_CALLBACK(gsupplicant_proxy_interface_removed), self);
priv->proxy_handler_id[PROXY_NOTIFY_NAME_OWNER] =
g_signal_connect(priv->proxy, "notify::g-name-owner",
G_CALLBACK(gsupplicant_notify_name_owner), self);
priv->proxy_handler_id[PROXY_NOTIFY_CAPABILITIES] =
g_signal_connect(priv->proxy, "notify::capabilities",
G_CALLBACK(gsupplicant_notify_capabilities), self);
priv->proxy_handler_id[PROXY_NOTIFY_EAP_METHODS] =
g_signal_connect(priv->proxy, "notify::eap-methods",
G_CALLBACK(gsupplicant_notify_eap_methods), self);
priv->proxy_handler_id[PROXY_NOTIFY_INTERFACES] =
g_signal_connect(priv->proxy, "notify::interfaces",
G_CALLBACK(gsupplicant_notify_interfaces), self);
gsupplicant_update_name_owner(self);
gsupplicant_update_capabilities(self);
gsupplicant_update_eap_methods(self);
gsupplicant_update_interfaces(self);
gsupplicant_emit_pending_signals(self);
} else {
GERR("%s", GERRMSG(error));
g_error_free(error);
}
gsupplicant_unref(self);
}
static
void
gsupplicant_bus_get_finished(
GObject* object,
GAsyncResult* result,
gpointer data)
{
GSupplicant* self = GSUPPLICANT(data);
GSupplicantPriv* priv = self->priv;
GError* error = NULL;
priv->bus = g_bus_get_finish(result, &error);
if (priv->bus) {
GDEBUG("Bus connected");
/* Start the initialization sequence */
fi_w1_wpa_supplicant1_proxy_new(priv->bus, G_DBUS_PROXY_FLAGS_NONE,
GSUPPLICANT_SERVICE, GSUPPLICANT_PATH, NULL,
gsupplicant_proxy_created, gsupplicant_ref(self));
} else {
GERR("Failed to attach to system bus: %s", GERRMSG(error));
g_error_free(error);
}
gsupplicant_unref(self);
}
/*==========================================================================*
* API
*==========================================================================*/
GSupplicant*
gsupplicant_new()
{
/* Weak reference to the single instance of GSupplicant */
static GSupplicant* gsupplicant_instance = NULL;
if (gsupplicant_instance) {
gsupplicant_ref(gsupplicant_instance);
} else {
gsupplicant_instance = g_object_new(GSUPPLICANT_TYPE, NULL);
g_bus_get(GSUPPLICANT_BUS_TYPE, NULL, gsupplicant_bus_get_finished,
gsupplicant_ref(gsupplicant_instance));
g_object_add_weak_pointer(G_OBJECT(gsupplicant_instance),
(gpointer*)(&gsupplicant_instance));
}
return gsupplicant_instance;
}
GSupplicant*
gsupplicant_ref(
GSupplicant* self)
{
if (G_LIKELY(self)) {
g_object_ref(GSUPPLICANT(self));
return self;
} else {
return NULL;
}
}
void
gsupplicant_unref(
GSupplicant* self)
{
if (G_LIKELY(self)) {
g_object_unref(GSUPPLICANT(self));
}
}
gulong
gsupplicant_add_property_changed_handler(
GSupplicant* self,
GSUPPLICANT_PROPERTY property,
GSupplicantPropertyFunc fn,
void* data)
{
if (G_LIKELY(self) && G_LIKELY(fn)) {
const char* signal_name;
char buf[sizeof(SIGNAL_PROPERTY_CHANGED_NAME) + 2 +
SIGNAL_PROPERTY_CHANGED_DETAIL_MAX_LEN];
if (property) {
snprintf(buf, sizeof(buf), SIGNAL_PROPERTY_CHANGED_NAME "::"
SIGNAL_PROPERTY_CHANGED_DETAIL, property);
buf[sizeof(buf)-1] = 0;
signal_name = buf;
} else {
signal_name = SIGNAL_PROPERTY_CHANGED_NAME;
}
return g_signal_connect(self, signal_name, G_CALLBACK(fn), data);
}
return 0;
}
gulong
gsupplicant_add_handler(
GSupplicant* self,
GSUPPLICANT_PROPERTY prop,
GSupplicantFunc fn,
void* data)
{
if (G_LIKELY(self) && G_LIKELY(fn)) {
const char* signame;
switch (prop) {
#define SIGNAL_NAME_(P,p) case GSUPPLICANT_PROPERTY_##P: \
signame = gsupplicant_signame[SIGNAL_##P##_CHANGED]; \
break;
GSUPPLICANT_PROPERTIES_(SIGNAL_NAME_)
default:
signame = NULL;
break;
}
if (G_LIKELY(signame)) {
return g_signal_connect(self, signame, G_CALLBACK(fn), data);
}
}
return 0;
}
void
gsupplicant_remove_handler(
GSupplicant* self,
gulong id)
{
if (G_LIKELY(self) && G_LIKELY(id)) {
g_signal_handler_disconnect(self, id);
}
}
void
gsupplicant_remove_handlers(
GSupplicant* self,
gulong* ids,
guint count)
{
gutil_disconnect_handlers(self, ids, count);
}
GCancellable*
gsupplicant_create_interface(
GSupplicant* self,
const GSupplicantCreateInterfaceParams* params,
GSupplicantStringResultFunc fn,
void* data)
{
if (G_LIKELY(self) && self->valid && params && params->ifname) {
GSupplicantPriv* priv = self->priv;
GSupplicantCall* call = gsupplicant_call_new(self,
gsupplicant_call_finish_create_interface, G_CALLBACK(fn), data);
GVariantBuilder builder;
GVariant* dict;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
gsupplicant_dict_add_string(&builder, "Ifname", params->ifname);
gsupplicant_dict_add_string0(&builder, "BridgeIfname",
params->bridge_ifname);
gsupplicant_dict_add_string0(&builder, "Driver", params->driver);
gsupplicant_dict_add_string0(&builder, "ConfigFile",
params->config_file);
dict = g_variant_ref_sink(g_variant_builder_end(&builder));
fi_w1_wpa_supplicant1_call_create_interface(priv->proxy, dict,
call->cancel, gsupplicant_call_finished, call);
g_variant_unref(dict);
return call->cancel;
}
return NULL;
}
GCancellable*
gsupplicant_remove_interface(
GSupplicant* self,
const char* path,
GSupplicantResultFunc fn,
void* data)
{
if (G_LIKELY(self) && self->valid &&
path && g_variant_is_object_path(path)) {
GSupplicantPriv* priv = self->priv;
GSupplicantCall* call = gsupplicant_call_new(self,
gsupplicant_call_finish_void, G_CALLBACK(fn), data);
fi_w1_wpa_supplicant1_call_remove_interface(priv->proxy, path,
call->cancel, gsupplicant_call_finished, call);
return call->cancel;
}
return NULL;
}
GCancellable*
gsupplicant_get_interface(
GSupplicant* self,
const char* ifname,
GSupplicantStringResultFunc fn,
void* data)
{
if (G_LIKELY(self) && self->valid) {
GSupplicantPriv* priv = self->priv;
GSupplicantCall* call = gsupplicant_call_new(self,
gsupplicant_call_finish_get_interface, G_CALLBACK(fn), data);
fi_w1_wpa_supplicant1_call_get_interface(priv->proxy, ifname,
call->cancel, gsupplicant_call_finished, call);
return call->cancel;
}
return NULL;
}
const char*
gsupplicant_caps_name(
guint caps,
guint* cap)
{
return gsupplicant_name_int_find_bit(caps, cap,
gsupplicant_caps, G_N_ELEMENTS(gsupplicant_caps));
}
const char*
gsupplicant_eap_method_name(
guint methods,
guint* method)
{
return gsupplicant_name_int_find_bit(methods, method,
gsupplicant_eap_methods, G_N_ELEMENTS(gsupplicant_eap_methods));
}
const char*
gsupplicant_cipher_suite_name(
guint cipher_suites,
guint* cipher_suite)
{
return gsupplicant_name_int_find_bit(cipher_suites, cipher_suite,
gsupplicant_cipher_suites, G_N_ELEMENTS(gsupplicant_cipher_suites));
}
const char*
gsupplicant_keymgmt_suite_name(
guint keymgmt_suites,
guint* keymgmt_suite)
{
return gsupplicant_name_int_find_bit(keymgmt_suites, keymgmt_suite,
gsupplicant_keymgmt_suites, G_N_ELEMENTS(gsupplicant_keymgmt_suites));
}
/*==========================================================================*
* Internals
*==========================================================================*/
/**
* Per instance initializer
*/
static
void
gsupplicant_init(
GSupplicant* self)
{
GSupplicantPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
GSUPPLICANT_TYPE, GSupplicantPriv);
self->priv = priv;
}
/**
* Final stage of deinitialization
*/
static
void
gsupplicant_finalize(
GObject* object)
{
GSupplicant* self = GSUPPLICANT(object);
GSupplicantPriv* priv = self->priv;
GVERBOSE_("");
if (priv->proxy) {
gutil_disconnect_handlers(priv->proxy, priv->proxy_handler_id,
G_N_ELEMENTS(priv->proxy_handler_id));
g_object_unref(priv->proxy);
}
if (priv->bus) {
g_dbus_connection_flush_sync(priv->bus, NULL, NULL);
g_object_unref(priv->bus);
}
g_strfreev(priv->interfaces);
G_OBJECT_CLASS(gsupplicant_parent_class)->finalize(object);
}
/**
* Per class initializer
*/
static
void
gsupplicant_class_init(
GSupplicantClass* klass)
{
int i;
G_OBJECT_CLASS(klass)->finalize = gsupplicant_finalize;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_type_class_add_private(klass, sizeof(GSupplicantPriv));
G_GNUC_END_IGNORE_DEPRECATIONS
for (i=0; i<SIGNAL_PROPERTY_CHANGED; i++) {
gsupplicant_signals[i] = g_signal_new(gsupplicant_signame[i],
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
gsupplicant_signals[SIGNAL_PROPERTY_CHANGED] =
g_signal_new(SIGNAL_PROPERTY_CHANGED_NAME, G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED, 0, NULL, NULL, NULL,
G_TYPE_NONE, 1, G_TYPE_UINT);
/* Register errors with gio */
gsupplicant_error_quark();
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/