Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.5 KiB

20 years ago
20 years ago
20 years ago
20 years ago
  1. /* $OpenBSD: util.c,v 1.15 2013/10/15 20:35:55 krw Exp $ */
  2. /*
  3. * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/time.h>
  18. #include <limits.h>
  19. #include <stdio.h>
  20. #include "ntpd.h"
  21. double
  22. gettime_corrected(void)
  23. {
  24. return (gettime() + getoffset());
  25. }
  26. double
  27. getoffset(void)
  28. {
  29. struct timeval tv;
  30. if (adjtime(NULL, &tv) == -1)
  31. return (0.0);
  32. return (tv.tv_sec + 1.0e-6 * tv.tv_usec);
  33. }
  34. double
  35. gettime(void)
  36. {
  37. struct timeval tv;
  38. if (gettimeofday(&tv, NULL) == -1)
  39. fatal("gettimeofday");
  40. return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
  41. }
  42. time_t
  43. getmonotime(void)
  44. {
  45. struct timespec ts;
  46. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
  47. fatal("clock_gettime");
  48. return (ts.tv_sec);
  49. }
  50. void
  51. d_to_tv(double d, struct timeval *tv)
  52. {
  53. tv->tv_sec = d;
  54. tv->tv_usec = (d - tv->tv_sec) * 1000000;
  55. while (tv->tv_usec < 0) {
  56. tv->tv_usec += 1000000;
  57. tv->tv_sec -= 1;
  58. }
  59. }
  60. double
  61. lfp_to_d(struct l_fixedpt lfp)
  62. {
  63. double ret;
  64. lfp.int_partl = ntohl(lfp.int_partl);
  65. lfp.fractionl = ntohl(lfp.fractionl);
  66. ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
  67. return (ret);
  68. }
  69. struct l_fixedpt
  70. d_to_lfp(double d)
  71. {
  72. struct l_fixedpt lfp;
  73. lfp.int_partl = htonl((u_int32_t)d);
  74. lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
  75. return (lfp);
  76. }
  77. double
  78. sfp_to_d(struct s_fixedpt sfp)
  79. {
  80. double ret;
  81. sfp.int_parts = ntohs(sfp.int_parts);
  82. sfp.fractions = ntohs(sfp.fractions);
  83. ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
  84. return (ret);
  85. }
  86. struct s_fixedpt
  87. d_to_sfp(double d)
  88. {
  89. struct s_fixedpt sfp;
  90. sfp.int_parts = htons((u_int16_t)d);
  91. sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
  92. return (sfp);
  93. }
  94. char *
  95. print_rtable(int r)
  96. {
  97. static char b[11];
  98. b[0] = 0;
  99. if (r > 0)
  100. snprintf(b, sizeof(b), "rtable %d", r);
  101. return(b);
  102. }