00001 /** @file 00002 00003 A brief file description 00004 00005 @section license License 00006 00007 Licensed to the Apache Software Foundation (ASF) under one 00008 or more contributor license agreements. See the NOTICE file 00009 distributed with this work for additional information 00010 regarding copyright ownership. The ASF licenses this file 00011 to you under the Apache License, Version 2.0 (the 00012 "License"); you may not use this file except in compliance 00013 with the License. You may obtain a copy of the License at 00014 00015 http://www.apache.org/licenses/LICENSE-2.0 00016 00017 Unless required by applicable law or agreed to in writing, software 00018 distributed under the License is distributed on an "AS IS" BASIS, 00019 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 See the License for the specific language governing permissions and 00021 limitations under the License. 00022 */ 00023 00024 /************************************************************************** 00025 00026 P_DNSConnection.h 00027 Description: 00028 struct DNSConnection 00029 **************************************************************************/ 00030 00031 #ifndef __P_DNSCONNECTION_H__ 00032 #define __P_DNSCONNECTION_H__ 00033 00034 #include "I_EventSystem.h" 00035 00036 // 00037 // Connection 00038 // 00039 struct DNSHandler; 00040 00041 struct DNSConnection { 00042 /// Options for connecting. 00043 struct Options { 00044 typedef Options self; ///< Self reference type. 00045 00046 /// Connection is done non-blocking. 00047 /// Default: @c true. 00048 bool _non_blocking_connect; 00049 /// Set socket to have non-blocking I/O. 00050 /// Default: @c true. 00051 bool _non_blocking_io; 00052 /// Use TCP if @c true, use UDP if @c false. 00053 /// Default: @c false. 00054 bool _use_tcp; 00055 /// Bind to a random port. 00056 /// Default: @c true. 00057 bool _bind_random_port; 00058 /// Bind to this local address when using IPv6. 00059 /// Default: unset, bind to IN6ADDR_ANY. 00060 sockaddr const* _local_ipv6; 00061 /// Bind to this local address when using IPv4. 00062 /// Default: unset, bind to INADDRY_ANY. 00063 sockaddr const* _local_ipv4; 00064 00065 Options(); 00066 00067 self& setUseTcp(bool p); 00068 self& setNonBlockingConnect(bool p); 00069 self& setNonBlockingIo(bool p); 00070 self& setBindRandomPort(bool p); 00071 self& setLocalIpv6(sockaddr const* addr); 00072 self& setLocalIpv4(sockaddr const* addr); 00073 }; 00074 00075 int fd; 00076 IpEndpoint ip; 00077 int num; 00078 LINK(DNSConnection, link); 00079 EventIO eio; 00080 InkRand generator; 00081 DNSHandler* handler; 00082 00083 int connect(sockaddr const* addr, Options const& opt = DEFAULT_OPTIONS); 00084 /* 00085 bool non_blocking_connect = NON_BLOCKING_CONNECT, 00086 bool use_tcp = CONNECT_WITH_TCP, bool non_blocking = NON_BLOCKING, bool bind_random_port = BIND_ANY_PORT); 00087 */ 00088 int close(); 00089 void trigger(); 00090 00091 virtual ~DNSConnection(); 00092 DNSConnection(); 00093 00094 static Options const DEFAULT_OPTIONS; 00095 }; 00096 00097 inline DNSConnection::Options::Options() 00098 : _non_blocking_connect(true) 00099 , _non_blocking_io(true) 00100 , _use_tcp(false) 00101 , _bind_random_port(true) 00102 , _local_ipv6(0) 00103 , _local_ipv4(0) 00104 { 00105 } 00106 00107 inline DNSConnection::Options& 00108 DNSConnection::Options::setNonBlockingIo(bool p) { _non_blocking_io = p; return *this; } 00109 inline DNSConnection::Options& 00110 DNSConnection::Options::setNonBlockingConnect(bool p) { _non_blocking_connect = p; return *this; } 00111 inline DNSConnection::Options& 00112 DNSConnection::Options::setUseTcp(bool p) { _use_tcp = p; return *this; } 00113 inline DNSConnection::Options& 00114 DNSConnection::Options::setBindRandomPort(bool p) { _bind_random_port = p; return *this; } 00115 inline DNSConnection::Options& 00116 DNSConnection::Options::setLocalIpv4(sockaddr const* ip) { _local_ipv4 = ip; return *this; } 00117 inline DNSConnection::Options& 00118 DNSConnection::Options::setLocalIpv6(sockaddr const* ip) { _local_ipv6 = ip; return *this; } 00119 00120 #endif /*_P_DNSConnection_h*/