NetlinkHandler.cpp revision e07effe6f8e9340dbee9428b29672adfb647c413
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 <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <errno.h>
21
22#define LOG_TAG "Netd"
23
24#include <cutils/log.h>
25
26#include <sysutils/NetlinkEvent.h>
27#include "NetlinkHandler.h"
28#include "NetlinkManager.h"
29#include "ResponseCode.h"
30
31NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
32                               int format) :
33                        NetlinkListener(listenerSocket, format) {
34    mNm = nm;
35}
36
37NetlinkHandler::~NetlinkHandler() {
38}
39
40int NetlinkHandler::start() {
41    return this->startListener();
42}
43
44int NetlinkHandler::stop() {
45    return this->stopListener();
46}
47
48void NetlinkHandler::onEvent(NetlinkEvent *evt) {
49    const char *subsys = evt->getSubsystem();
50    if (!subsys) {
51        ALOGW("No subsystem found in netlink event");
52        return;
53    }
54
55    ALOGV("subsystem %s", subsys);
56
57    if (!strcmp(subsys, "net")) {
58        int action = evt->getAction();
59        const char *iface = evt->findParam("INTERFACE");
60
61        if (action == evt->NlActionAdd) {
62            notifyInterfaceAdded(iface);
63        } else if (action == evt->NlActionRemove) {
64            notifyInterfaceRemoved(iface);
65        } else if (action == evt->NlActionChange) {
66            evt->dump();
67            notifyInterfaceChanged("nana", true);
68        } else if (action == evt->NlActionLinkUp) {
69            notifyInterfaceLinkChanged(iface, true);
70        } else if (action == evt->NlActionLinkDown) {
71            notifyInterfaceLinkChanged(iface, false);
72        }
73
74    } else if (!strcmp(subsys, "qlog")) {
75        const char *alertName = evt->findParam("ALERT_NAME");
76        const char *iface = evt->findParam("INTERFACE");
77        notifyQuotaLimitReached(alertName, iface);
78
79    } else if (!strcmp(subsys, "xt_idletimer")) {
80        int action = evt->getAction();
81        const char *iface = evt->findParam("INTERFACE");
82        const char *state = evt->findParam("STATE");
83        if (state)
84            notifyInterfaceActivity(iface, !strcmp("active", state));
85
86    }
87}
88
89void NetlinkHandler::notifyInterfaceAdded(const char *name) {
90    char msg[255];
91    snprintf(msg, sizeof(msg), "Iface added %s", name);
92
93    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
94            msg, false);
95}
96
97void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
98    char msg[255];
99    snprintf(msg, sizeof(msg), "Iface removed %s", name);
100
101    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
102            msg, false);
103}
104
105void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
106    char msg[255];
107    snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
108             (isUp ? "up" : "down"));
109
110    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
111            msg, false);
112}
113
114void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
115    char msg[255];
116    snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
117             (isUp ? "up" : "down"));
118
119    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
120            msg, false);
121}
122
123void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
124    char msg[255];
125    snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
126
127    mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
128            msg, false);
129}
130
131void NetlinkHandler::notifyInterfaceActivity(const char *name, bool isActive) {
132    char msg[255];
133
134    snprintf(msg, sizeof(msg), "Iface %s %s", name, isActive ? "active" : "idle");
135    ALOGV("Broadcasting interface activity msg: %s", msg);
136    mNm->getBroadcaster()->sendBroadcast(isActive ? ResponseCode::InterfaceActive
137            : ResponseCode::InterfaceIdle,
138            msg, false);
139}
140