From commits-return-13269-apmail-trafficserver-commits-archive=trafficserver.apache.org@trafficserver.apache.org Mon Mar 23 20:32:35 2015 Return-Path: X-Original-To: apmail-trafficserver-commits-archive@www.apache.org Delivered-To: apmail-trafficserver-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0869417620 for ; Mon, 23 Mar 2015 20:32:35 +0000 (UTC) Received: (qmail 49181 invoked by uid 500); 23 Mar 2015 20:32:34 -0000 Delivered-To: apmail-trafficserver-commits-archive@trafficserver.apache.org Received: (qmail 49123 invoked by uid 500); 23 Mar 2015 20:32:34 -0000 Mailing-List: contact commits-help@trafficserver.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@trafficserver.apache.org Delivered-To: mailing list commits@trafficserver.apache.org Received: (qmail 49114 invoked by uid 99); 23 Mar 2015 20:32:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Mar 2015 20:32:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 80256E1835; Mon, 23 Mar 2015 20:32:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: zwoop@apache.org To: commits@trafficserver.apache.org Date: Mon, 23 Mar 2015 20:32:37 -0000 Message-Id: In-Reply-To: <40710b3aa56e4de9bf7e088dcb3a90e5@git.apache.org> References: <40710b3aa56e4de9bf7e088dcb3a90e5@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/52] [partial] trafficserver git commit: TS-3419 Fix some enum's such that clang-format can handle it the way we want. Basically this means having a trailing , on short enum's. TS-3419 Run clang-format over most of the source http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/wccp/WccpConfig.cc ---------------------------------------------------------------------- diff --git a/lib/wccp/WccpConfig.cc b/lib/wccp/WccpConfig.cc index fca9966..b2a5327 100644 --- a/lib/wccp/WccpConfig.cc +++ b/lib/wccp/WccpConfig.cc @@ -19,30 +19,34 @@ limitations under the License. */ -# include "WccpLocal.h" -# include -# include -# include -# include -# include -# include +#include "WccpLocal.h" +#include +#include +#include +#include +#include +#include using ts::config::Configuration; using ts::config::Value; // Support that must go in the standard namespace. -namespace std { - -inline ostream& operator << ( ostream& s, ts::ConstBuffer const& b ) { - if (b._ptr) s.write(b._ptr, b._size); - else s << b._size; +namespace std +{ +inline ostream &operator<<(ostream &s, ts::ConstBuffer const &b) +{ + if (b._ptr) + s.write(b._ptr, b._size); + else + s << b._size; return s; } } // namespace std // WCCP related things that are file local. -namespace { +namespace +{ using namespace wccp; // Scratch global list of seed router addresses. @@ -51,64 +55,69 @@ using namespace wccp; std::vector Seed_Router; // Names used for various elements and properties. -static char const * const SVC_NAME = "service"; - -static char const * const SVC_PROP_ID = "id"; -static char const * const SVC_PROP_TYPE = "type"; -static char const * const SVC_PROP_PRIORITY = "priority"; -static char const * const SVC_PROP_PROTOCOL = "protocol"; -static char const * const SVC_PROP_FLAGS = "flags"; -static char const * const SVC_PROP_PRIMARY_HASH = "primary-hash"; -static char const * const SVC_PROP_ALT_HASH = "alt-hash"; -static char const * const SVC_PROP_PORTS = "ports"; -static char const * const SVC_PROP_PORT_TYPE = "port-type"; -static char const * const SVC_PROP_SECURITY = "security"; -static char const * const SVC_PROP_ROUTERS = "routers"; -static char const * const SVC_PROP_FORWARD = "forward"; -static char const * const SVC_PROP_RETURN = "return"; -static char const * const SVC_PROP_ASSIGN = "assignment"; -static char const * const SVC_PROP_PROC = "proc-name"; - -static char const * const SECURITY_PROP_OPTION = "option"; -static char const * const SECURITY_PROP_KEY = "key"; +static char const *const SVC_NAME = "service"; + +static char const *const SVC_PROP_ID = "id"; +static char const *const SVC_PROP_TYPE = "type"; +static char const *const SVC_PROP_PRIORITY = "priority"; +static char const *const SVC_PROP_PROTOCOL = "protocol"; +static char const *const SVC_PROP_FLAGS = "flags"; +static char const *const SVC_PROP_PRIMARY_HASH = "primary-hash"; +static char const *const SVC_PROP_ALT_HASH = "alt-hash"; +static char const *const SVC_PROP_PORTS = "ports"; +static char const *const SVC_PROP_PORT_TYPE = "port-type"; +static char const *const SVC_PROP_SECURITY = "security"; +static char const *const SVC_PROP_ROUTERS = "routers"; +static char const *const SVC_PROP_FORWARD = "forward"; +static char const *const SVC_PROP_RETURN = "return"; +static char const *const SVC_PROP_ASSIGN = "assignment"; +static char const *const SVC_PROP_PROC = "proc-name"; + +static char const *const SECURITY_PROP_OPTION = "option"; +static char const *const SECURITY_PROP_KEY = "key"; /// Helper structure for processing configuration strings. struct CfgString { - char const* m_text; ///< Text value of the option. - bool m_found; ///< String was found. + char const *m_text; ///< Text value of the option. + bool m_found; ///< String was found. }; typedef std::vector CfgOpts; -# define N_OPTS(x) (sizeof(x) / sizeof(*x)) +#define N_OPTS(x) (sizeof(x) / sizeof(*x)) -CfgString FORWARD_OPTS[] = { { "gre" } , { "l2" } }; -size_t const N_FORWARD_OPTS = sizeof(FORWARD_OPTS)/sizeof(*FORWARD_OPTS); +CfgString FORWARD_OPTS[] = {{"gre"}, {"l2"}}; +size_t const N_FORWARD_OPTS = sizeof(FORWARD_OPTS) / sizeof(*FORWARD_OPTS); -CfgString RETURN_OPTS[] = { { "gre" } , { "l2" } }; -size_t const N_RETURN_OPTS = sizeof(RETURN_OPTS)/sizeof(*RETURN_OPTS); +CfgString RETURN_OPTS[] = {{"gre"}, {"l2"}}; +size_t const N_RETURN_OPTS = sizeof(RETURN_OPTS) / sizeof(*RETURN_OPTS); -CfgString ASSIGN_OPTS[] = { { "hash" } , { "mask" } }; +CfgString ASSIGN_OPTS[] = {{"hash"}, {"mask"}}; -CfgString HASH_OPTS[] = { { "src_ip" } , { "dst_ip" } , { "src_port" } , { "dst_port" } }; +CfgString HASH_OPTS[] = {{"src_ip"}, {"dst_ip"}, {"src_port"}, {"dst_port"}}; -ts::Errata::Code code_max(ts::Errata const& err) { +ts::Errata::Code +code_max(ts::Errata const &err) +{ ts::Errata::Code zret = std::numeric_limits::min(); ts::Errata::const_iterator spot = err.begin(); ts::Errata::const_iterator limit = err.end(); - for ( ; spot != limit ; ++spot ) + for (; spot != limit; ++spot) zret = std::max(zret, spot->getCode()); return zret; } struct ValueNamePrinter { - Value const& _v; - ValueNamePrinter(Value const& v) : _v(v) {} + Value const &_v; + ValueNamePrinter(Value const &v) : _v(v) {} }; -std::ostream& operator << ( std::ostream& out, ValueNamePrinter const& v ) { - ts::ConstBuffer const& name = v._v.getName(); - if (name._ptr) out << "'" << name << "'"; - else out << v._v.getIndex(); +std::ostream &operator<<(std::ostream &out, ValueNamePrinter const &v) +{ + ts::ConstBuffer const &name = v._v.getName(); + if (name._ptr) + out << "'" << name << "'"; + else + out << v._v.getIndex(); return out; } @@ -131,102 +140,93 @@ ts::Errata::Message File_Read_Error(char const* text) { } #endif -ts::Errata::Message Unable_To_Create_Service_Group(int line) { +ts::Errata::Message +Unable_To_Create_Service_Group(int line) +{ std::ostringstream out; - out << "Unable to create service group at line " << line - << " because of configuration errors." - ; + out << "Unable to create service group at line " << line << " because of configuration errors."; return ts::Errata::Message(23, LVL_FATAL, out.str()); } -ts::Errata::Message Services_Not_Found() { +ts::Errata::Message +Services_Not_Found() +{ return ts::Errata::Message(3, LVL_INFO, "No services found in configuration."); } -ts::Errata::Message Services_Not_A_Sequence() { +ts::Errata::Message +Services_Not_A_Sequence() +{ return ts::Errata::Message(4, LVL_INFO, "The 'services' setting was not a list nor array."); } -ts::Errata::Message Service_Not_A_Group(int line) { +ts::Errata::Message +Service_Not_A_Group(int line) +{ std::ostringstream out; - out << "'" << SVC_NAME << "' must be a group at line " - << line << "." - ; + out << "'" << SVC_NAME << "' must be a group at line " << line << "."; return ts::Errata::Message(5, LVL_WARN, out.str()); } -ts::Errata::Message Service_Type_Defaulted(wccp::ServiceGroup::Type type, int line) { +ts::Errata::Message +Service_Type_Defaulted(wccp::ServiceGroup::Type type, int line) +{ std::ostringstream out; - out << "'type' not found in " << SVC_NAME << " at line " - << line << "' -- defaulting to " - << ( type == wccp::ServiceGroup::STANDARD ? "STANDARD" : "DYNAMIC" ) - ; + out << "'type' not found in " << SVC_NAME << " at line " << line << "' -- defaulting to " + << (type == wccp::ServiceGroup::STANDARD ? "STANDARD" : "DYNAMIC"); return ts::Errata::Message(6, LVL_INFO, out.str()); } -ts::Errata::Message Service_Type_Invalid(ts::ConstBuffer const& text, int line) { +ts::Errata::Message +Service_Type_Invalid(ts::ConstBuffer const &text, int line) +{ std::ostringstream out; - out << "Service type '" << text - << "' at line " << line - << " invalid. Must be \"STANDARD\" or \"DYNAMIC\"" - ; + out << "Service type '" << text << "' at line " << line << " invalid. Must be \"STANDARD\" or \"DYNAMIC\""; return ts::Errata::Message(7, LVL_WARN, out.str()); } -ts::Errata::Message Prop_Not_Found(char const* prop_name, char const* group_name, int line) { +ts::Errata::Message +Prop_Not_Found(char const *prop_name, char const *group_name, int line) +{ std::ostringstream out; - out << "Required '" << prop_name << "' property not found in '" - << group_name << "' at line " << line << "." - ; + out << "Required '" << prop_name << "' property not found in '" << group_name << "' at line " << line << "."; return ts::Errata::Message(8, LVL_WARN, out.str()); } -ts::Errata::Message Prop_Invalid_Type( - Value const& prop_cfg, - ts::config::ValueType expected -) { +ts::Errata::Message +Prop_Invalid_Type(Value const &prop_cfg, ts::config::ValueType expected) +{ std::ostringstream out; - out << "'" << prop_cfg.getName() << "' at line " << prop_cfg.getSourceLine() - << " is of type '" << prop_cfg.getType() - << "' instead of required type '" << expected << "'." - ; + out << "'" << prop_cfg.getName() << "' at line " << prop_cfg.getSourceLine() << " is of type '" << prop_cfg.getType() + << "' instead of required type '" << expected << "'."; return ts::Errata::Message(9, LVL_WARN, out.str()); } -ts::Errata::Message Prop_List_Invalid_Type( - Value const& elt_cfg, ///< List element. - ts::config::ValueType expected -) { +ts::Errata::Message +Prop_List_Invalid_Type(Value const &elt_cfg, ///< List element. + ts::config::ValueType expected) +{ std::ostringstream out; - out << "Element " << ValueNamePrinter(elt_cfg) - << " at line " << elt_cfg.getSourceLine() - << " in the aggregate property '" << elt_cfg.getParent().getName() - << "' is of type '" << elt_cfg.getType() - << "' instead of required type '" << expected << "'." - ; + out << "Element " << ValueNamePrinter(elt_cfg) << " at line " << elt_cfg.getSourceLine() << " in the aggregate property '" + << elt_cfg.getParent().getName() << "' is of type '" << elt_cfg.getType() << "' instead of required type '" << expected + << "'."; return ts::Errata::Message(9, LVL_WARN, out.str()); } -ts::Errata::Message Svc_Prop_Out_Of_Range( - char const* name, - Value const& elt_cfg, - int v, int min, int max -) { +ts::Errata::Message +Svc_Prop_Out_Of_Range(char const *name, Value const &elt_cfg, int v, int min, int max) +{ std::ostringstream out; - out << "Service property '" << name - << "' at line " << elt_cfg.getSourceLine() - << " has a value " << v - << " that is not in the allowed range of " - << min << ".." << max << "." - ; + out << "Service property '" << name << "' at line " << elt_cfg.getSourceLine() << " has a value " << v + << " that is not in the allowed range of " << min << ".." << max << "."; return ts::Errata::Message(10, LVL_WARN, out.str()); } -ts::Errata::Message Svc_Prop_Ignored(char const* name, int line) { +ts::Errata::Message +Svc_Prop_Ignored(char const *name, int line) +{ std::ostringstream out; - out << "Service property '" << name << "' at line " << line - << " ignored because the service is of type standard." - ; + out << "Service property '" << name << "' at line " << line << " ignored because the service is of type standard."; return ts::Errata::Message(11, LVL_INFO, out.str()); } @@ -247,154 +247,144 @@ ts::Errata::Message Svc_Flags_Ignored(int line) { } #endif -ts::Errata::Message Svc_Ports_Too_Many(int line, int n) { +ts::Errata::Message +Svc_Ports_Too_Many(int line, int n) +{ std::ostringstream out; - out << "Excess ports ignored at line " << line - << ". " << n << " ports specified, only" - << wccp::ServiceGroup::N_PORTS << " supported." - ; + out << "Excess ports ignored at line " << line << ". " << n << " ports specified, only" << wccp::ServiceGroup::N_PORTS + << " supported."; return ts::Errata::Message(14, LVL_INFO, out.str()); } -ts::Errata::Message Svc_Ports_Malformed(int line) { +ts::Errata::Message +Svc_Ports_Malformed(int line) +{ std::ostringstream out; - out << "Port value ignored (not a number) at line " << line - << "." - ; + out << "Port value ignored (not a number) at line " << line << "."; return ts::Errata::Message(15, LVL_INFO, out.str()); } -ts::Errata::Message Svc_Ports_None_Valid(int line) { +ts::Errata::Message +Svc_Ports_None_Valid(int line) +{ std::ostringstream out; - out << "A '" << SVC_PROP_PORTS << "' property was found at line " - << line << " but none of the ports were valid." - ; - return ts::Errata::Message(17, LVL_WARN, out.str()); + out << "A '" << SVC_PROP_PORTS << "' property was found at line " << line << " but none of the ports were valid."; + return ts::Errata::Message(17, LVL_WARN, out.str()); } -ts::Errata::Message Svc_Ports_Not_Found(int line) { +ts::Errata::Message +Svc_Ports_Not_Found(int line) +{ std::ostringstream out; - out << "Ports not found in service at line " << line - << ". Ports must be defined for a dynamic service."; - return ts::Errata::Message(18, LVL_WARN, out.str()); + out << "Ports not found in service at line " << line << ". Ports must be defined for a dynamic service."; + return ts::Errata::Message(18, LVL_WARN, out.str()); } -ts::Errata::Message Svc_Prop_Ignored_In_Standard(const char* name, int line) { +ts::Errata::Message +Svc_Prop_Ignored_In_Standard(const char *name, int line) +{ std::ostringstream out; - out << "Service property '" << name << "' at line " << line - << " ignored because the service is of type STANDARD." - ; + out << "Service property '" << name << "' at line " << line << " ignored because the service is of type STANDARD."; return ts::Errata::Message(19, LVL_INFO, out.str()); } -ts::Errata::Message Security_Opt_Invalid(ts::ConstBuffer const& text, int line) { +ts::Errata::Message +Security_Opt_Invalid(ts::ConstBuffer const &text, int line) +{ std::ostringstream out; - out << "Security option '" << text - << "' at line " << line - << " is invalid. It must be 'none' or 'md5'." - ; + out << "Security option '" << text << "' at line " << line << " is invalid. It must be 'none' or 'md5'."; return ts::Errata::Message(20, LVL_WARN, out.str()); } -ts::Errata::Message Value_Malformed(char const* name, char const* text, int line) { +ts::Errata::Message +Value_Malformed(char const *name, char const *text, int line) +{ std::ostringstream out; - out << "'" << name << "' value '" << text - << "' malformed at line " << line << "." - ; + out << "'" << name << "' value '" << text << "' malformed at line " << line << "."; return ts::Errata::Message(21, LVL_WARN, out.str()); } -ts::Errata::Message No_Valid_Routers(int line) { +ts::Errata::Message +No_Valid_Routers(int line) +{ std::ostringstream out; - out << "No valid IP address for routers found for Service Group at line " - << line << "." - ; + out << "No valid IP address for routers found for Service Group at line " << line << "."; return ts::Errata::Message(22, LVL_WARN, out.str()); } -ts::Errata::Message Ignored_Option_Value( - ts::ConstBuffer const& text, - ts::ConstBuffer const& name, - int line -) { +ts::Errata::Message +Ignored_Option_Value(ts::ConstBuffer const &text, ts::ConstBuffer const &name, int line) +{ std::ostringstream out; - out << "Value '" << text << "' at line " << line - << " was ignored because it is not a valid option for '" - << name << "'." - ; + out << "Value '" << text << "' at line " << line << " was ignored because it is not a valid option for '" << name << "'."; return ts::Errata::Message(24, LVL_INFO, out.str()); } -ts::Errata::Message Ignored_Opt_Errors( - char const* name, - int line -) { +ts::Errata::Message +Ignored_Opt_Errors(char const *name, int line) +{ std::ostringstream out; - out << "Errors in '" << name << "' at line " << line - << " were ignored." - ; + out << "Errors in '" << name << "' at line " << line << " were ignored."; return ts::Errata::Message(28, LVL_INFO, out.str()); } -ts::Errata::Message List_Valid_Opts( - ts::ConstBuffer const& name, - int line, - CfgString* values, - size_t n -) { +ts::Errata::Message +List_Valid_Opts(ts::ConstBuffer const &name, int line, CfgString *values, size_t n) +{ std::ostringstream out; - out << "Valid values for the '" << name << "' property at line " << line - << " are: " - ; + out << "Valid values for the '" << name << "' property at line " << line << " are: "; out << '"' << values[0].m_text << '"'; - for ( size_t i = 1 ; i < n ; ++i ) + for (size_t i = 1; i < n; ++i) out << ", \"" << values[i].m_text << '"'; out << '.'; return ts::Errata::Message(29, LVL_INFO, out.str()); } -ts::Errata::Message Port_Type_Invalid(ts::ConstBuffer const& text, int line) { +ts::Errata::Message +Port_Type_Invalid(ts::ConstBuffer const &text, int line) +{ std::ostringstream out; - out << "Value '" << text - << "' at line " << line - << "for property '" << SVC_PROP_PORT_TYPE - << "' is invalid. It must be 'src' or 'dst'." - ; + out << "Value '" << text << "' at line " << line << "for property '" << SVC_PROP_PORT_TYPE + << "' is invalid. It must be 'src' or 'dst'."; return ts::Errata::Message(30, LVL_WARN, out.str()); } } // anon namespace -namespace wccp { - -inline bool operator == ( ts::ConstBuffer const& b, char const* text ) { +namespace wccp +{ +inline bool operator==(ts::ConstBuffer const &b, char const *text) +{ return 0 == strncasecmp(text, b._ptr, b._size); } -inline bool operator == ( char const* text, ts::ConstBuffer const& b ) { +inline bool operator==(char const *text, ts::ConstBuffer const &b) +{ return 0 == strncasecmp(text, b._ptr, b._size); } ts::Errata -load_option_set(Value const& setting, CfgString* opts, size_t count) { +load_option_set(Value const &setting, CfgString *opts, size_t count) +{ ts::Errata zret; - CfgString* spot; - CfgString* limit = opts + count; - ts::ConstBuffer const& name = setting.getName(); + CfgString *spot; + CfgString *limit = opts + count; + ts::ConstBuffer const &name = setting.getName(); int src_line = setting.getSourceLine(); // Clear all found flags. - for ( spot = opts ; spot < limit ; ++spot ) spot->m_found = false; + for (spot = opts; spot < limit; ++spot) + spot->m_found = false; // Walk through the strings in the setting. if (setting.isContainer()) { int nr = setting.childCount(); bool list_opts = false; - for ( int i = 0 ; i < nr ; ++i ) { + for (int i = 0; i < nr; ++i) { Value item = setting[i]; if (ts::config::StringValue == item.getType()) { ts::ConstBuffer text = item.getText(); - for ( spot = opts ; spot < limit ; ++spot ) { + for (spot = opts; spot < limit; ++spot) { if (spot->m_text == text) { spot->m_found = true; break; @@ -421,9 +411,9 @@ load_option_set(Value const& setting, CfgString* opts, size_t count) { the option was none and the pointer is @c NULL */ ts::Rv -load_security ( - Value const& setting ///< Security setting. -) { +load_security(Value const &setting ///< Security setting. + ) +{ ts::Rv zret; int src_line; ts::ConstBuffer text; @@ -465,24 +455,26 @@ load_security ( /// Process a router address list. ts::Errata -load_routers ( - Value const& setting, ///< Source of addresses. - std::vector& addrs ///< Output list -) { +load_routers(Value const &setting, ///< Source of addresses. + std::vector &addrs ///< Output list + ) +{ ts::Errata zret; - char const* text; - static char const * const NAME = "IPv4 Address"; + char const *text; + static char const *const NAME = "IPv4 Address"; if (setting.isContainer()) { int nr = setting.childCount(); - for ( int i = 0 ; i < nr ; ++i ) { - Value const& addr_cfg = setting[i]; + for (int i = 0; i < nr; ++i) { + Value const &addr_cfg = setting[i]; int addr_line = addr_cfg.getSourceLine(); in_addr addr; if (ts::config::StringValue == addr_cfg.getType()) { text = addr_cfg.getText()._ptr; - if (inet_aton(text, &addr)) addrs.push_back(addr.s_addr); - else zret.push(Value_Malformed(NAME, text, addr_line)); + if (inet_aton(text, &addr)) + addrs.push_back(addr.s_addr); + else + zret.push(Value_Malformed(NAME, text, addr_line)); } else { zret.push(Prop_List_Invalid_Type(addr_cfg, ts::config::StringValue)); } @@ -494,27 +486,33 @@ load_routers ( } ts::Errata -CacheImpl::loadServicesFromFile(char const* path) { +CacheImpl::loadServicesFromFile(char const *path) +{ ts::Errata zret; - int src_line = 0; // scratch for local source line caching. + int src_line = 0; // scratch for local source line caching. std::vector routers; // scratch per service loop. - Value prop; // scratch var. + Value prop; // scratch var. ts::Rv cv = Configuration::loadFromPath(path); - if (!cv.isOK()) return cv.errata(); + if (!cv.isOK()) + return cv.errata(); ts::config::Configuration cfg = cv.result(); Value svc_list = cfg.find("services"); // No point in going on from here. - if (!svc_list) return Services_Not_Found(); + if (!svc_list) + return Services_Not_Found(); - if (!svc_list.isContainer()) return Services_Not_A_Sequence(); + if (!svc_list.isContainer()) + return Services_Not_A_Sequence(); // Check for global (default) security setting. if ((prop = cfg[SVC_PROP_SECURITY]).hasValue()) { ts::Rv rv = load_security(prop); - if (rv.isOK()) this->useMD5Security(rv); - else zret.pull(rv.errata()); + if (rv.isOK()) + this->useMD5Security(rv); + else + zret.pull(rv.errata()); } if ((prop = cfg[SVC_PROP_ROUTERS]).hasValue()) { @@ -522,13 +520,13 @@ CacheImpl::loadServicesFromFile(char const* path) { } int idx, nsvc; - for ( idx = 0, nsvc = svc_list.childCount() ; idx < nsvc ; ++idx ) { + for (idx = 0, nsvc = svc_list.childCount(); idx < nsvc; ++idx) { int x; // scratch int. - char const* md5_key = 0; + char const *md5_key = 0; ts::ConstBuffer text; SecurityOption security_style = SECURITY_NONE; bool use_group_local_security = false; - Value const& svc_cfg = svc_list[idx]; + Value const &svc_cfg = svc_list[idx]; int svc_line = svc_cfg.getSourceLine(); ServiceGroup svc_info; @@ -566,11 +564,7 @@ CacheImpl::loadServicesFromFile(char const* path) { zret.push(Prop_Invalid_Type(prop, ts::config::StringValue)); } } else { // default type based on ID. - ServiceGroup::Type svc_type = - svc_info.getSvcId() <= ServiceGroup::RESERVED - ? ServiceGroup::STANDARD - : ServiceGroup::DYNAMIC - ; + ServiceGroup::Type svc_type = svc_info.getSvcId() <= ServiceGroup::RESERVED ? ServiceGroup::STANDARD : ServiceGroup::DYNAMIC; svc_info.setSvcType(svc_type); zret.push(Service_Type_Defaulted(svc_type, svc_line)); } @@ -615,11 +609,13 @@ CacheImpl::loadServicesFromFile(char const* path) { ts::Errata status = load_option_set(prop, HASH_OPTS, N_OPTS(HASH_OPTS)); uint32_t f = 0; src_line = prop.getSourceLine(); - for ( size_t i = 0 ; i < N_OPTS(HASH_OPTS) ; ++i ) - if (HASH_OPTS[i].m_found) f |= ServiceGroup::SRC_IP_HASH << i; + for (size_t i = 0; i < N_OPTS(HASH_OPTS); ++i) + if (HASH_OPTS[i].m_found) + f |= ServiceGroup::SRC_IP_HASH << i; if (f) { svc_info.enableFlags(f); - if (!status) zret.push(Ignored_Opt_Errors(SVC_PROP_PRIMARY_HASH, src_line).set(status)); + if (!status) + zret.push(Ignored_Opt_Errors(SVC_PROP_PRIMARY_HASH, src_line).set(status)); } else { zret.push(List_Valid_Opts(prop.getName(), src_line, HASH_OPTS, N_OPTS(HASH_OPTS)).set(status)); } @@ -631,17 +627,20 @@ CacheImpl::loadServicesFromFile(char const* path) { ts::Errata status = load_option_set(prop, HASH_OPTS, N_OPTS(HASH_OPTS)); uint32_t f = 0; src_line = prop.getSourceLine(); - for ( size_t i = 0 ; i < N_OPTS(HASH_OPTS) ; ++i ) - if (HASH_OPTS[i].m_found) f |= ServiceGroup::SRC_IP_ALT_HASH << i; - if (f) svc_info.enableFlags(f); - if (!status) zret.push(Ignored_Opt_Errors(SVC_PROP_ALT_HASH, src_line).set(status)); + for (size_t i = 0; i < N_OPTS(HASH_OPTS); ++i) + if (HASH_OPTS[i].m_found) + f |= ServiceGroup::SRC_IP_ALT_HASH << i; + if (f) + svc_info.enableFlags(f); + if (!status) + zret.push(Ignored_Opt_Errors(SVC_PROP_ALT_HASH, src_line).set(status)); } if ((prop = svc_cfg[SVC_PROP_PORT_TYPE]).hasValue()) { src_line = prop.getSourceLine(); if (ts::config::StringValue == prop.getType()) { text = prop.getText(); - if ("src" == text) + if ("src" == text) svc_info.enableFlags(ServiceGroup::PORTS_SOURCE); else if ("dst" == text) svc_info.disableFlags(ServiceGroup::PORTS_SOURCE); @@ -659,7 +658,7 @@ CacheImpl::loadServicesFromFile(char const* path) { if (ServiceGroup::STANDARD == svc_info.getSvcType()) { zret.push(Svc_Prop_Ignored_In_Standard(SVC_PROP_PORTS, src_line)); } else { - if ( prop.isContainer() ) { + if (prop.isContainer()) { size_t nport = prop.childCount(); size_t pidx, sidx; bool malformed_error = false; @@ -669,8 +668,8 @@ CacheImpl::loadServicesFromFile(char const* path) { nport = ServiceGroup::N_PORTS; } // Step through the ports. - for ( pidx = sidx = 0 ; pidx < nport ; ++pidx ) { - Value const& port_cfg = prop[pidx]; + for (pidx = sidx = 0; pidx < nport; ++pidx) { + Value const &port_cfg = prop[pidx]; if (ts::config::IntegerValue == port_cfg.getType()) { x = atoi(port_cfg.getText()._ptr); if (0 <= x && x <= 65535) @@ -682,8 +681,10 @@ CacheImpl::loadServicesFromFile(char const* path) { malformed_error = true; } } - if (sidx) svc_info.enableFlags(ServiceGroup::PORTS_DEFINED); - else zret.push(Svc_Ports_None_Valid(src_line)); + if (sidx) + svc_info.enableFlags(ServiceGroup::PORTS_DEFINED); + else + zret.push(Svc_Ports_None_Valid(src_line)); } else { zret.push(Prop_Invalid_Type(prop, ts::config::ListValue)); } @@ -725,22 +726,22 @@ CacheImpl::loadServicesFromFile(char const* path) { } // Properties after this are optional so we can proceed if they fail. - GroupData& svc = this->defineServiceGroup(svc_info); + GroupData &svc = this->defineServiceGroup(svc_info); // Is there a process we should track? if ((prop = svc_cfg[SVC_PROP_PROC]).hasValue()) { if (ts::config::StringValue == prop.getType()) { - svc.setProcName(prop.getText()); + svc.setProcName(prop.getText()); } else { zret.push(Prop_Invalid_Type(prop, ts::config::StringValue)); } - } + } // Add seed routers. std::vector::iterator rspot, rlimit; - for ( rspot = routers.begin(), rlimit = routers.end() ; rspot != rlimit ; ++rspot ) + for (rspot = routers.begin(), rlimit = routers.end(); rspot != rlimit; ++rspot) svc.seedRouter(*rspot); - for ( rspot = Seed_Router.begin(), rlimit = Seed_Router.end() ; rspot != rlimit ; ++rspot ) + for (rspot = Seed_Router.begin(), rlimit = Seed_Router.end(); rspot != rlimit; ++rspot) svc.seedRouter(*rspot); if (use_group_local_security) @@ -754,11 +755,7 @@ CacheImpl::loadServicesFromFile(char const* path) { bool gre = FORWARD_OPTS[0].m_found; bool l2 = FORWARD_OPTS[1].m_found; if (gre || l2) { - svc.m_packet_forward = - gre - ? l2 ? ServiceGroup::GRE_OR_L2 : ServiceGroup::GRE - : ServiceGroup::L2 - ; + svc.m_packet_forward = gre ? l2 ? ServiceGroup::GRE_OR_L2 : ServiceGroup::GRE : ServiceGroup::L2; if (!status.isOK()) zret.push(Ignored_Opt_Errors(SVC_PROP_FORWARD, prop.getSourceLine()).set(status)); } else { @@ -772,12 +769,9 @@ CacheImpl::loadServicesFromFile(char const* path) { bool gre = RETURN_OPTS[0].m_found; bool l2 = RETURN_OPTS[1].m_found; if (gre || l2) { - svc.m_packet_return = - gre - ? l2 ? ServiceGroup::GRE_OR_L2 : ServiceGroup::GRE - : ServiceGroup::L2 - ; - if (!status.isOK()) zret.push(Ignored_Opt_Errors(SVC_PROP_RETURN, prop.getSourceLine()).set(status)); + svc.m_packet_return = gre ? l2 ? ServiceGroup::GRE_OR_L2 : ServiceGroup::GRE : ServiceGroup::L2; + if (!status.isOK()) + zret.push(Ignored_Opt_Errors(SVC_PROP_RETURN, prop.getSourceLine()).set(status)); } else { zret.push(ts::Errata::Message(26, LVL_INFO, "Defaulting to GRE return.").set(status)); } @@ -789,12 +783,9 @@ CacheImpl::loadServicesFromFile(char const* path) { bool hash = ASSIGN_OPTS[0].m_found; bool mask = ASSIGN_OPTS[1].m_found; if (hash || mask) { - svc.m_cache_assign = - hash - ? mask ? ServiceGroup::HASH_OR_MASK : ServiceGroup::HASH_ONLY - : ServiceGroup::MASK_ONLY - ; - if (!status.isOK()) zret.push(Ignored_Opt_Errors(SVC_PROP_ASSIGN, prop.getSourceLine()).set(status)); + svc.m_cache_assign = hash ? mask ? ServiceGroup::HASH_OR_MASK : ServiceGroup::HASH_ONLY : ServiceGroup::MASK_ONLY; + if (!status.isOK()) + zret.push(Ignored_Opt_Errors(SVC_PROP_ASSIGN, prop.getSourceLine()).set(status)); } else { status.push(ts::Errata::Message(26, LVL_INFO, "Defaulting to hash assignment only.")); zret.push(List_Valid_Opts(prop.getName(), src_line, ASSIGN_OPTS, N_OPTS(ASSIGN_OPTS)).set(status)); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/wccp/WccpEndPoint.cc ---------------------------------------------------------------------- diff --git a/lib/wccp/WccpEndPoint.cc b/lib/wccp/WccpEndPoint.cc index 2426bc1..41bfe16 100644 --- a/lib/wccp/WccpEndPoint.cc +++ b/lib/wccp/WccpEndPoint.cc @@ -19,61 +19,62 @@ limitations under the License. */ -# include "WccpLocal.h" -# include "WccpUtil.h" -# include "WccpMeta.h" -# include -# include "ink_string.h" -# include "ink_defs.h" +#include "WccpLocal.h" +#include "WccpUtil.h" +#include "WccpMeta.h" +#include +#include "ink_string.h" +#include "ink_defs.h" // ------------------------------------------------------ -namespace wccp { - +namespace wccp +{ #if defined IP_RECVDSTADDR -# define DSTADDR_SOCKOPT IP_RECVDSTADDR -# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_addr))) -# define dstaddr(x) (CMSG_DATA(x)) +#define DSTADDR_SOCKOPT IP_RECVDSTADDR +#define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_addr))) +#define dstaddr(x) (CMSG_DATA(x)) #elif defined IP_PKTINFO -# define DSTADDR_SOCKOPT IP_PKTINFO -# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_pktinfo))) -# define dstaddr(x) (&(((struct in_pktinfo *)(CMSG_DATA(x)))->ipi_addr)) +#define DSTADDR_SOCKOPT IP_PKTINFO +#define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_pktinfo))) +#define dstaddr(x) (&(((struct in_pktinfo *)(CMSG_DATA(x)))->ipi_addr)) #else -# error "can't determine socket option" +#error "can't determine socket option" #endif // ------------------------------------------------------ -Impl::GroupData::GroupData() - : m_generation(0) - , m_use_security_opt(false) - , m_use_security_key(false) { +Impl::GroupData::GroupData() : m_generation(0), m_use_security_opt(false), m_use_security_key(false) +{ } -Impl::GroupData& -Impl::GroupData::setKey(char const* key) { +Impl::GroupData & +Impl::GroupData::setKey(char const *key) +{ m_use_security_key = true; strncpy(m_security_key, key, SecurityComp::KEY_SIZE); return *this; } -Impl::GroupData& -Impl::GroupData::setSecurity(SecurityOption style) { +Impl::GroupData & +Impl::GroupData::setSecurity(SecurityOption style) +{ m_use_security_opt = true; m_security_opt = style; return *this; } -Impl::Impl() - : m_addr(INADDR_ANY) - , m_fd(ts::NO_FD) { +Impl::Impl() : m_addr(INADDR_ANY), m_fd(ts::NO_FD) +{ } -Impl::~Impl() { +Impl::~Impl() +{ this->close(); } int -Impl::open(uint addr) { +Impl::open(uint addr) +{ struct sockaddr saddr; - sockaddr_in& in_addr = reinterpret_cast(saddr); + sockaddr_in &in_addr = reinterpret_cast(saddr); int fd; if (ts::NO_FD != m_fd) { @@ -86,7 +87,8 @@ Impl::open(uint addr) { return -errno; } - if (INADDR_ANY != addr) m_addr = addr; // overridden. + if (INADDR_ANY != addr) + m_addr = addr; // overridden. memset(&saddr, 0, sizeof(saddr)); in_addr.sin_family = AF_INET; in_addr.sin_port = htons(DEFAULT_PORT); @@ -115,7 +117,7 @@ Impl::open(uint addr) { return -errno; } -# if defined IP_MTU_DISCOVER +#if defined IP_MTU_DISCOVER /// Disable PMTU on Linux because of a bug in IOS routers. /// WCCP packets are rejected as duplicates if the IP fragment /// identifier is 0, which is the value used when PMTU is enabled. @@ -125,14 +127,15 @@ Impl::open(uint addr) { this->close(); return -errno; } -# endif +#endif m_fd = fd; return 0; } void -Impl::close() { +Impl::close() +{ if (ts::NO_FD != m_fd) { ::close(m_fd); m_fd = ts::NO_FD; @@ -140,7 +143,8 @@ Impl::close() { } void -Impl::useMD5Security(ts::ConstBuffer const& key) { +Impl::useMD5Security(ts::ConstBuffer const &key) +{ m_use_security_opt = true; m_security_opt = SECURITY_MD5; m_use_security_key = true; @@ -150,35 +154,46 @@ Impl::useMD5Security(ts::ConstBuffer const& key) { } SecurityOption -Impl::setSecurity(BaseMsg& msg, GroupData const& group) const { +Impl::setSecurity(BaseMsg &msg, GroupData const &group) const +{ SecurityOption zret = SECURITY_NONE; - if (group.m_use_security_opt) zret = group.m_security_opt; - else if (m_use_security_opt) zret = m_security_opt; - if (group.m_use_security_key) msg.m_security.setKey(group.m_security_key); - else if (m_use_security_key) msg.m_security.setKey(m_security_key); + if (group.m_use_security_opt) + zret = group.m_security_opt; + else if (m_use_security_opt) + zret = m_security_opt; + if (group.m_use_security_key) + msg.m_security.setKey(group.m_security_key); + else if (m_use_security_key) + msg.m_security.setKey(m_security_key); return zret; } bool -Impl::validateSecurity(BaseMsg& msg, GroupData const& group) { +Impl::validateSecurity(BaseMsg &msg, GroupData const &group) +{ SecurityOption opt = msg.m_security.getOption(); if (group.m_use_security_opt) { - if (opt != group.m_security_opt) return false; + if (opt != group.m_security_opt) + return false; } else if (m_use_security_opt) { - if (opt != m_security_opt) return false; + if (opt != m_security_opt) + return false; } if (opt == SECURITY_MD5) { - if (group.m_use_security_key) msg.m_security.setKey(group.m_security_key); - else if (m_use_security_key) msg.m_security.setKey(m_security_key); + if (group.m_use_security_key) + msg.m_security.setKey(group.m_security_key); + else if (m_use_security_key) + msg.m_security.setKey(m_security_key); return msg.validateSecurity(); } return true; } ts::Rv -Impl::handleMessage() { +Impl::handleMessage() +{ ts::Rv zret; - ssize_t n; // recv byte count. + ssize_t n; // recv byte count. struct sockaddr src_addr; // sender's address. msghdr recv_hdr; iovec recv_buffer; @@ -188,7 +203,8 @@ Impl::handleMessage() { static size_t const ANC_BUFFER_SIZE = DSTADDR_DATASIZE; char anc_buffer[ANC_BUFFER_SIZE]; - if (ts::NO_FD == m_fd) return -ENOTCONN; + if (ts::NO_FD == m_fd) + return -ENOTCONN; recv_buffer.iov_base = buffer; recv_buffer.iov_len = BUFFER_SIZE; @@ -201,34 +217,42 @@ Impl::handleMessage() { recv_hdr.msg_controllen = ANC_BUFFER_SIZE; n = recvmsg(m_fd, &recv_hdr, MSG_TRUNC); - if (n > BUFFER_SIZE) return -EMSGSIZE; - else if (n < 0) return -errno; + if (n > BUFFER_SIZE) + return -EMSGSIZE; + else if (n < 0) + return -errno; // Extract the original destination address. ip_header.m_src = access_field(&sockaddr_in::sin_addr, &src_addr).s_addr; - for ( cmsghdr* anc = CMSG_FIRSTHDR(&recv_hdr); - anc; - anc = CMSG_NXTHDR(&recv_hdr, anc) - ) { + for (cmsghdr *anc = CMSG_FIRSTHDR(&recv_hdr); anc; anc = CMSG_NXTHDR(&recv_hdr, anc)) { if (anc->cmsg_level == IPPROTO_IP && anc->cmsg_type == DSTADDR_SOCKOPT) { - ip_header.m_dst = ((struct in_addr*)dstaddr(anc))->s_addr; + ip_header.m_dst = ((struct in_addr *)dstaddr(anc))->s_addr; break; } } // Check to see if there is a valid header. MsgHeaderComp header; - MsgBuffer msg_buffer(buffer,n); + MsgBuffer msg_buffer(buffer, n); if (PARSE_SUCCESS == header.parse(msg_buffer)) { message_type_t msg_type = header.getType(); - ts::Buffer chunk(buffer,n); + ts::Buffer chunk(buffer, n); switch (msg_type) { - case HERE_I_AM: this->handleHereIAm(ip_header, chunk); break; - case I_SEE_YOU: this->handleISeeYou(ip_header, chunk); break; - case REDIRECT_ASSIGN: this->handleRedirectAssign(ip_header, chunk); break; - case REMOVAL_QUERY: this->handleRemovalQuery(ip_header, chunk); break; - default: fprintf(stderr, "Unknown message type %d ignored.\n", msg_type); + case HERE_I_AM: + this->handleHereIAm(ip_header, chunk); + break; + case I_SEE_YOU: + this->handleISeeYou(ip_header, chunk); + break; + case REDIRECT_ASSIGN: + this->handleRedirectAssign(ip_header, chunk); + break; + case REMOVAL_QUERY: + this->handleRemovalQuery(ip_header, chunk); + break; + default: + fprintf(stderr, "Unknown message type %d ignored.\n", msg_type); break; }; } else { @@ -238,28 +262,33 @@ Impl::handleMessage() { } ts::Errata -Impl::handleHereIAm(IpHeader const&, ts::Buffer const&) { +Impl::handleHereIAm(IpHeader const &, ts::Buffer const &) +{ return log(LVL_INFO, "Unanticipated WCCP2_HERE_I_AM message ignored"); } ts::Errata -Impl::handleISeeYou(IpHeader const&, ts::Buffer const& /* data ATS_UNUSED */) { +Impl::handleISeeYou(IpHeader const &, ts::Buffer const & /* data ATS_UNUSED */) +{ return log(LVL_INFO, "Unanticipated WCCP2_I_SEE_YOU message ignored."); } ts::Errata -Impl::handleRedirectAssign(IpHeader const&, ts::Buffer const& /* data ATS_UNUSED */) { +Impl::handleRedirectAssign(IpHeader const &, ts::Buffer const & /* data ATS_UNUSED */) +{ return log(LVL_INFO, "Unanticipated WCCP2_REDIRECT_ASSIGN message ignored."); } ts::Errata -Impl::handleRemovalQuery(IpHeader const&, ts::Buffer const& /* data ATS_UNUSED */) { +Impl::handleRemovalQuery(IpHeader const &, ts::Buffer const & /* data ATS_UNUSED */) +{ return log(LVL_INFO, "Unanticipated WCCP2_REMOVAL_QUERY message ignored."); } // ------------------------------------------------------ -CacheImpl::GroupData::GroupData() - : m_proc_name(NULL), m_assignment_pending(false) { +CacheImpl::GroupData::GroupData() : m_proc_name(NULL), m_assignment_pending(false) +{ } -CacheImpl::GroupData& -CacheImpl::GroupData::seedRouter(uint32_t addr) { +CacheImpl::GroupData & +CacheImpl::GroupData::seedRouter(uint32_t addr) +{ // Be nice and don't add it if it's already there. if (m_seed_routers.end() == find_by_member(m_seed_routers, &SeedRouter::m_addr, addr)) m_seed_routers.push_back(SeedRouter(addr)); @@ -267,12 +296,12 @@ CacheImpl::GroupData::seedRouter(uint32_t addr) { } time_t -CacheImpl::GroupData::removeSeedRouter(uint32_t addr) { +CacheImpl::GroupData::removeSeedRouter(uint32_t addr) +{ time_t zret = 0; std::vector::iterator begin = m_seed_routers.begin(); std::vector::iterator end = m_seed_routers.end(); - std::vector::iterator spot = - std::find_if(begin, end, ts::predicate(&SeedRouter::m_addr, addr)); + std::vector::iterator spot = std::find_if(begin, end, ts::predicate(&SeedRouter::m_addr, addr)); if (end != spot) { zret = spot->m_xmit; @@ -282,80 +311,73 @@ CacheImpl::GroupData::removeSeedRouter(uint32_t addr) { return zret; } -CacheImpl::GroupData& CacheImpl::GroupData::setKey(char const* key) { return static_cast(this->super::setKey(key)); } +CacheImpl::GroupData & +CacheImpl::GroupData::setKey(char const *key) +{ + return static_cast(this->super::setKey(key)); +} -CacheImpl::GroupData& CacheImpl::GroupData::setSecurity(SecurityOption style) { return static_cast(this->super::setSecurity(style)); } +CacheImpl::GroupData & +CacheImpl::GroupData::setSecurity(SecurityOption style) +{ + return static_cast(this->super::setSecurity(style)); +} CacheImpl::CacheBag::iterator -CacheImpl::GroupData::findCache(uint32_t addr) { - return std::find_if( - m_caches.begin(), - m_caches.end(), - ts::predicate(&CacheData::idAddr, addr) - ); +CacheImpl::GroupData::findCache(uint32_t addr) +{ + return std::find_if(m_caches.begin(), m_caches.end(), ts::predicate(&CacheData::idAddr, addr)); } CacheImpl::RouterBag::iterator -CacheImpl::GroupData::findRouter(uint32_t addr) { - return std::find_if( - m_routers.begin(), - m_routers.end(), - ts::predicate(&RouterData::m_addr, addr) - ); +CacheImpl::GroupData::findRouter(uint32_t addr) +{ + return std::find_if(m_routers.begin(), m_routers.end(), ts::predicate(&RouterData::m_addr, addr)); } void -CacheImpl::GroupData::resizeCacheSources() { +CacheImpl::GroupData::resizeCacheSources() +{ int count = m_routers.size(); - for ( CacheBag::iterator spot = m_caches.begin(), - limit = m_caches.end(); - spot != limit; - ++spot - ) { + for (CacheBag::iterator spot = m_caches.begin(), limit = m_caches.end(); spot != limit; ++spot) { spot->m_src.resize(count); } } -inline CacheImpl::RouterData::RouterData() - : m_addr(0) - , m_generation(0) - , m_rapid(0) - , m_assign(false) - , m_send_caps(false) { +inline CacheImpl::RouterData::RouterData() : m_addr(0), m_generation(0), m_rapid(0), m_assign(false), m_send_caps(false) +{ } inline CacheImpl::RouterData::RouterData(uint32_t addr) - : m_addr(addr) - , m_generation(0) - , m_rapid(0) - , m_assign(false) - , m_send_caps(false) { + : m_addr(addr), m_generation(0), m_rapid(0), m_assign(false), m_send_caps(false) +{ } time_t -CacheImpl::RouterData::pingTime(time_t now) const { - time_t tx = m_xmit.m_time + (m_rapid ? TIME_UNIT/10 : TIME_UNIT); +CacheImpl::RouterData::pingTime(time_t now) const +{ + time_t tx = m_xmit.m_time + (m_rapid ? TIME_UNIT / 10 : TIME_UNIT); return tx < now ? 0 : tx - now; } time_t -CacheImpl::RouterData::waitTime(time_t now) const { +CacheImpl::RouterData::waitTime(time_t now) const +{ return m_assign ? 0 : this->pingTime(now); } inline uint32_t -CacheImpl::CacheData::idAddr() const { +CacheImpl::CacheData::idAddr() const +{ return m_id.getAddr(); } -CacheImpl::GroupData& -CacheImpl::defineServiceGroup( - ServiceGroup const& svc, - ServiceGroup::Result* result -) { +CacheImpl::GroupData & +CacheImpl::defineServiceGroup(ServiceGroup const &svc, ServiceGroup::Result *result) +{ uint8_t svc_id = svc.getSvcId(); GroupMap::iterator spot = m_groups.find(svc_id); - GroupData* group; // service with target ID. + GroupData *group; // service with target ID. ServiceGroup::Result zret; if (spot == m_groups.end()) { // not defined group = &(m_groups[svc_id]); @@ -367,44 +389,44 @@ CacheImpl::defineServiceGroup( group = &spot->second; zret = group->m_svc == svc ? ServiceGroup::EXISTS : ServiceGroup::CONFLICT; } - if (result) *result = zret; + if (result) + *result = zret; return *group; } time_t -CacheImpl::GroupData::waitTime(time_t now) const { +CacheImpl::GroupData::waitTime(time_t now) const +{ time_t zret = std::numeric_limits::max(); // Active routers. - for ( RouterBag::const_iterator router = m_routers.begin(), - router_limit = m_routers.end() ; - router != router_limit && zret; - ++router - ) { + for (RouterBag::const_iterator router = m_routers.begin(), router_limit = m_routers.end(); router != router_limit && zret; + ++router) { zret = std::min(zret, router->waitTime(now)); - } + } // Seed routers. - for ( std::vector::const_iterator - router = m_seed_routers.begin(), - router_limit = m_seed_routers.end() ; - router != router_limit && zret; - ++router - ) { + for (std::vector::const_iterator router = m_seed_routers.begin(), router_limit = m_seed_routers.end(); + router != router_limit && zret; ++router) { time_t tx = router->m_xmit + TIME_UNIT; - if (tx < now) zret = 0; - else zret = std::min(tx - now, zret); + if (tx < now) + zret = 0; + else + zret = std::min(tx - now, zret); } // Assignment if (m_assignment_pending) { - time_t tx = m_generation_time + ( 3 * TIME_UNIT / 2 ); - if (tx < now) zret = 0; - else zret = std::min(tx - now, zret); + time_t tx = m_generation_time + (3 * TIME_UNIT / 2); + if (tx < now) + zret = 0; + else + zret = std::min(tx - now, zret); } return zret; } bool -CacheImpl::GroupData::processUp() { +CacheImpl::GroupData::processUp() +{ bool zret = false; const char *proc_pid_path = this->getProcName(); if (proc_pid_path == NULL || proc_pid_path[0] == '\0') { @@ -414,7 +436,7 @@ CacheImpl::GroupData::processUp() { int fd = open(proc_pid_path, O_RDONLY); if (fd > 0) { char buffer[256]; - ssize_t read_count = read(fd, buffer, sizeof(buffer)-1); + ssize_t read_count = read(fd, buffer, sizeof(buffer) - 1); close(fd); if (read_count > 0) { buffer[read_count] = '\0'; @@ -425,7 +447,7 @@ CacheImpl::GroupData::processUp() { fd = open(buffer, O_RDONLY); if (fd > 0) { zret = true; - close(fd); + close(fd); } } } @@ -435,25 +457,23 @@ CacheImpl::GroupData::processUp() { } bool -CacheImpl::GroupData::cullRouters(time_t now) { +CacheImpl::GroupData::cullRouters(time_t now) +{ bool zret = false; size_t idx = 0, n = m_routers.size(); - while ( idx < n ) { - RouterData& router = m_routers[idx]; + while (idx < n) { + RouterData &router = m_routers[idx]; if (router.m_recv.m_time + TIME_UNIT * 3 < now) { uint32_t addr = router.m_addr; // Clip the router by copying down and resizing. // Must do all caches as well. --n; // Decrement router counter first. - if (idx < n) router = m_routers[n]; + if (idx < n) + router = m_routers[n]; m_routers.resize(n); - for ( CacheBag::iterator - cache = m_caches.begin(), - cache_limit = m_caches.end(); - cache != cache_limit; - ++cache - ) { - if (idx < n) cache->m_src[idx] = cache->m_src[n]; + for (CacheBag::iterator cache = m_caches.begin(), cache_limit = m_caches.end(); cache != cache_limit; ++cache) { + if (idx < n) + cache->m_src[idx] = cache->m_src[n]; cache->m_src.resize(n); } // Put it back in the seeds. @@ -464,12 +484,14 @@ CacheImpl::GroupData::cullRouters(time_t now) { ++idx; // move to next router. } } - if (zret) this->viewChanged(now); + if (zret) + this->viewChanged(now); return zret; } -CacheImpl::GroupData& -CacheImpl::GroupData::viewChanged(time_t now) { +CacheImpl::GroupData & +CacheImpl::GroupData::viewChanged(time_t now) +{ m_generation += 1; m_generation_time = now; m_assign_info.setActive(false); // invalidate current assignment. @@ -481,40 +503,43 @@ CacheImpl::GroupData::viewChanged(time_t now) { return *this; } -Cache::Service& -Cache::Service::setKey(char const* key) { +Cache::Service & +Cache::Service::setKey(char const *key) +{ m_group->setKey(key); return *this; } -Cache::Service& -Cache::Service::setSecurity(SecurityOption opt) { +Cache::Service & +Cache::Service::setSecurity(SecurityOption opt) +{ m_group->setSecurity(opt); return *this; } -CacheImpl& -CacheImpl::seedRouter(uint8_t id, uint32_t addr) { +CacheImpl & +CacheImpl::seedRouter(uint8_t id, uint32_t addr) +{ GroupMap::iterator spot = m_groups.find(id); - if (spot != m_groups.end()) spot->second.seedRouter(addr); + if (spot != m_groups.end()) + spot->second.seedRouter(addr); return *this; } bool -CacheImpl::isConfigured() const { +CacheImpl::isConfigured() const +{ return INADDR_ANY != m_addr && m_groups.size() > 0; } int -CacheImpl::open(uint32_t addr) { +CacheImpl::open(uint32_t addr) +{ int zret = this->super::open(addr); // If the socket was successfully opened, go through the // services and update the local service descriptor. if (0 <= zret) { - for ( GroupMap::iterator spot = m_groups.begin(), limit = m_groups.end(); - spot != limit; - ++spot - ) { + for (GroupMap::iterator spot = m_groups.begin(), limit = m_groups.end(); spot != limit; ++spot) { spot->second.m_id.setAddr(m_addr); } } @@ -522,26 +547,22 @@ CacheImpl::open(uint32_t addr) { } time_t -CacheImpl::waitTime() const { +CacheImpl::waitTime() const +{ time_t now = time(0); return ts::minima(m_groups, &GroupData::waitTime, now); } void -CacheImpl::generateHereIAm( - HereIAmMsg& msg, - GroupData& group -) { +CacheImpl::generateHereIAm(HereIAmMsg &msg, GroupData &group) +{ msg.fill(group, group.m_id, this->setSecurity(msg, group)); msg.finalize(); } void -CacheImpl::generateHereIAm( - HereIAmMsg& msg, - GroupData& group, - RouterData& router -) { +CacheImpl::generateHereIAm(HereIAmMsg &msg, GroupData &group, RouterData &router) +{ SecurityOption sec_opt = this->setSecurity(msg, group); msg.fill(group, group.m_id, sec_opt); @@ -553,32 +574,26 @@ CacheImpl::generateHereIAm( } void -CacheImpl::generateRedirectAssign( - RedirectAssignMsg& msg, - GroupData& group -) { +CacheImpl::generateRedirectAssign(RedirectAssignMsg &msg, GroupData &group) +{ msg.fill(group, this->setSecurity(msg, group)); msg.finalize(); } ts::Errata -CacheImpl::checkRouterAssignment( - GroupData const& group, - RouterViewComp const& comp -) const { - detail::Assignment const& ainfo = group.m_assign_info; +CacheImpl::checkRouterAssignment(GroupData const &group, RouterViewComp const &comp) const +{ + detail::Assignment const &ainfo = group.m_assign_info; // If group doesn't have an active assignment, always match w/o checking. ts::Errata zret; // default is success. // if active assignment and data we can check, then check. - if (ainfo.isActive() && ! comp.isEmpty()) { + if (ainfo.isActive() && !comp.isEmpty()) { // Validate the assignment key. - if (ainfo.getKey().getAddr() != comp.getKeyAddr() - || ainfo.getKey().getChangeNumber() != comp.getKeyChangeNumber() - ) { - log(zret, LVL_INFO, "Router assignment key did not match.");; - } else if (ServiceGroup::HASH_ONLY == group.m_cache_assign - ) { + if (ainfo.getKey().getAddr() != comp.getKeyAddr() || ainfo.getKey().getChangeNumber() != comp.getKeyChangeNumber()) { + log(zret, LVL_INFO, "Router assignment key did not match."); + ; + } else if (ServiceGroup::HASH_ONLY == group.m_cache_assign) { // Still not sure how much checking we really want or should // do here. For now, we'll just leave the checks validating // the assignment key. @@ -592,10 +607,11 @@ CacheImpl::checkRouterAssignment( } int -CacheImpl::housekeeping() { +CacheImpl::housekeeping() +{ int zret = 0; sockaddr_in dst_addr; - sockaddr* addr_ptr = reinterpret_cast(&dst_addr); + sockaddr *addr_ptr = reinterpret_cast(&dst_addr); time_t now = time(0); static size_t const BUFFER_SIZE = 4096; MsgBuffer msg_buffer; @@ -608,24 +624,15 @@ CacheImpl::housekeeping() { dst_addr.sin_port = htons(DEFAULT_PORT); // Walk the service groups and do their housekeeping. - for ( GroupMap::iterator - svc_spot = m_groups.begin(), - svc_limit = m_groups.end(); - svc_spot != svc_limit; - ++svc_spot - ) { - GroupData& group = svc_spot->second; + for (GroupMap::iterator svc_spot = m_groups.begin(), svc_limit = m_groups.end(); svc_spot != svc_limit; ++svc_spot) { + GroupData &group = svc_spot->second; // Check to see if it's time for an assignment. - if (group.m_assignment_pending - && group.m_generation_time + ASSIGN_WAIT <= now - ) { + if (group.m_assignment_pending && group.m_generation_time + ASSIGN_WAIT <= now) { // Is a valid assignment possible? if (group.m_assign_info.fill(group, m_addr)) { group.m_assign_info.setActive(true); - ts::for_each(group.m_routers, - ts::assign_member(&RouterData::m_assign, true) - ); + ts::for_each(group.m_routers, ts::assign_member(&RouterData::m_assign, true)); } // Always clear because no point in sending an assign we can't generate. @@ -637,11 +644,7 @@ CacheImpl::housekeeping() { // Check to see if the related service is up if (group.processUp()) { // Check the active routers for scheduled packets. - for ( RouterBag::iterator rspot = group.m_routers.begin(), - rend = group.m_routers.end() ; - rspot != rend ; - ++rspot - ) { + for (RouterBag::iterator rspot = group.m_routers.begin(), rend = group.m_routers.end(); rspot != rend; ++rspot) { dst_addr.sin_addr.s_addr = rspot->m_addr; if (0 == rspot->pingTime(now)) { HereIAmMsg here_i_am; @@ -651,13 +654,10 @@ CacheImpl::housekeeping() { if (0 <= zret) { rspot->m_xmit.set(now, group.m_generation); rspot->m_send_caps = false; - logf(LVL_DEBUG, "Sent HERE_I_AM for service group %d to router %s%s[#%d,%lu].", - group.m_svc.getSvcId(), - ip_addr_to_str(rspot->m_addr), - rspot->m_rapid ? " [rapid] " : " ", - group.m_generation, now - ); - if (rspot->m_rapid) --(rspot->m_rapid); + logf(LVL_DEBUG, "Sent HERE_I_AM for service group %d to router %s%s[#%d,%lu].", group.m_svc.getSvcId(), + ip_addr_to_str(rspot->m_addr), rspot->m_rapid ? " [rapid] " : " ", group.m_generation, now); + if (rspot->m_rapid) + --(rspot->m_rapid); } else { logf_errno(LVL_WARN, "Failed to send to router " ATS_IP_PRINTF_CODE " - ", ATS_IP_OCTETS(rspot->m_addr)); } @@ -666,46 +666,35 @@ CacheImpl::housekeeping() { redirect_assign.setBuffer(msg_buffer); this->generateRedirectAssign(redirect_assign, group); zret = sendto(m_fd, msg_data, redirect_assign.getCount(), 0, addr_ptr, sizeof(dst_addr)); - if (0 <= zret) rspot->m_assign = false; + if (0 <= zret) + rspot->m_assign = false; } } } // Seed routers. - for ( std::vector::iterator - sspot = group.m_seed_routers.begin(), - slimit = group.m_seed_routers.end() ; - sspot != slimit ; - ++sspot - ) { + for (std::vector::iterator sspot = group.m_seed_routers.begin(), slimit = group.m_seed_routers.end(); + sspot != slimit; ++sspot) { // Check to see if the related service is up if (group.processUp()) { HereIAmMsg here_i_am; here_i_am.setBuffer(msg_buffer); // Is the router due for a ping? - if (sspot->m_xmit + TIME_UNIT > now) continue; // no + if (sspot->m_xmit + TIME_UNIT > now) + continue; // no this->generateHereIAm(here_i_am, group); dst_addr.sin_addr.s_addr = sspot->m_addr; - zret = sendto(m_fd, msg_data, here_i_am.getCount(), 0, - addr_ptr, sizeof(dst_addr)); + zret = sendto(m_fd, msg_data, here_i_am.getCount(), 0, addr_ptr, sizeof(dst_addr)); if (0 <= zret) { - logf(LVL_DEBUG, "Sent HERE_I_AM for SG %d to seed router %s [gen=#%d,t=%lu,n=%lu].", - group.m_svc.getSvcId(), - ip_addr_to_str(sspot->m_addr), - group.m_generation, now, here_i_am.getCount() - ); + logf(LVL_DEBUG, "Sent HERE_I_AM for SG %d to seed router %s [gen=#%d,t=%lu,n=%lu].", group.m_svc.getSvcId(), + ip_addr_to_str(sspot->m_addr), group.m_generation, now, here_i_am.getCount()); sspot->m_xmit = now; sspot->m_count += 1; - } - else logf(LVL_DEBUG, - "Error [%d:%s] sending HERE_I_AM for SG %d to seed router %s [#%d,%lu].", - zret, strerror(errno), - group.m_svc.getSvcId(), - ip_addr_to_str(sspot->m_addr), - group.m_generation, now - ); + } else + logf(LVL_DEBUG, "Error [%d:%s] sending HERE_I_AM for SG %d to seed router %s [#%d,%lu].", zret, strerror(errno), + group.m_svc.getSvcId(), ip_addr_to_str(sspot->m_addr), group.m_generation, now); } } } @@ -713,7 +702,8 @@ CacheImpl::housekeeping() { } ts::Errata -CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer const& chunk) { +CacheImpl::handleISeeYou(IpHeader const & /* ip_hdr ATS_UNUSED */, ts::Buffer const &chunk) +{ ts::Errata zret; ISeeYouMsg msg; // Set if our view of the group changes enough to bump the @@ -730,7 +720,7 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con if (spot == m_groups.end()) return logf(LVL_INFO, "WCCP2_I_SEE_YOU ignored - service group %d not found.", svc.getSvcId()); - GroupData& group = spot->second; + GroupData &group = spot->second; if (!this->validateSecurity(msg, group)) return log(LVL_INFO, "Ignored WCCP2_I_SEE_YOU with invalid security.\n"); @@ -749,10 +739,10 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con uint32_t to_addr = msg.m_router_id.getToAddr(); uint32_t recv_id = msg.m_router_id.idElt().getRecvId(); RouterBag::iterator ar_spot; // active router - int router_idx; // index in active routers. - std::vector< SeedRouter >::iterator seed_spot; + int router_idx; // index in active routers. + std::vector::iterator seed_spot; - CapComp& caps = msg.m_capabilities; + CapComp &caps = msg.m_capabilities; // Handle the router that sent us this. ar_spot = find_by_member(group.m_routers, &RouterData::m_addr, router_addr); if (ar_spot == group.m_routers.end()) { @@ -768,7 +758,7 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con // Validate capabilities. ServiceGroup::PacketStyle ps; ServiceGroup::CacheAssignmentStyle as; - char const* caps_tag = caps.isEmpty() ? "default" : "router"; + char const *caps_tag = caps.isEmpty() ? "default" : "router"; // No caps -> use GRE forwarding. ps = caps.isEmpty() ? ServiceGroup::GRE : caps.getPacketForwardStyle(); @@ -814,11 +804,9 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con ts::Errata status = this->checkRouterAssignment(group, msg.m_router_view); if (status.size()) { ar_spot->m_assign = true; // schedule an assignment message. - logf(status, LVL_INFO, "Router assignment reported from " - ATS_IP_PRINTF_CODE - " did not match local assignment. Resending assignment.\n ", - ATS_IP_OCTETS(router_addr) - ); + logf(status, LVL_INFO, + "Router assignment reported from " ATS_IP_PRINTF_CODE " did not match local assignment. Resending assignment.\n ", + ATS_IP_OCTETS(router_addr)); } } time_t then = ar_spot->m_recv.m_time; // used for comparisons later. @@ -828,12 +816,12 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con // Reply with our own capability options iff the router sent one to us. // This is a violation of the spec but it's what we have to do in practice // for mask assignment. - ar_spot->m_send_caps = ! caps.isEmpty(); + ar_spot->m_send_caps = !caps.isEmpty(); // For all the other listed routers, seed them if they're not // already active. uint32_t nr = msg.m_router_view.getRouterCount(); - for ( uint32_t idx = 0; idx < nr ; ++idx ) { + for (uint32_t idx = 0; idx < nr; ++idx) { uint32_t addr = msg.m_router_view.getRouterAddr(idx); if (group.m_routers.end() == find_by_member(group.m_routers, &RouterData::m_addr, addr)) group.seedRouter(addr); @@ -844,8 +832,8 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con // in its last packet. group.resizeCacheSources(); uint32_t nc = msg.m_router_view.getCacheCount(); - for ( uint32_t idx = 0 ; idx < nc ; ++idx ) { - CacheIdBox& cache = msg.m_router_view.cacheId(idx); + for (uint32_t idx = 0; idx < nc; ++idx) { + CacheIdBox &cache = msg.m_router_view.cacheId(idx); CacheBag::iterator ac_spot = group.findCache(cache.getAddr()); if (group.m_caches.end() == ac_spot) { group.m_caches.push_back(CacheData()); @@ -857,21 +845,25 @@ CacheImpl::handleISeeYou(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer con // Check if the cache wasn't reported last time but was reported // this time. In that case we need to bump the view to trigger // assignment generation. - if (ac_spot->m_src[router_idx].m_time != then) view_changed = true; + if (ac_spot->m_src[router_idx].m_time != then) + view_changed = true; } ac_spot->m_id.fill(cache); // If cache is this cache, update data in router record. - if (cache.getAddr() == m_addr) ar_spot->m_local_cache_id.fill(cache); + if (cache.getAddr() == m_addr) + ar_spot->m_local_cache_id.fill(cache); ac_spot->m_src[router_idx].set(now, recv_id); } - if (view_changed) group.viewChanged(now); + if (view_changed) + group.viewChanged(now); return zret; } ts::Errata -CacheImpl::handleRemovalQuery(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffer const& chunk) { +CacheImpl::handleRemovalQuery(IpHeader const & /* ip_hdr ATS_UNUSED */, ts::Buffer const &chunk) +{ ts::Errata zret; RemovalQueryMsg msg; time_t now = time(0); @@ -885,7 +877,7 @@ CacheImpl::handleRemovalQuery(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffe if (spot == m_groups.end()) return logf(LVL_INFO, "WCCP2_REMOVAL_QUERY ignored - service group %d not found.", svc.getSvcId()); - GroupData& group = spot->second; + GroupData &group = spot->second; if (!this->validateSecurity(msg, group)) return log(LVL_INFO, "Ignored WCCP2_REMOVAL_QUERY with invalid security.\n"); @@ -900,25 +892,15 @@ CacheImpl::handleRemovalQuery(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffe if (group.m_routers.end() != router) { router->m_rapid = true; // do rapid responses. router->m_recv.set(now, msg.m_query.getRecvId()); - logf(LVL_INFO, "WCCP2_REMOVAL_QUERY from router " - ATS_IP_PRINTF_CODE ".\n", - ATS_IP_OCTETS(raddr) - ); + logf(LVL_INFO, "WCCP2_REMOVAL_QUERY from router " ATS_IP_PRINTF_CODE ".\n", ATS_IP_OCTETS(raddr)); } else { - logf(LVL_INFO, "WCCP2_REMOVAL_QUERY from unknown router " - ATS_IP_PRINTF_CODE ".\n", - ATS_IP_OCTETS(raddr) - ); + logf(LVL_INFO, "WCCP2_REMOVAL_QUERY from unknown router " ATS_IP_PRINTF_CODE ".\n", ATS_IP_OCTETS(raddr)); } } else { // Not an error in the multi-cast case, so just log under debug. - logf(LVL_DEBUG, "WCCP2_REMOVAL_QUERY ignored -- target cache address " - ATS_IP_PRINTF_CODE - " did not match local address " - ATS_IP_PRINTF_CODE - "\n.", - ATS_IP_OCTETS(target_addr), ATS_IP_OCTETS(m_addr) - ); + logf(LVL_DEBUG, "WCCP2_REMOVAL_QUERY ignored -- target cache address " ATS_IP_PRINTF_CODE + " did not match local address " ATS_IP_PRINTF_CODE "\n.", + ATS_IP_OCTETS(target_addr), ATS_IP_OCTETS(m_addr)); } logf(LVL_DEBUG, "Received WCCP2_REMOVAL_QUERY for group %d.", group.m_svc.getSvcId()); @@ -927,29 +909,27 @@ CacheImpl::handleRemovalQuery(IpHeader const& /* ip_hdr ATS_UNUSED */, ts::Buffe } // ------------------------------------------------------ inline uint32_t -RouterImpl::CacheData::idAddr() const { +RouterImpl::CacheData::idAddr() const +{ return m_id.getAddr(); } -RouterImpl::GroupData::GroupData() { } +RouterImpl::GroupData::GroupData() +{ +} RouterImpl::CacheBag::iterator -RouterImpl::GroupData::findCache(uint32_t addr) { - return std::find_if( - m_caches.begin(), - m_caches.end(), - ts::predicate(&CacheData::idAddr, addr) - ); -} - -RouterImpl::GroupData& -RouterImpl::defineServiceGroup( - ServiceGroup const& svc, - ServiceGroup::Result* result -) { +RouterImpl::GroupData::findCache(uint32_t addr) +{ + return std::find_if(m_caches.begin(), m_caches.end(), ts::predicate(&CacheData::idAddr, addr)); +} + +RouterImpl::GroupData & +RouterImpl::defineServiceGroup(ServiceGroup const &svc, ServiceGroup::Result *result) +{ uint8_t svc_id = svc.getSvcId(); GroupMap::iterator spot = m_groups.find(svc_id); - GroupData* group; // service with target ID. + GroupData *group; // service with target ID. ServiceGroup::Result zret; if (spot == m_groups.end()) { // not defined group = &(m_groups[svc_id]); @@ -959,24 +939,27 @@ RouterImpl::defineServiceGroup( group = &spot->second; zret = group->m_svc == svc ? ServiceGroup::EXISTS : ServiceGroup::CONFLICT; } - if (result) *result = zret; + if (result) + *result = zret; return *group; } void -RouterImpl::GroupData::resizeRouterSources() { +RouterImpl::GroupData::resizeRouterSources() +{ ts::for_each(m_routers, &RouterData::resize, m_caches.size()); } ts::Errata -RouterImpl::handleHereIAm(IpHeader const& ip_hdr, ts::Buffer const& chunk) { +RouterImpl::handleHereIAm(IpHeader const &ip_hdr, ts::Buffer const &chunk) +{ ts::Errata zret; HereIAmMsg msg; static GroupData nil_group; // scratch until I clean up the security. // Set if our view of the group changes enough to bump the // generation number. bool view_changed = false; - int i; // scratch index var. + int i; // scratch index var. time_t now = time(0); // don't call this over and over. int parse = msg.parse(chunk); @@ -988,7 +971,7 @@ RouterImpl::handleHereIAm(IpHeader const& ip_hdr, ts::Buffer const& chunk) { ServiceGroup svc(msg.m_service); ServiceGroup::Result r; - GroupData& group = this->defineServiceGroup(svc, &r); + GroupData &group = this->defineServiceGroup(svc, &r); if (ServiceGroup::CONFLICT == r) return logf(LVL_INFO, "WCCP2_HERE_I_AM ignored - service group %d definition does not match.\n", svc.getSvcId()); else if (ServiceGroup::DEFINED == r) @@ -1009,7 +992,7 @@ RouterImpl::handleHereIAm(IpHeader const& ip_hdr, ts::Buffer const& chunk) { } else { // Did the cache mention us specifically? // If so, make sure the sequence # is correct. - RouterIdElt* me = msg.m_cache_view.findf_router_elt(m_addr); + RouterIdElt *me = msg.m_cache_view.findf_router_elt(m_addr); if (me && me->getRecvId() != cache->m_recv_count) return logf(LVL_INFO, "Discarded out of date (recv=%d, local=%ld) WCCP2_HERE_I_AM.\n", me->getRecvId(), cache->m_recv_count); } @@ -1024,7 +1007,7 @@ RouterImpl::handleHereIAm(IpHeader const& ip_hdr, ts::Buffer const& chunk) { // Add any new routers i = msg.m_cache_view.getRouterCount(); - while(i-- > 0) { + while (i-- > 0) { uint32_t addr = msg.m_cache_view.routerElt(i).getAddr(); RouterBag::iterator spot = find_by_member(group.m_routers, &RouterData::m_addr, addr); if (spot == group.m_routers.end()) { @@ -1038,16 +1021,14 @@ RouterImpl::handleHereIAm(IpHeader const& ip_hdr, ts::Buffer const& chunk) { spot->m_src[cache_idx].set(now, cache_gen); } - if (view_changed) ++(group.m_generation); + if (view_changed) + ++(group.m_generation); return zret; } void -RouterImpl::generateISeeYou( - ISeeYouMsg& msg, - GroupData& group, - CacheData& cache -) { +RouterImpl::generateISeeYou(ISeeYouMsg &msg, GroupData &group, CacheData &cache) +{ int i; size_t n_routers = group.m_routers.size(); size_t n_caches = group.m_caches.size(); @@ -1056,46 +1037,37 @@ RouterImpl::generateISeeYou( msg.fill(group, this->setSecurity(msg, group), group.m_assign_info, 1, n_routers, n_caches); // Fill in ID data not done by fill. - msg.m_router_id - .setIdElt(m_addr, cache.m_recv_count + 1) - .setToAddr(cache.m_to_addr) - .setFromAddr(0, cache.idAddr()); - ; + msg.m_router_id.setIdElt(m_addr, cache.m_recv_count + 1).setToAddr(cache.m_to_addr).setFromAddr(0, cache.idAddr()); + ; // Fill view routers. i = 0; - for ( RouterBag::iterator router = group.m_routers.begin(), - router_limit = group.m_routers.end(); - router != router_limit; - ++router, ++i - ) { + for (RouterBag::iterator router = group.m_routers.begin(), router_limit = group.m_routers.end(); router != router_limit; + ++router, ++i) { msg.m_router_view.setRouterAddr(i, router->m_addr); } // Fill view caches. i = 0; - for ( CacheBag::iterator spot = group.m_caches.begin(), - limit = group.m_caches.end(); - spot != limit; - ++spot, ++i - ) { + for (CacheBag::iterator spot = group.m_caches.begin(), limit = group.m_caches.end(); spot != limit; ++spot, ++i) { // TBD: This needs to track memory because cache ID elements // turn out to be variable sized. -// msg.m_router_view.cacheId(i) = spot->m_id; + // msg.m_router_view.cacheId(i) = spot->m_id; } msg.finalize(); } int -RouterImpl::xmitISeeYou() { +RouterImpl::xmitISeeYou() +{ int zret = 0; ISeeYouMsg msg; MsgBuffer buffer; sockaddr_in dst_addr; time_t now = time(0); static size_t const BUFFER_SIZE = 4096; - char* data = static_cast(alloca(BUFFER_SIZE)); + char *data = static_cast(alloca(BUFFER_SIZE)); memset(&dst_addr, 0, sizeof(dst_addr)); dst_addr.sin_family = AF_INET; @@ -1103,33 +1075,24 @@ RouterImpl::xmitISeeYou() { buffer.set(data, BUFFER_SIZE); // Send out messages for each service group. - for ( GroupMap::iterator svc_spot = m_groups.begin(), - svc_limit = m_groups.end() ; - svc_spot != svc_limit ; - ++svc_spot - ) { - GroupData& group = svc_spot->second; + for (GroupMap::iterator svc_spot = m_groups.begin(), svc_limit = m_groups.end(); svc_spot != svc_limit; ++svc_spot) { + GroupData &group = svc_spot->second; // Check each active cache in the group. - for ( CacheBag::iterator cache = group.m_caches.begin(), - cache_limit = group.m_caches.end() ; - cache != cache_limit ; - ++cache - ) { - if (!cache->m_pending) continue; + for (CacheBag::iterator cache = group.m_caches.begin(), cache_limit = group.m_caches.end(); cache != cache_limit; ++cache) { + if (!cache->m_pending) + continue; msg.setBuffer(buffer); this->generateISeeYou(msg, group, *cache); dst_addr.sin_addr.s_addr = cache->m_id.getAddr(); - zret = sendto(m_fd, data, msg.getCount(), 0, - reinterpret_cast(&dst_addr), sizeof(dst_addr)); + zret = sendto(m_fd, data, msg.getCount(), 0, reinterpret_cast(&dst_addr), sizeof(dst_addr)); if (0 <= zret) { cache->m_xmit.set(now, group.m_generation); cache->m_pending = false; cache->m_recv_count = msg.m_router_id.getRecvId(); logf(LVL_DEBUG, "I_SEE_YOU -> %s\n", ip_addr_to_str(cache->m_id.getAddr())); - } - else { + } else { log_errno(LVL_WARN, "Router transmit failed -"); return zret; } @@ -1139,137 +1102,165 @@ RouterImpl::xmitISeeYou() { } int -RouterImpl::housekeeping() { +RouterImpl::housekeeping() +{ return this->xmitISeeYou(); } bool -RouterImpl::isConfigured() const { +RouterImpl::isConfigured() const +{ return false; } // ------------------------------------------------------ -EndPoint::EndPoint() { +EndPoint::EndPoint() +{ } -EndPoint::~EndPoint() { +EndPoint::~EndPoint() +{ } -EndPoint::EndPoint(self const& that) - : m_ptr(that.m_ptr) { +EndPoint::EndPoint(self const &that) : m_ptr(that.m_ptr) +{ } -inline EndPoint::ImplType* -EndPoint::instance() { +inline EndPoint::ImplType * +EndPoint::instance() +{ return m_ptr ? m_ptr.get() : this->make(); } -EndPoint& EndPoint::setAddr(uint32_t addr) { +EndPoint & +EndPoint::setAddr(uint32_t addr) +{ this->instance()->m_addr = addr; logf(LVL_DEBUG, "Endpoint address set to %s\n", ip_addr_to_str(addr)); return *this; } bool -EndPoint::isConfigured() const { +EndPoint::isConfigured() const +{ return m_ptr && m_ptr->isConfigured(); } int -EndPoint::open(uint32_t addr) { +EndPoint::open(uint32_t addr) +{ return this->instance()->open(addr); } void -EndPoint::useMD5Security(ts::ConstBuffer const& key) { +EndPoint::useMD5Security(ts::ConstBuffer const &key) +{ this->instance()->useMD5Security(key); } -int EndPoint::getSocket() const { +int +EndPoint::getSocket() const +{ return m_ptr ? m_ptr->m_fd : ts::NO_FD; } int -EndPoint::housekeeping() { +EndPoint::housekeeping() +{ // Don't force an instance because if there isn't one, // there's no socket either. return m_ptr && ts::NO_FD != m_ptr->m_fd ? m_ptr->housekeeping() : -ENOTCONN; } ts::Rv -EndPoint::handleMessage() { - return m_ptr - ? m_ptr->handleMessage() - : ts::Rv(-ENOTCONN, log(LVL_INFO, "EndPoint::handleMessage called on unconnected instance")); +EndPoint::handleMessage() +{ + return m_ptr ? m_ptr->handleMessage() : + ts::Rv(-ENOTCONN, log(LVL_INFO, "EndPoint::handleMessage called on unconnected instance")); } // ------------------------------------------------------ -Cache::Cache() { +Cache::Cache() +{ } -Cache::~Cache() { +Cache::~Cache() +{ } -EndPoint::ImplType* -Cache::make() { +EndPoint::ImplType * +Cache::make() +{ m_ptr.assign(new ImplType); return m_ptr.get(); } -inline Cache::ImplType* -Cache::instance() { - return static_cast(this->super::instance()); +inline Cache::ImplType * +Cache::instance() +{ + return static_cast(this->super::instance()); } -inline Cache::ImplType* Cache::impl() { - return static_cast(m_ptr.get()); +inline Cache::ImplType * +Cache::impl() +{ + return static_cast(m_ptr.get()); } -inline Cache::ImplType const* Cache::impl() const { - return static_cast(m_ptr.get()); +inline Cache::ImplType const * +Cache::impl() const +{ + return static_cast(m_ptr.get()); } Cache::Service -Cache::defineServiceGroup( - ServiceGroup const& svc, - ServiceGroup::Result* result -) { +Cache::defineServiceGroup(ServiceGroup const &svc, ServiceGroup::Result *result) +{ return Service(*this, this->instance()->defineServiceGroup(svc, result)); } -time_t Cache::waitTime() const { +time_t +Cache::waitTime() const +{ return m_ptr ? this->impl()->waitTime() : std::numeric_limits::max(); } -Cache& -Cache::addSeedRouter(uint8_t id, uint32_t addr) { +Cache & +Cache::addSeedRouter(uint8_t id, uint32_t addr) +{ this->instance()->seedRouter(id, addr); return *this; } ts::Errata -Cache::loadServicesFromFile(char const* path) { +Cache::loadServicesFromFile(char const *path) +{ return this->instance()->loadServicesFromFile(path); } // ------------------------------------------------------ -Router::Router() { +Router::Router() +{ } -Router::~Router() { +Router::~Router() +{ } -EndPoint::ImplType* -Router::make() { +EndPoint::ImplType * +Router::make() +{ m_ptr.assign(new ImplType); return m_ptr.get(); } -inline Router::ImplType* -Router::instance() { - return static_cast(this->super::instance()); +inline Router::ImplType * +Router::instance() +{ + return static_cast(this->super::instance()); } -inline Router::ImplType* -Router::impl() { - return static_cast(m_ptr.get()); +inline Router::ImplType * +Router::impl() +{ + return static_cast(m_ptr.get()); } // ------------------------------------------------------ } // namespace wccp