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 void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
15 {
16  //
17  // Check input arguments
18  //
19  SBG_ASSERT(pBuffer);
20  SBG_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 ipAddrA;
38  int32 ipAddrB;
39  int32 ipAddrC;
40  int32 ipAddrD;
41  int32 numReadParams;
42 
43  //
44  // Check input arguments
45  //
46  SBG_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)ipAddrA, (uint8)ipAddrB, (uint8)ipAddrC, (uint8)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 y;
84  uint32 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 }
sbgIpAddress sbgNetworkIpFromString(const char *pBuffer)
Definition: sbgNetwork.c:35
SBG_INLINE uint8 sbgIpAddrGetA(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:79
unsigned int uint32
Definition: sbgTypes.h:58
uint32 sbgIpAddress
Definition: sbgTypes.h:70
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 uint32 sbgSwap32(uint32 x)
Definition: sbgSwap.h:52
bool sbgIpNetMaskValid(sbgIpAddress netmask)
Definition: sbgNetwork.c:81
Useful methods for Network handling such as ip addresses.
#define SBG_UNUSED_PARAMETER(x)
Definition: sbgDefines.h:102
SBG_INLINE uint8 sbgIpAddrGetC(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:107
unsigned char uint8
Definition: sbgTypes.h:56
#define SBG_ASSERT(expression)
Definition: sbgDebug.h:52
signed int int32
Definition: sbgTypes.h:64
SBG_INLINE uint8 sbgIpAddrGetD(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:121
void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
Definition: sbgNetwork.c:14
Set of functions used to swap numbers.


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