sbgNetwork.h
Go to the documentation of this file.
1 
35 #ifndef SBG_NETWORK_H
36 #define SBG_NETWORK_H
37 
38 //----------------------------------------------------------------------//
39 //- Header (open extern C block) -//
40 //----------------------------------------------------------------------//
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #include <sbgCommon.h>
46 
47 //----------------------------------------------------------------------//
48 //- Common IPv4 definitions -//
49 //----------------------------------------------------------------------//
50 #define SBG_IPV4_UNSPECIFIED_ADDR sbgIpAddr(0, 0, 0, 0)
51 #define SBG_IPV4_BROADCAST_ADDR sbgIpAddr(255, 255, 255, 255)
53 //----------------------------------------------------------------------//
54 //- IP setters / getters -//
55 //----------------------------------------------------------------------//
56 
65 SBG_INLINE sbgIpAddress sbgIpAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
66 {
67 #if SBG_CONFIG_BIG_ENDIAN == 1
68  return (a << 24) | (b << 16) | (c << 8) | d;
69 #else
70  return a | (b << 8) | (c << 16) | (d << 24);
71 #endif
72 }
73 
80 {
81 #if SBG_CONFIG_BIG_ENDIAN == 1
82  return (uint8_t)((ipAddr & 0xFF000000) >> 24);
83 #else
84  return (uint8_t)((ipAddr & 0x000000FF));
85 #endif
86 }
87 
94 {
95 #if SBG_CONFIG_BIG_ENDIAN == 1
96  return (uint8_t)((ipAddr & 0x00FF0000) >> 16);
97 #else
98  return (uint8_t)((ipAddr & 0x0000FF00) >> 8);
99 #endif
100 }
101 
108 {
109 #if SBG_CONFIG_BIG_ENDIAN == 1
110  return (uint8_t)((ipAddr & 0x0000FF00) >> 8);
111 #else
112  return (uint8_t)((ipAddr & 0x00FF0000) >> 16);
113 #endif
114 }
115 
122 {
123 #if SBG_CONFIG_BIG_ENDIAN == 1
124  return (uint8_t)((ipAddr & 0x000000FF));
125 #else
126  return (uint8_t)((ipAddr & 0xFF000000) >> 24);
127 #endif
128 }
129 
130 //----------------------------------------------------------------------//
131 //- IP manipulation methods -//
132 //----------------------------------------------------------------------//
133 
140 SBG_COMMON_LIB_API void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize);
141 
148 
149 //----------------------------------------------------------------------//
150 //- IP operations -//
151 //----------------------------------------------------------------------//
152 
160 {
161  return (ipAddress & netmask);
162 }
163 
171 {
172  return (ipAddress & ~netmask);
173 }
174 
175 //----------------------------------------------------------------------//
176 //- IP validation methods -//
177 //----------------------------------------------------------------------//
178 
185 {
186  if (ipAddress == 0)
187  {
188  return true;
189  }
190  else
191  {
192  return false;
193  }
194 }
195 
202 {
203  //
204  // Check the if A part of the ip address is within 1 and 223
205  //
206  if ((sbgIpAddrGetA(ipAddress) > 0) && (sbgIpAddrGetA(ipAddress) < 224))
207  {
208  //
209  // The ip address is valid
210  //
211  return true;
212  }
213  else
214  {
215  //
216  // The ip address is not valid
217  //
218  return false;
219  }
220 }
221 
229 {
230  //
231  // Just check if the host part is equals to zero
232  //
233  if (sbgIpGetHostAddr(ipAddress, netmask) == 0)
234  {
235  return false;
236  }
237  else
238  {
239  return true;
240  }
241 }
242 
251 {
252  if ((firstIpAddr & netmask) == (secondIpAddr & netmask))
253  {
254  return true;
255  }
256  else
257  {
258  return false;
259  }
260 }
261 
268 
269 //----------------------------------------------------------------------//
270 //- Footer (close extern C block) -//
271 //----------------------------------------------------------------------//
272 #ifdef __cplusplus
273 }
274 #endif
275 
276 #endif /* SBG_NETWORK_H */
SBG_COMMON_LIB_API bool sbgIpNetMaskValid(sbgIpAddress netmask)
Definition: sbgNetwork.c:81
#define SBG_COMMON_LIB_API
Definition: sbgDefines.h:58
SBG_COMMON_LIB_API void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
Definition: sbgNetwork.c:14
SBG_INLINE bool sbgIpAddrIsSameNetwork(sbgIpAddress firstIpAddr, sbgIpAddress secondIpAddr, sbgIpAddress netmask)
Definition: sbgNetwork.h:250
SBG_INLINE sbgIpAddress sbgIpGetHostAddr(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:170
SBG_INLINE bool sbgIpAddressValid(sbgIpAddress ipAddress)
Definition: sbgNetwork.h:201
SBG_INLINE sbgIpAddress sbgIpAddrWithinSubnet(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:228
SBG_INLINE bool sbgIpAddressIsUnspecified(sbgIpAddress ipAddress)
Definition: sbgNetwork.h:184
SBG_INLINE uint8_t sbgIpAddrGetC(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:107
SBG_INLINE sbgIpAddress sbgIpAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Definition: sbgNetwork.h:65
uint32_t sbgIpAddress
Definition: sbgTypes.h:64
#define SBG_INLINE
Definition: sbgDefines.h:186
Main header file for SBG Systems common C library.
SBG_INLINE uint8_t sbgIpAddrGetD(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:121
SBG_INLINE sbgIpAddress sbgIpGetNetworkAddr(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:159
SBG_COMMON_LIB_API sbgIpAddress sbgNetworkIpFromString(const char *pBuffer)
Definition: sbgNetwork.c:35
SBG_INLINE uint8_t sbgIpAddrGetA(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:79
SBG_INLINE uint8_t sbgIpAddrGetB(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:93


sbg_driver
Author(s): SBG Systems
autogenerated on Sat Sep 3 2022 02:53:36