util.h (5159B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifndef __UTIL_H 24 #define __UTIL_H 25 26 #include <stdint.h> 27 #include <unistd.h> 28 #include <ell/cleanup.h> 29 30 #define ___PASTE(a, b) a ## b 31 #define __PASTE(a, b) ___PASTE(a, b) 32 #define UNIQUE_ID(x, id) __PASTE(__unique_prefix_, __PASTE(x, id)) 33 34 #define align_len(len, boundary) (((len)+(boundary)-1) & ~((boundary)-1)) 35 36 #define MAC "%02x:%02x:%02x:%02x:%02x:%02x" 37 #define MAC_STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] 38 39 const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid); 40 bool util_ssid_is_utf8(size_t len, const uint8_t *ssid); 41 bool util_ssid_is_hidden(size_t len, const uint8_t *ssid); 42 const char *util_address_to_string(const uint8_t *addr); 43 bool util_string_to_address(const char *str, uint8_t *addr); 44 bool util_is_group_address(const uint8_t *addr); 45 bool util_is_broadcast_address(const uint8_t *addr); 46 bool util_is_valid_sta_address(const uint8_t *addr); 47 48 const char *util_get_domain(const char *identity); 49 const char *util_get_username(const char *identity); 50 51 /* 52 * Returns either true_value or false_value (depending if mask is 0xFF or 0x00 53 * respectively). 54 * This constant time selection method allows to keep an identical memory 55 * access pattern. 56 */ 57 static inline uint8_t util_secure_select_byte(uint8_t mask, 58 const uint8_t true_value, 59 const uint8_t false_value) 60 { 61 return (mask & true_value) | (~mask & false_value); 62 } 63 64 /* 65 * Copies either true_value or false_value (depending if mask is 0xFF or 0x00 66 * respectively) into dest. All three buffers are assumed to be the same size. 67 * This constant time selection method allows to keep an identical memory 68 * access pattern. 69 */ 70 static inline void util_secure_select(uint8_t mask, const uint8_t *true_value, 71 const uint8_t *false_value, 72 uint8_t *dest, size_t size) 73 { 74 size_t i = 0; 75 76 for (; i < size; i++) 77 dest[i] = util_secure_select_byte(mask, true_value[i], 78 false_value[i]); 79 } 80 81 /* Create a value filled with the MSB of the input. */ 82 static inline uint32_t util_secure_fill_with_msb(uint32_t val) 83 { 84 return (uint32_t) (val >> (sizeof(val)*8 - 1)) * 0xFFFFFFFF; 85 } 86 87 bool util_ip_prefix_tohl(const char *ip, uint8_t *prefix, uint32_t *start_out, 88 uint32_t *end_out, uint32_t *mask_out); 89 90 /* Host byte-order IPv4 netmask */ 91 static inline uint32_t util_netmask_from_prefix(uint8_t prefix_len) 92 { 93 return ~((1ull << (32 - prefix_len)) - 1); 94 } 95 96 /* Expects network byte-order (big-endian) addresses */ 97 static inline bool util_ip_subnet_match(uint8_t prefix_len, 98 const void *addr1, const void *addr2) 99 { 100 const uint8_t *u8_1 = addr1; 101 const uint8_t *u8_2 = addr2; 102 uint8_t pref_bytes = prefix_len / 8; 103 104 return (!pref_bytes || !memcmp(u8_1, u8_2, pref_bytes)) && 105 !((u8_1[pref_bytes] ^ u8_2[pref_bytes]) & 106 ~((1u << (8 - (prefix_len % 8))) - 1)); 107 } 108 109 /* 110 * Linearly maps (interpolates) 'value' from range 'a' to range 'b' 111 * 112 * Fails if: 113 * - value is not between a and b 114 * - a_start == a_end 115 * - b_start == b_end 116 */ 117 bool util_linear_map(double value, double a_start, double a_end, 118 double b_start, double b_end, double *mapped_value); 119 120 typedef void (*scan_freq_set_func_t)(uint32_t freq, void *userdata); 121 122 struct scan_freq_set *scan_freq_set_new(void); 123 void scan_freq_set_free(struct scan_freq_set *freqs); 124 bool scan_freq_set_add(struct scan_freq_set *freqs, uint32_t freq); 125 bool scan_freq_set_remove(struct scan_freq_set *freqs, uint32_t freq); 126 bool scan_freq_set_contains(const struct scan_freq_set *freqs, uint32_t freq); 127 uint32_t scan_freq_set_get_bands(const struct scan_freq_set *freqs); 128 void scan_freq_set_foreach(const struct scan_freq_set *freqs, 129 scan_freq_set_func_t func, void *user_data); 130 void scan_freq_set_merge(struct scan_freq_set *to, 131 const struct scan_freq_set *from); 132 void scan_freq_set_constrain(struct scan_freq_set *set, 133 const struct scan_freq_set *constraint); 134 void scan_freq_set_subtract(struct scan_freq_set *set, 135 const struct scan_freq_set *subtract); 136 bool scan_freq_set_isempty(const struct scan_freq_set *set); 137 uint32_t *scan_freq_set_to_fixed_array(const struct scan_freq_set *set, 138 size_t *len_out); 139 struct scan_freq_set *scan_freq_set_clone(const struct scan_freq_set *set, 140 uint32_t band_mask); 141 142 DEFINE_CLEANUP_FUNC(scan_freq_set_free); 143 144 double util_exponential_decay(unsigned int n); 145 146 #endif /* __UTIL_H */