forked from atheme/atheme-contrib-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_resolve.c
79 lines (58 loc) · 1.76 KB
/
os_resolve.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (c) 2011 William Pitcock <[email protected]>
* Rights to this code are as documented in doc/LICENSE.
*
* Does an A record lookup.
*/
#include "atheme-compat.h"
DECLARE_MODULE_V1
(
"contrib/os_resolve", false, _modinit, _moddeinit,
PACKAGE_STRING,
VENDOR_STRING
);
static void os_cmd_resolve(sourceinfo_t *si, int parc, char *parv[]);
command_t cmd_resolve = { "RESOLVE", N_("Perform DNS lookup on hostname"), PRIV_ADMIN, 1, os_cmd_resolve, { .path = "contrib/os_resolve" } };
void _modinit(module_t *m)
{
service_named_bind_command("operserv", &cmd_resolve);
}
void _moddeinit(module_unload_intent_t intent)
{
service_named_unbind_command("operserv", &cmd_resolve);
}
typedef struct {
dns_query_t dns_query;
sourceinfo_t *si;
} resolve_req_t;
mowgli_heap_t *request_heap = NULL;
static void resolve_cb(void *vptr, dns_reply_t *reply)
{
resolve_req_t *req = vptr;
char buf[BUFSIZE];
return_if_fail(vptr != NULL);
return_if_fail(reply != NULL);
if (reply->addr.saddr.sa.sa_family != AF_INET)
return;
inet_ntop(reply->addr.saddr.sa.sa_family, &reply->addr.saddr.sin.sin_addr, buf, reply->addr.saddr_len);
command_success_nodata(req->si, "Result is %s", buf);
mowgli_heap_free(request_heap, req);
object_unref(req->si);
}
static void os_cmd_resolve(sourceinfo_t *si, int parc, char *parv[])
{
resolve_req_t *req;
if (request_heap == NULL)
request_heap = mowgli_heap_create(sizeof(resolve_req_t), 32, BH_LAZY);
if (!parv[0])
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "RESOLVE");
return;
}
req = mowgli_heap_alloc(request_heap);
req->si = si;
req->dns_query.ptr = req;
req->dns_query.callback = resolve_cb;
gethost_byname_type(parv[0], &req->dns_query, T_A);
object_ref(req->si);
}