4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Provides accessors to configuration properties.
31 * slp_readConfig: attempts to locate slp.conf, and reads in all
32 * properties specified therein.
33 * slp_get_mtu: returns the MTU
34 * slp_get_next_onlist: parses a comma separated list of integers (in
35 * string form), returning one at a time.
36 * slp_parse_static_das: parses the list of DAs given in the DAAddresses
39 * Also see the config wrapper macros in slp-internal.h.
47 #include <slp-internal.h>
50 * Reads from fp and dynamically reallocates the buffer if necessary.
51 * Returns 1 on success, 0 on read completion, and -1 on failure.
53 static int super_fgets(char **buf
, size_t *bufsize
, FILE *fp
) {
55 size_t real_bufsize
, readlen
= 0;
58 real_bufsize
= *bufsize
;
60 r
= fgets(p
, (int)real_bufsize
, fp
);
66 if ((*buf
)[readlen
- 1] == '\n')
69 /* else buf is too small */
71 if (!(*buf
= realloc(*buf
, *bufsize
))) {
72 slp_err(LOG_CRIT
, 0, "super_fgets", "out of memory");
76 real_bufsize
= *bufsize
- readlen
;
80 static void skip_space(char **p
) {
81 while (*p
&& **p
!= '\n' && isspace(**p
))
85 static void null_space(char *p
) {
92 * Reads into the local property store all properties defined in
95 void slp_readConfig() {
100 /* check env for alternate config file */
102 if (cfile
= getenv("SLP_CONF_FILE"))
103 fp
= fopen(cfile
, "rF");
105 if (!(fp
= fopen(SLP_DEFAULT_CONFIG_FILE
, "rF"))) {
106 slp_err(LOG_INFO
, 0, "readConfig",
107 "cannot open config file");
111 if (!(buf
= malloc(buflen
))) {
112 slp_err(LOG_CRIT
, 0, "readConfig", "out of memory");
122 err
= super_fgets(&buf
, &buflen
, fp
);
123 if (err
== 0) continue;
125 slp_err(LOG_INFO
, 0, "readConfig",
126 "error reading file: %d",
133 /* skip comments and newlines */
136 if (*p
== '#' || *p
== ';' || *p
== '\n')
139 /* get property and value */
140 if (val
= strchr(p
, '=')) {
143 /* remove the trailing newline */
144 val
[strlen(val
) - 1] = 0;
148 SLPSetProperty(p
, val
? val
: "");
156 * Config convenience wrappers
158 size_t slp_get_mtu() {
160 size
= atoi(SLPGetProperty(SLP_CONFIG_MTU
));
161 size
= size
? size
: SLP_DEFAULT_SENDMTU
;
167 * On the first invocation, *state should == the value of the property
169 * If there are no more timeouts, returns -1, otherwise the timeout.
170 * If the value in the property is invalid, returns the default 2000.
172 int slp_get_next_onlist(char **state
) {
180 if (**state
== ',') {
181 (*state
)++; /* skip the ',' */
184 *state
= slp_utf_strchr(*state
, ',');
189 l
= (l
> 32 ? 32 : l
);
191 (void) strncpy(buf
, p
, l
);
195 return (answer
!= 0 ? answer
: 2000);
198 int slp_get_maxResults() {
199 int num
= atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS
));
201 return (num
<= 0 ? -1 : num
);