San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [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 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #define LOG_TAG "Nexus" |
| 21 | |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include "NetworkManager.h" |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 25 | #include "InterfaceConfig.h" |
San Mehat | d6c6796 | 2009-06-22 10:39:36 -0700 | [diff] [blame] | 26 | #include "DhcpClient.h" |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 27 | #include "DhcpState.h" |
| 28 | #include "DhcpEvent.h" |
| 29 | #include "ResponseCode.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 30 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 31 | NetworkManager *NetworkManager::sInstance = NULL; |
| 32 | |
| 33 | NetworkManager *NetworkManager::Instance() { |
| 34 | if (!sInstance) |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 35 | sInstance = new NetworkManager(new PropertyManager()); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 36 | return sInstance; |
| 37 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 38 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 39 | NetworkManager::NetworkManager(PropertyManager *propMngr) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 40 | mBroadcaster = NULL; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 41 | mControllerBindings = new ControllerBindingCollection(); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 42 | mPropMngr = propMngr; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 43 | mLastDhcpState = DhcpState::INIT; |
San Mehat | d6c6796 | 2009-06-22 10:39:36 -0700 | [diff] [blame] | 44 | mDhcp = new DhcpClient(this); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | NetworkManager::~NetworkManager() { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int NetworkManager::run() { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 51 | if (startControllers()) { |
| 52 | LOGW("Unable to start all controllers (%s)", strerror(errno)); |
| 53 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 57 | int NetworkManager::attachController(Controller *c) { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 58 | ControllerBinding *cb = new ControllerBinding(c); |
| 59 | mControllerBindings->push_back(cb); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 60 | return 0; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int NetworkManager::startControllers() { |
| 64 | int rc = 0; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 65 | ControllerBindingCollection::iterator it; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 66 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 67 | for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) { |
| 68 | int irc = (*it)->getController()->start(); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 69 | if (irc && !rc) |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | rc = irc; |
| 71 | } |
| 72 | return rc; |
| 73 | } |
| 74 | |
| 75 | int NetworkManager::stopControllers() { |
| 76 | int rc = 0; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 77 | ControllerBindingCollection::iterator it; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 78 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 79 | for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) { |
| 80 | int irc = (*it)->getController()->stop(); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 81 | if (irc && !rc) |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 82 | rc = irc; |
| 83 | } |
| 84 | return rc; |
| 85 | } |
| 86 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 87 | NetworkManager::ControllerBinding *NetworkManager::lookupBinding(Controller *c) { |
| 88 | ControllerBindingCollection::iterator it; |
| 89 | |
| 90 | for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) { |
| 91 | if ((*it)->getController() == c) |
| 92 | return (*it); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 93 | } |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 94 | errno = ENOENT; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 98 | Controller *NetworkManager::findController(const char *name) { |
| 99 | ControllerBindingCollection::iterator it; |
| 100 | |
| 101 | for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) { |
| 102 | if (!strcasecmp((*it)->getController()->getName(), name)) |
| 103 | return (*it)->getController(); |
| 104 | } |
| 105 | errno = ENOENT; |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | void NetworkManager::onInterfaceConnected(Controller *c) { |
San Mehat | d6c6796 | 2009-06-22 10:39:36 -0700 | [diff] [blame] | 110 | LOGD("Controller %s interface %s connected", c->getName(), c->getBoundInterface()); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 111 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 112 | if (mDhcp->start(c)) { |
| 113 | LOGE("Failed to start DHCP (%s)", strerror(errno)); |
| 114 | return; |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 115 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 118 | void NetworkManager::onInterfaceDisconnected(Controller *c) { |
| 119 | LOGD("Controller %s interface %s disconnected", c->getName(), |
| 120 | c->getBoundInterface()); |
San Mehat | d6c6796 | 2009-06-22 10:39:36 -0700 | [diff] [blame] | 121 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 122 | mDhcp->stop(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 123 | } |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 124 | |
| 125 | void NetworkManager::onControllerSuspending(Controller *c) { |
| 126 | LOGD("Controller %s interface %s suspending", c->getName(), |
| 127 | c->getBoundInterface()); |
| 128 | mDhcp->stop(); |
| 129 | } |
| 130 | |
| 131 | void NetworkManager::onControllerResumed(Controller *c) { |
| 132 | LOGD("Controller %s interface %s resumed", c->getName(), |
| 133 | c->getBoundInterface()); |
| 134 | } |
| 135 | |
| 136 | void NetworkManager::onDhcpStateChanged(Controller *c, int state) { |
| 137 | char tmp[255]; |
| 138 | char tmp2[255]; |
| 139 | |
| 140 | LOGD("onDhcpStateChanged(%s -> %s)", |
| 141 | DhcpState::toString(mLastDhcpState, tmp, sizeof(tmp)), |
| 142 | DhcpState::toString(state, tmp2, sizeof(tmp2))); |
| 143 | |
| 144 | switch(state) { |
| 145 | case DhcpState::BOUND: |
| 146 | // Refresh the 'net.xxx' for the controller |
| 147 | break; |
| 148 | case DhcpState::RENEWING: |
| 149 | break; |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | char *tmp3; |
| 155 | asprintf(&tmp3, |
| 156 | "DHCP state changed from %d (%s) -> %d (%s)", |
| 157 | mLastDhcpState, |
| 158 | DhcpState::toString(mLastDhcpState, tmp, sizeof(tmp)), |
| 159 | state, |
| 160 | DhcpState::toString(state, tmp2, sizeof(tmp2))); |
| 161 | |
| 162 | getBroadcaster()->sendBroadcast(ResponseCode::DhcpStateChange, |
| 163 | tmp3, |
| 164 | false); |
| 165 | free(tmp3); |
| 166 | |
| 167 | mLastDhcpState = state; |
| 168 | } |
| 169 | |
| 170 | void NetworkManager::onDhcpEvent(Controller *c, int evt) { |
| 171 | char tmp[64]; |
| 172 | LOGD("onDhcpEvent(%s)", DhcpEvent::toString(evt, tmp, sizeof(tmp))); |
| 173 | } |
| 174 | |
| 175 | void NetworkManager::onDhcpLeaseUpdated(Controller *c, struct in_addr *addr, |
| 176 | struct in_addr *net, |
| 177 | struct in_addr *brd, |
| 178 | struct in_addr *gw, |
| 179 | struct in_addr *dns1, |
| 180 | struct in_addr *dns2) { |
| 181 | ControllerBinding *bind = lookupBinding(c); |
| 182 | |
| 183 | if (!bind->getCurrentCfg()) |
| 184 | bind->setCurrentCfg(new InterfaceConfig(true)); |
| 185 | |
| 186 | bind->getCurrentCfg()->setIp(addr); |
| 187 | bind->getCurrentCfg()->setNetmask(net); |
| 188 | bind->getCurrentCfg()->setGateway(gw); |
| 189 | bind->getCurrentCfg()->setBroadcast(brd); |
| 190 | bind->getCurrentCfg()->setDns(0, dns1); |
| 191 | bind->getCurrentCfg()->setDns(1, dns2); |
| 192 | } |
| 193 | |
| 194 | NetworkManager::ControllerBinding::ControllerBinding(Controller *c) : |
| 195 | mController(c) { |
| 196 | } |
| 197 | |
| 198 | void NetworkManager::ControllerBinding::setCurrentCfg(InterfaceConfig *c) { |
| 199 | mCurrentCfg = c; |
| 200 | } |
| 201 | |
| 202 | void NetworkManager::ControllerBinding::setBoundCfg(InterfaceConfig *c) { |
| 203 | mBoundCfg = c; |
| 204 | } |
| 205 | |