summaryrefslogtreecommitdiffstats
path: root/sys/netatm/uni/unisig_util.c
blob: 6bb23ccd765f42e31f5426bc80912d3b57f3050a (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
/*-
 * ===================================
 * 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.
 */

/*
 * ATM Forum UNI 3.0/3.1 Signalling Manager
 * ----------------------------------------
 *
 * Protocol processing module
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <netatm/port.h>
#include <netatm/queue.h>
#include <netatm/atm.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_cm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_vc.h>
#include <netatm/atm_sigmgr.h>
#include <netatm/atm_stack.h>
#include <netatm/atm_pcb.h>
#include <netatm/atm_var.h>

#include <netatm/uni/unisig_var.h>
#include <netatm/uni/unisig_msg.h>

/*
 * Free a UNISIG signalling message
 *
 * Free the passed message and any IEs that are attached to it
 *
 * Arguments:
 *	msg	pointer to UNISIG protocol instance
 *
 * Returns:
 *	none
 *
 */
void
unisig_free_msg(msg)
	struct unisig_msg	*msg;
{
	int			i;
	struct ie_generic	*ie, *ienxt;

	ATM_DEBUG1("unisig_free_msg: msg=%p\n", msg);

	/*
	 * First free all the IEs
	 */
	for (i=0; i<UNI_MSG_IE_CNT; i++) {
		ie = msg->msg_ie_vec[i];
		while (ie) {
			ienxt = ie->ie_next;
			uma_zfree(unisig_ie_zone, ie);
			ie = ienxt;
		}
	}

	/*
	 * Finally, free the message structure itself
	 */
	uma_zfree(unisig_msg_zone, msg);
}

/*
 * Verify a VCCB
 *
 * Search UNISIG's VCCB queue to verify that a VCCB belongs to UNISIG.
 *
 * Arguments:
 *	usp	pointer to UNISIG protocol instance
 *	svp	pointer to a VCCB
 *
 * Returns:
 *	TRUE	the VCCB belongs to UNISIG
 *	FALSE	the VCCB doesn't belong to UNISIG
 *
 */
int
unisig_verify_vccb(usp, uvp)
	struct unisig		*usp;
	struct unisig_vccb	*uvp;

{
	struct unisig_vccb	*utp, *uvnext;

	for (utp = Q_HEAD(usp->us_vccq, struct unisig_vccb);
			utp; utp = uvnext){
		uvnext = Q_NEXT(utp, struct unisig_vccb, uv_sigelem);
		if (uvp == utp) {
			return(TRUE);
		}
	}
	return(FALSE);
}


/*
 * Find a connection
 *
 * Find a VCCB given the call reference
 *
 * Arguments:
 *	usp	pointer to UNISIG protocol instance
 *	cref	the call reference to search for
 *
 * Returns:
 *	0	there is no such VCCB
 *	uvp	the address of the VCCB
 *
 */
struct unisig_vccb *
unisig_find_conn(usp, cref)
	struct unisig	*usp;
	u_int		cref;

{
	struct unisig_vccb	*uvp, *uvnext;

	for (uvp = Q_HEAD(usp->us_vccq, struct unisig_vccb); uvp;
			uvp = uvnext){
		uvnext = Q_NEXT(uvp, struct unisig_vccb, uv_sigelem);
		if (uvp->uv_call_ref == cref)
			break;
	}
	return(uvp);
}


/*
 * Find a VCCB
 *
 * Find a VCCB given the VPI and VCI.
 *
 * Arguments:
 *	usp	pointer to UNISIG protocol instance
 *	vpi	the VPI to search for
 *	vci	the VCI to search for
 *	dir	the direction of the VCC (VCC_IN, VCC_OUT, or both).
 *		If dir is set to zero, return the address of any VCCB
 *		with the given VPI/VCI, regardless of direction.
 *
 * Returns:
 *	0	there is no such VCCB
 *	uvp	the address of the VCCB
 *
 */
struct unisig_vccb *
unisig_find_vpvc(usp, vpi, vci, dir)
	struct unisig	*usp;
	int		vpi, vci;
	u_char		dir;

{
	struct unisig_vccb	*uvp, *uvnext;

	for (uvp = Q_HEAD(usp->us_vccq, struct unisig_vccb); uvp;
			uvp = uvnext){
		uvnext = Q_NEXT(uvp, struct unisig_vccb, uv_sigelem);
		if (uvp->uv_vpi == vpi &&
				uvp->uv_vci == vci &&
				(uvp->uv_type & dir) == dir)
			break;
	}
	return(uvp);
}


/*
 * Allocate a call reference value
 *
 * Arguments:
 *	usp	pointer to UNISIG protocol instance
 *
 * Returns:
 *	0	call reference not available
 *	cref	the call reference value
 *
 */
int
unisig_alloc_call_ref(usp)
	struct unisig	*usp;

{
	int	cref;

	/*
	 * Get the next call reference value
	 */
	cref = usp->us_cref;

	/*
	 * Make sure it hasn't got too large
	 */
	if (cref >= UNI_MSG_CALL_REF_DUMMY) {
		/* XXX */
		log(LOG_ERR, "uni: call reference limit reached\n");
		return(0);
	}
	
	/*
	 * Bump the call reference value
	 */
	usp->us_cref++;

	return(cref);
}


/*
 * Print an ATM address
 *
 * Convert an ATM address into an ASCII string suitable for printing.
 *
 * Arguments:
 *	p	pointer to an ATM address
 *
 * Returns:
 *	the address of a string with the ASCII representation of the
 *	address.  This routine returns the address of a statically-
 *	allocated buffer, so if repeated calls to this routine are made,
 *	each call will destroy the result of the previous call.
 *
 */
char *
unisig_addr_print(p)
	Atm_addr		*p;
{
	int		i;
	char		*fp, *op, t_buff[16];
	u_char		*cp;
	static char	strbuff[256];

	static char	nf_DCC[] = "0xX.XX.X.XXX.XX.XX.XX.XXXXXX.X";
	static char	nf_ICD[] = "0xX.XX.X.XXX.XX.XX.XX.XXXXXX.X";
	static char	nf_E164[] = "0xX.XXXXXXXX.XX.XX.XXXXXX.X";

	union {
		int	w;
		char	c[4];
	} u1, u2;

	/*
	 * Clear the print buffer
	 */
	bzero(strbuff, sizeof(strbuff));

	/*
	 * Select appropriate printing format
	 */
	switch(p->address_format) {
	case T_ATM_ENDSYS_ADDR:
		/*
		 * Select format by NSAP type
		 */
		switch(((Atm_addr_nsap *)p->address)->aan_afi) {
		default:
		case AFI_DCC:
			fp = nf_DCC;
			break;
		case AFI_ICD:
			fp = nf_ICD;
			break;
		case AFI_E164:
			fp = nf_E164;
			break;
		}

		/*
		 * Loop through the format string, converting the NSAP
		 * to ASCII
		 */
		cp = (u_char *) p->address;
		op = strbuff;
		while (*fp) {
			if (*fp == 'X') {
				/*
				 * If format character is an 'X', put a
				 * two-digit hex representation of the
				 * NSAP byte in the output buffer
				 */
				snprintf(t_buff, sizeof(t_buff),
					"%x", *cp + 512);
				strcpy(op, &t_buff[strlen(t_buff)-2]);
				op++; op++;
				cp++;
			} else {
				/*
				 * If format character isn't an 'X',
				 * just copy it to the output buffer
				 */
				*op = *fp;
				op++;
			}
			fp++;
		}

		break;

	case T_ATM_E164_ADDR:
		/*
		 * Print the IA5 characters of the E.164 address
		 */
		for(i=0; i<p->address_length; i++) {
			snprintf(strbuff + strlen(strbuff),
			    sizeof(strbuff) - strlen(strbuff), "%c",
				((Atm_addr_e164 *)p->address)->aae_addr[i]);
		}
		break;

	case T_ATM_SPANS_ADDR:
		/*
		 * Get address into integers
		 */
		u1.c[0] = ((Atm_addr_spans *)p->address)->aas_addr[0];
		u1.c[1] = ((Atm_addr_spans *)p->address)->aas_addr[1];
		u1.c[2] = ((Atm_addr_spans *)p->address)->aas_addr[2];
		u1.c[3] = ((Atm_addr_spans *)p->address)->aas_addr[3];
		u2.c[0] = ((Atm_addr_spans *)p->address)->aas_addr[4];
		u2.c[1] = ((Atm_addr_spans *)p->address)->aas_addr[5];
		u2.c[2] = ((Atm_addr_spans *)p->address)->aas_addr[6];
		u2.c[3] = ((Atm_addr_spans *)p->address)->aas_addr[7];

		/*
		 * Print the address as two words xxxxx.yyyyyyyy
		 */
		snprintf(strbuff, sizeof(strbuff), "%x.%x", u1.w, u2.w);
		break;

	case T_ATM_ABSENT:
	default:
		strcpy(strbuff, "-");
	}

	return(strbuff);
}


/*
 * Print the contents of a message buffer chain
 *
 * Arguments:
 *	m	pointer to a buffer
 *
 * Returns:
 *	none
 *
 */
void
unisig_print_mbuf(m)
	KBuffer		*m;
{
	int i;
	caddr_t		cp;

	printf("unisig_print_mbuf:\n");
	while (m) { 
		KB_DATASTART(m, cp, caddr_t);
		for (i = 0; i < KB_LEN(m); i++) {
			if (i == 0)
				printf("   bfr=%p: ", m);
			printf("%x ", (u_char)*cp++);
		}
		printf("<end_bfr>\n");
		m = KB_NEXT(m);
	}
}
OpenPOWER on IntegriCloud