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 int mesh_interface_set(const char *interface, const char* ip_addr,
44 mesh_set_interface_type_e type)
50 char ip[IPV4_MAX_LENGTH] = {0,};
51 char buf[BUF_LENGTH] = {0,};
53 if (interface == NULL) {
54 MESH_LOGE("Invalid interface name !");
55 return MESHD_ERROR_INVALID_PARAMETER;
58 sock = socket(AF_INET, SOCK_STREAM, 0);
60 MESH_LOGE("Cannot open network interface socket");
61 return MESHD_ERROR_IO_ERROR;
64 MESH_LOGD("Initialize interface [%s]...", interface);
65 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
68 if (NULL != ip_addr) {
69 struct sockaddr_in sai;
70 memset(&sai, 0, sizeof(struct sockaddr_in));
71 sai.sin_family = AF_INET;
73 snprintf(ip, IPV4_MAX_LENGTH, "%s", ip_addr);
75 MESH_LOGD("Setting IP address: [%s]\n", ip);
76 if (!inet_aton(ip, &sai.sin_addr)) {
77 MESH_LOGE("Failed to convert ip address");
79 return MESHD_ERROR_OPERATION_FAILED;
82 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
84 ret = ioctl(sock, SIOCSIFADDR, &ifr);
86 (void) strerror_r(errno, buf, BUF_LENGTH);
87 MESH_LOGE("Failed to set IP[%s] for interface[%s] : %s",
88 ip, ifr.ifr_name, buf);
93 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
94 (void) strerror_r(errno, buf, BUF_LENGTH);
95 MESH_LOGE("Failed to get interface[%s] status : %s",
98 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
100 /* Set status flag */
101 if (MESH_INTERFACE_UP == type) {
102 ifr.ifr_flags |= (IFF_UP);
103 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
104 (void) strerror_r(errno, buf, BUF_LENGTH);
105 MESH_LOGE("Failed to change interface[%s] status UP : %s",
108 } else if (MESH_INTERFACE_DOWN == type) {
109 ifr.ifr_flags &= (~IFF_UP);
110 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
111 (void) strerror_r(errno, buf, BUF_LENGTH);
112 MESH_LOGE("Failed to change interface[%s] status DOWN : %s",
116 MESH_LOGD("Do not change up/down status");
120 return MESHD_ERROR_NONE;
123 /* Returns interface name in sequence order which is exists */
124 static char* _get_interface_exists_in_seq(const char* prefix)
130 const int IF_INDEX_MAX = 9;
132 for (i = 0; i <= IF_INDEX_MAX; i++) {
133 snprintf(buf, sizeof(buf), "/sys/class/net/%s%d", prefix, i);
135 ret = access(buf, F_OK);
137 /* This name is exists. use it */
138 snprintf(buf, sizeof(buf), "%s%d", prefix, i);
140 MESH_LOGD(" use [%s]", res);
148 /* Returns interface name in sequence order which is exists */
149 static char* _get_interface_not_exists_in_seq(const char* prefix)
155 const int IF_INDEX_MAX = 9;
157 for (i = 0; i <= IF_INDEX_MAX; i++) {
158 snprintf(buf, sizeof(buf), "/sys/class/net/%s%d", prefix, i);
160 ret = access(buf, F_OK);
162 /* This name is not exists. use it */
163 snprintf(buf, sizeof(buf), "%s%d", prefix, i);
165 MESH_LOGD(" use [%s]", res);
173 int mesh_interface_initialize(mesh_interface_s *info)
175 info->bridge_interface = _get_interface_not_exists_in_seq("br");
176 if (NULL == info->bridge_interface) {
177 MESH_LOGE("Failed to get bridge interface !");
178 return MESHD_ERROR_OPERATION_FAILED;
181 info->base_interface = _get_interface_exists_in_seq("wlan");
182 if (NULL == info->bridge_interface) {
183 MESH_LOGE("Failed to get base interface !");
184 return MESHD_ERROR_OPERATION_FAILED;
187 info->mesh_interface = _get_interface_not_exists_in_seq("mesh");
188 if (NULL == info->bridge_interface) {
189 MESH_LOGE("Failed to get mesh interface !");
190 return MESHD_ERROR_OPERATION_FAILED;
193 info->softap_interface = g_strdup("wlan1"); /* CHECK: interface changed */
194 info->external_interface = _get_interface_exists_in_seq("eth");
195 info->mesh_id = g_strdup("meshnet");
196 if (NULL == info->bridge_interface) {
197 MESH_LOGE("Failed to get mesh id !");
198 return MESHD_ERROR_OPERATION_FAILED;
200 info->mesh_channel = 7;
202 return MESHD_ERROR_NONE;