All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_socket.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_error.h>
20 #include <citrusleaf/cf_clock.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 #include <openssl/ssl.h>
25 
26 #include <aerospike/as_config.h>
27 
28 #include <unistd.h>
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 
33 #define as_socket_data_t void
34 #define as_socket_size_t size_t
35 #define as_close(fd) (close(fd))
36 
37 #if defined(__APPLE__)
38 #define SOL_TCP IPPROTO_TCP
39 #define MSG_NOSIGNAL 0
40 #endif
41 
42 #define AS_IP_ADDRESS_SIZE 64
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /**
49  * This structure holds TLS context which can be shared (read-only)
50  * by all the connections to a specific cluster.
51  */
52 typedef struct as_tls_context_s {
53  SSL_CTX* ssl_ctx;
57 
58 struct as_conn_pool_lock_s;
59 struct as_node_s;
60 
61 /**
62  * Socket fields for both regular and TLS sockets.
63  */
64 typedef struct as_socket_s {
65  int fd;
66  int family;
67  union {
68  struct as_conn_pool_lock_s* pool_lock; // Used when sync socket is active.
69  struct {
70  uint32_t max_socket_idle;
71  uint32_t last_used;
72  } idle_check; // Used when socket in pool.
73  };
75  const char* tls_name;
76  SSL* ssl;
77 } as_socket;
78 
79 /**
80  * @private
81  * Initialize an as_socket structure.
82  */
83 void
85 
86 /**
87  * @private
88  * Create non-blocking socket. Family should be AF_INET or AF_INET6.
89  * If socket create fails, return -errno.
90  */
91 int
92 as_socket_create_fd(int family);
93 
94 /**
95  * @private
96  * Create non-blocking socket.
97  * Family should be AF_INET or AF_INET6.
98  */
99 int
100 as_socket_create(as_socket* sock, int family, as_tls_context* ctx, const char* tls_name);
101 
102 /**
103  * @private
104  * Wrap existing fd in a socket.
105  * Family should be AF_INET or AF_INET6.
106  */
107 bool
108 as_socket_wrap(as_socket* sock, int family, int fd, as_tls_context* ctx, const char* tls_name);
109 
110 /**
111  * @private
112  * Connect to non-blocking socket.
113  */
114 bool
115 as_socket_start_connect(as_socket* sock, struct sockaddr* addr);
116 
117 /**
118  * @private
119  * Create non-blocking socket and connect.
120  */
121 as_status
122 as_socket_create_and_connect(as_socket* sock, as_error* err, struct sockaddr* addr, as_tls_context* ctx, const char* tls_name);
123 
124 /**
125  * @private
126  * Close and release resources associated with a as_socket.
127  */
128 void
130 
131 /**
132  * @private
133  * Create error message for socket error.
134  */
135 as_status
136 as_socket_error(int fd, struct as_node_s* node, as_error* err, as_status status, const char* msg, int code);
137 
138 /**
139  * @private
140  * Append address to error message.
141  */
142 void
143 as_socket_error_append(as_error* err, struct sockaddr* addr);
144 
145 /**
146  * @private
147  * Peek for socket connection status using underlying fd.
148  * Needed to support libuv.
149  *
150  * @return 0 : socket is connected, but no data available.
151  * > 0 : byte size of data available.
152  * < 0 : socket is invalid.
153  */
154 int
155 as_socket_validate_fd(int fd);
156 
157 /**
158  * @private
159  * Peek for socket connection status.
160  *
161  * @return 0 : socket is connected, but no data available.
162  * > 0 : byte size of data available.
163  * < 0 : socket is invalid.
164  */
165 int
167 
168 #if defined(__linux__) || defined(__APPLE__)
169 
170 /**
171  * @private
172  * Calculate future deadline given timeout.
173  */
174 static inline uint64_t
175 as_socket_deadline(uint32_t timeout_ms)
176 {
177  return (timeout_ms && timeout_ms <= INT32_MAX)? cf_getms() + timeout_ms : 0;
178 }
179 
180 /**
181  * @private
182  * Write socket data with future deadline in milliseconds.
183  * If deadline is zero, do not set deadline.
184  */
185 as_status
186 as_socket_write_deadline(
187  as_error* err, as_socket* sock, struct as_node_s* node, uint8_t *buf, size_t buf_len,
188  uint32_t max_idle, uint64_t deadline
189  );
190 
191 /**
192  * @private
193  * Read socket data with future deadline in milliseconds.
194  * If deadline is zero, do not set deadline.
195  */
196 as_status
197 as_socket_read_deadline(
198  as_error* err, as_socket* sock, struct as_node_s* node, uint8_t *buf, size_t buf_len,
199  uint32_t max_idle, uint64_t deadline
200  );
201 
202 #endif
203 
204 #ifdef __cplusplus
205 } // end extern "C"
206 #endif
struct as_conn_pool_lock_s * pool_lock
Definition: as_socket.h:68
as_status
Definition: as_status.h:30
as_status as_socket_error(int fd, struct as_node_s *node, as_error *err, as_status status, const char *msg, int code)
as_tls_context * ctx
Definition: as_socket.h:74
void as_socket_error_append(as_error *err, struct sockaddr *addr)
void * cert_blacklist
Definition: as_socket.h:54
int as_socket_validate(as_socket *sock)
int as_socket_validate_fd(int fd)
void as_socket_init(as_socket *sock)
int fd
Definition: as_socket.h:65
const char * tls_name
Definition: as_socket.h:75
uint32_t max_socket_idle
Definition: as_socket.h:70
SSL_CTX * ssl_ctx
Definition: as_socket.h:53
bool log_session_info
Definition: as_socket.h:55
uint32_t last_used
Definition: as_socket.h:71
void as_socket_close(as_socket *sock)
int as_socket_create_fd(int family)
bool as_socket_start_connect(as_socket *sock, struct sockaddr *addr)
int as_socket_create(as_socket *sock, int family, as_tls_context *ctx, const char *tls_name)
SSL * ssl
Definition: as_socket.h:76
bool as_socket_wrap(as_socket *sock, int family, int fd, as_tls_context *ctx, const char *tls_name)
int family
Definition: as_socket.h:66
as_status as_socket_create_and_connect(as_socket *sock, as_error *err, struct sockaddr *addr, as_tls_context *ctx, const char *tls_name)