Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is free software: you may copy, redistribute and/or modify it |
| 3 | * under the terms of the GNU General Public License as published by the |
| 4 | * Free Software Foundation, either version 2 of the License, or (at your |
| 5 | * option) any later version. |
| 6 | * |
| 7 | * This file is distributed in the hope that it will be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | * General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | * |
| 15 | * This file incorporates work covered by the following copyright and |
| 16 | * permission notice: |
| 17 | * |
| 18 | Copyright (c) 2007, 2008 by Juliusz Chroboczek |
| 19 | Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek |
| 20 | |
| 21 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | of this software and associated documentation files (the "Software"), to deal |
| 23 | in the Software without restriction, including without limitation the rights |
| 24 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | copies of the Software, and to permit persons to whom the Software is |
| 26 | furnished to do so, subject to the following conditions: |
| 27 | |
| 28 | The above copyright notice and this permission notice shall be included in |
| 29 | all copies or substantial portions of the Software. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 37 | THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include "babeld.h" |
| 41 | #include "babel_main.h" |
| 42 | #include "log.h" |
| 43 | |
| 44 | #if defined(i386) || defined(__mc68020__) || defined(__x86_64__) |
Matthieu Boutier | c7c53fa | 2012-01-08 16:43:08 +0100 | [diff] [blame] | 45 | #define DO_NTOHS(_d, _s) do{ _d = ntohs(*(const unsigned short*)(_s)); }while(0) |
| 46 | #define DO_NTOHL(_d, _s) do{ _d = ntohl(*(const unsigned*)(_s)); } while(0) |
| 47 | #define DO_HTONS(_d, _s) do{ *(unsigned short*)(_d) = htons(_s); } while(0) |
| 48 | #define DO_HTONL(_d, _s) do{ *(unsigned*)(_d) = htonl(_s); } while(0) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 49 | /* Some versions of gcc seem to be buggy, and ignore the packed attribute. |
| 50 | Disable this code until the issue is clarified. */ |
| 51 | /* #elif defined __GNUC__*/ |
| 52 | #elif 0 |
| 53 | struct __us { unsigned short x __attribute__((packed)); }; |
| 54 | #define DO_NTOHS(_d, _s) \ |
| 55 | do { _d = ntohs(((const struct __us*)(_s))->x); } while(0) |
| 56 | #define DO_HTONS(_d, _s) \ |
| 57 | do { ((struct __us*)(_d))->x = htons(_s); } while(0) |
| 58 | #else |
| 59 | #define DO_NTOHS(_d, _s) \ |
| 60 | do { short _dd; \ |
| 61 | memcpy(&(_dd), (_s), 2); \ |
| 62 | _d = ntohs(_dd); } while(0) |
| 63 | #define DO_HTONS(_d, _s) \ |
| 64 | do { unsigned short _dd; \ |
| 65 | _dd = htons(_s); \ |
| 66 | memcpy((_d), &(_dd), 2); } while(0) |
| 67 | #endif |
| 68 | |
| 69 | static inline int |
| 70 | seqno_compare(unsigned short s1, unsigned short s2) |
| 71 | { |
| 72 | if(s1 == s2) |
| 73 | return 0; |
| 74 | else |
| 75 | return ((s2 - s1) & 0x8000) ? 1 : -1; |
| 76 | } |
| 77 | |
| 78 | static inline short |
| 79 | seqno_minus(unsigned short s1, unsigned short s2) |
| 80 | { |
| 81 | return (short)((s1 - s2) & 0xFFFF); |
| 82 | } |
| 83 | |
| 84 | static inline unsigned short |
| 85 | seqno_plus(unsigned short s, int plus) |
| 86 | { |
| 87 | return ((s + plus) & 0xFFFF); |
| 88 | } |
| 89 | |
| 90 | unsigned roughly(unsigned value); |
| 91 | void timeval_minus(struct timeval *d, |
| 92 | const struct timeval *s1, const struct timeval *s2); |
| 93 | unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2) |
| 94 | ATTRIBUTE ((pure)); |
| 95 | void timeval_add_msec(struct timeval *d, |
| 96 | const struct timeval *s, const int msecs); |
| 97 | void set_timeout (struct timeval *timeout, int msecs); |
| 98 | int timeval_compare(const struct timeval *s1, const struct timeval *s2) |
| 99 | ATTRIBUTE ((pure)); |
| 100 | void timeval_min(struct timeval *d, const struct timeval *s); |
| 101 | void timeval_min_sec(struct timeval *d, time_t secs); |
| 102 | int parse_msec(const char *string) ATTRIBUTE ((pure)); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 103 | int in_prefix(const unsigned char *restrict address, |
| 104 | const unsigned char *restrict prefix, unsigned char plen) |
| 105 | ATTRIBUTE ((pure)); |
| 106 | unsigned char *mask_prefix(unsigned char *restrict ret, |
| 107 | const unsigned char *restrict prefix, |
| 108 | unsigned char plen); |
| 109 | const char *format_address(const unsigned char *address); |
| 110 | const char *format_prefix(const unsigned char *address, unsigned char prefix); |
| 111 | const char *format_eui64(const unsigned char *eui); |
Matthieu Boutier | d3351d1 | 2012-01-19 22:36:56 +0100 | [diff] [blame] | 112 | const char *format_bool(const int b); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 113 | int parse_address(const char *address, unsigned char *addr_r, int *af_r); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 114 | int parse_eui64(const char *eui, unsigned char *eui_r); |
| 115 | int wait_for_fd(int direction, int fd, int msecs); |
| 116 | int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure)); |
| 117 | int linklocal(const unsigned char *address) ATTRIBUTE ((pure)); |
| 118 | int v4mapped(const unsigned char *address) ATTRIBUTE ((pure)); |
| 119 | void v4tov6(unsigned char *dst, const unsigned char *src); |
| 120 | void inaddr_to_uchar(unsigned char *dest, const struct in_addr *src); |
| 121 | void uchar_to_inaddr(struct in_addr *dest, const unsigned char *src); |
| 122 | void in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src); |
| 123 | void uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src); |
| 124 | int daemonise(void); |
| 125 | |
| 126 | /* If debugging is disabled, we want to avoid calling format_address |
| 127 | for every omitted debugging message. So debug is a macro. But |
| 128 | vararg macros are not portable. */ |
| 129 | #if defined NO_DEBUG |
| 130 | |
| 131 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L |
| 132 | #define debugf(...) do {} while(0) |
| 133 | #elif defined __GNUC__ |
| 134 | #define debugf(_args...) do {} while(0) |
| 135 | #else |
| 136 | static inline void debugf(int level, const char *format, ...) { return; } |
| 137 | #endif |
| 138 | |
| 139 | #else /* NO_DEBUG */ |
| 140 | |
| 141 | /* some levels */ |
| 142 | #define BABEL_DEBUG_COMMON (1 << 0) |
| 143 | #define BABEL_DEBUG_KERNEL (1 << 1) |
| 144 | #define BABEL_DEBUG_FILTER (1 << 2) |
| 145 | #define BABEL_DEBUG_TIMEOUT (1 << 3) |
| 146 | #define BABEL_DEBUG_IF (1 << 4) |
| 147 | #define BABEL_DEBUG_ROUTE (1 << 5) |
| 148 | #define BABEL_DEBUG_ALL (0xFFFF) |
| 149 | |
| 150 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L |
| 151 | #define debugf(level, ...) \ |
| 152 | do { \ |
Matthieu Boutier | 3cb4134 | 2012-01-18 20:20:59 +0100 | [diff] [blame] | 153 | if(UNLIKELY(debug & level)) zlog_debug(__VA_ARGS__); \ |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 154 | } while(0) |
| 155 | #elif defined __GNUC__ |
| 156 | #define debugf(level, _args...) \ |
| 157 | do { \ |
Matthieu Boutier | 3cb4134 | 2012-01-18 20:20:59 +0100 | [diff] [blame] | 158 | if(UNLIKELY(debug & level)) zlog_debug(_args); \ |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 159 | } while(0) |
| 160 | #else |
| 161 | static inline void debugf(int level, const char *format, ...) { return; } |
| 162 | #endif |
| 163 | |
| 164 | #endif /* NO_DEBUG */ |
| 165 | |