aboutsummaryrefslogtreecommitdiffstats
path: root/ui/rtp_stream_id.c
blob: 2c00a5e43f3b48a0c97404f5c80b73916c49cd84 (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
/* rtp_stream_id.c
 * RTP stream id functions for Wireshark
 *
 * Copyright 2003, Alcatel Business Systems
 * By Lars Ruoff <lars.ruoff@gmx.net>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#include "file.h"

#include "ui/rtp_stream_id.h"
#include "epan/dissectors/packet-rtp.h"

/****************************************************************************/
/* rtpstream id functions */
/****************************************************************************/

/****************************************************************************/
/* deep copy of id */
void rtpstream_id_copy(const rtpstream_id_t *src, rtpstream_id_t *dest)
{
    copy_address(&(dest->src_addr), &(src->src_addr));
    dest->src_port=src->src_port;
    copy_address(&(dest->dst_addr), &(src->dst_addr));
    dest->dst_port=src->dst_port;
    dest->ssrc=src->ssrc;
}

/****************************************************************************/
/* deep copy of id from packet_info */
void rtpstream_id_copy_pinfo(const packet_info *pinfo, rtpstream_id_t *dest, gboolean swap_src_dst)
{
	if (!swap_src_dst)
	{
		copy_address(&(dest->src_addr), &(pinfo->src));
		dest->src_port=pinfo->srcport;
		copy_address(&(dest->dst_addr), &(pinfo->dst));
		dest->dst_port=pinfo->destport;
	}
	else
	{
		copy_address(&(dest->src_addr), &(pinfo->dst));
		dest->src_port=pinfo->destport;
		copy_address(&(dest->dst_addr), &(pinfo->src));
		dest->dst_port=pinfo->srcport;
	}
}

/****************************************************************************/
/* shallow copy from packet_info to id */
void rtpstream_id_copy_pinfo_shallow(const packet_info *pinfo, rtpstream_id_t *dest, gboolean swap_src_dst)
{
	if (!swap_src_dst)
	{
		copy_address_shallow(&(dest->src_addr), &(pinfo->src));
		dest->src_port=pinfo->srcport;
		copy_address_shallow(&(dest->dst_addr), &(pinfo->dst));
		dest->dst_port=pinfo->destport;
	}
	else
	{
		copy_address_shallow(&(dest->src_addr), &(pinfo->dst));
		dest->src_port=pinfo->destport;
		copy_address_shallow(&(dest->dst_addr), &(pinfo->src));
		dest->dst_port=pinfo->srcport;
	}
}
/****************************************************************************/
/* free memory allocated for id */
void rtpstream_id_free(rtpstream_id_t *id)
{
	free_address(&(id->src_addr));
	free_address(&(id->dst_addr));
	memset(id, 0, sizeof(*id));
}

/****************************************************************************/
/* convert rtpstream_id_t to hash */
guint rtpstream_id_to_hash(const rtpstream_id_t *id)
{
	guint hash = 0;

	if (!id) { return 0; }
	/* XOR of: */
	/* SRC PORT | DST_PORT */
	/* SSRC */
	/* SRC ADDR */
	/* DST ADDR */
	hash ^= id->src_port | id->dst_port << 16;
	hash ^= id->ssrc;
	hash = add_address_to_hash(hash, &id->src_addr);
	hash = add_address_to_hash(hash, &id->dst_addr);

	return hash;
}

/****************************************************************************/
/* compare two ids by flags */
gboolean rtpstream_id_equal(const rtpstream_id_t *id1, const rtpstream_id_t *id2, guint flags)
{
	if (addresses_equal(&(id1->src_addr), &(id2->src_addr))
		&& id1->src_port == id2->src_port
		&& addresses_equal(&(id1->dst_addr), &(id2->dst_addr))
		&& id1->dst_port == id2->dst_port)
	{
		gboolean equal = TRUE;

		if ((flags & RTPSTREAM_ID_EQUAL_SSRC)
			&& id1->ssrc != id2->ssrc)
		{
			equal = FALSE;
		}

		return equal;
	}

	return FALSE;
}

/****************************************************************************/
/* compare an rtpstream id address and ports with pinfo */
gboolean rtpstream_id_equal_pinfo(const rtpstream_id_t *id, const packet_info *pinfo, bool swap_src_dst)
{
        if (!swap_src_dst) {
                if (addresses_equal(&(id->src_addr), &(pinfo->src))
                        && id->src_port == pinfo->srcport
                        && addresses_equal(&(id->dst_addr), &(pinfo->dst))
                        && id->dst_port == pinfo->destport)
                {
                        return TRUE;
                }
        } else {
                if (addresses_equal(&(id->src_addr), &(pinfo->dst))
                        && id->src_port == pinfo->destport
                        && addresses_equal(&(id->dst_addr), &(pinfo->src))
                        && id->dst_port == pinfo->srcport)
                {
                        return TRUE;
                }
        }

	return FALSE;
}
/****************************************************************************/
/* compare two ids, one in pinfo */
gboolean rtpstream_id_equal_pinfo_rtp_info(const rtpstream_id_t *id, const packet_info *pinfo, const struct _rtp_info *rtp_info)
{
	if (addresses_equal(&(id->src_addr), &(pinfo->src))
		&& id->src_port == pinfo->srcport
		&& addresses_equal(&(id->dst_addr), &(pinfo->dst))
		&& id->dst_port == pinfo->destport
                && id->ssrc == rtp_info->info_sync_src)
	{
		return TRUE;
	}

	return FALSE;
}

/****************************************************************************/
/* convert packet_info and _rtp_info to hash */
guint pinfo_rtp_info_to_hash(const packet_info *pinfo, const struct _rtp_info *rtp_info)
{
	guint hash = 0;

	if (!pinfo || !rtp_info) { return 0; }
	/* XOR of: */
	/* SRC PORT | DST_PORT */
	/* SSRC */
	/* SRC ADDR */
	/* DST ADDR */
	hash ^= pinfo->srcport | pinfo->destport << 16;
	hash ^= rtp_info->info_sync_src;
	hash = add_address_to_hash(hash, &pinfo->src);
	hash = add_address_to_hash(hash, &pinfo->dst);

	return hash;
}