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> |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 33 | /* Functions to read various integers that may be unaligned */ |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 35 | /* Read a uint16_t in native host order */ |
| 36 | static inline uint16_t read_u16(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 37 | { |
| 38 | const union { uint16_t u16; } __attribute__((packed))*u = p; |
| 39 | return u->u16; |
| 40 | } |
| 41 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 42 | /* Read a uint32_t in native host order */ |
| 43 | static inline uint32_t read_u32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 45 | const union { uint32_t u32; } __attribute__((packed))*u = p; |
| 46 | return u->u32; |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 49 | /* Read a possibly wrapping number of bytes <bytes> into destination <dst>. The |
| 50 | * first segment is composed of <s1> bytes at p1. The remaining byte(s), if any, |
| 51 | * are read from <p2>. <s1> may be zero and may also be larger than <bytes>. The |
| 52 | * caller is always responsible for providing enough bytes. Note: the function |
| 53 | * is purposely *not* marked inline to let the compiler decide what to do with |
| 54 | * it, because it's around 34 bytes long, placed on critical path but rarely |
| 55 | * called, and uses uses a lot of arguments if not inlined. The compiler will |
| 56 | * thus decide what's best to do with it depending on the context. |
| 57 | */ |
| 58 | 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] | 59 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 60 | size_t idx; |
| 61 | |
| 62 | p2 -= s1; |
| 63 | for (idx = 0; idx < bytes; idx++) { |
| 64 | if (idx == s1) |
| 65 | p1 = p2; |
| 66 | ((uint8_t *)dst)[idx] = ((const uint8_t *)p1)[idx]; |
| 67 | } |
| 68 | /* this memory barrier is critical otherwise gcc may over-optimize this |
| 69 | * code, completely removing it as well as any surrounding boundary |
| 70 | * check (4.7.1..6.4.0)! |
| 71 | */ |
| 72 | __asm__ volatile("" ::: "memory"); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 75 | /* Read a possibly wrapping uint16_t in native host order. The first segment is |
| 76 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 77 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 78 | * responsible for providing enough bytes. |
| 79 | */ |
| 80 | 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] | 81 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 82 | if (unlikely(s1 == 1)) { |
| 83 | volatile uint16_t u16; |
| 84 | |
| 85 | ((uint8_t *)&u16)[0] = *(uint8_t *)p1; |
| 86 | ((uint8_t *)&u16)[1] = *(uint8_t *)p2; |
| 87 | return u16; |
| 88 | } |
| 89 | else { |
| 90 | const union { uint16_t u16; } __attribute__((packed)) *u; |
| 91 | |
| 92 | u = (s1 == 0) ? p2 : p1; |
| 93 | return u->u16; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Read a possibly wrapping uint32_t in native host order. The first segment is |
| 98 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 99 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 100 | * responsible for providing enough bytes. |
| 101 | */ |
| 102 | static inline uint32_t readv_u32(const void *p1, size_t s1, const void *p2) |
| 103 | { |
| 104 | uint32_t u32; |
| 105 | |
| 106 | if (!unlikely(s1 < sizeof(u32))) |
| 107 | u32 = read_u32(p1); |
| 108 | else |
| 109 | readv_bytes(&u32, sizeof(u32), p1, s1, p2); |
| 110 | return u32; |
| 111 | } |
| 112 | |
| 113 | /* Signed integer versions : return the same data but signed */ |
| 114 | |
| 115 | /* Read an int16_t in native host order */ |
| 116 | static inline int16_t read_i16(const void *p) |
| 117 | { |
| 118 | return read_u16(p); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 121 | /* Read an int32_t in native host order */ |
| 122 | static inline int32_t read_i32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 123 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 124 | return read_u32(p); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 127 | /* Read a possibly wrapping int16_t in native host order */ |
| 128 | static inline int16_t readv_i16(const void *p1, size_t s1, const void *p2) |
| 129 | { |
| 130 | return readv_u16(p1, s1, p2); |
| 131 | } |
| 132 | |
| 133 | /* Read a possibly wrapping int32_t in native host order */ |
| 134 | static inline int32_t readv_i32(const void *p1, size_t s1, const void *p2) |
| 135 | { |
| 136 | return readv_u32(p1, s1, p2); |
| 137 | } |
| 138 | |
| 139 | /* Read a uint16_t, and convert from network order to host order */ |
| 140 | static inline uint16_t read_n16(const void *p) |
| 141 | { |
| 142 | return ntohs(read_u16(p)); |
| 143 | } |
| 144 | |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 145 | /* Read a uint32_t, and convert from network order to host order */ |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 146 | static inline uint32_t read_n32(const void *p) |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 147 | { |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame^] | 148 | return ntohl(read_u32(p)); |
| 149 | } |
| 150 | |
| 151 | /* Read a possibly wrapping uint16_t in network order. The first segment is |
| 152 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 153 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 154 | * responsible for providing enough bytes. |
| 155 | */ |
| 156 | static inline uint16_t readv_n16(const void *p1, size_t s1, const void *p2) |
| 157 | { |
| 158 | if (unlikely(s1 < 2)) { |
| 159 | if (s1 == 0) |
| 160 | p1 = p2++; |
| 161 | } |
| 162 | else |
| 163 | p2 = p1 + 1; |
| 164 | return (*(uint8_t *)p1 << 8) + *(uint8_t *)p2; |
| 165 | } |
| 166 | |
| 167 | /* Read a possibly wrapping uint32_t in network order. The first segment is |
| 168 | * composed of <s1> bytes at p1. The remaining byte(s), if any, are read from |
| 169 | * <p2>. <s1> may be zero and may be larger than the type. The caller is always |
| 170 | * responsible for providing enough bytes. |
| 171 | */ |
| 172 | static inline uint32_t readv_n32(const void *p1, size_t s1, const void *p2) |
| 173 | { |
| 174 | return ntohl(readv_u32(p1, s1, p2)); |
Olivier Houchard | e962fd8 | 2017-08-07 19:20:04 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #endif /* COMMON_NET_HELPER_H */ |