From 3c0094df7568611f9d2a63e12384b88c62026988 Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Sat, 24 Apr 2010 18:25:54 -0700 Subject: [PATCH] make whitespace consistent, make emacs format code appropriately --- .dir-locals.el | 5 ++ .gitignore | 2 +- named.c | 132 +++++++++++++++++++++++++------------------------ 3 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 .dir-locals.el diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..75adedc --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,5 @@ +((nil . ((indent-tabs-mode . nil) + (tab-width . 4) + (fill-column . 80))) + (c-mode . ((c-file-style . "BSD") + (c-basic-offset . 4)))) diff --git a/.gitignore b/.gitignore index b8cd647..97e96fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.sw? - +bin diff --git a/named.c b/named.c index ab7349b..e44eb8f 100644 --- a/named.c +++ b/named.c @@ -58,62 +58,63 @@ const char *COL_NAME = "name"; // Definitions -static void query_name(const char *name, QueryClass qclass, QueryType qtype, AnswerFunc callback) { - char *error_msg = NULL; - char sql[256 + strlen(name)]; +static void query_name(const char *name, QueryClass qclass, QueryType qtype, AnswerFunc callback) +{ + char *error_msg = NULL; + char sql[256 + strlen(name)]; fprintf(stderr, "query: %s, class: %d, type: %d\n", name, qtype, qclass); if (qtype != WildcardQueryType && qclass != WildcardQueryClass) sprintf(sql, "SELECT name, qtype, qclass, data, ttl FROM responses WHERE name = '%s' AND qclass = %d AND qtype = %d", name, (int)qclass, (int)qtype); - else if (qtype == WildcardQueryType) + else if (qtype == WildcardQueryType) sprintf(sql, "SELECT name, qtype, qclass, data, ttl FROM responses WHERE name = '%s' AND qclass = %d", name, (int)qclass); - else if (qclass == WildcardQueryClass) + else if (qclass == WildcardQueryClass) sprintf(sql, "SELECT name, qtype, qclass, data, ttl FROM responses WHERE name = '%s' AND qtype = %d", name, (int)qtype); - else + else sprintf(sql, "SELECT name, qtype, qclass, data, ttl FROM responses WHERE name = '%s'", name); - int query_name_response(void *ctx, int col_count, char **data, char **column_names) { - const char *response_data = ""; - const char *response_name = ""; - QueryClass response_qclass = InternetQueryClass; - QueryType response_qtype = 0; - int response_ttl = TTL; - int response_data_len = 0; - for (int i = 0; i < col_count; i++) { - const char *col = column_names[i]; - const char *val = data[i]; - if (val == NULL) - continue; - if (strcmp(col, COL_DATA) == 0) { - response_data = val; - response_data_len = strlen(val); - } else if (strcmp(col, COL_TTL) == 0) - response_ttl = atoi(val); - else if (strcmp(col, COL_NAME) == 0) - response_name = val; - else if (strcmp(col, COL_QCLASS) == 0) - response_qclass = atoi(val); - else if (strcmp(col, COL_QTYPE) == 0) - response_qtype = atoi(val); - } - - if (response_qtype == TxtQueryType) { - int buf_size = response_data_len + (response_data_len / 255) + 16; - char *buf = alloca(buf_size); - fmt_txt_buf(response_data, strlen(response_data), buf, &buf_size, 255); - response_data = buf; - response_data_len = buf_size; - } - callback(response_name, response_data, response_data_len, response_ttl, response_qclass, response_qtype); - return 0; - } - - int rc = sqlite3_exec(db, sql, query_name_response, 0, &error_msg); - if (rc != SQLITE_OK) { - fprintf(stderr, "SQL Error: %s\n", error_msg); - sqlite3_free(error_msg); - exit(1); - } + int query_name_response(void *ctx, int col_count, char **data, char **column_names) { + const char *response_data = ""; + const char *response_name = ""; + QueryClass response_qclass = InternetQueryClass; + QueryType response_qtype = 0; + int response_ttl = TTL; + int response_data_len = 0; + for (int i = 0; i < col_count; i++) { + const char *col = column_names[i]; + const char *val = data[i]; + if (val == NULL) + continue; + if (strcmp(col, COL_DATA) == 0) { + response_data = val; + response_data_len = strlen(val); + } else if (strcmp(col, COL_TTL) == 0) + response_ttl = atoi(val); + else if (strcmp(col, COL_NAME) == 0) + response_name = val; + else if (strcmp(col, COL_QCLASS) == 0) + response_qclass = atoi(val); + else if (strcmp(col, COL_QTYPE) == 0) + response_qtype = atoi(val); + } + + if (response_qtype == TxtQueryType) { + int buf_size = response_data_len + (response_data_len / 255) + 16; + char *buf = alloca(buf_size); + fmt_txt_buf(response_data, strlen(response_data), buf, &buf_size, 255); + response_data = buf; + response_data_len = buf_size; + } + callback(response_name, response_data, response_data_len, response_ttl, response_qclass, response_qtype); + return 0; + } + + int rc = sqlite3_exec(db, sql, query_name_response, 0, &error_msg); + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL Error: %s\n", error_msg); + sqlite3_free(error_msg); + exit(1); + } } @@ -121,13 +122,14 @@ int count_char(uint8_t *buf, int buf_len, uint8_t byte) { int num = 0; int i =0; for (int i = 0; i < buf_len; i++) { - if (buf[i] == byte) - num++; + if (buf[i] == byte) + num++; } return num; } -static void fmt_txt_buf(const uint8_t *in_data, int in_len, uint8_t *out_data, int *out_len, uint8_t max_chunk_size) { +static void fmt_txt_buf(const uint8_t *in_data, int in_len, uint8_t *out_data, int *out_len, uint8_t max_chunk_size) +{ int remaining = in_len; int written = 0; for (int i = 0; i < in_len && written < *out_len; i++) { @@ -138,19 +140,20 @@ static void fmt_txt_buf(const uint8_t *in_data, int in_len, uint8_t *out_data, i written++; } out_data[written] = in_data[i]; - written++; + written++; } *out_len = written; -} +} -static void evdns_server_callback(struct evdns_server_request *req, void *data) { +static void evdns_server_callback(struct evdns_server_request *req, void *data) +{ int ttl = 300; fprintf(stderr, "request\n"); fprintf(stderr, "replying to request\n"); for (int i = 0; i < req->nquestions; i++) { struct evdns_server_question *question = req->questions[i]; - void on_answer(const char *name, const char *data, int data_len, int ttl, QueryClass qclass, QueryType qtype) { + void on_answer(const char *name, const char *data, int data_len, int ttl, QueryClass qclass, QueryType qtype) { fprintf(stderr, "answer name:%s, data:%s\n", name, data); EV_CHECK("add reply", evdns_server_request_add_reply( req, @@ -166,33 +169,33 @@ static void evdns_server_callback(struct evdns_server_request *req, void *data) query_name(question->name, question->dns_question_class, question->type, on_answer); } EV_CHECK("respond", evdns_server_request_respond(req, 0)); - + fprintf(stderr, "responded\n"); } -static void logger(int is_warn, const char *msg) { +static void logger(int is_warn, const char *msg) +{ fprintf(stderr, "%s: %s\n", is_warn ? "WARN" : "INFO", msg); } -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ + int rc; + int sock; + struct sockaddr_in my_addr; struct event_base *event_base = NULL; struct evdns_base *evdns_base = NULL; - { - int rc = sqlite3_open(argv[1], &db); - if (rc){ + if (rc){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); exit(1); - } } event_base = event_base_new(); evdns_base = evdns_base_new(event_base, 0); evdns_set_log_fn(logger); - int sock; - struct sockaddr_in my_addr; sock = socket(PF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("socket"); @@ -214,4 +217,3 @@ int main(int argc, char **argv) { } #undef EV_CHECK -