Home | History | Annotate | Download | only in nexus
      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 <errno.h>
     19 
     20 #define LOG_TAG "Nexus"
     21 
     22 #include <cutils/log.h>
     23 
     24 #include "NetworkManager.h"
     25 #include "InterfaceConfig.h"
     26 #include "DhcpClient.h"
     27 #include "DhcpState.h"
     28 #include "DhcpEvent.h"
     29 #include "ResponseCode.h"
     30 
     31 NetworkManager *NetworkManager::sInstance = NULL;
     32 
     33 NetworkManager *NetworkManager::Instance() {
     34     if (!sInstance)
     35         sInstance = new NetworkManager(new PropertyManager());
     36     return sInstance;
     37 }
     38 
     39 NetworkManager::NetworkManager(PropertyManager *propMngr) {
     40     mBroadcaster = NULL;
     41     mControllerBindings = new ControllerBindingCollection();
     42     mPropMngr = propMngr;
     43     mLastDhcpState = DhcpState::INIT;
     44     mDhcp = new DhcpClient(this);
     45 }
     46 
     47 NetworkManager::~NetworkManager() {
     48 }
     49 
     50 int NetworkManager::run() {
     51     if (startControllers()) {
     52         LOGW("Unable to start all controllers (%s)", strerror(errno));
     53     }
     54     return 0;
     55 }
     56 
     57 int NetworkManager::attachController(Controller *c) {
     58     ControllerBinding *cb = new ControllerBinding(c);
     59     mControllerBindings->push_back(cb);
     60     return 0;
     61 }
     62 
     63 int NetworkManager::startControllers() {
     64     int rc = 0;
     65     ControllerBindingCollection::iterator it;
     66 
     67     for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
     68         int irc = (*it)->getController()->start();
     69         if (irc && !rc)
     70             rc = irc;
     71     }
     72     return rc;
     73 }
     74 
     75 int NetworkManager::stopControllers() {
     76     int rc = 0;
     77     ControllerBindingCollection::iterator it;
     78 
     79     for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
     80         int irc = (*it)->getController()->stop();
     81         if (irc && !rc)
     82             rc = irc;
     83     }
     84     return rc;
     85 }
     86 
     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);
     93     }
     94     errno = ENOENT;
     95     return NULL;
     96 }
     97 
     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) {
    110     LOGD("Controller %s interface %s connected", c->getName(), c->getBoundInterface());
    111 
    112     if (mDhcp->start(c)) {
    113         LOGE("Failed to start DHCP (%s)", strerror(errno));
    114         return;
    115     }
    116 }
    117 
    118 void NetworkManager::onInterfaceDisconnected(Controller *c) {
    119     LOGD("Controller %s interface %s disconnected", c->getName(),
    120          c->getBoundInterface());
    121 
    122     mDhcp->stop();
    123 }
    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 
    206