sbgNetwork.c
Go to the documentation of this file.
1 #include "sbgNetwork.h"
2 #include <swap/sbgSwap.h>
3 
4 //----------------------------------------------------------------------//
5 //- IP manipulation methods -//
6 //----------------------------------------------------------------------//
7 
14 SBG_COMMON_LIB_API void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
15 {
16  //
17  // Check input arguments
18  //
19  assert(pBuffer);
20  assert(maxSize >= 16);
21 
22  SBG_UNUSED_PARAMETER(maxSize);
23 
24  //
25  // Write the IP address
26  //
27  sprintf(pBuffer, "%u.%u.%u.%u", sbgIpAddrGetA(ipAddr), sbgIpAddrGetB(ipAddr), sbgIpAddrGetC(ipAddr), sbgIpAddrGetD(ipAddr));
28 }
29 
36 {
37  int32_t ipAddrA;
38  int32_t ipAddrB;
39  int32_t ipAddrC;
40  int32_t ipAddrD;
41  int32_t numReadParams;
42 
43  //
44  // Check input arguments
45  //
46  assert(pBuffer);
47 
48  //
49  // Parse input arguments
50  //
51  numReadParams = sscanf(pBuffer, "%d.%d.%d.%d", &ipAddrA, &ipAddrB, &ipAddrC, &ipAddrD);
52 
53  //
54  // Make sure the parsed IP is normal
55  //
56  if ((numReadParams == 4) && (ipAddrA >= 0) && (ipAddrA <= 255) && (ipAddrB >= 0) && (ipAddrB <= 255) && (ipAddrC >= 0) && (ipAddrC <= 255) && (ipAddrD >= 0) && (ipAddrD <= 255))
57  {
58  //
59  // Ip address correctly parsed, return it
60  //
61  return sbgIpAddr((uint8_t)ipAddrA, (uint8_t)ipAddrB, (uint8_t)ipAddrC, (uint8_t)ipAddrD);
62  }
63  else
64  {
65  //
66  // The IP address couldn't be parsed correctly, return a null ip address
67  //
68  return sbgIpAddr(0, 0, 0, 0);
69  }
70 }
71 
72 //----------------------------------------------------------------------//
73 //- IP validation methods -//
74 //----------------------------------------------------------------------//
75 
82 {
83  uint32_t y;
84  uint32_t z;
85 
86  //
87  // First test that the netmask is not zero, if yes, it's valid
88  //
89  if (netmask != 0)
90  {
91  //
92  // We are doing arithmetic so we have to make sure the netmask is using the platform endianness
93  // The IP address is always stored in big endian so we have to swap it for little endian platforms
94  //
95 #if SBG_CONFIG_BIG_ENDIAN == 0
96  netmask = sbgSwap32(netmask);
97 #endif
98 
99  //
100  // Compute the bitwise inverse
101  //
102  y = ~netmask;
103 
104  //
105  // Add one to the inverse so if netmask was a proper one, there will be at most 1 bit set in this.
106  //
107  z = y + 1;
108 
109  //
110  // Test that, simply and z with z - 1, which happens to be y. The result will be zero if all is OK, non zero otherwise.
111  //
112  if ((z&y) == 0)
113  {
114  //
115  // We have a valid netmask
116  //
117  return true;
118  }
119  else
120  {
121  return false;
122  }
123  }
124 
125  return true;
126 }
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 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
Useful methods for Network handling such as ip addresses.
#define SBG_UNUSED_PARAMETER(x)
Definition: sbgDefines.h:194
SBG_COMMON_LIB_API sbgIpAddress sbgNetworkIpFromString(const char *pBuffer)
Definition: sbgNetwork.c:35
SBG_INLINE uint8_t sbgIpAddrGetD(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:121
SBG_INLINE uint32_t sbgSwap32(uint32_t x)
Definition: sbgSwap.h:52
SBG_INLINE uint8_t sbgIpAddrGetA(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:79
Set of functions used to swap numbers.
SBG_INLINE uint8_t sbgIpAddrGetB(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:93


sbg_driver
Author(s): SBG Systems
autogenerated on Thu Oct 22 2020 03:47:22