GoogleGit

blob: 8331ae0dc425e25c34ee624977a57b8a76d205de [file] [log] [blame]
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #define LOG_TAG "Netd"
  21. #include <cutils/log.h>
  22. #include <sysutils/NetlinkEvent.h>
  23. #include "NetlinkHandler.h"
  24. #include "NetlinkManager.h"
  25. #include "ResponseCode.h"
  26. NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
  27. int format) :
  28. NetlinkListener(listenerSocket, format) {
  29. mNm = nm;
  30. }
  31. NetlinkHandler::~NetlinkHandler() {
  32. }
  33. int NetlinkHandler::start() {
  34. return this->startListener();
  35. }
  36. int NetlinkHandler::stop() {
  37. return this->stopListener();
  38. }
  39. void NetlinkHandler::onEvent(NetlinkEvent *evt) {
  40. const char *subsys = evt->getSubsystem();
  41. if (!subsys) {
  42. LOGW("No subsystem found in netlink event");
  43. return;
  44. }
  45. if (!strcmp(subsys, "net")) {
  46. int action = evt->getAction();
  47. const char *iface = evt->findParam("INTERFACE");
  48. if (action == evt->NlActionAdd) {
  49. notifyInterfaceAdded(iface);
  50. } else if (action == evt->NlActionRemove) {
  51. notifyInterfaceRemoved(iface);
  52. } else if (action == evt->NlActionChange) {
  53. evt->dump();
  54. notifyInterfaceChanged("nana", true);
  55. } else if (action == evt->NlActionLinkUp) {
  56. notifyInterfaceLinkChanged(iface, true);
  57. } else if (action == evt->NlActionLinkDown) {
  58. notifyInterfaceLinkChanged(iface, false);
  59. }
  60. } else if (!strcmp(subsys, "qlog")) {
  61. const char *alertName = evt->findParam("ALERT_NAME");
  62. const char *iface = evt->findParam("INTERFACE");
  63. notifyQuotaLimitReached(alertName, iface);
  64. }
  65. }
  66. void NetlinkHandler::notifyInterfaceAdded(const char *name) {
  67. char msg[255];
  68. snprintf(msg, sizeof(msg), "Iface added %s", name);
  69. mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
  70. msg, false);
  71. }
  72. void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
  73. char msg[255];
  74. snprintf(msg, sizeof(msg), "Iface removed %s", name);
  75. mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
  76. msg, false);
  77. }
  78. void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
  79. char msg[255];
  80. snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
  81. (isUp ? "up" : "down"));
  82. mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
  83. msg, false);
  84. }
  85. void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
  86. char msg[255];
  87. snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
  88. (isUp ? "up" : "down"));
  89. mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
  90. msg, false);
  91. }
  92. void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
  93. char msg[255];
  94. snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
  95. mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
  96. msg, false);
  97. }