import less(1)
[unleashed/tickless.git] / usr / src / lib / libnls / common / nlsenv.c
blobf48ff1390d1f9961143e6f8b0242b4413cbbe40c
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
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * nlsenv.c:
36 * Utilities for servers to access environment set by listener.
38 * nlsgetcall: Returns pointer to t_call structure listener recieved during
39 * the t_listen. Gets data from environment and converts
40 * the data to internal address form.
42 * nlsprovider: Returns name of provider from environment.
46 #include <ctype.h>
47 #include <strings.h>
48 #include <sys/tiuser.h>
49 #include "listen.h"
52 * define DEBUGMODE for diagnostic printf's to stderr
55 /* #define DEBUGMODE */
57 #ifdef DEBUGMODE
58 #include <stdio.h>
59 #endif
62 * nlsenv: (static)
64 * Given an environment variable name, a receiving buffer and the length
65 * of the receiving buffer, getenv gets the environment variable, decodes
66 * it and places the decoded data in addr. The return value is the length
67 * of "addr" if succesful, or a negative value if unsuccessful.
70 extern char *getenv();
72 int
73 nlsenv(struct netbuf *buf, char *envname)
75 char *charaddr;
76 extern char *calloc();
77 extern int nlsc2addr();
78 int length;
80 if (!(charaddr = getenv(envname)))
81 return(-11);
83 #ifdef DEBUGMODE
84 fprintf(stderr, "nlsenv: environ %s = %s len = %d\n",
85 envname, charaddr, strlen(charaddr));
86 #endif
88 if ((int)strlen(charaddr) & 1)
89 return(-12);
91 length = (strlen(charaddr) + 1) / 2;
92 if (!(buf->buf = calloc(1, length)))
93 return(-13);
94 else
95 buf->maxlen = length;
96 return(nlsc2addr(buf->buf, buf->maxlen, charaddr));
101 * nlsgetcall: Get calling data provided by the client via the listener.
103 * nlsgetcall allows network server processes started by the
104 * network listener process to access the callers 't_call'
105 * structure provided in the client's t_connect primitive.
107 * This routine gets this data from the environment
108 * via putenv(3C), interprets the data and places the data
109 * in a t_call structure allocated via t_alloc.
111 * synopsis:
113 * struct t_call *nlsgetcall(fd);
114 * int fd; arg now ignored
117 * returns: Address of an allocated t_call structure
118 * or
119 * NULL for failure. (calloc failed)
120 * If calloc succeeds, non-existant
121 * env. variables or data is indicated
122 * by a negative 'len' field in the approp.
123 * netbuf structure. A length of zero in the
124 * netbuf structure is valid.
128 struct t_call *
129 nlsgetcall(int fd)
131 struct t_call *call;
132 extern char *calloc();
134 if (!(call = (struct t_call *) calloc(1, sizeof(struct t_call))))
135 return(NULL);
138 * Note: space for buffers gets allocated by nlsenv on the fly
141 call->addr.len = nlsenv(&call->addr, NLSADDR);
142 call->opt.len = nlsenv(&call->opt, NLSOPT);
143 call->udata.len = nlsenv(&call->udata, NLSUDATA);
145 return (call);
150 * nlsprovider: Return the name of the transport provider
151 * as placed in the environment by the Network listener
152 * process. If the variable is not defined in the
153 * environment, a NULL pointer is returned.
155 * If the provider is "/dev/starlan", nlsprovider
156 * returns a pointer to the null terminated character string:
157 * "/dev/starlan" if this calling process is a child of the
158 * network listener process.
161 char *
162 nlsprovider()
164 return(getenv(NLSPROVIDER));
169 * nlsc2addr: Convert external address to internal form.
170 * (from nlsaddr.c)
174 * asctohex(X): convert char X to integer value
175 * assumes isxdigit(X). returns integer value.
176 * Note that 'a' > 'A'. See usage in code below.
179 #define asctohex(X) \
180 ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))
183 * nlsc2addr: Given a buffer containing the hex/ascii representation
184 * of a logical address, the buffer's size and an address
185 * of a receiving buffer, char2addr converts the logical
186 * addr to internal format and returns the size of the logical
187 * address. A negative value is returned and the receiving
188 * buffers contents are undefined if:
190 * A. The receiving buffer is not large enough. (rc = -1)
191 * B. If 'charaddr' does not contain a series of octets
192 * (strlen(charaddr) must be even). (rc = -2)
193 * C. Any character in 'charaddr' is not an ASCII hex digit.
194 * (rc = -3)
196 * NOTE: that even if the internal representation of an address is
197 * an ASCII string, there is no guarantee that the output will be
198 * null terminated, thus the returned length must be used when
199 * accessing the internal address.
204 nlsc2addr(char *addr, int maxlen, char *charaddr)
206 int len;
207 int i;
208 char c;
209 unsigned char val;
211 if (strlen(charaddr) & 1)
212 return(-1);
214 for (len = 0; ((maxlen--) && (*charaddr)); ++len) {
215 for (i = 2, val = 0; i--; ) {
216 c = *charaddr++;
217 if (!(isxdigit(c)))
218 return(-3);
219 val = (val << 4) | (unsigned char)asctohex(c);
222 *addr++ = (char)val;
225 #ifdef DEBUGMODE
226 fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
227 #endif
229 return(*charaddr ? -2 : len);