Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/net_helper.h |
| 3 | * This file contains miscellaneous network helper functions. |
| 4 | * |
| 5 | * Copyright (C) 2017 Olivier Houchard |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 6 | * Copyright (C) 2017 Willy Tarreau |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _COMMON_NET_HELPER_H |
| 28 | #define _COMMON_NET_HELPER_H |
| 29 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 30 | #include <common/compiler.h> |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 31 | #include <common/standard.h> |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 32 | #include <arpa/inet.h> |
| 33 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 34 | /* Functions to read/write various integers that may be unaligned */ |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 36 | /* Read a uint16_t in native host order */ |
| 37 | static inline uint16_t read_u16(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 38 | { |
| 39 | const union { uint16_t u16; } __attribute__((packed))*u = p; |
| 40 | return u->u16; |
| 41 | } |
| 42 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 43 | /* Write a uint16_t in native host order */ |
| 44 | static inline void write_u16(void *p, const uint16_t u16) |
| 45 | { |
| 46 | union { uint16_t u16; } __attribute__((packed))*u = p; |
| 47 | u->u16 = u16; |
| 48 | } |
| 49 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 50 | /* Read a uint32_t in native host order */ |
| 51 | static inline uint32_t read_u32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 52 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 53 | const union { uint32_t u32; } __attribute__((packed))*u = p; |
| 54 | return u->u32; |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 57 | /* Write a uint32_t in native host order */ |
| 58 | static inline void write_u32(void *p, const uint32_t u32) |
| 59 | { |
| 60 | union { uint32_t u32; } __attribute__((packed))*u = p; |
| 61 | u->u32 = u32; |
| 62 | } |
| 63 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 64 | /* Read a uint64_t in native host order */ |
| 65 | static inline uint64_t read_u64(const void *p) |
| 66 | { |
| 67 | const union { uint64_t u64; } __attribute__((packed))*u = p; |
| 68 | return u->u64; |
| 69 | } |
| 70 | |
| 71 | /* Write a uint64_t in native host order */ |
| 72 | static inline void write_u64(void *p, const uint64_t u64) |
| 73 | { |
| 74 | union { uint64_t u64; } __attribute__((packed))*u = p; |
| 75 | u->u64 = u64; |
| 76 | } |
| 77 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 78 | /* Read a possibly wrapping number of bytes <bytes> into destination <dst>. The |
| 79 | * first segment is composed of <s1> bytes at p1. The remaining byte(s), if any, |
| 80 | * are read from <p2>. <s1> may be zero and may also be larger than <bytes>. The |
| 81 | * caller is always responsible for providing enough bytes. Note: the function |
| 82 | * is purposely *not* marked inline to let the compiler decide what to do with |
| 83 | * it, because it's around 34 bytes long, placed on critical path but rarely |
| 84 | * called, and uses uses a lot of arguments if not inlined. The compiler will |
| 85 | * thus decide what's best to do with it depending on the context. |
| 86 | */ |
| 87 | static void readv_bytes(void *dst, const size_t bytes, const void *p1, size_t s1, const void *p2) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 88 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 89 | size_t idx; |
| 90 | |
| 91 | p2 -= s1; |
| 92 | for (idx = 0; idx < bytes; idx++) { |
| 93 | if (idx == s1) |
| 94 | p1 = p2; |
| 95 | ((uint8_t *)dst)[idx] = ((const uint8_t *)p1)[idx]; |
| 96 | } |
| 97 | /* this memory barrier is critical otherwise gcc may over-optimize this |
| 98 | * code, completely removing it as well as any surrounding boundary |
| 99 | * check (4.7.1..6.4.0)! |
| 100 | */ |
| 101 | __asm__ volatile("" ::: "memory"); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 104 | /* Write a possibly wrapping number of bytes <bytes> from location <src>. The |
| 105 | * first segment is composed of <s1> bytes at p1. The remaining byte(s), if any, |
| 106 | * are written to <p2>. <s1> may be zero and may also be larger than <bytes>. |
| 107 | * The caller is always responsible for providing enough room. Note: the |
| 108 | * function is purposely *not* marked inline to let the compiler decide what to |
| 109 | * do with it, because it's around 34 bytes long, placed on critical path but |
| 110 | * rarely called, and uses uses a lot of arguments if not inlined. The compiler |
| 111 | * will thus decide what's best to do with it depending on the context. |
| 112 | */ |
| 113 | static void writev_bytes(const void *src, const size_t bytes, void *p1, size_t s1, void *p2) |
| 114 | { |
| 115 | size_t idx; |
| 116 | |
| 117 | p2 -= s1; |
| 118 | for (idx = 0; idx < bytes; idx++) { |
| 119 | if (idx == s1) |
| 120 | p1 = p2; |
| 121 | ((uint8_t *)p1)[idx] = ((const uint8_t *)src)[idx]; |
| 122 | } |
| 123 | } |
| 124 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 125 | /* Read a possibly wrapping uint16_t in native host order. The first segment is |
| 126 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 127 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 128 | * responsible for providing enough bytes. |
| 129 | */ |
| 130 | static inline uint16_t readv_u16(const void *p1, size_t s1, const void *p2) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 131 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 132 | if (unlikely(s1 == 1)) { |
| 133 | volatile uint16_t u16; |
| 134 | |
| 135 | ((uint8_t *)&u16)[0] = *(uint8_t *)p1; |
| 136 | ((uint8_t *)&u16)[1] = *(uint8_t *)p2; |
| 137 | return u16; |
| 138 | } |
| 139 | else { |
| 140 | const union { uint16_t u16; } __attribute__((packed)) *u; |
| 141 | |
| 142 | u = (s1 == 0) ? p2 : p1; |
| 143 | return u->u16; |
| 144 | } |
| 145 | } |
| 146 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 147 | /* Write a possibly wrapping uint16_t in native host order. The first segment is |
| 148 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 149 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 150 | * responsible for providing enough room. |
| 151 | */ |
| 152 | static inline void writev_u16(void *p1, size_t s1, void *p2, const uint16_t u16) |
| 153 | { |
| 154 | union { uint16_t u16; } __attribute__((packed)) *u; |
| 155 | |
| 156 | if (unlikely(s1 == 1)) { |
| 157 | *(uint8_t *)p1 = ((const uint8_t *)&u16)[0]; |
| 158 | *(uint8_t *)p2 = ((const uint8_t *)&u16)[1]; |
| 159 | } |
| 160 | else { |
| 161 | u = (s1 == 0) ? p2 : p1; |
| 162 | u->u16 = u16; |
| 163 | } |
| 164 | } |
| 165 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 166 | /* Read a possibly wrapping uint32_t in native host order. The first segment is |
| 167 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 168 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 169 | * responsible for providing enough bytes. |
| 170 | */ |
| 171 | static inline uint32_t readv_u32(const void *p1, size_t s1, const void *p2) |
| 172 | { |
| 173 | uint32_t u32; |
| 174 | |
Tim Duesterhus | 1d48ba9 | 2020-02-21 13:02:04 +0100 | [diff] [blame] | 175 | if (likely(s1 >= sizeof(u32))) |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 176 | u32 = read_u32(p1); |
| 177 | else |
| 178 | readv_bytes(&u32, sizeof(u32), p1, s1, p2); |
| 179 | return u32; |
| 180 | } |
| 181 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 182 | /* Write a possibly wrapping uint32_t in native host order. The first segment is |
| 183 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 184 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 185 | * responsible for providing enough room. |
| 186 | */ |
| 187 | static inline void writev_u32(void *p1, size_t s1, void *p2, const uint32_t u32) |
| 188 | { |
Tim Duesterhus | 1d48ba9 | 2020-02-21 13:02:04 +0100 | [diff] [blame] | 189 | if (likely(s1 >= sizeof(u32))) |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 190 | write_u32(p1, u32); |
| 191 | else |
| 192 | writev_bytes(&u32, sizeof(u32), p1, s1, p2); |
| 193 | } |
| 194 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 195 | /* Read a possibly wrapping uint64_t in native host order. The first segment is |
| 196 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 197 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 198 | * responsible for providing enough bytes. |
| 199 | */ |
| 200 | static inline uint64_t readv_u64(const void *p1, size_t s1, const void *p2) |
| 201 | { |
| 202 | uint64_t u64; |
| 203 | |
Tim Duesterhus | 1d48ba9 | 2020-02-21 13:02:04 +0100 | [diff] [blame] | 204 | if (likely(s1 >= sizeof(u64))) |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 205 | u64 = read_u64(p1); |
| 206 | else |
| 207 | readv_bytes(&u64, sizeof(u64), p1, s1, p2); |
| 208 | return u64; |
| 209 | } |
| 210 | |
| 211 | /* Write a possibly wrapping uint64_t in native host order. The first segment is |
| 212 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 213 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 214 | * responsible for providing enough room. |
| 215 | */ |
| 216 | static inline void writev_u64(void *p1, size_t s1, void *p2, const uint64_t u64) |
| 217 | { |
Tim Duesterhus | 1d48ba9 | 2020-02-21 13:02:04 +0100 | [diff] [blame] | 218 | if (likely(s1 >= sizeof(u64))) |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 219 | write_u64(p1, u64); |
| 220 | else |
| 221 | writev_bytes(&u64, sizeof(u64), p1, s1, p2); |
| 222 | } |
| 223 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 224 | /* Signed integer versions : return the same data but signed */ |
| 225 | |
| 226 | /* Read an int16_t in native host order */ |
| 227 | static inline int16_t read_i16(const void *p) |
| 228 | { |
| 229 | return read_u16(p); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 232 | /* Read an int32_t in native host order */ |
| 233 | static inline int32_t read_i32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 234 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 235 | return read_u32(p); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 238 | /* Read an int64_t in native host order */ |
| 239 | static inline int64_t read_i64(const void *p) |
| 240 | { |
| 241 | return read_u64(p); |
| 242 | } |
| 243 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 244 | /* Read a possibly wrapping int16_t in native host order */ |
| 245 | static inline int16_t readv_i16(const void *p1, size_t s1, const void *p2) |
| 246 | { |
| 247 | return readv_u16(p1, s1, p2); |
| 248 | } |
| 249 | |
| 250 | /* Read a possibly wrapping int32_t in native host order */ |
| 251 | static inline int32_t readv_i32(const void *p1, size_t s1, const void *p2) |
| 252 | { |
| 253 | return readv_u32(p1, s1, p2); |
| 254 | } |
| 255 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 256 | /* Read a possibly wrapping int64_t in native host order */ |
| 257 | static inline int64_t readv_i64(const void *p1, size_t s1, const void *p2) |
| 258 | { |
| 259 | return readv_u64(p1, s1, p2); |
| 260 | } |
| 261 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 262 | /* Read a uint16_t, and convert from network order to host order */ |
| 263 | static inline uint16_t read_n16(const void *p) |
| 264 | { |
| 265 | return ntohs(read_u16(p)); |
| 266 | } |
| 267 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 268 | /* Write a uint16_t after converting it from host order to network order */ |
| 269 | static inline void write_n16(void *p, const uint16_t u16) |
| 270 | { |
| 271 | write_u16(p, htons(u16)); |
| 272 | } |
| 273 | |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 274 | /* Read a uint32_t, and convert from network order to host order */ |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 275 | static inline uint32_t read_n32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 276 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 277 | return ntohl(read_u32(p)); |
| 278 | } |
| 279 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 280 | /* Write a uint32_t after converting it from host order to network order */ |
| 281 | static inline void write_n32(void *p, const uint32_t u32) |
| 282 | { |
| 283 | write_u32(p, htonl(u32)); |
| 284 | } |
| 285 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 286 | /* Read a uint64_t, and convert from network order to host order */ |
| 287 | static inline uint64_t read_n64(const void *p) |
| 288 | { |
| 289 | return my_ntohll(read_u64(p)); |
| 290 | } |
| 291 | |
| 292 | /* Write a uint64_t after converting it from host order to network order */ |
| 293 | static inline void write_n64(void *p, const uint64_t u64) |
| 294 | { |
| 295 | write_u64(p, my_htonll(u64)); |
| 296 | } |
| 297 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 298 | /* Read a possibly wrapping uint16_t in network order. The first segment is |
| 299 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 300 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 301 | * responsible for providing enough bytes. |
| 302 | */ |
| 303 | static inline uint16_t readv_n16(const void *p1, size_t s1, const void *p2) |
| 304 | { |
| 305 | if (unlikely(s1 < 2)) { |
| 306 | if (s1 == 0) |
| 307 | p1 = p2++; |
| 308 | } |
| 309 | else |
| 310 | p2 = p1 + 1; |
| 311 | return (*(uint8_t *)p1 << 8) + *(uint8_t *)p2; |
| 312 | } |
| 313 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 314 | /* Write a possibly wrapping uint16_t in network order. The first segment is |
| 315 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 316 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 317 | * responsible for providing enough room. |
| 318 | */ |
| 319 | static inline void writev_n16(const void *p1, size_t s1, const void *p2, const uint16_t u16) |
| 320 | { |
| 321 | if (unlikely(s1 < 2)) { |
| 322 | if (s1 == 0) |
| 323 | p1 = p2++; |
| 324 | } |
| 325 | else |
| 326 | p2 = p1 + 1; |
| 327 | *(uint8_t *)p1 = u16 >> 8; |
| 328 | *(uint8_t *)p2 = u16; |
| 329 | } |
| 330 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 331 | /* Read a possibly wrapping uint32_t in network order. The first segment is |
| 332 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 333 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 334 | * responsible for providing enough bytes. |
| 335 | */ |
| 336 | static inline uint32_t readv_n32(const void *p1, size_t s1, const void *p2) |
| 337 | { |
| 338 | return ntohl(readv_u32(p1, s1, p2)); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Willy Tarreau | 2888c08 | 2017-09-19 17:27:05 +0200 | [diff] [blame] | 341 | /* Write a possibly wrapping uint32_t in network order. The first segment is |
| 342 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 343 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 344 | * responsible for providing enough room. |
| 345 | */ |
| 346 | static inline void writev_n32(void *p1, size_t s1, void *p2, const uint32_t u32) |
| 347 | { |
| 348 | writev_u32(p1, s1, p2, htonl(u32)); |
| 349 | } |
| 350 | |
Willy Tarreau | 5531d57 | 2017-09-20 08:14:52 +0200 | [diff] [blame] | 351 | /* Read a possibly wrapping uint64_t in network order. The first segment is |
| 352 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 353 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 354 | * responsible for providing enough bytes. |
| 355 | */ |
| 356 | static inline uint64_t readv_n64(const void *p1, size_t s1, const void *p2) |
| 357 | { |
| 358 | return my_ntohll(readv_u64(p1, s1, p2)); |
| 359 | } |
| 360 | |
| 361 | /* Write a possibly wrapping uint64_t in network order. The first segment is |
| 362 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are written to |
| 363 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 364 | * responsible for providing enough room. |
| 365 | */ |
| 366 | static inline void writev_n64(void *p1, size_t s1, void *p2, const uint64_t u64) |
| 367 | { |
| 368 | writev_u64(p1, s1, p2, my_htonll(u64)); |
| 369 | } |
| 370 | |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 371 | #endif /* COMMON_NET_HELPER_H */ |