Commit 0a9fedaf authored by Michael Graff's avatar Michael Graff
Browse files

Use an LFSR to generate unpredictable IDs.

parent 21cbbc3e
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <isc/assertions.h> #include <isc/assertions.h>
#include <isc/error.h> #include <isc/error.h>
#include <isc/lfsr.h>
#include <isc/mem.h> #include <isc/mem.h>
#include <isc/mutex.h> #include <isc/mutex.h>
#include <isc/socket.h> #include <isc/socket.h>
...@@ -87,7 +88,7 @@ struct dns_dispatch { ...@@ -87,7 +88,7 @@ struct dns_dispatch {
ISC_LIST(dns_dispentry_t) rq_handlers; /* request handler list */ ISC_LIST(dns_dispentry_t) rq_handlers; /* request handler list */
ISC_LIST(dns_dispatchevent_t) rq_events; /* holder for rq events */ ISC_LIST(dns_dispatchevent_t) rq_events; /* holder for rq events */
dns_tcpmsg_t tcpmsg; /* for tcp streams */ dns_tcpmsg_t tcpmsg; /* for tcp streams */
isc_int32_t qid_state; /* state generator info */ isc_lfsr_t qid_lfsr; /* state generator info */
unsigned int qid_hashsize; /* hash table size */ unsigned int qid_hashsize; /* hash table size */
unsigned int qid_mask; /* mask for hash table */ unsigned int qid_mask; /* mask for hash table */
dns_displist_t *qid_table; /* the table itself */ dns_displist_t *qid_table; /* the table itself */
...@@ -105,54 +106,37 @@ struct dns_dispatch { ...@@ -105,54 +106,37 @@ struct dns_dispatch {
/* /*
* statics. * statics.
*/ */
static dns_dispentry_t * static dns_dispentry_t *bucket_search(dns_dispatch_t *, isc_sockaddr_t *,
bucket_search(dns_dispatch_t *, isc_sockaddr_t *, dns_messageid_t, unsigned int);
dns_messageid_t, unsigned int); static void destroy(dns_dispatch_t *);
static void udp_recv(isc_task_t *, isc_event_t *);
static void static void tcp_recv(isc_task_t *, isc_event_t *);
destroy(dns_dispatch_t *); static inline void startrecv(dns_dispatch_t *);
static dns_messageid_t randomid(dns_dispatch_t *);
static void static unsigned int hash(dns_dispatch_t *, isc_sockaddr_t *, dns_messageid_t);
udp_recv(isc_task_t *, isc_event_t *); static void free_buffer(dns_dispatch_t *disp, void *buf, unsigned int len);
static void *allocate_buffer(dns_dispatch_t *disp, unsigned int len);
static void static inline void free_event(dns_dispatch_t *disp, dns_dispatchevent_t *ev);
tcp_recv(isc_task_t *, isc_event_t *); static inline dns_dispatchevent_t *allocate_event(dns_dispatch_t *disp);
static void do_next_request(dns_dispatch_t *disp, dns_dispentry_t *resp);
static void static void do_next_response(dns_dispatch_t *disp, dns_dispentry_t *resp);
startrecv(dns_dispatch_t *); static void do_cancel(dns_dispatch_t *disp, dns_dispentry_t *resp);
static dns_dispentry_t *linear_first(dns_dispatch_t *disp);
static dns_messageid_t static dns_dispentry_t *linear_next(dns_dispatch_t *disp,
randomid(dns_dispatch_t *); dns_dispentry_t *resp);
static unsigned int
hash(dns_dispatch_t *, isc_sockaddr_t *, dns_messageid_t);
static void
free_buffer(dns_dispatch_t *disp, void *buf, unsigned int len);
static void *
allocate_buffer(dns_dispatch_t *disp, unsigned int len);
static inline void
free_event(dns_dispatch_t *disp, dns_dispatchevent_t *ev);
static inline dns_dispatchevent_t *
allocate_event(dns_dispatch_t *disp);
static void /*
do_next_request(dns_dispatch_t *disp, dns_dispentry_t *resp); * Return an unpredictable message ID.
*/
static void static inline dns_messageid_t
do_next_response(dns_dispatch_t *disp, dns_dispentry_t *resp); randomid(dns_dispatch_t *disp)
{
static void isc_uint32_t id;
do_cancel(dns_dispatch_t *disp, dns_dispentry_t *resp);
static dns_dispentry_t * id = isc_lfsr_generate(&disp->qid_lfsr);
linear_first(dns_dispatch_t *disp);
static dns_dispentry_t * return ((dns_messageid_t)(id & 0x0000ffff));
linear_next(dns_dispatch_t *disp, dns_dispentry_t *resp); }
static dns_dispentry_t * static dns_dispentry_t *
linear_first(dns_dispatch_t *disp) linear_first(dns_dispatch_t *disp)
...@@ -212,18 +196,6 @@ hash(dns_dispatch_t *disp, isc_sockaddr_t *dest, dns_messageid_t id) ...@@ -212,18 +196,6 @@ hash(dns_dispatch_t *disp, isc_sockaddr_t *dest, dns_messageid_t id)
return (ret); return (ret);
} }
/*
* Return a random message ID. For now this isn't too clever...
* XXXMLG
*/
static dns_messageid_t
randomid(dns_dispatch_t *disp)
{
disp->qid_state += 7;
return ((dns_messageid_t)disp->qid_state);
}
/* /*
* Called when refcount reaches 0 (and safe to destroy) * Called when refcount reaches 0 (and safe to destroy)
*/ */
...@@ -919,9 +891,10 @@ dns_dispatch_create(isc_mem_t *mctx, isc_socket_t *sock, isc_task_t *task, ...@@ -919,9 +891,10 @@ dns_dispatch_create(isc_mem_t *mctx, isc_socket_t *sock, isc_task_t *task,
} }
/* /*
* should initialize qid_state here XXXMLG * Initialize to a 32-bit LFSR.
* x^31 + x^6 + x^4 + x^2 + x + 1
*/ */
disp->qid_state = (unsigned int)disp; isc_lfsr_init(&disp->qid_lfsr, random(), 32, 0x80000057U);
disp->magic = DISPATCH_MAGIC; disp->magic = DISPATCH_MAGIC;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment