summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/engine.c
blob: 994bca277e301ae5ac3c49c2d3f0fdbdb66a3e3d (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * PGM engine.
 *
 * Copyright (c) 2006-2010 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
 */

#ifndef _WIN32
#	include <netdb.h>
#endif
#include <impl/i18n.h>
#include <impl/framework.h>
#include <impl/engine.h>
#include <impl/mem.h>
#include <impl/socket.h>
#include <pgm/engine.h>
#include <pgm/version.h>


//#define ENGINE_DEBUG


/* globals */
int			pgm_ipproto_pgm PGM_GNUC_READ_MOSTLY = IPPROTO_PGM;

#ifdef _WIN32
LPFN_WSARECVMSG		pgm_WSARecvMsg PGM_GNUC_READ_MOSTLY = NULL;
#endif

#ifdef PGM_DEBUG
unsigned		pgm_loss_rate PGM_GNUC_READ_MOSTLY = 0;
#endif

/* locals */
static bool		pgm_is_supported = FALSE;
static volatile uint32_t pgm_ref_count	 = 0;

#ifdef _WIN32
#	ifndef WSAID_WSARECVMSG
/* http://cvs.winehq.org/cvsweb/wine/include/mswsock.h */
#		define WSAID_WSARECVMSG {0xf689d7c8,0x6f1f,0x436b,{0x8a,0x53,0xe5,0x4f,0xe3,0x51,0xc3,0x22}}
#	endif
#endif


/* startup PGM engine, mainly finding PGM protocol definition, if any from NSS
 *
 * returns TRUE on success, returns FALSE if an error occurred, implying some form of
 * system re-configuration is required to resolve before trying again.
 *
 * NB: Valgrind loves generating errors in getprotobyname().
 */
bool
pgm_init (
	pgm_error_t**	error
	)
{
	if (pgm_atomic_exchange_and_add32 (&pgm_ref_count, 1) > 0)
		return TRUE;

/* initialise dependent modules */
	pgm_messages_init();

	pgm_minor ("OpenPGM %d.%d.%d%s%s%s %s %s %s %s",
			pgm_major_version, pgm_minor_version, pgm_micro_version,
			pgm_build_revision ? " (" : "", pgm_build_revision ? pgm_build_revision : "", pgm_build_revision ? ")" : "",
			pgm_build_date, pgm_build_time, pgm_build_system, pgm_build_machine);

	pgm_thread_init();
	pgm_mem_init();
	pgm_rand_init();

#ifdef _WIN32
	WORD wVersionRequested = MAKEWORD (2, 2);
	WSADATA wsaData;
	if (WSAStartup (wVersionRequested, &wsaData) != 0)
	{
		const int save_errno = WSAGetLastError ();
		pgm_set_error (error,
			     PGM_ERROR_DOMAIN_ENGINE,
			     pgm_error_from_wsa_errno (save_errno),
			     _("WSAStartup failure: %s"),
			     pgm_wsastrerror (save_errno));
		goto err_shutdown;
	}

	if (LOBYTE (wsaData.wVersion) != 2 || HIBYTE (wsaData.wVersion) != 2)
	{
		WSACleanup();
		pgm_set_error (error,
			       PGM_ERROR_DOMAIN_ENGINE,
			       PGM_ERROR_FAILED,
			       _("WSAStartup failed to provide requested version 2.2."));
		goto err_shutdown;
	}

#	ifndef CONFIG_TARGET_WINE
/* find WSARecvMsg API */
	if (NULL == pgm_WSARecvMsg) {
		GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
		DWORD cbBytesReturned;
		const SOCKET sock = socket (AF_INET, SOCK_DGRAM, 0);
		if (SOCKET_ERROR == sock) {
			WSACleanup();
			pgm_set_error (error,
				       PGM_ERROR_DOMAIN_ENGINE,
				       PGM_ERROR_FAILED,
				       _("Cannot open socket."));
			goto err_shutdown;
		}
		if (SOCKET_ERROR == WSAIoctl (sock,
					      SIO_GET_EXTENSION_FUNCTION_POINTER,
					      &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
					      &pgm_WSARecvMsg, sizeof(pgm_WSARecvMsg),
					      &cbBytesReturned,
					      NULL,
					      NULL))
		{
			closesocket (sock);
			WSACleanup();
			pgm_set_error (error,
				       PGM_ERROR_DOMAIN_ENGINE,
				       PGM_ERROR_FAILED,
				       _("WSARecvMsg function not found."));
			goto err_shutdown;
		}
		pgm_debug ("Retrieved address of WSARecvMsg.");
		closesocket (sock);
	}
#	endif
#endif /* _WIN32 */

/* find PGM protocol id overriding default value, use first value from NIS */
#ifdef CONFIG_HAVE_GETPROTOBYNAME_R
	char b[1024];
	struct protoent protobuf;
	const struct protoent* proto = getprotobyname_r ("pgm", &protobuf, b, sizeof(b));
	if (NULL != proto) {
		if (proto->p_proto != pgm_ipproto_pgm) {
			pgm_minor (_("Setting PGM protocol number to %i from /etc/protocols."),
				proto->p_proto);
			pgm_ipproto_pgm = proto->p_proto;
		}
	}
#elif defined(CONFIG_HAVE_GETPROTOBYNAME_R2)
	char b[1024];
	struct protoent protobuf, *proto;
	const int e = getprotobyname_r ("pgm", &protobuf, b, sizeof(b), &proto);
	if (e != -1 && proto != NULL) {
		if (proto->p_proto != pgm_ipproto_pgm) {
			pgm_minor (_("Setting PGM protocol number to %i from /etc/protocols."),
				proto->p_proto);
			pgm_ipproto_pgm = proto->p_proto;
		}
	}
#else
	const struct protoent *proto = getprotobyname ("pgm");
	if (proto != NULL) {
		if (proto->p_proto != pgm_ipproto_pgm) {
#ifndef _WIN32
			pgm_minor (_("Setting PGM protocol number to %i from /etc/protocols."),
				proto->p_proto);
#else
			pgm_minor (_("Setting PGM protocol number to %i from %%SYSTEMROOT%%\\system32\\drivers\\etc\\protocols."),
				proto->p_proto);
#endif
			pgm_ipproto_pgm = proto->p_proto;
		}
	}
#endif

/* ensure timing enabled */
	pgm_error_t* sub_error = NULL;
	if (!pgm_time_init (&sub_error)) {
		if (sub_error)
			pgm_propagate_error (error, sub_error);
#ifdef _WIN32
		WSACleanup();
#endif
		goto err_shutdown;
	}

/* receiver simulated loss rate */
#ifdef PGM_DEBUG
	const char *loss_rate = getenv ("PGM_LOSS_RATE");
	if (NULL != loss_rate) {
		int value = atoi (loss_rate);
		if (value > 0 && value <= 100) {
			pgm_loss_rate = value;
			pgm_minor (_("Setting PGM packet loss rate to %i%%."), pgm_loss_rate);
		}
	}
#endif

/* create global sock list lock */
	pgm_rwlock_init (&pgm_sock_list_lock);

	pgm_is_supported = TRUE;
	return TRUE;

err_shutdown:
	pgm_rand_shutdown();
	pgm_mem_shutdown();
	pgm_thread_shutdown();
	pgm_messages_shutdown();
	pgm_atomic_dec32 (&pgm_ref_count);
	return FALSE;
}

/* returns TRUE if PGM engine has been initialized
 */

bool
pgm_supported (void)
{
	return ( pgm_is_supported == TRUE );
}

/* returns TRUE on success, returns FALSE if an error occurred.
 */

bool
pgm_shutdown (void)
{
	pgm_return_val_if_fail (pgm_atomic_read32 (&pgm_ref_count) > 0, FALSE);

	if (pgm_atomic_exchange_and_add32 (&pgm_ref_count, (uint32_t)-1) != 1)
		return TRUE;

	pgm_is_supported = FALSE;

/* destroy all open socks */
	while (pgm_sock_list) {
		pgm_close ((pgm_sock_t*)pgm_sock_list->data, FALSE);
	}

	pgm_time_shutdown();

#ifdef _WIN32
	WSACleanup();
#endif

	pgm_rand_shutdown();
	pgm_mem_shutdown();
	pgm_thread_shutdown();
	pgm_messages_shutdown();
	return TRUE;
}

/* helper to drop out of setuid 0 after creating PGM sockets
 */
void
pgm_drop_superuser (void)
{
#ifndef _WIN32
	if (0 == getuid()) {
		setuid((gid_t)65534);
		setgid((uid_t)65534);
	}
#endif
}

/* eof */