aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/dns/DnsJavaLogger.java
blob: bcf7982c0de160b7f01618b572519b87b15c3ad7 (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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.dns;

import net.java.sip.communicator.util.*;
import org.jitsi.service.packetlogging.*;
import org.xbill.DNS.*;

import java.net.*;

/**
 * Custom logger that will log packages using packet logging service.
 *
 * @author Damian Minkov
 */
public class DnsJavaLogger
    implements PacketLogger
{
    /**
     * The logger.
     */
    private static final Logger logger
        = Logger.getLogger(DnsJavaLogger.class);

    /**
     * The packet logging service.
     */
    private PacketLoggingService packetLoggingService = null;

    /**
     * Obtain packet logging service.
     * @return
     */
    private PacketLoggingService getPacketLoggingService()
    {
        if(packetLoggingService == null
            && UtilActivator.bundleContext != null)
        {
            packetLoggingService = ServiceUtils.getService(
                UtilActivator.bundleContext,
                PacketLoggingService.class);
        }

        return packetLoggingService;
    }

    @Override
    public void log(String prefix,
                    SocketAddress local,
                    SocketAddress remote,
                    byte[] data)
    {
        // make sure that error here will not stop further processing
        try
        {
            logInternal(local, remote, prefix, data);
        }
        catch(Throwable t)
        {
            logger.error("Error saving packet", t);
        }
    }

    /**
     * Logs the dns packet, checking its prefix message to distinguish
     * incoming and outgoing messages and the transport used TCP or UDP.
     *
     * @param local the local address
     * @param remote the remote address
     * @param prefix the prefix used by the dns lib
     * @param data the data that is send or received through the wire
     */
    private void logInternal(SocketAddress local,
                    SocketAddress remote,
                    String prefix, byte[] data)
    {
        if(getPacketLoggingService() == null
            || !(local instanceof InetSocketAddress
                && remote instanceof InetSocketAddress))
        {
            return;
        }

        InetSocketAddress localAddress = (InetSocketAddress)local;
        InetSocketAddress remoteAddress = (InetSocketAddress)remote;

        PacketLoggingService.TransportName transportName
            = PacketLoggingService.TransportName.UDP;

        if(prefix.contains("TCP"))
            transportName = PacketLoggingService.TransportName.TCP;

        boolean isSender = true;
        if(prefix.contains("read"))
            isSender = false;

        byte[] srcAddr;
        int srcPort;
        byte[] dstAddr;
        int dstPort;

        if(isSender)
        {
            srcAddr = localAddress.getAddress().getAddress();
            srcPort = localAddress.getPort();
            dstAddr = remoteAddress.getAddress().getAddress();
            dstPort = remoteAddress.getPort();
        }
        else
        {
            dstAddr = localAddress.getAddress().getAddress();
            dstPort = localAddress.getPort();
            srcAddr = remoteAddress.getAddress().getAddress();
            srcPort = remoteAddress.getPort();
        }

        getPacketLoggingService().logPacket(
            PacketLoggingService.ProtocolName.DNS,
            srcAddr,
            srcPort,
            dstAddr,
            dstPort,
            transportName,
            isSender,
            data,
            0,
            data.length);
    }
}