Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / contrib / isode / compat / na2norm.c
CommitLineData
5f522de8
C
1/* na2norm.c - normalize NSAPaddr */
2
3#ifndef lint
4static char *rcsid = "$Header: /f/osi/compat/RCS/na2norm.c,v 7.3 91/02/22 09:15:36 mrose Interim $";
5#endif
6
7/*
8 * $Header: /f/osi/compat/RCS/na2norm.c,v 7.3 91/02/22 09:15:36 mrose Interim $
9 *
10 *
11 * $Log: na2norm.c,v $
12 * Revision 7.3 91/02/22 09:15:36 mrose
13 * Interim 6.8
14 *
15 * Revision 7.2 90/08/08 14:03:00 mrose
16 * stuff
17 *
18 * Revision 7.1 90/07/09 14:32:04 mrose
19 * sync
20 *
21 * Revision 7.0 89/11/23 21:23:18 mrose
22 * Release 6.0
23 *
24 */
25
26/*
27 * NOTICE
28 *
29 * Acquisition, use, and distribution of this module and related
30 * materials are subject to the restrictions of a license agreement.
31 * Consult the Preface in the User's Manual for the full terms of
32 * this agreement.
33 *
34 */
35
36
37/* LINTLIBRARY */
38
39#include <stdio.h>
40#include "general.h"
41#include "manifest.h"
42#include "isoaddrs.h"
43#include "internet.h"
44#include "tailor.h"
45
46/* Encoding based on
47
48 "An interim approach to use of Network Addresses",
49 S.E. Kille, January 16, 1989
50
51 */
52
53/* \f */
54
55struct NSAPaddr *na2norm (na)
56register struct NSAPaddr *na;
57{
58 int ilen;
59 register char *cp,
60 *dp;
61 char nsap[NASIZE * 2 + 1];
62 register struct hostent *hp;
63 register struct ts_interim *ts;
64 static struct NSAPaddr nas;
65 register struct NSAPaddr *ca = &nas;
66
67 if (na -> na_stack == NA_NSAP) {
68 *ca = *na; /* struct copy */
69 return ca;
70 }
71
72 bzero ((char *) ca, sizeof *ca);
73 ca -> na_stack = NA_NSAP;
74
75 for (ts = ts_interim; ts -> ts_name; ts++)
76 if (ts -> ts_subnet == na -> na_community)
77 break;
78 if (!ts -> ts_name) {
79 SLOG (addr_log, LLOG_EXCEPTIONS, NULLCP,
80 ("unable to find community #%d", na -> na_community));
81 return NULLNA;
82 }
83
84 cp = nsap;
85 switch (na -> na_stack) {
86 case NA_TCP:
87 if ((hp = gethostbystring (na -> na_domain)) == NULL) {
88 SLOG (addr_log, LLOG_EXCEPTIONS, NULLCP,
89 ("%s: unknown host", na -> na_domain));
90 return NULLNA;
91 }
92#define s2a(b) (((int) (b)) & 0xff)
93 (void) sprintf (cp, "%03d%03d%03d%03d",
94 s2a (hp -> h_addr[0]),
95 s2a (hp -> h_addr[1]),
96 s2a (hp -> h_addr[2]),
97 s2a (hp -> h_addr[3]));
98 cp += strlen (cp);
99#undef s2a
100
101 if (na -> na_port) {
102 (void) sprintf (cp, "%05d", (int) ntohs (na -> na_port));
103 cp += strlen (cp);
104
105 if (na -> na_tset || na -> na_tset != NA_TSET_TCP) {
106 (void) sprintf (cp, "%05d", (int) na -> na_tset);
107 cp += strlen (cp);
108 }
109 }
110 break;
111
112 case NA_X25:
113 case NA_BRG:
114 if (na -> na_community == SUBNET_INT_X25
115 && na -> na_cudflen == 0
116 && na -> na_pidlen == 0
117 && na -> na_dte[0] != '0') { /* SEK - X121 form */
118 /* should be more general */
119 (void) sprintf (nsap, "36%014s", na -> na_dte);
120 ts = NULL;
121 break;
122 }
123
124 if (ilen = na -> na_pidlen & 0xff)
125 *cp++ = '1', dp = na -> na_pid;
126 else
127 if (ilen = na -> na_cudflen & 0xff)
128 *cp++ = '2', dp = na -> na_cudf;
129 else
130 *cp++ = '0';
131 if (ilen) {
132 (void) sprintf (cp, "%01d", ilen);
133 cp += strlen (cp);
134
135 for (; ilen-- > 0; cp += 3)
136 (void) sprintf (cp, "%03d", *dp++ & 0xff);
137 }
138 (void) strcpy (cp, na -> na_dte);
139 break;
140
141 default:
142 SLOG (addr_log, LLOG_EXCEPTIONS, NULLCP,
143 ("unknown address type 0x%x", na -> na_stack));
144 return NULLNA;
145 }
146
147 cp = nsap, dp = ca -> na_address;
148 if (ts) {
149 bcopy (ts -> ts_prefix, dp, ts -> ts_length);
150 dp += ts -> ts_length;
151 }
152 while (*cp) {
153 *dp = (*cp++ - '0') << 4;
154 if (*cp)
155 *dp++ |= (*cp++ - '0') & 0x0f;
156 else
157 *dp++ |= 0x0f;
158 }
159 ca -> na_addrlen = dp - ca -> na_address;
160
161 return ca;
162}