Unleashed v1.4
[unleashed.git] / usr / src / cmd / listen / nlsaddr.c
blobbe802e2001bfc2ae4b0da2cff4bb82f30e22dbee
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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"
35 * nlsaddr.c:
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.
54 #include <ctype.h>
55 #include <sys/tiuser.h>
57 #ifndef T_NONE
58 #define T_NONE 0
59 #endif
62 * define DEBUGMODE for diagnostic printf's to stderr
65 /* #define DEBUGMODE */
67 #ifdef DEBUGMODE
68 #include <stdio.h>
69 #endif
71 /* use to debug in listener *only* (not in user programs) */
72 /* #define DEBUG(args) debug args */
74 #ifndef DEBUG
75 #define DEBUG(args)
76 #endif
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.
85 #define asctohex(X) \
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.
110 struct t_call *
111 nlscalloc(fd)
112 int fd;
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;
123 if (t_getinfo(fd, &info) == -1) {
124 DEBUG((5, "nlscalloc: t_getinfo failed, t_errno %d errno %d"));
125 return (NULL);
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 (NULL);
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;
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;
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;
151 return(call);
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(NULL);
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";
174 void
175 nlsaddr2c(charaddr, addr, len)
176 char *charaddr, *addr;
177 int len;
179 register unsigned i;
181 while (len--) {
182 i = (unsigned)*addr++;
183 *charaddr++ = hex_digits[(i>>4) & 0xf];
184 *charaddr++ = hex_digits[i & 0xf];
186 *charaddr = '\0';
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.
202 * (rc = -3)
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;
214 int maxlen;
216 int len;
217 int i;
218 char c;
219 unsigned char val;
221 if (strlen(charaddr) & 1)
222 return(-1);
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);
232 *addr++ = (char)val;
235 #ifdef DEBUGMODE
236 fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
237 #endif
239 return(*charaddr ? -2 : len);