1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30
31
32 #pragma ident "%Z%%M% %I% %E% SMI"
33
34 /*
35 * nlsaddr.c:
36 *
37 * name/address conversion routines for listener and nlsadmin
38 * and other user processes. Converts internal address <--> external addr
39 *
40 * internal address is any number of octets. (length must be provided)
41 * external address is ascii/hex string. (implicit length)
42 *
43 *
44 * nlscalloc: dynamically allocates a t_call structure large enough
45 * to hold the external representations of addr, opt and udata.
46 * Use t_free to release the call structure.
47 *
48 * nlsaddr2c: Convert internal address to external form.
49 *
50 * nlsc2addr: Convert external address to internal form.
51 *
52 */
53
54 #include <ctype.h>
55 #include <sys/tiuser.h>
56
57 #ifndef T_NONE
58 #define T_NONE 0
59 #endif
60
61 /*
62 * define DEBUGMODE for diagnostic printf's to stderr
63 */
64
65 /* #define DEBUGMODE */
66
67 #ifdef DEBUGMODE
68 #include <stdio.h>
69 #endif
70
71 /* use to debug in listener *only* (not in user programs) */
72 /* #define DEBUG(args) debug args */
73
74 #ifndef DEBUG
75 #define DEBUG(args)
76 #endif
77
78
79 /*
80 * asctohex(X): convert char X to integer value
81 * assumes isxdigit(X). returns integer value.
82 * Note that 'a' > 'A'. See usage in code below.
83 */
84
85 #define asctohex(X) \
86 ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))
87
88 /*
89 * externsz(I): returns the number of chars required to hold an external
90 * address whose internal representation contains I octets.
91 * Adds enough space for a 16 char environ name to be prepended
92 * to the external name for the listener.
93 */
94
95 #define externsz(I) (unsigned int)((I<<1) + 41)
96
97
98 /*
99 * nlscalloc: allocate a call structure large enough to hold the
100 * external representation of the addr, opt and udata fields.
101 * similar to the way t_alloc works for the internal
102 * representation of an address.
103 *
104 * returns a pointer to the t_call strucure if successful,
105 * a NULL pointer indicates failure. The external integer
106 * t_errno will contain an error code.
107 *
108 */
109
110 struct t_call *
nlscalloc(fd)111 nlscalloc(fd)
112 int fd;
113 {
114 struct t_info info;
115 struct t_call *call;
116 register unsigned size;
117 register char *p;
118 extern char *malloc(), *t_alloc();
119 extern int t_getinfo();
120 extern char *malloc();
121 extern int t_errno, errno;
122
123 if (t_getinfo(fd, &info) == -1) {
124 DEBUG((5, "nlscalloc: t_getinfo failed, t_errno %d errno %d"));
125 return ((struct t_call *)0);
126 }
127
128 if (!(call = (struct t_call *)t_alloc(fd, T_CALL, T_NONE))) {
129 DEBUG((5, "nlscalloc: t_alloc failed, t_errno %d errno %d"));
130 return ((struct t_call *)0);
131 }
132
133 if (size = externsz((unsigned)info.addr))
134 if (!(p = malloc(size)))
135 goto fail;
136 if (call->addr.maxlen = size)
137 call->addr.buf = p;
138
139 if (size = externsz((unsigned)info.options))
140 if (!(p = malloc(size)))
141 goto fail;
142 if (call->opt.maxlen = size)
143 call->opt.buf = p;
144
145 if (size = externsz((unsigned)info.connect))
146 if (!(p = malloc(size)))
147 goto fail;
148 if (call->udata.maxlen = size)
149 call->udata.buf = p;
150
151 return(call);
152
153 fail:
154 DEBUG((1, "nlscalloc: malloc failed!"));
155 t_free((char *)call, T_CALL); /* t_free will release allocated mem*/
156 t_errno = TSYSERR; /* errno must be ENOMEM */
157 return((struct t_call *)0);
158 }
159
160
161 /*
162 * nlsaddr2c: given a pointer to a logical address and it's length
163 * and a receiving buffer, addr2char converts the
164 * logical address to the hex/ascii char
165 * representation of that address.
166 *
167 * WARNING: The receiving buffer must be large enough.
168 * rcv buffer must be at least (2*len)+1 bytes.
169 *
170 */
171
172 static char hex_digits[] = "0123456789ABCDEF";
173
174 void
nlsaddr2c(charaddr,addr,len)175 nlsaddr2c(charaddr, addr, len)
176 char *charaddr, *addr;
177 int len;
178 {
179 register unsigned i;
180
181 while (len--) {
182 i = (unsigned)*addr++;
183 *charaddr++ = hex_digits[(i>>4) & 0xf];
184 *charaddr++ = hex_digits[i & 0xf];
185 }
186 *charaddr = (char)0;
187 }
188
189
190 /*
191 * nlsc2addr: Given a buffer containing the hex/ascii representation
192 * of a logical address, the buffer's size and an address
193 * of a receiving buffer, char2addr converts the logical
194 * addr to internal format and returns the size of the logical
195 * address. A negative value is returned and the receiving
196 * buffers contents are undefined if:
197 *
198 * A. The receiving buffer is not large enough. (rc = -1)
199 * B. If 'charaddr' does not contain a series of octets
200 * (strlen(charaddr) must be even). (rc = -2)
201 * C. Any character in 'charaddr' is not an ASCII hex digit.
202 * (rc = -3)
203 *
204 * NOTE: that even if the internal representation of an address is
205 * an ASCII string, there is no guarantee that the output will be
206 * null terminated, thus the returned length must be used when
207 * accessing the internal address.
208 */
209
210
211 int
nlsc2addr(addr,maxlen,charaddr)212 nlsc2addr(addr, maxlen, charaddr)
213 char *addr, *charaddr;
214 int maxlen;
215 {
216 int len;
217 int i;
218 char c;
219 unsigned char val;
220
221 if (strlen(charaddr) & 1)
222 return(-1);
223
224 for (len = 0; ((maxlen--) && (*charaddr)); ++len) {
225 for (i = 2, val = 0; i--; ) {
226 c = *charaddr++;
227 if (!(isxdigit(c)))
228 return(-3);
229 val = (val << 4) | (unsigned char)asctohex(c);
230 }
231
232 *addr++ = (char)val;
233 }
234
235 #ifdef DEBUGMODE
236 fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
237 #endif
238
239 return(*charaddr ? -2 : len);
240 }
241
242