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 "I_NetVConnection.h"
25 #include "ProxyProtocol.h"
26 
27 inline sockaddr const *
get_remote_addr()28 NetVConnection::get_remote_addr()
29 {
30   if (!got_remote_addr) {
31     if (pp_info.version != ProxyProtocolVersion::UNDEFINED) {
32       set_remote_addr(get_proxy_protocol_src_addr());
33     } else {
34       set_remote_addr();
35     }
36     got_remote_addr = true;
37   }
38   return &remote_addr.sa;
39 }
40 
41 inline IpEndpoint const &
get_remote_endpoint()42 NetVConnection::get_remote_endpoint()
43 {
44   get_remote_addr(); // Make sure the value is filled in
45   return remote_addr;
46 }
47 
48 inline in_addr_t
get_remote_ip()49 NetVConnection::get_remote_ip()
50 {
51   sockaddr const *addr = this->get_remote_addr();
52   return ats_is_ip4(addr) ? ats_ip4_addr_cast(addr) : 0;
53 }
54 
55 /// @return The remote port in host order.
56 inline uint16_t
get_remote_port()57 NetVConnection::get_remote_port()
58 {
59   return ats_ip_port_host_order(this->get_remote_addr());
60 }
61 
62 inline IpEndpoint const &
get_local_endpoint()63 NetVConnection::get_local_endpoint()
64 {
65   get_local_addr();
66   return local_addr;
67 }
68 
69 inline sockaddr const *
get_local_addr()70 NetVConnection::get_local_addr()
71 {
72   if (!got_local_addr) {
73     set_local_addr();
74     if ((ats_is_ip(&local_addr) && ats_ip_port_cast(&local_addr))                    // IP and has a port.
75         || (ats_is_ip4(&local_addr) && INADDR_ANY != ats_ip4_addr_cast(&local_addr)) // IPv4
76         || (ats_is_ip6(&local_addr) && !IN6_IS_ADDR_UNSPECIFIED(&local_addr.sin6.sin6_addr))) {
77       got_local_addr = true;
78     }
79   }
80   return &local_addr.sa;
81 }
82 
83 inline in_addr_t
get_local_ip()84 NetVConnection::get_local_ip()
85 {
86   sockaddr const *addr = this->get_local_addr();
87   return ats_is_ip4(addr) ? ats_ip4_addr_cast(addr) : 0;
88 }
89 
90 /// @return The local port in host order.
91 inline uint16_t
get_local_port()92 NetVConnection::get_local_port()
93 {
94   return ats_ip_port_host_order(this->get_local_addr());
95 }
96 
97 inline sockaddr const *
get_proxy_protocol_addr(const ProxyProtocolData src_or_dst)98 NetVConnection::get_proxy_protocol_addr(const ProxyProtocolData src_or_dst) const
99 {
100   const IpEndpoint &addr = (src_or_dst == ProxyProtocolData::SRC ? pp_info.src_addr : pp_info.dst_addr);
101 
102   if ((addr.isValid() && addr.port() != 0) || (ats_is_ip4(&addr) && INADDR_ANY != ats_ip4_addr_cast(&addr)) // IPv4
103       || (ats_is_ip6(&addr) && !IN6_IS_ADDR_UNSPECIFIED(&addr.sin6.sin6_addr))) {
104     return &addr.sa;
105   }
106 
107   return nullptr;
108 }
109 
110 inline void
set_proxy_protocol_info(const ProxyProtocol & src)111 NetVConnection::set_proxy_protocol_info(const ProxyProtocol &src)
112 {
113   if (pp_info.version == ProxyProtocolVersion::UNDEFINED) {
114     pp_info = src;
115   }
116 }
117 
118 inline const ProxyProtocol &
get_proxy_protocol_info()119 NetVConnection::get_proxy_protocol_info() const
120 {
121   return pp_info;
122 }
123