35 #include <sys/syscall.h>
36 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
46 #include "uuid/uuid.h"
49 #include "safec_lib.h"
53 static int connect_to_authServer(
const char *ip,
int port,
int *auth_fd)
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);
62 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
65 Log() <<
"Socket creation failed with errno : " << errno;
68 Log() <<
"Connecting to " << ip <<
" : " << port << std::endl;
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;
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);
87 if (fcntl(socket_fd, F_SETFL, current_flags) < 0) {
88 Log() <<
"fcntl failed " << std::endl;
104 const std::string IntToString(
int i)
106 return dynamic_cast< std::ostringstream &
>(( std::ostringstream() << std::dec << (i) ) ).str();
109 const std::string IntToString(
int i,
int j)
111 return dynamic_cast< std::ostringstream &
>(( std::ostringstream() << std::dec << (i) <<
"." << (j)) ).str();
114 const std::string GenerateUUID(
void)
118 uuid_generate(value);
119 uuid_unparse(value, guid);
120 return std::string(guid);
123 void HexDump(
const std::vector<uint8_t> &in)
125 const unsigned char *ptr = &in[0];
126 size_t len = in.size();
128 for (
int i = 0; i < len; i++) {
129 printf(
"%02X ", ptr[i]);
135 uint64_t GetCurrentEpoch(
void)
138 ::gettimeofday(&tp, 0);
139 uint64_t now = (uint64_t)(((uint64_t) tp.tv_sec * 1000) + tp.tv_usec / 1000);
143 std::ostream & Timestamp (std::ostream &os)
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,
159 syscall(SYS_gettid));
167 std::ostream & Log(
void)
169 return std::cout << Timestamp ;
172 std::string GetAuthToken(
const char *generateTokenRequest)
174 static std::string responseBody;
175 std::string response;
177 if (responseBody.size() != 0) {
181 if (generateTokenRequest == NULL) {
185 if (response.size() == 0) {
186 static int writeCount = strlen(generateTokenRequest) + 1;
188 connect_to_authServer(
"127.0.0.1", 50050, &auth_fd);
190 int ret = write(auth_fd, generateTokenRequest, writeCount);
191 if (ret == writeCount) {
194 char buf[64] = {
'\0'};
195 ret = read(auth_fd, buf,
sizeof(buf));
197 response.insert(response.end(), buf, buf+ret);
208 if (response.size() > 0 && (response.find(
"200 OK") != std::string::npos)) {
211 size_t startPos = response.find(
"\r\n\r\n");
212 if (startPos != std::string::npos) {
215 responseBody = response.substr(startPos);
226 #ifndef TRM_VERSION_MAJOR
227 #define TRM_VERSION_MAJOR 2
230 #ifndef TRM_VERSION_MINOR
231 #define TRM_VERSION_MINOR 1
234 static const SpecVersion ver(TRM_VERSION_MAJOR, TRM_VERSION_MINOR);