mbgtools-lx  4.2.8
lan_util.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: lan_util.h 1.9 2018/02/28 16:55:31 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Definitions and prototypes for lan_util.c.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: lan_util.h $
13  * Revision 1.9 2018/02/28 16:55:31 martin
14  * Updated function prototypes.
15  * Revision 1.8 2017/10/05 08:20:40 daniel
16  * Introduced check_port_status() function
17  * Revision 1.7 2017/07/05 14:10:58 martin
18  * Some definitions used with IPv6 added by hannes.
19  * Removed definitions of old MBG_LU_... return codes.
20  * Standard MBG_RETURN_CODES are now used instead.
21  * Updated function prototypes.
22  * Revision 1.6 2014/09/24 08:32:58 martin
23  * Fixed a POSIX header file dependency.
24  * Revision 1.5 2013/10/02 07:20:36 martin
25  * Updated function prototypes.
26  * Revision 1.4 2013/02/19 15:15:53 martin
27  * Added some inline functions.
28  * Redefined return codes as named enum.
29  * Updated function prototypes.
30  * Revision 1.3 2012/10/02 18:24:29 martin
31  * Added some macros to simpliy conversion to string.
32  * Revision 1.2 2012/03/09 08:51:44 martin
33  * Updated function prototypes.
34  * Revision 1.1 2011/03/04 10:01:32 martin
35  * Initial revision.
36  *
37  **************************************************************************/
38 
39 #ifndef _LAN_UTIL_H
40 #define _LAN_UTIL_H
41 
42 
43 /* Other headers to be included */
44 
45 #include <mbg_tgt.h>
46 #include <gpsdefs.h> // for some Meinberg data structures
47 
48 #include <stdlib.h>
49 
50 #if defined( MBG_TGT_POSIX )
51  #include <sys/types.h>
52  #include <sys/socket.h>
53  #include <net/if.h>
54  #include <ifaddrs.h> // *must* be included *after* net/if.h
55 #else
56  // A dummy declaration to prevent from warnings due to usage
57  // of this type with function prototypes.
58  struct ifreq
59  {
60  int dummy;
61  };
62 #endif
63 
64 
65 #if defined( IFHWADDRLEN ) // usually defined in net/if.h
66  #if ( IFHWADDRLEN != 6 )
67  #error Warning: IFHWADDRLEN is not 6!
68  #endif
69 #endif
70 
71 
72 
73 #ifdef _LAN_UTIL
74  #define _ext
75  #define _DO_INIT
76 #else
77  #define _ext extern
78 #endif
79 
80 
81 /* Start of header body */
82 
83 #if defined( _USE_PACK )
84  #pragma pack( 1 ) // set byte alignment
85  #define _USING_BYTE_ALIGNMENT
86 #endif
87 
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91 
92 
93 #if !defined( MAC_SEP_CHAR )
94  #define MAC_SEP_CHAR ':' // character used to separate octets of a MAC ID
95 #endif
96 
97 #if !defined( MAC_SEP_CHAR_ALT )
98  #define MAC_SEP_CHAR_ALT '-' // alternate character
99 #endif
100 
101 
102 #define MAX_IP4_BITS ( 8 * (int) sizeof( IP4_ADDR ) )
103 
104 #define IP4_MSB_MASK ( 1UL << ( MAX_IP4_BITS - 1 ) )
105 
106 #define MIN_IP4_CIDR_NETMASK_BITS 0
107 #define MAX_IP4_CIDR_NETMASK_BITS MAX_IP4_BITS
108 
109 
110 #define IP6_MSB_MASK ( 1UL << ( 8 - 1 ) )
111 
112 #define MIN_IP6_CIDR_NETMASK_BITS 0
113 #define MAX_IP6_CIDR_NETMASK_BITS IP6_ADDR_BITS
114 
115 
116 
129 static __mbg_inline
130 IP4_ADDR ip4_net_mask_from_cidr( int netmask_bits )
131 {
132  return (IP4_ADDR) ~( ( 1UL << ( MAX_IP4_BITS - netmask_bits ) ) - 1 );
133 
134 } // ip4_net_mask_from_cidr
135 
136 
137 
150 static __mbg_inline
151 IP4_ADDR ip4_broad_addr_from_addr( const IP4_ADDR *p_addr, const IP4_ADDR *p_mask )
152 {
153  return *p_addr | ~(*p_mask);
154 
155 } // ip4_broad_addr_from_addr
156 
157 
158 
171 static __mbg_inline
172 IP4_ADDR ip4_net_part_from_addr( const IP4_ADDR *p_addr, const IP4_ADDR *p_mask )
173 {
174  return *p_addr & *p_mask;
175 
176 } // ip4_net_part_from_addr
177 
178 
179 
189 static __mbg_inline
190 int ip4_net_part_matches( const IP4_ADDR *p_addr1, const IP4_ADDR *p_addr2,
191  const IP4_ADDR *p_mask )
192 {
193  return ip4_net_part_from_addr( p_addr1, p_mask )
194  == ip4_net_part_from_addr( p_addr2, p_mask );
195 
196 } // ip4_net_part_matches
197 
198 
199 
200 #define _ip4_addr_to_str( _s, _a ) \
201  snprint_ip4_addr( _s, sizeof( _s ), _a, NULL )
202 
203 #define _mac_addr_to_str( _s, _a ) \
204  snprint_mac_addr( _s, sizeof( _s ), _a )
205 
206 
207 
208 /* ----- function prototypes begin ----- */
209 
210 /* This section was generated automatically */
211 /* by MAKEHDR, do not remove the comments. */
212 
225  int get_ip4_net_mask_bits( const IP4_ADDR *p_mask ) ;
226 
241  size_t snprint_ip4_addr( char *s, size_t max_len, const IP4_ADDR *p_addr, const char *info ) ;
242 
260  size_t snprint_ip4_cidr_addr( char *s, size_t max_len, const IP4_ADDR *p_addr, const IP4_ADDR *p_mask, const char *info ) ;
261 
279  int str_to_ip4_addr( IP4_ADDR *p_addr, const char *s ) ;
280 
301  int cidr_str_to_ip4_addr_and_net_mask( IP4_ADDR *p_addr, IP4_ADDR *p_mask, const char *cidr_str ) ;
302 
315  int get_ip6_net_mask_bits( const IP6_ADDR *p_mask ) ;
316 
333  size_t snprint_ip6_addr( char *s, size_t max_len, const IP6_ADDR *p_addr, const char *info ) ;
334 
354  size_t snprint_ip6_cidr_addr( char *s, size_t max_len, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask, const char *info ) ;
355 
375  size_t snprint_ip6_cidr_mask_addr( char *s, size_t max_len, const IP6_ADDR *p_addr, const int cidr_mask_bits, const char* info ) ;
376 
400  int str_to_ip6_addr( IP6_ADDR *p_addr, const char *s ) ;
401 
422  int cidr_str_to_ip6_addr_and_net_mask( IP6_ADDR *p_addr, IP6_ADDR *p_mask, const char *cidr_str ) ;
423 
442  int cidr_str_to_ip6_addr_and_cidr_bits( IP6_ADDR *p_addr, int *p_cidr, const char *cidr_str ) ;
443 
455  void ip6_net_mask_from_cidr( IP6_ADDR *p_mask, int netmask_bits ) ;
456 
466  void ip6_net_part_from_addr( IP6_ADDR *p_net_part, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask ) ;
467 
484  size_t snprint_octets( char *s, size_t max_len, const uint8_t *octets, int num_octets, char sep, const char *info ) ;
485 
499  size_t snprint_ptp_clock_id( char *s, size_t max_len, const PTP_CLOCK_ID *p ) ;
500 
514  size_t snprint_mac_addr( char *s, size_t max_len, const MBG_MAC_ADDR *p_mac_addr ) ;
515 
529  int str_to_octets( uint8_t *octets, int num_octets, const char *s ) ;
530 
543  bool octets_are_all_zero( const uint8_t *octets, int num_octets ) ;
544 
554  bool mac_addr_is_all_zero( const MBG_MAC_ADDR *p_addr ) ;
555 
565  int do_siocg_ioctl( const char *if_name, int ioctl_code, struct ifreq *p_ifreq ) ;
566 
576  int get_port_intf_idx( const char *if_name, int *p_intf_idx ) ;
577 
587  int get_port_mac_addr( const char *if_name, MBG_MAC_ADDR *p_mac_addr ) ;
588 
598  int check_port_link( const char *if_name ) ;
599 
611  int check_port_status( const char *if_name, int *p_speed, int *p_duplex ) ;
612 
630  int get_port_ip4_addr( const char *if_name, IP4_ADDR *p_addr ) ;
631 
649  int get_port_ip4_netmask( const char *if_name, IP4_ADDR *p_addr ) ;
650 
668  int get_port_ip4_broad_addr( const char *if_name, IP4_ADDR *p_addr ) ;
669 
678  int get_ip4_gateway( IP4_ADDR *p_addr ) ;
679 
698  int get_port_ip4_addr_str( const char *if_name, char *p_addr_buf, int buf_size ) ;
699 
718  int get_port_ip4_netmask_str( const char *if_name, char *p_addr_buf, int buf_size ) ;
719 
738  int get_port_ip4_broad_addr_str( const char *if_name, char *p_addr_buf, int buf_size ) ;
739 
756  int get_port_ip4_settings( const char *if_name, IP4_SETTINGS *p ) ;
757 
758 
759 /* ----- function prototypes end ----- */
760 
761 #ifdef __cplusplus
762 }
763 #endif
764 
765 
766 #if defined( _USING_BYTE_ALIGNMENT )
767  #pragma pack() // set default alignment
768  #undef _USING_BYTE_ALIGNMENT
769 #endif
770 
771 /* End of header body */
772 
773 
774 #undef _ext
775 #undef _DO_INIT
776 
777 #endif /* _LAN_UTIL_H */
778 
int cidr_str_to_ip6_addr_and_net_mask(IP6_ADDR *p_addr, IP6_ADDR *p_mask, const char *cidr_str)
Convert a string in CIDR notation to an IP6_ADDR and net mask.
Definition: lan_util.c:823
int do_siocg_ioctl(const char *if_name, int ioctl_code, struct ifreq *p_ifreq)
Do a SIOCGxxx IOCTL call to read specific information from a LAN interface.
Definition: lan_util.c:1155
int check_port_status(const char *if_name, int *p_speed, int *p_duplex)
Check the state of a network interface.
Definition: lan_util.c:1302
int get_port_ip4_broad_addr(const char *if_name, IP4_ADDR *p_addr)
Retrieve the IPv4 broadcast address of a network interface.
Definition: lan_util.c:1456
The MAC address of a network interface.
Definition: gpsdefs.h:11031
Settings of an IPv4-only network interface.
Definition: gpsdefs.h:11239
uint32_t IP4_ADDR
An IPv4 address.
Definition: gpsdefs.h:11045
static __mbg_inline IP4_ADDR ip4_net_part_from_addr(const IP4_ADDR *p_addr, const IP4_ADDR *p_mask)
Determine the network part of an IP4 address based on the net mask.
Definition: lan_util.h:172
bool mac_addr_is_all_zero(const MBG_MAC_ADDR *p_addr)
Check if a MAC address is all zero.
Definition: lan_util.c:1135
int check_port_link(const char *if_name)
Check the link state of a network interface.
Definition: lan_util.c:1267
int get_ip6_net_mask_bits(const IP6_ADDR *p_mask)
Count the number of sequential bits in an IPv6 net mask.
Definition: lan_util.c:417
int get_port_intf_idx(const char *if_name, int *p_intf_idx)
Retrieve the index of a specific network interface.
Definition: lan_util.c:1197
int cidr_str_to_ip4_addr_and_net_mask(IP4_ADDR *p_addr, IP4_ADDR *p_mask, const char *cidr_str)
Convert a string in CIDR notation to an IP4_ADDR and net mask.
Definition: lan_util.c:349
int str_to_ip6_addr(IP6_ADDR *p_addr, const char *s)
Convert a string to an IP6_ADDR.
Definition: lan_util.c:673
A PTP clock identity.
Definition: gpsdefs.h:13038
int get_ip4_gateway(IP4_ADDR *p_addr)
Retrieve the IPv4 gateway (default route)
Definition: lan_util.c:1679
static __mbg_inline int ip4_net_part_matches(const IP4_ADDR *p_addr1, const IP4_ADDR *p_addr2, const IP4_ADDR *p_mask)
Check if two IP4 addresses have the same network part.
Definition: lan_util.h:190
int get_port_ip4_settings(const char *if_name, IP4_SETTINGS *p)
Retrieve the current IPv4 settings of a network interface.
Definition: lan_util.c:1923
static __mbg_inline IP4_ADDR ip4_net_mask_from_cidr(int netmask_bits)
Compute an IP4 net mask according to the number of CIDR netmask bits.
Definition: lan_util.h:130
size_t snprint_ip6_cidr_addr(char *s, size_t max_len, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask, const char *info)
Print an IPv6 address plus net mask to string in CIDR notation.
Definition: lan_util.c:592
int cidr_str_to_ip6_addr_and_cidr_bits(IP6_ADDR *p_addr, int *p_cidr, const char *cidr_str)
Convert a string in CIDR notation to an IP6_ADDR and net mask bits.
Definition: lan_util.c:859
size_t snprint_ip6_addr(char *s, size_t max_len, const IP6_ADDR *p_addr, const char *info)
Print an IPv6 address in optimized format to a string.
Definition: lan_util.c:463
size_t snprint_ptp_clock_id(char *s, size_t max_len, const PTP_CLOCK_ID *p)
Print a PTP_CLOCK_ID to a string.
Definition: lan_util.c:1027
int get_port_mac_addr(const char *if_name, MBG_MAC_ADDR *p_mac_addr)
Retrieve the MAC address of a network interface.
Definition: lan_util.c:1232
unsigned char uint8_t
Definition: words.h:210
int get_port_ip4_addr(const char *if_name, IP4_ADDR *p_addr)
Retrieve the IPv4 address of a network interface.
Definition: lan_util.c:1404
int get_port_ip4_netmask(const char *if_name, IP4_ADDR *p_addr)
Retrieve the IPv4 net mask of a network interface.
Definition: lan_util.c:1430
size_t snprint_ip4_addr(char *s, size_t max_len, const IP4_ADDR *p_addr, const char *info)
Print an IPv4 address to a dotted quad formatted string.
Definition: lan_util.c:217
size_t snprint_octets(char *s, size_t max_len, const uint8_t *octets, int num_octets, char sep, const char *info)
Print a MAC ID or similar array of octets to a string.
Definition: lan_util.c:990
int str_to_ip4_addr(IP4_ADDR *p_addr, const char *s)
Convert a string to an IP4_ADDR.
Definition: lan_util.c:291
Definition: myutil.h:117
#define MAX_IP4_BITS
Definition: lan_util.h:102
size_t snprint_ip4_cidr_addr(char *s, size_t max_len, const IP4_ADDR *p_addr, const IP4_ADDR *p_mask, const char *info)
Print an IPv4 address plus net mask in CIDR notation to a string.
Definition: lan_util.c:255
int get_ip4_net_mask_bits(const IP4_ADDR *p_mask)
Count the number of sequential bits in an IPv4 net mask.
Definition: lan_util.c:183
size_t snprint_mac_addr(char *s, size_t max_len, const MBG_MAC_ADDR *p_mac_addr)
Print a MAC address to a string.
Definition: lan_util.c:1049
size_t snprint_ip6_cidr_mask_addr(char *s, size_t max_len, const IP6_ADDR *p_addr, const int cidr_mask_bits, const char *info)
Print an IPv6 address plus number of net mask bits to string in CIDR notation.
Definition: lan_util.c:629
void ip6_net_mask_from_cidr(IP6_ADDR *p_mask, int netmask_bits)
Compute an IPv6 net mask according to the number of CIDR netmask bits.
Definition: lan_util.c:918
bool octets_are_all_zero(const uint8_t *octets, int num_octets)
Check if an array of octets is all zero.
Definition: lan_util.c:1110
void ip6_net_part_from_addr(IP6_ADDR *p_net_part, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask)
Determine the network part of an IPv6 address based on the net mask.
Definition: lan_util.c:961
int str_to_octets(uint8_t *octets, int num_octets, const char *s)
Set a MAC ID or a similar array of octets from a string.
Definition: lan_util.c:1071
An IPv6 address.
Definition: gpsdefs.h:11061
int get_port_ip4_netmask_str(const char *if_name, char *p_addr_buf, int buf_size)
Retrieve the IPv4 net mask of a network interface as string.
Definition: lan_util.c:1859
int get_port_ip4_broad_addr_str(const char *if_name, char *p_addr_buf, int buf_size)
Retrieve the IPv4 broadcast address of a network interface as string.
Definition: lan_util.c:1892
static __mbg_inline IP4_ADDR ip4_broad_addr_from_addr(const IP4_ADDR *p_addr, const IP4_ADDR *p_mask)
Determine the broadcast address for an IP4 address plus net mask.
Definition: lan_util.h:151
int get_port_ip4_addr_str(const char *if_name, char *p_addr_buf, int buf_size)
Retrieve the IPv4 address of a network interface as string.
Definition: lan_util.c:1826