aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-paltalk.c
blob: d2cb0d0d5c482436cbdd264a64a092dbb8c717b3 (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
/* packet-paltalk.c
 * Routines for Paltalk dissection
 * Copyright 2005, Tim Hentenaar < tim at hentenaar dot com >
 * Copyright 2008, Mohammad Ebrahim Mohammadi Panah < mebrahim at gmail dot com >
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"


#include <epan/packet.h>

#include "packet-tcp.h"

void proto_register_paltalk(void);
void proto_reg_handoff_paltalk(void);

#define INET_IPV4_ADDRESS_FROM_BYTES(a,b,c,d) g_htonl((((guint32)a)<<24) | ((b)<<16) | ((c)<<8) | (d)) /* *network* order */

#define PALTALK_SERVERS_ADDRESS INET_IPV4_ADDRESS_FROM_BYTES(199,106,0,0)      /* 199.106.0.0 in *network* order */
#define PALTALK_SERVERS_NETMASK INET_IPV4_ADDRESS_FROM_BYTES(0xFF, 0xFE, 0x00, 0x00)  /* /15  in *network* order */

#define PALTALK_HEADER_LENGTH 6

static int proto_paltalk = -1;

static int hf_paltalk_pdu_type = -1;
static int hf_paltalk_version = -1;
static int hf_paltalk_length = -1;
static int hf_paltalk_content = -1;

static gint ett_paltalk = -1;

static guint
dissect_paltalk_get_len(packet_info *pinfo _U_, tvbuff_t *tvb,
                        int offset, void *data _U_)
{
    return tvb_get_ntohs(tvb, offset + 4) + PALTALK_HEADER_LENGTH;
}

static int
dissect_paltalk_desegmented(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    proto_item *ti = NULL;
    proto_tree *pt_tree = NULL;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "Paltalk");
    col_clear(pinfo->cinfo, COL_INFO);

    if (tree)       /* we are being asked for details */
    {
        ti = proto_tree_add_item(tree, proto_paltalk, tvb, 0, -1, ENC_NA);
        pt_tree = proto_item_add_subtree(ti, ett_paltalk);
        proto_tree_add_item(pt_tree, hf_paltalk_pdu_type, tvb, 0, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt_tree, hf_paltalk_version, tvb, 2, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt_tree, hf_paltalk_length, tvb, 4, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt_tree, hf_paltalk_content, tvb, 6, tvb_get_ntohs(tvb, 4), ENC_NA);
    }

    return tvb_captured_length(tvb);
}

static gboolean
dissect_paltalk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    guint32 src32, dst32;

    /* Detect if this TCP session is a Paltalk one */
    /* TODO: Optimize detection logic if possible */

    if ((pinfo->net_src.type != AT_IPv4)
        || (pinfo->net_dst.type != AT_IPv4)
        || (pinfo->net_src.len != 4)
        || (pinfo->net_dst.len != 4)
        || !pinfo->net_src.data
        || !pinfo->net_dst.data)
        return FALSE;

    memcpy((guint8 *)&src32, pinfo->net_src.data, 4); /* *Network* order */
    memcpy((guint8 *)&dst32, pinfo->net_dst.data, 4); /* *Network* order */

    if ( ((src32 & PALTALK_SERVERS_NETMASK) != PALTALK_SERVERS_ADDRESS)
         &&
         ((dst32 & PALTALK_SERVERS_NETMASK) != PALTALK_SERVERS_ADDRESS))
        return FALSE;

    /* Dissect result of desegmented TCP data */
    tcp_dissect_pdus(tvb, pinfo, tree, TRUE, PALTALK_HEADER_LENGTH,
            dissect_paltalk_get_len, dissect_paltalk_desegmented, data);
    return TRUE;
}

void
proto_register_paltalk(void)
{
    static hf_register_info hf[] = {
        { &hf_paltalk_pdu_type, { "Packet Type", "paltalk.type",
                      FT_UINT16, BASE_HEX, NULL, 0x00, NULL, HFILL }},
        { &hf_paltalk_version,  { "Protocol Version", "paltalk.version",
                      FT_INT16, BASE_DEC, NULL, 0x00, NULL, HFILL }},
        { &hf_paltalk_length,   { "Payload Length", "paltalk.length",
                      FT_INT16, BASE_DEC, NULL, 0x00, NULL, HFILL }},
        { &hf_paltalk_content,  { "Payload Content", "paltalk.content",
                      FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL }}
    };

    static gint *ett[] = { &ett_paltalk };

    proto_paltalk = proto_register_protocol("Paltalk Messenger Protocol", "Paltalk", "paltalk");
    proto_register_field_array(proto_paltalk, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_paltalk(void)
{
    heur_dissector_add("tcp", dissect_paltalk, "Paltalk over TCP", "paltalk_tcp", proto_paltalk, HEURISTIC_ENABLE);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */