RDK Documentation (Open Sourced RDK Components)
Util.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup common
25 * @{
26 **/
27 
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <iostream>
32 #include <sstream>
33 #include <vector>
34 #include <sys/time.h>
35 #include <sys/syscall.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <errno.h>
44 
45 
46 #include "uuid/uuid.h"
47 #include "trm/TRM.h"
48 #include "Util.h"
49 #include "safec_lib.h"
50 
51 TRM_BEGIN_NAMESPACE
52 
53 static int connect_to_authServer(const char *ip, int port, int *auth_fd)
54 {
55  int socket_fd = -1;
56  int socket_error = 0;
57  struct sockaddr_in auth_address = {0};
58  auth_address.sin_family = AF_INET;
59  auth_address.sin_addr.s_addr = inet_addr(ip);
60  auth_address.sin_port = htons(port);
61 
62  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
63  if(socket_fd < 0)
64  {
65  Log() << "Socket creation failed with errno : " << errno; //CID:18159-Resolve the negative returns
66  return -1;
67  }
68  Log() << "Connecting to " << ip << " : " << port << std::endl;
69  while(1) {
70  int retry_count = 10;
71  socket_error = connect(socket_fd, (struct sockaddr *) &auth_address, sizeof(struct sockaddr_in));
72  if (socket_error == ECONNREFUSED && retry_count > 0) {
73  Log() << "Auth Server is not started...retry to connect" << std::endl;
74  sleep(2);
75  retry_count--;
76  }
77  else {
78  break;
79  }
80  }
81 
82  if (socket_error == 0){
83  Log() << "Connected to " << ip << " : " << port << std::endl;
84  int current_flags = fcntl(socket_fd, F_GETFL, 0);
85  current_flags &= (~O_NONBLOCK);
86  /* RDKSEC-811 coverity fix - CHECKED_RETURN */
87  if (fcntl(socket_fd, F_SETFL, current_flags) < 0) {
88  Log() << "fcntl failed " << std::endl;
89  close(socket_fd);
90  *auth_fd = -1;
91  socket_error = -1;
92  return socket_error;
93  }
94  *auth_fd = socket_fd;
95  }
96  else {
97  close(socket_fd);
98  *auth_fd = -1;
99  }
100 
101  return socket_error;
102 }
103 
104 const std::string IntToString(int i)
105 {
106  return dynamic_cast< std::ostringstream & >(( std::ostringstream() << std::dec << (i) ) ).str();
107 }
108 
109 const std::string IntToString(int i, int j)
110 {
111  return dynamic_cast< std::ostringstream & >(( std::ostringstream() << std::dec << (i) << "." << (j)) ).str();
112 }
113 
114 const std::string GenerateUUID(void)
115 {
116  uuid_t value;
117  char guid[64];
118  uuid_generate(value);
119  uuid_unparse(value, guid);
120  return std::string(guid);
121 }
122 
123 void HexDump(const std::vector<uint8_t> &in)
124 {
125  const unsigned char *ptr = &in[0];
126  size_t len = in.size();
127 
128  for (int i = 0; i < len; i++) {
129  printf("%02X ", ptr[i]);
130  }
131 
132  printf("\r\n");
133 }
134 
135 uint64_t GetCurrentEpoch(void)
136 {
137  struct timeval tp;
138  ::gettimeofday(&tp, 0);
139  uint64_t now = (uint64_t)(((uint64_t) tp.tv_sec * 1000) + tp.tv_usec / 1000);
140  return now;
141 }
142 
143 std::ostream & Timestamp (std::ostream &os)
144 {
145  struct tm __tm;
146  struct timeval __tv;
147  char buf[64];
148  errno_t safec_rc = -1;
149  gettimeofday(&__tv, NULL);
150  localtime_r(&__tv.tv_sec, &__tm);
151  safec_rc = sprintf_s(buf,sizeof(buf), "%02d%02d%02d-%02d:%02d:%02d:%06d [tid=%ld] ",
152  __tm.tm_year+1900-2000,
153  __tm.tm_mon+1,
154  __tm.tm_mday,
155  __tm.tm_hour,
156  __tm.tm_min,
157  __tm.tm_sec,
158  (int)__tv.tv_usec,
159  syscall(SYS_gettid));
160  if(safec_rc < EOK) {
161  ERR_CHK(safec_rc);
162  }
163  os << buf;
164  return os;
165 }
166 
167 std::ostream & Log(void)
168 {
169  return std::cout << Timestamp ;
170 }
171 
172 std::string GetAuthToken(const char *generateTokenRequest)
173 {
174  static std::string responseBody;
175  std::string response;
176 
177  if (responseBody.size() != 0) {
178  return responseBody;
179  }
180 
181  if (generateTokenRequest == NULL) {
182  return responseBody;
183  }
184 
185  if (response.size() == 0) {
186  static int writeCount = strlen(generateTokenRequest) + 1;
187  int auth_fd = -1;
188  connect_to_authServer("127.0.0.1", 50050, &auth_fd);
189  if (auth_fd >= 0) {
190  int ret = write(auth_fd, generateTokenRequest, writeCount);
191  if (ret == writeCount) {
192  /* read for response till EOF*/
193  while(1) {
194  char buf[64] = {'\0'};
195  ret = read(auth_fd, buf, sizeof(buf));
196  if (ret > 0) {
197  response.insert(response.end(), buf, buf+ret);
198  }
199  else {
200  break;
201  }
202  }
203  }
204  close(auth_fd);
205  }
206  }
207 
208  if (response.size() > 0 && (response.find("200 OK") != std::string::npos)) {
209  //Log() << "Response : \r\n [" << response << "] " << std::endl;
210  /* Get to the body part */
211  size_t startPos = response.find("\r\n\r\n");
212  if (startPos != std::string::npos) {
213  startPos += 4; //\r\n\r\n
214  //Log() << "Json Body is [" << response.substr(startPos) << "]" << std::endl;
215  responseBody = response.substr(startPos);
216  }
217  else {
218  }
219  }
220 
221  return responseBody;
222 }
223 
224 const SpecVersion & GetSpecVersion(void)
225 {
226 #ifndef TRM_VERSION_MAJOR
227 #define TRM_VERSION_MAJOR 2
228 #endif
229 
230 #ifndef TRM_VERSION_MINOR
231 #define TRM_VERSION_MINOR 1
232 #endif
233 
234  static const SpecVersion ver(TRM_VERSION_MAJOR, TRM_VERSION_MINOR);
235  return ver;
236 }
237 
238 TRM_END_NAMESPACE
239 
240 
241 /** @} */
242 /** @} */
SpecVersion
Definition: Util.h:41