2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Portions Copyright (c) 1996 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic const char rcsid[] = "$Id: irp_pr.c,v 1.3 2005/04/27 04:56:29 sra Exp $";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A/* extern */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <syslog.h>
2N/A#include <sys/types.h>
2N/A
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <netdb.h>
2N/A#include <syslog.h>
2N/A
2N/A#include <irs.h>
2N/A#include <irp.h>
2N/A#include <isc/memcluster.h>
2N/A#include <isc/irpmarshall.h>
2N/A
2N/A#include "irs_p.h"
2N/A#include "lcl_p.h"
2N/A#include "irp_p.h"
2N/A
2N/A#include "port_after.h"
2N/A
2N/A
2N/A#define MAXALIASES 35
2N/A
2N/A/* Types */
2N/A
2N/Astruct pvt {
2N/A struct irp_p *girpdata;
2N/A int warned;
2N/A struct protoent proto;
2N/A};
2N/A
2N/A/* Forward */
2N/A
2N/Astatic void pr_close(struct irs_pr *);
2N/Astatic struct protoent * pr_next(struct irs_pr *);
2N/Astatic struct protoent * pr_byname(struct irs_pr *, const char *);
2N/Astatic struct protoent * pr_bynumber(struct irs_pr *, int);
2N/Astatic void pr_rewind(struct irs_pr *);
2N/Astatic void pr_minimize(struct irs_pr *);
2N/A
2N/Astatic void free_proto(struct protoent *pr);
2N/A
2N/A/* Public */
2N/A
2N/A/*%
2N/A * struct irs_pr * irs_irp_pr(struct irs_acc *this)
2N/A *
2N/A */
2N/A
2N/Astruct irs_pr *
2N/Airs_irp_pr(struct irs_acc *this) {
2N/A struct irs_pr *pr;
2N/A struct pvt *pvt;
2N/A
2N/A if (!(pr = memget(sizeof *pr))) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(pr, 0x0, sizeof *pr);
2N/A
2N/A if (!(pvt = memget(sizeof *pvt))) {
2N/A memput(pr, sizeof *pr);
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(pvt, 0, sizeof *pvt);
2N/A pvt->girpdata = this->private;
2N/A
2N/A pr->private = pvt;
2N/A pr->close = pr_close;
2N/A pr->byname = pr_byname;
2N/A pr->bynumber = pr_bynumber;
2N/A pr->next = pr_next;
2N/A pr->rewind = pr_rewind;
2N/A pr->minimize = pr_minimize;
2N/A return (pr);
2N/A}
2N/A
2N/A/* Methods */
2N/A
2N/A/*%
2N/A * void pr_close(struct irs_pr *this)
2N/A *
2N/A */
2N/A
2N/Astatic void
2N/Apr_close(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A pr_minimize(this);
2N/A
2N/A free_proto(&pvt->proto);
2N/A
2N/A memput(pvt, sizeof *pvt);
2N/A memput(this, sizeof *this);
2N/A}
2N/A
2N/A/*%
2N/A * struct protoent * pr_byname(struct irs_pr *this, const char *name)
2N/A *
2N/A */
2N/A
2N/Astatic struct protoent *
2N/Apr_byname(struct irs_pr *this, const char *name) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct protoent *pr = &pvt->proto;
2N/A char *body = NULL;
2N/A size_t bodylen;
2N/A int code;
2N/A int i;
2N/A char text[256];
2N/A
2N/A if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) {
2N/A return (pr);
2N/A }
2N/A
2N/A if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name);
2N/A if (i != 0)
2N/A return (NULL);
2N/A
2N/A if (irs_irp_get_full_response(pvt->girpdata, &code,
2N/A text, sizeof text,
2N/A &body, &bodylen) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (code == IRPD_GETPROTO_OK) {
2N/A free_proto(pr);
2N/A if (irp_unmarshall_pr(pr, body) != 0) {
2N/A pr = NULL;
2N/A }
2N/A } else {
2N/A pr = NULL;
2N/A }
2N/A
2N/A if (body != NULL) {
2N/A memput(body, bodylen);
2N/A }
2N/A
2N/A return (pr);
2N/A}
2N/A
2N/A/*%
2N/A * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
2N/A *
2N/A */
2N/A
2N/Astatic struct protoent *
2N/Apr_bynumber(struct irs_pr *this, int proto) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct protoent *pr = &pvt->proto;
2N/A char *body = NULL;
2N/A size_t bodylen;
2N/A int code;
2N/A int i;
2N/A char text[256];
2N/A
2N/A if (pr->p_name != NULL && proto == pr->p_proto) {
2N/A return (pr);
2N/A }
2N/A
2N/A if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto);
2N/A if (i != 0)
2N/A return (NULL);
2N/A
2N/A if (irs_irp_get_full_response(pvt->girpdata, &code,
2N/A text, sizeof text,
2N/A &body, &bodylen) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (code == IRPD_GETPROTO_OK) {
2N/A free_proto(pr);
2N/A if (irp_unmarshall_pr(pr, body) != 0) {
2N/A pr = NULL;
2N/A }
2N/A } else {
2N/A pr = NULL;
2N/A }
2N/A
2N/A if (body != NULL) {
2N/A memput(body, bodylen);
2N/A }
2N/A
2N/A return (pr);
2N/A}
2N/A
2N/A/*%
2N/A * void pr_rewind(struct irs_pr *this)
2N/A *
2N/A */
2N/A
2N/Astatic void
2N/Apr_rewind(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A char text[256];
2N/A int code;
2N/A
2N/A if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2N/A return;
2N/A }
2N/A
2N/A if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) {
2N/A return;
2N/A }
2N/A
2N/A code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2N/A if (code != IRPD_GETPROTO_SETOK) {
2N/A if (irp_log_errors) {
2N/A syslog(LOG_WARNING, "setprotoent failed: %s", text);
2N/A }
2N/A }
2N/A
2N/A return;
2N/A}
2N/A
2N/A/*%
2N/A * Prepares the cache if necessary and returns the next item in it.
2N/A *
2N/A */
2N/A
2N/Astatic struct protoent *
2N/Apr_next(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct protoent *pr = &pvt->proto;
2N/A char *body;
2N/A size_t bodylen;
2N/A int code;
2N/A char text[256];
2N/A
2N/A if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (irs_irp_get_full_response(pvt->girpdata, &code,
2N/A text, sizeof text,
2N/A &body, &bodylen) != 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (code == IRPD_GETPROTO_OK) {
2N/A free_proto(pr);
2N/A if (irp_unmarshall_pr(pr, body) != 0) {
2N/A pr = NULL;
2N/A }
2N/A } else {
2N/A pr = NULL;
2N/A }
2N/A
2N/A if (body != NULL) {
2N/A memput(body, bodylen);
2N/A }
2N/A
2N/A return (pr);
2N/A}
2N/A
2N/A/*%
2N/A * void pr_minimize(struct irs_pr *this)
2N/A *
2N/A */
2N/A
2N/Astatic void
2N/Apr_minimize(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A irs_irp_disconnect(pvt->girpdata);
2N/A}
2N/A
2N/A/*%
2N/A * Deallocate all the memory irp_unmarshall_pr allocated.
2N/A *
2N/A */
2N/A
2N/Astatic void
2N/Afree_proto(struct protoent *pr) {
2N/A char **p;
2N/A
2N/A if (pr == NULL)
2N/A return;
2N/A
2N/A if (pr->p_name != NULL)
2N/A free(pr->p_name);
2N/A
2N/A for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++)
2N/A free(*p);
2N/A}
2N/A
2N/A/*! \file */