Created
May 25, 2011 02:24
uv dns api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef enum { | |
UV_DNS_A, | |
UV_DNS_AAAA, | |
UV_DNS_PTR, | |
UV_DNS_MX, | |
UV_DNS_SRV, | |
... etc ... | |
} uv_dns_type_t; | |
typedef struct { | |
uv_dns_type_t type; | |
union { | |
struct { | |
unsigned int addr; | |
} a; | |
struct { | |
unsigned char addr[16]; | |
} aaaa; | |
struct { | |
/* name may not be null-terminated. observe name_len. */ | |
char* name; | |
size_t name_len; | |
} ptr; | |
struct { | |
char* name; | |
size_t name_len; | |
unsigned short port; | |
unsigned short priority; | |
unsigned short weight; | |
} srv; | |
struct { | |
char* name; | |
size_t name_len; | |
unsigned short preference; | |
} mx; | |
}; | |
} uv_dns_record_t; | |
int uv_dns_query(uv_req_t* req, char* name, uv_dns_type_t class); | |
void dns_cb(uv_req_t* req, int status) { | |
char *name; | |
assert(status == 0); | |
uv_dns_record_t record; | |
while (uv_dns_get_record(req, &record)) { | |
switch (record->type) { | |
case UV_DNS_A: | |
printf("addr is %u\n", record.a.addr); | |
break; | |
case UV_DNS_PTR: | |
name = strndup(record.ptr.name, record.ptr.name_len); | |
printf("points to %s\n", name); | |
free(name); | |
break; | |
case UV_DNS_SRV: | |
name = strndup(record.ptr.name, record.ptr.name_len); | |
printf("%s %hu %hu %hu\n", name, record.srv.port, record.srv.priority, record.srv.weight); | |
free(name); | |
break; | |
... etc ... | |
} | |
} | |
/* Allow uv to free the dns result buffer associated with the req. */ | |
/* This implies that all string pointers reported by uv_dns_get_record (eg record.ptr.name) become invalid. */ | |
uv_dns_cleanup(req); | |
} | |
int main() { | |
uv_handle_t handle; | |
uv_req_t req; | |
... | |
uv_dns_init(&handle, NULL, NULL); /* Do we really need a handle for it? */ | |
uv_req_init(&req, &handle, dns_cb); | |
uv_dns_query(&req, "www.cloudkick.com", UV_DNS_A); | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment