NetlinkHandler.cpp revision 19fb0c4e5ec6a10473666a2d45267fbc8305ba85
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 NetlinkListener(listenerSocket) { 33 mNm = nm; 34} 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(); 49 if (!subsys) { 50 LOGW("No subsystem found in netlink event"); 51 return; 52 } 53 if (!strcmp(subsys, "net")) { 54 int action = evt->getAction(); 55 if (action == evt->NlActionAdd) { 56 const char *iface = evt->findParam("INTERFACE"); 57 notifyInterfaceAdded(iface); 58 } else if (action == evt->NlActionRemove) { 59 const char *iface = evt->findParam("INTERFACE"); 60 notifyInterfaceRemoved(iface); 61 } else if (action == evt->NlActionChange) { 62 evt->dump(); 63 const char *iface = evt->findParam("INTERFACE"); 64 notifyInterfaceChanged("nana", true); 65 } 66 } 67} 68 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]; 87 snprintf(msg, sizeof(msg), "Iface is %s %s", (isUp ? "up" : "down"), name); 88 89 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange, 90 msg, false); 91} 92