2 * Network Configuration Module
4 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
36 #include "mesh-util.h"
37 #include "mesh-service.h"
38 #include "mesh-interface.h"
40 #define IPV4_MAX_LENGTH 16
41 #define BUF_LENGTH 256
43 #define MESH_DEFAULT_BASE_INTERFACE "wlan0"
44 #define MESH_DEFAULT_MESH_INTERFACE "mesh0"
45 #define MESH_DEFAULT_BRIDGE_INTERFACE "br0"
46 #define MESH_DEFAULT_EXTERNAL_INTERFACE "eth0"
47 #define MESH_DEFAULT_SOFTAP_INTERFACE "wlan1"
50 ETHERNET_CABLE_DETACHED = 0,
51 ETHERNET_CABLE_ATTACHED
54 int mesh_interface_set(const char *interface, const char* ip_addr,
55 mesh_set_interface_type_e type)
61 char ip[IPV4_MAX_LENGTH] = {0,};
62 char buf[BUF_LENGTH] = {0,};
64 if (interface == NULL) {
65 MESH_LOGE("Invalid interface name !");
66 return MESHD_ERROR_INVALID_PARAMETER;
69 sock = socket(AF_INET, SOCK_STREAM, 0);
71 MESH_LOGE("Cannot open network interface socket");
72 return MESHD_ERROR_IO_ERROR;
75 MESH_LOGD("Initialize interface [%s]...", interface);
76 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
79 if (NULL != ip_addr) {
80 struct sockaddr_in sai;
81 memset(&sai, 0, sizeof(struct sockaddr_in));
82 sai.sin_family = AF_INET;
84 snprintf(ip, IPV4_MAX_LENGTH, "%s", ip_addr);
86 MESH_LOGD("Setting IP address: [%s]\n", ip);
87 if (!inet_aton(ip, &sai.sin_addr)) {
88 MESH_LOGE("Failed to convert ip address");
90 return MESHD_ERROR_OPERATION_FAILED;
93 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
95 ret = ioctl(sock, SIOCSIFADDR, &ifr);
97 (void) strerror_r(errno, buf, BUF_LENGTH);
98 MESH_LOGE("Failed to set IP[%s] for interface[%s] : %s",
99 ip, ifr.ifr_name, buf);
103 /* Get status flag */
104 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
105 (void) strerror_r(errno, buf, BUF_LENGTH);
106 MESH_LOGE("Failed to get interface[%s] status : %s",
109 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
111 /* Set status flag */
112 if (MESH_INTERFACE_UP == type) {
113 ifr.ifr_flags |= (IFF_UP);
114 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
115 (void) strerror_r(errno, buf, BUF_LENGTH);
116 MESH_LOGE("Failed to change interface[%s] status UP : %s",
119 } else if (MESH_INTERFACE_DOWN == type) {
120 ifr.ifr_flags &= (~IFF_UP);
121 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
122 (void) strerror_r(errno, buf, BUF_LENGTH);
123 MESH_LOGE("Failed to change interface[%s] status DOWN : %s",
127 MESH_LOGD("Do not change up/down status");
131 return MESHD_ERROR_NONE;
135 /* Returns interface name in sequence order which is exists */
136 static char* _get_interface_exists_in_seq(const char* prefix)
142 const int IF_INDEX_MAX = 9;
144 for (i = 0; i <= IF_INDEX_MAX; i++) {
145 snprintf(buf, sizeof(buf), "/sys/class/net/%s%d", prefix, i);
147 ret = access(buf, F_OK);
149 /* This name is exists. use it */
150 snprintf(buf, sizeof(buf), "%s%d", prefix, i);
152 MESH_LOGD(" use [%s]", res);
160 /* Returns interface name in sequence order which is exists */
161 static char* _get_interface_not_exists_in_seq(const char* prefix)
167 const int IF_INDEX_MAX = 9;
169 for (i = 0; i <= IF_INDEX_MAX; i++) {
170 snprintf(buf, sizeof(buf), "/sys/class/net/%s%d", prefix, i);
172 ret = access(buf, F_OK);
174 /* This name is not exists. use it */
175 snprintf(buf, sizeof(buf), "%s%d", prefix, i);
177 MESH_LOGD(" use [%s]", res);
186 /* Returns interface name in sequence order which is exists */
187 static bool _check_interface_exists(const char* if_name)
192 const int IF_INDEX_MAX = 9;
194 for (i = 0; i <= IF_INDEX_MAX; i++) {
195 snprintf(buf, sizeof(buf), "/sys/class/net/%s", if_name);
197 ret = access(buf, F_OK);
199 /* This name is exists. */
207 char* mesh_interface_get_address(const char* if_name)
210 char buf[32] = { 0, };
213 snprintf(buf, sizeof(buf), "/sys/class/net/%s/address", if_name);
214 pf = fopen(buf, "r");
217 MESH_LOGD("Interface[%s] address[%s]", if_name, buf);
220 result = g_strdup(buf);
228 int mesh_interface_initialize(mesh_interface_s *info)
231 MESH_LOGE("Invalid parameter");
232 return MESHD_ERROR_INVALID_PARAMETER;
235 info->bridge_interface = g_strdup(MESH_DEFAULT_BRIDGE_INTERFACE);
236 if (NULL == info->bridge_interface) {
237 MESH_LOGE("Failed to get bridge interface !");
238 return MESHD_ERROR_OPERATION_FAILED;
241 info->base_interface = g_strdup(MESH_DEFAULT_BASE_INTERFACE);
242 if (NULL == info->bridge_interface) {
243 MESH_LOGE("Failed to get base interface !");
244 return MESHD_ERROR_OPERATION_FAILED;
247 info->mesh_interface = g_strdup(MESH_DEFAULT_MESH_INTERFACE);
248 if (NULL == info->bridge_interface) {
249 MESH_LOGE("Failed to get mesh interface !");
250 return MESHD_ERROR_OPERATION_FAILED;
253 info->softap_interface = g_strdup(MESH_DEFAULT_SOFTAP_INTERFACE);
254 info->external_interface = g_strdup(MESH_DEFAULT_EXTERNAL_INTERFACE);
256 MESH_LOGD("Interface configuration for mesh network :");
257 MESH_LOGD(" Base : [%s]", info->base_interface);
258 MESH_LOGD(" Mesh : [%s]", info->mesh_interface);
259 MESH_LOGD(" Bridge : [%s]", info->bridge_interface);
260 MESH_LOGD(" SoftAP : [%s]", info->softap_interface);
261 MESH_LOGD(" External: [%s]", info->external_interface);
263 return MESHD_ERROR_NONE;
266 static int _check_ethernet_cable_plugin_status(const char* interface,
267 cable_state_e *status)
272 char error_buf[256] = {0, };
273 char file_path[256] = {0, };
275 snprintf(file_path, 256, "/sys/class/net/%s/carrier", interface);
277 if (0 == access(file_path, F_OK)) {
278 fd = fopen(file_path, "r");
280 MESH_LOGE("Error! Could not open /sys/class/net/%s/carrier file",
282 return MESHD_ERROR_IO_ERROR;
285 MESH_LOGE("Error! Could not access /sys/class/net/%s/carrier file",
287 return MESHD_ERROR_IO_ERROR;
291 rv = fscanf(fd, "%d", &ret);
293 strerror_r(errno, error_buf, 256);
294 MESH_LOGE("Error! Failed to read from file, rv:[%d], error:[%s]",
297 return MESHD_ERROR_IO_ERROR;
301 MESH_LOGD("/sys/class/net/%s/carrier : [%d]", interface, ret);
302 *status = ETHERNET_CABLE_ATTACHED;
303 } else if (ret == 0) {
304 MESH_LOGD("/sys/class/net/%s/carrier : [%d]", interface, ret);
305 *status = ETHERNET_CABLE_DETACHED;
309 return MESHD_ERROR_NONE;
312 int mesh_interface_check_external_exists(const char* external_interface, bool *state)
314 /* TODO: Current logic checks only ethernet interface.
315 This logic should consider wireless interface if can */
316 int ret = MESHD_ERROR_NONE;
317 cable_state_e cable_state = ETHERNET_CABLE_DETACHED;
319 if (NULL == external_interface || NULL == state) {
320 return MESHD_ERROR_INVALID_PARAMETER;
323 bool ex = _check_interface_exists(external_interface);
325 MESH_LOGE("External interface[%s] was not found.", external_interface);
326 return MESHD_ERROR_INVALID_PARAMETER;
329 /* If external interface seems Ethernet, check cable state */
330 if (g_str_has_prefix(external_interface, "eth")) {
331 ret = _check_ethernet_cable_plugin_status(external_interface, &cable_state);
332 if (MESHD_ERROR_NONE != ret) {
333 MESH_LOGE("Failed to get Ethernet cable state");
334 return MESHD_ERROR_OPERATION_FAILED;
339 if (ETHERNET_CABLE_ATTACHED == cable_state) {
343 return MESHD_ERROR_NONE;