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 |
| 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 */ |
| 34 | uint16_t readu16(const void *p) |
| 35 | { |
| 36 | const union { uint16_t u16; } __attribute__((packed))*u = p; |
| 37 | return u->u16; |
| 38 | } |
| 39 | |
| 40 | /* Read a int16_t */ |
| 41 | int16_t readi16(const void *p) |
| 42 | { |
| 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 */ |
| 48 | uint16_t readn16(const void *p) |
| 49 | { |
| 50 | const union { uint16_t u16; } __attribute__((packed))*u = p; |
| 51 | return ntohs(u->u16); |
| 52 | } |
| 53 | |
| 54 | /* Read a uint32_t */ |
| 55 | uint32_t readu32(const void *p) |
| 56 | { |
| 57 | const union { uint32_t u32; } __attribute__((packed))*u = p; |
| 58 | return u->u32; |
| 59 | } |
| 60 | |
| 61 | /* Read a int32_t */ |
| 62 | int16_t readi32(const void *p) |
| 63 | { |
| 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 */ |
| 69 | uint32_t readn32(const void *p) |
| 70 | { |
| 71 | const union { uint32_t u32; } __attribute__((packed))*u = p; |
| 72 | return ntohl(u->u32); |
| 73 | } |
| 74 | |
| 75 | #endif /* COMMON_NET_HELPER_H */ |