| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/assert.h> |
| 7 |
#include <aws/common/uuid.h> |
| 8 |
#include <aws/io/logging.h> |
| 9 |
#include <aws/io/private/socket_impl.h> |
| 10 |
#include <aws/io/socket.h> |
| 11 |
|
| 12 |
void aws_socket_clean_up(struct aws_socket *socket) { |
| 13 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_cleanup_fn); |
| 14 |
socket->vtable->socket_cleanup_fn(socket); |
| 15 |
} |
| 16 |
|
| 17 |
int aws_socket_connect(struct aws_socket *socket, struct aws_socket_connect_options *socket_connect_options) { |
| 18 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_connect_fn); |
| 19 |
return socket->vtable->socket_connect_fn(socket, socket_connect_options); |
| 20 |
} |
| 21 |
|
| 22 |
int aws_socket_bind(struct aws_socket *socket, struct aws_socket_bind_options *socket_bind_options) { |
| 23 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_bind_fn); |
| 24 |
return socket->vtable->socket_bind_fn(socket, socket_bind_options); |
| 25 |
} |
| 26 |
|
| 27 |
int aws_socket_listen(struct aws_socket *socket, int backlog_size) { |
| 28 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_listen_fn); |
| 29 |
return socket->vtable->socket_listen_fn(socket, backlog_size); |
| 30 |
} |
| 31 |
|
| 32 |
int aws_socket_start_accept( |
| 33 |
struct aws_socket *socket, |
| 34 |
struct aws_event_loop *accept_loop, |
| 35 |
struct aws_socket_listener_options options) { |
| 36 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_start_accept_fn); |
| 37 |
return socket->vtable->socket_start_accept_fn(socket, accept_loop, options); |
| 38 |
} |
| 39 |
|
| 40 |
int aws_socket_stop_accept(struct aws_socket *socket) { |
| 41 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_stop_accept_fn); |
| 42 |
return socket->vtable->socket_stop_accept_fn(socket); |
| 43 |
} |
| 44 |
|
| 45 |
int aws_socket_close(struct aws_socket *socket) { |
| 46 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_close_fn); |
| 47 |
return socket->vtable->socket_close_fn(socket); |
| 48 |
} |
| 49 |
|
| 50 |
int aws_socket_set_close_complete_callback( |
| 51 |
struct aws_socket *socket, |
| 52 |
aws_socket_on_shutdown_complete_fn fn, |
| 53 |
void *user_data) { |
| 54 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_set_close_callback); |
| 55 |
return socket->vtable->socket_set_close_callback(socket, fn, user_data); |
| 56 |
} |
| 57 |
|
| 58 |
int aws_socket_set_cleanup_complete_callback( |
| 59 |
struct aws_socket *socket, |
| 60 |
aws_socket_on_shutdown_complete_fn fn, |
| 61 |
void *user_data) { |
| 62 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_set_cleanup_callback); |
| 63 |
return socket->vtable->socket_set_cleanup_callback(socket, fn, user_data); |
| 64 |
} |
| 65 |
|
| 66 |
int aws_socket_shutdown_dir(struct aws_socket *socket, enum aws_channel_direction dir) { |
| 67 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_shutdown_dir_fn); |
| 68 |
return socket->vtable->socket_shutdown_dir_fn(socket, dir); |
| 69 |
} |
| 70 |
|
| 71 |
int aws_socket_set_options(struct aws_socket *socket, const struct aws_socket_options *options) { |
| 72 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_set_options_fn); |
| 73 |
return socket->vtable->socket_set_options_fn(socket, options); |
| 74 |
} |
| 75 |
|
| 76 |
int aws_socket_assign_to_event_loop(struct aws_socket *socket, struct aws_event_loop *event_loop) { |
| 77 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_assign_to_event_loop_fn); |
| 78 |
return socket->vtable->socket_assign_to_event_loop_fn(socket, event_loop); |
| 79 |
} |
| 80 |
|
| 81 |
struct aws_event_loop *aws_socket_get_event_loop(struct aws_socket *socket) { |
| 82 |
return socket->event_loop; |
| 83 |
} |
| 84 |
|
| 85 |
int aws_socket_subscribe_to_readable_events( |
| 86 |
struct aws_socket *socket, |
| 87 |
aws_socket_on_readable_fn *on_readable, |
| 88 |
void *user_data) { |
| 89 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_subscribe_to_readable_events_fn); |
| 90 |
return socket->vtable->socket_subscribe_to_readable_events_fn(socket, on_readable, user_data); |
| 91 |
} |
| 92 |
|
| 93 |
int aws_socket_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read) { |
| 94 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_read_fn); |
| 95 |
return socket->vtable->socket_read_fn(socket, buffer, amount_read); |
| 96 |
} |
| 97 |
|
| 98 |
int aws_socket_write( |
| 99 |
struct aws_socket *socket, |
| 100 |
const struct aws_byte_cursor *cursor, |
| 101 |
aws_socket_on_write_completed_fn *written_fn, |
| 102 |
void *user_data) { |
| 103 |
|
| 104 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_write_fn); |
| 105 |
return socket->vtable->socket_write_fn(socket, cursor, written_fn, user_data); |
| 106 |
} |
| 107 |
|
| 108 |
int aws_socket_get_error(struct aws_socket *socket) { |
| 109 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_get_error_fn); |
| 110 |
return socket->vtable->socket_get_error_fn(socket); |
| 111 |
} |
| 112 |
|
| 113 |
bool aws_socket_is_open(struct aws_socket *socket) { |
| 114 |
AWS_PRECONDITION(socket->vtable && socket->vtable->socket_is_open_fn); |
| 115 |
return socket->vtable->socket_is_open_fn(socket); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
enum aws_socket_impl_type aws_socket_get_default_impl_type(void) { |
| 123 |
|
| 124 |
#ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK |
| 125 |
return AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK; |
| 126 |
#else |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
# if defined(AWS_ENABLE_KQUEUE) || defined(AWS_ENABLE_EPOLL) |
| 132 |
return AWS_SOCKET_IMPL_POSIX; |
| 133 |
# elif defined(AWS_ENABLE_DISPATCH_QUEUE) |
| 134 |
return AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK; |
| 135 |
# elif defined(AWS_ENABLE_IO_COMPLETION_PORTS) |
| 136 |
return AWS_SOCKET_IMPL_WINSOCK; |
| 137 |
# else |
| 138 |
AWS_FATAL_ASSERT( |
| 139 |
true && "Invalid default socket impl type. Please check make sure the library is compiled the correct "); |
| 140 |
return AWS_SOCKET_IMPL_PLATFORM_DEFAULT; |
| 141 |
# endif |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
static int aws_socket_impl_type_validate_platform(enum aws_socket_impl_type type); |
| 146 |
int aws_socket_init(struct aws_socket *socket, struct aws_allocator *alloc, const struct aws_socket_options *options) { |
| 147 |
|
| 148 |
|
| 149 |
enum aws_socket_impl_type type = options->impl_type; |
| 150 |
if (type == AWS_SOCKET_IMPL_PLATFORM_DEFAULT) { |
| 151 |
type = aws_socket_get_default_impl_type(); |
| 152 |
} |
| 153 |
|
| 154 |
if (aws_socket_impl_type_validate_platform(type)) { |
| 155 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Invalid event loop type on the platform."); |
| 156 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
switch (type) { |
| 161 |
case AWS_SOCKET_IMPL_POSIX: |
| 162 |
return aws_socket_init_posix(socket, alloc, options); |
| 163 |
case AWS_SOCKET_IMPL_WINSOCK: |
| 164 |
return aws_socket_init_winsock(socket, alloc, options); |
| 165 |
case AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK: |
| 166 |
return aws_socket_init_apple_nw_socket(socket, alloc, options); |
| 167 |
default: |
| 168 |
AWS_ASSERT(false && "Invalid socket implementation on platform."); |
| 169 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
int aws_socket_get_bound_address(const struct aws_socket *socket, struct aws_socket_endpoint *out_address) { |
| 174 |
if (socket->local_endpoint.address[0] == 0) { |
| 175 |
AWS_LOGF_ERROR( |
| 176 |
AWS_LS_IO_SOCKET, |
| 177 |
"id=%p fd=%d: Socket has no local address. Socket must be bound first.", |
| 178 |
(void *)socket, |
| 179 |
socket->io_handle.data.fd); |
| 180 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 181 |
} |
| 182 |
*out_address = socket->local_endpoint; |
| 183 |
return AWS_OP_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint) { |
| 187 |
(void)endpoint; |
| 188 |
struct aws_uuid uuid; |
| 189 |
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS); |
| 190 |
char uuid_str[AWS_UUID_STR_LEN] = {0}; |
| 191 |
struct aws_byte_buf uuid_buf = aws_byte_buf_from_empty_array(uuid_str, sizeof(uuid_str)); |
| 192 |
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS); |
| 193 |
|
| 194 |
enum aws_socket_impl_type socket_type = aws_socket_get_default_impl_type(); |
| 195 |
if (socket_type == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) { |
| 196 |
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".local", AWS_BYTE_BUF_PRI(uuid_buf)); |
| 197 |
} else if (socket_type == AWS_SOCKET_IMPL_POSIX) { |
| 198 |
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf)); |
| 199 |
} else if (socket_type == AWS_SOCKET_IMPL_WINSOCK) { |
| 200 |
snprintf( |
| 201 |
endpoint->address, sizeof(endpoint->address), "\\\\.\\pipe\\testsock" PRInSTR, AWS_BYTE_BUF_PRI(uuid_buf)); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static int aws_socket_impl_type_validate_platform(enum aws_socket_impl_type type) { |
| 206 |
switch (type) { |
| 207 |
case AWS_SOCKET_IMPL_POSIX: |
| 208 |
#if !defined(AWS_ENABLE_EPOLL) && !defined(AWS_ENABLE_KQUEUE) |
| 209 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Posix socket is not supported on the platform."); |
| 210 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 211 |
#endif |
| 212 |
break; |
| 213 |
case AWS_SOCKET_IMPL_WINSOCK: |
| 214 |
#ifndef AWS_ENABLE_IO_COMPLETION_PORTS |
| 215 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "WINSOCK is not supported on the platform."); |
| 216 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 217 |
#endif |
| 218 |
break; |
| 219 |
case AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK: |
| 220 |
#ifndef AWS_ENABLE_DISPATCH_QUEUE |
| 221 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Apple Network Framework is not supported on the platform."); |
| 222 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 223 |
#endif |
| 224 |
break; |
| 225 |
default: |
| 226 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Invalid socket implementation type."); |
| 227 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 228 |
break; |
| 229 |
} |
| 230 |
return AWS_OP_SUCCESS; |
| 231 |
} |
| 232 |
|
| 233 |
#if !defined(AWS_ENABLE_EPOLL) && !defined(AWS_ENABLE_KQUEUE) |
| 234 |
int aws_socket_init_posix( |
| 235 |
struct aws_socket *socket, |
| 236 |
struct aws_allocator *alloc, |
| 237 |
const struct aws_socket_options *options) { |
| 238 |
(void)socket; |
| 239 |
(void)alloc; |
| 240 |
(void)options; |
| 241 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Posix socket is not supported on the platform."); |
| 242 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 243 |
} |
| 244 |
#endif |
| 245 |
|
| 246 |
#ifndef AWS_ENABLE_IO_COMPLETION_PORTS |
| 247 |
int aws_socket_init_winsock( |
| 248 |
struct aws_socket *socket, |
| 249 |
struct aws_allocator *alloc, |
| 250 |
const struct aws_socket_options *options) { |
| 251 |
(void)socket; |
| 252 |
(void)alloc; |
| 253 |
(void)options; |
| 254 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "WINSOCK is not supported on the platform."); |
| 255 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
#ifndef AWS_ENABLE_DISPATCH_QUEUE |
| 260 |
int aws_socket_init_apple_nw_socket( |
| 261 |
struct aws_socket *socket, |
| 262 |
struct aws_allocator *alloc, |
| 263 |
const struct aws_socket_options *options) { |
| 264 |
(void)socket; |
| 265 |
(void)alloc; |
| 266 |
(void)options; |
| 267 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "Apple Network Framework is not supported on the platform."); |
| 268 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 269 |
} |
| 270 |
#endif |