diff options
Diffstat (limited to 'src/lib/util')
-rw-r--r-- | src/lib/util/Makefile.am | 27 | ||||
-rw-r--r-- | src/lib/util/gnunet_crypto_dbus_lib_pop.c | 145 | ||||
-rw-r--r-- | src/lib/util/gnunet_crypto_dbus_lib_push.c | 54 | ||||
-rw-r--r-- | src/lib/util/gnunet_time_dbus_lib_pop.c | 52 | ||||
-rw-r--r-- | src/lib/util/gnunet_time_dbus_lib_push.c | 61 |
5 files changed, 339 insertions, 0 deletions
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am new file mode 100644 index 0000000..f7f6b7c --- /dev/null +++ b/src/lib/util/Makefile.am | |||
@@ -0,0 +1,27 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | lib_LTLIBRARIES = \ | ||
3 | libgnunettimedbus.la \ | ||
4 | libgnunetcryptodbus.la | ||
5 | |||
6 | libgnunettimedbus_la_SOURCES = \ | ||
7 | gnunet_time_dbus_lib_pop.c \ | ||
8 | gnunet_time_dbus_lib_push.c | ||
9 | |||
10 | libgnunettimedbus_la_CFLAGS = \ | ||
11 | -I$(top_builddir)/src/lib/include \ | ||
12 | $(DBUS_CFLAGS) | ||
13 | |||
14 | libgnunettimedbus_la_LDFLAGS = \ | ||
15 | $(DBUS_LIBS) | ||
16 | |||
17 | libgnunetcryptodbus_la_SOURCES = \ | ||
18 | gnunet_crypto_dbus_lib_pop.c \ | ||
19 | gnunet_crypto_dbus_lib_push.c | ||
20 | |||
21 | libgnunetcryptodbus_la_CFLAGS = \ | ||
22 | -I$(top_builddir)/src/lib/include \ | ||
23 | $(DBUS_CFLAGS) | ||
24 | |||
25 | libgnunetcryptodbus_la_LDFLAGS = \ | ||
26 | $(DBUS_LIBS) | ||
27 | |||
diff --git a/src/lib/util/gnunet_crypto_dbus_lib_pop.c b/src/lib/util/gnunet_crypto_dbus_lib_pop.c new file mode 100644 index 0000000..a598f1d --- /dev/null +++ b/src/lib/util/gnunet_crypto_dbus_lib_pop.c | |||
@@ -0,0 +1,145 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include "gnunet_dbus_lib.h" | ||
4 | |||
5 | DBusMessage * | ||
6 | GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key ( | ||
7 | DBusMessage *message, | ||
8 | DBusMessageIter *iter, | ||
9 | const char *arg_name, | ||
10 | struct GNUNET_CRYPTO_EcdsaPublicKey *value) | ||
11 | { | ||
12 | DBusMessage *ret = NULL; | ||
13 | DBusMessageIter iter_sub; | ||
14 | DBusMessageIter iter_sub_sub; | ||
15 | |||
16 | ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); | ||
17 | if (ret) | ||
18 | return ret; | ||
19 | |||
20 | const char *encoded; | ||
21 | int success; | ||
22 | int arg_type = dbus_message_iter_get_arg_type (&iter_sub); | ||
23 | int element_type; | ||
24 | int n_elements; | ||
25 | unsigned char *marshalled_array; | ||
26 | switch (arg_type) | ||
27 | { | ||
28 | case DBUS_TYPE_STRING: | ||
29 | dbus_message_iter_get_basic (&iter_sub, &encoded); | ||
30 | success = GNUNET_CRYPTO_ecdsa_public_key_from_string (encoded, strlen (encoded), value); | ||
31 | if (GNUNET_OK != success) | ||
32 | { | ||
33 | return dbus_message_new_error_printf ( | ||
34 | message, | ||
35 | DBUS_ERROR_INVALID_ARGS, | ||
36 | "String is not a valid base32 encoded ECDSA public key for argument '%s'", | ||
37 | arg_name); | ||
38 | } | ||
39 | GNUNET_DBUS_message_set_pretty (message, true); | ||
40 | return NULL; | ||
41 | case DBUS_TYPE_ARRAY: | ||
42 | element_type = dbus_message_iter_get_element_type (&iter_sub); | ||
43 | if (DBUS_TYPE_BYTE == element_type) | ||
44 | { | ||
45 | dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); | ||
46 | dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); | ||
47 | if (sizeof (value->q_y) == n_elements) | ||
48 | { | ||
49 | memcpy (value->q_y, marshalled_array, n_elements); | ||
50 | return NULL; | ||
51 | }; | ||
52 | return dbus_message_new_error_printf ( | ||
53 | message, | ||
54 | DBUS_ERROR_INVALID_ARGS, | ||
55 | "ECDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", | ||
56 | arg_name, | ||
57 | n_elements); | ||
58 | }; | ||
59 | return dbus_message_new_error_printf ( | ||
60 | message, | ||
61 | DBUS_ERROR_INVALID_ARGS, | ||
62 | "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).", | ||
63 | arg_name, | ||
64 | GNUNET_DBUS_signature_typecode_to_string (element_type)); | ||
65 | default: | ||
66 | return dbus_message_new_error_printf ( | ||
67 | message, | ||
68 | DBUS_ERROR_INVALID_ARGS, | ||
69 | "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'", | ||
70 | arg_name, | ||
71 | GNUNET_DBUS_signature_typecode_to_string (arg_type)); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | DBusMessage * | ||
76 | GNUNET_CRYPTO_DBUS_pop_eddsa_public_key ( | ||
77 | DBusMessage *message, | ||
78 | DBusMessageIter *iter, | ||
79 | const char *arg_name, | ||
80 | struct GNUNET_CRYPTO_EddsaPublicKey *value) | ||
81 | { | ||
82 | DBusMessage *ret = NULL; | ||
83 | DBusMessageIter iter_sub; | ||
84 | DBusMessageIter iter_sub_sub; | ||
85 | |||
86 | ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); | ||
87 | if (ret) | ||
88 | return ret; | ||
89 | |||
90 | const char *encoded; | ||
91 | int success; | ||
92 | int arg_type = dbus_message_iter_get_arg_type (&iter_sub); | ||
93 | int element_type; | ||
94 | int n_elements; | ||
95 | unsigned char *marshalled_array; | ||
96 | switch (arg_type) | ||
97 | { | ||
98 | case DBUS_TYPE_STRING: | ||
99 | dbus_message_iter_get_basic (&iter_sub, &encoded); | ||
100 | success = GNUNET_CRYPTO_eddsa_public_key_from_string (encoded, strlen (encoded), value); | ||
101 | if (GNUNET_OK != success) | ||
102 | { | ||
103 | return dbus_message_new_error_printf ( | ||
104 | message, | ||
105 | DBUS_ERROR_INVALID_ARGS, | ||
106 | "String is not a valid base32 encoded EDDSA public key for argument '%s'", | ||
107 | arg_name); | ||
108 | } | ||
109 | GNUNET_DBUS_message_set_pretty (message, true); | ||
110 | return NULL; | ||
111 | case DBUS_TYPE_ARRAY: | ||
112 | element_type = dbus_message_iter_get_element_type (&iter_sub); | ||
113 | if (DBUS_TYPE_BYTE == element_type) | ||
114 | { | ||
115 | dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); | ||
116 | dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); | ||
117 | if (sizeof (value->q_y) == n_elements) | ||
118 | { | ||
119 | memcpy (value->q_y, marshalled_array, n_elements); | ||
120 | return NULL; | ||
121 | }; | ||
122 | return dbus_message_new_error_printf ( | ||
123 | message, | ||
124 | DBUS_ERROR_INVALID_ARGS, | ||
125 | "EDDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", | ||
126 | arg_name, | ||
127 | n_elements); | ||
128 | }; | ||
129 | return dbus_message_new_error_printf ( | ||
130 | message, | ||
131 | DBUS_ERROR_INVALID_ARGS, | ||
132 | "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an EDDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).", | ||
133 | arg_name, | ||
134 | GNUNET_DBUS_signature_typecode_to_string (element_type)); | ||
135 | default: | ||
136 | return dbus_message_new_error_printf ( | ||
137 | message, | ||
138 | DBUS_ERROR_INVALID_ARGS, | ||
139 | "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'", | ||
140 | arg_name, | ||
141 | GNUNET_DBUS_signature_typecode_to_string (arg_type)); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | |||
diff --git a/src/lib/util/gnunet_crypto_dbus_lib_push.c b/src/lib/util/gnunet_crypto_dbus_lib_push.c new file mode 100644 index 0000000..6e1cd3a --- /dev/null +++ b/src/lib/util/gnunet_crypto_dbus_lib_push.c | |||
@@ -0,0 +1,54 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include "gnunet_dbus_lib.h" | ||
4 | |||
5 | void | ||
6 | GNUNET_CRYPTO_DBUS_push_ecdsa_public_key ( | ||
7 | DBusMessage *message, | ||
8 | DBusMessageIter *iter, | ||
9 | const struct GNUNET_CRYPTO_EcdsaPublicKey *value) | ||
10 | { | ||
11 | DBusMessageIter iter_sub; | ||
12 | |||
13 | if (GNUNET_DBUS_message_get_pretty (message)) | ||
14 | { | ||
15 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); | ||
16 | char *encoded = GNUNET_CRYPTO_ecdsa_public_key_to_string (value); | ||
17 | const char *encoded_const = encoded; | ||
18 | GNUNET_DBUS_push_string (message, &iter_sub, &encoded_const); | ||
19 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
20 | GNUNET_free (encoded); | ||
21 | } | ||
22 | else | ||
23 | { | ||
24 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); | ||
25 | GNUNET_DBUS_push_byte_array (message, &iter_sub, value->q_y, 32); | ||
26 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
27 | }; | ||
28 | }; | ||
29 | |||
30 | void | ||
31 | GNUNET_CRYPTO_DBUS_push_eddsa_public_key ( | ||
32 | DBusMessage *message, | ||
33 | DBusMessageIter *iter, | ||
34 | const struct GNUNET_CRYPTO_EddsaPublicKey *value) | ||
35 | { | ||
36 | DBusMessageIter iter_sub; | ||
37 | |||
38 | if (GNUNET_DBUS_message_get_pretty (message)) | ||
39 | { | ||
40 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); | ||
41 | char *encoded = GNUNET_CRYPTO_eddsa_public_key_to_string (value); | ||
42 | const char *encoded_const = encoded; | ||
43 | GNUNET_DBUS_push_string (message, &iter_sub, &encoded_const); | ||
44 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
45 | GNUNET_free (encoded); | ||
46 | } | ||
47 | else | ||
48 | { | ||
49 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); | ||
50 | GNUNET_DBUS_push_byte_array (message, &iter_sub, value->q_y, 32); | ||
51 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
52 | }; | ||
53 | }; | ||
54 | |||
diff --git a/src/lib/util/gnunet_time_dbus_lib_pop.c b/src/lib/util/gnunet_time_dbus_lib_pop.c new file mode 100644 index 0000000..10322c6 --- /dev/null +++ b/src/lib/util/gnunet_time_dbus_lib_pop.c | |||
@@ -0,0 +1,52 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | #include <gnunet/gnunet_strings_lib.h> | ||
6 | |||
7 | #include "gnunet_time_dbus_lib.h" | ||
8 | |||
9 | DBusMessage * | ||
10 | GNUNET_TIME_DBUS_pop_absolute ( | ||
11 | DBusMessage *message, | ||
12 | DBusMessageIter *iter, | ||
13 | const char *arg_name, | ||
14 | struct GNUNET_TIME_Absolute *value) | ||
15 | { | ||
16 | DBusMessageIter iter_sub; | ||
17 | DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); | ||
18 | if (ret) | ||
19 | return ret; | ||
20 | |||
21 | int arg_type = dbus_message_iter_get_arg_type (&iter_sub); | ||
22 | switch (arg_type) | ||
23 | { | ||
24 | case DBUS_TYPE_STRING: { | ||
25 | const char *time; | ||
26 | dbus_message_iter_get_basic (&iter_sub, &time); | ||
27 | int err = GNUNET_STRINGS_fancy_time_to_absolute (time, value); | ||
28 | if (GNUNET_OK != err) | ||
29 | { | ||
30 | return dbus_message_new_error_printf ( | ||
31 | message, | ||
32 | DBUS_ERROR_INVALID_ARGS, | ||
33 | "Bad argument for '%s'. Malformed time string. GNUNET_STRINGS_fancy_time_to_absolute returned %d", | ||
34 | arg_name, | ||
35 | err); | ||
36 | }; | ||
37 | return NULL; | ||
38 | }; | ||
39 | case DBUS_TYPE_UINT64: | ||
40 | dbus_message_iter_get_basic (&iter_sub, &value->abs_value_us); | ||
41 | return NULL; | ||
42 | default: | ||
43 | return dbus_message_new_error_printf ( | ||
44 | message, | ||
45 | DBUS_ERROR_INVALID_ARGS, | ||
46 | "Bad argument for '%s'. Variant must contain a time encoded as a human readable string or a uint64 representing the number of microseconds since epoch. Contains %s.", | ||
47 | arg_name, | ||
48 | GNUNET_DBUS_signature_typecode_to_string (arg_type)); | ||
49 | }; | ||
50 | }; | ||
51 | |||
52 | |||
diff --git a/src/lib/util/gnunet_time_dbus_lib_push.c b/src/lib/util/gnunet_time_dbus_lib_push.c new file mode 100644 index 0000000..a618278 --- /dev/null +++ b/src/lib/util/gnunet_time_dbus_lib_push.c | |||
@@ -0,0 +1,61 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | #include <gnunet/gnunet_strings_lib.h> | ||
6 | |||
7 | #include "gnunet_time_dbus_lib.h" | ||
8 | |||
9 | #define LOG(kind, ...) GNUNET_log_from (kind, "time-dbus-push", __VA_ARGS__) | ||
10 | |||
11 | void | ||
12 | GNUNET_TIME_DBUS_push_absolute ( | ||
13 | DBusMessage *message, | ||
14 | DBusMessageIter *iter, | ||
15 | const struct GNUNET_TIME_Absolute *value) | ||
16 | { | ||
17 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing absolute time\n"); | ||
18 | DBusMessageIter iter_sub; | ||
19 | if (GNUNET_DBUS_message_get_pretty (message)) | ||
20 | { | ||
21 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as string\n"); | ||
22 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); | ||
23 | const char *stringified = GNUNET_STRINGS_absolute_time_to_string (*value); | ||
24 | GNUNET_DBUS_push_string (message, &iter_sub, &stringified); | ||
25 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
26 | } | ||
27 | else | ||
28 | { | ||
29 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as uint64\n"); | ||
30 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT64); | ||
31 | GNUNET_DBUS_push_uint64 (message, &iter_sub, &value->abs_value_us); | ||
32 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | void | ||
37 | GNUNET_TIME_DBUS_push_relative ( | ||
38 | DBusMessage *message, | ||
39 | DBusMessageIter *iter, | ||
40 | const struct GNUNET_TIME_Relative *value) | ||
41 | { | ||
42 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing relative time\n"); | ||
43 | DBusMessageIter iter_sub; | ||
44 | if (GNUNET_DBUS_message_get_pretty (message)) | ||
45 | { | ||
46 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as string\n"); | ||
47 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); | ||
48 | const char *stringified = GNUNET_STRINGS_relative_time_to_string (*value, 0); | ||
49 | GNUNET_DBUS_push_string (message, &iter_sub, &stringified); | ||
50 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
51 | } | ||
52 | else | ||
53 | { | ||
54 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as uint64\n"); | ||
55 | GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT64); | ||
56 | GNUNET_DBUS_push_uint64 (message, &iter_sub, &value->rel_value_us); | ||
57 | GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | |||