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 <stdlib.h>
18#include <string.h>
19
20#define LOG_TAG "InterfaceConfig"
21#include <cutils/log.h>
22
23#include "InterfaceConfig.h"
24#include "NetworkManager.h"
25
26InterfaceConfig::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);
32}
33
34InterfaceConfig::~InterfaceConfig() {
35    delete mStaticProperties.propIp;
36    delete mStaticProperties.propNetmask;
37    delete mStaticProperties.propGateway;
38    delete mStaticProperties.propBroadcast;
39    delete mStaticProperties.propDns;
40}
41
42void InterfaceConfig::setIp(struct in_addr *addr) {
43    memcpy(&mIp, addr, sizeof(struct in_addr));
44}
45
46void InterfaceConfig::setNetmask(struct in_addr *addr) {
47    memcpy(&mNetmask, addr, sizeof(struct in_addr));
48}
49
50void InterfaceConfig::setGateway(struct in_addr *addr) {
51    memcpy(&mGateway, addr, sizeof(struct in_addr));
52}
53
54void InterfaceConfig::setBroadcast(struct in_addr *addr) {
55    memcpy(&mBroadcast, addr, sizeof(struct in_addr));
56}
57
58void InterfaceConfig::setDns(int idx, struct in_addr *addr) {
59    memcpy(&mDns[idx], addr, sizeof(struct in_addr));
60}
61
62int 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);
68    return 0;
69}
70
71int 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);
77    return 0;
78}
79
80InterfaceConfig::InterfaceDnsProperty::InterfaceDnsProperty(InterfaceConfig *c,
81                                                            bool ro) :
82                 IPV4AddressProperty("Dns", ro, 2) {
83    mCfg = c;
84}
85
86int InterfaceConfig::InterfaceDnsProperty::set(int idx, struct in_addr *value) {
87    memcpy(&mCfg->mDns[idx], value, sizeof(struct in_addr));
88    return 0;
89}
90int InterfaceConfig::InterfaceDnsProperty::get(int idx, struct in_addr *buf) {
91    memcpy(buf, &mCfg->mDns[idx], sizeof(struct in_addr));
92    return 0;
93}
94
95