File: network.c

package info (click to toggle)
spread 3.17.4-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,800 kB
  • ctags: 2,322
  • sloc: ansic: 15,666; sh: 2,611; java: 2,291; perl: 556; yacc: 523; makefile: 255; lex: 204; xml: 77
file content (743 lines) | stat: -rw-r--r-- 23,154 bytes parent folder | download
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*
 * The Spread Toolkit.
 *     
 * The contents of this file are subject to the Spread Open-Source
 * License, Version 1.0 (the ``License''); you may not use
 * this file except in compliance with the License.  You may obtain a
 * copy of the License at:
 *
 * http://www.spread.org/license/
 *
 * or in the file ``license.txt'' found in this distribution.
 *
 * Software distributed under the License is distributed on an AS IS basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 * for the specific language governing rights and limitations under the 
 * License.
 *
 * The Creators of Spread are:
 *  Yair Amir, Michal Miskin-Amir, Jonathan Stanton.
 *
 *  Copyright (C) 1993-2004 Spread Concepts LLC <spread@spreadconcepts.com>
 *
 *  All Rights Reserved.
 *
 * Major Contributor(s):
 * ---------------
 *    Cristina Nita-Rotaru crisn@cs.purdue.edu - group communication security.
 *    Theo Schlossnagle    jesus@omniti.com - Perl, skiplists, autoconf.
 *    Dan Schoenblum       dansch@cnds.jhu.edu - Java interface.
 *    John Schultz         jschultz@cnds.jhu.edu - contribution to process group membership.
 *
 */


#include <assert.h>
#include "arch.h"
#include "spread_params.h"
#include "network.h"
#include "net_types.h"
#include "data_link.h"
#include "sp_events.h"
#include "status.h"
#include "alarm.h"
#include "configuration.h"

/* for Memb_print_form_token() */
#include "membership.h"

static	channel		Bcast_channel[MAX_INTERFACES_PROC];
static  channel		Token_channel[MAX_INTERFACES_PROC];
static	channel		Send_channel;

static  int             Num_bcast_channels;
static  int             Num_token_channels;

static	int		Bcast_needed;
static	int32		Bcast_address;
static	int16		Bcast_port;

static	int		Num_send_needed;
static  int32		Send_address[MAX_SEGMENTS];
static	int16		Send_ports[MAX_SEGMENTS];

/* ### Pack: 3 lines */
/* Global in function so both Net_queue_bcast and Net_flush_bcast can access them */
static  sys_scatter     Queue_scat;
static  int             Queued_bytes = 0;
static  const char      align_padding[4] = "padd";

/* address for token sending - which is always needed */
static	int32		Token_address;
static	int16		Token_port;

static	configuration	Net_membership;
static	int		Segment_leader;

static 	configuration	Cn;
static	proc		My;
static	segment 	My_seg;

static	int16		Partition[MAX_PROCS_RING];
static	sp_time		Partition_timeout 	= { 60, 0};
static	int		My_index;

static	void		Clear_partition(int dummy, void *dummy_p);
static	int		In_my_component( int32	proc_id );
static	void		Flip_pack( packet_header *pack_ptr );
static	void		Flip_token( token_header *token_ptr );


void	Net_init()
{
	proc		dummy_proc;
        int32u          interface_addr;
        int             i;
        bool            bcast_bound = FALSE;

	Cn = Conf();
	My = Conf_my();

	My_index = Conf_proc_by_id( My.id, &dummy_proc );
	Clear_partition(0, NULL);

	if( Cn.segments[My.seg_index].num_procs > 1 )
	{
		/* I am not allone in segment */
		Bcast_needed  = 1;
		Bcast_address = Cn.segments[My.seg_index].bcast_address;
		Bcast_port    = My.port;

		Alarm( NETWORK, "Net_init: Bcast needed to address (%d, %d)\n",
			Bcast_address, Bcast_port );
	}else{
		Bcast_needed  = 0;
		Bcast_address = 0;
		Alarm( NETWORK, "Net_init: Bcast is not needed\n" );
	}

        /* To receive broadcast (and possibly multicast) packets on a socket
         * bound to a specific interface, we also have to bind to the broadcast
         * address on the interface as well as the unicast interface. That is 
         * what the double bind of the bcast_address does.
         */
        for ( i=0; i < My.num_if; i++)
        {
                if (Is_IfType_Daemon( My.ifc[i].type ) || Is_IfType_Any( My.ifc[i].type ) )
                {
                        if (Is_IfType_Any( My.ifc[i].type ) )
                                interface_addr = 0;
                        else {
                                interface_addr = My.ifc[i].ip;
                                if (Bcast_needed && !bcast_bound) {
#ifndef ARCH_PC_WIN95
                                    Bcast_channel[Num_bcast_channels++] = DL_init_channel( RECV_CHANNEL, My.port, Bcast_address, Bcast_address );
#endif
                                    bcast_bound = TRUE;
                                }
                        }
                        Bcast_channel[Num_bcast_channels++] = DL_init_channel( RECV_CHANNEL, My.port, Bcast_address, interface_addr );
                        Token_channel[Num_token_channels++] = DL_init_channel( RECV_CHANNEL, My.port+1, 0, interface_addr );
                }
        }

	Send_channel  = DL_init_channel( SEND_CHANNEL, My.port+2, 0, My.id );

	Num_send_needed = 0;
}

void	Net_set_membership( configuration memb )
{
	int	i;
	int	my_index_in_seg;
	int	my_next_index;

	Net_membership = memb;
	My_seg = Net_membership.segments[My.seg_index];
	my_index_in_seg = Conf_id_in_seg( &My_seg, My.id );
	if( my_index_in_seg < 0 )
		Alarm( EXIT,"Net_set_membership: I am not in membership %c",
			Conf_print( &Net_membership ) );
	else if( my_index_in_seg == 0) {
		Segment_leader = 1;
		Alarm( NETWORK,"Net_set_membership: I am a Segment leader\n");
	} else  Segment_leader = 0;

	Num_send_needed = 0;
	my_next_index = -1;
	for( i=0; i < Conf().num_segments; i++ )
	{
	    if( i == My.seg_index )
	    {
		/* 
		 * This is my segment.
		 * There is no need to send to it
		 * but I need my_next_index to calculate
		 * the token send address.
		 */
		my_next_index = Num_send_needed;

	    } else if( Net_membership.segments[i].num_procs > 0 ) {

		Send_address[Num_send_needed] = Net_membership.segments[i].procs[0]->id;
		Send_ports  [Num_send_needed] = Net_membership.segments[i].port;

		Num_send_needed++;
	    }
	}
	assert(my_next_index != -1);
	for( i=0; i < Num_send_needed; i++ )
		Alarm( NETWORK, 
			"Net_set_membership: Send_addr[%d] is (%d,%d)\n",
			i, Send_address[i], Send_ports[i] );

	/* Calculate where to send the token */
	Token_address = 0;
	if( my_index_in_seg < My_seg.num_procs-1 )
	{
		Token_address = My_seg.procs[my_index_in_seg+1]->id;
		Token_port    = My_seg.port+1;
	}else{
		/* I am last in my segment */
		if( Num_send_needed == 0 )
		{
			/* 
			 * My segment is the only segment
			 * sending token to the first in my segment 
			 */
			Token_address = My_seg.procs[0]->id;
			Token_port    = My_seg.port+1;
		} else if( Num_send_needed == my_next_index ) {
			/* 
			 * My segment is the last segment
			 * sending token to the first in first valid segment
			 */	
			Token_address = Send_address[0];
			Token_port    = Send_ports[0]+1;
		} else {
			/*
			 * There is a valid segment after mine
			 * sending token to the first in next valid segment
			 */
			Token_address = Send_address[my_next_index];
			Token_port    = Send_ports[my_next_index]+1;
		}

	}
	Alarm( NETWORK, "Net_set_membership: Token_address : (%d, %d)\n",
			Token_address, Token_port );
}

int	Net_bcast( sys_scatter *scat )
{
	packet_header	*pack_ptr;
	int 		i;
	int		ret;

	ret = 0;
	/* routing on channels if needed according to membership */
	pack_ptr = (packet_header *)scat->elements[0].buf;
	pack_ptr->type  = Set_routed( pack_ptr->type );
	pack_ptr->type  = Set_endian( pack_ptr->type );
	pack_ptr->transmiter_id = My.id;
	for ( i=0; i< Num_send_needed; i++ )
	{
	    ret = DL_send( Send_channel, Send_address[i], Send_ports[i], scat );
	}
	pack_ptr->type = Clear_routed( pack_ptr->type );

	/* broadcasting if needed according to configuration */
	if( Bcast_needed )
	{	
	    ret = DL_send( Send_channel, Bcast_address, Bcast_port, scat );
	}

        if( !Bcast_needed && (Num_send_needed == 0) )
            ret = 1; /* No actual send is needed, but 'packet' can be considered 'sent' */

	return( ret );
}

/* ### Pack: 2 routines */
int	Net_queue_bcast( sys_scatter *scat )
{
	packet_header	*pack_ptr;
        int             new_bytes;
	int 		i, j;
        int             ret;
        int             align_bytes, align_num_scatter;

        /* This line is redundent because of static initialization to 0 */
        if ( Queued_bytes == 0 ) Queue_scat.num_elements = 0;

        ret = 0;
        new_bytes = 0;

        for ( i=0; i < scat->num_elements; i++) {
                new_bytes += scat->elements[i].len;
        }
        /* Fix alignment of packed messages  so they will each begin on a 4 byte alignment 
         * This is needed for Sparc, might need enhancement if other archs 
         * have more extensive alignement rules
         */
        align_bytes = 0;
        align_num_scatter = 0;
        switch(Queued_bytes % 4) {
        case 1:
                align_bytes++;
        case 2:
                align_bytes++;
        case 3:
                align_bytes++;
                align_num_scatter = 1;
        case 0:
                /* nothing since already aligned */
                break; 
        }

        if ( ( (Queued_bytes + new_bytes + align_bytes)  >  MAX_PACKET_SIZE ) ||
             ( (Queue_scat.num_elements + scat->num_elements + align_num_scatter) > ARCH_SCATTER_SIZE ) )
        {
                ret = Net_flush_bcast();
                align_bytes = 0;
                align_num_scatter = 0;
        }

        if ( Queued_bytes == 0 ) {
                /* routing on channels if needed according to membership */
                pack_ptr = (packet_header *)scat->elements[0].buf;
                pack_ptr->type  = Set_routed( pack_ptr->type );
                pack_ptr->type  = Set_endian( pack_ptr->type );
                pack_ptr->transmiter_id = My.id;
        }

        if ( align_bytes > 0 )
        {
                Queue_scat.elements[Queue_scat.num_elements].len = align_bytes;
                Queue_scat.elements[Queue_scat.num_elements].buf = (char *)align_padding;

                Queued_bytes += align_bytes;
                Queue_scat.num_elements += 1;
                Alarm(NETWORK, "Net_queue_bcast: Inserted padding of %d bytes to message of size %d\n", align_bytes, new_bytes );
        }

        /* Add new packet to Queue_scat to be sent as packed packet */
        for ( i=0, j=Queue_scat.num_elements; i < scat->num_elements; i++, j++) {
                Queue_scat.elements[j].len = scat->elements[i].len;
                Queue_scat.elements[j].buf = scat->elements[i].buf;
        }
        Queued_bytes += new_bytes;
        Queue_scat.num_elements += scat->num_elements ;
        
	return( ret );
}

int     Net_flush_bcast(void)
{
        packet_header   *pack_ptr;
        int             i;
        int             ret;

        if (Queued_bytes == 0 ) return( 0 );
        
        Alarm(NETWORK, "Net_flush_bcast: Flushing with Queued_bytes = %d; num_elements in scat = %d; size of scat0,1 = %d %d\n", Queued_bytes, Queue_scat.num_elements, Queue_scat.elements[0].len, Queue_scat.elements[1].len);
        
        ret = 0;

        for ( i=0; i< Num_send_needed; i++ )
        {
                ret = DL_send( Send_channel, Send_address[i], Send_ports[i], &Queue_scat );
        }
        pack_ptr = (packet_header *)Queue_scat.elements[0].buf; 
	pack_ptr->type = Clear_routed( pack_ptr->type );

        /* broadcasting if needed according to configuration */
        if( Bcast_needed )
        {	
                ret = DL_send( Send_channel, Bcast_address, Bcast_port, &Queue_scat );
        }

        if( !Bcast_needed && (Num_send_needed == 0) )
            ret = 1; /* No actual send is needed, but 'packet' can be considered 'sent' */

        Queue_scat.num_elements = 0;
        Queued_bytes = 0;
        return( ret );
}

int	Net_scast( int16 seg_index, sys_scatter *scat )
{
	packet_header	*pack_ptr;
	int		ret;
        bool            send_not_needed_p = FALSE;

	ret = 0;
	pack_ptr = (packet_header *)scat->elements[0].buf;
	pack_ptr->type = Set_endian( pack_ptr->type );
	pack_ptr->transmiter_id = My.id;
	if( seg_index == My.seg_index )
	{
	    if( Bcast_needed )
	    {
	    	ret = DL_send( Send_channel, Bcast_address, Bcast_port, scat );
	    } else 
                send_not_needed_p = TRUE;
	}else{
	    if( Net_membership.segments[seg_index].num_procs > 0 )
	    {
		pack_ptr->type = Set_routed( pack_ptr->type );
	    	ret = DL_send( Send_channel, 
			Net_membership.segments[seg_index].procs[0]->id,
			Net_membership.segments[seg_index].port,
			scat );
		pack_ptr->type = Clear_routed( pack_ptr->type );
	    } else
                send_not_needed_p = TRUE;
	}

        if (send_not_needed_p)
            ret = 1; /* notify that packet can be considered sent, even though no network send actually needed */

	return( ret );
}

int	Net_ucast( int32 proc_id, sys_scatter *scat )
{
	packet_header	*pack_ptr;
	proc		p;
	int		ret;

	pack_ptr = (packet_header *)scat->elements[0].buf;
	pack_ptr->type = Set_endian( pack_ptr->type );
	pack_ptr->transmiter_id = My.id;
	ret = Conf_proc_by_id( proc_id, &p );
	if( ret < 0 )
	{
		Alarm( PRINT, "Net_ucast: non existing proc_id %d\n",proc_id );
		return( ret );
	}
	ret = DL_send( Send_channel, proc_id, p.port, scat );
	return( ret );
}

int	Net_recv ( channel fd, sys_scatter *scat )
{
static	scatter		save;
	packet_header	*pack_ptr;
	int		bytes_left;
	int		received_bytes, body_offset;
	int		processed_bytes;
	int		pack_same_endian;
	int		i;
        bool            ch_found;

	pack_ptr = (packet_header *)scat->elements[0].buf;

        ch_found = FALSE;
        for (i = 0 ; i < Num_bcast_channels; i++) {
            if ( fd == Bcast_channel[i]) {
                ch_found = TRUE; 
                break;
            }
        }
        if (ch_found == FALSE) {
            Alarm(EXIT, "Net_recv: Listening and received packet on un-used interface %d\n", fd);
        }

	received_bytes = DL_recv( fd, scat ); 

	if( received_bytes <= 0 ) return( received_bytes );

	if( received_bytes < sizeof( packet_header ) ) 
	{
		Alarm(PRINT, "Net_recv: ignoring packet of size %d, smaller than packet header size %d\n", 
			received_bytes, sizeof(packet_header) );
		return( -1 );
	}

	/* Fliping packet header to my form if needed */
	if( !Same_endian( pack_ptr->type ) ) Flip_pack( pack_ptr );

	if( Is_partition( pack_ptr->type ) )
	{
		/* Monitor : updating partition */
		int16	*cur_partition;

                if ( ! Conf_get_dangerous_monitor_state() ) {
                        Alarm( PRINT, "Net_recv: Request to set partition or kill daemons from (%d.%d.%d.%d) denied. Monitor in safe mode\n", IP1(pack_ptr->proc_id), IP2(pack_ptr->proc_id), IP3(pack_ptr->proc_id), IP4(pack_ptr->proc_id) );
                        return( 0 );
                }

		if( ! ( pack_ptr->memb_id.proc_id == 15051963 && Conf_id_in_conf( &Cn, pack_ptr->proc_id ) != -1  ) ) return( 0 );

		cur_partition = (int16 *)scat->elements[1].buf;

		for( i=0; i < Conf_num_procs( &Cn ); i++ )
		    if( Same_endian( pack_ptr->type ) ) Partition[i] = cur_partition[i];
		    else Partition[i] = Flip_int16( cur_partition[i] );

		E_queue( Clear_partition, 0, NULL, Partition_timeout );
		if( Partition[My_index] < 0 )
		    Alarm( EXIT, "Net_recv: Instructed to exit by monitor\n");
		Alarm( PRINT  , "Net_recv: Got monitor message, component %d\n", 
			Partition[My_index] );

		return( 0 );
	} 

	/* Monitor : droping packet from other monitor components */
	if( ! ( pack_ptr->memb_id.proc_id == 15051963 || In_my_component( pack_ptr->transmiter_id ) ) )
		return( 0 );

	/* no need to return my own packets */
	if( pack_ptr->transmiter_id == My.id )
		return( 0 );

	if( Bcast_needed && Is_routed( pack_ptr->type ) )
	{
		if( !Segment_leader ) Alarm( NETWORK, 
		"Net_recv: recv routed message from %d but not seg leader\n",
			pack_ptr->proc_id);

		/* saving scat lens for another DL_recv */
		save.num_elements = scat->num_elements;
		for( i=0; i < save.num_elements; i++ )
			save.elements[i].len = scat->elements[i].len;

		/* computing true scat lens for sending */
		bytes_left = received_bytes;
		i = 0;
		while ( bytes_left > 0 )
		{
			if( bytes_left < scat->elements[i].len )
				scat->elements[i].len = bytes_left;
			bytes_left -=  scat->elements[i].len;			
			i ++;
		}
		scat->num_elements = i;

		pack_ptr->type = Clear_routed ( pack_ptr->type );
		pack_ptr->transmiter_id = My.id;

		/* fliping to original form */
		if( !Same_endian( pack_ptr->type ) ) Flip_pack( pack_ptr );
		DL_send( Send_channel, Bcast_address, Bcast_port, scat );
		/* re-fliping to my form */
		if( !Same_endian( pack_ptr->type ) ) Flip_pack( pack_ptr );

		/* restoring scat lens for another DL_recv */
		scat->num_elements = save.num_elements;
		for( i=0; i < save.num_elements; i++ )
			scat->elements[i].len = save.elements[i].len;

	}
	/*
	 * we clear routed anyway in order not to ask if Bcast_needed again.
	 * This way, if bcast is not needed we give it to the upper layer
	 * right away. It will always get to the upper layer with this
	 * bit cleared.
	 */
	pack_ptr->type = Clear_routed ( pack_ptr->type );

	/* 
	 * Check validity of packet size and flip every packet header 
	 * other than first header (which is already flipped).
	 * If packet size is not valid, return -1, otherwise 
	 * return size of received packet.
	 */
	processed_bytes = sizeof( packet_header ) + pack_ptr->data_len;
	pack_same_endian = Same_endian( pack_ptr->type );
        /* ignore any alignment padding */
        if ( processed_bytes < received_bytes ) {
                switch(processed_bytes % 4)
                {
                case 1:
                        processed_bytes++;
                case 2:
                        processed_bytes++;
                case 3:
                        processed_bytes++;
                case 0:
                        /* already aligned */
                        break;
                }
        }
	while( processed_bytes < received_bytes )
	{
                body_offset = processed_bytes - sizeof(packet_header); 
                pack_ptr = (packet_header *)&scat->elements[1].buf[body_offset];

                /* flip contigues packet header */
		if( !pack_same_endian  ) {
                        Flip_pack( pack_ptr );
		}

		processed_bytes += sizeof( packet_header ) + pack_ptr->data_len;
                /* ignore any alignment padding */
                if ( processed_bytes < received_bytes ) {
                        switch(processed_bytes % 4)
                        {
                        case 1:
                                processed_bytes++;
                        case 2:
                                processed_bytes++;
                        case 3:
                                processed_bytes++;
                        case 0:
                                /* already aligned */
                                break;
                        }
                }
	}                
        Alarm( NETWORK, "Net_recv: Received Packet - packet length(%d), packed message length(%d)\n", received_bytes, processed_bytes);
	if( processed_bytes != received_bytes ) {
                Alarm( PRINT, "Net_recv: Received Packet - packet length(%d) != packed message length(%d)\n", received_bytes, processed_bytes);
                return( -1 );
        }
	return( received_bytes );
}

int	Net_send_token( sys_scatter *scat )
{
	token_header	*token_ptr;
	int		ret;

	token_ptr = (token_header *)scat->elements[0].buf;
	token_ptr->type = Set_endian( token_ptr->type );
	token_ptr->transmiter_id = My.id;

        if ( token_ptr->rtr_len > (MAX_PACKET_SIZE - sizeof(token_header) ) )
        {
            if ( Is_form( token_ptr->type ) )
                Memb_print_form_token( scat );
            Alarmp( SPLOG_FATAL, PRINT, "Net_send_token: Token too long for packet!\n");
        }

	ret = DL_send( Send_channel, Token_address, Token_port, scat );
	return ( ret );
}

int	Net_recv_token( channel fd, sys_scatter *scat )
{
	token_header	*token_ptr;
	int		ret, i;
        bool            ch_found;

	token_ptr = (token_header *)scat->elements[0].buf;

        ch_found = FALSE;
        for (i = 0 ; i < Num_token_channels; i++) {
            if ( fd == Token_channel[i]) {
                ch_found = TRUE; 
                break;
            }
        }
        if (ch_found == FALSE) {
            Alarm(EXIT, "Net_recv: Listening and received packet on un-used interface %d\n", fd);
        }

	ret = DL_recv( fd, scat );

	if( ret <= 0 ) return( ret );

	/* Fliping token header to my form if needed */
	if( !Same_endian( token_ptr->type ) ) Flip_token( token_ptr );

	/* Monitor : droping token from other monitor components */
	if( !In_my_component( token_ptr->transmiter_id ) )
		return( 0 );

	return ( ret );
}

int	Net_ucast_token( int32 proc_id, sys_scatter *scat )
{
	token_header	*token_ptr;
	proc		p;
	int		ret;

	token_ptr = (token_header *)scat->elements[0].buf;
	token_ptr->type = Set_endian( token_ptr->type );
	token_ptr->transmiter_id = My.id;
	ret = Conf_proc_by_id( proc_id, &p );
	if( ret < 0 )
	{
		Alarm( PRINT, "Net_ucast_token: non existing proc_id %d\n",
			proc_id );
		return( ret );
	}
        if ( token_ptr->rtr_len > (MAX_PACKET_SIZE - sizeof(token_header) ) )
        {
            Memb_print_form_token( scat );
            Alarmp( SPLOG_FATAL, PRINT, "Net_ucast_token: Token too long for packet!\n");
        }

	ret = DL_send( Send_channel, proc_id, p.port+1, scat );
	return( ret );
}

void     Net_num_channels(int *num_bcast, int *num_token)
{
    *num_bcast = Num_bcast_channels;
    *num_token = Num_token_channels;
}

channel *Net_bcast_channel()
{
	return( &(Bcast_channel[0]) );
}

channel *Net_token_channel()
{
	return( &(Token_channel[0]) );
}

static	void	Clear_partition(int dummy, void *dummy_p)
{
	int	i;

	for( i=0; i < Conf_num_procs( &Cn ); i++ )
		Partition[i] = 0;
}

static	int	In_my_component( int32	proc_id )
{
	int	proc_index;
	proc	dummy_proc;
	char	ip[16];

	proc_index = Conf_proc_by_id( proc_id, &dummy_proc );
	if( proc_index < 0 )
	{
		Conf_id_to_str( proc_id, ip );
		Alarm( PRINT, "In_my_component: unknown proc %s\n", ip );
		return( 0 );
	}

	return( Partition[My_index] == Partition[proc_index] );
}

void	Flip_pack( packet_header *pack_ptr )
{
	pack_ptr->type		  = Flip_int32( pack_ptr->type );
	pack_ptr->transmiter_id	  = Flip_int32( pack_ptr->transmiter_id );
	pack_ptr->proc_id	  = Flip_int32( pack_ptr->proc_id );
	pack_ptr->memb_id.proc_id = Flip_int32( pack_ptr->memb_id.proc_id );
	pack_ptr->memb_id.time	  = Flip_int32( pack_ptr->memb_id.time );
	pack_ptr->seq		  = Flip_int32( pack_ptr->seq );
	pack_ptr->fifo_seq	  = Flip_int32( pack_ptr->fifo_seq );
	pack_ptr->packet_index	  = Flip_int16( pack_ptr->packet_index );
	pack_ptr->data_len	  = Flip_int16( pack_ptr->data_len );
}

void	Flip_token( token_header *token_ptr )
{
	token_ptr->type		 = Flip_int32( token_ptr->type );
	token_ptr->transmiter_id = Flip_int32( token_ptr->transmiter_id );
	token_ptr->seq		 = Flip_int32( token_ptr->seq );
	token_ptr->proc_id	 = Flip_int32( token_ptr->proc_id );
	token_ptr->aru		 = Flip_int32( token_ptr->aru );
	token_ptr->aru_last_id	 = Flip_int32( token_ptr->aru_last_id );
	token_ptr->flow_control	 = Flip_int16( token_ptr->flow_control );
	token_ptr->rtr_len	 = Flip_int16( token_ptr->rtr_len );
}