diff options
-rw-r--r-- | src/include/gnunet_time_lib.h | 31 | ||||
-rw-r--r-- | src/include/gnunet_transport_communication_service.h | 39 | ||||
-rw-r--r-- | src/util/disk.c | 6 | ||||
-rw-r--r-- | src/util/gnunet-service-resolver.c | 1 | ||||
-rw-r--r-- | src/util/test_regex.c | 2 | ||||
-rw-r--r-- | src/util/test_time.c | 25 | ||||
-rw-r--r-- | src/util/test_tun.c | 2 | ||||
-rw-r--r-- | src/util/time.c | 126 |
8 files changed, 220 insertions, 12 deletions
diff --git a/src/include/gnunet_time_lib.h b/src/include/gnunet_time_lib.h index c7a06ba23..674a95534 100644 --- a/src/include/gnunet_time_lib.h +++ b/src/include/gnunet_time_lib.h | |||
@@ -365,7 +365,8 @@ GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future); | |||
365 | * assuming it continues at the same speed | 365 | * assuming it continues at the same speed |
366 | */ | 366 | */ |
367 | struct GNUNET_TIME_Relative | 367 | struct GNUNET_TIME_Relative |
368 | GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, | 368 | GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, |
369 | uint64_t finished, | ||
369 | uint64_t total); | 370 | uint64_t total); |
370 | 371 | ||
371 | 372 | ||
@@ -565,6 +566,34 @@ unsigned int | |||
565 | GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at); | 566 | GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at); |
566 | 567 | ||
567 | 568 | ||
569 | /** | ||
570 | * A configuration object. | ||
571 | */ | ||
572 | struct GNUNET_CONFIGURATION_Handle; | ||
573 | |||
574 | |||
575 | /** | ||
576 | * Obtain the current time and make sure it is monotonically | ||
577 | * increasing. Guards against systems without an RTC or | ||
578 | * clocks running backwards and other nasty surprises. Does | ||
579 | * not guarantee that the returned time is near the current | ||
580 | * time returned by #GNUNET_TIME_absolute_get(). Two | ||
581 | * subsequent calls (within a short time period) may return the | ||
582 | * same value. Persists the last returned time on disk to | ||
583 | * ensure that time never goes backwards. As a result, the | ||
584 | * resulting value can be used to check if a message is the | ||
585 | * "most recent" value and replays of older messages (from | ||
586 | * the same origin) would be discarded. | ||
587 | * | ||
588 | * @param cfg configuration, used to determine where to | ||
589 | * store the time; user can also insist RTC is working | ||
590 | * nicely and disable the feature | ||
591 | * @return monotonically increasing time | ||
592 | */ | ||
593 | struct GNUNET_TIME_Absolute | ||
594 | GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg); | ||
595 | |||
596 | |||
568 | #if 0 /* keep Emacsens' auto-indent happy */ | 597 | #if 0 /* keep Emacsens' auto-indent happy */ |
569 | { | 598 | { |
570 | #endif | 599 | #endif |
diff --git a/src/include/gnunet_transport_communication_service.h b/src/include/gnunet_transport_communication_service.h index f81ee6d9a..8cf275660 100644 --- a/src/include/gnunet_transport_communication_service.h +++ b/src/include/gnunet_transport_communication_service.h | |||
@@ -103,6 +103,19 @@ enum GNUNET_TRANSPORT_CommunicatorCharacteristics { | |||
103 | 103 | ||
104 | 104 | ||
105 | /** | 105 | /** |
106 | * Function called when the transport service has received an | ||
107 | * acknowledgement for this communicator (!) via a different return | ||
108 | * path. | ||
109 | */ | ||
110 | typedef void | ||
111 | (*GNUNET_TRANSPORT_CommunicatorNotify) (void *cls, | ||
112 | const struct GNUNET_PeerIdentity *sender, | ||
113 | struct GNUNET_TIME_Absolute monotonic_time, | ||
114 | struct GNUNET_TIME_Relative validity, | ||
115 | const struct GNUNET_HashCode *token | ||
116 | ); | ||
117 | |||
118 | /** | ||
106 | * Connect to the transport service. | 119 | * Connect to the transport service. |
107 | * | 120 | * |
108 | * @param cfg configuration to use | 121 | * @param cfg configuration to use |
@@ -122,7 +135,9 @@ GNUNET_TRANSPORT_communicator_connect (const struct GNUNET_CONFIGURATION_Handle | |||
122 | const char *addr_prefix, | 135 | const char *addr_prefix, |
123 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, | 136 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, |
124 | GNUNET_TRANSPORT_CommunicatorMqInit mq_init, | 137 | GNUNET_TRANSPORT_CommunicatorMqInit mq_init, |
125 | void *mq_init_cls); | 138 | void *mq_init_cls, |
139 | GNUNET_TRANSPORT_CommunicatorNotify notify_cb, | ||
140 | void *notify_cb_cls); | ||
126 | 141 | ||
127 | 142 | ||
128 | /** | 143 | /** |
@@ -275,6 +290,28 @@ void | |||
275 | GNUNET_TRANSPORT_communicator_address_remove (struct GNUNET_TRANSPORT_AddressIdentifier *ai); | 290 | GNUNET_TRANSPORT_communicator_address_remove (struct GNUNET_TRANSPORT_AddressIdentifier *ai); |
276 | 291 | ||
277 | 292 | ||
293 | /** | ||
294 | * The communicator asks the transport service to route a message via | ||
295 | * a different path to another communicator service at another peer. | ||
296 | * This must only be done for special control traffic (as there is no | ||
297 | * flow control for this API), such as acknowledgements, and generally | ||
298 | * only be done if the communicator is uni-directional (i.e. cannot | ||
299 | * send the message back itself). | ||
300 | * | ||
301 | * @param ch handle of this communicator | ||
302 | * @param target_pid peer to send the message to | ||
303 | * @param target_comm name of the communicator to send the message to | ||
304 | */ | ||
305 | void | ||
306 | GNUNET_TRANSPORT_communicator_notify (struct GNUNET_TRANSPORT_CommunicatorHandle *ch, | ||
307 | const struct GNUNET_PeerIdentity *pid, | ||
308 | const char *comm, | ||
309 | struct GNUNET_TIME_Absolute monotonic_time, | ||
310 | struct GNUNET_TIME_Relative validity, | ||
311 | const struct GNUNET_HashCode *token | ||
312 | ); | ||
313 | |||
314 | |||
278 | #if 0 /* keep Emacsens' auto-indent happy */ | 315 | #if 0 /* keep Emacsens' auto-indent happy */ |
279 | { | 316 | { |
280 | #endif | 317 | #endif |
diff --git a/src/util/disk.c b/src/util/disk.c index dc38d1137..d723303d8 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -236,7 +236,8 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, | |||
236 | * @return the new position on success, #GNUNET_SYSERR otherwise | 236 | * @return the new position on success, #GNUNET_SYSERR otherwise |
237 | */ | 237 | */ |
238 | off_t | 238 | off_t |
239 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, | 239 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, |
240 | off_t offset, | ||
240 | enum GNUNET_DISK_Seek whence) | 241 | enum GNUNET_DISK_Seek whence) |
241 | { | 242 | { |
242 | if (h == NULL) | 243 | if (h == NULL) |
@@ -1034,7 +1035,8 @@ GNUNET_DISK_fn_read (const char *fn, | |||
1034 | */ | 1035 | */ |
1035 | ssize_t | 1036 | ssize_t |
1036 | GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | 1037 | GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, |
1037 | const void *buffer, size_t n) | 1038 | const void *buffer, |
1039 | size_t n) | ||
1038 | { | 1040 | { |
1039 | if (NULL == h) | 1041 | if (NULL == h) |
1040 | { | 1042 | { |
diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c index 065a224eb..5f8957d8c 100644 --- a/src/util/gnunet-service-resolver.c +++ b/src/util/gnunet-service-resolver.c | |||
@@ -1123,7 +1123,6 @@ check_get (void *cls, | |||
1123 | 1123 | ||
1124 | (void) cls; | 1124 | (void) cls; |
1125 | size = ntohs (get->header.size) - sizeof (*get); | 1125 | size = ntohs (get->header.size) - sizeof (*get); |
1126 | const char *str = &get[1]; | ||
1127 | direction = ntohl (get->direction); | 1126 | direction = ntohl (get->direction); |
1128 | if (GNUNET_NO == direction) | 1127 | if (GNUNET_NO == direction) |
1129 | { | 1128 | { |
diff --git a/src/util/test_regex.c b/src/util/test_regex.c index 2e7d52828..4eb7cf87d 100644 --- a/src/util/test_regex.c +++ b/src/util/test_regex.c | |||
@@ -21,7 +21,7 @@ | |||
21 | * @author Maximilian Szengel | 21 | * @author Maximilian Szengel |
22 | */ | 22 | */ |
23 | #include "platform.h" | 23 | #include "platform.h" |
24 | #include "gnunet_tun_lib.h" | 24 | #include "gnunet_util_lib.h" |
25 | 25 | ||
26 | /** | 26 | /** |
27 | * 'wildcard', matches all possible values (for HEX encoding). | 27 | * 'wildcard', matches all possible values (for HEX encoding). |
diff --git a/src/util/test_time.c b/src/util/test_time.c index 2ac93e01d..80c4e5337 100644 --- a/src/util/test_time.c +++ b/src/util/test_time.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2001-2013 GNUnet e.V. | 3 | Copyright (C) 2001-2013, 2018 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -223,7 +223,28 @@ main (int argc, char *argv[]) | |||
223 | /*check GNUNET_TIME_absolute_max */ | 223 | /*check GNUNET_TIME_absolute_max */ |
224 | GNUNET_assert (now.abs_value_us == | 224 | GNUNET_assert (now.abs_value_us == |
225 | GNUNET_TIME_absolute_max (now, last).abs_value_us); | 225 | GNUNET_TIME_absolute_max (now, last).abs_value_us); |
226 | 226 | for (unsigned int i=0;i<30;i++) | |
227 | { | ||
228 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
229 | |||
230 | cfg = GNUNET_CONFIGURATION_create (); | ||
231 | last = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
232 | now = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
233 | GNUNET_assert (now.abs_value_us > last.abs_value_us); | ||
234 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
235 | GNUNET_CONFIGURATION_set_value_string (cfg, | ||
236 | "util", | ||
237 | "MONOTONIC_TIME_FILENAME", | ||
238 | "monotonic-time.dat"); | ||
239 | last = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
240 | now = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
241 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
242 | GNUNET_assert (now.abs_value_us > last.abs_value_us); | ||
243 | GNUNET_CONFIGURATION_destroy (cfg); | ||
244 | } | ||
245 | GNUNET_break (GNUNET_OK == | ||
246 | GNUNET_DISK_directory_remove ("monotonic-time.dat")); | ||
247 | |||
227 | return 0; | 248 | return 0; |
228 | } | 249 | } |
229 | 250 | ||
diff --git a/src/util/test_tun.c b/src/util/test_tun.c index edbd4c05d..429678fd5 100644 --- a/src/util/test_tun.c +++ b/src/util/test_tun.c | |||
@@ -22,7 +22,7 @@ | |||
22 | * @author Christian Grothoff | 22 | * @author Christian Grothoff |
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_tun_lib.h" | 25 | #include "gnunet_util_lib.h" |
26 | 26 | ||
27 | static int ret; | 27 | static int ret; |
28 | 28 | ||
diff --git a/src/util/time.c b/src/util/time.c index b02c43c1b..46d3a2b65 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2001-2013 GNUnet e.V. | 3 | Copyright (C) 2001-2013, 2018 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -22,8 +22,7 @@ | |||
22 | * @brief functions for handling time and time arithmetic | 22 | * @brief functions for handling time and time arithmetic |
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_crypto_lib.h" | 25 | #include "gnunet_util_lib.h" |
26 | #include "gnunet_time_lib.h" | ||
27 | 26 | ||
28 | #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) | 27 | #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) |
29 | 28 | ||
@@ -757,6 +756,127 @@ GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIM | |||
757 | } | 756 | } |
758 | 757 | ||
759 | 758 | ||
759 | /** | ||
760 | * Obtain the current time and make sure it is monotonically | ||
761 | * increasing. Guards against systems without an RTC or | ||
762 | * clocks running backwards and other nasty surprises. Does | ||
763 | * not guarantee that the returned time is near the current | ||
764 | * time returned by #GNUNET_TIME_absolute_get(). Two | ||
765 | * subsequent calls (within a short time period) may return the | ||
766 | * same value. Persists the last returned time on disk to | ||
767 | * ensure that time never goes backwards. As a result, the | ||
768 | * resulting value can be used to check if a message is the | ||
769 | * "most recent" value and replays of older messages (from | ||
770 | * the same origin) would be discarded. | ||
771 | * | ||
772 | * @param cfg configuration, used to determine where to | ||
773 | * store the time; user can also insist RTC is working | ||
774 | * nicely and disable the feature | ||
775 | * @return monotonically increasing time | ||
776 | */ | ||
777 | struct GNUNET_TIME_Absolute | ||
778 | GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
779 | { | ||
780 | static const struct GNUNET_CONFIGURATION_Handle *last_cfg; | ||
781 | static struct GNUNET_TIME_Absolute last_time; | ||
782 | static struct GNUNET_DISK_MapHandle *map_handle; | ||
783 | static struct GNUNET_TIME_AbsoluteNBO *map; | ||
784 | struct GNUNET_TIME_Absolute now; | ||
785 | |||
786 | now = GNUNET_TIME_absolute_get (); | ||
787 | if (last_cfg != cfg) | ||
788 | { | ||
789 | char *filename; | ||
790 | |||
791 | if (NULL != map_handle) | ||
792 | { | ||
793 | GNUNET_DISK_file_unmap (map_handle); | ||
794 | map_handle = NULL; | ||
795 | } | ||
796 | map = NULL; | ||
797 | |||
798 | last_cfg = cfg; | ||
799 | if ( (NULL != cfg) && | ||
800 | (GNUNET_OK == | ||
801 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
802 | "util", | ||
803 | "MONOTONIC_TIME_FILENAME", | ||
804 | &filename)) ) | ||
805 | { | ||
806 | struct GNUNET_DISK_FileHandle *fh; | ||
807 | |||
808 | fh = GNUNET_DISK_file_open (filename, | ||
809 | GNUNET_DISK_OPEN_READWRITE | GNUNET_DISK_OPEN_CREATE, | ||
810 | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_WRITE | | ||
811 | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_GROUP_READ); | ||
812 | if (NULL == fh) | ||
813 | { | ||
814 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
815 | _("Failed to map `%s', cannot assure monotonic time!\n"), | ||
816 | filename); | ||
817 | } | ||
818 | else | ||
819 | { | ||
820 | off_t size; | ||
821 | |||
822 | size = 0; | ||
823 | GNUNET_break (GNUNET_OK == | ||
824 | GNUNET_DISK_file_handle_size (fh, | ||
825 | &size)); | ||
826 | if (size < sizeof (*map)) | ||
827 | { | ||
828 | struct GNUNET_TIME_AbsoluteNBO o; | ||
829 | |||
830 | o = GNUNET_TIME_absolute_hton (now); | ||
831 | if (sizeof (o) != | ||
832 | GNUNET_DISK_file_write (fh, | ||
833 | &o, | ||
834 | sizeof (o))) | ||
835 | size = 0; | ||
836 | else | ||
837 | size = sizeof (o); | ||
838 | } | ||
839 | if (size == sizeof (*map)) | ||
840 | { | ||
841 | map = GNUNET_DISK_file_map (fh, | ||
842 | &map_handle, | ||
843 | GNUNET_DISK_MAP_TYPE_READWRITE, | ||
844 | sizeof (*map)); | ||
845 | if (NULL == map) | ||
846 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
847 | _("Failed to map `%s', cannot assure monotonic time!\n"), | ||
848 | filename); | ||
849 | } | ||
850 | else | ||
851 | { | ||
852 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
853 | _("Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"), | ||
854 | filename); | ||
855 | } | ||
856 | } | ||
857 | GNUNET_DISK_file_close (fh); | ||
858 | GNUNET_free (filename); | ||
859 | } | ||
860 | } | ||
861 | if (NULL != map) | ||
862 | last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (*map), | ||
863 | last_time); | ||
864 | if (now.abs_value_us <= last_time.abs_value_us) | ||
865 | now.abs_value_us = last_time.abs_value_us+1; | ||
866 | last_time = now; | ||
867 | if (NULL != map) | ||
868 | *map = GNUNET_TIME_absolute_hton (now); | ||
869 | return now; | ||
870 | } | ||
871 | |||
760 | 872 | ||
873 | /** | ||
874 | * Destructor | ||
875 | */ | ||
876 | void __attribute__ ((destructor)) | ||
877 | GNUNET_util_time_fini () | ||
878 | { | ||
879 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
880 | } | ||
761 | 881 | ||
762 | /* end of time.c */ | 882 | /* end of time.c */ |