OSDN Git Service

am 6aac1ccd: Merge "system: netd: prevent infinite loop"
[android-x86/system-netd.git] / NetlinkHandler.cpp
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #define LOG_TAG "Netd"
24
25 #include <cutils/log.h>
26
27 #include <sysutils/NetlinkEvent.h>
28 #include "NetlinkHandler.h"
29 #include "NetlinkManager.h"
30 #include "ResponseCode.h"
31
32 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
33                                int format) :
34                         NetlinkListener(listenerSocket, format) {
35     mNm = nm;
36 }
37
38 NetlinkHandler::~NetlinkHandler() {
39 }
40
41 int NetlinkHandler::start() {
42     return this->startListener();
43 }
44
45 int NetlinkHandler::stop() {
46     return this->stopListener();
47 }
48
49 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
50     const char *subsys = evt->getSubsystem();
51     if (!subsys) {
52         ALOGW("No subsystem found in netlink event");
53         return;
54     }
55
56     if (!strcmp(subsys, "net")) {
57         int action = evt->getAction();
58         const char *iface = evt->findParam("INTERFACE");
59
60         if (action == evt->NlActionAdd) {
61             notifyInterfaceAdded(iface);
62         } else if (action == evt->NlActionRemove) {
63             notifyInterfaceRemoved(iface);
64         } else if (action == evt->NlActionChange) {
65             evt->dump();
66             notifyInterfaceChanged("nana", true);
67         } else if (action == evt->NlActionLinkUp) {
68             notifyInterfaceLinkChanged(iface, true);
69         } else if (action == evt->NlActionLinkDown) {
70             notifyInterfaceLinkChanged(iface, false);
71         } else if (action == evt->NlActionAddressUpdated ||
72                    action == evt->NlActionAddressRemoved) {
73             const char *address = evt->findParam("ADDRESS");
74             const char *flags = evt->findParam("FLAGS");
75             const char *scope = evt->findParam("SCOPE");
76             if (iface && flags && scope) {
77                 notifyAddressChanged(action, address, iface, flags, scope);
78             }
79         } else if (action == evt->NlActionRdnss) {
80             const char *lifetime = evt->findParam("LIFETIME");
81             const char *servers = evt->findParam("SERVERS");
82             if (lifetime && servers) {
83                 notifyInterfaceDnsServers(iface, lifetime, servers);
84             }
85         }
86
87     } else if (!strcmp(subsys, "qlog")) {
88         const char *alertName = evt->findParam("ALERT_NAME");
89         const char *iface = evt->findParam("INTERFACE");
90         notifyQuotaLimitReached(alertName, iface);
91
92     } else if (!strcmp(subsys, "xt_idletimer")) {
93         int action = evt->getAction();
94         const char *label = evt->findParam("LABEL");
95         const char *state = evt->findParam("STATE");
96         // if no LABEL, use INTERFACE instead
97         if (label == NULL) {
98             label = evt->findParam("INTERFACE");
99         }
100         if (state)
101             notifyInterfaceClassActivity(label, !strcmp("active", state));
102
103 #if !LOG_NDEBUG
104     } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
105         /* It is not a VSYNC or a backlight event */
106         ALOGV("unexpected event from subsystem %s", subsys);
107 #endif
108     }
109 }
110
111 void NetlinkHandler::notify(int code, const char *format, ...) {
112     char *msg;
113     va_list args;
114     va_start(args, format);
115     if (vasprintf(&msg, format, args) >= 0) {
116         mNm->getBroadcaster()->sendBroadcast(code, msg, false);
117         free(msg);
118     } else {
119         SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
120     }
121     va_end(args);
122 }
123
124 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
125     notify(ResponseCode::InterfaceChange, "Iface added %s", name);
126 }
127
128 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
129     notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
130 }
131
132 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
133     notify(ResponseCode::InterfaceChange,
134            "Iface changed %s %s", name, (isUp ? "up" : "down"));
135 }
136
137 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
138     notify(ResponseCode::InterfaceChange,
139            "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
140 }
141
142 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
143     notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
144 }
145
146 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
147                                                   bool isActive) {
148     notify(ResponseCode::InterfaceClassActivity,
149            "IfaceClass %s %s", isActive ? "active" : "idle", name);
150 }
151
152 void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
153                                           const char *iface, const char *flags,
154                                           const char *scope) {
155     notify(ResponseCode::InterfaceAddressChange,
156            "Address %s %s %s %s %s",
157            (action == NetlinkEvent::NlActionAddressUpdated) ?
158            "updated" : "removed", addr, iface, flags, scope);
159 }
160
161 void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
162                                                const char *lifetime,
163                                                const char *servers) {
164     notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
165            iface, lifetime, servers);
166 }