blob: 7d5636df782c52e0c5d24a044cecd27190d8ffc3 [file] [log] [blame]
Olivier Houcharde962fd82017-08-07 19:20:04 +02001/*
2 * include/common/net_helper.h
3 * This file contains miscellaneous network helper functions.
4 *
5 * Copyright (C) 2017 Olivier Houchard
Willy Tarreaud5370e12017-09-19 14:59:52 +02006 * Copyright (C) 2017 Willy Tarreau
Olivier Houcharde962fd82017-08-07 19:20:04 +02007 *
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 Tarreaud5370e12017-09-19 14:59:52 +020030#include <common/compiler.h>
Olivier Houcharde962fd82017-08-07 19:20:04 +020031#include <arpa/inet.h>
32
Willy Tarreaud5370e12017-09-19 14:59:52 +020033/* Functions to read various integers that may be unaligned */
Olivier Houcharde962fd82017-08-07 19:20:04 +020034
Willy Tarreaud5370e12017-09-19 14:59:52 +020035/* Read a uint16_t in native host order */
36static inline uint16_t read_u16(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020037{
38 const union { uint16_t u16; } __attribute__((packed))*u = p;
39 return u->u16;
40}
41
Willy Tarreaud5370e12017-09-19 14:59:52 +020042/* Read a uint32_t in native host order */
43static inline uint32_t read_u32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020044{
Willy Tarreaud5370e12017-09-19 14:59:52 +020045 const union { uint32_t u32; } __attribute__((packed))*u = p;
46 return u->u32;
Olivier Houcharde962fd82017-08-07 19:20:04 +020047}
48
Willy Tarreaud5370e12017-09-19 14:59:52 +020049/* 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 */
58static void readv_bytes(void *dst, const size_t bytes, const void *p1, size_t s1, const void *p2)
Olivier Houcharde962fd82017-08-07 19:20:04 +020059{
Willy Tarreaud5370e12017-09-19 14:59:52 +020060 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 Houcharde962fd82017-08-07 19:20:04 +020073}
74
Willy Tarreaud5370e12017-09-19 14:59:52 +020075/* 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 */
80static inline uint16_t readv_u16(const void *p1, size_t s1, const void *p2)
Olivier Houcharde962fd82017-08-07 19:20:04 +020081{
Willy Tarreaud5370e12017-09-19 14:59:52 +020082 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 */
102static 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 */
116static inline int16_t read_i16(const void *p)
117{
118 return read_u16(p);
Olivier Houcharde962fd82017-08-07 19:20:04 +0200119}
120
Willy Tarreaud5370e12017-09-19 14:59:52 +0200121/* Read an int32_t in native host order */
122static inline int32_t read_i32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +0200123{
Willy Tarreaud5370e12017-09-19 14:59:52 +0200124 return read_u32(p);
Olivier Houcharde962fd82017-08-07 19:20:04 +0200125}
126
Willy Tarreaud5370e12017-09-19 14:59:52 +0200127/* Read a possibly wrapping int16_t in native host order */
128static 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 */
134static 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 */
140static inline uint16_t read_n16(const void *p)
141{
142 return ntohs(read_u16(p));
143}
144
Olivier Houcharde962fd82017-08-07 19:20:04 +0200145/* Read a uint32_t, and convert from network order to host order */
Willy Tarreaud5370e12017-09-19 14:59:52 +0200146static inline uint32_t read_n32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +0200147{
Willy Tarreaud5370e12017-09-19 14:59:52 +0200148 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 */
156static 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 */
172static inline uint32_t readv_n32(const void *p1, size_t s1, const void *p2)
173{
174 return ntohl(readv_u32(p1, s1, p2));
Olivier Houcharde962fd82017-08-07 19:20:04 +0200175}
176
177#endif /* COMMON_NET_HELPER_H */