summaryrefslogtreecommitdiffstats
path: root/ui/qt/models/resolved_addresses_models.cpp
blob: 2555777198ccf5ab9e3434b66ca5835467a9a5c8 (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
/* resolved_addresses_models.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/models/resolved_addresses_models.h>

#include "file.h"

#include "epan/addr_resolv.h"
#include <wiretap/wtap.h>

extern "C"
{

static void
serv_port_hash_to_qstringlist(void *key, void *value, void *member_ptr)
{
    PortsModel *model = static_cast<PortsModel *>(member_ptr);
    serv_port_t *serv_port = (serv_port_t *)value;
    unsigned port = GPOINTER_TO_UINT(key);

    if (serv_port->tcp_name) {
        QStringList entries;

        entries << serv_port->tcp_name;
        entries << QString::number(port);
        entries << "tcp";
        model->appendRow(entries);
    }
    if (serv_port->udp_name) {
        QStringList entries;

        entries << serv_port->udp_name;
        entries << QString::number(port);
        entries << "udp";
        model->appendRow(entries);
    }
    if (serv_port->sctp_name) {
        QStringList entries;

        entries << serv_port->sctp_name;
        entries << QString::number(port);
        entries << "sctp";
        model->appendRow(entries);
    }
    if (serv_port->dccp_name) {
        QStringList entries;

        entries << serv_port->dccp_name;
        entries << QString::number(port);
        entries << "dccp";
        model->appendRow(entries);
    }
}

static void
ipv4_hash_table_resolved_to_list(void *, void *value, void *sl_ptr)
{
    QList<QStringList> *hosts = (QList<QStringList> *) sl_ptr;
    hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *) value;

    if ((ipv4_hash_table_entry->flags & (USED_AND_RESOLVED_MASK)) == USED_AND_RESOLVED_MASK) {
        *hosts << (QStringList() << QString(ipv4_hash_table_entry->ip) << QString(ipv4_hash_table_entry->name));
    }
}

static void
ipv6_hash_table_resolved_to_list(void *, void *value, void *sl_ptr)
{
    QList<QStringList> *hosts = (QList<QStringList> *) sl_ptr;
    hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *) value;

    if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
        *hosts << (QStringList() << QString(ipv6_hash_table_entry->ip6) << QString(ipv6_hash_table_entry->name));
    }
}

static void
eth_hash_to_qstringlist(void *, void *value, void *sl_ptr)
{
    QList<QStringList> *values = (QList<QStringList> *) sl_ptr;
    hashether_t* tp = (hashether_t*)value;

    if (get_hash_ether_used(tp)) {
        *values << (QStringList() << QString(get_hash_ether_hexaddr(tp)) << QString(get_hash_ether_resolved_name(tp)));
    }
}

static void
manuf_hash_to_qstringlist(void *key, void *value, void *sl_ptr)
{
    QList<QStringList> *values = (QList<QStringList> *) sl_ptr;
    hashmanuf_t *manuf = (hashmanuf_t*)value;
    unsigned eth_as_uint = GPOINTER_TO_UINT(key);

    if (get_hash_manuf_used(manuf)) {
        QString entry = QString("%1:%2:%3")
                .arg((eth_as_uint >> 16 & 0xff), 2, 16, QChar('0'))
                .arg((eth_as_uint >>  8 & 0xff), 2, 16, QChar('0'))
                .arg((eth_as_uint & 0xff), 2, 16, QChar('0'));

        *values << (QStringList() << entry << QString(get_hash_manuf_resolved_name(manuf)));
    }
}

static void
wka_hash_to_qstringlist(void *key, void *value, void *sl_ptr)
{
    QList<QStringList> *values = (QList<QStringList> *) sl_ptr;
    hashwka_t *wkahash = (hashwka_t *)value;
    uint8_t *eth_addr = (uint8_t*)key;

    if (get_hash_wka_used(wkahash)) {
        QString entry = QString("%1:%2:%3:%4:%5:%6")
                .arg(eth_addr[0], 2, 16, QChar('0'))
                .arg(eth_addr[1], 2, 16, QChar('0'))
                .arg(eth_addr[2], 2, 16, QChar('0'))
                .arg(eth_addr[3], 2, 16, QChar('0'))
                .arg(eth_addr[4], 2, 16, QChar('0'))
                .arg(eth_addr[5], 2, 16, QChar('0'));

        *values << (QStringList() << entry << QString(get_hash_wka_resolved_name(wkahash)));
    }
}

}

EthernetAddressModel::EthernetAddressModel(QObject * parent):
    AStringListListModel(parent)
{
    populate();
}

QStringList EthernetAddressModel::headerColumns() const
{
    return QStringList() << tr("Type") << tr("Address") << tr("Name");
}

QStringList EthernetAddressModel::filterValues() const
{
    return QStringList()
        << tr("All entries")
        << tr("Hosts")
        << tr("Ethernet Addresses") << tr("Ethernet Manufacturers")
        << tr("Ethernet Well-Known Addresses");
}

void EthernetAddressModel::populate()
{
    QList<QStringList> hosts;   // List of (address, names)
    if (wmem_map_t *ipv4_hash_table = get_ipv4_hash_table()) {
        wmem_map_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &hosts);
    }
    if (wmem_map_t *ipv6_hash_table = get_ipv6_hash_table()) {
        wmem_map_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &hosts);
    }
    const QString &hosts_label = tr("Hosts");
    foreach (const QStringList &addr_name, hosts)
        appendRow(QStringList() << hosts_label << addr_name);

    QList<QStringList> values;
    if (wmem_map_t *eth_hashtable = get_eth_hashtable()) {
        wmem_map_foreach(eth_hashtable, eth_hash_to_qstringlist, &values);
    }
    const QString &eth_label = tr("Ethernet Addresses");
    foreach (const QStringList &line, values)
        appendRow(QStringList() << eth_label << line);
    values.clear();
    if (wmem_map_t *eth_hashtable = get_manuf_hashtable()) {
        wmem_map_foreach(eth_hashtable, manuf_hash_to_qstringlist, &values);
    }
    const QString &manuf_label = tr("Ethernet Manufacturers");
    foreach (const QStringList &line, values)
        appendRow(QStringList() << manuf_label << line);
    values.clear();
    if (wmem_map_t *eth_hashtable = get_wka_hashtable()) {
        wmem_map_foreach(eth_hashtable, wka_hash_to_qstringlist, &values);
    }
    const QString &wka_label = tr("Ethernet Well-Known Addresses");
    foreach (const QStringList &line, values)
        appendRow(QStringList() << wka_label << line);
}

PortsModel::PortsModel(QObject * parent):
    AStringListListModel(parent)
{
    populate();
}

QStringList PortsModel::filterValues() const
{
    return QStringList()
        << tr("All entries") << tr("tcp") << tr("udp") << tr("sctp") << tr("dccp");
}

QStringList PortsModel::headerColumns() const
{
    return QStringList() << tr("Name") << tr("Port") << tr("Type");
}

void PortsModel::populate()
{
    wmem_map_t *serv_port_hashtable = get_serv_port_hashtable();
    if (serv_port_hashtable) {
        wmem_map_foreach(serv_port_hashtable, serv_port_hash_to_qstringlist, this);
    }
}