~ubuntu-branches/ubuntu/precise/uget/precise-backports

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 *
 *   Copyright (C) 2005-2010 by Raymond Huang
 *   plushuang at users.sourceforge.net
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU Lesser General Public License in all respects
 *  for all of the code used other than OpenSSL.  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so.  If you
 *  do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <ug_ipc.h>
#include <ug_utils.h>

static gboolean server_accept  (GIOChannel *source, GIOCondition condition, UgIpc* ipc);
static gboolean server_connect (GIOChannel *source, GIOCondition condition, UgIpc* ipc);

#ifdef _WIN32
// Windows version --------------------------------------------------
//#include <ws2tcpip.h>	// ADDRINFO

gboolean	ug_ipc_init_server (UgIpc* ipc)
{
	SOCKET		fd;
	struct sockaddr_in	saddr;
/*
	ADDRINFO	addrInfo;
	ADDRINFO*	ai_result = NULL;

	// AI_PASSIVE     // Socket address will be used in bind() call
	// AI_CANONNAME   // Return canonical name in first ai_canonname
	// AI_NUMERICHOST // Nodename must be a numeric address string
	// AI_NUMERICSERV // Servicename must be a numeric port number
	memset(&addrInfo, 0, sizeof(addrInfo));
	addrInfo.ai_family		= AF_INET;
	addrInfo.ai_socktype	= SOCK_STREAM;
	addrInfo.ai_flags		= AI_NUMERICSERV | AI_PASSIVE;

	if (getaddrinfo ("localhost", UGET_SOCKET_PORT_S, &addrInfo, &ai_result))
		return FALSE;

	fd = socket (AF_INET, SOCK_STREAM, 0);
	if (fd == INVALID_SOCKET)
		return FALSE;

	if (bind(fd, ai_result->ai_addr, ai_result->ai_addrlen) == SOCKET_ERROR) {
		closesocket (fd);
		return FALSE;
	}
*/

	saddr.sin_family = AF_INET;
	saddr.sin_port = htons (UGET_SOCKET_PORT);
	saddr.sin_addr.S_un.S_addr = inet_addr ("127.0.0.1");

	fd = socket (AF_INET, SOCK_STREAM, 0);
	if (fd == INVALID_SOCKET)
		return FALSE;

	if (bind (fd, (struct sockaddr *)&saddr, sizeof (saddr)) == SOCKET_ERROR) {
		closesocket (fd);
		return FALSE;
	}
	if (listen (fd, 5) == SOCKET_ERROR) {
		closesocket (fd);
		return FALSE;
	}

	ipc->argument_func = NULL;
	ipc->filename = NULL;
	ipc->channel  = g_io_channel_win32_new_socket (fd);
	g_io_channel_set_flags (ipc->channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_encoding (ipc->channel, NULL, NULL);	// binary
	g_io_add_watch (ipc->channel, G_IO_IN, (GIOFunc)server_accept, ipc);
	return TRUE;
}

gboolean	ug_ipc_init_client (UgIpc* ipc)
{
	SOCKET	fd;
	struct sockaddr_in	saddr;
//	int  error;

	fd = socket (AF_INET, SOCK_STREAM, 0);
//	error = WSAGetLastError();
	if (fd == INVALID_SOCKET)
		return FALSE;

	saddr.sin_family = AF_INET;
	saddr.sin_port = htons (UGET_SOCKET_PORT);
	saddr.sin_addr.S_un.S_addr = inet_addr ("127.0.0.1");

	if (connect (fd, (struct sockaddr *) &saddr, sizeof(saddr)) == SOCKET_ERROR) {
		closesocket (fd);
		return FALSE;
	}

	ipc->argument_func = NULL;
	ipc->filename = NULL;
	ipc->channel  = g_io_channel_win32_new_socket (fd);
//	g_io_channel_set_flags (ipc->channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_encoding (ipc->channel, NULL, NULL);	// binary
	return TRUE;
}

#else	// UNIX
#include <sys/socket.h>    // socket api
#include <unistd.h>        // uid_t and others
#include <errno.h>

#define INVALID_SOCKET	(-1)
#define closesocket		close


gboolean	ug_ipc_init_server (UgIpc* ipc)
{
	int		fd;
	struct sockaddr_un	saddr;

	fd = socket (AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1)
		return FALSE;

	saddr.sun_family = AF_UNIX;
	sprintf (saddr.sun_path, "%s/%s-%s",
	         g_get_tmp_dir (), UGET_SOCKET_NAME, g_get_user_name ());
	// --------------------------------
	// if server exist, return FALSE.
	if (connect (fd, (struct sockaddr *) &saddr, sizeof(saddr)) == 0) {
		close (fd);
		return FALSE;
	}
	// --------------------------------
	// delete socket file before server start
	ug_unlink (saddr.sun_path);

	if (bind (fd, (struct sockaddr *)&saddr, sizeof (saddr)) == -1) {
		close (fd);
		return FALSE;
	}
	if (listen (fd, 5) == -1) {
		close (fd);
		return FALSE;
	}

	ipc->argument_func = NULL;
	ipc->filename = g_strdup (saddr.sun_path);
	ipc->channel  = g_io_channel_unix_new (fd);
//	g_io_channel_set_flags (ipc->channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_encoding (ipc->channel, NULL, NULL);	// binary
	g_io_add_watch (ipc->channel, G_IO_IN, (GIOFunc)server_accept, ipc);

	return TRUE;
}

gboolean	ug_ipc_init_client (UgIpc* ipc)
{
	int		fd;
	uid_t	stored_uid, euid;
	struct sockaddr_un	saddr;

	fd = socket (AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1)
		return FALSE;

	saddr.sun_family = AF_UNIX;
	stored_uid = getuid();
	euid = geteuid();
	setuid(euid);
	sprintf (saddr.sun_path, "%s/%s-%s",
	         g_get_tmp_dir (), UGET_SOCKET_NAME, g_get_user_name ());
	setreuid(stored_uid, euid);

	if (connect (fd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
		close (fd);
		return FALSE;
	}

	ipc->argument_func = NULL;
	ipc->filename = NULL;
	ipc->channel  = g_io_channel_unix_new (fd);
//	g_io_channel_set_flags (ipc->channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_encoding (ipc->channel, NULL, NULL);	// binary
	return TRUE;
}
//	End of UNIX code
#endif  // End of _WIN32

void	ug_ipc_finalize (UgIpc* ipc)
{
	SOCKET		ipc_fd;

	if (ipc->channel) {
		ipc_fd = g_io_channel_unix_get_fd (ipc->channel);
		g_io_channel_shutdown (ipc->channel, FALSE, NULL);
		g_io_channel_unref (ipc->channel);
		closesocket (ipc_fd);
		ipc->channel = NULL;
	}
	if (ipc->filename) {
		ug_unlink (ipc->filename);
		g_free (ipc->filename);
		ipc->filename = NULL;
	}
}

static gboolean server_accept (GIOChannel *source, GIOCondition condition, UgIpc* ipc)
{
	SOCKET		source_fd;
	SOCKET		new_fd;
	GIOChannel*	new_channel;

	source_fd = g_io_channel_unix_get_fd (source);
	new_fd = accept (source_fd, NULL, NULL);
	if (new_fd == INVALID_SOCKET)
		return TRUE;

#ifdef _WIN32
	new_channel = g_io_channel_win32_new_socket (new_fd);
#else
	new_channel = g_io_channel_unix_new (new_fd);
#endif
	g_io_channel_set_encoding (new_channel, NULL, NULL);	// binary
	g_io_add_watch (new_channel, G_IO_IN | G_IO_HUP, (GIOFunc)server_connect, ipc);

	return TRUE;
}

static gboolean server_connect (GIOChannel *source, GIOCondition condition, UgIpc* ipc)
{
	SOCKET		source_fd;
	GIOStatus	status;
	GString*	gstr;
	GPtrArray*	array;
	GPtrArray*	array_backup;
	gint		array_len;
	gsize		bytes_read;
	gchar		header[3];

	if (condition == G_IO_HUP)
		goto shutdown;

	g_io_channel_read_chars (source, header, 3, &bytes_read, NULL);
	if (bytes_read < 3)
		goto shutdown;
	if (header[0] != 'U' || header[1] != 'G') {
#ifndef NDEBUG
		g_print ("Uget IPC server : Not UG\nData is %c%c\n", header[0], header[1]);
#endif
		goto shutdown;
	}

#ifndef NDEBUG
	g_print ("Uget IPC server : get message from client.\n");
#endif

	array = NULL;
	switch (header[2]) {
	case UG_IPC_SEND:
		gstr = g_string_sized_new (256);
		g_io_channel_read_line_string (source, gstr, &bytes_read, NULL);
		if (bytes_read == 0) {
			g_string_free (gstr, TRUE);
			goto error;
		}
		array_len = atoi (gstr->str);
		array = g_ptr_array_sized_new (array_len);
		while (array_len) {
			status = g_io_channel_read_line_string (source, gstr, &bytes_read, NULL);
			if (status == G_IO_STATUS_ERROR)
				break;
			gstr->str [bytes_read] = 0;
			g_ptr_array_add (array, g_strndup (gstr->str, gstr->len));
			array_len--;
		}
		// break;	// don't break, I need response UG_IPC_OK

	case UG_IPC_PING:
		header[2] = UG_IPC_OK;
		g_io_channel_write_chars (source, header, 3, NULL, NULL);
		g_io_channel_flush (source, NULL);
		break;

	default:
		goto error;
	}

//	post processing...
	if (array) {
		array_len = array->len;
		array_backup = g_ptr_array_sized_new (array_len);
		array_backup->len = array_len;
		while (array_len--)
			g_ptr_array_index (array_backup, array_len) = g_ptr_array_index (array, array_len);
		if (ipc->argument_func)
			ipc->argument_func (ipc->argument_data, array_backup->len, (char**) array_backup->pdata);
		g_ptr_array_foreach (array, (GFunc) g_free, NULL);
		g_ptr_array_free (array, TRUE);
		g_ptr_array_free (array_backup, TRUE);
	}
	return TRUE;

error:
	header[0] = 'U';
	header[1] = 'G';
	header[2] = UG_IPC_ERROR;
	g_io_channel_write_chars (source, header, 3, NULL, NULL);
	g_io_channel_flush (source, NULL);
	return TRUE;

shutdown:
#ifndef NDEBUG
	g_print ("Uget IPC server : close connection\n");
#endif
	source_fd = g_io_channel_unix_get_fd (source);
	g_io_channel_shutdown (source, TRUE, NULL);
	g_io_channel_unref (source);
	closesocket (source_fd);
	return FALSE;
}

gboolean	ug_ipc_ping (UgIpc* ipc)
{
	GIOStatus	status;
	gsize		n_bytes;
	gchar		header[3];

	header[0] = 'U';
	header[1] = 'G';
	header[2] = UG_IPC_PING;

	status = g_io_channel_write_chars (ipc->channel, header, 3, NULL, NULL);
	if (status == G_IO_STATUS_ERROR)
		return FALSE;
	g_io_channel_flush (ipc->channel, NULL);

	// get response
	g_usleep (100 * 1000);
	status = g_io_channel_read_chars (ipc->channel, header, 3, &n_bytes, NULL);
	if (n_bytes == 3 && header[0] == 'U' && header[1] == 'G' && header[2] == UG_IPC_OK)
		return TRUE;
	return FALSE;
}

gboolean	ug_ipc_send (UgIpc* ipc, int argc, char** argv)
{
	GIOStatus	status;
	GString*	gstr;
	gsize		n_bytes;
	gchar		header[3];
	int			index;

	// header, command
	gstr = g_string_sized_new (256);
	g_string_append (gstr, UGET_SOCKET_HEADER_S);
	g_string_append_c (gstr, UG_IPC_SEND);

	// argc, argv
	g_string_append_printf (gstr, "%d\n", argc);
	for (index=0;  index < argc;  index++) {
		g_string_append (gstr, argv[index]);
		g_string_append_c (gstr, '\n');
	}

	status = g_io_channel_write_chars (ipc->channel, gstr->str, gstr->len, NULL, NULL);
	g_string_free (gstr, TRUE);
	if (status == G_IO_STATUS_ERROR)
		return FALSE;
	g_io_channel_flush (ipc->channel, NULL);

	// get response
	g_usleep (100 * 1000);
	status = g_io_channel_read_chars (ipc->channel, header, 3, &n_bytes, NULL);
	if (n_bytes == 3 && header[0] == 'U' && header[1] == 'G' && header[2] == UG_IPC_OK)
		return TRUE;

	return FALSE;
}