blob: 78975144cbddbda5a5171dcb1c9ac238b4b083c8 [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
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#ifndef _COMMON_NET_HELPER_H
27#define _COMMON_NET_HELPER_H
28
29#include <arpa/inet.h>
30
31/* Functions to read various integer that may be unaligned */
32
33/* Read a uint16_t */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020034static inline uint16_t readu16(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020035{
36 const union { uint16_t u16; } __attribute__((packed))*u = p;
37 return u->u16;
38}
39
40/* Read a int16_t */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020041static inline int16_t readi16(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020042{
43 const union { int16_t i16; } __attribute__((packed))*u = p;
44 return u->i16;
45}
46
47/* Read a uint16_t, and convert from network order to host order */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020048static inline uint16_t readn16(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020049{
50 const union { uint16_t u16; } __attribute__((packed))*u = p;
51 return ntohs(u->u16);
52}
53
54/* Read a uint32_t */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020055static inline uint32_t readu32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020056{
57 const union { uint32_t u32; } __attribute__((packed))*u = p;
58 return u->u32;
59}
60
61/* Read a int32_t */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020062static inline int16_t readi32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020063{
64 const union { int32_t i32; } __attribute__((packed))*u = p;
65 return u->i32;
66}
67
68/* Read a uint32_t, and convert from network order to host order */
Olivier Houcharded0d96c2017-09-13 11:49:22 +020069static inline uint32_t readn32(const void *p)
Olivier Houcharde962fd82017-08-07 19:20:04 +020070{
71 const union { uint32_t u32; } __attribute__((packed))*u = p;
72 return ntohl(u->u32);
73}
74
75#endif /* COMMON_NET_HELPER_H */