ff197bc751d55a26cdd142a0c7cefe5e90f662df
[platform/core/connectivity/net-config.git] / src / mptcp-internal.c
1 #include <glib.h>
2 #include <sys/sysctl.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "mptcp-internal.h"
10 #include "log.h"
11
12 #define NET_PROC_SYS_NET_MPTCP  "/proc/sys/net/mptcp/"
13 #define NET_PROC_SYS_NET_MPTCP_VALUE_MAX_SIZE   15
14
15 static gboolean __write_mptcp_parameter(const char* key, const char* value)
16 {
17         char path[50];
18         int fd = -1;
19
20         snprintf(path, sizeof(path), NET_PROC_SYS_NET_MPTCP"%s", key);
21
22         fd = open(path, O_RDWR);
23         if (fd < 0) {
24                 ERR("Fail to open %s", path);
25                 return FALSE;
26         }
27
28         DBG("write mptcp parameter. len: %zd, value: %s", strlen(value), value);
29         if (write(fd, value, strlen(value)) < 0) {
30                 ERR("Fail to write %s to %s", value, path);
31                 close(fd);
32                 return FALSE;
33         }
34
35         close(fd);
36         return TRUE;
37 }
38
39 static char* __read_mptcp_parameter(const char* key)
40 {
41         char path[50];
42         int fd = -1;
43         int len = -1;
44         char buf[NET_PROC_SYS_NET_MPTCP_VALUE_MAX_SIZE];
45
46         snprintf(path, sizeof(path), NET_PROC_SYS_NET_MPTCP"%s", key);
47
48         fd = open(path, O_RDWR);
49         if (fd < 0) {
50                 ERR("Fail to open %s", path);
51                 return NULL;
52         }
53
54         if ((len = read(fd, buf, NET_PROC_SYS_NET_MPTCP_VALUE_MAX_SIZE)) < 0) {
55                 ERR("Fail to read %s", path);
56                 close(fd);
57                 return NULL;
58         }
59
60         if (len < 1) {
61                 ERR("Fail to read");
62                 close(fd);
63                 return NULL;
64         }
65
66         buf[len - 1] = '\0';
67
68         DBG("read mptcp parameter. len: %d, value: %s", len, buf);
69         close(fd);
70         return g_strndup(buf, strlen(buf));
71 }
72
73 gboolean mptcp_supported()
74 {
75         return g_file_test(NET_PROC_SYS_NET_MPTCP, G_FILE_TEST_EXISTS);
76 }
77
78 gboolean mptcp_set_enabled(gint enable)
79 {
80         const char* sysctlname = "mptcp_enabled";
81         char buf[2];
82         if (enable != 0 && enable != 1 && enable != 2) {
83                 ERR("Invalid parameter enable %d", enable);
84                 return FALSE;
85         }
86
87         snprintf(buf, 2, "%d", enable);
88         return __write_mptcp_parameter(sysctlname, buf);
89 }
90
91 gint mptcp_get_enabled()
92 {
93         const char* sysctlname = "mptcp_enabled";
94         char* buf;
95         int enabled = -1;
96
97         if ((buf = __read_mptcp_parameter(sysctlname)) == NULL)  {
98                 ERR("Fail to read %s", sysctlname);
99                 return -1;
100         }
101
102         enabled = atoi(buf);
103         g_free(buf);
104
105         return enabled;
106 }
107
108 gboolean mptcp_set_path_manager(const gchar* pm)
109 {
110         const char* sysctlname = "mptcp_path_manager";
111         return __write_mptcp_parameter(sysctlname, pm);
112 }
113
114 gchar* mptcp_get_path_manager()
115 {
116         const char* sysctlname = "mptcp_path_manager";
117         return __read_mptcp_parameter(sysctlname);
118 }
119
120 gboolean mptcp_mptcp_set_scheduler(const gchar* scheduler)
121 {
122         const char* sysctlname = "mptcp_scheduler";
123         return __write_mptcp_parameter(sysctlname, scheduler);
124 }
125
126 gchar* mptcp_get_scheduler()
127 {
128         const char* sysctlname = "mptcp_scheduler";
129         return __read_mptcp_parameter(sysctlname);
130 }
131