Sfoglia il codice sorgente

Add magic number for type-checking channel casts

Andrea Shepard 12 anni fa
parent
commit
bddfb9ffa8
3 ha cambiato i file con 47 aggiunte e 4 eliminazioni
  1. 3 0
      src/or/channel.h
  2. 35 2
      src/or/channeltls.c
  3. 9 2
      src/or/channeltls.h

+ 3 - 0
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;
 

+ 35 - 2
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 *
  *******************************************/

+ 9 - 2
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);