10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 30Sstevel@tonic-gate * Portions Copyright (c) 1996 by Internet Software Consortium. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irp_pr.c,v 1.3 2005/04/27 04:56:29 sra Exp $"; 200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 210Sstevel@tonic-gate 220Sstevel@tonic-gate /* extern */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate #include "port_before.h" 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <syslog.h> 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <errno.h> 300Sstevel@tonic-gate #include <fcntl.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate #include <stdio.h> 330Sstevel@tonic-gate #include <stdlib.h> 340Sstevel@tonic-gate #include <netdb.h> 350Sstevel@tonic-gate #include <syslog.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <irs.h> 380Sstevel@tonic-gate #include <irp.h> 390Sstevel@tonic-gate #include <isc/memcluster.h> 400Sstevel@tonic-gate #include <isc/irpmarshall.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include "irs_p.h" 430Sstevel@tonic-gate #include "lcl_p.h" 440Sstevel@tonic-gate #include "irp_p.h" 450Sstevel@tonic-gate 460Sstevel@tonic-gate #include "port_after.h" 470Sstevel@tonic-gate 480Sstevel@tonic-gate 490Sstevel@tonic-gate #define MAXALIASES 35 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* Types */ 520Sstevel@tonic-gate 530Sstevel@tonic-gate struct pvt { 540Sstevel@tonic-gate struct irp_p *girpdata; 550Sstevel@tonic-gate int warned; 560Sstevel@tonic-gate struct protoent proto; 570Sstevel@tonic-gate }; 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* Forward */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate static void pr_close(struct irs_pr *); 620Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *); 630Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *); 640Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int); 650Sstevel@tonic-gate static void pr_rewind(struct irs_pr *); 660Sstevel@tonic-gate static void pr_minimize(struct irs_pr *); 670Sstevel@tonic-gate 680Sstevel@tonic-gate static void free_proto(struct protoent *pr); 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* Public */ 710Sstevel@tonic-gate 72*11038SRao.Shoaib@Sun.COM /*% 730Sstevel@tonic-gate * struct irs_pr * irs_irp_pr(struct irs_acc *this) 740Sstevel@tonic-gate * 750Sstevel@tonic-gate */ 760Sstevel@tonic-gate 770Sstevel@tonic-gate struct irs_pr * 780Sstevel@tonic-gate irs_irp_pr(struct irs_acc *this) { 790Sstevel@tonic-gate struct irs_pr *pr; 800Sstevel@tonic-gate struct pvt *pvt; 810Sstevel@tonic-gate 820Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) { 830Sstevel@tonic-gate errno = ENOMEM; 840Sstevel@tonic-gate return (NULL); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate memset(pr, 0x0, sizeof *pr); 870Sstevel@tonic-gate 880Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 890Sstevel@tonic-gate memput(pr, sizeof *pr); 900Sstevel@tonic-gate errno = ENOMEM; 910Sstevel@tonic-gate return (NULL); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 940Sstevel@tonic-gate pvt->girpdata = this->private; 950Sstevel@tonic-gate 960Sstevel@tonic-gate pr->private = pvt; 970Sstevel@tonic-gate pr->close = pr_close; 980Sstevel@tonic-gate pr->byname = pr_byname; 990Sstevel@tonic-gate pr->bynumber = pr_bynumber; 1000Sstevel@tonic-gate pr->next = pr_next; 1010Sstevel@tonic-gate pr->rewind = pr_rewind; 1020Sstevel@tonic-gate pr->minimize = pr_minimize; 1030Sstevel@tonic-gate return (pr); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* Methods */ 1070Sstevel@tonic-gate 108*11038SRao.Shoaib@Sun.COM /*% 1090Sstevel@tonic-gate * void pr_close(struct irs_pr *this) 1100Sstevel@tonic-gate * 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate static void 1140Sstevel@tonic-gate pr_close(struct irs_pr *this) { 1150Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate pr_minimize(this); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate free_proto(&pvt->proto); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1220Sstevel@tonic-gate memput(this, sizeof *this); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 125*11038SRao.Shoaib@Sun.COM /*% 1260Sstevel@tonic-gate * struct protoent * pr_byname(struct irs_pr *this, const char *name) 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate static struct protoent * 1310Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) { 1320Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1330Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 1340Sstevel@tonic-gate char *body = NULL; 1350Sstevel@tonic-gate size_t bodylen; 1360Sstevel@tonic-gate int code; 1370Sstevel@tonic-gate int i; 1380Sstevel@tonic-gate char text[256]; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) { 1410Sstevel@tonic-gate return (pr); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 1450Sstevel@tonic-gate return (NULL); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name); 1490Sstevel@tonic-gate if (i != 0) 1500Sstevel@tonic-gate return (NULL); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 1530Sstevel@tonic-gate text, sizeof text, 1540Sstevel@tonic-gate &body, &bodylen) != 0) { 1550Sstevel@tonic-gate return (NULL); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 1590Sstevel@tonic-gate free_proto(pr); 1600Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 1610Sstevel@tonic-gate pr = NULL; 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate } else { 1640Sstevel@tonic-gate pr = NULL; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (body != NULL) { 1680Sstevel@tonic-gate memput(body, bodylen); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate return (pr); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 174*11038SRao.Shoaib@Sun.COM /*% 1750Sstevel@tonic-gate * struct protoent * pr_bynumber(struct irs_pr *this, int proto) 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate static struct protoent * 1800Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) { 1810Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1820Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 1830Sstevel@tonic-gate char *body = NULL; 1840Sstevel@tonic-gate size_t bodylen; 1850Sstevel@tonic-gate int code; 1860Sstevel@tonic-gate int i; 1870Sstevel@tonic-gate char text[256]; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (pr->p_name != NULL && proto == pr->p_proto) { 1900Sstevel@tonic-gate return (pr); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 1940Sstevel@tonic-gate return (NULL); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto); 1980Sstevel@tonic-gate if (i != 0) 1990Sstevel@tonic-gate return (NULL); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 2020Sstevel@tonic-gate text, sizeof text, 2030Sstevel@tonic-gate &body, &bodylen) != 0) { 2040Sstevel@tonic-gate return (NULL); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 2080Sstevel@tonic-gate free_proto(pr); 2090Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 2100Sstevel@tonic-gate pr = NULL; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate } else { 2130Sstevel@tonic-gate pr = NULL; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (body != NULL) { 2170Sstevel@tonic-gate memput(body, bodylen); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate return (pr); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 223*11038SRao.Shoaib@Sun.COM /*% 2240Sstevel@tonic-gate * void pr_rewind(struct irs_pr *this) 2250Sstevel@tonic-gate * 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate static void 2290Sstevel@tonic-gate pr_rewind(struct irs_pr *this) { 2300Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2310Sstevel@tonic-gate char text[256]; 2320Sstevel@tonic-gate int code; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 2350Sstevel@tonic-gate return; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) { 2390Sstevel@tonic-gate return; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate code = irs_irp_read_response(pvt->girpdata, text, sizeof text); 2430Sstevel@tonic-gate if (code != IRPD_GETPROTO_SETOK) { 2440Sstevel@tonic-gate if (irp_log_errors) { 2450Sstevel@tonic-gate syslog(LOG_WARNING, "setprotoent failed: %s", text); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate return; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 252*11038SRao.Shoaib@Sun.COM /*% 2530Sstevel@tonic-gate * Prepares the cache if necessary and returns the next item in it. 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate static struct protoent * 2580Sstevel@tonic-gate pr_next(struct irs_pr *this) { 2590Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2600Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 2610Sstevel@tonic-gate char *body; 2620Sstevel@tonic-gate size_t bodylen; 2630Sstevel@tonic-gate int code; 2640Sstevel@tonic-gate char text[256]; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 2670Sstevel@tonic-gate return (NULL); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) { 2710Sstevel@tonic-gate return (NULL); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 2750Sstevel@tonic-gate text, sizeof text, 2760Sstevel@tonic-gate &body, &bodylen) != 0) { 2770Sstevel@tonic-gate return (NULL); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 2810Sstevel@tonic-gate free_proto(pr); 2820Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 2830Sstevel@tonic-gate pr = NULL; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate } else { 2860Sstevel@tonic-gate pr = NULL; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate if (body != NULL) { 2900Sstevel@tonic-gate memput(body, bodylen); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate return (pr); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 296*11038SRao.Shoaib@Sun.COM /*% 2970Sstevel@tonic-gate * void pr_minimize(struct irs_pr *this) 2980Sstevel@tonic-gate * 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate static void 3020Sstevel@tonic-gate pr_minimize(struct irs_pr *this) { 3030Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate irs_irp_disconnect(pvt->girpdata); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 308*11038SRao.Shoaib@Sun.COM /*% 3090Sstevel@tonic-gate * Deallocate all the memory irp_unmarshall_pr allocated. 3100Sstevel@tonic-gate * 3110Sstevel@tonic-gate */ 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate static void 3140Sstevel@tonic-gate free_proto(struct protoent *pr) { 3150Sstevel@tonic-gate char **p; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate if (pr == NULL) 3180Sstevel@tonic-gate return; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if (pr->p_name != NULL) 3210Sstevel@tonic-gate free(pr->p_name); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++) 3240Sstevel@tonic-gate free(*p); 3250Sstevel@tonic-gate } 326*11038SRao.Shoaib@Sun.COM 327*11038SRao.Shoaib@Sun.COM /*! \file */ 328