1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #include "Http2SessionAccept.h"
25 #include "Http2ClientSession.h"
26 #include "I_Machine.h"
27 #include "../IPAllow.h"
28 
Http2SessionAccept(const HttpSessionAccept::Options & _o)29 Http2SessionAccept::Http2SessionAccept(const HttpSessionAccept::Options &_o) : SessionAccept(nullptr), options(_o)
30 {
31   SET_HANDLER(&Http2SessionAccept::mainEvent);
32 }
33 
34 Http2SessionAccept::~Http2SessionAccept() = default;
35 
36 bool
accept(NetVConnection * netvc,MIOBuffer * iobuf,IOBufferReader * reader)37 Http2SessionAccept::accept(NetVConnection *netvc, MIOBuffer *iobuf, IOBufferReader *reader)
38 {
39   sockaddr const *client_ip = netvc->get_remote_addr();
40   IpAllow::ACL session_acl  = IpAllow::match(client_ip, IpAllow::SRC_ADDR);
41   if (!session_acl.isValid()) {
42     ip_port_text_buffer ipb;
43     Warning("HTTP/2 client '%s' prohibited by ip-allow policy", ats_ip_ntop(client_ip, ipb, sizeof(ipb)));
44     return false;
45   }
46 
47   netvc->attributes = this->options.transport_type;
48 
49   if (is_debug_tag_set("http2_seq")) {
50     ip_port_text_buffer ipb;
51 
52     Debug("http2_seq", "[HttpSessionAccept2:mainEvent %p] accepted connection from %s transport type = %d", netvc,
53           ats_ip_nptop(client_ip, ipb, sizeof(ipb)), netvc->attributes);
54   }
55 
56   Http2ClientSession *new_session = THREAD_ALLOC_INIT(http2ClientSessionAllocator, this_ethread());
57   new_session->acl                = std::move(session_acl);
58   new_session->accept_options     = &options;
59   new_session->new_connection(netvc, iobuf, reader);
60 
61   return true;
62 }
63 
64 int
mainEvent(int event,void * data)65 Http2SessionAccept::mainEvent(int event, void *data)
66 {
67   ink_release_assert(event == NET_EVENT_ACCEPT || event == EVENT_ERROR);
68   ink_release_assert((event == NET_EVENT_ACCEPT) ? (data != nullptr) : (1));
69 
70   if (event == NET_EVENT_ACCEPT) {
71     NetVConnection *netvc = static_cast<NetVConnection *>(data);
72     if (!this->accept(netvc, nullptr, nullptr)) {
73       netvc->do_io_close();
74     }
75     return EVENT_CONT;
76   }
77 
78   // XXX We should hoist the error handling so that all the protocols generate the statistics
79   // without code duplication.
80   if (((long)data) == -ECONNABORTED) {
81     HTTP_SUM_DYN_STAT(http_ua_msecs_counts_errors_pre_accept_hangups_stat, 0);
82   }
83 
84   ink_abort("HTTP/2 accept received fatal error: errno = %d", -(static_cast<int>((intptr_t)data)));
85   return EVENT_CONT;
86 }
87