File: protocol.c

package info (click to toggle)
gnome-network 1.0.2-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,968 kB
  • ctags: 1,007
  • sloc: ansic: 8,129; sh: 6,837; objc: 961; makefile: 251
file content (466 lines) | stat: -rw-r--r-- 10,824 bytes parent folder | download | duplicates (2)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * GnomeTalk: Protocol initialization and transactions module
 * (C) 1997 the Free Software Foundation
 *
 * Authors:
 *          Miguel de Icaza (miguel@gnu.org)
 *          Federico Mena   (quartic@gimp.org)
 */

#include "config.h"

/*
 * libc5 doesn't define struct osockaddr. But at least it _is_ the same
 * as struct sockaddr
 */

#include <sys/types.h>
#include <sys/socket.h>

#ifndef HAVE_STRUCT_OSOCKADDR
#define osockaddr sockaddr
#endif


#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "gnome.h"
#include "global.h"
#include "protocol.h"
#include "talk.h"
#include "names.h"
#include "gnome-talk.h"

#define TALK_MAX_ERROR 9
static char *talk_results [TALK_MAX_ERROR];

static CTL_MSG control_msg;
static struct sockaddr_in control_addr;
static struct sockaddr_in daemon_addr;
static int control_socket;
static int daemon_port;

static int talk_machine_tag = -1;
static int retry_tag        = -1;
static int socket_tag       = -1;

int connected = 0;
int remote_id, local_id;

static enum {
	CHECK_INVITATION,
	ANNOUNCE_SENT,
	WAITING_FOR_CANCEL
} talk_state;

static GtkWidget *info_window, *label;

void
talk_report_shutdown (void)
{
	gtk_widget_destroy (info_window);
	info_window = NULL;
	label = NULL;
}

void
talk_cancel_ctl_callback ()
{
	if (talk_machine_tag == -1)
		return;
	gdk_input_remove (talk_machine_tag);
	talk_machine_tag = -1;
}

void
talk_cancel_retry ()
{
	if (retry_tag == -1)
		return;
	gtk_timeout_remove (retry_tag);
	retry_tag = -1;
}

static void
talk_cancel (void)
{
	talk_report_shutdown ();
	talk_cancel_ctl_callback ();
	get_names_from_user ();
}

void
talk_report (char *msg)
{
	if (!info_window){
		info_window = gnome_dialog_new (_("Talk progress"), GNOME_STOCK_BUTTON_CANCEL, NULL);
		label = gtk_label_new (msg);
		gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (info_window)->vbox), label, TRUE, TRUE, 0);
		gtk_widget_show_all (info_window);
		gtk_window_set_modal (GTK_WINDOW (info_window), TRUE);
		gnome_dialog_button_connect (GNOME_DIALOG (info_window), 0, GTK_SIGNAL_FUNC(talk_cancel), NULL);
		return;
	} else
		gtk_label_set_text (GTK_LABEL (label), msg);
}

void
quit_event_loop (void)
{
	gtk_main_quit ();
}

void
talk_abort (char *msg)
{
	if (connected)
	{
	}
	else {
		GtkWidget *w;
		
		talk_cancel_ctl_callback ();
		w = gnome_message_box_new (msg, GNOME_MESSAGE_BOX_ERROR, GNOME_STOCK_BUTTON_OK, NULL);
		gtk_window_set_title (GTK_WINDOW (w), _("Error"));
		gtk_widget_show (w);
		gtk_signal_connect (GTK_OBJECT (w), "destroy", quit_event_loop, NULL);
		gtk_main ();
	}
	/* popup some error */
}

static char *
init_control_socket(void)
{
	size_t size;
	
	control_addr.sin_family = AF_INET;
	control_addr.sin_port   = 0;
	control_addr.sin_addr   = our_machine_addr;

	control_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if (control_socket < 0) 
		return _("could not create socket");

	if (bind(control_socket, (struct sockaddr *) &control_addr, sizeof(control_addr)) != 0)
		return _("init_control_socket: could not bind()");

	size = sizeof(control_addr);

	if (getsockname(control_socket, (struct sockaddr *) &control_addr, &size) != 0)
		return _("getsockname() failed");
	return NULL;
}

int retry (gpointer data);

static char *
init_talk_socket(void)
{
	size_t size;
	
	talk_addr.sin_family = AF_INET;
	talk_addr.sin_port   = 0;
	talk_addr.sin_addr   = our_machine_addr;

	talk_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (talk_socket < 0)
		return _("could not create socket");

	if (bind(talk_socket, (struct sockaddr *) &talk_addr, sizeof(talk_addr)) != 0)
		return _("could not bind ()");

	size = sizeof(talk_addr);

	if (getsockname(talk_socket, (struct sockaddr *) &talk_addr, &size) != 0)
		return _("getsockname() failed");
	return NULL;
}

static char *
transaction_send(struct in_addr dest, int type)
{
	int count;

	control_msg.type = type;

	daemon_addr.sin_addr = dest;
	daemon_addr.sin_port = daemon_port;

	if (retry_tag == -1)
		retry_tag = gtk_timeout_add (2 * 1000, retry, 0);

	for (;;){
		count = sendto(control_socket, (char *) &control_msg, sizeof(control_msg),
			       0, (struct sockaddr *) &daemon_addr, sizeof(daemon_addr));
		if (count != sizeof(control_msg)) {
			if (errno == EINTR)
				continue;
			return _("Error sending");
		}
		return NULL;
	}
}

static char *
transaction_get_reply (CTL_RESPONSE *response, int type)
{
	int count;

	talk_cancel_retry ();
	
	do {
		count = recv(control_socket, (char *) response, sizeof(*response), 0);
		if (count < 0) {
			if (errno == EINTR)
				continue;
			
			return _("Error when reading from talk daemon");
		}
	} while ((count == -1) ||
		 (response->vers != TALK_VERSION) ||
		 (response->type != type));
	
	response->id_num = ntohl(response->id_num);
	response->addr.sa_family = ntohs(response->addr.sa_family);
	return NULL;
}

int
retry (gpointer data)
{
	transaction_send (daemon_addr.sin_addr, control_msg.type);
	return 1;
}

static void
start_invite(void)
{
	if (listen (talk_socket, 5) != 0){
		talk_abort (_("Error trying to listen for caller"));
		return;
	}
	control_msg.id_num = htonl (-1);
	control_msg.addr = *(struct osockaddr *)&talk_addr;
	control_msg.addr.sa_family = htons (AF_INET);
	talk_report (_("Trying to connect to your party's talk daemon"));
	transaction_send (his_machine_addr, ANNOUNCE);
	talk_state = ANNOUNCE_SENT;
}

/*
 * This routine should actually have another state machine for not
 * blocking while receiving/sending the edit chars, but this is left
 * as an excercise to the reader of this code
 */
static void
set_edit_chars(void)
{
	char edit_chars [3];
	int  status;
	
	edit_chars [0] = TALK_ERASE;
	edit_chars [1] = TALK_KILL;
	edit_chars [2] = TALK_WIN_ERASE;
	status = write (talk_socket, edit_chars, 3);
	if (status != 3){
		talk_abort (_("Lost connection"));
		return;
	}
	status = read (talk_socket, &edit_chars, 3);
	if (status != 3){
		talk_abort (_("Lost connection"));
		return;
	}
	his_erase     = edit_chars [0];
	his_kill      = edit_chars [1];
	his_win_erase = edit_chars [2];
}

/* Called when we are ready for doing an accept on our talk socket */
void
socket_ready_for_accept (gpointer data, gint source, GdkInputCondition cond)
{
	int new_talk_socket;

	do {
		new_talk_socket = accept (talk_socket, 0, 0);
		if (new_talk_socket == -1 && errno != EINTR){
			talk_abort (_("Unable to connect with your party"));
			return;
		}
	} while (new_talk_socket == -1);
	close (talk_socket);
	talk_socket = new_talk_socket;
	talk_report (_("Waiting for your party to respond"));

	/* tell the daemons to delete the invitations */
	control_msg.id_num = htonl (local_id);
	transaction_send (our_machine_addr, DELETE);
	
	control_msg.id_num = htonl (remote_id);
	transaction_send (his_machine_addr, DELETE);

	set_edit_chars ();
	connected = 1;
	
	/* launch it! */
	create_talk_interface ();
	gdk_input_remove (socket_tag);
}

/*
 * Called when there is data on the control udp socket
 */
void
talk_state_machine (gpointer data, gint source, GdkInputCondition cond)
{
	CTL_RESPONSE response;
	int result;

/*	printf ("state: %d\n", talk_state); */
	switch (talk_state){
	case CHECK_INVITATION:
		transaction_get_reply (&response, LOOK_UP);
		if (response.answer != SUCCESS){
			start_invite ();
			return;
		}
		control_msg.id_num = htonl (response.id_num);
		gdk_input_remove (talk_machine_tag);
		if (response.addr.sa_family != AF_INET){
			talk_abort (_("response uses an invalid network address"));
			return;
		}

		do {
			result = connect (talk_socket, (struct sockaddr *)&response.addr,
					  sizeof(response.addr));
			if (result == -1){
				if (errno == ECONNREFUSED){
					transaction_send (his_machine_addr, DELETE);
					close (talk_socket);
					init_talk_socket ();
					start_invite ();
				}
				if (errno == EINTR)
					continue;
			}
		} while (result == -1);

		/* Start talk */
		set_edit_chars ();
		connected = 1;
		create_talk_interface ();
		break;
		
	case ANNOUNCE_SENT:
		transaction_get_reply (&response, ANNOUNCE);
		remote_id = response.id_num;
		if (response.answer != SUCCESS){
			if (response.answer < TALK_MAX_ERROR)
				talk_abort (talk_results [response.answer-1]);
			return;
		}
		transaction_send (our_machine_addr, LEAVE_INVITE);
		/* we can do this, as this is the local machine */
		transaction_get_reply (&response, LEAVE_INVITE);
		local_id = response.id_num;
		talk_report (_("Waiting for your party to respond"));
		socket_tag = gdk_input_add (talk_socket, GDK_INPUT_READ, socket_ready_for_accept, 0);
		
		talk_state = WAITING_FOR_CANCEL;
		break;

	case WAITING_FOR_CANCEL:
		transaction_get_reply (&response, DELETE);
		printf (_("CANCEL CALLBACK:Answer:%d\n"), response.answer);
		gdk_input_remove (talk_machine_tag);
		talk_cancel_ctl_callback ();
		talk_cancel_retry ();
		break;
	}
}

char *
create_connection(char *me, char *he, char *tty)
{
	struct servent *server;
	char *err;
	
	/* Initialize talk_msg template */

	control_msg.pid = htonl(getpid());
	control_msg.vers = TALK_VERSION;
	control_msg.id_num = htonl(0);

	strncpy(control_msg.l_name, me, NAME_SIZE);
	control_msg.l_name[NAME_SIZE - 1] = 0;

	strncpy(control_msg.r_name, he, NAME_SIZE);
	control_msg.r_name[NAME_SIZE - 1] = 0;

	strncpy(control_msg.r_tty, tty, TTY_SIZE);
	control_msg.r_tty[TTY_SIZE - 1] = 0;

	if ((err = init_control_socket()) != NULL)
		return err;
	
	if ((err = init_talk_socket()) != NULL)
		return err;

	control_msg.ctl_addr = *(struct osockaddr *)&control_addr;
        control_msg.ctl_addr.sa_family = htons (AF_INET);
	control_msg.addr.sa_family = htons (AF_INET);
	
	/* Init daemon port */

	server = getservbyname("ntalk", "udp");

	/* we have a failback: it once happened to me, and I was pissed off */
	if (server)
		daemon_port = server->s_port;
	else
		daemon_port = 518;

	err = transaction_send (his_machine_addr, LOOK_UP);
	if (err)
		return err;

	talk_machine_tag = gdk_input_add (control_socket, GDK_INPUT_READ, talk_state_machine, 0);
	talk_state = CHECK_INVITATION;
	return NULL;
}

void
init_protocol (void)
{
	/* NOT_HERE */
	talk_results [0] = _("Your party is not logged on");

	/* FAILED */
        talk_results [1] = _("Target machine is too confused to talk to us");
	
	/* MACHINE_UNKNOWN */
        talk_results [2] = _("Target machine does not recognize us");
	
	/* PERMISSION_REFUSED */
        talk_results [3] = _("Your party is refusing messages");
	
	/* UNKNOWN_REQUEST */
        talk_results [4] = _("Target machine can not handle remote talk");
	
	/* BADVERSION */
        talk_results [5] = _("Target machine indicates protocol mismatch");
	
	/* BADADDR */
        talk_results [6] = _("Target machine indicates protocol botch (addr)");
	
	/* BADCTLADDR */
	talk_results [7] = _("Target machine indicates protocol botch (ctl_addr)");
}