/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1996,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
static struct servent *
parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
struct pvt *pvt = (struct pvt *)this->private;
char *p, *cp, **cpp, **new;
int proto_len;
int num = 0;
int max = 0;
for (cpp = hes_list; *cpp; cpp++) {
cp = *cpp;
/* Strip away comments, if any. */
if ((p = strchr(cp, '#')))
*p = 0;
/* Check to make sure the protocol matches. */
p = cp;
while (*p && !isspace((unsigned char)*p))
p++;
if (!*p)
continue;
if (proto) {
proto_len = strlen(proto);
if (strncasecmp(++p, proto, proto_len) != 0)
continue;
if (p[proto_len] && !isspace(p[proto_len]&0xff))
continue;
}
/* OK, we've got a live one. Let's parse it for real. */
if (pvt->svbuf)
free(pvt->svbuf);
pvt->svbuf = strdup(cp);
p = pvt->svbuf;
pvt->serv.s_name = p;
while (*p && !isspace(*p&0xff))
p++;
if (!*p)
continue;
*p++ = '\0';
pvt->serv.s_proto = p;
while (*p && !isspace(*p&0xff))
p++;
if (!*p)
continue;
*p++ = '\0';
pvt->serv.s_port = htons((u_short) atoi(p));
while (*p && !isspace(*p&0xff))
p++;
if (*p)
*p++ = '\0';
while (*p) {
if ((num + 1) >= max || !pvt->serv.s_aliases) {
max += 10;
new = realloc(pvt->serv.s_aliases,
max * sizeof(char *));
if (!new) {
errno = ENOMEM;
goto cleanup;
}
pvt->serv.s_aliases = new;
}
pvt->serv.s_aliases[num++] = p;
while (*p && !isspace(*p&0xff))
p++;
if (*p)
*p++ = '\0';
}
if (!pvt->serv.s_aliases)
pvt->serv.s_aliases = malloc(sizeof(char *));
if (!pvt->serv.s_aliases)
goto cleanup;
pvt->serv.s_aliases[num] = NULL;
return (&pvt->serv);
}
cleanup:
if (pvt->serv.s_aliases) {
free(pvt->serv.s_aliases);
pvt->serv.s_aliases = NULL;
}
if (pvt->svbuf) {
free(pvt->svbuf);
pvt->svbuf = NULL;
}
return (NULL);
}