17c478bd9Sstevel@tonic-gate /* 2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 37c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1996 by Internet Software Consortium. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 87c478bd9Sstevel@tonic-gate * 9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: irp_pr.c,v 1.3 2005/04/27 04:56:29 sra Exp $"; 207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* extern */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include "port_before.h" 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <syslog.h> 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <errno.h> 307c478bd9Sstevel@tonic-gate #include <fcntl.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <netdb.h> 357c478bd9Sstevel@tonic-gate #include <syslog.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include <irs.h> 387c478bd9Sstevel@tonic-gate #include <irp.h> 397c478bd9Sstevel@tonic-gate #include <isc/memcluster.h> 407c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include "irs_p.h" 437c478bd9Sstevel@tonic-gate #include "lcl_p.h" 447c478bd9Sstevel@tonic-gate #include "irp_p.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include "port_after.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define MAXALIASES 35 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* Types */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate struct pvt { 547c478bd9Sstevel@tonic-gate struct irp_p *girpdata; 557c478bd9Sstevel@tonic-gate int warned; 567c478bd9Sstevel@tonic-gate struct protoent proto; 577c478bd9Sstevel@tonic-gate }; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* Forward */ 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static void pr_close(struct irs_pr *); 627c478bd9Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *); 637c478bd9Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *); 647c478bd9Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int); 657c478bd9Sstevel@tonic-gate static void pr_rewind(struct irs_pr *); 667c478bd9Sstevel@tonic-gate static void pr_minimize(struct irs_pr *); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static void free_proto(struct protoent *pr); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* Public */ 717c478bd9Sstevel@tonic-gate 72*9525b14bSRao Shoaib /*% 737c478bd9Sstevel@tonic-gate * struct irs_pr * irs_irp_pr(struct irs_acc *this) 747c478bd9Sstevel@tonic-gate * 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate struct irs_pr * 787c478bd9Sstevel@tonic-gate irs_irp_pr(struct irs_acc *this) { 797c478bd9Sstevel@tonic-gate struct irs_pr *pr; 807c478bd9Sstevel@tonic-gate struct pvt *pvt; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) { 837c478bd9Sstevel@tonic-gate errno = ENOMEM; 847c478bd9Sstevel@tonic-gate return (NULL); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate memset(pr, 0x0, sizeof *pr); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 897c478bd9Sstevel@tonic-gate memput(pr, sizeof *pr); 907c478bd9Sstevel@tonic-gate errno = ENOMEM; 917c478bd9Sstevel@tonic-gate return (NULL); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 947c478bd9Sstevel@tonic-gate pvt->girpdata = this->private; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate pr->private = pvt; 977c478bd9Sstevel@tonic-gate pr->close = pr_close; 987c478bd9Sstevel@tonic-gate pr->byname = pr_byname; 997c478bd9Sstevel@tonic-gate pr->bynumber = pr_bynumber; 1007c478bd9Sstevel@tonic-gate pr->next = pr_next; 1017c478bd9Sstevel@tonic-gate pr->rewind = pr_rewind; 1027c478bd9Sstevel@tonic-gate pr->minimize = pr_minimize; 1037c478bd9Sstevel@tonic-gate return (pr); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* Methods */ 1077c478bd9Sstevel@tonic-gate 108*9525b14bSRao Shoaib /*% 1097c478bd9Sstevel@tonic-gate * void pr_close(struct irs_pr *this) 1107c478bd9Sstevel@tonic-gate * 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static void 1147c478bd9Sstevel@tonic-gate pr_close(struct irs_pr *this) { 1157c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate pr_minimize(this); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate free_proto(&pvt->proto); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1227c478bd9Sstevel@tonic-gate memput(this, sizeof *this); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 125*9525b14bSRao Shoaib /*% 1267c478bd9Sstevel@tonic-gate * struct protoent * pr_byname(struct irs_pr *this, const char *name) 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate static struct protoent * 1317c478bd9Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) { 1327c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1337c478bd9Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 1347c478bd9Sstevel@tonic-gate char *body = NULL; 1357c478bd9Sstevel@tonic-gate size_t bodylen; 1367c478bd9Sstevel@tonic-gate int code; 1377c478bd9Sstevel@tonic-gate int i; 1387c478bd9Sstevel@tonic-gate char text[256]; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) { 1417c478bd9Sstevel@tonic-gate return (pr); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 1457c478bd9Sstevel@tonic-gate return (NULL); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name); 1497c478bd9Sstevel@tonic-gate if (i != 0) 1507c478bd9Sstevel@tonic-gate return (NULL); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 1537c478bd9Sstevel@tonic-gate text, sizeof text, 1547c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) { 1557c478bd9Sstevel@tonic-gate return (NULL); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 1597c478bd9Sstevel@tonic-gate free_proto(pr); 1607c478bd9Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 1617c478bd9Sstevel@tonic-gate pr = NULL; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate } else { 1647c478bd9Sstevel@tonic-gate pr = NULL; 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if (body != NULL) { 1687c478bd9Sstevel@tonic-gate memput(body, bodylen); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate return (pr); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 174*9525b14bSRao Shoaib /*% 1757c478bd9Sstevel@tonic-gate * struct protoent * pr_bynumber(struct irs_pr *this, int proto) 1767c478bd9Sstevel@tonic-gate * 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate static struct protoent * 1807c478bd9Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) { 1817c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1827c478bd9Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 1837c478bd9Sstevel@tonic-gate char *body = NULL; 1847c478bd9Sstevel@tonic-gate size_t bodylen; 1857c478bd9Sstevel@tonic-gate int code; 1867c478bd9Sstevel@tonic-gate int i; 1877c478bd9Sstevel@tonic-gate char text[256]; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if (pr->p_name != NULL && proto == pr->p_proto) { 1907c478bd9Sstevel@tonic-gate return (pr); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 1947c478bd9Sstevel@tonic-gate return (NULL); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto); 1987c478bd9Sstevel@tonic-gate if (i != 0) 1997c478bd9Sstevel@tonic-gate return (NULL); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 2027c478bd9Sstevel@tonic-gate text, sizeof text, 2037c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) { 2047c478bd9Sstevel@tonic-gate return (NULL); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 2087c478bd9Sstevel@tonic-gate free_proto(pr); 2097c478bd9Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 2107c478bd9Sstevel@tonic-gate pr = NULL; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate } else { 2137c478bd9Sstevel@tonic-gate pr = NULL; 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (body != NULL) { 2177c478bd9Sstevel@tonic-gate memput(body, bodylen); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate return (pr); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 223*9525b14bSRao Shoaib /*% 2247c478bd9Sstevel@tonic-gate * void pr_rewind(struct irs_pr *this) 2257c478bd9Sstevel@tonic-gate * 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static void 2297c478bd9Sstevel@tonic-gate pr_rewind(struct irs_pr *this) { 2307c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2317c478bd9Sstevel@tonic-gate char text[256]; 2327c478bd9Sstevel@tonic-gate int code; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 2357c478bd9Sstevel@tonic-gate return; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) { 2397c478bd9Sstevel@tonic-gate return; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate code = irs_irp_read_response(pvt->girpdata, text, sizeof text); 2437c478bd9Sstevel@tonic-gate if (code != IRPD_GETPROTO_SETOK) { 2447c478bd9Sstevel@tonic-gate if (irp_log_errors) { 2457c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "setprotoent failed: %s", text); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate return; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 252*9525b14bSRao Shoaib /*% 2537c478bd9Sstevel@tonic-gate * Prepares the cache if necessary and returns the next item in it. 2547c478bd9Sstevel@tonic-gate * 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate static struct protoent * 2587c478bd9Sstevel@tonic-gate pr_next(struct irs_pr *this) { 2597c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2607c478bd9Sstevel@tonic-gate struct protoent *pr = &pvt->proto; 2617c478bd9Sstevel@tonic-gate char *body; 2627c478bd9Sstevel@tonic-gate size_t bodylen; 2637c478bd9Sstevel@tonic-gate int code; 2647c478bd9Sstevel@tonic-gate char text[256]; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 2677c478bd9Sstevel@tonic-gate return (NULL); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) { 2717c478bd9Sstevel@tonic-gate return (NULL); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code, 2757c478bd9Sstevel@tonic-gate text, sizeof text, 2767c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) { 2777c478bd9Sstevel@tonic-gate return (NULL); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (code == IRPD_GETPROTO_OK) { 2817c478bd9Sstevel@tonic-gate free_proto(pr); 2827c478bd9Sstevel@tonic-gate if (irp_unmarshall_pr(pr, body) != 0) { 2837c478bd9Sstevel@tonic-gate pr = NULL; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate } else { 2867c478bd9Sstevel@tonic-gate pr = NULL; 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate if (body != NULL) { 2907c478bd9Sstevel@tonic-gate memput(body, bodylen); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate return (pr); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 296*9525b14bSRao Shoaib /*% 2977c478bd9Sstevel@tonic-gate * void pr_minimize(struct irs_pr *this) 2987c478bd9Sstevel@tonic-gate * 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static void 3027c478bd9Sstevel@tonic-gate pr_minimize(struct irs_pr *this) { 3037c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate irs_irp_disconnect(pvt->girpdata); 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 308*9525b14bSRao Shoaib /*% 3097c478bd9Sstevel@tonic-gate * Deallocate all the memory irp_unmarshall_pr allocated. 3107c478bd9Sstevel@tonic-gate * 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate static void 3147c478bd9Sstevel@tonic-gate free_proto(struct protoent *pr) { 3157c478bd9Sstevel@tonic-gate char **p; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (pr == NULL) 3187c478bd9Sstevel@tonic-gate return; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if (pr->p_name != NULL) 3217c478bd9Sstevel@tonic-gate free(pr->p_name); 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++) 3247c478bd9Sstevel@tonic-gate free(*p); 3257c478bd9Sstevel@tonic-gate } 326*9525b14bSRao Shoaib 327*9525b14bSRao Shoaib /*! \file */ 328