aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:07 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:12 +0100
commit99779b455ce3bf9c53dd411575766bf298a3a5f3 (patch)
tree12cd49f097fdd1947580b08ec10ca961f8c250b9 /src/util
parent8c702327dae9e504e0f6e1678884d9327321f44a (diff)
downloadgnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.tar.gz
gnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.zip
introducing GNUNET_TIME_Timestamp
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Makefile.am2
-rw-r--r--src/util/getopt_helpers.c249
-rw-r--r--src/util/strings.c22
-rw-r--r--src/util/time.c218
4 files changed, 288 insertions, 203 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 9fda40f51..406d42b1e 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -144,7 +144,7 @@ libgnunetutil_la_LIBADD = \
144 144
145libgnunetutil_la_LDFLAGS = \ 145libgnunetutil_la_LDFLAGS = \
146 $(GN_LIB_LDFLAGS) \ 146 $(GN_LIB_LDFLAGS) \
147 -version-info 14:0:0 147 -version-info 15:0:0
148 148
149GNUNET_ECC = gnunet-ecc 149GNUNET_ECC = gnunet-ecc
150GNUNET_SCRYPT = gnunet-scrypt 150GNUNET_SCRYPT = gnunet-scrypt
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index 592875531..917aa440b 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -38,7 +38,7 @@
38 * @param value not used (NULL) 38 * @param value not used (NULL)
39 * @return #GNUNET_NO (do not continue, not an error) 39 * @return #GNUNET_NO (do not continue, not an error)
40 */ 40 */
41static int 41static enum GNUNET_GenericReturnValue
42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
43 void *scls, 43 void *scls,
44 const char *option, 44 const char *option,
@@ -53,12 +53,6 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
53} 53}
54 54
55 55
56/**
57 * Define the option to print the version of
58 * the application (-v option)
59 *
60 * @param version string with the version number
61 */
62struct GNUNET_GETOPT_CommandLineOption 56struct GNUNET_GETOPT_CommandLineOption
63GNUNET_GETOPT_option_version (const char *version) 57GNUNET_GETOPT_option_version (const char *version)
64{ 58{
@@ -90,7 +84,7 @@ GNUNET_GETOPT_option_version (const char *version)
90 * @param value not used (NULL) 84 * @param value not used (NULL)
91 * @return #GNUNET_NO (do not continue, not an error) 85 * @return #GNUNET_NO (do not continue, not an error)
92 */ 86 */
93static int 87static enum GNUNET_GenericReturnValue
94format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 88format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
95 void *scls, 89 void *scls,
96 const char *option, 90 const char *option,
@@ -192,12 +186,6 @@ OUTER:
192} 186}
193 187
194 188
195/**
196 * Defining the option to print the command line
197 * help text (-h option).
198 *
199 * @param about string with brief description of the application
200 */
201struct GNUNET_GETOPT_CommandLineOption 189struct GNUNET_GETOPT_CommandLineOption
202GNUNET_GETOPT_option_help (const char *about) 190GNUNET_GETOPT_option_help (const char *about)
203{ 191{
@@ -229,7 +217,7 @@ GNUNET_GETOPT_option_help (const char *about)
229 * @param value not used (NULL) 217 * @param value not used (NULL)
230 * @return #GNUNET_OK 218 * @return #GNUNET_OK
231 */ 219 */
232static int 220static enum GNUNET_GenericReturnValue
233increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 221increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
234 void *scls, 222 void *scls,
235 const char *option, 223 const char *option,
@@ -245,15 +233,6 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
245} 233}
246 234
247 235
248/**
249 * Increment @a val each time the option flag is given by one.
250 *
251 * @param shortName short name of the option
252 * @param name long name of the option
253 * @param argumentHelp help text for the option argument
254 * @param description long help text for the option
255 * @param[out] val increment by 1 each time the option is present
256 */
257struct GNUNET_GETOPT_CommandLineOption 236struct GNUNET_GETOPT_CommandLineOption
258GNUNET_GETOPT_option_increment_uint (char shortName, 237GNUNET_GETOPT_option_increment_uint (char shortName,
259 const char *name, 238 const char *name,
@@ -272,12 +251,6 @@ GNUNET_GETOPT_option_increment_uint (char shortName,
272} 251}
273 252
274 253
275/**
276 * Define the '-V' verbosity option. Using the option more
277 * than once increments @a level each time.
278 *
279 * @param[out] level set to the verbosity level
280 */
281struct GNUNET_GETOPT_CommandLineOption 254struct GNUNET_GETOPT_CommandLineOption
282GNUNET_GETOPT_option_verbose (unsigned int *level) 255GNUNET_GETOPT_option_verbose (unsigned int *level)
283{ 256{
@@ -308,7 +281,7 @@ GNUNET_GETOPT_option_verbose (unsigned int *level)
308 * @param value not used (NULL) 281 * @param value not used (NULL)
309 * @return #GNUNET_OK 282 * @return #GNUNET_OK
310 */ 283 */
311static int 284static enum GNUNET_GenericReturnValue
312set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 285set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
313 void *scls, 286 void *scls,
314 const char *option, 287 const char *option,
@@ -324,16 +297,6 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
324} 297}
325 298
326 299
327/**
328 * Allow user to specify a flag (which internally means setting
329 * an integer to 1/#GNUNET_YES/#GNUNET_OK.
330 *
331 * @param shortName short name of the option
332 * @param name long name of the option
333 * @param argumentHelp help text for the option argument
334 * @param description long help text for the option
335 * @param[out] val set to 1 if the option is present
336 */
337struct GNUNET_GETOPT_CommandLineOption 300struct GNUNET_GETOPT_CommandLineOption
338GNUNET_GETOPT_option_flag (char shortName, 301GNUNET_GETOPT_option_flag (char shortName,
339 const char *name, 302 const char *name,
@@ -366,7 +329,7 @@ GNUNET_GETOPT_option_flag (char shortName,
366 * @param value actual value of the option (a string) 329 * @param value actual value of the option (a string)
367 * @return #GNUNET_OK 330 * @return #GNUNET_OK
368 */ 331 */
369static int 332static enum GNUNET_GenericReturnValue
370set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 333set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
371 void *scls, 334 void *scls,
372 const char *option, 335 const char *option,
@@ -383,15 +346,6 @@ set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
383} 346}
384 347
385 348
386/**
387 * Allow user to specify a string.
388 *
389 * @param shortName short name of the option
390 * @param name long name of the option
391 * @param argumentHelp help text for the option argument
392 * @param description long help text for the option
393 * @param[out] str set to the string
394 */
395struct GNUNET_GETOPT_CommandLineOption 349struct GNUNET_GETOPT_CommandLineOption
396GNUNET_GETOPT_option_string (char shortName, 350GNUNET_GETOPT_option_string (char shortName,
397 const char *name, 351 const char *name,
@@ -413,12 +367,6 @@ GNUNET_GETOPT_option_string (char shortName,
413} 367}
414 368
415 369
416/**
417 * Define the '-L' log level option. Note that we do not check
418 * that the log level is valid here.
419 *
420 * @param[out] level set to the log level
421 */
422struct GNUNET_GETOPT_CommandLineOption 370struct GNUNET_GETOPT_CommandLineOption
423GNUNET_GETOPT_option_loglevel (char **level) 371GNUNET_GETOPT_option_loglevel (char **level)
424{ 372{
@@ -447,7 +395,7 @@ GNUNET_GETOPT_option_loglevel (char **level)
447 * @param value actual value of the option (a string) 395 * @param value actual value of the option (a string)
448 * @return #GNUNET_OK 396 * @return #GNUNET_OK
449 */ 397 */
450static int 398static enum GNUNET_GenericReturnValue
451set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 399set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
452 void *scls, 400 void *scls,
453 const char *option, 401 const char *option,
@@ -464,15 +412,6 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
464} 412}
465 413
466 414
467/**
468 * Allow user to specify a filename (automatically path expanded).
469 *
470 * @param shortName short name of the option
471 * @param name long name of the option
472 * @param argumentHelp help text for the option argument
473 * @param description long help text for the option
474 * @param[out] str set to the string
475 */
476struct GNUNET_GETOPT_CommandLineOption 415struct GNUNET_GETOPT_CommandLineOption
477GNUNET_GETOPT_option_filename (char shortName, 416GNUNET_GETOPT_option_filename (char shortName,
478 const char *name, 417 const char *name,
@@ -494,11 +433,6 @@ GNUNET_GETOPT_option_filename (char shortName,
494} 433}
495 434
496 435
497/**
498 * Allow user to specify log file name (-l option)
499 *
500 * @param[out] logfn set to the name of the logfile
501 */
502struct GNUNET_GETOPT_CommandLineOption 436struct GNUNET_GETOPT_CommandLineOption
503GNUNET_GETOPT_option_logfile (char **logfn) 437GNUNET_GETOPT_option_logfile (char **logfn)
504{ 438{
@@ -517,11 +451,6 @@ GNUNET_GETOPT_option_logfile (char **logfn)
517} 451}
518 452
519 453
520/**
521 * Allow user to specify configuration file name (-c option)
522 *
523 * @param[out] fn set to the name of the configuration file
524 */
525struct GNUNET_GETOPT_CommandLineOption 454struct GNUNET_GETOPT_CommandLineOption
526GNUNET_GETOPT_option_cfgfile (char **fn) 455GNUNET_GETOPT_option_cfgfile (char **fn)
527{ 456{
@@ -552,7 +481,7 @@ GNUNET_GETOPT_option_cfgfile (char **fn)
552 * @param value actual value of the option as a string. 481 * @param value actual value of the option as a string.
553 * @return #GNUNET_OK if parsing the value worked 482 * @return #GNUNET_OK if parsing the value worked
554 */ 483 */
555static int 484static enum GNUNET_GenericReturnValue
556set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 485set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
557 void *scls, 486 void *scls,
558 const char *option, 487 const char *option,
@@ -573,15 +502,6 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
573} 502}
574 503
575 504
576/**
577 * Allow user to specify an `unsigned long long`
578 *
579 * @param shortName short name of the option
580 * @param name long name of the option
581 * @param argumentHelp help text for the option argument
582 * @param description long help text for the option
583 * @param[out] val set to the value specified at the command line
584 */
585struct GNUNET_GETOPT_CommandLineOption 505struct GNUNET_GETOPT_CommandLineOption
586GNUNET_GETOPT_option_ulong (char shortName, 506GNUNET_GETOPT_option_ulong (char shortName,
587 const char *name, 507 const char *name,
@@ -616,7 +536,7 @@ GNUNET_GETOPT_option_ulong (char shortName,
616 * @param value actual value of the option as a string. 536 * @param value actual value of the option as a string.
617 * @return #GNUNET_OK if parsing the value worked 537 * @return #GNUNET_OK if parsing the value worked
618 */ 538 */
619static int 539static enum GNUNET_GenericReturnValue
620set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 540set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
621 void *scls, 541 void *scls,
622 const char *option, 542 const char *option,
@@ -664,15 +584,6 @@ set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
664} 584}
665 585
666 586
667/**
668 * Allow user to specify a `long long` with an offset to add to the current
669 * system time to construct the time seen by the application. Used for
670 * debugging / testing.
671 *
672 * @param shortName short name of the option
673 * @param name long name of the option
674 * @param[out] val set to the time specified at the command line
675 */
676struct GNUNET_GETOPT_CommandLineOption 587struct GNUNET_GETOPT_CommandLineOption
677GNUNET_GETOPT_option_timetravel (char shortName, 588GNUNET_GETOPT_option_timetravel (char shortName,
678 const char *name) 589 const char *name)
@@ -684,8 +595,7 @@ GNUNET_GETOPT_option_timetravel (char shortName,
684 .description = _ ( 595 .description = _ (
685 "modify system time by given offset (for debugging/testing only)"), 596 "modify system time by given offset (for debugging/testing only)"),
686 .require_argument = 1, 597 .require_argument = 1,
687 .processor = 598 .processor = &set_timetravel_time
688 &set_timetravel_time
689 }; 599 };
690 600
691 return clo; 601 return clo;
@@ -705,7 +615,7 @@ GNUNET_GETOPT_option_timetravel (char shortName,
705 * @param value actual value of the option as a string. 615 * @param value actual value of the option as a string.
706 * @return #GNUNET_OK if parsing the value worked 616 * @return #GNUNET_OK if parsing the value worked
707 */ 617 */
708static int 618static enum GNUNET_GenericReturnValue
709set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 619set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
710 void *scls, 620 void *scls,
711 const char *option, 621 const char *option,
@@ -725,16 +635,6 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
725} 635}
726 636
727 637
728/**
729 * Allow user to specify a `struct GNUNET_TIME_Relative`
730 * (using human-readable "fancy" time).
731 *
732 * @param shortName short name of the option
733 * @param name long name of the option
734 * @param argumentHelp help text for the option argument
735 * @param description long help text for the option
736 * @param[out] val set to the time specified at the command line
737 */
738struct GNUNET_GETOPT_CommandLineOption 638struct GNUNET_GETOPT_CommandLineOption
739GNUNET_GETOPT_option_relative_time (char shortName, 639GNUNET_GETOPT_option_relative_time (char shortName,
740 const char *name, 640 const char *name,
@@ -748,8 +648,7 @@ GNUNET_GETOPT_option_relative_time (char shortName,
748 .argumentHelp = argumentHelp, 648 .argumentHelp = argumentHelp,
749 .description = description, 649 .description = description,
750 .require_argument = 1, 650 .require_argument = 1,
751 .processor = 651 .processor = &set_relative_time,
752 &set_relative_time,
753 .scls = (void *) val 652 .scls = (void *) val
754 }; 653 };
755 654
@@ -770,7 +669,7 @@ GNUNET_GETOPT_option_relative_time (char shortName,
770 * @param value actual value of the option as a string. 669 * @param value actual value of the option as a string.
771 * @return #GNUNET_OK if parsing the value worked 670 * @return #GNUNET_OK if parsing the value worked
772 */ 671 */
773static int 672static enum GNUNET_GenericReturnValue
774set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 673set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
775 void *scls, 674 void *scls,
776 const char *option, 675 const char *option,
@@ -790,16 +689,6 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
790} 689}
791 690
792 691
793/**
794 * Allow user to specify a `struct GNUNET_TIME_Absolute`
795 * (using human-readable "fancy" time).
796 *
797 * @param shortName short name of the option
798 * @param name long name of the option
799 * @param argumentHelp help text for the option argument
800 * @param description long help text for the option
801 * @param[out] val set to the time specified at the command line
802 */
803struct GNUNET_GETOPT_CommandLineOption 692struct GNUNET_GETOPT_CommandLineOption
804GNUNET_GETOPT_option_absolute_time (char shortName, 693GNUNET_GETOPT_option_absolute_time (char shortName,
805 const char *name, 694 const char *name,
@@ -813,8 +702,71 @@ GNUNET_GETOPT_option_absolute_time (char shortName,
813 .argumentHelp = argumentHelp, 702 .argumentHelp = argumentHelp,
814 .description = description, 703 .description = description,
815 .require_argument = 1, 704 .require_argument = 1,
816 .processor = 705 .processor = &set_absolute_time,
817 &set_absolute_time, 706 .scls = (void *) val
707 };
708
709 return clo;
710}
711
712
713/**
714 * Set an option of type 'struct GNUNET_TIME_Timestamp' from the command line.
715 * A pointer to this function should be passed as part of the
716 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
717 * of this type. It should be followed by a pointer to a value of
718 * type 'struct GNUNET_TIME_Absolute'.
719 *
720 * @param ctx command line processing context
721 * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`)
722 * @param option name of the option
723 * @param value actual value of the option as a string.
724 * @return #GNUNET_OK if parsing the value worked
725 */
726static enum GNUNET_GenericReturnValue
727set_timestamp (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
728 void *scls,
729 const char *option,
730 const char *value)
731{
732 struct GNUNET_TIME_Timestamp *t = scls;
733 struct GNUNET_TIME_Absolute abs;
734
735 (void) ctx;
736 if (GNUNET_OK !=
737 GNUNET_STRINGS_fancy_time_to_absolute (value,
738 &abs))
739 {
740 fprintf (stderr,
741 _ ("You must pass a timestamp to the `%s' option.\n"),
742 option);
743 return GNUNET_SYSERR;
744 }
745 if (0 != abs.abs_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us)
746 {
747 fprintf (stderr,
748 _ ("The maximum precision allowed for timestamps is seconds.\n"));
749 return GNUNET_SYSERR;
750 }
751 t->abs_time = abs;
752 return GNUNET_OK;
753}
754
755
756struct GNUNET_GETOPT_CommandLineOption
757GNUNET_GETOPT_option_timestamp (char shortName,
758 const char *name,
759 const char *argumentHelp,
760 const char *description,
761 struct GNUNET_TIME_Timestamp *val)
762{
763 struct GNUNET_GETOPT_CommandLineOption clo = {
764 .shortName = shortName,
765 .name = name,
766 .argumentHelp = argumentHelp,
767 .description = description,
768 .require_argument = 1,
769 .processor = &set_timestamp,
818 .scls = (void *) val 770 .scls = (void *) val
819 }; 771 };
820 772
@@ -835,7 +787,7 @@ GNUNET_GETOPT_option_absolute_time (char shortName,
835 * @param value actual value of the option as a string. 787 * @param value actual value of the option as a string.
836 * @return #GNUNET_OK if parsing the value worked 788 * @return #GNUNET_OK if parsing the value worked
837 */ 789 */
838static int 790static enum GNUNET_GenericReturnValue
839set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 791set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
840 void *scls, 792 void *scls,
841 const char *option, 793 const char *option,
@@ -864,15 +816,6 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
864} 816}
865 817
866 818
867/**
868 * Allow user to specify an unsigned integer.
869 *
870 * @param shortName short name of the option
871 * @param name long name of the option
872 * @param argumentHelp help text for the option argument
873 * @param description long help text for the option
874 * @param[out] val set to the value specified at the command line
875 */
876struct GNUNET_GETOPT_CommandLineOption 819struct GNUNET_GETOPT_CommandLineOption
877GNUNET_GETOPT_option_uint (char shortName, 820GNUNET_GETOPT_option_uint (char shortName,
878 const char *name, 821 const char *name,
@@ -907,7 +850,7 @@ GNUNET_GETOPT_option_uint (char shortName,
907 * @param value actual value of the option as a string. 850 * @param value actual value of the option as a string.
908 * @return #GNUNET_OK if parsing the value worked 851 * @return #GNUNET_OK if parsing the value worked
909 */ 852 */
910static int 853static enum GNUNET_GenericReturnValue
911set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 854set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
912 void *scls, 855 void *scls,
913 const char *option, 856 const char *option,
@@ -938,15 +881,6 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
938} 881}
939 882
940 883
941/**
942 * Allow user to specify an uint16_t.
943 *
944 * @param shortName short name of the option
945 * @param name long name of the option
946 * @param argumentHelp help text for the option argument
947 * @param description long help text for the option
948 * @param[out] val set to the value specified at the command line
949 */
950struct GNUNET_GETOPT_CommandLineOption 884struct GNUNET_GETOPT_CommandLineOption
951GNUNET_GETOPT_option_uint16 (char shortName, 885GNUNET_GETOPT_option_uint16 (char shortName,
952 const char *name, 886 const char *name,
@@ -998,7 +932,7 @@ struct Base32Context
998 * @param value actual value of the option as a string. 932 * @param value actual value of the option as a string.
999 * @return #GNUNET_OK if parsing the value worked 933 * @return #GNUNET_OK if parsing the value worked
1000 */ 934 */
1001static int 935static enum GNUNET_GenericReturnValue
1002set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 936set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
1003 void *scls, 937 void *scls,
1004 const char *option, 938 const char *option,
@@ -1036,17 +970,6 @@ free_bc (void *cls)
1036} 970}
1037 971
1038 972
1039/**
1040 * Allow user to specify a binary value using Crockford
1041 * Base32 encoding.
1042 *
1043 * @param shortName short name of the option
1044 * @param name long name of the option
1045 * @param argumentHelp help text for the option argument
1046 * @param description long help text for the option
1047 * @param[out] val binary value decoded from Crockford Base32-encoded argument
1048 * @param val_size size of @a val in bytes
1049 */
1050struct GNUNET_GETOPT_CommandLineOption 973struct GNUNET_GETOPT_CommandLineOption
1051GNUNET_GETOPT_option_base32_fixed_size (char shortName, 974GNUNET_GETOPT_option_base32_fixed_size (char shortName,
1052 const char *name, 975 const char *name,
@@ -1073,12 +996,6 @@ GNUNET_GETOPT_option_base32_fixed_size (char shortName,
1073} 996}
1074 997
1075 998
1076/**
1077 * Make the given option mandatory.
1078 *
1079 * @param opt option to modify
1080 * @return @a opt with the mandatory flag set.
1081 */
1082struct GNUNET_GETOPT_CommandLineOption 999struct GNUNET_GETOPT_CommandLineOption
1083GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt) 1000GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
1084{ 1001{
@@ -1087,12 +1004,6 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
1087} 1004}
1088 1005
1089 1006
1090/**
1091 * Make the given option mutually exclusive with other options.
1092 *
1093 * @param opt option to modify
1094 * @return @a opt with the exclusive flag set.
1095 */
1096struct GNUNET_GETOPT_CommandLineOption 1007struct GNUNET_GETOPT_CommandLineOption
1097GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt) 1008GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
1098{ 1009{
diff --git a/src/util/strings.c b/src/util/strings.c
index 673915888..db672da87 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -201,7 +201,7 @@ struct ConversionTable
201 * @param output where to store the result 201 * @param output where to store the result
202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
203 */ 203 */
204static int 204static enum GNUNET_GenericReturnValue
205convert_with_table (const char *input, 205convert_with_table (const char *input,
206 const struct ConversionTable *table, 206 const struct ConversionTable *table,
207 unsigned long long *output) 207 unsigned long long *output)
@@ -256,7 +256,7 @@ convert_with_table (const char *input,
256} 256}
257 257
258 258
259int 259enum GNUNET_GenericReturnValue
260GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, 260GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
261 unsigned long long *size) 261 unsigned long long *size)
262{ 262{
@@ -280,7 +280,7 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
280} 280}
281 281
282 282
283int 283enum GNUNET_GenericReturnValue
284GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, 284GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
285 struct GNUNET_TIME_Relative *rtime) 285 struct GNUNET_TIME_Relative *rtime)
286{ 286{
@@ -322,7 +322,7 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
322} 322}
323 323
324 324
325int 325enum GNUNET_GenericReturnValue
326GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, 326GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
327 struct GNUNET_TIME_Absolute *atime) 327 struct GNUNET_TIME_Absolute *atime)
328{ 328{
@@ -354,6 +354,15 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
354} 354}
355 355
356 356
357enum GNUNET_GenericReturnValue
358GNUNET_STRINGS_fancy_time_to_timestamp (const char *fancy_time,
359 struct GNUNET_TIME_Timestamp *atime)
360{
361 return GNUNET_STRINGS_fancy_time_to_absolute (fancy_time,
362 &atime->abs_time);
363}
364
365
357char * 366char *
358GNUNET_STRINGS_conv (const char *input, 367GNUNET_STRINGS_conv (const char *input,
359 size_t len, 368 size_t len,
@@ -607,7 +616,7 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
607 time_t tt; 616 time_t tt;
608 struct tm *tp; 617 struct tm *tp;
609 618
610 if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 619 if (GNUNET_TIME_absolute_is_never (t))
611 return "end of time"; 620 return "end of time";
612 tt = t.abs_value_us / 1000LL / 1000LL; 621 tt = t.abs_value_us / 1000LL / 1000LL;
613 tp = localtime (&tt); 622 tp = localtime (&tt);
@@ -616,7 +625,8 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
616 * As for msvcrt, use the wide variant, which always returns utf16 625 * As for msvcrt, use the wide variant, which always returns utf16
617 * (otherwise we'd have to detect current codepage or use W32API character 626 * (otherwise we'd have to detect current codepage or use W32API character
618 * set conversion routines to convert to UTF8). 627 * set conversion routines to convert to UTF8).
619 */strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp); 628 */
629 strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp);
620 630
621 return buf; 631 return buf;
622} 632}
diff --git a/src/util/time.c b/src/util/time.c
index 144e1b401..83b39b4e8 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -58,27 +58,35 @@ GNUNET_TIME_get_offset ()
58} 58}
59 59
60 60
61int 61struct GNUNET_TIME_Timestamp
62GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at) 62GNUNET_TIME_absolute_to_timestamp (struct GNUNET_TIME_Absolute at)
63{ 63{
64 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 64 struct GNUNET_TIME_Timestamp ts;
65 return GNUNET_OK; 65
66 if (0 == at->abs_value_us % 1000000) 66 if (GNUNET_TIME_absolute_is_never (at))
67 return GNUNET_OK; 67 return GNUNET_TIME_UNIT_FOREVER_TS;
68 at->abs_value_us -= at->abs_value_us % 1000000; 68 ts.abs_time.abs_value_us = at.abs_value_us - at.abs_value_us % 1000000;
69 return GNUNET_NO; 69 return ts;
70}
71
72
73struct GNUNET_TIME_TimestampNBO
74GNUNET_TIME_timestamp_hton (struct GNUNET_TIME_Timestamp t)
75{
76 struct GNUNET_TIME_TimestampNBO tn;
77
78 tn.abs_time_nbo = GNUNET_TIME_absolute_hton (t.abs_time);
79 return tn;
70} 80}
71 81
72 82
73int 83struct GNUNET_TIME_Timestamp
74GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt) 84GNUNET_TIME_timestamp_ntoh (struct GNUNET_TIME_TimestampNBO tn)
75{ 85{
76 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 86 struct GNUNET_TIME_Timestamp t;
77 return GNUNET_OK; 87
78 if (0 == rt->rel_value_us % 1000000) 88 t.abs_time = GNUNET_TIME_absolute_ntoh (tn.abs_time_nbo);
79 return GNUNET_OK; 89 return t;
80 rt->rel_value_us -= rt->rel_value_us % 1000000;
81 return GNUNET_NO;
82} 90}
83 91
84 92
@@ -96,6 +104,14 @@ GNUNET_TIME_absolute_get ()
96} 104}
97 105
98 106
107struct GNUNET_TIME_Timestamp
108GNUNET_TIME_timestamp_get ()
109{
110 return GNUNET_TIME_absolute_to_timestamp (
111 GNUNET_TIME_absolute_get ());
112}
113
114
99struct GNUNET_TIME_Relative 115struct GNUNET_TIME_Relative
100GNUNET_TIME_relative_get_zero_ () 116GNUNET_TIME_relative_get_zero_ ()
101{ 117{
@@ -177,12 +193,114 @@ GNUNET_TIME_absolute_get_forever_ ()
177} 193}
178 194
179 195
196const char *
197GNUNET_TIME_timestamp2s (struct GNUNET_TIME_Timestamp ts)
198{
199 static GNUNET_THREAD_LOCAL char buf[255];
200 time_t tt;
201 struct tm *tp;
202
203 if (GNUNET_TIME_absolute_is_never (ts.abs_time))
204 return "end of time";
205 tt = ts.abs_time.abs_value_us / 1000LL / 1000LL;
206 tp = localtime (&tt);
207 /* This is hacky, but i don't know a way to detect libc character encoding.
208 * Just expect utf8 from glibc these days.
209 * As for msvcrt, use the wide variant, which always returns utf16
210 * (otherwise we'd have to detect current codepage or use W32API character
211 * set conversion routines to convert to UTF8).
212 */
213 strftime (buf,
214 sizeof(buf),
215 "%a %b %d %H:%M:%S %Y",
216 tp);
217 return buf;
218}
219
220
221const char *
222GNUNET_TIME_absolute2s (struct GNUNET_TIME_Absolute t)
223{
224 static GNUNET_THREAD_LOCAL char buf[255];
225 time_t tt;
226 struct tm *tp;
227
228 if (GNUNET_TIME_absolute_is_never (t))
229 return "end of time";
230 tt = t.abs_value_us / 1000LL / 1000LL;
231 tp = localtime (&tt);
232 /* This is hacky, but i don't know a way to detect libc character encoding.
233 * Just expect utf8 from glibc these days.
234 * As for msvcrt, use the wide variant, which always returns utf16
235 * (otherwise we'd have to detect current codepage or use W32API character
236 * set conversion routines to convert to UTF8).
237 */
238 strftime (buf,
239 sizeof(buf),
240 "%a %b %d %H:%M:%S %Y",
241 tp);
242 return buf;
243}
244
245
246const char *
247GNUNET_TIME_relative2s (struct GNUNET_TIME_Relative delta,
248 bool do_round)
249{
250 static GNUNET_THREAD_LOCAL char buf[128];
251 const char *unit = /* time unit */ "µs";
252 uint64_t dval = delta.rel_value_us;
253
254 if (GNUNET_TIME_relative_is_forever (delta))
255 return "forever";
256 if (0 == delta.rel_value_us)
257 return "0 ms";
258 if ( ((GNUNET_YES == do_round) &&
259 (dval > 5 * 1000)) ||
260 (0 == (dval % 1000)))
261 {
262 dval = dval / 1000;
263 unit = /* time unit */ "ms";
264 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
265 {
266 dval = dval / 1000;
267 unit = /* time unit */ "s";
268 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
269 {
270 dval = dval / 60;
271 unit = /* time unit */ "m";
272 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
273 {
274 dval = dval / 60;
275 unit = /* time unit */ "h";
276 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) ||
277 (0 == (dval % 24)))
278 {
279 dval = dval / 24;
280 if (1 == dval)
281 unit = /* time unit */ "day";
282 else
283 unit = /* time unit */ "days";
284 }
285 }
286 }
287 }
288 }
289 GNUNET_snprintf (buf,
290 sizeof(buf),
291 "%llu %s",
292 (unsigned long long) dval,
293 unit);
294 return buf;
295}
296
297
180struct GNUNET_TIME_Absolute 298struct GNUNET_TIME_Absolute
181GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel) 299GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
182{ 300{
183 struct GNUNET_TIME_Absolute ret; 301 struct GNUNET_TIME_Absolute ret;
184 302
185 if (rel.rel_value_us == UINT64_MAX) 303 if (GNUNET_TIME_relative_is_forever (rel))
186 return GNUNET_TIME_UNIT_FOREVER_ABS; 304 return GNUNET_TIME_UNIT_FOREVER_ABS;
187 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 305 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
188 306
@@ -196,6 +314,14 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
196} 314}
197 315
198 316
317struct GNUNET_TIME_Timestamp
318GNUNET_TIME_relative_to_timestamp (struct GNUNET_TIME_Relative rel)
319{
320 return GNUNET_TIME_absolute_to_timestamp (
321 GNUNET_TIME_relative_to_absolute (rel));
322}
323
324
199struct GNUNET_TIME_Relative 325struct GNUNET_TIME_Relative
200GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, 326GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
201 struct GNUNET_TIME_Relative t2) 327 struct GNUNET_TIME_Relative t2)
@@ -228,12 +354,28 @@ GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
228} 354}
229 355
230 356
357struct GNUNET_TIME_Timestamp
358GNUNET_TIME_timestamp_max (struct GNUNET_TIME_Timestamp t1,
359 struct GNUNET_TIME_Timestamp t2)
360{
361 return (t1.abs_time.abs_value_us > t2.abs_time.abs_value_us) ? t1 : t2;
362}
363
364
365struct GNUNET_TIME_Timestamp
366GNUNET_TIME_timestamp_min (struct GNUNET_TIME_Timestamp t1,
367 struct GNUNET_TIME_Timestamp t2)
368{
369 return (t1.abs_time.abs_value_us < t2.abs_time.abs_value_us) ? t1 : t2;
370}
371
372
231struct GNUNET_TIME_Relative 373struct GNUNET_TIME_Relative
232GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future) 374GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
233{ 375{
234 struct GNUNET_TIME_Relative ret; 376 struct GNUNET_TIME_Relative ret;
235 377
236 if (future.abs_value_us == UINT64_MAX) 378 if (GNUNET_TIME_absolute_is_never (future))
237 return GNUNET_TIME_UNIT_FOREVER_REL; 379 return GNUNET_TIME_UNIT_FOREVER_REL;
238 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 380 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
239 381
@@ -250,7 +392,7 @@ GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
250{ 392{
251 struct GNUNET_TIME_Relative ret; 393 struct GNUNET_TIME_Relative ret;
252 394
253 if (end.abs_value_us == UINT64_MAX) 395 if (GNUNET_TIME_absolute_is_never (end))
254 return GNUNET_TIME_UNIT_FOREVER_REL; 396 return GNUNET_TIME_UNIT_FOREVER_REL;
255 if (end.abs_value_us < start.abs_value_us) 397 if (end.abs_value_us < start.abs_value_us)
256 return GNUNET_TIME_UNIT_ZERO; 398 return GNUNET_TIME_UNIT_ZERO;
@@ -279,8 +421,8 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
279{ 421{
280 struct GNUNET_TIME_Absolute ret; 422 struct GNUNET_TIME_Absolute ret;
281 423
282 if ((start.abs_value_us == UINT64_MAX) || 424 if (GNUNET_TIME_absolute_is_never (start) ||
283 (duration.rel_value_us == UINT64_MAX)) 425 GNUNET_TIME_relative_is_forever (duration))
284 return GNUNET_TIME_UNIT_FOREVER_ABS; 426 return GNUNET_TIME_UNIT_FOREVER_ABS;
285 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us) 427 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
286 { 428 {
@@ -300,7 +442,7 @@ GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
300 442
301 if (start.abs_value_us <= duration.rel_value_us) 443 if (start.abs_value_us <= duration.rel_value_us)
302 return GNUNET_TIME_UNIT_ZERO_ABS; 444 return GNUNET_TIME_UNIT_ZERO_ABS;
303 if (start.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 445 if (GNUNET_TIME_absolute_is_never (start))
304 return GNUNET_TIME_UNIT_FOREVER_ABS; 446 return GNUNET_TIME_UNIT_FOREVER_ABS;
305 ret.abs_value_us = start.abs_value_us - duration.rel_value_us; 447 ret.abs_value_us = start.abs_value_us - duration.rel_value_us;
306 return ret; 448 return ret;
@@ -315,7 +457,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
315 457
316 if (0 == factor) 458 if (0 == factor)
317 return GNUNET_TIME_UNIT_ZERO; 459 return GNUNET_TIME_UNIT_ZERO;
318 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 460 if (GNUNET_TIME_relative_is_forever (rel))
319 return GNUNET_TIME_UNIT_FOREVER_REL; 461 return GNUNET_TIME_UNIT_FOREVER_REL;
320 ret.rel_value_us = rel.rel_value_us * factor; 462 ret.rel_value_us = rel.rel_value_us * factor;
321 if (ret.rel_value_us / factor != rel.rel_value_us) 463 if (ret.rel_value_us / factor != rel.rel_value_us)
@@ -328,7 +470,8 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
328 470
329 471
330struct GNUNET_TIME_Relative 472struct GNUNET_TIME_Relative
331relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor) 473relative_multiply_double (struct GNUNET_TIME_Relative rel,
474 double factor)
332{ 475{
333 struct GNUNET_TIME_Relative out; 476 struct GNUNET_TIME_Relative out;
334 double m; 477 double m;
@@ -337,7 +480,7 @@ relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor)
337 480
338 if (0 == factor) 481 if (0 == factor)
339 return GNUNET_TIME_UNIT_ZERO; 482 return GNUNET_TIME_UNIT_ZERO;
340 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 483 if (GNUNET_TIME_relative_is_forever (rel))
341 return GNUNET_TIME_UNIT_FOREVER_REL; 484 return GNUNET_TIME_UNIT_FOREVER_REL;
342 485
343 m = ((double) rel.rel_value_us) * factor; 486 m = ((double) rel.rel_value_us) * factor;
@@ -361,7 +504,7 @@ GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
361 504
362 if (0 == factor) 505 if (0 == factor)
363 return GNUNET_TIME_UNIT_ZERO; 506 return GNUNET_TIME_UNIT_ZERO;
364 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 507 if (GNUNET_TIME_relative_is_forever (rel))
365 return GNUNET_TIME_UNIT_FOREVER_REL; 508 return GNUNET_TIME_UNIT_FOREVER_REL;
366 ret.rel_value_us = rel.rel_value_us * factor; 509 ret.rel_value_us = rel.rel_value_us * factor;
367 if (ret.rel_value_us / factor != rel.rel_value_us) 510 if (ret.rel_value_us / factor != rel.rel_value_us)
@@ -379,7 +522,7 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
379 struct GNUNET_TIME_Relative ret; 522 struct GNUNET_TIME_Relative ret;
380 523
381 if ((0 == factor) || 524 if ((0 == factor) ||
382 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)) 525 (GNUNET_TIME_relative_is_forever (rel)))
383 return GNUNET_TIME_UNIT_FOREVER_REL; 526 return GNUNET_TIME_UNIT_FOREVER_REL;
384 ret.rel_value_us = rel.rel_value_us / factor; 527 ret.rel_value_us = rel.rel_value_us / factor;
385 return ret; 528 return ret;
@@ -538,6 +681,20 @@ GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch)
538} 681}
539 682
540 683
684struct GNUNET_TIME_Timestamp
685GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch)
686{
687 struct GNUNET_TIME_Timestamp ret;
688
689 ret.abs_time.abs_value_us
690 = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch;
691 if (ret.abs_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us
692 != s_after_epoch)
693 ret = GNUNET_TIME_UNIT_FOREVER_TS;
694 return ret;
695}
696
697
541struct GNUNET_TIME_Absolute 698struct GNUNET_TIME_Absolute
542GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) 699GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
543{ 700{
@@ -645,6 +802,13 @@ GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
645} 802}
646 803
647 804
805bool
806GNUNET_TIME_absolute_is_zero (struct GNUNET_TIME_Absolute abs)
807{
808 return 0 == abs.abs_value_us;
809}
810
811
648struct GNUNET_TIME_Relative 812struct GNUNET_TIME_Relative
649GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r) 813GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
650{ 814{