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 
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)((ipAddr & 0xFF000000) >> 24);
83 #else
84  return (uint8)((ipAddr & 0x000000FF));
85 #endif
86 }
87 
94 {
95 #if SBG_CONFIG_BIG_ENDIAN == 1
96  return (uint8)((ipAddr & 0x00FF0000) >> 16);
97 #else
98  return (uint8)((ipAddr & 0x0000FF00) >> 8);
99 #endif
100 }
101 
108 {
109 #if SBG_CONFIG_BIG_ENDIAN == 1
110  return (uint8)((ipAddr & 0x0000FF00) >> 8);
111 #else
112  return (uint8)((ipAddr & 0x00FF0000) >> 16);
113 #endif
114 }
115 
122 {
123 #if SBG_CONFIG_BIG_ENDIAN == 1
124  return (uint8)((ipAddr & 0x000000FF));
125 #else
126  return (uint8)((ipAddr & 0xFF000000) >> 24);
127 #endif
128 }
129 
130 //----------------------------------------------------------------------//
131 //- IP manipulation methods -//
132 //----------------------------------------------------------------------//
133 
140 void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize);
141 
147 sbgIpAddress sbgNetworkIpFromString(const char *pBuffer);
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 
267 bool sbgIpNetMaskValid(sbgIpAddress netmask);
268 
269 //----------------------------------------------------------------------//
270 //- Footer (close extern C block) -//
271 //----------------------------------------------------------------------//
272 #ifdef __cplusplus
273 }
274 #endif
275 
276 #endif /* __SBG_NETWORK_H__ */
SBG_INLINE uint8 sbgIpAddrGetA(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:79
uint32 sbgIpAddress
Definition: sbgTypes.h:70
SBG_INLINE bool sbgIpAddrIsSameNetwork(sbgIpAddress firstIpAddr, sbgIpAddress secondIpAddr, sbgIpAddress netmask)
Definition: sbgNetwork.h:250
SBG_INLINE uint8 sbgIpAddrGetB(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:93
SBG_INLINE sbgIpAddress sbgIpAddr(uint8 a, uint8 b, uint8 c, uint8 d)
Definition: sbgNetwork.h:65
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
bool sbgIpNetMaskValid(sbgIpAddress netmask)
Definition: sbgNetwork.c:81
#define SBG_INLINE
Definition: sbgDefines.h:94
Main header file for SBG Systems common C library.
void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
Definition: sbgNetwork.c:14
SBG_INLINE uint8 sbgIpAddrGetC(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:107
unsigned char uint8
Definition: sbgTypes.h:56
SBG_INLINE uint8 sbgIpAddrGetD(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:121
SBG_INLINE sbgIpAddress sbgIpGetNetworkAddr(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:159
sbgIpAddress sbgNetworkIpFromString(const char *pBuffer)
Definition: sbgNetwork.c:35


sbg_driver
Author(s):
autogenerated on Sun Jan 27 2019 03:42:20