This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new f8bda56  Remove error-prone mirror enum in code handling TS API SSL 
hooks.
f8bda56 is described below

commit f8bda563c9791b5242d3b82339039f9eb6a8c39a
Author: Walter Karas <[email protected]>
AuthorDate: Fri Feb 8 16:36:13 2019 -0600

    Remove error-prone mirror enum in code handling TS API SSL hooks.
---
 iocore/net/SSLNetVConnection.cc | 18 ++++++-------
 iocore/net/SSLUtils.cc          |  6 ++---
 proxy/InkAPIInternal.h          | 56 ++++++++++++++++++++++-------------------
 src/traffic_server/InkAPI.cc    |  4 +--
 4 files changed, 44 insertions(+), 40 deletions(-)

diff --git a/iocore/net/SSLNetVConnection.cc b/iocore/net/SSLNetVConnection.cc
index a93869b..bc3cd8e 100644
--- a/iocore/net/SSLNetVConnection.cc
+++ b/iocore/net/SSLNetVConnection.cc
@@ -1124,7 +1124,7 @@ SSLNetVConnection::sslServerHandShakeEvent(int &err)
   if (sslHandshakeHookState == HANDSHAKE_HOOKS_PRE) {
     if (!curHook) {
       Debug("ssl", "Initialize preaccept curHook from NULL");
-      curHook = ssl_hooks->get(TS_VCONN_START_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_VCONN_START_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1375,7 +1375,7 @@ SSLNetVConnection::sslClientHandShakeEvent(int &err)
   if (sslHandshakeHookState == HANDSHAKE_HOOKS_OUTBOUND_PRE) {
     if (!curHook) {
       Debug("ssl", "Initialize outbound connect curHook from NULL");
-      curHook = ssl_hooks->get(TS_VCONN_OUTBOUND_START_INTERNAL_HOOK);
+      curHook = 
ssl_hooks->get(TSSslHookInternalID(TS_VCONN_OUTBOUND_START_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1694,7 +1694,7 @@ SSLNetVConnection::callHooks(TSEvent eventId)
   case HANDSHAKE_HOOKS_CLIENT_HELLO:
   case HANDSHAKE_HOOKS_CLIENT_HELLO_INVOKE:
     if (!curHook) {
-      curHook = ssl_hooks->get(TS_SSL_CLIENT_HELLO_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_CLIENT_HELLO_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1708,14 +1708,14 @@ SSLNetVConnection::callHooks(TSEvent eventId)
     // The server verify event addresses ATS to origin handshake
     // All the other events are for client to ATS
     if (!curHook) {
-      curHook = ssl_hooks->get(TS_SSL_VERIFY_SERVER_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_VERIFY_SERVER_HOOK));
     } else {
       curHook = curHook->next();
     }
     break;
   case HANDSHAKE_HOOKS_SNI:
     if (!curHook) {
-      curHook = ssl_hooks->get(TS_SSL_SERVERNAME_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_SERVERNAME_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1726,7 +1726,7 @@ SSLNetVConnection::callHooks(TSEvent eventId)
   case HANDSHAKE_HOOKS_CERT:
   case HANDSHAKE_HOOKS_CERT_INVOKE:
     if (!curHook) {
-      curHook = ssl_hooks->get(TS_SSL_CERT_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_CERT_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1739,7 +1739,7 @@ SSLNetVConnection::callHooks(TSEvent eventId)
   case HANDSHAKE_HOOKS_CLIENT_CERT:
   case HANDSHAKE_HOOKS_CLIENT_CERT_INVOKE:
     if (!curHook) {
-      curHook = ssl_hooks->get(TS_SSL_VERIFY_CLIENT_INTERNAL_HOOK);
+      curHook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_VERIFY_CLIENT_HOOK));
     } else {
       curHook = curHook->next();
     }
@@ -1749,14 +1749,14 @@ SSLNetVConnection::callHooks(TSEvent eventId)
     if (eventId == TS_EVENT_VCONN_CLOSE) {
       sslHandshakeHookState = HANDSHAKE_HOOKS_DONE;
       if (curHook == nullptr) {
-        curHook = ssl_hooks->get(TS_VCONN_CLOSE_INTERNAL_HOOK);
+        curHook = ssl_hooks->get(TSSslHookInternalID(TS_VCONN_CLOSE_HOOK));
       } else {
         curHook = curHook->next();
       }
     } else if (eventId == TS_EVENT_VCONN_OUTBOUND_CLOSE) {
       sslHandshakeHookState = HANDSHAKE_HOOKS_DONE;
       if (curHook == nullptr) {
-        curHook = ssl_hooks->get(TS_VCONN_OUTBOUND_CLOSE_INTERNAL_HOOK);
+        curHook = 
ssl_hooks->get(TSSslHookInternalID(TS_VCONN_OUTBOUND_CLOSE_HOOK));
       } else {
         curHook = curHook->next();
       }
diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc
index c83b314..41411ab 100644
--- a/iocore/net/SSLUtils.cc
+++ b/iocore/net/SSLUtils.cc
@@ -193,7 +193,7 @@ ssl_get_cached_session(SSL *ssl, const unsigned char *id, 
int len, int *copy)
     Debug("ssl.session_cache.get", "ssl_get_cached_session cached session '%s' 
context %p", printable_buf, SSL_get_SSL_CTX(ssl));
   }
 
-  APIHook *hook = ssl_hooks->get(TS_SSL_SESSION_INTERNAL_HOOK);
+  APIHook *hook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_SESSION_HOOK));
   while (hook) {
     hook->invoke(TS_EVENT_SSL_SESSION_GET, &sid);
     hook = hook->m_link.next;
@@ -241,7 +241,7 @@ ssl_new_cached_session(SSL *ssl, SSL_SESSION *sess)
   session_cache->insertSession(sid, sess);
 
   // Call hook after new session is created
-  APIHook *hook = ssl_hooks->get(TS_SSL_SESSION_INTERNAL_HOOK);
+  APIHook *hook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_SESSION_HOOK));
   while (hook) {
     hook->invoke(TS_EVENT_SSL_SESSION_NEW, &sid);
     hook = hook->m_link.next;
@@ -258,7 +258,7 @@ ssl_rm_cached_session(SSL_CTX *ctx, SSL_SESSION *sess)
   SSLSessionID sid(id, len);
 
   // Call hook before session is removed
-  APIHook *hook = ssl_hooks->get(TS_SSL_SESSION_INTERNAL_HOOK);
+  APIHook *hook = ssl_hooks->get(TSSslHookInternalID(TS_SSL_SESSION_HOOK));
   while (hook) {
     hook->invoke(TS_EVENT_SSL_SESSION_REMOVE, &sid);
     hook = hook->m_link.next;
diff --git a/proxy/InkAPIInternal.h b/proxy/InkAPIInternal.h
index 7955c53..ccfd08b 100644
--- a/proxy/InkAPIInternal.h
+++ b/proxy/InkAPIInternal.h
@@ -156,7 +156,7 @@ APIHooks::is_empty() const
     maximum hook ID so the valid ids are 0..(N-1) in the standard C array 
style.
  */
 template <typename ID, ///< Type of hook ID
-          ID N         ///< Number of hooks
+          int N        ///< Number of hooks
           >
 class FeatureAPIHooks
 {
@@ -194,14 +194,14 @@ private:
   APIHooks m_hooks[N];
 };
 
-template <typename ID, ID N> FeatureAPIHooks<ID, N>::FeatureAPIHooks() : 
hooks_p(false) {}
+template <typename ID, int N> FeatureAPIHooks<ID, N>::FeatureAPIHooks() : 
hooks_p(false) {}
 
-template <typename ID, ID N> FeatureAPIHooks<ID, N>::~FeatureAPIHooks()
+template <typename ID, int N> FeatureAPIHooks<ID, N>::~FeatureAPIHooks()
 {
   this->clear();
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 void
 FeatureAPIHooks<ID, N>::clear()
 {
@@ -211,7 +211,7 @@ FeatureAPIHooks<ID, N>::clear()
   hooks_p = false;
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 void
 FeatureAPIHooks<ID, N>::prepend(ID id, INKContInternal *cont)
 {
@@ -221,7 +221,7 @@ FeatureAPIHooks<ID, N>::prepend(ID id, INKContInternal 
*cont)
   }
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 void
 FeatureAPIHooks<ID, N>::append(ID id, INKContInternal *cont)
 {
@@ -231,14 +231,14 @@ FeatureAPIHooks<ID, N>::append(ID id, INKContInternal 
*cont)
   }
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 APIHook *
 FeatureAPIHooks<ID, N>::get(ID id) const
 {
   return likely(is_valid(id)) ? m_hooks[id].get() : nullptr;
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 void
 FeatureAPIHooks<ID, N>::invoke(ID id, int event, void *data)
 {
@@ -247,14 +247,14 @@ FeatureAPIHooks<ID, N>::invoke(ID id, int event, void 
*data)
   }
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 bool
 FeatureAPIHooks<ID, N>::has_hooks() const
 {
   return hooks_p;
 }
 
-template <typename ID, ID N>
+template <typename ID, int N>
 bool
 FeatureAPIHooks<ID, N>::is_valid(ID id)
 {
@@ -265,22 +265,26 @@ class HttpAPIHooks : public FeatureAPIHooks<TSHttpHookID, 
TS_HTTP_LAST_HOOK>
 {
 };
 
-typedef enum {
-  TS_SSL_INTERNAL_FIRST_HOOK,
-  TS_VCONN_START_INTERNAL_HOOK = TS_SSL_INTERNAL_FIRST_HOOK,
-  TS_VCONN_CLOSE_INTERNAL_HOOK,
-  TS_SSL_CLIENT_HELLO_INTERNAL_HOOK,
-  TS_SSL_CERT_INTERNAL_HOOK,
-  TS_SSL_SERVERNAME_INTERNAL_HOOK,
-  TS_SSL_VERIFY_SERVER_INTERNAL_HOOK,
-  TS_SSL_VERIFY_CLIENT_INTERNAL_HOOK,
-  TS_SSL_SESSION_INTERNAL_HOOK,
-  TS_VCONN_OUTBOUND_START_INTERNAL_HOOK,
-  TS_VCONN_OUTBOUND_CLOSE_INTERNAL_HOOK,
-  TS_SSL_INTERNAL_LAST_HOOK
-} TSSslHookInternalID;
-
-class SslAPIHooks : public FeatureAPIHooks<TSSslHookInternalID, 
TS_SSL_INTERNAL_LAST_HOOK>
+class TSSslHookInternalID
+{
+public:
+  constexpr TSSslHookInternalID(TSHttpHookID id) : _id(id - TS_SSL_FIRST_HOOK) 
{}
+
+  constexpr operator int() const { return _id; }
+
+  static const int NUM = TS_SSL_LAST_HOOK - TS_SSL_FIRST_HOOK + 1;
+
+  constexpr bool
+  is_in_bounds() const
+  {
+    return (_id >= 0) && (_id < NUM);
+  }
+
+private:
+  const int _id;
+};
+
+class SslAPIHooks : public FeatureAPIHooks<TSSslHookInternalID, 
TSSslHookInternalID::NUM>
 {
 };
 
diff --git a/src/traffic_server/InkAPI.cc b/src/traffic_server/InkAPI.cc
index 62cda08..c2ac860 100644
--- a/src/traffic_server/InkAPI.cc
+++ b/src/traffic_server/InkAPI.cc
@@ -4696,8 +4696,8 @@ TSHttpHookAdd(TSHttpHookID id, TSCont contp)
 
   icontp = reinterpret_cast<INKContInternal *>(contp);
 
-  if (id >= TS_SSL_FIRST_HOOK && id <= TS_SSL_LAST_HOOK) {
-    TSSslHookInternalID internalId = static_cast<TSSslHookInternalID>(id - 
TS_SSL_FIRST_HOOK);
+  TSSslHookInternalID internalId{id};
+  if (internalId.is_in_bounds()) {
     ssl_hooks->append(internalId, icontp);
   } else { // Follow through the regular HTTP hook framework
     http_global_hooks->append(id, icontp);

Reply via email to