util.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #define DO_NTOHS(_d, _s) \
  20. do { unsigned short _dd; \
  21. memcpy(&(_dd), (_s), 2); \
  22. _d = ntohs(_dd); } while(0)
  23. #define DO_NTOHL(_d, _s) \
  24. do { unsigned int _dd; \
  25. memcpy(&(_dd), (_s), 4); \
  26. _d = ntohl(_dd); } while(0)
  27. #define DO_HTONS(_d, _s) \
  28. do { unsigned short _dd; \
  29. _dd = htons(_s); \
  30. memcpy((_d), &(_dd), 2); } while(0)
  31. #define DO_HTONL(_d, _s) \
  32. do { unsigned _dd; \
  33. _dd = htonl(_s); \
  34. memcpy((_d), &(_dd), 4); } while(0)
  35. static inline int
  36. seqno_compare(unsigned short s1, unsigned short s2)
  37. {
  38. if(s1 == s2)
  39. return 0;
  40. else
  41. return ((s2 - s1) & 0x8000) ? 1 : -1;
  42. }
  43. static inline short
  44. seqno_minus(unsigned short s1, unsigned short s2)
  45. {
  46. return (short)((s1 - s2) & 0xFFFF);
  47. }
  48. static inline unsigned short
  49. seqno_plus(unsigned short s, int plus)
  50. {
  51. return ((s + plus) & 0xFFFF);
  52. }
  53. /* Returns a time in microseconds on 32 bits (thus modulo 2^32,
  54. i.e. about 4295 seconds). */
  55. static inline unsigned int
  56. time_us(const struct timeval t)
  57. {
  58. return (unsigned int) (t.tv_sec * 1000000 + t.tv_usec);
  59. }
  60. int roughly(int value);
  61. void timeval_minus(struct timeval *d,
  62. const struct timeval *s1, const struct timeval *s2);
  63. unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
  64. ATTRIBUTE ((pure));
  65. void timeval_add_msec(struct timeval *d,
  66. const struct timeval *s, int msecs);
  67. int timeval_compare(const struct timeval *s1, const struct timeval *s2)
  68. ATTRIBUTE ((pure));
  69. void timeval_min(struct timeval *d, const struct timeval *s);
  70. void timeval_min_sec(struct timeval *d, time_t secs);
  71. int parse_nat(const char *string) ATTRIBUTE ((pure));
  72. int parse_thousands(const char *string) ATTRIBUTE ((pure));
  73. void do_debugf(int level, const char *format, ...)
  74. ATTRIBUTE ((format (printf, 2, 3))) COLD;
  75. int in_prefix(const unsigned char *restrict address,
  76. const unsigned char *restrict prefix, unsigned char plen)
  77. ATTRIBUTE ((pure));
  78. unsigned char *normalize_prefix(unsigned char *restrict ret,
  79. const unsigned char *restrict prefix,
  80. unsigned char plen);
  81. const char *format_address(const unsigned char *address);
  82. const char *format_prefix(const unsigned char *address, unsigned char prefix);
  83. const char *format_eui64(const unsigned char *eui);
  84. const char *format_thousands(unsigned int value);
  85. int parse_address(const char *address, unsigned char *addr_r, int *af_r);
  86. int parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
  87. int *af_r);
  88. int parse_eui64(const char *eui, unsigned char *eui_r);
  89. int wait_for_fd(int direction, int fd, int msecs);
  90. int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure));
  91. int linklocal(const unsigned char *address) ATTRIBUTE ((pure));
  92. int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
  93. void v4tov6(unsigned char *dst, const unsigned char *src);
  94. int daemonise(void);
  95. int set_src_prefix(unsigned char *src_addr, unsigned char *src_plen);
  96. enum prefix_status {
  97. PST_EQUALS = 0,
  98. PST_DISJOINT,
  99. PST_MORE_SPECIFIC,
  100. PST_LESS_SPECIFIC
  101. };
  102. enum prefix_status
  103. prefix_cmp(const unsigned char *p1, unsigned char plen1,
  104. const unsigned char *p2, unsigned char plen2);
  105. /* If debugging is disabled, we want to avoid calling format_address
  106. for every omitted debugging message. So debug is a macro. But
  107. vararg macros are not portable. */
  108. #if defined NO_DEBUG
  109. #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  110. #define debugf(...) do {} while(0)
  111. #define kdebugf(...) do {} while(0)
  112. #elif defined __GNUC__
  113. #define debugf(_args...) do {} while(0)
  114. #define kdebugf(_args...) do {} while(0)
  115. #else
  116. static inline void debugf(const char *format, ...) { return; }
  117. static inline void kdebugf(const char *format, ...) { return; }
  118. #endif
  119. #else
  120. #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  121. #define debugf(...) \
  122. do { \
  123. if(UNLIKELY(debug >= 2)) do_debugf(2, __VA_ARGS__); \
  124. } while(0)
  125. #define kdebugf(...) \
  126. do { \
  127. if(UNLIKELY(debug >= 3)) do_debugf(3, __VA_ARGS__); \
  128. } while(0)
  129. #elif defined __GNUC__
  130. #define debugf(_args...) \
  131. do { \
  132. if(UNLIKELY(debug >= 2)) do_debugf(2, _args); \
  133. } while(0)
  134. #define kdebugf(_args...) \
  135. do { \
  136. if(UNLIKELY(debug >= 3)) do_debugf(3, _args); \
  137. } while(0)
  138. #else
  139. static inline void debugf(const char *format, ...) { return; }
  140. static inline void kdebugf(const char *format, ...) { return; }
  141. #endif
  142. #endif