1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include <string.h>
26 #include "nghttp2_option.h"
27 
28 #include "nghttp2_session.h"
29 #ifdef INFRA_MEM_STATS
30 #include "infra_mem_stats.h"
31 #endif
32 
33 extern void *HAL_Malloc(uint32_t size);
34 extern void HAL_Free(void *ptr);
35 
36 #if INFRA_MEM_STATS
37 #define NGHTTP2_OPTION_MALLOC(size) \
38     LITE_malloc(size, MEM_MAGIC, "nghttp2.option")
39 #define NGHTTP2_OPTION_FREE(ptr) LITE_free(ptr)
40 #else
41 #define NGHTTP2_OPTION_MALLOC(size) HAL_Malloc(size)
42 #define NGHTTP2_OPTION_FREE(ptr) \
43     {                            \
44         HAL_Free((void *)ptr);   \
45         ptr = NULL;              \
46     }
47 #endif
48 
nghttp2_option_new(nghttp2_option ** option_ptr)49 int nghttp2_option_new(nghttp2_option **option_ptr)
50 {
51     *option_ptr = NGHTTP2_OPTION_MALLOC(sizeof(nghttp2_option));
52 
53     if (*option_ptr == NULL) {
54         return NGHTTP2_ERR_NOMEM;
55     }
56 
57     memset(*option_ptr, 0, sizeof(nghttp2_option));
58     return 0;
59 }
60 
nghttp2_option_del(nghttp2_option * option)61 void nghttp2_option_del(nghttp2_option *option)
62 {
63     NGHTTP2_OPTION_FREE(option);
64 }
65 
nghttp2_option_set_no_auto_window_update(nghttp2_option * option,int val)66 void nghttp2_option_set_no_auto_window_update(nghttp2_option *option, int val)
67 {
68     option->opt_set_mask |= NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE;
69     option->no_auto_window_update = val;
70 }
71 
nghttp2_option_set_peer_max_concurrent_streams(nghttp2_option * option,uint32_t val)72 void nghttp2_option_set_peer_max_concurrent_streams(nghttp2_option *option,
73                                                     uint32_t val)
74 {
75     option->opt_set_mask |= NGHTTP2_OPT_PEER_MAX_CONCURRENT_STREAMS;
76     option->peer_max_concurrent_streams = val;
77 }
78 
nghttp2_option_set_no_recv_client_magic(nghttp2_option * option,int val)79 void nghttp2_option_set_no_recv_client_magic(nghttp2_option *option, int val)
80 {
81     option->opt_set_mask |= NGHTTP2_OPT_NO_RECV_CLIENT_MAGIC;
82     option->no_recv_client_magic = val;
83 }
84 
nghttp2_option_set_no_http_messaging(nghttp2_option * option,int val)85 void nghttp2_option_set_no_http_messaging(nghttp2_option *option, int val)
86 {
87     option->opt_set_mask |= NGHTTP2_OPT_NO_HTTP_MESSAGING;
88     option->no_http_messaging = val;
89 }
90 
nghttp2_option_set_max_reserved_remote_streams(nghttp2_option * option,uint32_t val)91 void nghttp2_option_set_max_reserved_remote_streams(nghttp2_option *option,
92                                                     uint32_t val)
93 {
94     option->opt_set_mask |= NGHTTP2_OPT_MAX_RESERVED_REMOTE_STREAMS;
95     option->max_reserved_remote_streams = val;
96 }
97 
set_ext_type(uint8_t * ext_types,uint8_t type)98 static void set_ext_type(uint8_t *ext_types, uint8_t type)
99 {
100     ext_types[type / 8] = (uint8_t)(ext_types[type / 8] | (1 << (type & 0x7)));
101 }
102 
nghttp2_option_set_user_recv_extension_type(nghttp2_option * option,uint8_t type)103 void nghttp2_option_set_user_recv_extension_type(nghttp2_option *option,
104                                                  uint8_t type)
105 {
106     if (type < 10) {
107         return;
108     }
109 
110     option->opt_set_mask |= NGHTTP2_OPT_USER_RECV_EXT_TYPES;
111     set_ext_type(option->user_recv_ext_types, type);
112 }
113 
nghttp2_option_set_builtin_recv_extension_type(nghttp2_option * option,uint8_t type)114 void nghttp2_option_set_builtin_recv_extension_type(nghttp2_option *option,
115                                                     uint8_t type)
116 {
117     switch (type) {
118     case NGHTTP2_ALTSVC:
119         option->opt_set_mask |= NGHTTP2_OPT_BUILTIN_RECV_EXT_TYPES;
120         option->builtin_recv_ext_types |= NGHTTP2_TYPEMASK_ALTSVC;
121         return;
122     default:
123         return;
124     }
125 }
126 
nghttp2_option_set_no_auto_ping_ack(nghttp2_option * option,int val)127 void nghttp2_option_set_no_auto_ping_ack(nghttp2_option *option, int val)
128 {
129     option->opt_set_mask |= NGHTTP2_OPT_NO_AUTO_PING_ACK;
130     option->no_auto_ping_ack = val;
131 }
132 
nghttp2_option_set_max_send_header_block_length(nghttp2_option * option,size_t val)133 void nghttp2_option_set_max_send_header_block_length(nghttp2_option *option,
134                                                      size_t val)
135 {
136     option->opt_set_mask |= NGHTTP2_OPT_MAX_SEND_HEADER_BLOCK_LENGTH;
137     option->max_send_header_block_length = val;
138 }
139 
nghttp2_option_set_max_deflate_dynamic_table_size(nghttp2_option * option,size_t val)140 void nghttp2_option_set_max_deflate_dynamic_table_size(nghttp2_option *option,
141                                                        size_t val)
142 {
143     option->opt_set_mask |= NGHTTP2_OPT_MAX_DEFLATE_DYNAMIC_TABLE_SIZE;
144     option->max_deflate_dynamic_table_size = val;
145 }
146 
nghttp2_option_set_no_closed_streams(nghttp2_option * option,int val)147 void nghttp2_option_set_no_closed_streams(nghttp2_option *option, int val)
148 {
149     option->opt_set_mask |= NGHTTP2_OPT_NO_CLOSED_STREAMS;
150     option->no_closed_streams = val;
151 }
152