10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Portions Copyright (c) 1996,1998 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_nw.c,v 1.4 2006/03/09 23:57:56 marka Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate
220Sstevel@tonic-gate #if 0
230Sstevel@tonic-gate
240Sstevel@tonic-gate #endif
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Imports */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include "port_before.h"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <syslog.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <arpa/inet.h>
360Sstevel@tonic-gate #include <arpa/nameser.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <resolv.h>
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <irs.h>
470Sstevel@tonic-gate #include <irp.h>
480Sstevel@tonic-gate #include <isc/irpmarshall.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <isc/memcluster.h>
510Sstevel@tonic-gate #include <isc/misc.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate #include "irs_p.h"
540Sstevel@tonic-gate #include "lcl_p.h"
550Sstevel@tonic-gate #include "irp_p.h"
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include "port_after.h"
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define MAXALIASES 35
600Sstevel@tonic-gate #define MAXADDRSIZE 4
610Sstevel@tonic-gate
620Sstevel@tonic-gate struct pvt {
630Sstevel@tonic-gate struct irp_p *girpdata;
640Sstevel@tonic-gate int warned;
650Sstevel@tonic-gate struct nwent net;
660Sstevel@tonic-gate };
670Sstevel@tonic-gate
680Sstevel@tonic-gate /* Forward */
690Sstevel@tonic-gate
700Sstevel@tonic-gate static void nw_close(struct irs_nw *);
710Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int);
720Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
730Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *);
740Sstevel@tonic-gate static void nw_rewind(struct irs_nw *);
750Sstevel@tonic-gate static void nw_minimize(struct irs_nw *);
760Sstevel@tonic-gate
770Sstevel@tonic-gate static void free_nw(struct nwent *nw);
780Sstevel@tonic-gate
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* Public */
810Sstevel@tonic-gate
82*11038SRao.Shoaib@Sun.COM /*%
830Sstevel@tonic-gate * struct irs_nw * irs_irp_nw(struct irs_acc *this)
840Sstevel@tonic-gate *
850Sstevel@tonic-gate */
860Sstevel@tonic-gate
870Sstevel@tonic-gate struct irs_nw *
irs_irp_nw(struct irs_acc * this)880Sstevel@tonic-gate irs_irp_nw(struct irs_acc *this) {
890Sstevel@tonic-gate struct irs_nw *nw;
900Sstevel@tonic-gate struct pvt *pvt;
910Sstevel@tonic-gate
920Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
930Sstevel@tonic-gate errno = ENOMEM;
940Sstevel@tonic-gate return (NULL);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) {
990Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1000Sstevel@tonic-gate errno = ENOMEM;
1010Sstevel@tonic-gate return (NULL);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate memset(nw, 0x0, sizeof *nw);
1040Sstevel@tonic-gate pvt->girpdata = this->private;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate nw->private = pvt;
1070Sstevel@tonic-gate nw->close = nw_close;
1080Sstevel@tonic-gate nw->byname = nw_byname;
1090Sstevel@tonic-gate nw->byaddr = nw_byaddr;
1100Sstevel@tonic-gate nw->next = nw_next;
1110Sstevel@tonic-gate nw->rewind = nw_rewind;
1120Sstevel@tonic-gate nw->minimize = nw_minimize;
1130Sstevel@tonic-gate return (nw);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /* Methods */
1170Sstevel@tonic-gate
118*11038SRao.Shoaib@Sun.COM /*%
1190Sstevel@tonic-gate * void nw_close(struct irs_nw *this)
1200Sstevel@tonic-gate *
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static void
nw_close(struct irs_nw * this)1240Sstevel@tonic-gate nw_close(struct irs_nw *this) {
1250Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate nw_minimize(this);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate free_nw(&pvt->net);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1320Sstevel@tonic-gate memput(this, sizeof *this);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
135*11038SRao.Shoaib@Sun.COM /*%
1360Sstevel@tonic-gate * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
1370Sstevel@tonic-gate * int length, int type)
1380Sstevel@tonic-gate *
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static struct nwent *
nw_byaddr(struct irs_nw * this,void * net,int length,int type)1420Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
1430Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1440Sstevel@tonic-gate struct nwent *nw = &pvt->net;
1450Sstevel@tonic-gate char *body = NULL;
1460Sstevel@tonic-gate size_t bodylen;
1470Sstevel@tonic-gate int code;
148*11038SRao.Shoaib@Sun.COM char paddr[24]; /*%< bigenough for ip4 w/ cidr spec. */
1490Sstevel@tonic-gate char text[256];
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) {
1520Sstevel@tonic-gate return (NULL);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1560Sstevel@tonic-gate return (NULL);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s",
1600Sstevel@tonic-gate paddr, ADDR_T_STR(type)) != 0)
1610Sstevel@tonic-gate return (NULL);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
1640Sstevel@tonic-gate text, sizeof text,
1650Sstevel@tonic-gate &body, &bodylen) != 0) {
1660Sstevel@tonic-gate return (NULL);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (code == IRPD_GETNET_OK) {
1700Sstevel@tonic-gate free_nw(nw);
1710Sstevel@tonic-gate if (irp_unmarshall_nw(nw, body) != 0) {
1720Sstevel@tonic-gate nw = NULL;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate } else {
1750Sstevel@tonic-gate nw = NULL;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if (body != NULL) {
1790Sstevel@tonic-gate memput(body, bodylen);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate return (nw);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
185*11038SRao.Shoaib@Sun.COM /*%
1860Sstevel@tonic-gate * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
1870Sstevel@tonic-gate *
1880Sstevel@tonic-gate */
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate static struct nwent *
nw_byname(struct irs_nw * this,const char * name,int type)1910Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) {
1920Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1930Sstevel@tonic-gate struct nwent *nw = &pvt->net;
1940Sstevel@tonic-gate char *body = NULL;
1950Sstevel@tonic-gate size_t bodylen;
1960Sstevel@tonic-gate int code;
1970Sstevel@tonic-gate char text[256];
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate if (nw->n_name != NULL &&
2000Sstevel@tonic-gate strcmp(name, nw->n_name) == 0 &&
2010Sstevel@tonic-gate nw->n_addrtype == type) {
2020Sstevel@tonic-gate return (nw);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2060Sstevel@tonic-gate return (NULL);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0)
2100Sstevel@tonic-gate return (NULL);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
2130Sstevel@tonic-gate text, sizeof text,
2140Sstevel@tonic-gate &body, &bodylen) != 0) {
2150Sstevel@tonic-gate return (NULL);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (code == IRPD_GETNET_OK) {
2190Sstevel@tonic-gate free_nw(nw);
2200Sstevel@tonic-gate if (irp_unmarshall_nw(nw, body) != 0) {
2210Sstevel@tonic-gate nw = NULL;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate } else {
2240Sstevel@tonic-gate nw = NULL;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (body != NULL) {
2280Sstevel@tonic-gate memput(body, bodylen);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate return (nw);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
234*11038SRao.Shoaib@Sun.COM /*%
2350Sstevel@tonic-gate * void nw_rewind(struct irs_nw *this)
2360Sstevel@tonic-gate *
2370Sstevel@tonic-gate */
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate static void
nw_rewind(struct irs_nw * this)2400Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
2410Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2420Sstevel@tonic-gate char text[256];
2430Sstevel@tonic-gate int code;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2460Sstevel@tonic-gate return;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "setnetent") != 0) {
2500Sstevel@tonic-gate return;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2540Sstevel@tonic-gate if (code != IRPD_GETNET_SETOK) {
2550Sstevel@tonic-gate if (irp_log_errors) {
2560Sstevel@tonic-gate syslog(LOG_WARNING, "setnetent failed: %s", text);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate return;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
263*11038SRao.Shoaib@Sun.COM /*%
2640Sstevel@tonic-gate * Prepares the cache if necessary and returns the first, or
2650Sstevel@tonic-gate * next item from it.
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate static struct nwent *
nw_next(struct irs_nw * this)2690Sstevel@tonic-gate nw_next(struct irs_nw *this) {
2700Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2710Sstevel@tonic-gate struct nwent *nw = &pvt->net;
2720Sstevel@tonic-gate char *body;
2730Sstevel@tonic-gate size_t bodylen;
2740Sstevel@tonic-gate int code;
2750Sstevel@tonic-gate char text[256];
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2780Sstevel@tonic-gate return (NULL);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) {
2820Sstevel@tonic-gate return (NULL);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
2860Sstevel@tonic-gate text, sizeof text,
2870Sstevel@tonic-gate &body, &bodylen) != 0) {
2880Sstevel@tonic-gate return (NULL);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (code == IRPD_GETNET_OK) {
2920Sstevel@tonic-gate free_nw(nw);
2930Sstevel@tonic-gate if (irp_unmarshall_nw(nw, body) != 0) {
2940Sstevel@tonic-gate nw = NULL;
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate } else {
2970Sstevel@tonic-gate nw = NULL;
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
300*11038SRao.Shoaib@Sun.COM if (body != NULL)
301*11038SRao.Shoaib@Sun.COM memput(body, bodylen);
3020Sstevel@tonic-gate return (nw);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
305*11038SRao.Shoaib@Sun.COM /*%
3060Sstevel@tonic-gate * void nw_minimize(struct irs_nw *this)
3070Sstevel@tonic-gate *
3080Sstevel@tonic-gate */
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate static void
nw_minimize(struct irs_nw * this)3110Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
3120Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate irs_irp_disconnect(pvt->girpdata);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate /* private. */
3210Sstevel@tonic-gate
322*11038SRao.Shoaib@Sun.COM /*%
3230Sstevel@tonic-gate * deallocate all the memory irp_unmarshall_pw allocated.
3240Sstevel@tonic-gate *
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate static void
free_nw(struct nwent * nw)3280Sstevel@tonic-gate free_nw(struct nwent *nw) {
3290Sstevel@tonic-gate char **p;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (nw == NULL)
3320Sstevel@tonic-gate return;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate if (nw->n_name != NULL)
3350Sstevel@tonic-gate free(nw->n_name);
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate if (nw->n_aliases != NULL) {
3380Sstevel@tonic-gate for (p = nw->n_aliases ; *p != NULL ; p++) {
3390Sstevel@tonic-gate free(*p);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate free(nw->n_aliases);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (nw->n_addr != NULL)
3450Sstevel@tonic-gate free(nw->n_addr);
3460Sstevel@tonic-gate }
347*11038SRao.Shoaib@Sun.COM
348*11038SRao.Shoaib@Sun.COM /*! \file */
349