San Mehat | 192331d | 2009-05-22 13:58:06 -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 | */ |
| 16 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
| 20 | #define LOG_TAG "InterfaceConfig" |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | #include "InterfaceConfig.h" |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 24 | #include "NetworkManager.h" |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 25 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 26 | InterfaceConfig::InterfaceConfig(bool propertiesReadOnly) { |
| 27 | mStaticProperties.propIp = new IPV4AddressPropertyHelper("Addr", propertiesReadOnly, &mIp); |
| 28 | mStaticProperties.propNetmask = new IPV4AddressPropertyHelper("Netmask", propertiesReadOnly, &mNetmask); |
| 29 | mStaticProperties.propGateway = new IPV4AddressPropertyHelper("Gateway", propertiesReadOnly, &mGateway); |
| 30 | mStaticProperties.propBroadcast = new IPV4AddressPropertyHelper("Broadcast", propertiesReadOnly, &mBroadcast); |
| 31 | mStaticProperties.propDns = new InterfaceDnsProperty(this, propertiesReadOnly); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | InterfaceConfig::~InterfaceConfig() { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 35 | delete mStaticProperties.propIp; |
| 36 | delete mStaticProperties.propNetmask; |
| 37 | delete mStaticProperties.propGateway; |
| 38 | delete mStaticProperties.propBroadcast; |
| 39 | delete mStaticProperties.propDns; |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 40 | } |
| 41 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 42 | void InterfaceConfig::setIp(struct in_addr *addr) { |
| 43 | memcpy(&mIp, addr, sizeof(struct in_addr)); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 46 | void InterfaceConfig::setNetmask(struct in_addr *addr) { |
| 47 | memcpy(&mNetmask, addr, sizeof(struct in_addr)); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 48 | } |
| 49 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 50 | void InterfaceConfig::setGateway(struct in_addr *addr) { |
| 51 | memcpy(&mGateway, addr, sizeof(struct in_addr)); |
| 52 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 53 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 54 | void InterfaceConfig::setBroadcast(struct in_addr *addr) { |
| 55 | memcpy(&mBroadcast, addr, sizeof(struct in_addr)); |
| 56 | } |
| 57 | |
| 58 | void InterfaceConfig::setDns(int idx, struct in_addr *addr) { |
| 59 | memcpy(&mDns[idx], addr, sizeof(struct in_addr)); |
| 60 | } |
| 61 | |
| 62 | int InterfaceConfig::attachProperties(PropertyManager *pm, const char *nsName) { |
| 63 | pm->attachProperty(nsName, mStaticProperties.propIp); |
| 64 | pm->attachProperty(nsName, mStaticProperties.propNetmask); |
| 65 | pm->attachProperty(nsName, mStaticProperties.propGateway); |
| 66 | pm->attachProperty(nsName, mStaticProperties.propBroadcast); |
| 67 | pm->attachProperty(nsName, mStaticProperties.propDns); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 71 | int InterfaceConfig::detachProperties(PropertyManager *pm, const char *nsName) { |
| 72 | pm->detachProperty(nsName, mStaticProperties.propIp); |
| 73 | pm->detachProperty(nsName, mStaticProperties.propNetmask); |
| 74 | pm->detachProperty(nsName, mStaticProperties.propGateway); |
| 75 | pm->detachProperty(nsName, mStaticProperties.propBroadcast); |
| 76 | pm->detachProperty(nsName, mStaticProperties.propDns); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 80 | InterfaceConfig::InterfaceDnsProperty::InterfaceDnsProperty(InterfaceConfig *c, |
| 81 | bool ro) : |
| 82 | IPV4AddressProperty("Dns", ro, 2) { |
| 83 | mCfg = c; |
| 84 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 85 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 86 | int InterfaceConfig::InterfaceDnsProperty::set(int idx, struct in_addr *value) { |
| 87 | memcpy(&mCfg->mDns[idx], value, sizeof(struct in_addr)); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 88 | return 0; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 89 | } |
| 90 | int InterfaceConfig::InterfaceDnsProperty::get(int idx, struct in_addr *buf) { |
| 91 | memcpy(buf, &mCfg->mDns[idx], sizeof(struct in_addr)); |
| 92 | return 0; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 93 | } |
| 94 | |