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 /*
277c478bd9Sstevel@tonic-gate * Provides accessors to configuration properties.
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate * slp_readConfig: attempts to locate slp.conf, and reads in all
307c478bd9Sstevel@tonic-gate * properties specified therein.
317c478bd9Sstevel@tonic-gate * slp_get_mtu: returns the MTU
327c478bd9Sstevel@tonic-gate * slp_get_next_onlist: parses a comma separated list of integers (in
337c478bd9Sstevel@tonic-gate * string form), returning one at a time.
347c478bd9Sstevel@tonic-gate * slp_parse_static_das: parses the list of DAs given in the DAAddresses
357c478bd9Sstevel@tonic-gate * property.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate * Also see the config wrapper macros in slp-internal.h.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <syslog.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <slp-internal.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * Reads from fp and dynamically reallocates the buffer if necessary.
497c478bd9Sstevel@tonic-gate * Returns 1 on success, 0 on read completion, and -1 on failure.
507c478bd9Sstevel@tonic-gate */
super_fgets(char ** buf,size_t * bufsize,FILE * fp)517c478bd9Sstevel@tonic-gate static int super_fgets(char **buf, size_t *bufsize, FILE *fp) {
527c478bd9Sstevel@tonic-gate char *r, *p;
537c478bd9Sstevel@tonic-gate size_t real_bufsize, readlen = 0;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate p = *buf;
567c478bd9Sstevel@tonic-gate real_bufsize = *bufsize;
577c478bd9Sstevel@tonic-gate for (;;) {
587c478bd9Sstevel@tonic-gate r = fgets(p, (int)real_bufsize, fp);
597c478bd9Sstevel@tonic-gate if (feof(fp) && !r)
607c478bd9Sstevel@tonic-gate return (0);
617c478bd9Sstevel@tonic-gate if (!r)
627c478bd9Sstevel@tonic-gate return (-1);
637c478bd9Sstevel@tonic-gate readlen += strlen(r);
647c478bd9Sstevel@tonic-gate if ((*buf)[readlen - 1] == '\n')
657c478bd9Sstevel@tonic-gate return (1);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /* else buf is too small */
687c478bd9Sstevel@tonic-gate *bufsize *= 2;
697c478bd9Sstevel@tonic-gate if (!(*buf = realloc(*buf, *bufsize))) {
707c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "super_fgets", "out of memory");
717c478bd9Sstevel@tonic-gate return (-1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate p = *buf + readlen;
747c478bd9Sstevel@tonic-gate real_bufsize = *bufsize - readlen;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
skip_space(char ** p)787c478bd9Sstevel@tonic-gate static void skip_space(char **p) {
797c478bd9Sstevel@tonic-gate while (*p && **p != '\n' && isspace(**p))
807c478bd9Sstevel@tonic-gate (*p)++;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
null_space(char * p)837c478bd9Sstevel@tonic-gate static void null_space(char *p) {
847c478bd9Sstevel@tonic-gate for (; *p; p++)
857c478bd9Sstevel@tonic-gate if (isspace(*p))
867c478bd9Sstevel@tonic-gate *p = 0;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * Reads into the local property store all properties defined in
917c478bd9Sstevel@tonic-gate * the config file.
927c478bd9Sstevel@tonic-gate */
slp_readConfig()937c478bd9Sstevel@tonic-gate void slp_readConfig() {
947c478bd9Sstevel@tonic-gate char *cfile, *buf;
957c478bd9Sstevel@tonic-gate FILE *fp;
967c478bd9Sstevel@tonic-gate size_t buflen = 512;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* check env for alternate config file */
997c478bd9Sstevel@tonic-gate fp = NULL;
1007c478bd9Sstevel@tonic-gate if (cfile = getenv("SLP_CONF_FILE"))
101*004388ebScasper fp = fopen(cfile, "rF");
1027c478bd9Sstevel@tonic-gate if (!fp)
103*004388ebScasper if (!(fp = fopen(SLP_DEFAULT_CONFIG_FILE, "rF"))) {
1047c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "readConfig",
1057c478bd9Sstevel@tonic-gate "cannot open config file");
1067c478bd9Sstevel@tonic-gate return;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (!(buf = malloc(buflen))) {
1107c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "readConfig", "out of memory");
1117c478bd9Sstevel@tonic-gate (void) fclose(fp);
1127c478bd9Sstevel@tonic-gate return;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate while (!feof(fp)) {
1167c478bd9Sstevel@tonic-gate char *val, *p;
1177c478bd9Sstevel@tonic-gate int err;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /* read a line */
1207c478bd9Sstevel@tonic-gate err = super_fgets(&buf, &buflen, fp);
1217c478bd9Sstevel@tonic-gate if (err == 0) continue;
1227c478bd9Sstevel@tonic-gate if (err == -1) {
1237c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "readConfig",
1247c478bd9Sstevel@tonic-gate "error reading file: %d",
1257c478bd9Sstevel@tonic-gate ferror(fp));
1267c478bd9Sstevel@tonic-gate (void) fclose(fp);
1277c478bd9Sstevel@tonic-gate free(buf);
1287c478bd9Sstevel@tonic-gate return;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* skip comments and newlines */
1327c478bd9Sstevel@tonic-gate p = buf;
1337c478bd9Sstevel@tonic-gate skip_space(&p);
1347c478bd9Sstevel@tonic-gate if (*p == '#' || *p == ';' || *p == '\n')
1357c478bd9Sstevel@tonic-gate continue;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /* get property and value */
1387c478bd9Sstevel@tonic-gate if (val = strchr(p, '=')) {
1397c478bd9Sstevel@tonic-gate *val++ = 0;
1407c478bd9Sstevel@tonic-gate skip_space(&val);
1417c478bd9Sstevel@tonic-gate /* remove the trailing newline */
1427c478bd9Sstevel@tonic-gate val[strlen(val) - 1] = 0;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate null_space(p);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate SLPSetProperty(p, val ? val : "");
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate (void) fclose(fp);
1507c478bd9Sstevel@tonic-gate free(buf);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate * Config convenience wrappers
1557c478bd9Sstevel@tonic-gate */
slp_get_mtu()1567c478bd9Sstevel@tonic-gate size_t slp_get_mtu() {
1577c478bd9Sstevel@tonic-gate size_t size;
1587c478bd9Sstevel@tonic-gate size = atoi(SLPGetProperty(SLP_CONFIG_MTU));
1597c478bd9Sstevel@tonic-gate size = size ? size : SLP_DEFAULT_SENDMTU;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate return (size);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * On the first invocation, *state should == the value of the property
1667c478bd9Sstevel@tonic-gate * to parse.
1677c478bd9Sstevel@tonic-gate * If there are no more timeouts, returns -1, otherwise the timeout.
1687c478bd9Sstevel@tonic-gate * If the value in the property is invalid, returns the default 2000.
1697c478bd9Sstevel@tonic-gate */
slp_get_next_onlist(char ** state)1707c478bd9Sstevel@tonic-gate int slp_get_next_onlist(char **state) {
1717c478bd9Sstevel@tonic-gate char *p, buf[33];
1727c478bd9Sstevel@tonic-gate size_t l;
1737c478bd9Sstevel@tonic-gate int answer;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (!*state)
1767c478bd9Sstevel@tonic-gate return (-1);
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (**state == ',') {
1797c478bd9Sstevel@tonic-gate (*state)++; /* skip the ',' */
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate p = *state;
1827c478bd9Sstevel@tonic-gate *state = slp_utf_strchr(*state, ',');
1837c478bd9Sstevel@tonic-gate if (!*state)
1847c478bd9Sstevel@tonic-gate l = strlen(p);
1857c478bd9Sstevel@tonic-gate else {
1867c478bd9Sstevel@tonic-gate l = *state - p;
1877c478bd9Sstevel@tonic-gate l = (l > 32 ? 32 : l);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate (void) strncpy(buf, p, l);
1907c478bd9Sstevel@tonic-gate buf[l] = 0;
1917c478bd9Sstevel@tonic-gate answer = atoi(buf);
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate return (answer != 0 ? answer : 2000);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
slp_get_maxResults()1967c478bd9Sstevel@tonic-gate int slp_get_maxResults() {
1977c478bd9Sstevel@tonic-gate int num = atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS));
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate return (num <= 0 ? -1 : num);
2007c478bd9Sstevel@tonic-gate }
201