Fork me on GitHub
ip-utils.h
Go to the documentation of this file.
1 
17 #ifndef _JANUS_IP_UTILS_H
18 #define _JANUS_IP_UTILS_H
19 
20 #include <ifaddrs.h>
21 #include <netinet/in.h>
22 
23 
26 typedef enum janus_network_query_options {
35 
40  const char *device_name;
42  struct in_addr ipv4;
43  struct in6_addr ipv6;
45 
53 typedef struct janus_network_address {
57  int family;
58  union {
59  struct in_addr ipv4;
60  struct in6_addr ipv6;
61  };
63 
75  int family;
76  union {
77  char ipv4[INET_ADDRSTRLEN];
78  char ipv6[INET6_ADDRSTRLEN];
79  };
81 
92 int janus_network_prepare_device_query(const char *user_value, const janus_network_query_options query_mode, janus_network_query_config *query);
93 
103 
114 const struct ifaddrs *janus_network_query_devices(const struct ifaddrs *ifas, const janus_network_query_config *query);
115 
125 int janus_network_get_devices_ipv4(const struct ifaddrs *ifa, const janus_network_query_config *query, struct in_addr *result);
126 
136 int janus_network_get_devices_ipv6(const struct ifaddrs *ifa, const janus_network_query_config *query, struct in6_addr *result);
137 
143 int janus_network_get_device_address(const struct ifaddrs *ifa, janus_network_address *result);
144 
151 
159 
167 
179 
186 
194 
202 
210 int janus_network_string_is_valid_address(janus_network_query_options addr_type, const char *user_value);
211 
220 int janus_network_string_to_address(janus_network_query_options addr_type, const char *user_value, janus_network_address *result);
221 
229 int janus_network_lookup_interface(const struct ifaddrs *ifas, const char *iface, janus_network_address *result);
230 
239 
249 
250 #endif
int family
Definition: ip-utils.h:75
janus_network_query_options
Definition: ip-utils.h:27
int janus_network_prepare_device_query_default(const char *user_value, janus_network_query_config *query)
Initialise a network device query with default query options. This function will Initialise the query...
Definition: ip-utils.c:106
int janus_network_string_to_address(janus_network_query_options addr_type, const char *user_value, janus_network_address *result)
Convert an IP address string to a janus_network_address instance.
Definition: ip-utils.c:224
int janus_network_get_device_address(const struct ifaddrs *ifa, janus_network_address *result)
Copies the IP address from a network interface description to the given result structure.
Definition: ip-utils.c:141
int janus_network_lookup_interface(const struct ifaddrs *ifas, const char *iface, janus_network_address *result)
Convert an interface name or IP address to a janus_network_address instance.
Definition: ip-utils.c:241
const char * device_name
Definition: ip-utils.h:40
struct janus_network_address janus_network_address
Structure to hold network addresses in a tagged union which should be IPv4 and IPv6 compatible...
void janus_network_address_nullify(janus_network_address *a)
Set the given network address to a null/nil value.
Definition: ip-utils.c:158
int janus_network_string_is_valid_address(janus_network_query_options addr_type, const char *user_value)
Test if a given IP address string is a valid address of the specified type.
Definition: ip-utils.c:219
struct in_addr ipv4
Definition: ip-utils.h:42
int janus_network_prepare_device_query(const char *user_value, const janus_network_query_options query_mode, janus_network_query_config *query)
Initialise a network device query.
Definition: ip-utils.c:83
Definition: ip-utils.h:29
Internal object representation of a network device query (configuration).
Definition: ip-utils.h:39
Definition: ip-utils.h:30
int janus_network_address_to_string_buffer(const janus_network_address *a, janus_network_address_string_buffer *buf)
Convert the given network address to a form which can be used to extract a human readable network add...
Definition: ip-utils.c:186
void janus_network_address_string_buffer_nullify(janus_network_address_string_buffer *b)
Set the given network address string buffer to a null/nil value.
Definition: ip-utils.c:200
int janus_network_get_devices_ipv4(const struct ifaddrs *ifa, const janus_network_query_config *query, struct in_addr *result)
Copies the IPv4 address from a network inteface description to the given result structure.
Definition: ip-utils.c:115
Definition: ip-utils.h:28
int janus_network_address_string_buffer_is_null(const janus_network_address_string_buffer *b)
Test if a given network address string buffer is null-valued.
Definition: ip-utils.c:207
Definition: ip-utils.h:32
struct janus_network_address_string_buffer janus_network_address_string_buffer
Structure to hold human readable forms of network addresses in a tagged union which should be IPv4 an...
const char * janus_network_address_string_from_buffer(const janus_network_address_string_buffer *b)
Extract the human readable representation of a network address from a given buffer.
Definition: ip-utils.c:211
char * janus_network_detect_local_ip_as_string(janus_network_query_options addr_type)
Wrapper to janus_network_detect_local_ip that returns a string instead.
Definition: ip-utils.c:316
Structure to hold network addresses in a tagged union which should be IPv4 and IPv6 compatible...
Definition: ip-utils.h:53
const struct ifaddrs * janus_network_query_devices(const struct ifaddrs *ifas, const janus_network_query_config *query)
Look up network devices matching the given query. The first matching device is returned, so to find all matching devices simply pass the ifa_next of the returned device in a subsequent call to this function to find more matches.
Definition: ip-utils.c:73
struct in6_addr ipv6
Definition: ip-utils.h:43
int family
Definition: ip-utils.h:57
int janus_network_address_is_null(const janus_network_address *a)
Test if a given network address is null-valued.
Definition: ip-utils.c:165
janus_network_query_options mode
Definition: ip-utils.h:41
Definition: ip-utils.h:31
Definition: ip-utils.h:33
struct janus_network_query_config janus_network_query_config
Internal object representation of a network device query (configuration).
Structure to hold human readable forms of network addresses in a tagged union which should be IPv4 an...
Definition: ip-utils.h:71
int janus_network_get_devices_ipv6(const struct ifaddrs *ifa, const janus_network_query_config *query, struct in6_addr *result)
Copies the IPv6 address from a network inteface description to the given result structure.
Definition: ip-utils.c:133
int janus_network_detect_local_ip(janus_network_query_options addr_type, janus_network_address *result)
Helper method to find a valid local IP address, that is an address that can be used to communicate...
Definition: ip-utils.c:262
int janus_network_address_from_sockaddr(struct sockaddr *s, janus_network_address *a)
Convert a struct sockaddr to a janus_network_address.
Definition: ip-utils.c:169