summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/clib/slp_config.c
blob: d48c1cce9afa61a2372f49c39fc508c4566a4bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Provides accessors to configuration properties.
 *
 * slp_readConfig:	attempts to locate slp.conf, and reads in all
 *				properties specified therein.
 * slp_get_mtu:		returns the MTU
 * slp_get_next_onlist:	parses a comma separated list of integers (in
 *				string form), returning one at a time.
 * slp_parse_static_das: parses the list of DAs given in the DAAddresses
 *				property.
 *
 * Also see the config wrapper macros in slp-internal.h.
 */

#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <slp-internal.h>

/*
 * Reads from fp and dynamically reallocates the buffer if necessary.
 * Returns 1 on success, 0 on read completion, and -1 on failure.
 */
static int super_fgets(char **buf, size_t *bufsize, FILE *fp) {
	char *r, *p;
	size_t real_bufsize, readlen = 0;

	p = *buf;
	real_bufsize = *bufsize;
	for (;;) {
		r = fgets(p, (int)real_bufsize, fp);
		if (feof(fp) && !r)
			return (0);
		if (!r)
			return (-1);
		readlen += strlen(r);
		if ((*buf)[readlen - 1] == '\n')
			return (1);

		/* else	buf is too small */
		*bufsize *= 2;
		if (!(*buf = realloc(*buf, *bufsize))) {
			slp_err(LOG_CRIT, 0, "super_fgets", "out of memory");
			return (-1);
		}
		p = *buf + readlen;
		real_bufsize = *bufsize - readlen;
	}
}

static void skip_space(char **p) {
	while (*p && **p != '\n' && isspace(**p))
		(*p)++;
}

static void null_space(char *p) {
	for (; *p; p++)
		if (isspace(*p))
			*p = 0;
}

/*
 * Reads into the local property store all properties defined in
 * the config file.
 */
void slp_readConfig() {
	char *cfile, *buf;
	FILE *fp;
	size_t buflen = 512;

	/* check env for alternate config file */
	fp = NULL;
	if (cfile = getenv("SLP_CONF_FILE"))
		fp = fopen(cfile, "rF");
	if (!fp)
		if (!(fp = fopen(SLP_DEFAULT_CONFIG_FILE, "rF"))) {
			slp_err(LOG_INFO, 0, "readConfig",
				"cannot open config file");
			return;
		}

	if (!(buf = malloc(buflen))) {
		slp_err(LOG_CRIT, 0, "readConfig", "out of memory");
		(void) fclose(fp);
		return;
	}

	while (!feof(fp)) {
		char *val, *p;
		int err;

		/* read a line */
		err = super_fgets(&buf, &buflen, fp);
		if (err == 0) continue;
		if (err == -1) {
			slp_err(LOG_INFO, 0, "readConfig",
				"error reading file: %d",
				ferror(fp));
			(void) fclose(fp);
			free(buf);
			return;
		}

		/* skip comments and newlines */
		p = buf;
		skip_space(&p);
		if (*p == '#' || *p == ';' || *p == '\n')
			continue;

		/* get property and value */
		if (val = strchr(p, '=')) {
			*val++ = 0;
			skip_space(&val);
			/* remove the trailing newline */
			val[strlen(val) - 1] = 0;
		}
		null_space(p);

		SLPSetProperty(p, val ? val : "");
	}

	(void) fclose(fp);
	free(buf);
}

/*
 * Config convenience wrappers
 */
size_t slp_get_mtu() {
	size_t size;
	size = atoi(SLPGetProperty(SLP_CONFIG_MTU));
	size = size ? size : SLP_DEFAULT_SENDMTU;

	return (size);
}

/*
 * On the first invocation, *state should == the value of the property
 * to parse.
 * If there are no more timeouts, returns -1, otherwise the timeout.
 * If the value in the property is invalid, returns the default 2000.
 */
int slp_get_next_onlist(char **state) {
	char *p, buf[33];
	size_t l;
	int answer;

	if (!*state)
		return (-1);

	if (**state == ',') {
		(*state)++;	/* skip the ',' */
	}
	p = *state;
	*state = slp_utf_strchr(*state, ',');
	if (!*state)
		l = strlen(p);
	else {
		l = *state - p;
		l = (l > 32 ? 32 : l);
	}
	(void) strncpy(buf, p, l);
	buf[l] = 0;
	answer = atoi(buf);

	return (answer != 0 ? answer : 2000);
}

int slp_get_maxResults() {
	int num = atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS));

	return (num <= 0 ? -1 : num);
}