Add Privacy-Guard
[platform/core/security/privacy-guard.git] / common / inc / SocketConnection.h
1 /*
2  * Copyright (c) 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 #ifndef _SOCKETCONNECTION_H_
18 #define _SOCKETCONNECTION_H_
19
20 #include <dlog.h>
21 #include <new>
22 #include <list>
23 #include <utility>
24 #include <iostream>
25 #include "Utils.h"
26 #include "SocketStream.h"
27 #include "PrivacyGuardTypes.h"
28
29 /*
30  * This class implements interface for generic read and write from given socket.
31  * It does not maintain socket descriptor, so any connecting and disconnecting should be
32  */
33
34 /*
35  * Throws SocketConnectionException when read/write will not succeed or if any bad allocation
36  * exception occurs during read.
37  */
38
39 class EXTERN_API SocketConnection
40 {
41
42 public:
43
44         explicit SocketConnection(int socket_fd) : m_socketStream(socket_fd){
45                 LOGI("Created");
46         }
47
48         template<typename T, typename ...Args>
49         int read(T* out, const Args&... args )
50         {
51                 int res = read(out);
52                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
53                 res = read(args...);
54                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
55
56                 return PRIV_FLTR_ERROR_SUCCESS;
57         }
58
59         template<typename T>
60         int read(T* out)
61         {
62                 int length = 0;
63                 int res = m_socketStream.readStream(sizeof(length), &length);
64                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
65                 char* pBuf = new (std::nothrow) char[length + 1];
66                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
67
68                 res = m_socketStream.readStream(length, pBuf);
69                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
70
71                 pBuf[length] = 0;
72
73                 out = T(pBuf);
74
75                 delete[] pBuf;
76
77                 return PRIV_FLTR_ERROR_SUCCESS;
78         }
79         
80         int read(bool* pB)
81         {
82                 int length = 0;
83                 int res = m_socketStream.readStream(sizeof(length), &length);
84                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
85
86                 char* pBuf = new (std::nothrow) char[length + 1];
87                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
88
89                 res = m_socketStream.readStream(length, pBuf);
90                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
91
92                 pBuf[length] = 0;
93
94                 *pB = * reinterpret_cast <bool* > (pBuf);
95
96                 delete[] pBuf;
97
98                 return PRIV_FLTR_ERROR_SUCCESS;
99         }
100
101         int read(bool& b)
102         {
103                 return read(&b);
104         }
105         
106         int read(int& i)
107         {
108                 return read(&i);
109         }
110
111         int read(int* pI)
112         {
113                 int length = 0;
114                 int res = m_socketStream.readStream(sizeof(length), &length);
115                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
116
117                 char* pBuf = new (std::nothrow) char[length + 1];
118                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
119
120                 res = m_socketStream.readStream(length, pBuf);
121                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
122
123                 pBuf[length] = 0;
124
125                 *pI = * reinterpret_cast <int* > (pBuf);
126
127                 delete[] pBuf;
128
129                 return PRIV_FLTR_ERROR_SUCCESS;
130         }
131
132         int read(unsigned int* pUi)
133         {
134                 int length = 0;
135                 int res = m_socketStream.readStream(sizeof(length), &length);
136                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
137
138                 char* pBuf = new (std::nothrow) char[length + 1];
139                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
140
141                 res = m_socketStream.readStream(length, pBuf);
142                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
143
144                 pBuf[length] = 0;
145
146                 *pUi = * reinterpret_cast <unsigned int* > (pBuf);
147
148                 delete[] pBuf;
149
150                 return PRIV_FLTR_ERROR_SUCCESS;
151         }
152         int read(std::string* pStr)
153         {
154                 int length = 0;
155                 int res = m_socketStream.readStream(sizeof(length), &length);
156                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
157
158                 char* pBuf = new (std::nothrow) char[length + 1];
159                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
160
161                 m_socketStream.readStream(length, pBuf);
162                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
163
164                 pBuf[length] = 0;
165
166                 *pStr = std::string(pBuf);
167                 delete[] pBuf;
168
169                 return PRIV_FLTR_ERROR_SUCCESS;
170         }
171         
172         int read(std::string& str)
173         {
174                 return read(&str);
175         }
176
177         int read(privacy_data_s& out)
178         {
179                 int length = 0;
180                 int res = 0;
181                 char* pBuf;
182                 // privacy id
183                 res = m_socketStream.readStream(sizeof(length), &length);
184                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
185                 pBuf = new (std::nothrow) char[length + 1];
186                 TryReturn(pBuf != NULL, PRIV_FLTR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_FLTR_ERROR_OUT_OF_MEMORY);
187                 m_socketStream.readStream(length, pBuf);
188                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
189                 pBuf[length] = 0;
190                 out.privacy_id = strdup(pBuf);
191                 delete[] pBuf;
192
193                 // monitor policy
194                 res = read(&(out.monitor_policy));
195                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "readStream : %d", res);
196
197                 return PRIV_FLTR_ERROR_SUCCESS;
198         }
199         template < typename T >
200         int  read (std::list<T>& list)
201         {
202                 int length = 0;
203                 int res = read(length);
204                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
205
206                 for (int i = 0; i < length; ++i) 
207                 {
208                         T obj;
209                         res = read (obj);
210                         TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
211                         list.push_back(obj);
212                 }
213
214                 return PRIV_FLTR_ERROR_SUCCESS;
215         }
216
217         template < typename T >
218         int read (std::list<T>* pList)
219         {
220                 return read(*pList);
221         }
222
223         template < typename K, typename P >
224         int read (std::pair<K, P>& pair)
225         {
226                 int res = read( pair.first);
227                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
228                 res = read( pair.second);
229                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "read : %d", res);
230
231                 return PRIV_FLTR_ERROR_SUCCESS;
232         }
233
234         template < typename K, typename P >
235         int read (std::pair<K, P>* pPair)
236         {
237                 return read( *pPair);
238         }
239
240         template<typename T, typename ...Args>
241         int write(const T& in, const Args&... args)
242         {
243                 int res = write(in);
244                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
245                 res = write(args...);
246                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
247
248                 return PRIV_FLTR_ERROR_SUCCESS;
249         }
250
251         int write(const std::string& in)
252         {
253                 int length = in.size();
254                 int res = m_socketStream.writeStream(sizeof(length), &length);
255                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
256                 res = m_socketStream.writeStream(length, in.c_str());
257                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
258
259                 return PRIV_FLTR_ERROR_SUCCESS;
260         }
261         
262         int write(const unsigned int& in)
263         {
264                 int length = sizeof(in);
265                 int res = m_socketStream.writeStream(sizeof(length), &length);
266                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
267                 res = m_socketStream.writeStream(length, &in);
268                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
269
270                 return PRIV_FLTR_ERROR_SUCCESS;
271         }
272
273         int write(const int& in)
274         {
275                 int length = sizeof(in);
276                 int res = m_socketStream.writeStream(sizeof(length), &length);
277                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
278                 res = m_socketStream.writeStream(length, &in);
279                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
280
281                 return PRIV_FLTR_ERROR_SUCCESS;
282         }
283
284         int write(const bool& in)
285         {
286                 int length = sizeof(in);
287                 int res = m_socketStream.writeStream(sizeof(length), &length);
288                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
289                 res = m_socketStream.writeStream(length, &in);
290                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
291
292                 return PRIV_FLTR_ERROR_SUCCESS;
293         }
294
295         int write(const char*& in)
296         {
297                 int length = strlen(in);
298                 int res = m_socketStream.writeStream(sizeof(length), &length);
299                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
300                 res = m_socketStream.writeStream(length, in);
301                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
302
303                 return PRIV_FLTR_ERROR_SUCCESS;
304         }
305
306         int write(const char* in_str, const int in_int1, const int in_int2)
307         {
308                 int res = 0;
309                 res = write(in_str);
310                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
311                 res = write(in_int1);
312                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
313                 res = write(in_int2);
314                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
315                 return PRIV_FLTR_ERROR_SUCCESS;
316         }
317
318         int write(const privacy_data_s& in)
319         {
320                 // privacy id
321                 int length = strlen(in.privacy_id);
322                 int res = 0;
323                 res = m_socketStream.writeStream(sizeof(length), &length);
324                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
325                 res = m_socketStream.writeStream(length, in.privacy_id);
326                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "writeStream : %d", res);
327
328                 // monitor policy
329                 res = write(in.monitor_policy);
330                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
331
332                 return PRIV_FLTR_ERROR_SUCCESS;
333         }
334         template<typename T, typename ...Args>
335         int write(const T* in, const Args&... args)
336         {
337                 int res = write(in);
338                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
339                 res = write(args...);
340                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
341
342                 return PRIV_FLTR_ERROR_SUCCESS;
343         }
344
345         template<typename K, typename T>
346         int write(const std::pair<K, T> p)
347         {
348                 int res = write(p.first);
349                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
350                 res = write(p.second);
351                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
352
353                 return PRIV_FLTR_ERROR_SUCCESS;
354         }
355
356         template<typename K, typename T>
357         int write(const std::pair<K, T&> p)
358         {
359                 int res = write(p.first);
360                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
361                 res = write(p.second);
362                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
363
364                 return PRIV_FLTR_ERROR_SUCCESS;
365         }
366
367         template<typename K, typename T>
368         int write(const std::pair<K, T&>* pPair)
369         {
370                 return write(*pPair);
371         }
372
373         template<typename T>
374         int write(const std::list <T> list)
375         {
376                 int length = list.size();
377                 int res = write(length);
378                 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
379                 for (typename std::list <T>::const_iterator iter = list.begin(); iter != list.end(); iter++) {
380                         res = write(*iter);
381                         TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "write : %d", res);
382                 }
383
384                 return PRIV_FLTR_ERROR_SUCCESS;
385         }
386
387         template<typename T>
388         int write(const std::list <T>* pList)
389         {
390                 return write(*pList);
391         }
392
393
394 private:
395         SocketStream m_socketStream;
396 };
397
398 #endif // _SOCKETCONNECTION_H_