blob: 800c86cb7ab43e47a74214c0998a6804a877460e [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080019#include <string.h>
San Mehatd1830422010-01-15 08:02:39 -080020#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"
Robert Greenwalt67c57532010-02-17 17:42:37 -080028#include "NetlinkManager.h"
29#include "ResponseCode.h"
San Mehatd1830422010-01-15 08:02:39 -080030
Wink Saville19fb0c42011-01-09 12:21:40 -080031NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket) :
32 NetlinkListener(listenerSocket) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080033 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080034}
35
36NetlinkHandler::~NetlinkHandler() {
37}
38
39int NetlinkHandler::start() {
40 return this->startListener();
41}
42
43int NetlinkHandler::stop() {
44 return this->stopListener();
45}
46
47void NetlinkHandler::onEvent(NetlinkEvent *evt) {
48 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080049 if (!subsys) {
50 LOGW("No subsystem found in netlink event");
51 return;
52 }
Robert Greenwalt67c57532010-02-17 17:42:37 -080053 if (!strcmp(subsys, "net")) {
54 int action = evt->getAction();
55 if (action == evt->NlActionAdd) {
Wink Saville19fb0c42011-01-09 12:21:40 -080056 const char *iface = evt->findParam("INTERFACE");
Robert Greenwalt67c57532010-02-17 17:42:37 -080057 notifyInterfaceAdded(iface);
58 } else if (action == evt->NlActionRemove) {
Wink Saville19fb0c42011-01-09 12:21:40 -080059 const char *iface = evt->findParam("INTERFACE");
Robert Greenwalt67c57532010-02-17 17:42:37 -080060 notifyInterfaceRemoved(iface);
61 } else if (action == evt->NlActionChange) {
62 evt->dump();
Wink Saville19fb0c42011-01-09 12:21:40 -080063 const char *iface = evt->findParam("INTERFACE");
Robert Greenwalt67c57532010-02-17 17:42:37 -080064 notifyInterfaceChanged("nana", true);
65 }
San Mehatd1830422010-01-15 08:02:39 -080066 }
67}
Robert Greenwalt67c57532010-02-17 17:42:37 -080068
69void NetlinkHandler::notifyInterfaceAdded(const char *name) {
70 char msg[255];
71 snprintf(msg, sizeof(msg), "Iface added %s", name);
72
73 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
74 msg, false);
75}
76
77void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
78 char msg[255];
79 snprintf(msg, sizeof(msg), "Iface removed %s", name);
80
81 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
82 msg, false);
83}
84
85void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
86 char msg[255];
Wink Saville19fb0c42011-01-09 12:21:40 -080087 snprintf(msg, sizeof(msg), "Iface is %s %s", (isUp ? "up" : "down"), name);
Robert Greenwalt67c57532010-02-17 17:42:37 -080088
89 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
90 msg, false);
91}