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
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.
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]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 #pragma ident "%Z%%M% %I% %E% SMI"
37 * name/address conversion routines for listener and nlsadmin
38 * and other user processes. Converts internal address <--> external addr
40 * internal address is any number of octets. (length must be provided)
41 * external address is ascii/hex string. (implicit length)
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.
48 * nlsaddr2c: Convert internal address to external form.
50 * nlsc2addr: Convert external address to internal form.
55 #include <sys/tiuser.h>
62 * define DEBUGMODE for diagnostic printf's to stderr
65 /* #define DEBUGMODE */
71 /* use to debug in listener *only* (not in user programs) */
72 /* #define DEBUG(args) debug args */
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.
86 ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))
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.
95 #define externsz(I) (unsigned int)((I<<1) + 41)
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.
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.
116 register unsigned size
;
118 extern char *malloc(), *t_alloc();
119 extern int t_getinfo();
120 extern char *malloc();
121 extern int t_errno
, errno
;
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);
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);
133 if (size
= externsz((unsigned)info
.addr
))
134 if (!(p
= malloc(size
)))
136 if (call
->addr
.maxlen
= size
)
139 if (size
= externsz((unsigned)info
.options
))
140 if (!(p
= malloc(size
)))
142 if (call
->opt
.maxlen
= size
)
145 if (size
= externsz((unsigned)info
.connect
))
146 if (!(p
= malloc(size
)))
148 if (call
->udata
.maxlen
= size
)
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);
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.
167 * WARNING: The receiving buffer must be large enough.
168 * rcv buffer must be at least (2*len)+1 bytes.
172 static char hex_digits
[] = "0123456789ABCDEF";
175 nlsaddr2c(charaddr
, addr
, len
)
176 char *charaddr
, *addr
;
182 i
= (unsigned)*addr
++;
183 *charaddr
++ = hex_digits
[(i
>>4) & 0xf];
184 *charaddr
++ = hex_digits
[i
& 0xf];
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:
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.
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.
212 nlsc2addr(addr
, maxlen
, charaddr
)
213 char *addr
, *charaddr
;
221 if (strlen(charaddr
) & 1)
224 for (len
= 0; ((maxlen
--) && (*charaddr
)); ++len
) {
225 for (i
= 2, val
= 0; i
--; ) {
229 val
= (val
<< 4) | (unsigned char)asctohex(c
);
236 fprintf(stderr
, "nlsc2addr: returned length = %d\n", len
);
239 return(*charaddr
? -2 : len
);