reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ares_gethostbyaddr.c
Go to the documentation of this file.
1/* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
14 */
15
16
17#include <sys/types.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#ifndef WIN32
23#include <sys/socket.h>
24#include <netinet/in.h>
25#ifndef __CYGWIN__
26# include "ares_nameser.h"
27#endif
28#include <netdb.h>
29#endif
30
31#include "ares.h"
32#include "ares_private.h"
33
34struct addr_query {
35 /* Arguments passed to ares_gethostbyaddr() */
37 struct in_addr addr;
39 void *arg;
40
41 const char *remaining_lookups;
42};
43
44static void next_lookup(struct addr_query *aquery);
45static void addr_callback(void *arg, int status, unsigned char *abuf,
46 int alen);
47static void end_aquery(struct addr_query *aquery, int status,
48 struct hostent *host);
49static int file_lookup(struct in_addr *addr, struct hostent **host);
50
51#ifdef WIN32
52extern char w32hostspath[];
53#endif
54
55void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
56 int family, ares_host_callback callback, void *arg)
57{
58 struct addr_query *aquery;
59
60 if (family != AF_INET || addrlen != sizeof(struct in_addr))
61 {
63 return;
64 }
65
66 aquery = malloc(sizeof(struct addr_query));
67 if (!aquery)
68 {
70 return;
71 }
72 aquery->channel = channel;
73 memcpy(&aquery->addr, addr, sizeof(aquery->addr));
74 aquery->callback = callback;
75 aquery->arg = arg;
76 aquery->remaining_lookups = channel->lookups;
77
79}
80
81static void next_lookup(struct addr_query *aquery)
82{
83 const char *p;
84 char name[64];
85 int a1, a2, a3, a4, status;
86 struct hostent *host;
87 unsigned long addr;
88
89 for (p = aquery->remaining_lookups; *p; p++)
90 {
91 switch (*p)
92 {
93 case 'b':
94 addr = ntohl(aquery->addr.s_addr);
95 a1 = (addr >> 24) & 0xff; // .kw. in case signed-ness changes
96 a2 = (addr >> 16) & 0xff;
97 a3 = (addr >> 8) & 0xff;
98 a4 = addr & 0xff;
99 snprintf(name, sizeof(name), "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
100 aquery->remaining_lookups = p + 1;
101 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
102 aquery);
103 return;
104 case 'f':
105 status = file_lookup(&aquery->addr, &host);
106 if (status != ARES_ENOTFOUND)
107 {
108 end_aquery(aquery, status, host);
109 return;
110 }
111 break;
112 }
113 }
115}
116
117static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
118{
119 struct addr_query *aquery = (struct addr_query *) arg;
120 struct hostent *host;
121
122 if (status == ARES_SUCCESS)
123 {
124 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
125 sizeof(struct in_addr), AF_INET, &host);
126 end_aquery(aquery, status, host);
127 }
128 else if (status == ARES_EDESTRUCTION)
129 end_aquery(aquery, status, NULL);
130 else
132}
133
134static void end_aquery(struct addr_query *aquery, int status,
135 struct hostent *host)
136{
137 aquery->callback(aquery->arg, status, host);
138 if (host)
139 ares_free_hostent(host);
140 free(aquery);
141}
142
143static int file_lookup(struct in_addr* addr, struct hostent** host)
144{
145 FILE* fp;
146 int status;
147
148#ifdef WIN32
149 fopen_s(&fp, w32hostspath, "r");
150#else
151 fp = fopen(PATH_HOSTS, "r");
152#endif
153 if (!fp)
154 return ARES_ENOTFOUND;
155
156 while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
157 {
158 if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
159 break;
160 ares_free_hostent(*host);
161 }
162 fclose(fp);
163 if (status == ARES_EOF)
164 status = ARES_ENOTFOUND;
165 if (status != ARES_SUCCESS)
166 *host = NULL;
167 return status;
168}
static void callback(void *arg, int status, unsigned char *abuf, int alen)
Definition adig.c:388
#define ARES_SUCCESS
Definition ares.h:79
#define ARES_ENOMEM
Definition ares.h:98
#define ARES_EOF
Definition ares.h:96
#define ARES_ENOTFOUND
Definition ares.h:85
#define ARES_EDESTRUCTION
Definition ares.h:99
#define ARES_ENOTIMP
Definition ares.h:86
void ares_free_hostent(struct hostent *host)
void(* ares_host_callback)(void *arg, int status, struct hostent *hostent)
Definition ares.h:174
void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition ares_query.c:38
int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, int addrlen, int family, struct hostent **host)
int ares__get_hostent(FILE *fp, struct hostent **host)
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, int family, ares_host_callback callback, void *arg)
static void next_lookup(struct addr_query *aquery)
static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
static int file_lookup(struct in_addr *addr, struct hostent **host)
static void end_aquery(struct addr_query *aquery, int status, struct hostent *host)
#define T_PTR
#define C_IN
#define PATH_HOSTS
const char * remaining_lookups
ares_channel channel
struct in_addr addr
ares_host_callback callback
void list()
Definition userAdmin.cxx:66