]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/lib/libatm/atm_addr.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / lib / libatm / atm_addr.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  */
26
27 #ifndef lint
28 #if 0   /* original (broken) import id */
29 static char *RCSid = "@(#) $Id: atm_addr.c,v 1.1 1998/07/09 21:45:18 johnc Exp $";
30 #endif
31 #endif
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * User Space Library Functions
37  * ----------------------------
38  *
39  * ATM address utility functions
40  *
41  */
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47 #include <netinet/in.h>
48 #include <netatm/port.h>
49 #include <netatm/atm.h>
50 #include <netatm/atm_if.h>
51 #include <netatm/atm_sap.h>
52 #include <netatm/atm_sys.h>
53 #include <netatm/atm_ioctl.h>
54
55 #include <stdio.h>
56 #include <string.h>
57
58 #include "libatm.h"
59
60 extern char     *prog;
61
62 /*
63  * Get NSAP, NSAP prefix or MAC address
64  *
65  * Arguments:
66  *      in      pointer to an address in ASCII
67  *      out     pointer to a buffer for the converted address
68  *      len     the length of the output buffer
69  *
70  * Returns:
71  *      0       error in format
72  *      len     the length of the data in the output buffer
73  *
74  */
75 int
76 get_hex_atm_addr(const char *in, u_char *out, int len)
77 {
78         int     c_type, c_value, i, out_len, state, val = 0;
79
80         /*
81          * Character table
82          */
83         static struct {
84                 char    c;
85                 int     type;
86                 int     value;
87         } char_table[] = {
88                 {'.',   0,      0},     /* Type 0 -- period */
89                 {':',   0,      0},     /* Type 0 -- colon */
90                 {'0',   1,      0},     /* Type 1 -- hex digit */
91                 {'1',   1,      1},
92                 {'2',   1,      2},
93                 {'3',   1,      3},
94                 {'4',   1,      4},
95                 {'5',   1,      5},
96                 {'6',   1,      6},
97                 {'7',   1,      7},
98                 {'8',   1,      8},
99                 {'9',   1,      9},
100                 {'a',   1,      10},
101                 {'b',   1,      11},
102                 {'c',   1,      12},
103                 {'d',   1,      13},
104                 {'e',   1,      14},
105                 {'f',   1,      15},
106                 {'A',   1,      10},
107                 {'B',   1,      11},
108                 {'C',   1,      12},
109                 {'D',   1,      13},
110                 {'E',   1,      14},
111                 {'F',   1,      15},
112                 {'\0',  2,      0},     /* Type 2 -- end of input */
113         };
114
115         /*
116          * State table
117          */
118         static struct {
119                 int     action;
120                 int     state;
121         } state_table[3][3] = {
122                 /* Period     Hex       End                     */
123                 { { 0, 0 }, { 1, 1 }, { 2, 0} },        /* Init */
124                 { { 4, 0 }, { 3, 2 }, { 4, 0} },        /* C1   */
125                 { { 0, 2 }, { 1, 1 }, { 2, 0} },        /* C2   */
126         };
127
128         /*
129          * Initialize
130          */
131         state = 0;
132         out_len = 0;
133         if (!strncasecmp(in, "0x", 2)) {
134                 in += 2;
135         }
136
137         /*
138          * Loop through input until state table says to return
139          */
140         while (1) {
141                 /*
142                  * Get the character type and value
143                  */
144                 for (i=0; char_table[i].c; i++)
145                         if (char_table[i].c == *in)
146                                 break;
147                 if (char_table[i].c != *in)
148                         return(0);
149                 c_type = char_table[i].type;
150                 c_value = char_table[i].value;
151
152                 /*
153                  * Process next character based on state and type
154                  */
155                 switch(state_table[state][c_type].action) {
156                 case 0:
157                         /*
158                          * Ignore the character
159                          */
160                         break;
161
162                 case 1:
163                         /*
164                          * Save the character's value
165                          */
166                         val = c_value;
167                         break;
168
169                 case 2:
170                         /*
171                          * Return the assembled NSAP
172                          */
173                         return(out_len);
174
175                 case 3:
176                         /*
177                          * Assemble and save the output byte
178                          */
179                         val = val << 4;
180                         val += c_value;
181                         out[out_len] = (u_char) val;
182                         out_len++;
183                         if (out_len > len)
184                                 (void)fprintf(stderr, "%s() out_len > len (%d > %d)\n",
185                                                 __func__, out_len, len);
186                                 
187                         break;
188
189                 case 4:
190                         /*
191                          * Invalid input sequence
192                          */
193                         return(0);
194
195                 default:
196                         return(0);
197                 }
198
199                 /*
200                  * Set the next state and go on to the next character
201                  */
202                 state = state_table[state][c_type].state;
203                 in++;
204         }
205 }
206
207
208 /*
209  * Format an ATM address into a string
210  * 
211  * Arguments:
212  *      addr    pointer to an atm address
213  *
214  * Returns:
215  *      none
216  *
217  */
218 char *
219 format_atm_addr(const Atm_addr *addr)
220 {
221         int             i;
222         const char              *nsap_format;
223         const Atm_addr_nsap     *atm_nsap;
224         const Atm_addr_e164     *atm_e164;
225         const Atm_addr_spans    *atm_spans;
226         const Atm_addr_pvc      *atm_pvc;
227         static char     str[256];
228         union {
229                 int     w;
230                 char    c[4];
231         } u1, u2;
232
233         static const char       nsap_format_DCC[] = "0x%02x.%02x%02x.%02x.%02x%02x%02x.%02x%02x.%02x%02x.%02x%02x.%02x%02x%02x%02x%02x%02x.%02x";
234         static const char       nsap_format_ICD[] = "0x%02x.%02x%02x.%02x.%02x%02x%02x.%02x%02x.%02x%02x.%02x%02x.%02x%02x%02x%02x%02x%02x.%02x";
235         static const char       nsap_format_E164[] = "0x%02x.%02x%02x%02x%02x%02x%02x%02x%02x.%02x%02x.%02x%02x.%02x%02x%02x%02x%02x%02x.%02x";
236
237         /*
238          * Clear the returned string
239          */
240         bzero(str, sizeof(str));
241         strcpy(str, "-");
242
243         /*
244          * Print format is determined by address type
245          */
246         switch (addr->address_format) {
247         case T_ATM_ENDSYS_ADDR:
248                 atm_nsap = (const Atm_addr_nsap *)addr->address;
249                 switch(atm_nsap->aan_afi) {
250                 default:
251                 case AFI_DCC:
252                         nsap_format = nsap_format_DCC;
253                         break;
254                 case AFI_ICD:
255                         nsap_format = nsap_format_ICD;
256                         break;
257                 case AFI_E164:
258                         nsap_format = nsap_format_E164;
259                         break;
260                 }
261                 sprintf(str, nsap_format, 
262                                 atm_nsap->aan_afi,
263                                 atm_nsap->aan_afspec[0],
264                                 atm_nsap->aan_afspec[1],
265                                 atm_nsap->aan_afspec[2],
266                                 atm_nsap->aan_afspec[3],
267                                 atm_nsap->aan_afspec[4],
268                                 atm_nsap->aan_afspec[5],
269                                 atm_nsap->aan_afspec[6],
270                                 atm_nsap->aan_afspec[7],
271                                 atm_nsap->aan_afspec[8],
272                                 atm_nsap->aan_afspec[9],
273                                 atm_nsap->aan_afspec[10],
274                                 atm_nsap->aan_afspec[11],
275                                 atm_nsap->aan_esi[0],
276                                 atm_nsap->aan_esi[1],
277                                 atm_nsap->aan_esi[2],
278                                 atm_nsap->aan_esi[3],
279                                 atm_nsap->aan_esi[4],
280                                 atm_nsap->aan_esi[5],
281                                 atm_nsap->aan_sel);
282                 break;
283
284         case T_ATM_E164_ADDR:
285                 atm_e164 = (const Atm_addr_e164 *)addr->address;
286                 for(i=0; i<addr->address_length; i++) {
287                         sprintf(&str[strlen(str)], "%c",
288                                         atm_e164->aae_addr[i]);
289                 }
290                 break;
291
292         case T_ATM_SPANS_ADDR:
293                 /*
294                  * Print SPANS address as two words, xxxx.yyyy
295                  */
296                 atm_spans = (const Atm_addr_spans *)addr->address;
297                 u1.c[0] = atm_spans->aas_addr[0];
298                 u1.c[1] = atm_spans->aas_addr[1];
299                 u1.c[2] = atm_spans->aas_addr[2];
300                 u1.c[3] = atm_spans->aas_addr[3];
301
302                 u2.c[0] = atm_spans->aas_addr[4];
303                 u2.c[1] = atm_spans->aas_addr[5];
304                 u2.c[2] = atm_spans->aas_addr[6];
305                 u2.c[3] = atm_spans->aas_addr[7];
306
307                 if (!(u1.w == 0 && u2.w == 0))
308                         sprintf(str, "0x%08lx.%08lx",
309                                 (u_long)ntohl(u1.w), (u_long)ntohl(u2.w));
310                 break;
311
312         case T_ATM_PVC_ADDR:
313                 /*
314                  * Print PVC as VPI, VCI
315                  */
316                 atm_pvc = (const Atm_addr_pvc *)addr->address;
317                 sprintf(str, "%d, %d",
318                                 ATM_PVC_GET_VPI(atm_pvc),
319                                 ATM_PVC_GET_VCI(atm_pvc));
320                 break;
321
322         case T_ATM_ABSENT:
323         default:
324                 break;
325         }
326
327         return(str);
328 }