EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
inet.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/inet.h: POSIX-like identifiers for IP addresses, network interfaces and related items. */
21 
22 #ifndef ETCPAL_INET_H_
23 #define ETCPAL_INET_H_
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include "etcpal/error.h"
29 #include "etcpal/os_inet.h"
30 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
52 typedef enum
53 {
61 
63 #define ETCPAL_IPV6_BYTES 16
64 
74 typedef struct EtcPalIpAddr
75 {
79  union
80  {
82  uint32_t v4;
84  struct EtcPalIpv6Addr
85  {
87  unsigned long scope_id;
88  } v6;
89  } addr;
91 
110 #define ETCPAL_IP_INVALID_INIT_VALUES \
111  kEtcPalIpTypeInvalid, { 0 }
112 
121 #define ETCPAL_IP_INVALID_INIT \
122  { \
123  ETCPAL_IP_INVALID_INIT_VALUES \
124  }
125 
138 #define ETCPAL_IPV4_INIT_VALUES(v4_val) \
139  kEtcPalIpTypeV4, { .v4 = v4_val }
140 
151 #define ETCPAL_IPV4_INIT(addr_val) \
152  { \
153  ETCPAL_IPV4_INIT_VALUES(addr_val) \
154  }
155 
174 #define ETCPAL_IPV6_INIT_VALUES(...) \
175  kEtcPalIpTypeV6, \
176  { \
177  .v6 = {.addr_buf = {__VA_ARGS__}, .scope_id = 0 } \
178  }
179 
195 #define ETCPAL_IPV6_INIT(...) \
196  { \
197  ETCPAL_IPV6_INIT_VALUES(__VA_ARGS__) \
198  }
199 
205 #define ETCPAL_IP_IS_V4(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeV4)
206 
212 #define ETCPAL_IP_IS_V6(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeV6)
213 
219 #define ETCPAL_IP_IS_INVALID(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeInvalid)
220 
230 #define ETCPAL_IP_V4_ADDRESS(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v4)
231 
241 #define ETCPAL_IP_V6_ADDRESS(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v6.addr_buf)
242 
252 #define ETCPAL_IP_V6_SCOPE_ID(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v6.scope_id)
253 
262 #define ETCPAL_IP_SET_V4_ADDRESS(etcpal_ip_ptr, val) \
263  do \
264  { \
265  (etcpal_ip_ptr)->type = kEtcPalIpTypeV4; \
266  (etcpal_ip_ptr)->addr.v4 = val; \
267  } while (0)
268 
278 #define ETCPAL_IP_SET_V6_ADDRESS(etcpal_ip_ptr, addr_val) \
279  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(etcpal_ip_ptr, addr_val, 0u)
280 
291 #define ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(etcpal_ip_ptr, addr_val, scope_id_val) \
292  do \
293  { \
294  (etcpal_ip_ptr)->type = kEtcPalIpTypeV6; \
295  memcpy((etcpal_ip_ptr)->addr.v6.addr_buf, (addr_val), ETCPAL_IPV6_BYTES); \
296  (etcpal_ip_ptr)->addr.v6.scope_id = (scope_id_val); \
297  } while (0)
298 
303 #define ETCPAL_IP_SET_INVALID(etcpal_ip_ptr) ((etcpal_ip_ptr)->type = kEtcPalIpTypeInvalid)
304 
310 typedef struct EtcPalSockAddr
311 {
312  uint16_t port;
315 
317 #define ETCPAL_MAC_BYTES 6
318 
320 typedef struct EtcPalMacAddr
321 {
324 
333 #define ETCPAL_MAC_CMP(mac1ptr, mac2ptr) memcmp((mac1ptr)->data, (mac2ptr)->data, ETCPAL_MAC_BYTES)
334 
336 extern const EtcPalMacAddr kEtcPalNullMacAddr;
337 
346 #define ETCPAL_MAC_IS_NULL(macptr) (memcmp((macptr)->data, kEtcPalNullMacAddr.data, ETCPAL_MAC_BYTES) == 0)
347 
349 #define ETCPAL_NETINTINFO_ID_LEN 64
351 #define ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN 64
352 
354 typedef struct EtcPalNetintInfo
355 {
361  unsigned int index;
383 
387 typedef struct EtcPalMcastNetintId
388 {
390  unsigned int index;
392 
394 #define ETCPAL_IP_STRING_BYTES 46
396 #define ETCPAL_MAC_STRING_BYTES 18
397 
398 bool etcpal_ip_is_link_local(const EtcPalIpAddr* ip);
399 bool etcpal_ip_is_loopback(const EtcPalIpAddr* ip);
400 bool etcpal_ip_is_multicast(const EtcPalIpAddr* ip);
401 bool etcpal_ip_is_wildcard(const EtcPalIpAddr* ip);
403 
404 int etcpal_ip_cmp(const EtcPalIpAddr* ip1, const EtcPalIpAddr* ip2);
405 bool etcpal_ip_and_port_equal(const EtcPalSockAddr* sock1, const EtcPalSockAddr* sock2);
406 
407 unsigned int etcpal_ip_mask_length(const EtcPalIpAddr* netmask);
408 EtcPalIpAddr etcpal_ip_mask_from_length(etcpal_iptype_t type, unsigned int mask_length);
409 bool etcpal_ip_network_portions_equal(const EtcPalIpAddr* ip1, const EtcPalIpAddr* ip2, const EtcPalIpAddr* netmask);
410 
411 bool ip_os_to_etcpal(const etcpal_os_ipaddr_t* os_ip, EtcPalIpAddr* ip);
412 size_t ip_etcpal_to_os(const EtcPalIpAddr* ip, etcpal_os_ipaddr_t* os_ip);
413 
414 bool sockaddr_os_to_etcpal(const etcpal_os_sockaddr_t* os_sa, EtcPalSockAddr* sa);
415 size_t sockaddr_etcpal_to_os(const EtcPalSockAddr* sa, etcpal_os_sockaddr_t* os_sa);
416 
419 etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr* src, char* dest);
420 etcpal_error_t etcpal_string_to_mac(const char* src, EtcPalMacAddr* dest);
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 
426 #ifdef __cplusplus
427 
428 /* C++ utilities */
429 
430 /* Comparison operators for IpAddrs */
431 
432 inline bool operator==(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
433 {
434  return (etcpal_ip_cmp(&a, &b) == 0);
435 }
436 
437 inline bool operator!=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
438 {
439  return !(a == b);
440 }
441 
442 inline bool operator<(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
443 {
444  return (etcpal_ip_cmp(&a, &b) < 0);
445 }
446 
447 inline bool operator>(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
448 {
449  return b < a;
450 }
451 
452 inline bool operator<=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
453 {
454  return !(b < a);
455 }
456 
457 inline bool operator>=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
458 {
459  return !(a < b);
460 }
461 
462 /* Comparison operators for SockAddrs */
463 
464 inline bool operator==(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
465 {
466  return (a.ip == b.ip && a.port == b.port);
467 }
468 
469 inline bool operator!=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
470 {
471  return !(a == b);
472 }
473 
474 inline bool operator<(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
475 {
476  if (a.port == b.port)
477  return (etcpal_ip_cmp(&a.ip, &b.ip) < 0);
478  return (a.port < b.port);
479 }
480 
481 inline bool operator>(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
482 {
483  return b < a;
484 }
485 
486 inline bool operator<=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
487 {
488  return !(b < a);
489 }
490 
491 inline bool operator>=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
492 {
493  return !(a < b);
494 }
495 
496 /* Comparison operators for MacAddrs */
497 
498 inline bool operator==(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
499 {
500  return (ETCPAL_MAC_CMP(&a, &b) == 0);
501 }
502 
503 inline bool operator!=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
504 {
505  return !(a == b);
506 }
507 
508 inline bool operator<(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
509 {
510  return (ETCPAL_MAC_CMP(&a, &b) < 0);
511 }
512 
513 inline bool operator>(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
514 {
515  return b < a;
516 }
517 
518 inline bool operator<=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
519 {
520  return !(b < a);
521 }
522 
523 inline bool operator>=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
524 {
525  return !(a < b);
526 }
527 
528 #endif
529 
534 #endif /* ETCPAL_INET_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
struct EtcPalNetintInfo EtcPalNetintInfo
A description of a single address assigned to a network interface.
bool sockaddr_os_to_etcpal(const etcpal_os_sockaddr_t *os_sa, EtcPalSockAddr *sa)
Convert a platform-specific socket address to an EtcPalSockAddr.
EtcPalIpAddr etcpal_ip_mask_from_length(etcpal_iptype_t type, unsigned int mask_length)
Create a netmask given a length in bits.
Definition: inet.c:311
bool etcpal_ip_network_portions_equal(const EtcPalIpAddr *ip1, const EtcPalIpAddr *ip2, const EtcPalIpAddr *netmask)
Compare the network portions of two IP addresses using a netmask.
Definition: inet.c:379
#define ETCPAL_MAC_BYTES
The number of bytes in a MAC address.
Definition: inet.h:317
#define ETCPAL_MAC_CMP(mac1ptr, mac2ptr)
Compare two EtcPalMacAddrs numerically.
Definition: inet.h:333
void etcpal_ip_set_wildcard(etcpal_iptype_t type, EtcPalIpAddr *ip)
Initialize a EtcPalIpAddr with a wildcard address.
Definition: inet.c:174
bool etcpal_ip_is_loopback(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a loopback address.
Definition: inet.c:88
struct EtcPalMcastNetintId EtcPalMcastNetintId
A set of identifying information for a network interface, for multicast purposes.
bool etcpal_ip_is_multicast(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a multicast address.
Definition: inet.c:115
#define ETCPAL_NETINTINFO_ID_LEN
The maximum length of a network interface id.
Definition: inet.h:349
#define ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN
The maximum length of a user-friendly network interface name.
Definition: inet.h:351
struct EtcPalIpAddr EtcPalIpAddr
An IP address.
etcpal_iptype_t
Used to identify the type of IP address contained in a EtcPalIpAddr.
Definition: inet.h:53
etcpal_error_t etcpal_string_to_ip(etcpal_iptype_t type, const char *src, EtcPalIpAddr *dest)
Convert IPv4 and IPv6 addresses from text to binary form.
unsigned int etcpal_ip_mask_length(const EtcPalIpAddr *netmask)
Get the length in bits of a netmask.
Definition: inet.c:256
bool etcpal_ip_is_link_local(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a link-local address.
Definition: inet.c:60
size_t sockaddr_etcpal_to_os(const EtcPalSockAddr *sa, etcpal_os_sockaddr_t *os_sa)
Convert an EtcPalSockAddr to a platform-specific socket address.
struct EtcPalSockAddr EtcPalSockAddr
An IP address with associated interface and port.
etcpal_error_t etcpal_string_to_mac(const char *src, EtcPalMacAddr *dest)
Create a MAC address from a string representation.
Definition: inet.c:524
bool ip_os_to_etcpal(const etcpal_os_ipaddr_t *os_ip, EtcPalIpAddr *ip)
Convert a platform-specific IP address to an EtcPalIpAddr.
#define ETCPAL_IPV6_BYTES
The number of bytes in an IPv6 address.
Definition: inet.h:63
const EtcPalMacAddr kEtcPalNullMacAddr
A null (all 0's) MAC address, used by ETCPAL_MAC_IS_NULL() for comparison.
Definition: inet.c:24
bool etcpal_ip_is_wildcard(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a wildcard address.
Definition: inet.c:146
etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr *src, char *dest)
Create a string representation of a MAC address.
Definition: inet.c:502
struct EtcPalMacAddr EtcPalMacAddr
A MAC address.
int etcpal_ip_cmp(const EtcPalIpAddr *ip1, const EtcPalIpAddr *ip2)
Compare two EtcPalIpAddrs.
Definition: inet.c:208
bool etcpal_ip_and_port_equal(const EtcPalSockAddr *sock1, const EtcPalSockAddr *sock2)
Determine whether two instances of EtcPalSockAddr contain identical IP addresses and ports.
Definition: inet.c:236
etcpal_error_t etcpal_ip_to_string(const EtcPalIpAddr *src, char *dest)
Convert IPv4 and IPv6 addresses from binary to text form.
size_t ip_etcpal_to_os(const EtcPalIpAddr *ip, etcpal_os_ipaddr_t *os_ip)
Convert an EtcPalIpAddr to a platform-specific IP address.
@ kEtcPalIpTypeInvalid
This EtcPalIpAddr is not valid.
Definition: inet.h:55
@ kEtcPalIpTypeV4
This EtcPalIpAddr contains an IPv4 address.
Definition: inet.h:57
@ kEtcPalIpTypeV6
This EtcPalIpAddr contains an IPv6 address.
Definition: inet.h:59
An IP address.
Definition: inet.h:75
union EtcPalIpAddr::@0 addr
A union containing either the IPv4 or IPv6 address.
uint8_t addr_buf[ETCPAL_IPV6_BYTES]
The IPv6 address.
Definition: inet.h:86
uint32_t v4
The IPv4 address value in host byte order.
Definition: inet.h:82
unsigned long scope_id
The IPv6 scope ID.
Definition: inet.h:87
etcpal_iptype_t type
The IP address type (IPv4 or IPv6)
Definition: inet.h:77
A MAC address.
Definition: inet.h:321
uint8_t data[ETCPAL_MAC_BYTES]
The 6-byte address data.
Definition: inet.h:322
A set of identifying information for a network interface, for multicast purposes.
Definition: inet.h:388
unsigned int index
The OS index of the network interface.
Definition: inet.h:390
etcpal_iptype_t ip_type
The IP protocol used on the network interface.
Definition: inet.h:389
A description of a single address assigned to a network interface.
Definition: inet.h:355
bool is_default
Whether this is the default network interface.
Definition: inet.h:381
EtcPalIpAddr mask
The subnet mask for this interface.
Definition: inet.h:365
EtcPalMacAddr mac
The adapter MAC address.
Definition: inet.h:367
char friendly_name[ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN]
A user-friendly name for the interface.
Definition: inet.h:378
unsigned int index
The OS-specific network interface number.
Definition: inet.h:361
EtcPalIpAddr addr
The interface ip address.
Definition: inet.h:363
An IP address with associated interface and port.
Definition: inet.h:311
EtcPalIpAddr ip
IP address.
Definition: inet.h:313
uint16_t port
TCP or UDP port.
Definition: inet.h:312