17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*004388ebScasper * Common Development and Distribution License (the "License").
6*004388ebScasper * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*004388ebScasper * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Provides accessors to configuration properties.
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * slp_readConfig: attempts to locate slp.conf, and reads in all
327c478bd9Sstevel@tonic-gate * properties specified therein.
337c478bd9Sstevel@tonic-gate * slp_get_mtu: returns the MTU
347c478bd9Sstevel@tonic-gate * slp_get_next_onlist: parses a comma separated list of integers (in
357c478bd9Sstevel@tonic-gate * string form), returning one at a time.
367c478bd9Sstevel@tonic-gate * slp_parse_static_das: parses the list of DAs given in the DAAddresses
377c478bd9Sstevel@tonic-gate * property.
387c478bd9Sstevel@tonic-gate *
397c478bd9Sstevel@tonic-gate * Also see the config wrapper macros in slp-internal.h.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <slp-internal.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * Reads from fp and dynamically reallocates the buffer if necessary.
517c478bd9Sstevel@tonic-gate * Returns 1 on success, 0 on read completion, and -1 on failure.
527c478bd9Sstevel@tonic-gate */
super_fgets(char ** buf,size_t * bufsize,FILE * fp)537c478bd9Sstevel@tonic-gate static int super_fgets(char **buf, size_t *bufsize, FILE *fp) {
547c478bd9Sstevel@tonic-gate char *r, *p;
557c478bd9Sstevel@tonic-gate size_t real_bufsize, readlen = 0;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate p = *buf;
587c478bd9Sstevel@tonic-gate real_bufsize = *bufsize;
597c478bd9Sstevel@tonic-gate for (;;) {
607c478bd9Sstevel@tonic-gate r = fgets(p, (int)real_bufsize, fp);
617c478bd9Sstevel@tonic-gate if (feof(fp) && !r)
627c478bd9Sstevel@tonic-gate return (0);
637c478bd9Sstevel@tonic-gate if (!r)
647c478bd9Sstevel@tonic-gate return (-1);
657c478bd9Sstevel@tonic-gate readlen += strlen(r);
667c478bd9Sstevel@tonic-gate if ((*buf)[readlen - 1] == '\n')
677c478bd9Sstevel@tonic-gate return (1);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /* else buf is too small */
707c478bd9Sstevel@tonic-gate *bufsize *= 2;
717c478bd9Sstevel@tonic-gate if (!(*buf = realloc(*buf, *bufsize))) {
727c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "super_fgets", "out of memory");
737c478bd9Sstevel@tonic-gate return (-1);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate p = *buf + readlen;
767c478bd9Sstevel@tonic-gate real_bufsize = *bufsize - readlen;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
skip_space(char ** p)807c478bd9Sstevel@tonic-gate static void skip_space(char **p) {
817c478bd9Sstevel@tonic-gate while (*p && **p != '\n' && isspace(**p))
827c478bd9Sstevel@tonic-gate (*p)++;
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
null_space(char * p)857c478bd9Sstevel@tonic-gate static void null_space(char *p) {
867c478bd9Sstevel@tonic-gate for (; *p; p++)
877c478bd9Sstevel@tonic-gate if (isspace(*p))
887c478bd9Sstevel@tonic-gate *p = 0;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * Reads into the local property store all properties defined in
937c478bd9Sstevel@tonic-gate * the config file.
947c478bd9Sstevel@tonic-gate */
slp_readConfig()957c478bd9Sstevel@tonic-gate void slp_readConfig() {
967c478bd9Sstevel@tonic-gate char *cfile, *buf;
977c478bd9Sstevel@tonic-gate FILE *fp;
987c478bd9Sstevel@tonic-gate size_t buflen = 512;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /* check env for alternate config file */
1017c478bd9Sstevel@tonic-gate fp = NULL;
1027c478bd9Sstevel@tonic-gate if (cfile = getenv("SLP_CONF_FILE"))
103*004388ebScasper fp = fopen(cfile, "rF");
1047c478bd9Sstevel@tonic-gate if (!fp)
105*004388ebScasper if (!(fp = fopen(SLP_DEFAULT_CONFIG_FILE, "rF"))) {
1067c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "readConfig",
1077c478bd9Sstevel@tonic-gate "cannot open config file");
1087c478bd9Sstevel@tonic-gate return;
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (!(buf = malloc(buflen))) {
1127c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "readConfig", "out of memory");
1137c478bd9Sstevel@tonic-gate (void) fclose(fp);
1147c478bd9Sstevel@tonic-gate return;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate while (!feof(fp)) {
1187c478bd9Sstevel@tonic-gate char *val, *p;
1197c478bd9Sstevel@tonic-gate int err;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* read a line */
1227c478bd9Sstevel@tonic-gate err = super_fgets(&buf, &buflen, fp);
1237c478bd9Sstevel@tonic-gate if (err == 0) continue;
1247c478bd9Sstevel@tonic-gate if (err == -1) {
1257c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "readConfig",
1267c478bd9Sstevel@tonic-gate "error reading file: %d",
1277c478bd9Sstevel@tonic-gate ferror(fp));
1287c478bd9Sstevel@tonic-gate (void) fclose(fp);
1297c478bd9Sstevel@tonic-gate free(buf);
1307c478bd9Sstevel@tonic-gate return;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /* skip comments and newlines */
1347c478bd9Sstevel@tonic-gate p = buf;
1357c478bd9Sstevel@tonic-gate skip_space(&p);
1367c478bd9Sstevel@tonic-gate if (*p == '#' || *p == ';' || *p == '\n')
1377c478bd9Sstevel@tonic-gate continue;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /* get property and value */
1407c478bd9Sstevel@tonic-gate if (val = strchr(p, '=')) {
1417c478bd9Sstevel@tonic-gate *val++ = 0;
1427c478bd9Sstevel@tonic-gate skip_space(&val);
1437c478bd9Sstevel@tonic-gate /* remove the trailing newline */
1447c478bd9Sstevel@tonic-gate val[strlen(val) - 1] = 0;
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate null_space(p);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate SLPSetProperty(p, val ? val : "");
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate (void) fclose(fp);
1527c478bd9Sstevel@tonic-gate free(buf);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate * Config convenience wrappers
1577c478bd9Sstevel@tonic-gate */
slp_get_mtu()1587c478bd9Sstevel@tonic-gate size_t slp_get_mtu() {
1597c478bd9Sstevel@tonic-gate size_t size;
1607c478bd9Sstevel@tonic-gate size = atoi(SLPGetProperty(SLP_CONFIG_MTU));
1617c478bd9Sstevel@tonic-gate size = size ? size : SLP_DEFAULT_SENDMTU;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate return (size);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * On the first invocation, *state should == the value of the property
1687c478bd9Sstevel@tonic-gate * to parse.
1697c478bd9Sstevel@tonic-gate * If there are no more timeouts, returns -1, otherwise the timeout.
1707c478bd9Sstevel@tonic-gate * If the value in the property is invalid, returns the default 2000.
1717c478bd9Sstevel@tonic-gate */
slp_get_next_onlist(char ** state)1727c478bd9Sstevel@tonic-gate int slp_get_next_onlist(char **state) {
1737c478bd9Sstevel@tonic-gate char *p, buf[33];
1747c478bd9Sstevel@tonic-gate size_t l;
1757c478bd9Sstevel@tonic-gate int answer;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if (!*state)
1787c478bd9Sstevel@tonic-gate return (-1);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate if (**state == ',') {
1817c478bd9Sstevel@tonic-gate (*state)++; /* skip the ',' */
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate p = *state;
1847c478bd9Sstevel@tonic-gate *state = slp_utf_strchr(*state, ',');
1857c478bd9Sstevel@tonic-gate if (!*state)
1867c478bd9Sstevel@tonic-gate l = strlen(p);
1877c478bd9Sstevel@tonic-gate else {
1887c478bd9Sstevel@tonic-gate l = *state - p;
1897c478bd9Sstevel@tonic-gate l = (l > 32 ? 32 : l);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate (void) strncpy(buf, p, l);
1927c478bd9Sstevel@tonic-gate buf[l] = 0;
1937c478bd9Sstevel@tonic-gate answer = atoi(buf);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate return (answer != 0 ? answer : 2000);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
slp_get_maxResults()1987c478bd9Sstevel@tonic-gate int slp_get_maxResults() {
1997c478bd9Sstevel@tonic-gate int num = atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS));
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate return (num <= 0 ? -1 : num);
2027c478bd9Sstevel@tonic-gate }
203