timeout stubs

This commit is contained in:
Neel Goyal 2014-01-27 17:05:25 -05:00
parent f98b99e661
commit 67fdf0eb04
4 changed files with 109 additions and 7 deletions

View File

@ -34,6 +34,7 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unbound.h>
#include "context.h"
@ -52,6 +53,8 @@ static struct getdns_list *create_from_ldns_list(struct getdns_context *,
ldns_rdf **, size_t);
static getdns_return_t set_os_defaults(struct getdns_context *);
static int transaction_id_cmp(const void *, const void *);
static int timeout_cmp(const void *, const void *);
static int transaction_id_timeout_cmp(const void *, const void *);
static void set_ub_string_opt(struct getdns_context *, char *, char *);
static void set_ub_number_opt(struct getdns_context *, char *, uint16_t);
static inline void clear_resolution_type_set_flag(struct getdns_context *, uint16_t);
@ -62,6 +65,14 @@ static void cancel_dns_req(getdns_dns_req *);
#define UNUSED_PARAM(x) ((void)(x))
#define RETURN_IF_NULL(ptr, code) if(ptr == NULL) return code;
/* structs */
typedef struct getdns_timeout_data {
getdns_transaction_t transaction_id;
struct timeval timeout_time;
getdns_context_timeout_callback callback;
void* userarg;
} getdns_timeout_data;
/**
* Helper to get default lookup namespaces.
* TODO: Determine from OS
@ -228,6 +239,63 @@ transaction_id_cmp(const void *id1, const void *id2)
}
}
static int timeout_cmp(const void *to1, const void *to2) {
if (to1 == NULL && to2 == NULL) {
return 0;
} else if (to1 == NULL && to2 != NULL) {
return 1;
} else if (to1 != NULL && to2 == NULL) {
return -1;
} else {
const getdns_timeout_data* t1 = (const getdns_timeout_data*) to1;
const getdns_timeout_data* t2 = (const getdns_timeout_data*) to2;
if (t1->timeout_time.tv_sec < t2->timeout_time.tv_sec) {
return -1;
} else if (t1->timeout_time.tv_sec > t2->timeout_time.tv_sec) {
return 1;
} else {
/* compare usec.. */
if (t1->timeout_time.tv_usec < t2->timeout_time.tv_usec) {
return -1;
} else if (t1->timeout_time.tv_usec > t2->timeout_time.tv_usec) {
return 1;
} else {
// compare the transactions
const getdns_timeout_data* t1 = (const getdns_timeout_data*) to1;
const getdns_timeout_data* t2 = (const getdns_timeout_data*) to2;
if (t1->transaction_id == t2->transaction_id) {
return 0;
} else if (t1->transaction_id < t2->transaction_id) {
return -1;
} else {
return 1;
}
}
}
}
}
static int transaction_id_timeout_cmp(const void *to1, const void *to2) {
if (to1 == NULL && to2 == NULL) {
return 0;
} else if (to1 == NULL && to2 != NULL) {
return 1;
} else if (to1 != NULL && to2 == NULL) {
return -1;
} else {
const getdns_timeout_data* t1 = (const getdns_timeout_data*) to1;
const getdns_timeout_data* t2 = (const getdns_timeout_data*) to2;
if (t1->transaction_id == t2->transaction_id) {
return 0;
} else if (t1->transaction_id < t2->transaction_id) {
return -1;
} else {
return 1;
}
}
}
/*
* getdns_context_create
*
@ -1177,4 +1245,18 @@ getdns_extension_set_eventloop(struct getdns_context* context,
return GETDNS_RETURN_GOOD;
}
getdns_return_t
getdns_context_schedule_timeout(struct getdns_context* context,
getdns_transaction_t id, uint16_t timeout, getdns_timeout_callback callback,
getdns_free_timeout_userarg_t free_func, void* userarg) {
}
getdns_return_t
getdns_context_clear_timeout(struct getdns_context* context,
getdns_transaction_t id) {
}
/* getdns_context.c */

View File

@ -39,6 +39,8 @@ struct ub_ctx;
/** function pointer typedefs */
typedef void (*getdns_update_callback) (struct getdns_context *, uint16_t);
typedef void (*getdns_timeout_callback) (struct getdns_context* context, getdns_transaction_t transaction_id, void* userarg);
typedef void (*getdns_free_timeout_userarg_t) (struct getdns_context* context, void* user_arg);
struct getdns_context {
@ -87,6 +89,14 @@ struct getdns_context {
* in the extension struct
*/
void* extension_data;
/*
* Timeout info one tree to manage timeout data
* keyed by transaction id. Second to manage by
* timeout
*/
struct ldns_rbtree_t *timeouts_by_id;
struct ldns_rbtree_t *timeouts_by_time;
};
/** internal functions **/
@ -120,4 +130,12 @@ void getdns_bindata_destroy(
getdns_return_t getdns_extension_set_eventloop(struct getdns_context* context,
getdns_eventloop_extension* extension, void* extension_data);
/* timeout scheduling */
getdns_return_t getdns_context_schedule_timeout(struct getdns_context* context,
getdns_transaction_t id, uint16_t timeout, getdns_timeout_callback callback,
getdns_free_timeout_userarg_t free_func, void* userarg);
getdns_return_t getdns_context_clear_timeout(struct getdns_context* context,
getdns_transaction_t id);
#endif /* _GETDNS_CONTEXT_H_ */

View File

@ -795,19 +795,12 @@ char *getdns_pretty_print_dict(const struct getdns_dict *some_dict);
char *getdns_display_ip_address(const struct getdns_bindata
*bindata_of_ipv4_or_ipv6_address);
/*
getdns_return_t getdns_context_set_context_update_callback(
struct getdns_context *context,
void (*value) (struct getdns_context *context, uint16_t changed_item)
);
*/
getdns_return_t
getdns_context_set_context_update_callback(
struct getdns_context * context,
void (*value)(struct getdns_context *context, uint16_t changed_item)
);
getdns_return_t
getdns_context_set_resolution_type(struct getdns_context *context, uint16_t value);

View File

@ -239,8 +239,17 @@ void dns_req_free(getdns_dns_req * req);
/* extensions */
typedef getdns_return_t (*getdns_eventloop_cleanup_t)(struct getdns_context* context, void* data);
typedef getdns_return_t (*getdns_eventloop_schedule_timeout_t)(struct getdns_context* context,
getdns_transaction_t id, uint16_t timeout, getdns_timeout_callback callback,
void* userarg, void* data);
typedef getdns_return_t (*getdns_eventloop_clear_timeout_t)(struct getdns_context* context,
getdns_transaction_t id, void* data);
typedef struct getdns_eventloop_extension {
getdns_eventloop_cleanup_t cleanup_data;
getdns_eventloop_schedule_timeout_t schedule_timeout;
getdns_eventloop_clear_timeout_t clear_timeout;
} getdns_eventloop_extension;