aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/dns/UnboundResolver.java
blob: 309e58e131d8f9b6295977296550edd33d7fe6cb (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.dns;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

import net.java.sip.communicator.service.dns.*;
import net.java.sip.communicator.util.*;

import org.xbill.DNS.*;

/**
 * Implementation of the {@link Resolver} interface, wrapping the native NLnet
 * Labs Unbound resolver. Only the basic methods for queries are supported.
 *
 * @author Ingo Bauersachs
 */
public class UnboundResolver
    implements CustomResolver
{
    private final static Logger logger =
        Logger.getLogger(UnboundResolver.class);

    /**
     * Helper class to synchronize on asynchronous queries.
     */
    private static class CallbackData
    {
        /**
         * The resolver consumer that wishes to be informed when the request
         * completed.
         */
        ResolverListener listener;

        /**
         * The unbound session context.
         */
        long context;

        /**
         * The ID of the unbound async query.
         */
        int asyncId;

        /**
         * Java synchronization on top of unbound.
         */
        CountDownLatch sync = new CountDownLatch(1);
    }

    /**
     * Timeout for DNS queries.
     */
    private int timeout = 10000;

    /**
     * The recursive DNS servers answering our queries.
     */
    private String[] forwarders;

    /**
     * DNSSEC trust anchors for signed zones (usually for the root zone).
     */
    private List<String> trustAnchors = new LinkedList<String>();

    /**
     * Pool that executes our queries.
     */
    private ExecutorService threadPool;

    /**
     * Creates a new instance of this class.
     */
    public UnboundResolver()
    {
        threadPool = Executors.newCachedThreadPool();
    }

    /**
     * Sets a list of forwarders to use instead of the system default.
     *
     * @param forwarders list of servers to use for our queries.
     */
    public void setForwarders(String[] forwarders)
    {
        this.forwarders = forwarders;
    }

    /**
     * Clears any existing trust anchors previously added.
     */
    public void clearTrustAnchors()
    {
        trustAnchors.clear();
    }

    /**
     * Adds a DNSSEC trust anchor validation of the DNSKEYs.
     *
     * @param anchor trust anchor in the form of
     *            "'zone' IN DS 'key tag' 'algorithm' 'digest type' 'digest'"
     */
    public void addTrustAnchor(String anchor)
    {
        trustAnchors.add(anchor);
    }

    /**
     * {@inheritDoc}
     */
    public SecureMessage send(final Message query) throws IOException
    {
        Future<SecureMessage> future = threadPool.submit(
            new Callable<SecureMessage>()
        {
            public SecureMessage call() throws Exception
            {
                if(logger.isDebugEnabled())
                    logger.debug(query);

                SecureMessage secureMessage = null;
                final long context = prepareContext();
                try
                {
                    UnboundResult result = UnboundApi.resolve(
                        context,
                        query.getQuestion().getName().toString(),
                        query.getQuestion().getType(),
                        query.getQuestion().getDClass()
                        );
                    secureMessage = new SecureMessage(result);
                    validateMessage(secureMessage);
                }
                finally
                {
                    UnboundApi.deleteContext(context);
                    if(logger.isDebugEnabled() && secureMessage != null)
                        logger.debug(secureMessage);
                }

                return secureMessage;
            }
        });
        try
        {
            return future.get(timeout, TimeUnit.SECONDS);
        }
        catch (InterruptedException e)
        {
            logger.error(e);
            throw new IOException(e.getMessage());
        }
        catch (ExecutionException e)
        {
            if(e.getCause() instanceof DnssecRuntimeException)
                throw new DnssecRuntimeException(e.getCause().getMessage());
            logger.error(e);
            throw new IOException(e.getMessage());
        }
        catch (TimeoutException e)
        {
            throw new SocketTimeoutException(e.getMessage());
        }
    }

    /**
     * Method to allow overriders to inspect the message. This class'
     * implementation does nothing.
     *
     * @param msg The message to inspect.
     * @throws DnssecRuntimeException if the inspector does not want the code to
     *             continue normal processing of the answer.
     */
    protected void validateMessage(SecureMessage msg)
        throws DnssecRuntimeException
    {
    }

    /**
     * Prepares a unbound session context initialized with forwarders and trust
     * anchors.
     *
     * @return The context id
     */
    private long prepareContext()
    {
        final long context = UnboundApi.createContext();
        if(logger.isTraceEnabled())
            UnboundApi.setDebugLevel(context, 100);
        for(String fwd : forwarders == null
            ? ResolverConfig.getCurrentConfig().servers()
            : forwarders)
        {
            fwd = fwd.trim();
            if(NetworkUtils.isValidIPAddress(fwd))
            {
                if(fwd.startsWith("["))
                    fwd = fwd.substring(1, fwd.length() - 1);
                UnboundApi.setForwarder(context, fwd);
            }
        }
        for(String anchor : trustAnchors)
        {
            UnboundApi.addTrustAnchor(context, anchor);
        }
        return context;
    }

    /**
     * Cleans up an Unbound session context.
     *
     * @param cbData The helper object of the asynchronous call.
     * @param cancelAsync Whether an outstanding asynchronous unbound query
     *            should be canceled.
     */
    private static synchronized void deleteContext(CallbackData cbData,
        boolean cancelAsync)
    {
        if(cbData.context == 0)
            return;

        if(cancelAsync)
        {
            try
            {
                UnboundApi.cancelAsync(cbData.context, cbData.asyncId);
            }
            catch (UnboundException ignore)
            {}
        }
        UnboundApi.deleteContext(cbData.context);
        cbData.context = 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.xbill.DNS.Resolver#sendAsync(org.xbill.DNS.Message,
     * org.xbill.DNS.ResolverListener)
     */
    public CallbackData sendAsync(Message query, ResolverListener listener)
    {
        if(listener == null)
            throw new IllegalArgumentException("listener cannot be null");

        final long context = prepareContext();
        final CallbackData cbData = new CallbackData();
        cbData.listener = listener;
        cbData.context = context;
        int asyncId;
        try
        {
            asyncId = UnboundApi.resolveAsync(
                context,
                query.getQuestion().getName().toString(),
                query.getQuestion().getType(),
                query.getQuestion().getDClass(),
                cbData,
                new UnboundApi.UnboundCallback()
                {
                    public void UnboundResolveCallback(Object data, int err,
                        UnboundResult result)
                    {
                        CallbackData cbData = (CallbackData)data;
                        deleteContext(cbData, false);

                        ResolverListener l = cbData.listener;
                        if(err == 0)
                        {
                            try
                            {
                                l.receiveMessage(data,
                                    new SecureMessage(result));
                            }
                            catch (IOException e)
                            {
                                l.handleException(data, e);
                            }
                        }
                        else
                            l.handleException(data,
                                new Exception(
                                    UnboundApi.errorCodeToString(err)));

                        cbData.sync.countDown();
                    }
                }
            );
        }
        catch (UnboundException e)
        {
            listener.handleException(null, e);
            return null;
        }
        cbData.asyncId = asyncId;
        threadPool.execute(new Runnable()
        {
            public void run()
            {
                try
                {
                    UnboundApi.processAsync(context);
                }
                catch(UnboundException ex)
                {
                    cbData.listener.handleException(this, ex);
                    deleteContext(cbData, false);
                    cbData.sync.countDown();
                }
            }
        });
        return cbData;
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    public void setEDNS(int level)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    @SuppressWarnings("rawtypes")
    public void setEDNS(int level, int payloadSize, int flags, List options)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    public void setIgnoreTruncation(boolean flag)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    public void setPort(int port)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    public void setTCP(boolean flag)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Not supported.
     * @throws UnsupportedOperationException
     */
    public void setTSIGKey(TSIG key)
    {
        throw new UnsupportedOperationException();
    }

    /* (non-Javadoc)
     * @see org.xbill.DNS.Resolver#setTimeout(int)
     */
    public void setTimeout(int secs)
    {
        timeout = secs * 1000;
    }

    /* (non-Javadoc)
     * @see org.xbill.DNS.Resolver#setTimeout(int, int)
     */
    public void setTimeout(int secs, int msecs)
    {
        timeout = secs * 1000 + msecs;
    }

    /**
     * Does nothing.
     */
    public void reset()
    {
    }
}