lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_inet.h
1 /******************************************************************************
2  * Copyright 2018 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 lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 /* lwpa_inet.h: POSIX-like identifiers for IP addresses, network interfaces and
21  * related items. */
22 #ifndef _LWPA_INET_H_
23 #define _LWPA_INET_H_
24 
25 #include <string.h>
26 #include "lwpa_int.h"
27 #include "lwpa_bool.h"
28 
39 typedef enum
40 {
44  LWPA_IPV4 = 1,
46  LWPA_IPV6 = 2
48 
50 #define IPV6_BYTES 16
51 
54 typedef struct LwpaIpAddr
55 {
59  union AddrUnion
60  {
61  uint32_t v4;
62  uint8_t v6[IPV6_BYTES];
63  } addr;
65 
77 #define lwpaip_is_v4(lwpa_ip_ptr) ((lwpa_ip_ptr)->type == LWPA_IPV4)
82 #define lwpaip_is_v6(lwpa_ip_ptr) ((lwpa_ip_ptr)->type == LWPA_IPV6)
86 #define lwpaip_is_invalid(lwpa_ip_ptr) ((lwpa_ip_ptr)->type == LWPA_IP_INVALID)
92 #define lwpaip_v4_address(lwpa_ip_ptr) ((lwpa_ip_ptr)->addr.v4)
98 #define lwpaip_v6_address(lwpa_ip_ptr) ((lwpa_ip_ptr)->addr.v6)
103 #define lwpaip_set_v4_address(lwpa_ip_ptr, val) \
104  do \
105  { \
106  (lwpa_ip_ptr)->type = LWPA_IPV4; \
107  (lwpa_ip_ptr)->addr.v4 = val; \
108  } while (0)
114 #define lwpaip_set_v6_address(lwpa_ip_ptr, val) \
115  do \
116  { \
117  (lwpa_ip_ptr)->type = LWPA_IPV6; \
118  memcpy((lwpa_ip_ptr)->addr.v6, val, IPV6_BYTES); \
119  } while (0)
123 #define lwpaip_set_invalid(lwpa_ip_ptr) ((lwpa_ip_ptr)->type = LWPA_IP_INVALID)
129 #define lwpaip_is_multicast(lwpa_ip_ptr) \
130  (((lwpa_ip_ptr)->type == LWPA_IPV4) \
131  ? (((lwpa_ip_ptr)->addr.v4 > 0xe0000000) && ((lwpa_ip_ptr)->addr.v4 < 0xefffffff)) \
132  : ((lwpa_ip_ptr)->addr.v6[0] == 0xff))
139 #define lwpaip_equal(ipptr1, ipptr2) \
140  (((ipptr1)->type == (ipptr2)->type) \
141  ? (((ipptr1)->type == LWPA_IPV4) ? ((ipptr1)->addr.v4 == (ipptr2)->addr.v4) \
142  : (0 == memcmp((ipptr1)->addr.v6, (ipptr2)->addr.v6, IPV6_BYTES))) \
143  : false)
151 #define lwpaip_cmp(ipptr1, ipptr2) \
152  (((ipptr1)->type != (ipptr2)->type) \
153  ? ((ipptr1)->type - (ipptr2)->type) \
154  : (((ipptr1)->type == LWPA_IPV4) ? ((int)(ipptr1)->addr.v4 - (int)(ipptr2)->addr.v4) \
155  : memcmp((ipptr1)->addr.v6, (ipptr2)->addr.v6, IPV6_BYTES)))
156 
157 #define LWPA_INADDR_ANY 0
158 
162 #define lwpaip_make_any_v4(lwpa_ip_ptr) lwpaip_set_v4_address(lwpa_ip_ptr, LWPA_INADDR_ANY)
165 #define lwpaip_make_any_v6(lwpa_ip_ptr) \
166  do \
167  { \
168  (lwpa_ip_ptr)->type = LWPA_IPV6; \
169  memset(lwpa_ip_v6_address(lwpa_ip_ptr), 0, IPV6_BYTES); \
170  } while (0)
175 typedef struct LwpaSockaddr
176 {
177  uint16_t port;
179  uint32_t scope_id;
181 
188 #define lwpasock_ip_port_equal(sockptr1, sockptr2) \
189  (lwpaip_equal(&(sockptr1)->ip, &(sockptr2)->ip) && ((sockptr1)->port == (sockptr2)->port))
190 
191 #define NETINTINFO_MAC_LEN 6
192 #define NETINTINFO_NAME_LEN 64
193 
195 typedef struct LwpaNetintInfo
196 {
199  int ifindex;
207  uint8_t mac[NETINTINFO_MAC_LEN];
209  char name[NETINTINFO_NAME_LEN];
213 
216 #endif /* _LWPA_INET_H_ */
struct LwpaNetintInfo LwpaNetintInfo
A description of a network interface.
lwpa_iptype_t
Used to identify the type of IP address contained in a LwpaIpAddr.
Definition: lwpa_inet.h:40
struct LwpaIpAddr LwpaIpAddr
An IP address.
struct LwpaSockaddr LwpaSockaddr
An IP address with associated interface and port.
#define IPV6_BYTES
The number of bytes in an IPv6 address.
Definition: lwpa_inet.h:50
@ LWPA_IPV6
This lwpa_ip contains an IPv6 address.
Definition: lwpa_inet.h:46
@ LWPA_IP_INVALID
This lwpa_ip is not valid.
Definition: lwpa_inet.h:42
@ LWPA_IPV4
This lwpa_ip contains an IPv4 address.
Definition: lwpa_inet.h:44
An IP address.
Definition: lwpa_inet.h:55
lwpa_iptype_t type
The IP address type (IPv4 or IPv6)
Definition: lwpa_inet.h:57
A description of a network interface.
Definition: lwpa_inet.h:196
int ifindex
The OS-specific network interface number.
Definition: lwpa_inet.h:199
LwpaIpAddr addr
The interface ip address.
Definition: lwpa_inet.h:201
bool is_default
Whether this is the default network interface.
Definition: lwpa_inet.h:211
char name[NETINTINFO_NAME_LEN]
The adapter name as a string.
Definition: lwpa_inet.h:209
LwpaIpAddr gate
The address of the default gateway for this interface.
Definition: lwpa_inet.h:205
LwpaIpAddr mask
The subnet mask for this interface.
Definition: lwpa_inet.h:203
uint8_t mac[NETINTINFO_MAC_LEN]
The adapter MAC address.
Definition: lwpa_inet.h:207
An IP address with associated interface and port.
Definition: lwpa_inet.h:176
uint32_t scope_id
IPv6 scope ID.
Definition: lwpa_inet.h:179
uint16_t port
TCP or UDP port.
Definition: lwpa_inet.h:177
LwpaIpAddr ip
IP address.
Definition: lwpa_inet.h:178
The address, use either v4 or v6 depending on the value of type.
Definition: lwpa_inet.h:60