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 "HttpSessionAccept.h"
25 #include "IPAllow.h"
26 #include "Http1ClientSession.h"
27 #include "I_Machine.h"
28
29 bool
accept(NetVConnection * netvc,MIOBuffer * iobuf,IOBufferReader * reader)30 HttpSessionAccept::accept(NetVConnection *netvc, MIOBuffer *iobuf, IOBufferReader *reader)
31 {
32 sockaddr const *client_ip = netvc->get_remote_addr();
33 IpAllow::ACL acl;
34 ip_port_text_buffer ipb;
35
36 acl = IpAllow::match(client_ip, IpAllow::SRC_ADDR);
37 if (!acl.isValid()) { // if there's no ACL, it's a hard deny.
38 Warning("client '%s' prohibited by ip-allow policy", ats_ip_ntop(client_ip, ipb, sizeof(ipb)));
39 return false;
40 }
41
42 // Set the transport type if not already set
43 if (HttpProxyPort::TRANSPORT_NONE == netvc->attributes) {
44 netvc->attributes = transport_type;
45 }
46
47 if (is_debug_tag_set("http_seq")) {
48 Debug("http_seq", "[HttpSessionAccept:mainEvent %p] accepted connection from %s transport type = %d", netvc,
49 ats_ip_nptop(client_ip, ipb, sizeof(ipb)), netvc->attributes);
50 }
51
52 Http1ClientSession *new_session = THREAD_ALLOC_INIT(http1ClientSessionAllocator, this_ethread());
53
54 new_session->accept_options = static_cast<Options *>(this);
55 new_session->acl = std::move(acl);
56
57 // Pin session to current ET_NET thread
58 new_session->setThreadAffinity(this_ethread());
59 new_session->new_connection(netvc, iobuf, reader);
60
61 return true;
62 }
63
64 int
mainEvent(int event,void * data)65 HttpSessionAccept::mainEvent(int event, void *data)
66 {
67 NetVConnection *netvc;
68 ink_release_assert(event == NET_EVENT_ACCEPT || event == EVENT_ERROR);
69 ink_release_assert((event == NET_EVENT_ACCEPT) ? (data != nullptr) : (1));
70
71 if (event == NET_EVENT_ACCEPT) {
72 netvc = static_cast<NetVConnection *>(data);
73 if (!this->accept(netvc, nullptr, nullptr)) {
74 netvc->do_io_close();
75 }
76 return EVENT_CONT;
77 }
78
79 /////////////////
80 // EVENT_ERROR //
81 /////////////////
82 if (((long)data) == -ECONNABORTED) {
83 /////////////////////////////////////////////////
84 // Under Solaris, when accept() fails and sets //
85 // errno to EPROTO, it means the client has //
86 // sent a TCP reset before the connection has //
87 // been accepted by the server... Note that //
88 // in 2.5.1 with the Internet Server Supplement//
89 // and also in 2.6 the errno for this case has //
90 // changed from EPROTO to ECONNABORTED. //
91 /////////////////////////////////////////////////
92
93 // FIX: add time to user_agent_hangup
94 HTTP_SUM_DYN_STAT(http_ua_msecs_counts_errors_pre_accept_hangups_stat, 0);
95 }
96
97 ink_abort("HTTP accept received fatal error: errno = %d", -(static_cast<int>((intptr_t)data)));
98 return EVENT_CONT;
99 }
100