summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2007-02-16 13:51:03 +0000
committerLennart Poettering <lennart@poettering.net>2007-02-16 13:51:03 +0000
commit5d93f3bb4cd305969a97167de32132f2c7eee3e9 (patch)
tree092a09708262c42938d7fc8838faba41d82fcda7
parent7c1866ca71f33f7a3816efe6a4f40b19bc1bdc58 (diff)
minor code cleanusp:
* reindent * add missing config.h inclusion to asyncns-test.c * add few missing consts in casting git-svn-id: file:///home/lennart/svn/public/libasyncns/trunk@27 cc0fb855-19ed-0310-866e-8c1d96e4abae
-rw-r--r--libasyncns/asyncns-test.c82
-rw-r--r--libasyncns/asyncns.c81
2 files changed, 83 insertions, 80 deletions
diff --git a/libasyncns/asyncns-test.c b/libasyncns/asyncns-test.c
index a59ee30..46e7d5a 100644
--- a/libasyncns/asyncns-test.c
+++ b/libasyncns/asyncns-test.c
@@ -19,6 +19,10 @@
USA.
***/
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -60,9 +64,10 @@ int main(int argc, char *argv[]) {
q2 = asyncns_getnameinfo(asyncns, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
+ /* Make a res_query() call */
q3 = asyncns_res_query(asyncns, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
- /* Wait until the two queries are completed */
+ /* Wait until the three queries are completed */
while (!asyncns_isdone(asyncns, q1)
|| !asyncns_isdone(asyncns, q2)
|| !asyncns_isdone(asyncns, q3)) {
@@ -101,46 +106,45 @@ int main(int argc, char *argv[]) {
if ((ret = asyncns_res_done(asyncns, q3, &srv)) < 0) {
fprintf(stderr, "error: %s %i\n", strerror(ret), ret);
} else if (ret == 0) {
- fprintf(stderr, "No reply for SRV lookup\n");
+ fprintf(stderr, "No reply for SRV lookup\n");
} else {
- int qdcount;
- int ancount;
- int len;
- const unsigned char *pos = srv + sizeof(HEADER);
- unsigned char *end = srv + ret;
- HEADER *head = (HEADER *)srv;
-
- qdcount = ntohs(head->qdcount);
- ancount = ntohs(head->ancount);
-
- char name[256];
-
- printf("%d answers for srv lookup:\n", ancount);
-
- /* Ignore the questions */
- while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- assert(len >= 0);
- pos += len + QFIXEDSZ;
- }
-
- /* Parse the answers */
- while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- /* Ignore the initial string */
- uint16_t pref, weight, port;
- assert(len >= 0);
- pos += len;
- /* Ignore type, ttl, class and dlen */
- pos += 10;
+ int qdcount;
+ int ancount;
+ int len;
+ const unsigned char *pos = srv + sizeof(HEADER);
+ unsigned char *end = srv + ret;
+ HEADER *head = (HEADER *)srv;
+ char name[256];
+
+ qdcount = ntohs(head->qdcount);
+ ancount = ntohs(head->ancount);
+
+ printf("%d answers for srv lookup:\n", ancount);
+
+ /* Ignore the questions */
+ while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
+ assert(len >= 0);
+ pos += len + QFIXEDSZ;
+ }
+
+ /* Parse the answers */
+ while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
+ /* Ignore the initial string */
+ uint16_t pref, weight, port;
+ assert(len >= 0);
+ pos += len;
+ /* Ignore type, ttl, class and dlen */
+ pos += 10;
- GETSHORT(pref, pos);
- GETSHORT(weight, pos);
- GETSHORT(port, pos);
- len = dn_expand(srv, end, pos, name, 255);
- printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
- pref, weight, port, name);
-
- pos += len;
- }
+ GETSHORT(pref, pos);
+ GETSHORT(weight, pos);
+ GETSHORT(port, pos);
+ len = dn_expand(srv, end, pos, name, 255);
+ printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
+ pref, weight, port, name);
+
+ pos += len;
+ }
}
r = 0;
diff --git a/libasyncns/asyncns.c b/libasyncns/asyncns.c
index e312e36..60d8e24 100644
--- a/libasyncns/asyncns.c
+++ b/libasyncns/asyncns.c
@@ -41,7 +41,6 @@
#include <arpa/nameser.h>
#include <resolv.h>
-
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
@@ -148,15 +147,15 @@ typedef struct nameinfo_response {
} nameinfo_response_t;
typedef struct res_query_request {
- struct rheader header;
- int class;
- int type;
- size_t dlen;
+ struct rheader header;
+ int class;
+ int type;
+ size_t dlen;
} res_request_t;
typedef struct res_query_response {
- struct rheader header;
- int ret;
+ struct rheader header;
+ int ret;
} res_response_t;
#ifndef HAVE_STRNDUP
@@ -318,7 +317,7 @@ static int handle_request(int out_fd, const rheader_t *req, size_t length) {
switch (req->type) {
case REQUEST_ADDRINFO: {
struct addrinfo ai, *result = NULL;
- const addrinfo_request_t *ai_req = (addrinfo_request_t*) req;
+ const addrinfo_request_t *ai_req = (const addrinfo_request_t*) req;
const char *node, *service;
int ret;
@@ -344,7 +343,7 @@ static int handle_request(int out_fd, const rheader_t *req, size_t length) {
case REQUEST_NAMEINFO: {
int ret;
- const nameinfo_request_t *ni_req = (nameinfo_request_t*) req;
+ const nameinfo_request_t *ni_req = (const nameinfo_request_t*) req;
char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
const struct sockaddr *sa;
@@ -365,24 +364,24 @@ static int handle_request(int out_fd, const rheader_t *req, size_t length) {
case REQUEST_RES_QUERY:
case REQUEST_RES_SEARCH: {
- int ret;
- unsigned char answer[BUFSIZE];
- const res_request_t *res_req = (res_request_t *)req;
- const char *dname;
-
- assert(length >= sizeof(res_request_t));
- assert(length == sizeof(res_request_t) + res_req->dlen);
-
- dname = (const char *) req + sizeof(res_request_t);
-
- if (req->type == REQUEST_RES_QUERY) {
- ret = res_query(dname, res_req->class, res_req->type,
- answer, BUFSIZE);
- } else {
- ret = res_search(dname, res_req->class, res_req->type,
- answer, BUFSIZE);
- }
- return send_res_reply(out_fd, req->id, answer, ret);
+ int ret;
+ unsigned char answer[BUFSIZE];
+ const res_request_t *res_req = (const res_request_t *)req;
+ const char *dname;
+
+ assert(length >= sizeof(res_request_t));
+ assert(length == sizeof(res_request_t) + res_req->dlen);
+
+ dname = (const char *) req + sizeof(res_request_t);
+
+ if (req->type == REQUEST_RES_QUERY) {
+ ret = res_query(dname, res_req->class, res_req->type,
+ answer, BUFSIZE);
+ } else {
+ ret = res_search(dname, res_req->class, res_req->type,
+ answer, BUFSIZE);
+ }
+ return send_res_reply(out_fd, req->id, answer, ret);
}
case REQUEST_TERMINATE: {
@@ -751,10 +750,10 @@ static int handle_response(asyncns_t *asyncns, rheader_t *resp, size_t length) {
q->ret = ni_resp->ret;
if (ni_resp->hostlen)
- q->host = strndup((char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1);
+ q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1);
if (ni_resp->servlen)
- q->serv = strndup((char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1);
+ q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1);
complete_query(asyncns, q);
@@ -762,20 +761,20 @@ static int handle_response(asyncns_t *asyncns, rheader_t *resp, size_t length) {
}
case RESPONSE_RES: {
- const res_response_t *res_resp = (res_response_t *)resp;
+ const res_response_t *res_resp = (res_response_t *)resp;
- assert(length >= sizeof(res_response_t));
- assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
+ assert(length >= sizeof(res_response_t));
+ assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
- q->ret = res_resp->ret;
+ q->ret = res_resp->ret;
- if (res_resp->ret >= 0) {
- q->serv = malloc(res_resp->ret);
- memcpy(q->serv, (char *)resp + sizeof(res_response_t), res_resp->ret);
- }
+ if (res_resp->ret >= 0) {
+ q->serv = malloc(res_resp->ret);
+ memcpy(q->serv, (char *)resp + sizeof(res_response_t), res_resp->ret);
+ }
- complete_query(asyncns, q);
- break;
+ complete_query(asyncns, q);
+ break;
}
default:
@@ -1029,11 +1028,11 @@ fail:
}
asyncns_query_t* asyncns_res_query(asyncns_t *asyncns, const char *dname, int class, int type) {
- return asyncns_res(asyncns, REQUEST_RES_QUERY, dname, class, type);
+ return asyncns_res(asyncns, REQUEST_RES_QUERY, dname, class, type);
}
asyncns_query_t* asyncns_res_search(asyncns_t *asyncns, const char *dname, int class, int type) {
- return asyncns_res(asyncns, REQUEST_RES_SEARCH, dname, class, type);
+ return asyncns_res(asyncns, REQUEST_RES_SEARCH, dname, class, type);
}
int asyncns_res_done(asyncns_t *asyncns, asyncns_query_t* q, unsigned char **answer) {