Skip to content

Commit

Permalink
Update assert function.
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed May 24, 2015
1 parent eca4f63 commit 9e8eb96
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
18 changes: 0 additions & 18 deletions dnscrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,6 @@ print_binary_string_hex(uint8_t *s, size_t count)
printf("\n");
}

static inline void
_theAssert(char *estr, char *file, int line)
{
logger(LOG_DEBUG, "=== ASSERTION FAILED ===");
logger(LOG_DEBUG, "==> %s:%d '%s' is not true", file, line, estr);
}

static inline void
_theAssertPrintBinaryString(uint8_t *s, size_t count, char *estr, char *file,
int line)
{
_theAssert(estr, file, line);
print_binary_string_hex(s, count);
}

#define theAssert(_e) ((_e)?(void)0 : (_theAssert(#_e,__FILE__,__LINE__),_exit(1)))
#define theAssertPrintBinaryString(_s,_c,_e) ((_e)?(void)0 : (_theAssertPrintBinaryString(_s,_c,#_e,__FILE__,__LINE__),_exit(1)))

struct dnscrypt_query_header {
uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
uint8_t publickey[crypto_box_PUBLICKEYBYTES];
Expand Down
34 changes: 17 additions & 17 deletions tcp_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ tcp_request_kill(TCPRequest *const tcp_request)
}
c = tcp_request->context;
if (tcp_request->status.is_in_queue != 0) {
theAssert(!TAILQ_EMPTY(&c->tcp_request_queue));
debug_assert(!TAILQ_EMPTY(&c->tcp_request_queue));
TAILQ_REMOVE(&c->tcp_request_queue, tcp_request, queue);
theAssert(c->connections > 0U);
debug_assert(c->connections > 0U);
c->connections--;
}
tcp_request->context = NULL;
Expand Down Expand Up @@ -88,13 +88,13 @@ client_proxy_read_cb(struct bufferevent *const client_proxy_bev,
size_t max_query_size;

if (tcp_request->status.has_dns_query_len == 0) {
theAssert(evbuffer_get_length(input) >= (size_t) 2U);
debug_assert(evbuffer_get_length(input) >= (size_t) 2U);
evbuffer_remove(input, dns_query_len_buf, sizeof dns_query_len_buf);
tcp_request->dns_query_len = (size_t)
((dns_query_len_buf[0] << 8) | dns_query_len_buf[1]);
tcp_request->status.has_dns_query_len = 1;
}
theAssert(tcp_request->status.has_dns_query_len != 0);
debug_assert(tcp_request->status.has_dns_query_len != 0);
dns_query_len = tcp_request->dns_query_len;
if (dns_query_len < (size_t) DNS_HEADER_SIZE) {
logger(LOG_WARNING, "Short query received");
Expand All @@ -107,9 +107,9 @@ client_proxy_read_cb(struct bufferevent *const client_proxy_bev,
EV_READ, dns_query_len, dns_query_len);
return;
}
theAssert(available_size >= dns_query_len);
debug_assert(available_size >= dns_query_len);
bufferevent_disable(tcp_request->client_proxy_bev, EV_READ);
theAssert(tcp_request->proxy_resolver_query_evbuf == NULL);
debug_assert(tcp_request->proxy_resolver_query_evbuf == NULL);
if ((tcp_request->proxy_resolver_query_evbuf = evbuffer_new()) == NULL) {
tcp_request_kill(tcp_request);
return;
Expand All @@ -120,16 +120,16 @@ client_proxy_read_cb(struct bufferevent *const client_proxy_bev,
tcp_request_kill(tcp_request);
return;
}
theAssert(dns_query_len <= sizeof dns_query);
debug_assert(dns_query_len <= sizeof dns_query);
if ((ssize_t) evbuffer_remove(tcp_request->proxy_resolver_query_evbuf,
dns_query, dns_query_len)
!= (ssize_t) dns_query_len) {
tcp_request_kill(tcp_request);
return;
}
max_query_size = sizeof dns_query;
theAssert(max_query_size < DNS_MAX_PACKET_SIZE_TCP);
theAssert(SIZE_MAX - DNSCRYPT_MAX_PADDING - DNSCRYPT_QUERY_HEADER_SIZE
debug_assert(max_query_size < DNS_MAX_PACKET_SIZE_TCP);
debug_assert(SIZE_MAX - DNSCRYPT_MAX_PADDING - DNSCRYPT_QUERY_HEADER_SIZE
> dns_query_len);
size_t max_len =
dns_query_len + DNSCRYPT_MAX_PADDING + DNSCRYPT_QUERY_HEADER_SIZE;
Expand All @@ -140,9 +140,9 @@ client_proxy_read_cb(struct bufferevent *const client_proxy_bev,
tcp_request_kill(tcp_request);
return;
}
theAssert(max_len <= DNS_MAX_PACKET_SIZE_TCP - 2U);
theAssert(max_len <= sizeof dns_query);
theAssert(dns_query_len <= max_len);
debug_assert(max_len <= DNS_MAX_PACKET_SIZE_TCP - 2U);
debug_assert(max_len <= sizeof dns_query);
debug_assert(dns_query_len <= max_len);

// decrypt if encrypted
struct dnscrypt_query_header *dnscrypt_header =
Expand Down Expand Up @@ -233,13 +233,13 @@ resolver_proxy_read_cb(struct bufferevent *const proxy_resolver_bev,

logger(LOG_DEBUG, "Resolver read callback.");
if (tcp_request->status.has_dns_reply_len == 0) {
theAssert(evbuffer_get_length(input) >= (size_t) 2U);
debug_assert(evbuffer_get_length(input) >= (size_t) 2U);
evbuffer_remove(input, dns_reply_len_buf, sizeof dns_reply_len_buf);
tcp_request->dns_reply_len = (size_t)
((dns_reply_len_buf[0] << 8) | dns_reply_len_buf[1]);
tcp_request->status.has_dns_reply_len = 1;
}
theAssert(tcp_request->status.has_dns_reply_len != 0);
debug_assert(tcp_request->status.has_dns_reply_len != 0);
dns_reply_len = tcp_request->dns_reply_len;
if (dns_reply_len < (size_t) DNS_HEADER_SIZE) {
logger(LOG_WARNING, "Short reply received");
Expand All @@ -252,7 +252,7 @@ resolver_proxy_read_cb(struct bufferevent *const proxy_resolver_bev,
EV_READ, dns_reply_len, dns_reply_len);
return;
}
theAssert(available_size >= dns_reply_len);
debug_assert(available_size >= dns_reply_len);
dns_reply_bev = evbuffer_pullup(input, (ssize_t) dns_reply_len);
if (dns_reply_bev == NULL) {
tcp_request_kill(tcp_request);
Expand Down Expand Up @@ -400,7 +400,7 @@ tcp_accept_error_cb(struct evconnlistener *const tcp_conn_listener,
int
tcp_listener_bind(struct context *c)
{
theAssert(c->tcp_conn_listener == NULL);
debug_assert(c->tcp_conn_listener == NULL);
#ifndef LEV_OPT_DEFERRED_ACCEPT
# define LEV_OPT_DEFERRED_ACCEPT 0
#endif
Expand Down Expand Up @@ -446,7 +446,7 @@ tcp_listener_bind(struct context *c)
int
tcp_listener_start(struct context *c)
{
theAssert(c->tcp_conn_listener != NULL);
debug_assert(c->tcp_conn_listener != NULL);
if (evconnlistener_enable(c->tcp_conn_listener) != 0) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#ifndef VERSION_H
#define VERSION_H

const char *the_version = "0.1.15.17.gfc8126d";
const char *the_version = "0.1.15.21.geca4f63";

#endif

0 comments on commit 9e8eb96

Please sign in to comment.