Skip to content

Conversation

@soyersoyer
Copy link
Contributor

@soyersoyer soyersoyer commented Aug 8, 2025

This fixes the IP address setting error by improving IP address handling.

Summary by Sourcery

Improve IP address handling by fixing configuration loading, switching getters to return const references, and enhancing network initialization to robustly select between DHCP and static addressing with detailed logging.

Enhancements:

  • Change IP address getters to return const references to avoid unnecessary copies
  • Update CConfig::Load to initialize IP address fields only when properties are present
  • Revamp InitNetwork to branch between DHCP, static IP, or fallback modes, default missing gateway/DNS to zero, and log network parameters

returning by value calls
CIPAddress::CIPAddress (const CIPAddress &rAddress)
which assert (rAddress.m_bValid) so any unset ip will fail in debug builds
@sourcery-ai
Copy link

sourcery-ai bot commented Aug 8, 2025

Reviewer's Guide

Refactor IP address handling by changing CConfig getters to return const references, load IP properties only when present, and enhance CMiniDexed network initialization with explicit DHCP/static logic, detailed logging, and safe defaults for gateway and DNS.

Sequence diagram for improved network initialization in CMiniDexed::InitNetwork

sequenceDiagram
    participant CMiniDexed
    participant CConfig
    participant CNetSubSystem
    CMiniDexed->>CConfig: GetNetworkDHCP()
    alt DHCP enabled
        CMiniDexed->>CConfig: GetNetworkHostname()
        CMiniDexed->>CNetSubSystem: new (0,0,0,0,hostname, type)
    else Static IP and Subnet set
        CMiniDexed->>CConfig: GetNetworkIPAddress()
        CMiniDexed->>CConfig: GetNetworkSubnetMask()
        CMiniDexed->>CConfig: GetNetworkDefaultGateway()
        CMiniDexed->>CConfig: GetNetworkDNSServer()
        CMiniDexed->>CConfig: GetNetworkHostname()
        CMiniDexed->>CNetSubSystem: new (ip, mask, gw, dns, hostname, type)
    else Neither set
        CMiniDexed->>CConfig: GetNetworkHostname()
        CMiniDexed->>CNetSubSystem: new (0,0,0,0,hostname, type)
    end
    CMiniDexed->>CNetSubSystem: Initialize(false)
    alt Initialization fails
        CMiniDexed->>CMiniDexed: Log error
    end
Loading

Class diagram for updated CConfig IP address handling

classDiagram
    class CConfig {
        +bool GetNetworkDHCP() const
        +const char* GetNetworkType() const
        +const char* GetNetworkHostname() const
        +const CIPAddress& GetNetworkIPAddress() const
        +const CIPAddress& GetNetworkSubnetMask() const
        +const CIPAddress& GetNetworkDefaultGateway() const
        +const CIPAddress& GetNetworkDNSServer() const
        +bool GetSyslogEnabled() const
        +const CIPAddress& GetNetworkSyslogServerIPAddress() const
        +bool GetNetworkFTPEnabled() const
        -CIPAddress m_INetworkIPAddress
        -CIPAddress m_INetworkSubnetMask
        -CIPAddress m_INetworkDefaultGateway
        -CIPAddress m_INetworkDNSServer
        -CIPAddress m_INetworkSyslogServerIPAddress
    }
    class CIPAddress {
        +void Set(const u8* pIP)
        +bool IsSet() const
        +bool IsNull() const
        +void Format(CString* out) const
        +u32 Get() const
    }
    CConfig --> CIPAddress : uses
Loading

Class diagram for updated CMiniDexed::InitNetwork logic

classDiagram
    class CMiniDexed {
        +void UpdateNetwork()
        +bool InitNetwork()
        -CConfig* m_pConfig
        -CNetSubSystem* m_pNet
    }
    class CNetSubSystem {
        +CNetSubSystem(u32 ip, u32 mask, u32 gw, u32 dns, const char* hostname, NetDeviceType type)
        +bool Initialize(bool)
    }
    class CConfig {
        +bool GetNetworkDHCP() const
        +const char* GetNetworkHostname() const
        +const CIPAddress& GetNetworkIPAddress() const
        +const CIPAddress& GetNetworkSubnetMask() const
        +const CIPAddress& GetNetworkDefaultGateway() const
        +const CIPAddress& GetNetworkDNSServer() const
    }
    CMiniDexed --> CConfig : uses
    CMiniDexed --> CNetSubSystem : creates
    CConfig --> CIPAddress : uses
Loading

File-Level Changes

Change Details Files
Refactor CConfig to lazily set IP properties and return const references
  • Load NetworkIPAddress, SubnetMask, DefaultGateway, DNSServer, and SyslogServer only if GetIPAddress returns data, using Set()
  • Change CConfig getters for all network IPs to return const CIPAddress& instead of CIPAddress by value
  • Update config.h declarations to match new const reference getters
src/config.cpp
src/config.h
Use const reference for Syslog server IP in UpdateNetwork
  • Change ServerIP variable in CMiniDexed::UpdateNetwork to const CIPAddress&
  • Remove unnecessary copy when retrieving syslog server address
src/minidexed.cpp
Enhance InitNetwork: differentiate DHCP, static IP, and fallback with detailed logging
  • Add branch for DHCP: log hostname and create CNetSubSystem with zeroed addresses
  • Add branch for static IP: format IP and subnet to CString, log addresses, pass optional gateway and DNS (or zero)
  • Add fallback branch when neither DHCP nor static is set: log and default to DHCP
  • Remove old simplistic CNetSubSystem creation and streamline initialization checks
src/minidexed.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@soyersoyer soyersoyer changed the title InitNetwork fixes InitNetwork IPAddress related fixes Aug 8, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @soyersoyer - I've reviewed your changes - here's some feedback:

  • The fallback branch for when neither DHCP nor static IP is set duplicates the DHCP initialization path; consider refactoring the CNetSubSystem creation into a helper to reduce code duplication.
  • m_pNet is managed with a raw pointer—consider using std::unique_ptr (or another RAII approach) to ensure proper cleanup and avoid potential memory leaks.
  • In CConfig::Load, missing IP properties leave previous CIPAddress values intact; explicitly clear or reset those members when a property is absent to prevent stale data on reload.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The fallback branch for when neither DHCP nor static IP is set duplicates the DHCP initialization path; consider refactoring the CNetSubSystem creation into a helper to reduce code duplication.
- m_pNet is managed with a raw pointer—consider using std::unique_ptr (or another RAII approach) to ensure proper cleanup and avoid potential memory leaks.
- In CConfig::Load, missing IP properties leave previous CIPAddress values intact; explicitly clear or reset those members when a property is absent to prevent stale data on reload.

## Individual Comments

### Comment 1
<location> `src/config.cpp:209` </location>
<code_context>
-	m_INetworkIPAddress = m_Properties.GetIPAddress("NetworkIPAddress") != 0;
-	m_INetworkSubnetMask = m_Properties.GetIPAddress("NetworkSubnetMask") != 0;
-	m_INetworkDefaultGateway = m_Properties.GetIPAddress("NetworkDefaultGateway") != 0;
+	if (const u8 *pIP = m_Properties.GetIPAddress("NetworkIPAddress")) m_INetworkIPAddress.Set (pIP);
+	if (const u8 *pIP = m_Properties.GetIPAddress("NetworkSubnetMask")) m_INetworkSubnetMask.Set (pIP);
+	if (const u8 *pIP = m_Properties.GetIPAddress("NetworkDefaultGateway")) m_INetworkDefaultGateway.Set (pIP);
</code_context>

<issue_to_address>
Assignment logic for IP addresses now only sets values if present, but does not clear them if absent.

This may cause outdated values to persist if properties are removed at runtime. Please reset these member variables to a default state before setting them conditionally.
</issue_to_address>

### Comment 2
<location> `src/minidexed.cpp:2424` </location>
<code_context>
 				m_pNet = new CNetSubSystem(0, 0, 0, 0, m_pConfig->GetNetworkHostname(), NetDeviceType);
-			else
+			}
+			else if (m_pConfig->GetNetworkIPAddress().IsSet() && m_pConfig->GetNetworkSubnetMask().IsSet())
+			{
+				CString IPString, SubnetString;
</code_context>

<issue_to_address>
Default gateway and DNS server are set to 0 if not present, which may have unintended network effects.

If 0 is not a valid value for these parameters, use an explicit 'unset' indicator or confirm that CNetSubSystem treats 0 correctly.

Suggested implementation:

```cpp
				// Use INADDR_NONE as an explicit 'unset' indicator for gateway and DNS if not set
				m_pNet = new CNetSubSystem(
					m_pConfig->GetNetworkIPAddress().Get(),
					m_pConfig->GetNetworkSubnetMask().Get(),
					m_pConfig->GetNetworkDefaultGateway().IsSet() ? m_pConfig->GetNetworkDefaultGateway().Get() : INADDR_NONE,
					m_pConfig->GetNetworkDNSServer().IsSet() ? m_pConfig->GetNetworkDNSServer().Get() : INADDR_NONE,
					m_pConfig->GetNetworkHostname(), NetDeviceType);

```

- Make sure to `#include <arpa/inet.h>` or the appropriate header for `INADDR_NONE` at the top of your file if not already present.
- If your codebase uses a different constant for "unset" (e.g., `-1` or a custom value), replace `INADDR_NONE` with that value.
- Confirm that `CNetSubSystem` treats `INADDR_NONE` as "unset" for gateway and DNS server.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions
Copy link

github-actions bot commented Aug 8, 2025

Build for testing:
MiniDexed_1298_2025-08-08-8fa0e5b_32bit
MiniDexed_1298_2025-08-08-8fa0e5b_64bit
Use at your own risk.

@probonopd
Copy link
Owner

Thanks @soyersoyer. Please have a look at #980 (review), is there something to it?

@soyersoyer
Copy link
Contributor Author

soyersoyer commented Aug 8, 2025

is there something to it?

Yes, but it won't match the style of the file, so no.

For example, unique_ptr could be used, but then the code would have to be rewritten everywhere so that it would not be inconsistent.

@soyersoyer soyersoyer closed this Aug 12, 2025
@probonopd
Copy link
Owner

probonopd commented Aug 12, 2025

Hi @soyersoyer, why was this closed? Since this PR doesn't change the behavior or performance format and is, there is no reason to close it imho.

@probonopd probonopd reopened this Aug 12, 2025
@github-actions
Copy link

Build for testing:
MiniDexed_1299_2025-08-12-12455eb_32bit
MiniDexed_1299_2025-08-12-12455eb_64bit
Use at your own risk.

@soyersoyer
Copy link
Contributor Author

I didn't want to keep track of all the open PRs anymore, so I closed them. You can leave it open.

@probonopd probonopd merged commit 0075bc5 into probonopd:main Aug 21, 2025
6 checks passed
@probonopd
Copy link
Owner

Thanks @soyersoyer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants