blob: 66861d02a0a4a0570aa47877f2e6a2cc8dd51e53 [file] [log] [blame]
San Mehat192331d2009-05-22 13:58:06 -07001/*
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 Mehat3c5a6f02009-05-22 15:36:13 -070017#include <stdlib.h>
San Mehat192331d2009-05-22 13:58:06 -070018#include <string.h>
19
20#define LOG_TAG "InterfaceConfig"
21#include <cutils/log.h>
22
23#include "InterfaceConfig.h"
San Mehat3c5a6f02009-05-22 15:36:13 -070024#include "NetworkManager.h"
San Mehat192331d2009-05-22 13:58:06 -070025
San Mehat3c5a6f02009-05-22 15:36:13 -070026const char *InterfaceConfig::PropertyNames[] = { "dhcp", "ip",
27 "netmask",
28 "gateway", "dns1", "dns2",
29 "dns3", '\0' };
30
31InterfaceConfig::InterfaceConfig(const char *prop_prefix) {
32 mPropPrefix = strdup(prop_prefix);
San Mehat192331d2009-05-22 13:58:06 -070033 mUseDhcp = true;
San Mehat3c5a6f02009-05-22 15:36:13 -070034 registerProperties();
San Mehat192331d2009-05-22 13:58:06 -070035}
36
37InterfaceConfig::~InterfaceConfig() {
San Mehat3c5a6f02009-05-22 15:36:13 -070038 unregisterProperties();
39 free(mPropPrefix);
San Mehat192331d2009-05-22 13:58:06 -070040}
41
San Mehat3c5a6f02009-05-22 15:36:13 -070042InterfaceConfig::InterfaceConfig(const char *prop_prefix,
43 const char *ip, const char *nm,
San Mehat192331d2009-05-22 13:58:06 -070044 const char *gw, const char *dns1, const char *dns2,
45 const char *dns3) {
San Mehat3c5a6f02009-05-22 15:36:13 -070046 mPropPrefix = strdup(prop_prefix);
San Mehat192331d2009-05-22 13:58:06 -070047 mUseDhcp = false;
48
49 if (!inet_aton(ip, &mIp))
50 LOGW("Unable to parse ip (%s)", ip);
San Mehat3c5a6f02009-05-22 15:36:13 -070051 if (!inet_aton(nm, &mNetmask))
San Mehat192331d2009-05-22 13:58:06 -070052 LOGW("Unable to parse netmask (%s)", nm);
San Mehat3c5a6f02009-05-22 15:36:13 -070053 if (!inet_aton(gw, &mGateway))
San Mehat192331d2009-05-22 13:58:06 -070054 LOGW("Unable to parse gateway (%s)", gw);
San Mehat3c5a6f02009-05-22 15:36:13 -070055 if (!inet_aton(dns1, &mDns1))
San Mehat192331d2009-05-22 13:58:06 -070056 LOGW("Unable to parse dns1 (%s)", dns1);
San Mehat3c5a6f02009-05-22 15:36:13 -070057 if (!inet_aton(dns2, &mDns2))
San Mehat192331d2009-05-22 13:58:06 -070058 LOGW("Unable to parse dns2 (%s)", dns2);
San Mehat3c5a6f02009-05-22 15:36:13 -070059 if (!inet_aton(dns3, &mDns3))
San Mehat192331d2009-05-22 13:58:06 -070060 LOGW("Unable to parse dns3 (%s)", dns3);
San Mehat3c5a6f02009-05-22 15:36:13 -070061 registerProperties();
San Mehat192331d2009-05-22 13:58:06 -070062}
63
San Mehat3c5a6f02009-05-22 15:36:13 -070064InterfaceConfig::InterfaceConfig(const char *prop_prefix,
65 const struct in_addr *ip,
San Mehat192331d2009-05-22 13:58:06 -070066 const struct in_addr *nm, const struct in_addr *gw,
67 const struct in_addr *dns1, const struct in_addr *dns2,
68 const struct in_addr *dns3) {
San Mehat3c5a6f02009-05-22 15:36:13 -070069 mPropPrefix = strdup(prop_prefix);
San Mehat192331d2009-05-22 13:58:06 -070070 mUseDhcp = false;
71
72 memcpy(&mIp, ip, sizeof(struct in_addr));
73 memcpy(&mNetmask, nm, sizeof(struct in_addr));
74 memcpy(&mGateway, gw, sizeof(struct in_addr));
75 memcpy(&mDns1, dns1, sizeof(struct in_addr));
76 memcpy(&mDns2, dns2, sizeof(struct in_addr));
77 memcpy(&mDns3, dns3, sizeof(struct in_addr));
San Mehat3c5a6f02009-05-22 15:36:13 -070078 registerProperties();
San Mehat192331d2009-05-22 13:58:06 -070079}
80
San Mehat3c5a6f02009-05-22 15:36:13 -070081int InterfaceConfig::registerProperties() {
82 for (const char **p = InterfaceConfig::PropertyNames; *p != '\0'; p++) {
83 char *tmp;
84 asprintf(&tmp, "%s.if.%s", mPropPrefix, *p);
85
86 if (NetworkManager::Instance()->getPropMngr()->registerProperty(tmp,
87 this)) {
88 free(tmp);
89 return -1;
90 }
91 free(tmp);
92 }
93 return 0;
94}
95
96int InterfaceConfig::unregisterProperties() {
97 for (const char **p = InterfaceConfig::PropertyNames; *p != '\0'; p++) {
98 char *tmp;
99 asprintf(&tmp, "%s.if.%s", mPropPrefix, *p);
100
101 if (NetworkManager::Instance()->getPropMngr()->unregisterProperty(tmp))
102 LOGW("Unable to remove property '%s' (%s)", tmp, strerror(errno));
103 free(tmp);
104 }
105 return 0;
106}
107
108int InterfaceConfig::set(const char *name, const char *value) {
109 const char *n;
110
111 for (n = &name[strlen(name)]; *n != '.'; n--);
112 n++;
113
114 if (!strcasecmp(n, "name")) {
115 errno = EROFS;
116 return -1;
117 } else if (!strcasecmp(n, "ip") && !inet_aton(value, &mIp))
118 goto out_inval;
119 else if (!strcasecmp(n, "dhcp"))
120 mUseDhcp = (atoi(value) == 0 ? false : true);
121 else if (!strcasecmp(n, "netmask") && !inet_aton(value, &mNetmask))
122 goto out_inval;
123 else if (!strcasecmp(n, "gateway") && !inet_aton(value, &mGateway))
124 goto out_inval;
125 else if (!strcasecmp(n, "dns1") && !inet_aton(value, &mDns1))
126 goto out_inval;
127 else if (!strcasecmp(n, "dns2") && !inet_aton(value, &mDns2))
128 goto out_inval;
129 else if (!strcasecmp(n, "dns3") && !inet_aton(value, &mDns3))
130 goto out_inval;
131 else {
132 errno = ENOENT;
133 return -1;
134 }
135
136 return 0;
137
138out_inval:
139 errno = EINVAL;
140 return -1;
141}
142
143const char *InterfaceConfig::get(const char *name, char *buffer, size_t max) {
144 const char *n;
145
146 for (n = &name[strlen(name)]; *n != '.'; n--);
147 n++;
148
149 if (!strcasecmp(n, "ip"))
150 strncpy(buffer, inet_ntoa(mIp), max);
151 else if (!strcasecmp(n, "dhcp"))
152 snprintf(buffer, max, "%d", mUseDhcp);
153 else if (!strcasecmp(n, "netmask"))
154 strncpy(buffer, inet_ntoa(mNetmask), max);
155 else if (!strcasecmp(n, "gateway"))
156 strncpy(buffer, inet_ntoa(mGateway), max);
157 else if (!strcasecmp(n, "dns1"))
158 strncpy(buffer, inet_ntoa(mDns1), max);
159 else if (!strcasecmp(n, "dns2"))
160 strncpy(buffer, inet_ntoa(mDns2), max);
161 else if (!strcasecmp(n, "dns3"))
162 strncpy(buffer, inet_ntoa(mDns3), max);
163 else {
164 strncpy(buffer, "(internal error)", max);
165 errno = ENOENT;
166 return NULL;
167 }
168 return buffer;
169}