EtcPal  HEAD (unstable)
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);
407 
408 unsigned int etcpal_ip_mask_length(const EtcPalIpAddr* netmask);
409 EtcPalIpAddr etcpal_ip_mask_from_length(etcpal_iptype_t type, unsigned int mask_length);
410 bool etcpal_ip_network_portions_equal(const EtcPalIpAddr* ip1, const EtcPalIpAddr* ip2, const EtcPalIpAddr* netmask);
411 
412 bool ip_os_to_etcpal(const etcpal_os_ipaddr_t* os_ip, EtcPalIpAddr* ip);
413 size_t ip_etcpal_to_os(const EtcPalIpAddr* ip, etcpal_os_ipaddr_t* os_ip);
414 
415 bool sockaddr_os_to_etcpal(const etcpal_os_sockaddr_t* os_sa, EtcPalSockAddr* sa);
416 size_t sockaddr_etcpal_to_os(const EtcPalSockAddr* sa, etcpal_os_sockaddr_t* os_sa);
417 
420 etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr* src, char* dest);
421 etcpal_error_t etcpal_string_to_mac(const char* src, EtcPalMacAddr* dest);
422 
423 #ifdef __cplusplus
424 }
425 #endif
426 
427 #ifdef __cplusplus
428 
429 /* C++ utilities */
430 
431 /* Comparison operators for IpAddrs */
432 
433 inline bool operator==(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
434 {
435  return (etcpal_ip_cmp(&a, &b) == 0);
436 }
437 
438 inline bool operator!=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
439 {
440  return !(a == b);
441 }
442 
443 inline bool operator<(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
444 {
445  return (etcpal_ip_cmp(&a, &b) < 0);
446 }
447 
448 inline bool operator>(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
449 {
450  return b < a;
451 }
452 
453 inline bool operator<=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
454 {
455  return !(b < a);
456 }
457 
458 inline bool operator>=(const EtcPalIpAddr& a, const EtcPalIpAddr& b)
459 {
460  return !(a < b);
461 }
462 
463 /* Comparison operators for SockAddrs */
464 
465 inline bool operator==(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
466 {
467  return (a.ip == b.ip && a.port == b.port);
468 }
469 
470 inline bool operator!=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
471 {
472  return !(a == b);
473 }
474 
475 inline bool operator<(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
476 {
477  if (a.port == b.port)
478  return (etcpal_ip_cmp(&a.ip, &b.ip) < 0);
479  return (a.port < b.port);
480 }
481 
482 inline bool operator>(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
483 {
484  return b < a;
485 }
486 
487 inline bool operator<=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
488 {
489  return !(b < a);
490 }
491 
492 inline bool operator>=(const EtcPalSockAddr& a, const EtcPalSockAddr& b)
493 {
494  return !(a < b);
495 }
496 
497 /* Comparison operators for MacAddrs */
498 
499 inline bool operator==(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
500 {
501  return (ETCPAL_MAC_CMP(&a, &b) == 0);
502 }
503 
504 inline bool operator!=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
505 {
506  return !(a == b);
507 }
508 
509 inline bool operator<(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
510 {
511  return (ETCPAL_MAC_CMP(&a, &b) < 0);
512 }
513 
514 inline bool operator>(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
515 {
516  return b < a;
517 }
518 
519 inline bool operator<=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
520 {
521  return !(b < a);
522 }
523 
524 inline bool operator>=(const EtcPalMacAddr& a, const EtcPalMacAddr& b)
525 {
526  return !(a < b);
527 }
528 
529 /* Comparison operators for EtcPalNetintInfos */
530 
531 inline bool operator==(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
532 {
533  return (etcpal_netint_info_cmp(&a, &b) == 0);
534 }
535 
536 inline bool operator!=(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
537 {
538  return !(a == b);
539 }
540 
541 inline bool operator<(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
542 {
543  return (etcpal_netint_info_cmp(&a, &b) < 0);
544 }
545 
546 inline bool operator>(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
547 {
548  return b < a;
549 }
550 
551 inline bool operator<=(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
552 {
553  return !(b < a);
554 }
555 
556 inline bool operator>=(const EtcPalNetintInfo& a, const EtcPalNetintInfo& b)
557 {
558  return !(a < b);
559 }
560 
561 /* Comparison operators for McastNetintIds */
562 
563 inline bool operator==(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
564 {
565  return (a.index == b.index) && (a.ip_type == b.ip_type);
566 }
567 
568 inline bool operator!=(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
569 {
570  return !(a == b);
571 }
572 
573 inline bool operator<(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
574 {
575  if (a.index == b.index)
576  return a.ip_type < b.ip_type;
577  return a.index < b.index;
578 }
579 
580 inline bool operator>(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
581 {
582  return b < a;
583 }
584 
585 inline bool operator<=(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
586 {
587  return !(b < a);
588 }
589 
590 inline bool operator>=(const EtcPalMcastNetintId& a, const EtcPalMcastNetintId& b)
591 {
592  return !(a < b);
593 }
594 
595 #endif
596 
601 #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:351
int etcpal_netint_info_cmp(const EtcPalNetintInfo *i1, const EtcPalNetintInfo *i2)
Compare two EtcPalNetintInfos.
Definition: inet.c:254
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:419
#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:176
bool etcpal_ip_is_loopback(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a loopback address.
Definition: inet.c:90
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:117
#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:296
bool etcpal_ip_is_link_local(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a link-local address.
Definition: inet.c:62
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:564
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:26
bool etcpal_ip_is_wildcard(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a wildcard address.
Definition: inet.c:148
etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr *src, char *dest)
Create a string representation of a MAC address.
Definition: inet.c:542
struct EtcPalMacAddr EtcPalMacAddr
A MAC address.
int etcpal_ip_cmp(const EtcPalIpAddr *ip1, const EtcPalIpAddr *ip2)
Compare two EtcPalIpAddrs.
Definition: inet.c:210
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:238
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