Add magic number for type-checking channel casts

This commit is contained in:
Andrea Shepard 2012-10-09 14:16:51 -07:00
parent f00b44ef8c
commit bddfb9ffa8
3 changed files with 47 additions and 4 deletions

View File

@ -26,6 +26,9 @@ typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
*/ */
struct channel_s { struct channel_s {
/* Magic number for type-checking cast macros */
uint32_t magic;
/* Current channel state */ /* Current channel state */
channel_state_t state; channel_state_t state;

View File

@ -102,8 +102,9 @@ channel_tls_connect(const tor_addr_t *addr, uint16_t port,
const char *id_digest) const char *id_digest)
{ {
channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan)); 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); channel_init(chan);
chan->magic = TLS_CHAN_MAGIC;
chan->state = CHANNEL_STATE_OPENING; chan->state = CHANNEL_STATE_OPENING;
chan->close = channel_tls_close_method; chan->close = channel_tls_close_method;
chan->describe_transport = channel_tls_describe_transport_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_handle_incoming(or_connection_t *orconn)
{ {
channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan)); 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);
tor_assert(!(orconn->chan)); tor_assert(!(orconn->chan));
channel_init(chan); channel_init(chan);
chan->magic = TLS_CHAN_MAGIC;
chan->state = CHANNEL_STATE_OPENING; chan->state = CHANNEL_STATE_OPENING;
chan->close = channel_tls_close_method; chan->close = channel_tls_close_method;
chan->describe_transport = channel_tls_describe_transport_method; chan->describe_transport = channel_tls_describe_transport_method;
@ -267,6 +269,37 @@ channel_tls_handle_incoming(or_connection_t *orconn)
return chan; 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 * * Method implementations for channel_tls_t *
*******************************************/ *******************************************/

View File

@ -12,8 +12,10 @@
#include "or.h" #include "or.h"
#include "channel.h" #include "channel.h"
#define BASE_CHAN_TO_TLS(c) ((channel_tls_t *)(c)) #define BASE_CHAN_TO_TLS(c) (channel_tls_from_base((c)))
#define TLS_CHAN_TO_BASE(c) ((channel_t *)(c)) #define TLS_CHAN_TO_BASE(c) (channel_tls_to_base((c)))
#define TLS_CHAN_MAGIC 0x8a192427U
#ifdef _TOR_CHANNEL_INTERNAL #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_listener_t * channel_tls_start_listener(void);
channel_t * channel_tls_handle_incoming(or_connection_t *orconn); 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 */ /* Things for connection_or.c to call back into */
ssize_t channel_tls_flush_some_cells(channel_tls_t *chan, ssize_t num_cells); 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); int channel_tls_more_to_flush(channel_tls_t *chan);