1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set et sw=4 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 NSNOTIFYADDRLISTENER_LINUX_H_
7 #define NSNOTIFYADDRLISTENER_LINUX_H_
8 
9 #include <sys/socket.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <linux/netlink.h>
14 #include <linux/rtnetlink.h>
15 #include <arpa/inet.h>
16 #include <unistd.h>
17 
18 #include "nsINetworkLinkService.h"
19 #include "nsIRunnable.h"
20 #include "nsIObserver.h"
21 #include "nsThreadUtils.h"
22 #include "nsCOMPtr.h"
23 #include "mozilla/Atomics.h"
24 #include "mozilla/TimeStamp.h"
25 #include "nsITimer.h"
26 #include "nsClassHashtable.h"
27 
28 class nsNotifyAddrListener : public nsINetworkLinkService,
29                              public nsIRunnable,
30                              public nsIObserver
31 {
32     virtual ~nsNotifyAddrListener();
33 
34 public:
35     NS_DECL_THREADSAFE_ISUPPORTS
36     NS_DECL_NSINETWORKLINKSERVICE
37     NS_DECL_NSIRUNNABLE
38     NS_DECL_NSIOBSERVER
39 
40     nsNotifyAddrListener();
41     nsresult Init(void);
42 
43 private:
44     class ChangeEvent : public mozilla::Runnable {
45     public:
46         NS_DECL_NSIRUNNABLE
ChangeEvent(nsINetworkLinkService * aService,const char * aEventID)47         ChangeEvent(nsINetworkLinkService *aService, const char *aEventID)
48             : mService(aService), mEventID(aEventID) {
49         }
50     private:
51         nsCOMPtr<nsINetworkLinkService> mService;
52         const char *mEventID;
53     };
54 
55     // Called when xpcom-shutdown-threads is received.
56     nsresult Shutdown(void);
57 
58     // Called when a network change was detected
59     nsresult NetworkChanged();
60 
61     // Sends the network event.
62     nsresult SendEvent(const char *aEventID);
63 
64     // Figure out the current "network identification"
65     void calculateNetworkId(void);
66     nsCString mNetworkId;
67 
68     // Checks if there's a network "link"
69     void checkLink(void);
70 
71     // Deals with incoming NETLINK messages.
72     void OnNetlinkMessage(int NetlinkSocket);
73 
74     nsCOMPtr<nsIThread> mThread;
75 
76     // The network is up.
77     bool mLinkUp;
78 
79     // The network's up/down status is known.
80     bool mStatusKnown;
81 
82     // A pipe to signal shutdown with.
83     int mShutdownPipe[2];
84 
85     // Network changed events are enabled
86     bool mAllowChangedEvent;
87 
88     // Flag set while coalescing change events
89     bool mCoalescingActive;
90 
91     // Time stamp for first event during coalescing
92     mozilla::TimeStamp mChangeTime;
93 
94     // Seen Ip addresses. For Ipv6 addresses some time router renews their
95     // lifetime and we should not detect this as a network link change, so we
96     // keep info about all seen addresses.
97      nsClassHashtable<nsCStringHashKey, struct ifaddrmsg> mAddressInfo;
98  };
99 
100 #endif /* NSNOTIFYADDRLISTENER_LINUX_H_ */
101