Debian Codegraph @

crt/aws-c-io/source/alpn_handler.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/channel.h>
7 #include <aws/io/tls_channel_handler.h>
8
9 struct alpn_handler {
10 aws_tls_on_protocol_negotiated on_protocol_negotiated;
11 void *user_data;
12 };
13
14 static int s_alpn_process_read_message(
15 struct aws_channel_handler *handler,
16 struct aws_channel_slot *slot,
17 struct aws_io_message *message) {
18
19 if (message->message_tag != AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE) {
20 return aws_raise_error(AWS_IO_MISSING_ALPN_MESSAGE);
21 }
22
23 struct aws_tls_negotiated_protocol_message *protocol_message =
24 (struct aws_tls_negotiated_protocol_message *)message->message_data.buffer;
25
26 struct aws_channel_slot *new_slot = aws_channel_slot_new(slot->channel);
27
28 struct alpn_handler *alpn_handler = (struct alpn_handler *)handler->impl;
29
30 if (!new_slot) {
31 return AWS_OP_ERR;
32 }
33
34 struct aws_channel_handler *new_handler =
35 alpn_handler->on_protocol_negotiated(new_slot, &protocol_message->protocol, alpn_handler->user_data);
36
37 if (!new_handler) {
38 aws_mem_release(handler->alloc, (void *)new_slot);
39 return aws_raise_error(AWS_IO_UNHANDLED_ALPN_PROTOCOL_MESSAGE);
40 }
41
42 aws_channel_slot_replace(slot, new_slot);
43 aws_channel_slot_set_handler(new_slot, new_handler);
44 return AWS_OP_SUCCESS;
45 }
46
47 static int s_alpn_shutdown(
48 struct aws_channel_handler *handler,
49 struct aws_channel_slot *slot,
50 enum aws_channel_direction dir,
51 int error_code,
52 bool abort_immediately) {
53 (void)handler;
54 return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately);
55 }
56
57 static size_t s_alpn_get_initial_window_size(struct aws_channel_handler *handler) {
58 (void)handler;
59 return sizeof(struct aws_tls_negotiated_protocol_message);
60 }
61
62 static void s_alpn_destroy(struct aws_channel_handler *handler) {
63 struct alpn_handler *alpn_handler = (struct alpn_handler *)handler->impl;
64 aws_mem_release(handler->alloc, alpn_handler);
65 aws_mem_release(handler->alloc, handler);
66 }
67
68 static size_t s_alpn_message_overhead(struct aws_channel_handler *handler) {
69 (void)handler;
70 return 0;
71 }
72
73 static struct aws_channel_handler_vtable s_alpn_handler_vtable = {
74 .initial_window_size = s_alpn_get_initial_window_size,
75 .increment_read_window = NULL,
76 .shutdown = s_alpn_shutdown,
77 .process_write_message = NULL,
78 .process_read_message = s_alpn_process_read_message,
79 .destroy = s_alpn_destroy,
80 .message_overhead = s_alpn_message_overhead,
81 };
82
83 struct aws_channel_handler *aws_tls_alpn_handler_new(
84 struct aws_allocator *allocator,
85 aws_tls_on_protocol_negotiated on_protocol_negotiated,
86 void *user_data) {
87 struct aws_channel_handler *channel_handler =
88 (struct aws_channel_handler *)aws_mem_calloc(allocator, 1, sizeof(struct aws_channel_handler));
89
90 if (!channel_handler) {
91 return NULL;
92 }
93
94 struct alpn_handler *alpn_handler =
95 (struct alpn_handler *)aws_mem_calloc(allocator, 1, sizeof(struct alpn_handler));
96
97 if (!alpn_handler) {
98 aws_mem_release(allocator, (void *)channel_handler);
99 return NULL;
100 }
101
102 alpn_handler->on_protocol_negotiated = on_protocol_negotiated;
103 alpn_handler->user_data = user_data;
104 channel_handler->impl = alpn_handler;
105 channel_handler->alloc = allocator;
106
107 channel_handler->vtable = &s_alpn_handler_vtable;
108
109 return channel_handler;
110 }