Fix null dereferencing and unchecked return value
[platform/core/api/connection.git] / src / libnetwork_mptcp.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
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
17 #include <glib.h>
18
19 #include "net_connection_private.h"
20 #include "connection_extension.h"
21 #include "net_connection_mptcp_private.h"
22 #include "network-mptcp-intf.h"
23
24 //LCOV_EXCL_START
25 static const char* __convert_mptcp_path_manager_enum_to_str(connection_mptcp_path_manager_e pm)
26 {
27         switch (pm) {
28         case CONNECTION_MPTCP_PM_UNKNOWN:
29                 return NULL;
30         case CONNECTION_MPTCP_PM_DEFAULT:
31                 return "default";
32         case CONNECTION_MPTCP_PM_FULLMESH:
33                 return "fullmesh";
34         }
35         return NULL;
36 }
37
38 static connection_mptcp_path_manager_e __convert_mptcp_path_manager_str_to_enum(char* pm)
39 {
40         if (g_strcmp0(pm, "default") == 0)
41                 return CONNECTION_MPTCP_PM_DEFAULT;
42         if (g_strcmp0(pm, "fullmesh") == 0)
43                 return CONNECTION_MPTCP_PM_FULLMESH;
44         return CONNECTION_MPTCP_PM_UNKNOWN;     //LCOV_EXCL_LINE
45 }
46
47 static const char* __convert_mptcp_scheduler_enum_to_str(connection_mptcp_scheduler_e scheduler)
48 {
49         switch (scheduler) {
50         case CONNECTION_MPTCP_SCHEDULER_UNKNOWN:
51                 return NULL;
52         case CONNECTION_MPTCP_SCHEDULER_DEFAULT:
53                 return "default";
54         case CONNECTION_MPTCP_SCHEDULER_ROUNDROBIN:
55                 return "roundrobin";
56         }
57         return NULL;
58 }
59
60 static connection_mptcp_scheduler_e __convert_mptcp_scheduler_str_to_enum(char *scheduler)
61 {
62         if (g_strcmp0(scheduler, "default") == 0)
63                 return CONNECTION_MPTCP_SCHEDULER_DEFAULT;
64         if (g_strcmp0(scheduler, "roundrobin") == 0)
65                 return CONNECTION_MPTCP_SCHEDULER_ROUNDROBIN;
66         return CONNECTION_MPTCP_SCHEDULER_UNKNOWN;      //LCOV_EXCL_LINE
67 }
68
69 gboolean _connection_libnet_mptcp_supported(connection_handle_s *conn_handle)
70 {
71         int rv = 0;
72         gboolean support = false;
73
74         rv = net_mptcp_supported(conn_handle->network_info_handle, &support);
75         if (rv == NET_ERR_ACCESS_DENIED) {
76                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
77                 return false;
78         } else if (rv != NET_ERR_NONE) {
79                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable MPTCP [%d]", rv);    //LCOV_EXCL_LINE
80                 return false;
81         }
82
83         return support;
84 }
85
86 int _connection_libnet_mptcp_enable(connection_handle_s *conn_handle, connection_mptcp_enable_e enable)
87 {
88         int rv = 0;
89
90         rv = net_mptcp_set_enabled(conn_handle->network_info_handle, (int)enable);
91         if (rv == NET_ERR_ACCESS_DENIED) {
92                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
93                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
94         } else if (rv != NET_ERR_NONE) {
95                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable MPTCP [%d]", rv);    //LCOV_EXCL_LINE
96                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
97         }
98
99         return CONNECTION_ERROR_NONE;
100 }
101
102 int _connection_libnet_mptcp_get_enabled(connection_handle_s *conn_handle, connection_mptcp_enable_e* enable)
103 {
104         int rv = 0;
105         int result = 0;
106
107         rv = net_mptcp_get_enabled(conn_handle->network_info_handle, &result);
108         if (rv == NET_ERR_ACCESS_DENIED) {
109                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
110                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
111         } else if (rv != NET_ERR_NONE) {
112                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP enabled state [%d]", rv); //LCOV_EXCL_LINE
113                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
114         }
115
116         *enable = (connection_mptcp_enable_e)result;
117         return CONNECTION_ERROR_NONE;
118 }
119
120
121 int _connection_libnet_mptcp_set_path_manager(connection_handle_s *conn_handle, connection_mptcp_path_manager_e pm)
122 {
123         int rv = 0;
124         const char *str = __convert_mptcp_path_manager_enum_to_str(pm);
125
126         if (str == NULL) {
127                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid Parameter");
128                 return CONNECTION_ERROR_INVALID_PARAMETER;
129         }
130
131         CONNECTION_LOG(CONNECTION_ERROR, "set path manager %s", str);
132
133         rv = net_mptcp_set_path_manager(conn_handle->network_info_handle, str);
134         if (rv == NET_ERR_ACCESS_DENIED) {
135                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
136                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
137         } else if (rv != NET_ERR_NONE) {
138                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set MPTCP path manager [%d]", rv);  //LCOV_EXCL_LINE
139                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
140         }
141
142         return CONNECTION_ERROR_NONE;
143 }
144
145 int _connection_libnet_mptcp_get_path_manager(connection_handle_s *conn_handle, connection_mptcp_path_manager_e* pm)
146 {
147         int rv = 0;
148         char* result = NULL;
149
150         rv = net_mptcp_get_path_manager(conn_handle->network_info_handle, &result);
151         if (rv == NET_ERR_ACCESS_DENIED) {
152                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
153                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
154         } else if (rv != NET_ERR_NONE) {
155                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP path manager [%d]", rv);  //LCOV_EXCL_LINE
156                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
157         }
158
159         CONNECTION_LOG(CONNECTION_ERROR, "get path manager %s", result);
160         *pm = __convert_mptcp_path_manager_str_to_enum(result);
161         g_free(result);
162         return CONNECTION_ERROR_NONE;
163 }
164
165
166 int _connection_libnet_mptcp_set_scheduler(connection_handle_s *conn_handle, connection_mptcp_scheduler_e scheduler)
167 {
168         int rv = 0;
169         const char *str = __convert_mptcp_scheduler_enum_to_str(scheduler);
170
171         if (str == NULL) {
172                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid Parameter");
173                 return CONNECTION_ERROR_INVALID_PARAMETER;
174         }
175
176         CONNECTION_LOG(CONNECTION_ERROR, "set scheduler %s", str);
177
178         rv = net_mptcp_set_scheduler(conn_handle->network_info_handle, str);
179         if (rv == NET_ERR_ACCESS_DENIED) {
180                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
181                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
182         } else if (rv != NET_ERR_NONE) {
183                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set MPTCP scheduler [%d]", rv);     //LCOV_EXCL_LINE
184                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
185         }
186
187         return CONNECTION_ERROR_NONE;
188 }
189
190 int _connection_libnet_mptcp_get_scheduler(connection_handle_s *conn_handle, connection_mptcp_scheduler_e* scheduler)
191 {
192         int rv = 0;
193         char* result = NULL;
194
195         rv = net_mptcp_get_scheduler(conn_handle->network_info_handle, &result);
196         if (rv == NET_ERR_ACCESS_DENIED) {
197                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");      //LCOV_EXCL_LINE
198                 return CONNECTION_ERROR_PERMISSION_DENIED;      //LCOV_EXCL_LINE
199         } else if (rv != NET_ERR_NONE) {
200                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP scheduler [%d]", rv);     //LCOV_EXCL_LINE
201                 return CONNECTION_ERROR_OPERATION_FAILED;       //LCOV_EXCL_LINE
202         }
203
204         CONNECTION_LOG(CONNECTION_ERROR, "get scheduler %s", result);
205         *scheduler = __convert_mptcp_scheduler_str_to_enum(result);
206         g_free(result);
207         return CONNECTION_ERROR_NONE;
208 }
209 //LCOV_EXCL_STOP