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 | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 26 | const char *InterfaceConfig::PropertyNames[] = { "dhcp", "ip", |
| 27 | "netmask", |
| 28 | "gateway", "dns1", "dns2", |
| 29 | "dns3", '\0' }; |
| 30 | |
| 31 | InterfaceConfig::InterfaceConfig(const char *prop_prefix) { |
| 32 | mPropPrefix = strdup(prop_prefix); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 33 | mUseDhcp = true; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 34 | registerProperties(); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | InterfaceConfig::~InterfaceConfig() { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 38 | unregisterProperties(); |
| 39 | free(mPropPrefix); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 40 | } |
| 41 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 42 | InterfaceConfig::InterfaceConfig(const char *prop_prefix, |
| 43 | const char *ip, const char *nm, |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 44 | const char *gw, const char *dns1, const char *dns2, |
| 45 | const char *dns3) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 46 | mPropPrefix = strdup(prop_prefix); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 47 | mUseDhcp = false; |
| 48 | |
| 49 | if (!inet_aton(ip, &mIp)) |
| 50 | LOGW("Unable to parse ip (%s)", ip); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 51 | if (!inet_aton(nm, &mNetmask)) |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 52 | LOGW("Unable to parse netmask (%s)", nm); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 53 | if (!inet_aton(gw, &mGateway)) |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 54 | LOGW("Unable to parse gateway (%s)", gw); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 55 | if (!inet_aton(dns1, &mDns1)) |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 56 | LOGW("Unable to parse dns1 (%s)", dns1); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 57 | if (!inet_aton(dns2, &mDns2)) |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 58 | LOGW("Unable to parse dns2 (%s)", dns2); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 59 | if (!inet_aton(dns3, &mDns3)) |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 60 | LOGW("Unable to parse dns3 (%s)", dns3); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 61 | registerProperties(); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 62 | } |
| 63 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 64 | InterfaceConfig::InterfaceConfig(const char *prop_prefix, |
| 65 | const struct in_addr *ip, |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 66 | 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 Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 69 | mPropPrefix = strdup(prop_prefix); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 70 | 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 Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 78 | registerProperties(); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 81 | int 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 | |
| 96 | int 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 | |
| 108 | int 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 | |
| 138 | out_inval: |
| 139 | errno = EINVAL; |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | const 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 | } |