Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-network.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <stdbool.h>
22 
23 #include <sol-common-buildopts.h>
24 #include <sol-vector.h>
25 #include <sol-str-slice.h>
26 #include <sol-buffer.h>
27 #include <sol-util.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
62 #define SOL_NETWORK_INET_ADDR_STR_LEN 48
63 
67 #define SOL_BLUETOOTH_ADDR_STRLEN 18
68 
83 
93 };
94 
107 };
108 
127 };
128 
140 };
141 
145 typedef struct sol_network_link_addr {
147  union {
148  uint8_t in[4];
149  uint8_t in6[16];
150  struct {
151  uint8_t bt_type;
152  uint8_t bt_addr[6];
153  };
154  } addr;
155  uint16_t port;
157 
166 typedef struct sol_network_link {
167 #ifndef SOL_NO_API_VERSION
168 #define SOL_NETWORK_LINK_API_VERSION (1)
169  uint16_t api_version;
170 #endif
171  uint16_t index;
179 
180 #ifndef SOL_NO_API_VERSION
181 
188 #define SOL_NETWORK_LINK_CHECK_VERSION(link_, ...) \
189  if (SOL_UNLIKELY((link_)->api_version != \
190  SOL_NETWORK_LINK_API_VERSION)) { \
191  SOL_WRN("Unexpected API version (message is %u, expected %u)", \
192  (link_)->api_version, SOL_NETWORK_LINK_API_VERSION); \
193  return __VA_ARGS__; \
194  }
195 #else
196 #define SOL_NETWORK_LINK_CHECK_VERSION(link_, ...)
197 #endif
198 
210 const char *sol_network_link_addr_to_str(const struct sol_network_link_addr *addr, struct sol_buffer *buf);
211 
223 
235 static inline bool
237  const struct sol_network_link_addr *b, bool compare_ports)
238 {
239  size_t bytes;
240 
241  if (compare_ports && (a->port != b->port))
242  return false;
243 
244  if (a->family == b->family) {
245  const uint8_t *addr_a, *addr_b;
246 
247  if (a->family == SOL_NETWORK_FAMILY_INET) {
248  addr_a = a->addr.in;
249  addr_b = b->addr.in;
250  bytes = sizeof(a->addr.in);
251  } else if (a->family == SOL_NETWORK_FAMILY_INET6) {
252  addr_a = a->addr.in6;
253  addr_b = b->addr.in6;
254  bytes = sizeof(a->addr.in6);
255  } else if (a->family == SOL_NETWORK_FAMILY_BLUETOOTH) {
256  if (a->addr.bt_type != b->addr.bt_type)
257  return false;
258 
259  addr_a = a->addr.bt_addr;
260  addr_b = b->addr.bt_addr;
261  bytes = sizeof(a->addr.bt_addr);
262  } else
263  return false;
264  return !memcmp(addr_a, addr_b, bytes);
265  }
266 
267  if ((a->family == SOL_NETWORK_FAMILY_INET &&
271 
272  struct ipv6_map_prefix {
273  const uint8_t zeroes[10];
274  const uint16_t ones;
275  } __attribute__ ((packed)) prefix = {
276  { 0 }, sol_util_be16_to_cpu(0xffff)
277  };
278  const uint8_t *addr_ipv6, *addr_ipv4;
279 
280 
281  if (a->family == SOL_NETWORK_FAMILY_INET6) {
282  addr_ipv6 = a->addr.in6;
283  addr_ipv4 = b->addr.in;
284  } else {
285  addr_ipv6 = b->addr.in6;
286  addr_ipv4 = a->addr.in;
287  }
288 
289  bytes = sizeof(a->addr.in);
290 
296  if (!memcmp(addr_ipv6, &prefix, sizeof(struct ipv6_map_prefix)) &&
297  !memcmp(addr_ipv6 + 12, addr_ipv4, bytes))
298  return true;
299  }
300 
301  return false;
302 }
303 
314 static inline bool
316  const struct sol_network_link_addr *b)
317 {
318  return sol_network_link_addr_eq_full(a, b, false);
319 }
334 int sol_network_subscribe_events(void (*cb)(void *data, const struct sol_network_link *link,
335  enum sol_network_event event),
336  const void *data);
337 
352 int sol_network_unsubscribe_events(void (*cb)(void *data, const struct sol_network_link *link,
353  enum sol_network_event event),
354  const void *data);
355 
367 const struct sol_vector *sol_network_get_available_links(void);
368 
377 char *sol_network_link_get_name(const struct sol_network_link *link);
378 
390 int sol_network_link_up(uint16_t link_index);
391 
403 int sol_network_link_down(uint16_t link_index);
404 
431  enum sol_network_family family, void (*host_info_cb)(void *data,
432  const struct sol_str_slice hostname, const struct sol_vector *addrs_list),
433  const void *data);
434 
443 
448 #ifdef __cplusplus
449 }
450 #endif
sol_network_event
Type of events generated for a network link.
Definition: sol-network.h:89
sol_network_bt_addr_type
Type of a Bluetooth address.
Definition: sol-network.h:136
Bluetooth "raw" family.
Definition: sol-network.h:122
Definition: sol-network.h:90
Definition: sol-network.h:139
Definition: sol-network.h:104
static bool sol_network_link_addr_eq(const struct sol_network_link_addr *a, const struct sol_network_link_addr *b)
Checks if two address are equal.
Definition: sol-network.h:315
struct sol_network_link sol_network_link
Structure to represent a network link.
int sol_network_link_up(uint16_t link_index)
Sets a network link up.
static uint16_t sol_util_be16_to_cpu(uint16_t val)
Convert a 16 bytes big endian integer to cpu endianness.
Definition: sol-util.h:880
Definition: sol-network.h:103
Definition: sol-network.h:105
struct sol_network_link_addr sol_network_link_addr
Structure to represent a network address, both IPv6 and IPv4 are valid.
Definition: sol-network.h:106
Unspecified address type.
Definition: sol-network.h:116
String slice type.
Definition: sol-str-slice.h:84
These are routines that Soletta provides for its buffer implementation.
int sol_network_unsubscribe_events(void(*cb)(void *data, const struct sol_network_link *link, enum sol_network_event event), const void *data)
Stops receiving network link events.
char * sol_network_link_get_name(const struct sol_network_link *link)
Gets the name of a network link.
IPv6 family.
Definition: sol-network.h:120
Useful general routines.
struct sol_network_hostname_pending * sol_network_get_hostname_address_info(const struct sol_str_slice hostname, enum sol_network_family family, void(*host_info_cb)(void *data, const struct sol_str_slice hostname, const struct sol_vector *addrs_list), const void *data)
Gets a hostname address info.
Bluetooth RFCOMM family.
Definition: sol-network.h:124
Definition: sol-network.h:92
sol_network_family
Type of a network address.
Definition: sol-network.h:114
const struct sol_vector * sol_network_get_available_links(void)
Retrieve the available network links on a system.
int sol_network_subscribe_events(void(*cb)(void *data, const struct sol_network_link *link, enum sol_network_event event), const void *data)
Subscribes to receive network link events.
sol_network_link_flags
Bitwise OR-ed flags to represent the status of a sol_network_link.
Definition: sol-network.h:101
static bool sol_network_link_addr_eq_full(const struct sol_network_link_addr *a, const struct sol_network_link_addr *b, bool compare_ports)
Checks if two address are equal - possibly including the port field.
Definition: sol-network.h:236
Definition: sol-network.h:137
int sol_network_hostname_pending_cancel(struct sol_network_hostname_pending *handle)
Cancels a request to get the hostname info.
const char * sol_network_link_addr_to_str(const struct sol_network_link_addr *addr, struct sol_buffer *buf)
Converts a sol_network_link_addr to a string.
These are routines that Soletta provides for its string slice implementation.
Soletta vector is an array that grows dynamically.
Definition: sol-vector.h:58
IPv4 family.
Definition: sol-network.h:118
const struct sol_network_link_addr * sol_network_link_addr_from_str(struct sol_network_link_addr *addr, const char *buf)
Converts a string address to sol_network_link_addr.
These are routines that Soletta provides for its vector implementation.
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
int sol_network_link_down(uint16_t link_index)
Sets a network link down.
struct sol_network_hostname_pending sol_network_hostname_pending
A handle returned by sol_network_get_hostname_address_info()
Definition: sol-network.h:82
Definition: sol-network.h:91
Definition: sol-network.h:138
Bluetooth L2CAP family.
Definition: sol-network.h:126
Definition: sol-network.h:102