1 /*
2   Copyright (c) DataStax, Inc.
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 "external.hpp"
18 
19 #include "cassandra.h"
20 
21 #include <string.h>
22 #include <uv.h>
23 
24 #define NUM_SECONDS_PER_DAY (24U * 60U * 60U)
25 #define CASS_DATE_EPOCH 2147483648U // 2^31
26 #define CASS_TIME_NANOSECONDS_PER_SECOND 1000000000LL
27 
28 extern "C" {
29 
cass_error_desc(CassError error)30 const char* cass_error_desc(CassError error) {
31   switch (error) {
32 #define XX(source, _, code, desc) \
33   case CASS_ERROR(source, code):  \
34     return desc;
35     CASS_ERROR_MAPPING(XX)
36 #undef XX
37     default:
38       return "";
39   }
40 }
41 
cass_log_level_string(CassLogLevel log_level)42 const char* cass_log_level_string(CassLogLevel log_level) {
43   switch (log_level) {
44 #define XX(log_level, desc) \
45   case log_level:           \
46     return desc;
47     CASS_LOG_LEVEL_MAPPING(XX)
48 #undef XX
49     default:
50       return "";
51   }
52 }
53 
cass_consistency_string(CassConsistency consistency)54 const char* cass_consistency_string(CassConsistency consistency) {
55   switch (consistency) {
56 #define XX(consistency, desc) \
57   case consistency:           \
58     return desc;
59     CASS_CONSISTENCY_MAPPING(XX)
60 #undef XX
61     default:
62       return "";
63   }
64 }
65 
cass_write_type_string(CassWriteType write_type)66 const char* cass_write_type_string(CassWriteType write_type) {
67   switch (write_type) {
68 #define XX(write_type, desc) \
69   case write_type:           \
70     return desc;
71     CASS_WRITE_TYPE_MAPPING(XX)
72 #undef XX
73     default:
74       return "";
75   }
76 }
77 
cass_inet_init_v4(const cass_uint8_t * address)78 CassInet cass_inet_init_v4(const cass_uint8_t* address) {
79   CassInet inet;
80   inet.address_length = CASS_INET_V4_LENGTH;
81   memcpy(inet.address, address, CASS_INET_V4_LENGTH);
82   return inet;
83 }
84 
cass_inet_init_v6(const cass_uint8_t * address)85 CassInet cass_inet_init_v6(const cass_uint8_t* address) {
86   CassInet inet;
87   inet.address_length = CASS_INET_V6_LENGTH;
88   memcpy(inet.address, address, CASS_INET_V6_LENGTH);
89   return inet;
90 }
91 
cass_inet_string(CassInet inet,char * output)92 void cass_inet_string(CassInet inet, char* output) {
93   uv_inet_ntop(inet.address_length == CASS_INET_V4_LENGTH ? AF_INET : AF_INET6, inet.address,
94                output, CASS_INET_STRING_LENGTH);
95 }
96 
cass_inet_from_string(const char * str,CassInet * output)97 CassError cass_inet_from_string(const char* str, CassInet* output) {
98   if (uv_inet_pton(AF_INET, str, output->address) == 0) {
99     output->address_length = CASS_INET_V4_LENGTH;
100     return CASS_OK;
101   } else if (uv_inet_pton(AF_INET6, str, output->address) == 0) {
102     output->address_length = CASS_INET_V6_LENGTH;
103     return CASS_OK;
104   } else {
105     return CASS_ERROR_LIB_BAD_PARAMS;
106   }
107 }
108 
cass_inet_from_string_n(const char * str,size_t str_length,CassInet * output)109 CassError cass_inet_from_string_n(const char* str, size_t str_length, CassInet* output) {
110   char buf[CASS_INET_STRING_LENGTH];
111   // Need space for null terminator
112   if (str_length > CASS_INET_STRING_LENGTH - 1) {
113     return CASS_ERROR_LIB_BAD_PARAMS;
114   }
115   memcpy(buf, str, str_length);
116   buf[str_length] = '\0';
117   return cass_inet_from_string(buf, output);
118 }
119 
cass_date_from_epoch(cass_int64_t epoch_secs)120 cass_uint32_t cass_date_from_epoch(cass_int64_t epoch_secs) {
121   return (epoch_secs / NUM_SECONDS_PER_DAY) + CASS_DATE_EPOCH;
122 }
123 
cass_time_from_epoch(cass_int64_t epoch_secs)124 cass_int64_t cass_time_from_epoch(cass_int64_t epoch_secs) {
125   return CASS_TIME_NANOSECONDS_PER_SECOND * (epoch_secs % NUM_SECONDS_PER_DAY);
126 }
127 
cass_date_time_to_epoch(cass_uint32_t date,cass_int64_t time)128 cass_int64_t cass_date_time_to_epoch(cass_uint32_t date, cass_int64_t time) {
129   return (static_cast<cass_uint64_t>(date) - CASS_DATE_EPOCH) * NUM_SECONDS_PER_DAY +
130          time / CASS_TIME_NANOSECONDS_PER_SECOND;
131 }
132 
133 } // extern "C"
134