Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous src/inet cleanup #10450

Closed
wants to merge 4 commits into from

Conversation

kpschoedel
Copy link
Contributor

Problem

Working toward #7715 Virtualize System and Inet interfaces

Change overview

  • Convert some enum to enum class, and use BitFlags for flag sets.
  • Rename some class members with m preifx for consistency within src/inet.
  • Revise IPAddress construction.
  • Fix outdated IPAddress documentation.
  • Convert file-local functions from static to anonymous namespace.
  • Revise IPEndPointBasis to reduce duplication and remove dead code.
  • Remove unused flags argument from SendTo() and SendMsg().
  • Replace various sockaddr unions with SockAddr.
  • Remove some exit: return and use ReturnError… more.
  • Move some comments from definition (.cpp) to declaration (.h).
  • Use ObjectLifeCycle for InetLayer.
  • Make some TCPEndPoint members private with TCPTest for tests.

Testing

CI; no changes to functionality intended.

#### Problem

Working toward project-chip#7715 Virtualize System and Inet interfaces

#### Change overview

- Convert some `enum` to `enum class`, and use `BitFlags` for flag sets.
- Rename some class members with `m` preifx for consistency within `src/inet`.
- Revise `IPAddress` construction.
- Fix outdated `IPAddress` documentation.
- Convert file-local functions from `static` to anonymous `namespace`.
- Revise `IPEndPointBasis` to reduce duplication and remove dead code.
- Remove unused flags argument from `SendTo()` and `SendMsg()`.
- Replace various `sockaddr` unions with `SockAddr`.
- Remove some `exit: return` and use `ReturnError…` more.
- Move some comments from definition (.cpp) to declaration (.h).
- Use `ObjectLifeCycle` for `InetLayer`.
- Make some `TCPEndPoint` members `private` with `TCPTest` for tests.

#### Testing

CI; no changes to functionality intended.
@@ -90,7 +90,11 @@ CHIP_ERROR AsyncDNSResolverSockets::Shutdown()

AsyncMutexLock();

mInet->State = InetLayer::kState_ShutdownInProgress;
if (mInet->mLayerState.GetState() != ObjectLifeCycle::State::ShuttingDown)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the (Async)DNSResolver used/needed at all beyond tests? Or we're keeping it just in case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I honestly don't know. If someone can tell me definitively that AsyncDNSResolver in particular is not used, I'll be very happy to get rid of it, since it's the only thing using threads internal to the SDK, and therefore an obstacle to simplification (cf #7725).

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe CHIP should only use mDNS and we have either platform or minmdns for those , nothing in inet.

Lets delete it. If I am wrong we can always add it back.

}
return false;
}
bool Initializing() { return Transition<State::Uninitialized, State::Initializing>(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: not sure if it's just me, but imho state.Initializing() looks like a getter (is it in "Initializing" state). state.SetInitializing() would be less ambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I'll rename them all in some consistent way.

@@ -185,7 +185,7 @@ struct DiscoveredNodeData
for (int j = 0; j < numIPs; j++)
{
#if CHIP_DETAIL_LOGGING
char buf[Inet::kMaxIPAddressStringLength];
char buf[Inet::IPAddress::kMaxStringLength];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are the lines above:

        char addrBuffer[Inet::IPAddress::kMaxStringLength + 1];
        mAddress.ToString(addrBuffer);

and here it's not +1. Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Presumably one author didn't check whether kMaxStringLength includes the terminating NUL (it does), since it isn't documented at the declaration (it will be).

Copy link
Contributor

@bzbarsky-apple bzbarsky-apple left a comment

Choose a reason for hiding this comment

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

I stopped at the beginning of IPEndPointBasis.cpp. I just don't have a block of time of sufficient size to review something like this carefully... Might have been better as multiple smaller PRs.

resolver.mNumAddrs = 0;
resolver.mDNSOptions = options;
resolver.mAddrArray = addrArray;
resolver.AppState = appState;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not mAppState here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It belongs to System::Object, which I didn't want to add to this PR.

// when the request completes.
//
// To ensure the right thing happens, we NULL the OnComplete pointer here, which signals the
// To ensure the right thing happens, we nullptr the mOnComplete pointer here, which signals the
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// To ensure the right thing happens, we nullptr the mOnComplete pointer here, which signals the
// To ensure the right thing happens, we null out the mOnComplete pointer here, which signals the

"nullptr" is not a verb....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed and checked for other instances.

@@ -186,7 +188,7 @@ void DNSResolver::HandleResolveComplete()
* This method is called by LwIP network stack on success, failure, or timeout
* of a DNS request.
*
* @param[in] name A pointer to a NULL-terminated C string
* @param[in] name A pointer to a nullptr-terminated C string
Copy link
Contributor

Choose a reason for hiding this comment

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

No, it's not nullptr-terminated. It's terminated by '\0', presumably....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto

Comment on lines +84 to +87
kUnused = 0, ///< Used to indicate that the DNSResolver object is not used.
kActive = 2, ///< Used to indicate that a DNS resolution is being performed on the DNSResolver object.
kComplete = 3, ///< Used to indicate that the DNS resolution on the DNSResolver object is complete.
kCanceled = 4, ///< Used to indicate that the DNS resolution on the DNSResolver has been canceled.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
kUnused = 0, ///< Used to indicate that the DNSResolver object is not used.
kActive = 2, ///< Used to indicate that a DNS resolution is being performed on the DNSResolver object.
kComplete = 3, ///< Used to indicate that the DNS resolution on the DNSResolver object is complete.
kCanceled = 4, ///< Used to indicate that the DNS resolution on the DNSResolver has been canceled.
Unused = 0, ///< Used to indicate that the DNSResolver object is not used.
Active = 2, ///< Used to indicate that a DNS resolution is being performed on the DNSResolver object.
Complete = 3, ///< Used to indicate that the DNS resolution on the DNSResolver object is complete.
Canceled = 4, ///< Used to indicate that the DNS resolution on the DNSResolver has been canceled.

That "k" is just noise for enum class values....

Comment on lines +86 to +90
kUnknown = 0,
kRaw = 1,
kUDP = 2,
kUCP = 3,
kTCP = 4
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this addition of "k"? We don't generally do that for enum classes, as far as I can tell, nor should we.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That got requested in a previous review. Although I wouldn't choose it personally, existing code uses the k prefix about 2/3 of the time in enum class. https://gist.github.com/kpschoedel/dc99300e71e9292d95c0c26471101397

Copy link
Contributor

Choose a reason for hiding this comment

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

We should come up with some clear guidelines for this. I've now seen people changing it to "no-k" and back to "with-k" in a tug-of-war, which is just wasting everyone's time. @woody-apple @andy31415

Again, my personal preference is to drop the k; I've generally seen it appear as a result of mass-conversions to enum class that kept it because it was there for the non-enum-class version....

Copy link
Contributor

@andy31415 andy31415 Oct 18, 2021

Choose a reason for hiding this comment

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

I am on the use k side of the fence. The logic being that I would like to trivially see when constants are used in the code. If I have:

class Foo {
  public:
     constexpr int kBar = 1;
}

enum class Baz {
  kVal = 1;
}

then Foo::kBar and Baz::kVal seem trivially visible as constants regardless where they come from. Otherwise having Baz::Val and Foo::kBar seems inconsitent and switching everything to drop k would make Foo access constants as just Bar which seems wrong.

Comment on lines +90 to +93
kUnknown = 0, ///< Not used.
kIPv4 = 1, ///< Internet protocol version 4.
kIPv6 = 2, ///< Internet protocol version 6.
kAny = 3 ///< The unspecified internet address (independent of protocol version).
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, would be better without the "k" prefix, which is a holdover from non-scoped enums.

Comment on lines +105 to +108
kTransient = 0x01,

/** The multicast address is (1) based on a network prefix. */
kIPv6MulticastFlag_Prefix = 0x02
} IPv6MulticastFlag;
kPrefix = 0x02
Copy link
Contributor

Choose a reason for hiding this comment

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

And here, better without "k".

@kpschoedel
Copy link
Contributor Author

I stopped at the beginning of IPEndPointBasis.cpp. I just don't have a block of time of sufficient size to review something like this carefully... Might have been better as multiple smaller PRs.

I'll see whether I can split off parts without introducing too much sequential dependency.

@kpschoedel kpschoedel marked this pull request as draft October 14, 2021 17:46
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this pull request Oct 21, 2021
#### Problem

`Inet::IPAddress` has some redundancies and outdated comments.

#### Change overview

- Convert `IPAddress::From…()` functions that are semantically
  constructors into constructors.
- Replace several identical `union`s with `IPAddress::SockAddr`.
- Fix outdated documentation regarding IPv4-in-IPv6.
- Fix a use of `IPAddress::kMaxStringLength` (from project-chip#10450 review).

#### Testing

CI; no changes to functionality intended.
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this pull request Oct 21, 2021
#### Problem

`Inet::IPAddress` has some redundancies and outdated comments.

#### Change overview

- Convert `IPAddress::From…()` functions that are semantically
  constructors into constructors.
- Replace several identical `union`s with `IPAddress::SockAddr`.
- Fix outdated documentation regarding IPv4-in-IPv6.
- Fix a use of `IPAddress::kMaxStringLength` (from project-chip#10450 review).

#### Testing

CI; no changes to functionality intended.
andy31415 pushed a commit that referenced this pull request Oct 22, 2021
* Revisions to Inet::IPAddress

#### Problem

`Inet::IPAddress` has some redundancies and outdated comments.

#### Change overview

- Convert `IPAddress::From…()` functions that are semantically
  constructors into constructors.
- Replace several identical `union`s with `IPAddress::SockAddr`.
- Fix outdated documentation regarding IPv4-in-IPv6.
- Fix a use of `IPAddress::kMaxStringLength` (from #10450 review).

#### Testing

CI; no changes to functionality intended.

* review
@stale
Copy link

stale bot commented Oct 25, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale Stale issue or PR label Oct 25, 2021
@kpschoedel kpschoedel closed this Oct 25, 2021
@kpschoedel kpschoedel deleted the x7715-inet-3 branch October 25, 2021 15:09
@kpschoedel
Copy link
Contributor Author

Closed because most of this has now moved to other PRs.

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

Successfully merging this pull request may close these issues.

5 participants