Useful methods for Network handling such as ip addresses. More...
#include <sbgCommon.h>
Go to the source code of this file.
Macros | |
#define | SBG_IPV4_BROADCAST_ADDR sbgIpAddr(255, 255, 255, 255) |
#define | SBG_IPV4_UNSPECIFIED_ADDR sbgIpAddr(0, 0, 0, 0) |
Functions | |
SBG_INLINE sbgIpAddress | sbgIpAddr (uint8 a, uint8 b, uint8 c, uint8 d) |
SBG_INLINE bool | sbgIpAddressIsUnspecified (sbgIpAddress ipAddress) |
SBG_INLINE bool | sbgIpAddressValid (sbgIpAddress ipAddress) |
SBG_INLINE uint8 | sbgIpAddrGetA (sbgIpAddress ipAddr) |
SBG_INLINE uint8 | sbgIpAddrGetB (sbgIpAddress ipAddr) |
SBG_INLINE uint8 | sbgIpAddrGetC (sbgIpAddress ipAddr) |
SBG_INLINE uint8 | sbgIpAddrGetD (sbgIpAddress ipAddr) |
SBG_INLINE bool | sbgIpAddrIsSameNetwork (sbgIpAddress firstIpAddr, sbgIpAddress secondIpAddr, sbgIpAddress netmask) |
SBG_INLINE sbgIpAddress | sbgIpAddrWithinSubnet (sbgIpAddress ipAddress, sbgIpAddress netmask) |
SBG_INLINE sbgIpAddress | sbgIpGetHostAddr (sbgIpAddress ipAddress, sbgIpAddress netmask) |
SBG_INLINE sbgIpAddress | sbgIpGetNetworkAddr (sbgIpAddress ipAddress, sbgIpAddress netmask) |
bool | sbgIpNetMaskValid (sbgIpAddress netmask) |
sbgIpAddress | sbgNetworkIpFromString (const char *pBuffer) |
void | sbgNetworkIpToString (sbgIpAddress ipAddr, char *pBuffer, size_t maxSize) |
Useful methods for Network handling such as ip addresses.
IP v4 address is stored in memory with a uint32. Each address component A.B.C.D is stored in 8 bits using the network endianess ie Big Endian.
We thus have the following memory organisation:
In Little Endian: |LSB| | |MSB| | A | B | C | D |
In Big Endian: |MSB| | |LSB| | A | B | C | D |
Copyright (C) 2007-2016, SBG Systems SAS. All rights reserved.
This source code is intended for use only by SBG Systems SAS and those that have explicit written permission to use it from SBG Systems SAS.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
Definition in file sbgNetwork.h.
#define SBG_IPV4_BROADCAST_ADDR sbgIpAddr(255, 255, 255, 255) |
Broadcast IP address used to address all devices within the same network.
Definition at line 51 of file sbgNetwork.h.
#define SBG_IPV4_UNSPECIFIED_ADDR sbgIpAddr(0, 0, 0, 0) |
This represents an undefined IP address.
Definition at line 50 of file sbgNetwork.h.
SBG_INLINE sbgIpAddress sbgIpAddr | ( | uint8 | a, |
uint8 | b, | ||
uint8 | c, | ||
uint8 | d | ||
) |
Build an IP V4 address in the form a.b.c.d
[in] | a | First 8 bits IP address. |
[in] | b | Second 8 bits IP address. |
[in] | c | Third 8 bits IP address. |
[in] | d | Last 8 bits IP address. |
Definition at line 65 of file sbgNetwork.h.
SBG_INLINE bool sbgIpAddressIsUnspecified | ( | sbgIpAddress | ipAddress | ) |
Returns true if the provided IP address is unspecified ie (0.0.0.0)
[in] | ipAddress | The ip address to test |
Definition at line 184 of file sbgNetwork.h.
SBG_INLINE bool sbgIpAddressValid | ( | sbgIpAddress | ipAddress | ) |
Check if an IpV4 address is valid. The ip address format is A.B.C.D and A should respect 0 < A < 224
[in] | ipAddress | The ip address stored in an uint32 (host endianness). |
Definition at line 201 of file sbgNetwork.h.
SBG_INLINE uint8 sbgIpAddrGetA | ( | sbgIpAddress | ipAddr | ) |
Return the first A field of of an IP v4 address of the form A.B.C.D
[in] | ipAddr | An sbgIpAddress to convert. |
Definition at line 79 of file sbgNetwork.h.
SBG_INLINE uint8 sbgIpAddrGetB | ( | sbgIpAddress | ipAddr | ) |
Return the first B field of of an IP v4 address of the form A.B.C.D
[in] | ipAddr | An sbgIpAddress to convert. |
Definition at line 93 of file sbgNetwork.h.
SBG_INLINE uint8 sbgIpAddrGetC | ( | sbgIpAddress | ipAddr | ) |
Return the first C field of of an IP v4 address of the form A.B.C.D
[in] | ipAddr | An sbgIpAddress to convert. |
Definition at line 107 of file sbgNetwork.h.
SBG_INLINE uint8 sbgIpAddrGetD | ( | sbgIpAddress | ipAddr | ) |
Return the first D field of of an IP v4 address of the form A.B.C.D
[in] | ipAddr | An sbgIpAddress to convert. |
Definition at line 121 of file sbgNetwork.h.
SBG_INLINE bool sbgIpAddrIsSameNetwork | ( | sbgIpAddress | firstIpAddr, |
sbgIpAddress | secondIpAddr, | ||
sbgIpAddress | netmask | ||
) |
Check if two ip addresses are in the same network given the subnet.
[in] | firstIpAddr | The first IP address to check. |
[in] | secondIpAddr | The second IP address to check. |
[in] | netmask | The netmask of the network. |
Definition at line 250 of file sbgNetwork.h.
SBG_INLINE sbgIpAddress sbgIpAddrWithinSubnet | ( | sbgIpAddress | ipAddress, |
sbgIpAddress | netmask | ||
) |
Given an ip address and the netmask, returns true if this ip address is within the subnet.
[in] | ipAddress | The ip address stored in an uint32 (host endianness). |
[in] | netmask | The netmask stored in an uint32 (host endianness). |
Definition at line 228 of file sbgNetwork.h.
SBG_INLINE sbgIpAddress sbgIpGetHostAddr | ( | sbgIpAddress | ipAddress, |
sbgIpAddress | netmask | ||
) |
Given an ip address and the netmask, returns the host part (ip & ~subnetMask)
[in] | ipAddress | The ip address stored in an uint32 (host endianness). |
[in] | netmask | The netmask stored in an uint32 (host endianness). |
Definition at line 170 of file sbgNetwork.h.
SBG_INLINE sbgIpAddress sbgIpGetNetworkAddr | ( | sbgIpAddress | ipAddress, |
sbgIpAddress | netmask | ||
) |
Given an ip address and the netmask, returns the network part (ip & subnetMask)
[in] | ipAddress | The ip address stored in an uint32 (host endianness). |
[in] | netmask | The netmask stored in an uint32 (host endianness). |
Definition at line 159 of file sbgNetwork.h.
bool sbgIpNetMaskValid | ( | sbgIpAddress | netmask | ) |
Check if an IpV4 netmask is valid, the mask should be contiguous (1111 followed by 0)
[in] | netmask | The netmask stored in an uint32 (host endianness). |
Definition at line 81 of file sbgNetwork.c.
sbgIpAddress sbgNetworkIpFromString | ( | const char * | pBuffer | ) |
Convert an ip address stored in a string of the form A.B.C.D to an sbgIpAddress object.
[in] | pBuffer | IP address as a string of the form A.B.C.D |
Definition at line 35 of file sbgNetwork.c.
void sbgNetworkIpToString | ( | sbgIpAddress | ipAddr, |
char * | pBuffer, | ||
size_t | maxSize | ||
) |
Convert an ip to a string of the form A.B.C.D
[in] | ipAddr | IP address to convert to a string. |
[out] | pBuffer | Pointer on an allocated buffer than can hold ip address as a string. |
[in] | maxSize | Maximum number of chars that can be stored in pBuffer including the NULL char. |
Definition at line 14 of file sbgNetwork.c.