1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set et sw=2 ts=4: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NETLINKSERVICE_H_
7 #define NETLINKSERVICE_H_
9 #include <netinet/in.h>
11 #include "nsIRunnable.h"
12 #include "nsThreadUtils.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/TimeStamp.h"
16 #include "nsClassHashtable.h"
17 #include "mozilla/SHA1.h"
18 #include "mozilla/UniquePtr.h"
20 #include "mozilla/net/DNS.h"
26 class NetlinkNeighbor
;
31 class NetlinkServiceListener
: public nsISupports
{
33 virtual void OnNetworkChanged() = 0;
34 virtual void OnNetworkIDChanged() = 0;
35 virtual void OnLinkUp() = 0;
36 virtual void OnLinkDown() = 0;
37 virtual void OnLinkStatusKnown() = 0;
38 virtual void OnDnsSuffixListUpdated() = 0;
41 virtual ~NetlinkServiceListener() = default;
44 class NetlinkService
: public nsIRunnable
{
45 virtual ~NetlinkService();
48 NS_DECL_THREADSAFE_ISUPPORTS
52 nsresult
Init(NetlinkServiceListener
* aListener
);
54 void GetNetworkID(nsACString
& aNetworkID
);
55 void GetIsLinkUp(bool* aIsUp
);
56 nsresult
GetDnsSuffixList(nsTArray
<nsCString
>& aDnsSuffixList
);
57 nsresult
GetResolvers(nsTArray
<NetAddr
>& aResolvers
);
60 void EnqueueGenMsg(uint16_t aMsgType
, uint8_t aFamily
);
61 void EnqueueRtMsg(uint8_t aFamily
, void* aAddress
);
62 void RemovePendingMsg();
64 mozilla::Mutex mMutex MOZ_UNANNOTATED
{"NetlinkService::mMutex"};
66 void OnNetlinkMessage(int aNetlinkSocket
);
67 void OnLinkMessage(struct nlmsghdr
* aNlh
);
68 void OnAddrMessage(struct nlmsghdr
* aNlh
);
69 void OnRouteMessage(struct nlmsghdr
* aNlh
);
70 void OnNeighborMessage(struct nlmsghdr
* aNlh
);
71 void OnRouteCheckResult(struct nlmsghdr
* aNlh
);
73 void UpdateLinkStatus();
75 void TriggerNetworkIDCalculation();
77 void GetGWNeighboursForFamily(uint8_t aFamily
,
78 nsTArray
<NetlinkNeighbor
*>& aGwNeighbors
);
79 bool CalculateIDForFamily(uint8_t aFamily
, mozilla::SHA1Sum
* aSHA1
);
80 void CalculateNetworkID();
81 void ExtractDNSProperties();
83 nsCOMPtr
<nsIThread
> mThread
;
85 bool mInitialScanFinished
{false};
87 // A pipe to signal shutdown with.
88 int mShutdownPipe
[2]{-1, -1};
90 // IP addresses that are used to check the route for public traffic.
91 struct in_addr mRouteCheckIPv4
{};
92 struct in6_addr mRouteCheckIPv6
{};
99 // Flag indicating that network ID could change and should be recalculated.
100 // Calculation is postponed until we receive responses to all enqueued
102 bool mRecalculateNetworkId
{false};
104 // Flag indicating that network change event needs to be sent even if
105 // network ID hasn't changed.
106 bool mSendNetworkChangeEvent
{false};
108 // Time stamp of setting mRecalculateNetworkId to true
109 mozilla::TimeStamp mTriggerTime
;
111 nsCString mNetworkId
;
112 nsTArray
<nsCString
> mDNSSuffixList
;
113 nsTArray
<NetAddr
> mDNSResolvers
;
117 explicit LinkInfo(UniquePtr
<NetlinkLink
>&& aLink
);
120 // Updates mIsUp according to current mLink and mAddresses. Returns true if
121 // the value has changed.
124 // NetlinkLink structure for this link
125 UniquePtr
<NetlinkLink
> mLink
;
127 // All IPv4/IPv6 addresses on this link
128 nsTArray
<UniquePtr
<NetlinkAddress
>> mAddresses
;
130 // All neighbors on this link, key is an address
131 nsClassHashtable
<nsCStringHashKey
, NetlinkNeighbor
> mNeighbors
;
133 // Default IPv4/IPv6 routes
134 nsTArray
<UniquePtr
<NetlinkRoute
>> mDefaultRoutes
;
136 // Link is up when it's running, it's not a loopback and there is
137 // a non-local address associated with it.
141 bool CalculateIDForEthernetLink(uint8_t aFamily
,
142 NetlinkRoute
* aRouteCheckResult
,
143 uint32_t aRouteCheckIfIdx
,
144 LinkInfo
* aRouteCheckLinkInfo
,
145 mozilla::SHA1Sum
* aSHA1
);
146 bool CalculateIDForNonEthernetLink(uint8_t aFamily
,
147 NetlinkRoute
* aRouteCheckResult
,
148 nsTArray
<nsCString
>& aLinkNamesToHash
,
149 uint32_t aRouteCheckIfIdx
,
150 LinkInfo
* aRouteCheckLinkInfo
,
151 mozilla::SHA1Sum
* aSHA1
);
153 nsClassHashtable
<nsUint32HashKey
, LinkInfo
> mLinks
;
155 // Route for mRouteCheckIPv4 address
156 UniquePtr
<NetlinkRoute
> mIPv4RouteCheckResult
;
157 // Route for mRouteCheckIPv6 address
158 UniquePtr
<NetlinkRoute
> mIPv6RouteCheckResult
;
160 nsTArray
<UniquePtr
<NetlinkMsg
>> mOutgoingMessages
;
162 RefPtr
<NetlinkServiceListener
> mListener
;
166 } // namespace mozilla
168 #endif /* NETLINKSERVICE_H_ */