FunWithIPADDR - NetBurner IP Address Object Example
Overview
FunWithIPADDR
is a C++ demonstration application that showcases the NetBurner IPADDR object functionality. This application demonstrates how to work with IP addresses in a dual-stack (IPv4/IPv6) environment using NetBurner's unified IPADDR object system.
- Note
- In rare cases in which you want to create an object of a specific type, you can create objects as IPADDR4 or IPADDR6. However, we highly recommend using IPADDR for all your IP addresses. Note that if you are using a printf() style format specifier, you must use
"%HI"
instead of "%I"
and pass in a IPADDR4 object, otherwise the value displayed will be 0.0.0.0.
What is IPADDR?
The IPADDR object is a C++ class that provides a unified interface for handling both IPv4 and IPv6 addresses without requiring separate code paths. This design enables applications to support both IP versions seamlessly, with the object automatically determining the appropriate operations based on the assigned address type.
Key Features
- Dual-stack support: Single object handles both IPv4 and IPv6 addresses
- Automatic type detection: Member functions automatically call appropriate IPv4 or IPv6 implementations
- Unified printf formatting: Custom
I
format specifier for easy display
- Null address handling: Built-in methods to check for and set null addresses
- Type-specific objects: Optional IPADDR4 and IPADDR6 for specialized use cases
Application Functionality
The application demonstrates the following IPADDR capabilities:
1. Basic Address Assignment and Display
- Creates IPADDR objects for both IPv4 and IPv6 addresses
- Uses
SetFromAscii()
to assign addresses from string literals
- Displays addresses using the custom
I
printf format specifier
2. Member Function Usage
- Demonstrates the
print()
member function for address output
- Shows how the same function works for both IPv4 and IPv6 addresses
3. Null Address Handling
- Tests for null addresses using
IsNull()
member function
- Demonstrates setting addresses to null with
SetNull()
4. Type-Specific Objects (IPADDR4)
- Shows creation of IPADDR4 objects for IPv4-specific needs
- Demonstrates the required
HI
format specifier for IPADDR4 objects
5. IPv6 Static Methods (Conditional)
- When IPv6 is enabled, shows usage of
AsciiToIp6()
static method
Code Examples
Creating and Using IPADDR Objects
iprintf("IPv4 Address: %I\r\n", ipaddrIPv4);
ipaddrIPv6.
SetFromAscii(
"2001:0db8:85a3:0000:0000:8a2e:0370:0006");
iprintf("IPv6 Address: %I\r\n", ipaddrIPv6);
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
void SetFromAscii(const char *cp, bool bembed_v4addresses=true)
Set the IP address value of an IPADDR6 object.
Null Address Checking
iprintf("Address is null\r\n");
else
iprintf("Address: %I\r\n", ipaddrIPv4);
bool IsNull() const
Check if the IP address is null.
Definition ipv6_addr.h:133
Using Type-Specific Objects
iprintf("IPADDR4: %HI\r\n", ipaddrIPADDR4);
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
void SetFromAscii(const char *cp)
Set the IPv4 address from a character string.
Important Notes
Printf Format Specifiers
- Use
I
for general IPADDR objects
- Use
HI
for IPADDR4 objects (failure to do so will display 0.0.0.0)
- The
I
format works with all printf variants (printf, iprintf, sprintf, etc.)
Best Practices
- Use the generic IPADDR class for most applications
- Only use IPADDR4 or IPADDR6 when type-specific functionality is required
- Always check for null addresses when necessary
- The unified approach reduces code complexity and maintenance
Application Flow
- Initialization: Sets up the NetBurner system and network stack
- Network Wait: Waits up to 5 seconds for active network connectivity
- HTTP Start: Initializes the HTTP server
- IPADDR Demonstrations: Runs through various IPADDR usage examples
- Main Loop: Enters an infinite loop with 1-second delays
Build Requirements
- NetBurner development environment
- NetBurner RTOS (nbrtos.h)
- HTTP support (http.h)
- Standard C++ compiler with NetBurner extensions
System Features Used
- System diagnostics (enabled for debugging)
- Network stack initialization
- HTTP server functionality
- Custom printf formatting