summaryrefslogtreecommitdiffstats
path: root/usr.sbin/atm/scspd/scsp_output.c
blob: 7f8f1cd9e27e693d70e06b9768b2d995952c41ee (plain)
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
 *
 * ===================================
 * HARP  |  Host ATM Research Platform
 * ===================================
 *
 *
 * This Host ATM Research Platform ("HARP") file (the "Software") is
 * made available by Network Computing Services, Inc. ("NetworkCS")
 * "AS IS".  NetworkCS does not provide maintenance, improvements or
 * support of any kind.
 *
 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
 * In no event shall NetworkCS be responsible for any damages, including
 * but not limited to consequential damages, arising from or relating to
 * any use of the Software or related support.
 *
 * Copyright 1994-1998 Network Computing Services, Inc.
 *
 * Copies of this Software may be made, however, the above copyright
 * notice must be reproduced on all copies.
 *
 *	@(#) $Id: scsp_output.c,v 1.1 1998/09/15 08:23:17 phk Exp $
 *
 */

/*
 * Server Cache Synchronization Protocol (SCSP) Support
 * ----------------------------------------------------
 *
 * Output packet processing
 *
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netatm/port.h> 
#include <netatm/queue.h> 
#include <netatm/atm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_ioctl.h>

#include <errno.h>
#include <libatm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include "scsp_msg.h"
#include "scsp_if.h"
#include "scsp_var.h"

#ifndef lint
__RCSID("@(#) $Id: scsp_output.c,v 1.1 1998/09/15 08:23:17 phk Exp $");
#endif


/*
 * Put a long integer into the output buffer
 *
 * This routine is provided for cases where long ints may not be
 * word-aligned in the output buffer.
 *
 * Arguments:
 *	l	long integer
 *	cp	pointer to output buffer
 *
 * Returns:
 *	None
 *
 */
static void
put_long(l, cp)
	u_long	l;
	u_char	*cp;
{
	u_long	nl;

	/*
	 * Convert to network order and copy to output buffer
	 */
	nl = htonl(l);
	UM_COPY(&nl, cp, sizeof(u_long));
}


/*
 * Format a Sender or Receiver ID
 *
 * Arguments:
 *	idp	ponter to ID structure
 *	buff	pointer to ID
 *
 * Returns:
 *	0	input was invalid
 *	else	length of ID processed
 *
 */
static int
scsp_format_id(idp, buff)
	Scsp_id	*idp;
	char	*buff;
{
	/*
	 * Copy the ID
	 */
	UM_COPY(idp->id, buff, idp->id_len);

	/*
	 * Return the ID length
	 */
	return(idp->id_len);
}


/*
 * Format the Mandatory Common Part of an SCSP input packet
 *
 * Arguments:
 *	mcp	pointer to MCP
 *	buff	pointer to mandatory common part
 *
 * Returns:
 *	0	input was invalid
 *	else	length of MCP in message
 *
 */
static int
scsp_format_mcp(mcp, buff)
	Scsp_mcp	*mcp;
	char		*buff;
{
	int			len;
	char			*odp;
	struct scsp_nmcp	*smp;

	/*
	 * Set the protocol ID
	 */
	smp = (struct scsp_nmcp *)buff;
	smp->sm_pid = htons(mcp->pid);

	/*
	 * Set the server group ID
	 */
	smp->sm_sgid = htons(mcp->sgid);

	/*
	 * Set the flags
	 */
	smp->sm_flags = htons(mcp->flags);

	/*
	 * Set the sender ID and length
	 */
	smp->sm_sid_len = mcp->sid.id_len;
	odp = buff + sizeof(struct scsp_nmcp);
	len = scsp_format_id(&mcp->sid, odp);
	if (len == 0) {
		goto mcp_invalid;
	 }

	/*
	 * Set the receiver ID and length
	 */
	smp->sm_rid_len = mcp->rid.id_len;
	odp += mcp->sid.id_len;
	len = scsp_format_id(&mcp->rid, odp);
	if (len == 0) {
		goto mcp_invalid;
	 }

	/*
	 * Set the record count
	 */
	smp->sm_rec_cnt = htons(mcp->rec_cnt);

	/*
	 * Return the length of data we processed
	 */
	return(sizeof(struct scsp_nmcp) + mcp->sid.id_len +
			mcp->rid.id_len);

mcp_invalid:
	return(0);
}


/*
 * Format an Extension
 *
 * Arguments:
 *	exp	pointer to extension in internal format
 *	buff	pointer to output buffer
 *	blen	space available in buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of extension processed
 *
 */
static int
scsp_format_ext(exp, buff, blen)
	Scsp_ext	*exp;
	char		*buff;
	int		blen;
{
	struct scsp_next	*sep;

	/*
	 * Make sure there's room in the buffer
	 */
	if (blen < (sizeof(struct scsp_next) + exp->len))
		return(0);

	/*
	 * Set the type
	 */
	sep = (struct scsp_next *)buff;
	sep->se_type = htons(exp->type);

	/*
	 * Set the length
	 */
	sep->se_len = htons(exp->len);

	/*
	 * Set the value
	 */
	if (exp->len > 0) {
		buff += sizeof(struct scsp_next);
		UM_COPY((caddr_t)exp + sizeof(Scsp_ext),
				buff,
				exp->len);
	}

	/*
	 * Return the number of bytes processed
	 */
	return(sizeof(struct scsp_next) + exp->len);
}


/*
 * Format the ATMARP part of a CSA record
 *
 * Arguments:
 *	acsp	pointer to ATMARP protocol-specific CSA record
 *	buff	pointer to output buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of record processed
 *
 */
static int
scsp_format_atmarp(acsp, buff)
	Scsp_atmarp_csa	*acsp;
	char		*buff;
{
	char			*cp;
	int			len, pkt_len;
	struct scsp_atmarp_ncsa	*sanp;

	/*
	 * Figure out how long PDU is going to be
	 */
	pkt_len = sizeof(struct scsp_atmarp_ncsa);
	switch (acsp->sa_sha.address_format) {
	case T_ATM_ENDSYS_ADDR:
		pkt_len += acsp->sa_sha.address_length;
		break;

	case T_ATM_E164_ADDR:
		pkt_len += acsp->sa_sha.address_length;
		if (acsp->sa_ssa.address_format == T_ATM_ENDSYS_ADDR)
			pkt_len += acsp->sa_ssa.address_length;
		break;
	}

	switch (acsp->sa_tha.address_format) {
	case T_ATM_ENDSYS_ADDR:
		pkt_len += acsp->sa_tha.address_length;
		break;

	case T_ATM_E164_ADDR:
		pkt_len += acsp->sa_tha.address_length;
		if (acsp->sa_tha.address_format == T_ATM_ENDSYS_ADDR)
			pkt_len += acsp->sa_tha.address_length;
		break;
	}

	if (acsp->sa_spa.s_addr != 0)
		pkt_len += sizeof(struct in_addr);

	if (acsp->sa_tpa.s_addr != 0)
		pkt_len += sizeof(struct in_addr);

	/*
	 * Set up pointers
	 */
	sanp = (struct scsp_atmarp_ncsa *)buff;
	cp = (char *)sanp + sizeof(struct scsp_atmarp_ncsa);

	/*
	 * Build fields
	 */
	sanp->sa_hrd = htons(ARP_ATMFORUM);
	sanp->sa_pro = htons(ETHERTYPE_IP);

	/* sa_sha */
	len = acsp->sa_sha.address_length;
	switch (acsp->sa_sha.address_format) {
	case T_ATM_ENDSYS_ADDR:
		sanp->sa_shtl = ARP_TL_NSAPA | (len & ARP_TL_LMASK);

		/* sa_sha */
		UM_COPY(acsp->sa_sha.address, cp, len);
		cp += len;

		sanp->sa_sstl = 0;
		break;

	case T_ATM_E164_ADDR:
		sanp->sa_shtl = ARP_TL_E164 | (len & ARP_TL_LMASK);

		/* sa_sha */
		UM_COPY(acsp->sa_sha.address, cp, len);
		cp += len;

		if (acsp->sa_ssa.address_format == T_ATM_ENDSYS_ADDR) {
			len = acsp->sa_ssa.address_length;
			sanp->sa_sstl = ARP_TL_NSAPA |
					(len & ARP_TL_LMASK);

			/* sa_ssa */
			UM_COPY(acsp->sa_ssa.address, cp, len);
			cp += len;
		} else
			sanp->sa_sstl = 0;
		break;

	default:
		sanp->sa_shtl = 0;
		sanp->sa_sstl = 0;
	}

	/* sa_state */
	sanp->sa_state = acsp->sa_state;
	sanp->sa_fill1 = 0;

	/* sa_spa */
	if (acsp->sa_spa.s_addr != 0) {
		sanp->sa_spln = sizeof(struct in_addr);
		UM_COPY(&acsp->sa_spa, cp, sizeof(struct in_addr));
		cp += sizeof(struct in_addr);
	}

	/* sa_tha */
	len = acsp->sa_tha.address_length;
	switch (acsp->sa_tha.address_format) {
	case T_ATM_ENDSYS_ADDR:
		sanp->sa_thtl = ARP_TL_NSAPA | (len & ARP_TL_LMASK);

		/* sa_tha */
		UM_COPY(acsp->sa_tha.address, cp, len);
		cp += len;

		sanp->sa_tstl = 0;
		break;

	case T_ATM_E164_ADDR:
		sanp->sa_thtl = ARP_TL_E164 | (len & ARP_TL_LMASK);

		/* sa_tha */
		UM_COPY(acsp->sa_tha.address, cp, len);
		cp += len;

		if (acsp->sa_tsa.address_format == T_ATM_ENDSYS_ADDR) {
			len = acsp->sa_tha.address_length;
			sanp->sa_tstl = ARP_TL_NSAPA |
					(len & ARP_TL_LMASK);

			/* sa_tsa */
			UM_COPY(acsp->sa_tsa.address, cp, len);
			cp += len;
		} else
			sanp->sa_tstl = 0;
		break;

	default:
		sanp->sa_thtl = 0;
		sanp->sa_tstl = 0;
	}

	/* sa_tpa */
	if (acsp->sa_tpa.s_addr != 0) {
		sanp->sa_tpln = sizeof(struct in_addr);
		UM_COPY(&acsp->sa_tpa, cp, sizeof(struct in_addr));
	}

	return(pkt_len);
}


/*
 * Format a Cache State Advertisement or Cache State Advertisement
 * Summary record
 *
 * Arguments:
 *	csapp	pointer to CSA or CSAS
 *	buff	pointer to output buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of record processed
 *
 */
static int
scsp_format_csa(csap, buff)
	Scsp_csa	*csap;
	char		*buff;
{
	int			len = 0;
	char			*odp;
	struct scsp_ncsa	*scp;

	/*
	 * Set the hop count
	 */
	scp = (struct scsp_ncsa *)buff;
	scp->scs_hop_cnt = htons(csap->hops);

	/*
	 * Set the null flag
	 */
	if (csap->null) {
		scp->scs_nfill = htons(SCSP_CSAS_NULL);
	}

	/*
	 * Set the sequence number
	 */
	put_long(csap->seq, (u_char *)&scp->scs_seq);

	/*
	 * Set the cache key
	 */
	scp->scs_ck_len = csap->key.key_len;
	odp = buff + sizeof(struct scsp_ncsa);
	UM_COPY(csap->key.key, odp, scp->scs_ck_len);

	/*
	 * Set the originator ID
	 */
	odp += scp->scs_ck_len;
	scp->scs_oid_len = scsp_format_id(&csap->oid, odp);

	/*
	 * Set the protocol-specific data, if present.  At the
	 * moment, we only handle data for ATMARP.
	 */
	if (csap->atmarp_data) {
		odp += scp->scs_oid_len;
		len = scsp_format_atmarp(csap->atmarp_data, odp);
	}

	/*
	 * Set the record length
	 */
	scp->scs_len = htons(sizeof(struct scsp_ncsa) +
			scp->scs_ck_len + scp->scs_oid_len +
			len);

	/*
	 * Return the length of data we processed
	 */
	return(ntohs(scp->scs_len));
}


/*
 * Format a Cache Alignment message
 *
 * Arguments:
 *	cap	pointer to CA message
 *	buff	pointer to output buffer
 *	blen	space available in buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of CA message processed
 *
 */
static int
scsp_format_ca(cap, buff, blen)
	Scsp_ca	*cap;
	char	*buff;
	int	blen;
{
	int		i, len, proc_len;
	struct scsp_nca	*scap;
	Scsp_csa	*csap;

	/*
	 * Set the sequence number
	 */
	scap = (struct scsp_nca *)buff;
	put_long(cap->ca_seq, (u_char *)&scap->sca_seq);
	proc_len = sizeof(scap->sca_seq);
	buff += sizeof(scap->sca_seq);

	/*
	 * Set the flags
	 */
	cap->ca_mcp.flags = 0;
	if (cap->ca_m)
		cap->ca_mcp.flags |= SCSP_CA_M;
	if (cap->ca_i)
		cap->ca_mcp.flags |= SCSP_CA_I;
	if (cap->ca_o)
		cap->ca_mcp.flags |= SCSP_CA_O;

	/*
	 * Format the mandatory common part of the message
	 */
	len = scsp_format_mcp(&cap->ca_mcp, buff);
	if (len == 0)
		goto ca_invalid;
	buff += len;
	proc_len += len;

	/*
	 * Put any CSAS records into the message
	 */
	for (i = 0, csap = cap->ca_csa_rec; i < cap->ca_mcp.rec_cnt;
			i++, csap = csap->next) {
		len = scsp_format_csa(csap, buff);
		buff += len;
		proc_len += len;
		if (proc_len > blen) {
			scsp_log(LOG_CRIT, "scsp_format_ca: buffer overflow");
			abort();
		}
	}

	/*
	 * Return the length of processed data
	 */
	return(proc_len);

ca_invalid:
	return(0);
}


/*
 * Format a Cache State Update Request, Cache State Update Reply, or
 * Cache State Update Solicit message.  These all have the same format,
 * a Mandatory Common Part followed by a number of CSA or CSAS records.
 *
 * Arguments:
 *	csup	pointer to location to put pointer to CSU Req message
 *	buff	pointer to output buffer
 *	blen	space available in buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of CSU Req message processed
 *
 */
static int
scsp_format_csu(csup, buff, blen)
	Scsp_csu_msg	*csup;
	char		*buff;
	int		blen;
{
	int			i, len, proc_len;
	struct scsp_ncsu_msg	*scsup;
	Scsp_csa		*csap;

	/*
	 * Format the mandatory common part of the message
	 */
	scsup = (struct scsp_ncsu_msg *)buff;
	len = scsp_format_mcp(&csup->csu_mcp, buff);
	if (len == 0)
		goto csu_invalid;
	buff += len;
	proc_len = len;

	/*
	 * Put the CSAS records into the message
	 */
	for (i = 0, csap = csup->csu_csa_rec;
			i < csup->csu_mcp.rec_cnt && csap;
			i++, csap = csap->next) {
		len = scsp_format_csa(csap, buff);
		buff += len;
		proc_len += len;
		if (proc_len > blen) {
			scsp_log(LOG_CRIT, "scsp_format_csu: buffer overflow");
			abort();
		}
	}

	/*
	 * Return the length of processed data
	 */
	return(proc_len);

csu_invalid:
	return(0);
}


/*
 * Format a Hello message
 *
 * Arguments:
 *	hpp	pointer to Hello message
 *	buff	pointer to output buffer
 *	blen	space available in buffer
 *
 * Returns:
 *	0	input was invalid
 *	else	length of Hello message processed
 *
 */
static int
scsp_format_hello(hp, buff, blen)
	Scsp_hello	*hp;
	char		*buff;
	int		blen;
{
	int			len, proc_len;
	struct scsp_nhello	*shp;
	Scsp_id			*ridp;

	/*
	 * Set the hello interval
	 */
	shp = (struct scsp_nhello *)buff;
	shp->sch_hi = htons(hp->hello_int);

	/*
	 * Set the dead factor
	 */
	shp->sch_df = htons(hp->dead_factor);

	/*
	 * Set the family ID
	 */
	shp->sch_fid = htons(hp->family_id);

	/*
	 * Process the mandatory common part of the message
	 */
	proc_len = sizeof(struct scsp_nhello) -
			sizeof(struct scsp_nmcp);
	buff += proc_len;
	len = scsp_format_mcp(&hp->hello_mcp, buff);
	if (len == 0)
		goto hello_invalid;
	proc_len += len;
	buff += len;

	/*
	 * Add any additional receiver ID records to the message
	 */
	for (ridp = hp->hello_mcp.rid.next; ridp;
			ridp = ridp->next) {
		len = scsp_format_id(ridp, buff);
		if (len == 0) {
			goto hello_invalid;
		}
		proc_len += len;
		buff += len;
	}

	/*
	 * Return the length of the Hello message body
	 */
	if (proc_len > blen) {
		scsp_log(LOG_CRIT, "scsp_format_hello: buffer overflow");
		abort();
	}
	return(proc_len);

hello_invalid:
	return(0);
}


/*
 * Format an SCSP output packet
 *
 * Arguments:
 *	dcsp	pointer to DCS for which message is being prepared
 *	msg	pointer to input packet
 *	bpp	pointer to location to put pointer to formatted packet
 *
 * Returns:
 *	0	input packet was invalid
 *	else	length of formatted packet
 *
 */
int
scsp_format_msg(dcsp, msg, bpp)
	Scsp_dcs	*dcsp;
	Scsp_msg	*msg;
	char		**bpp;
{
	char			*buff = (char *)0, *e_buff = (char *)0;
	int			buff_len, e_buff_len;
	int			e_len, len, plen;
	struct scsp_nhdr	*shp;
	Scsp_ext		*exp;

	/*
	 * Allocate a buffer for the message
	 */
	buff_len = dcsp->sd_server->ss_mtu;
	buff = (char *)UM_ALLOC(buff_len);
	if (!buff) {
		scsp_mem_err("scsp_format_msg: dcsp->sd_server->ss_mtu");
	}
	UM_ZERO(buff, buff_len);
	*bpp = buff;

	/*
	 * Encode the fixed header
	 *
	 * Set the version
	 */
	shp = (struct scsp_nhdr *)buff;
	shp->sh_ver = SCSP_VER_1;

	/*
	 * Set the message type
	 */
	shp->sh_type = msg->sc_msg_type;

	/*
	 * Point past the fixed header
	 */
	len = sizeof(struct scsp_nhdr);
	buff_len -= len;

	/*
	 * Encode any extensions into a temporary buffer
	 */
	e_len = 0;
	if (msg->sc_ext) {
		/*
		 * Get a buffer for the extensions
		 */
		e_buff_len = 1024;
		e_buff = (char *)UM_ALLOC(e_buff_len);
		if (!buff) {
			scsp_mem_err("scsp_format_msg: e_buff_len");
		}
		UM_ZERO(e_buff, e_buff_len);

		/*
		 * Encode the extensions
		 */
		for (exp = msg->sc_ext = 0; exp; exp = exp->next) {
			plen = scsp_format_ext(exp, e_buff + e_len,
					e_buff_len - e_len);
			if (plen == 0) {
				goto ignore;
			}
			e_len += plen;
		}

		/*
		 * Free the buffer if we didn't use it
		 */
		if (!e_len) {
			UM_FREE(e_buff);
			e_buff = (char *)0;
		}
	}
	buff_len -= e_len;

	/*
	 * Encode the body of the message, depending on the type
	 */
	switch(msg->sc_msg_type) {
	case SCSP_CA_MSG:
		plen = scsp_format_ca(msg->sc_ca, buff + len, buff_len);
		break;
	case SCSP_CSU_REQ_MSG:
	case SCSP_CSU_REPLY_MSG:
	case SCSP_CSUS_MSG:
		plen = scsp_format_csu(msg->sc_csu_msg, buff + len,
				buff_len);
		break;
	case SCSP_HELLO_MSG:
		plen = scsp_format_hello(msg->sc_hello, buff + len,
				buff_len);
		break;
	default:
		goto ignore;
	}
	if (plen == 0) {
		goto ignore;
	}
	len += plen;

	/*
	 * Copy the extensions to the end of the message
	 */
	if (e_len) {
		shp->sh_ext_off = htons(len);
		UM_COPY(e_buff, buff + len, e_len);
		UM_FREE(e_buff);
	}

	/*
	 * Set the length
	 */
	shp->sh_len = htons(len);

	/*
	 * Compute the message checksum
	 */
	shp->sh_checksum = htons(ip_checksum(buff, len));

	/*
	 * Return the length of the buffer
	 */
	return(len);

ignore:
	if (buff)
		UM_FREE(buff);
	if (e_buff)
		UM_FREE(e_buff);
	*bpp = (char *)0;
	return(0);
}


/*
 * Send an SCSP message
 *
 * Arguments:
 *	dcsp	pointer to DCS control block
 *	msg	pointer to message to send
 *
 * Returns:
 *	0	success
 *	errno	error encountered
 *
 */
int
scsp_send_msg(dcsp, msg)
	Scsp_dcs	*dcsp;
	Scsp_msg	*msg;
{
	int	len, rc;
	char	*buff;

	/*
	 * Make sure we have a socket open
	 */
	if (dcsp->sd_sock == -1) {
		return(EBADF);
	}

	/*
	 * Trace the message
	 */
	if (((scsp_trace_mode & SCSP_TRACE_HELLO_MSG) &&
			msg->sc_msg_type == SCSP_HELLO_MSG) ||
			((scsp_trace_mode & SCSP_TRACE_CA_MSG) &&
			msg->sc_msg_type != SCSP_HELLO_MSG)) {
		scsp_trace_msg(dcsp, msg, 0);
		scsp_trace("\n");
	}

	/*
	 * Put the message into network format
	 */
	len = scsp_format_msg(dcsp, msg, &buff);
	if (len == 0) {
		scsp_log(LOG_ERR, "scsp_send_msg: message conversion failed\n");
		abort();
	}

	/*
	 * Write the message to the DCS
	 */
	rc = write(dcsp->sd_sock, (void *)buff, len);
	UM_FREE(buff);
	if (rc == len || (rc == -1 && errno == EINPROGRESS)) {
		rc = 0;
	} else {
		/*
		 * There was an error on the write--close the VCC
		 */
		(void)close(dcsp->sd_sock);
		dcsp->sd_sock = -1;

		/*
		 * Inform the Hello FSM
		 */
		(void)scsp_hfsm(dcsp, SCSP_HFSM_VC_CLOSED,
				(Scsp_msg *)0);

		/*
		 * Set the return code
		 */
		if (rc == -1)
			rc = errno;
		else
			rc = EINVAL;
	}

	return(rc);
}
OpenPOWER on IntegriCloud