]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libatm/atm_addr.c
This commit was generated by cvs2svn to compensate for changes in r77298,
[FreeBSD/FreeBSD.git] / 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  *      @(#) $FreeBSD$
27  *
28  */
29
30 /*
31  * User Space Library Functions
32  * ----------------------------
33  *
34  * ATM address utility functions
35  *
36  */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netatm/port.h>
44 #include <netatm/atm.h>
45 #include <netatm/atm_if.h>
46 #include <netatm/atm_sap.h>
47 #include <netatm/atm_sys.h>
48 #include <netatm/atm_ioctl.h>
49
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "libatm.h"
54
55 #ifndef lint
56 __RCSID("@(#) $FreeBSD$");
57 #endif
58
59
60 extern char     *prog;
61
62
63 /*
64  * Get NSAP, NSAP prefix or MAC address
65  *
66  * Arguments:
67  *      in      pointer to an address in ASCII
68  *      out     pointer to a buffer for the converted address
69  *      len     the length of the output buffer
70  *
71  * Returns:
72  *      0       error in format
73  *      len     the length of the data in the output buffer
74  *
75  */
76 int
77 get_hex_atm_addr(in, out, len)
78         char    *in;
79         u_char  *out;
80         int     len;
81 {
82         int     c_type, c_value, i, out_len, state, val = 0;
83
84         /*
85          * Character table
86          */
87         static struct {
88                 char    c;
89                 int     type;
90                 int     value;
91         } char_table[] = {
92                 {'.',   0,      0},     /* Type 0 -- period */
93                 {':',   0,      0},     /* Type 0 -- colon */
94                 {'0',   1,      0},     /* Type 1 -- hex digit */
95                 {'1',   1,      1},
96                 {'2',   1,      2},
97                 {'3',   1,      3},
98                 {'4',   1,      4},
99                 {'5',   1,      5},
100                 {'6',   1,      6},
101                 {'7',   1,      7},
102                 {'8',   1,      8},
103                 {'9',   1,      9},
104                 {'a',   1,      10},
105                 {'b',   1,      11},
106                 {'c',   1,      12},
107                 {'d',   1,      13},
108                 {'e',   1,      14},
109                 {'f',   1,      15},
110                 {'A',   1,      10},
111                 {'B',   1,      11},
112                 {'C',   1,      12},
113                 {'D',   1,      13},
114                 {'E',   1,      14},
115                 {'F',   1,      15},
116                 {'\0',  2,      0},     /* Type 2 -- end of input */
117         };
118
119         /*
120          * State table
121          */
122         static struct {
123                 int     action;
124                 int     state;
125         } state_table[3][3] = {
126                 /* Period     Hex       End                     */
127                 { { 0, 0 }, { 1, 1 }, { 2, 0} },        /* Init */
128                 { { 4, 0 }, { 3, 2 }, { 4, 0} },        /* C1   */
129                 { { 0, 2 }, { 1, 1 }, { 2, 0} },        /* C2   */
130         };
131
132         /*
133          * Initialize
134          */
135         state = 0;
136         out_len = 0;
137         if (!strncasecmp(in, "0x", 2)) {
138                 in += 2;
139         }
140
141         /*
142          * Loop through input until state table says to return
143          */
144         while (1) {
145                 /*
146                  * Get the character type and value
147                  */
148                 for (i=0; char_table[i].c; i++)
149                         if (char_table[i].c == *in)
150                                 break;
151                 if (char_table[i].c != *in)
152                         return(0);
153                 c_type = char_table[i].type;
154                 c_value = char_table[i].value;
155
156                 /*
157                  * Process next character based on state and type
158                  */
159                 switch(state_table[state][c_type].action) {
160                 case 0:
161                         /*
162                          * Ignore the character
163                          */
164                         break;
165
166                 case 1:
167                         /*
168                          * Save the character's value
169                          */
170                         val = c_value;
171                         break;
172
173                 case 2:
174                         /*
175                          * Return the assembled NSAP
176                          */
177                         return(out_len);
178
179                 case 3:
180                         /*
181                          * Assemble and save the output byte
182                          */
183                         val = val << 4;
184                         val += c_value;
185                         out[out_len] = (u_char) val;
186                         out_len++;
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(addr)
220         Atm_addr *addr;
221 {
222         int             i;
223         char            *nsap_format;
224         Atm_addr_nsap   *atm_nsap;
225         Atm_addr_e164   *atm_e164;
226         Atm_addr_spans  *atm_spans;
227         Atm_addr_pvc    *atm_pvc;
228         static char     str[256];
229         union {
230                 int     w;
231                 char    c[4];
232         } u1, u2;
233
234         static 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";
235         static 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";
236         static 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";
237
238         /*
239          * Clear the returned string
240          */
241         UM_ZERO(str, sizeof(str));
242         strcpy(str, "-");
243
244         /*
245          * Print format is determined by address type
246          */
247         switch (addr->address_format) {
248         case T_ATM_ENDSYS_ADDR:
249                 atm_nsap = (Atm_addr_nsap *)addr->address;
250                 switch(atm_nsap->aan_afi) {
251                 default:
252                 case AFI_DCC:
253                         nsap_format = nsap_format_DCC;
254                         break;
255                 case AFI_ICD:
256                         nsap_format = nsap_format_ICD;
257                         break;
258                 case AFI_E164:
259                         nsap_format = nsap_format_E164;
260                         break;
261                 }
262                 sprintf(str, nsap_format, 
263                                 atm_nsap->aan_afi,
264                                 atm_nsap->aan_afspec[0],
265                                 atm_nsap->aan_afspec[1],
266                                 atm_nsap->aan_afspec[2],
267                                 atm_nsap->aan_afspec[3],
268                                 atm_nsap->aan_afspec[4],
269                                 atm_nsap->aan_afspec[5],
270                                 atm_nsap->aan_afspec[6],
271                                 atm_nsap->aan_afspec[7],
272                                 atm_nsap->aan_afspec[8],
273                                 atm_nsap->aan_afspec[9],
274                                 atm_nsap->aan_afspec[10],
275                                 atm_nsap->aan_afspec[11],
276                                 atm_nsap->aan_esi[0],
277                                 atm_nsap->aan_esi[1],
278                                 atm_nsap->aan_esi[2],
279                                 atm_nsap->aan_esi[3],
280                                 atm_nsap->aan_esi[4],
281                                 atm_nsap->aan_esi[5],
282                                 atm_nsap->aan_sel);
283                 break;
284
285         case T_ATM_E164_ADDR:
286                 atm_e164 = (Atm_addr_e164 *)addr->address;
287                 for(i=0; i<addr->address_length; i++) {
288                         sprintf(&str[strlen(str)], "%c",
289                                         atm_e164->aae_addr[i]);
290                 }
291                 break;
292
293         case T_ATM_SPANS_ADDR:
294                 /*
295                  * Print SPANS address as two words, xxxx.yyyy
296                  */
297                 atm_spans = (Atm_addr_spans *)addr->address;
298                 u1.c[0] = atm_spans->aas_addr[0];
299                 u1.c[1] = atm_spans->aas_addr[1];
300                 u1.c[2] = atm_spans->aas_addr[2];
301                 u1.c[3] = atm_spans->aas_addr[3];
302
303                 u2.c[0] = atm_spans->aas_addr[4];
304                 u2.c[1] = atm_spans->aas_addr[5];
305                 u2.c[2] = atm_spans->aas_addr[6];
306                 u2.c[3] = atm_spans->aas_addr[7];
307
308                 if (!(u1.w == 0 && u2.w == 0))
309                         sprintf(str, "0x%08lx.%08lx", ntohl(u1.w), ntohl(u2.w));
310                 break;
311
312         case T_ATM_PVC_ADDR:
313                 /*
314                  * Print PVC as VPI, VCI
315                  */
316                 atm_pvc = (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 }