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 <pthread.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <openssl/ssl.h>
26 
27 #include <aerospike/as_config.h>
28 
29 #include <unistd.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 
34 #define as_socket_data_t void
35 #define as_socket_size_t size_t
36 #define as_close(fd) (close(fd))
37 
38 #if defined(__APPLE__)
39 #define SOL_TCP IPPROTO_TCP
40 #define MSG_NOSIGNAL 0
41 #endif
42 
43 #define AS_IP_ADDRESS_SIZE 64
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /**
50  * This structure holds TLS context which can be shared (read-only)
51  * by all the connections to a specific cluster.
52  */
53 typedef struct as_tls_context_s {
54  pthread_mutex_t lock;
55  SSL_CTX* ssl_ctx;
59 
60 struct as_conn_pool_lock_s;
61 struct as_node_s;
62 
63 /**
64  * Socket fields for both regular and TLS sockets.
65  */
66 typedef struct as_socket_s {
67  int fd;
68  int family;
69  union {
70  struct as_conn_pool_lock_s* pool_lock; // Used when sync socket is active.
71  struct {
72  uint32_t max_socket_idle;
73  uint32_t last_used;
74  } idle_check; // Used when socket in pool.
75  };
77  const char* tls_name;
78  SSL* ssl;
79 } as_socket;
80 
81 /**
82  * @private
83  * Initialize an as_socket structure.
84  */
85 void
87 
88 /**
89  * @private
90  * Create non-blocking socket. Family should be AF_INET or AF_INET6.
91  * If socket create fails, return -errno.
92  */
93 int
94 as_socket_create_fd(int family);
95 
96 /**
97  * @private
98  * Create non-blocking socket.
99  * Family should be AF_INET or AF_INET6.
100  */
101 int
102 as_socket_create(as_socket* sock, int family, as_tls_context* ctx, const char* tls_name);
103 
104 /**
105  * @private
106  * Wrap existing fd in a socket.
107  * Family should be AF_INET or AF_INET6.
108  */
109 bool
110 as_socket_wrap(as_socket* sock, int family, int fd, as_tls_context* ctx, const char* tls_name);
111 
112 /**
113  * @private
114  * Connect to non-blocking socket.
115  */
116 bool
117 as_socket_start_connect(as_socket* sock, struct sockaddr* addr);
118 
119 /**
120  * @private
121  * Create non-blocking socket and connect.
122  */
123 as_status
124 as_socket_create_and_connect(as_socket* sock, as_error* err, struct sockaddr* addr, as_tls_context* ctx, const char* tls_name);
125 
126 /**
127  * @private
128  * Close and release resources associated with a as_socket.
129  */
130 void
132 
133 /**
134  * @private
135  * Create error message for socket error.
136  */
137 as_status
138 as_socket_error(int fd, struct as_node_s* node, as_error* err, as_status status, const char* msg, int code);
139 
140 /**
141  * @private
142  * Append address to error message.
143  */
144 void
145 as_socket_error_append(as_error* err, struct sockaddr* addr);
146 
147 /**
148  * @private
149  * Peek for socket connection status using underlying fd.
150  * Needed to support libuv.
151  *
152  * @return 0 : socket is connected, but no data available.
153  * > 0 : byte size of data available.
154  * < 0 : socket is invalid.
155  */
156 int
157 as_socket_validate_fd(int fd);
158 
159 /**
160  * @private
161  * Peek for socket connection status.
162  *
163  * @return 0 : socket is connected, but no data available.
164  * > 0 : byte size of data available.
165  * < 0 : socket is invalid.
166  */
167 int
169 
170 #if defined(__linux__) || defined(__APPLE__)
171 
172 /**
173  * @private
174  * Calculate future deadline given timeout.
175  */
176 static inline uint64_t
177 as_socket_deadline(uint32_t timeout_ms)
178 {
179  return (timeout_ms && timeout_ms <= INT32_MAX)? cf_getms() + timeout_ms : 0;
180 }
181 
182 /**
183  * @private
184  * Write socket data with future deadline in milliseconds.
185  * If deadline is zero, do not set deadline.
186  */
187 as_status
188 as_socket_write_deadline(
189  as_error* err, as_socket* sock, struct as_node_s* node, uint8_t *buf, size_t buf_len,
190  uint32_t socket_timeout, uint64_t deadline
191  );
192 
193 /**
194  * @private
195  * Read socket data with future deadline in milliseconds.
196  * If deadline is zero, do not set deadline.
197  */
198 as_status
199 as_socket_read_deadline(
200  as_error* err, as_socket* sock, struct as_node_s* node, uint8_t *buf, size_t buf_len,
201  uint32_t socket_timeout, uint64_t deadline
202  );
203 
204 #endif
205 
206 #ifdef __cplusplus
207 } // end extern "C"
208 #endif
struct as_conn_pool_lock_s * pool_lock
Definition: as_socket.h:70
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:76
void as_socket_error_append(as_error *err, struct sockaddr *addr)
void * cert_blacklist
Definition: as_socket.h:56
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:67
const char * tls_name
Definition: as_socket.h:77
uint32_t max_socket_idle
Definition: as_socket.h:72
SSL_CTX * ssl_ctx
Definition: as_socket.h:55
pthread_mutex_t lock
Definition: as_socket.h:54
bool log_session_info
Definition: as_socket.h:57
uint32_t last_used
Definition: as_socket.h:73
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:78
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:68
as_status as_socket_create_and_connect(as_socket *sock, as_error *err, struct sockaddr *addr, as_tls_context *ctx, const char *tls_name)