[tor-commits] [tor/master] Add magic number for type-checking channel casts

andrea at torproject.org andrea at torproject.org
Thu Oct 11 02:05:23 UTC 2012


commit bddfb9ffa85a0fe73545fb231a0847c4101758c1
Author: Andrea Shepard <andrea at torproject.org>
Date:   Tue Oct 9 14:16:51 2012 -0700

    Add magic number for type-checking channel casts
---
 src/or/channel.h    |    3 +++
 src/or/channeltls.c |   37 +++++++++++++++++++++++++++++++++++--
 src/or/channeltls.h |   11 +++++++++--
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/src/or/channel.h b/src/or/channel.h
index c31806c..27fba8f 100644
--- a/src/or/channel.h
+++ b/src/or/channel.h
@@ -26,6 +26,9 @@ typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
  */
 
 struct channel_s {
+  /* Magic number for type-checking cast macros */
+  uint32_t magic;
+
   /* Current channel state */
   channel_state_t state;
 
diff --git a/src/or/channeltls.c b/src/or/channeltls.c
index 339663e..5d6a7a9 100644
--- a/src/or/channeltls.c
+++ b/src/or/channeltls.c
@@ -102,8 +102,9 @@ channel_tls_connect(const tor_addr_t *addr, uint16_t port,
                     const char *id_digest)
 {
   channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
-  channel_t *chan = TLS_CHAN_TO_BASE(tlschan);
+  channel_t *chan = &(tlschan->_base);
   channel_init(chan);
+  chan->magic = TLS_CHAN_MAGIC;
   chan->state = CHANNEL_STATE_OPENING;
   chan->close = channel_tls_close_method;
   chan->describe_transport = channel_tls_describe_transport_method;
@@ -233,12 +234,13 @@ channel_t *
 channel_tls_handle_incoming(or_connection_t *orconn)
 {
   channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
-  channel_t *chan = TLS_CHAN_TO_BASE(tlschan);
+  channel_t *chan = &(tlschan->_base);
 
   tor_assert(orconn);
   tor_assert(!(orconn->chan));
 
   channel_init(chan);
+  chan->magic = TLS_CHAN_MAGIC;
   chan->state = CHANNEL_STATE_OPENING;
   chan->close = channel_tls_close_method;
   chan->describe_transport = channel_tls_describe_transport_method;
@@ -267,6 +269,37 @@ channel_tls_handle_incoming(or_connection_t *orconn)
   return chan;
 }
 
+/*********
+ * Casts *
+ ********/
+
+/**
+ * Cast a channel_tls_t to a channel_t.
+ */
+
+channel_t *
+channel_tls_to_base(channel_tls_t *tlschan)
+{
+  if (!tlschan) return NULL;
+
+  return &(tlschan->_base);
+}
+
+/**
+ * Cast a channel_t to a channel_tls_t, with appropriate type-checking
+ * asserts.
+ */
+
+channel_tls_t *
+channel_tls_from_base(channel_t *chan)
+{
+  if (!chan) return NULL;
+
+  tor_assert(chan->magic == TLS_CHAN_MAGIC);
+
+  return (channel_tls_t *)(chan);
+}
+
 /********************************************
  * Method implementations for channel_tls_t *
  *******************************************/
diff --git a/src/or/channeltls.h b/src/or/channeltls.h
index b38e12a..ca2fc88 100644
--- a/src/or/channeltls.h
+++ b/src/or/channeltls.h
@@ -12,8 +12,10 @@
 #include "or.h"
 #include "channel.h"
 
-#define BASE_CHAN_TO_TLS(c) ((channel_tls_t *)(c))
-#define TLS_CHAN_TO_BASE(c) ((channel_t *)(c))
+#define BASE_CHAN_TO_TLS(c) (channel_tls_from_base((c)))
+#define TLS_CHAN_TO_BASE(c) (channel_tls_to_base((c)))
+
+#define TLS_CHAN_MAGIC 0x8a192427U
 
 #ifdef _TOR_CHANNEL_INTERNAL
 
@@ -32,6 +34,11 @@ channel_listener_t * channel_tls_get_listener(void);
 channel_listener_t * channel_tls_start_listener(void);
 channel_t * channel_tls_handle_incoming(or_connection_t *orconn);
 
+/* Casts */
+
+channel_t * channel_tls_to_base(channel_tls_t *tlschan);
+channel_tls_t * channel_tls_from_base(channel_t *chan);
+
 /* Things for connection_or.c to call back into */
 ssize_t channel_tls_flush_some_cells(channel_tls_t *chan, ssize_t num_cells);
 int channel_tls_more_to_flush(channel_tls_t *chan);





More information about the tor-commits mailing list