summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/tsi.c
blob: 5f9ae850363b5f40e7a1d1b5261bdb866b307646 (plain) (blame)
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
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * transport session ID helper functions.
 *
 * Copyright (c) 2006-2009 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <impl/framework.h>


//#define TSI_DEBUG


/* locals */


/* re-entrant form of pgm_tsi_print()
 *
 * returns number of bytes written to buffer on success, returns -1 on
 * invalid parameters.
 */

int
pgm_tsi_print_r (
	const pgm_tsi_t* restrict tsi,
	char*		 restrict buf,
	size_t			  bufsize
	)
{
	pgm_return_val_if_fail (NULL != tsi, -1);
	pgm_return_val_if_fail (NULL != buf, -1);
	pgm_return_val_if_fail (bufsize > 0, -1);

	const uint8_t* gsi = (const uint8_t*)tsi;
	const uint16_t source_port = tsi->sport;

	return snprintf (buf, bufsize, "%i.%i.%i.%i.%i.%i.%i",
			 gsi[0], gsi[1], gsi[2], gsi[3], gsi[4], gsi[5], ntohs (source_port));
}

/* transform TSI to ASCII string form.
 *
 * on success, returns pointer to ASCII string.  on error, returns NULL.
 */

char*
pgm_tsi_print (
	const pgm_tsi_t*	tsi
	)
{
	pgm_return_val_if_fail (tsi != NULL, NULL);

	static char buf[PGM_TSISTRLEN];
	pgm_tsi_print_r (tsi, buf, sizeof(buf));
	return buf;
}

/* create hash value of TSI for use with GLib hash tables.
 *
 * on success, returns a hash value corresponding to the TSI.  on error, fails
 * on assert.
 */

pgm_hash_t
pgm_tsi_hash (
	const void*	 p
        )
{
	const union {
		pgm_tsi_t	tsi;
		uint32_t	l[2];
	} *u = p;

/* pre-conditions */
	pgm_assert (NULL != p);

	return u->l[0] ^ u->l[1];
}

/* compare two transport session identifier TSI values.
 *
 * returns TRUE if they are equal, FALSE if they are not.
 */

bool
pgm_tsi_equal (
	const void* restrict p1,
	const void* restrict p2
        )
{
	const union {
		pgm_tsi_t	tsi;
		uint32_t	l[2];
		uint64_t	ll;
	} *restrict u1 = p1, *restrict u2 = p2;

/* pre-conditions */
	pgm_assert (NULL != p1);
	pgm_assert (NULL != p2);

	return (u1->l[0] == u2->l[0] && u1->l[1] == u2->l[1]);
}

/* eof */