Home | History | Annotate | Line # | Download | only in irs
irp_nw.c revision 1.1.1.1.14.1
      1  1.1.1.1.14.1      yamt /*	$NetBSD: irp_nw.c,v 1.1.1.1.14.1 2012/10/30 18:55:28 yamt Exp $	*/
      2           1.1  christos 
      3           1.1  christos /*
      4           1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5           1.1  christos  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
      6           1.1  christos  *
      7           1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8           1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9           1.1  christos  * copyright notice and this permission notice appear in all copies.
     10           1.1  christos  *
     11           1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12           1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13           1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14           1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15           1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16           1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17           1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18           1.1  christos  */
     19           1.1  christos 
     20           1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.1.14.1      yamt static const char rcsid[] = "Id: irp_nw.c,v 1.4 2006/03/09 23:57:56 marka Exp ";
     22           1.1  christos #endif /* LIBC_SCCS and not lint */
     23           1.1  christos 
     24           1.1  christos #if 0
     25           1.1  christos 
     26           1.1  christos #endif
     27           1.1  christos 
     28           1.1  christos /* Imports */
     29           1.1  christos 
     30           1.1  christos #include "port_before.h"
     31           1.1  christos 
     32           1.1  christos #include <syslog.h>
     33           1.1  christos #include <sys/types.h>
     34           1.1  christos #include <sys/socket.h>
     35           1.1  christos 
     36           1.1  christos #include <netinet/in.h>
     37           1.1  christos #include <arpa/inet.h>
     38           1.1  christos #include <arpa/nameser.h>
     39           1.1  christos 
     40           1.1  christos #include <errno.h>
     41           1.1  christos #include <fcntl.h>
     42           1.1  christos #include <resolv.h>
     43           1.1  christos #include <stdio.h>
     44           1.1  christos #include <stdlib.h>
     45           1.1  christos #include <string.h>
     46           1.1  christos #include <syslog.h>
     47           1.1  christos 
     48           1.1  christos #include <irs.h>
     49           1.1  christos #include <irp.h>
     50           1.1  christos #include <isc/irpmarshall.h>
     51           1.1  christos 
     52           1.1  christos #include <isc/memcluster.h>
     53           1.1  christos #include <isc/misc.h>
     54           1.1  christos 
     55           1.1  christos #include "irs_p.h"
     56           1.1  christos #include "lcl_p.h"
     57           1.1  christos #include "irp_p.h"
     58           1.1  christos 
     59           1.1  christos #include "port_after.h"
     60           1.1  christos 
     61           1.1  christos #define MAXALIASES 35
     62           1.1  christos #define MAXADDRSIZE 4
     63           1.1  christos 
     64           1.1  christos struct pvt {
     65           1.1  christos 	struct irp_p	       *girpdata;
     66           1.1  christos 	int			warned;
     67           1.1  christos 	struct nwent		net;
     68           1.1  christos };
     69           1.1  christos 
     70           1.1  christos /* Forward */
     71           1.1  christos 
     72           1.1  christos static void		nw_close(struct irs_nw *);
     73           1.1  christos static struct nwent *	nw_byname(struct irs_nw *, const char *, int);
     74           1.1  christos static struct nwent *	nw_byaddr(struct irs_nw *, void *, int, int);
     75           1.1  christos static struct nwent *	nw_next(struct irs_nw *);
     76           1.1  christos static void		nw_rewind(struct irs_nw *);
     77           1.1  christos static void		nw_minimize(struct irs_nw *);
     78           1.1  christos 
     79           1.1  christos static void		free_nw(struct nwent *nw);
     80           1.1  christos 
     81           1.1  christos 
     82           1.1  christos /* Public */
     83           1.1  christos 
     84           1.1  christos /*%
     85           1.1  christos  * struct irs_nw * irs_irp_nw(struct irs_acc *this)
     86           1.1  christos  *
     87           1.1  christos  */
     88           1.1  christos 
     89           1.1  christos struct irs_nw *
     90           1.1  christos irs_irp_nw(struct irs_acc *this) {
     91           1.1  christos 	struct irs_nw *nw;
     92           1.1  christos 	struct pvt *pvt;
     93           1.1  christos 
     94           1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
     95           1.1  christos 		errno = ENOMEM;
     96           1.1  christos 		return (NULL);
     97           1.1  christos 	}
     98           1.1  christos 	memset(pvt, 0, sizeof *pvt);
     99           1.1  christos 
    100           1.1  christos 	if (!(nw = memget(sizeof *nw))) {
    101           1.1  christos 		memput(pvt, sizeof *pvt);
    102           1.1  christos 		errno = ENOMEM;
    103           1.1  christos 		return (NULL);
    104           1.1  christos 	}
    105           1.1  christos 	memset(nw, 0x0, sizeof *nw);
    106           1.1  christos 	pvt->girpdata = this->private;
    107           1.1  christos 
    108           1.1  christos 	nw->private = pvt;
    109           1.1  christos 	nw->close = nw_close;
    110           1.1  christos 	nw->byname = nw_byname;
    111           1.1  christos 	nw->byaddr = nw_byaddr;
    112           1.1  christos 	nw->next = nw_next;
    113           1.1  christos 	nw->rewind = nw_rewind;
    114           1.1  christos 	nw->minimize = nw_minimize;
    115           1.1  christos 	return (nw);
    116           1.1  christos }
    117           1.1  christos 
    118           1.1  christos /* Methods */
    119           1.1  christos 
    120           1.1  christos /*%
    121           1.1  christos  * void nw_close(struct irs_nw *this)
    122           1.1  christos  *
    123           1.1  christos  */
    124           1.1  christos 
    125           1.1  christos static void
    126           1.1  christos nw_close(struct irs_nw *this) {
    127           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    128           1.1  christos 
    129           1.1  christos 	nw_minimize(this);
    130           1.1  christos 
    131           1.1  christos 	free_nw(&pvt->net);
    132           1.1  christos 
    133           1.1  christos 	memput(pvt, sizeof *pvt);
    134           1.1  christos 	memput(this, sizeof *this);
    135           1.1  christos }
    136           1.1  christos 
    137           1.1  christos /*%
    138           1.1  christos  * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
    139           1.1  christos  * 				int length, int type)
    140           1.1  christos  *
    141           1.1  christos  */
    142           1.1  christos 
    143           1.1  christos static struct nwent *
    144           1.1  christos nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
    145           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    146           1.1  christos 	struct nwent *nw = &pvt->net;
    147           1.1  christos 	char *body = NULL;
    148           1.1  christos 	size_t bodylen;
    149           1.1  christos 	int code;
    150           1.1  christos 	char paddr[24];			/*%< bigenough for ip4 w/ cidr spec. */
    151           1.1  christos 	char text[256];
    152           1.1  christos 
    153           1.1  christos 	if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) {
    154           1.1  christos 		return (NULL);
    155           1.1  christos 	}
    156           1.1  christos 
    157           1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    158           1.1  christos 		return (NULL);
    159           1.1  christos 	}
    160           1.1  christos 
    161           1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s",
    162           1.1  christos 				 paddr, ADDR_T_STR(type)) != 0)
    163           1.1  christos 		return (NULL);
    164           1.1  christos 
    165           1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    166           1.1  christos 				      text, sizeof text,
    167           1.1  christos 				      &body, &bodylen) != 0) {
    168           1.1  christos 		return (NULL);
    169           1.1  christos 	}
    170           1.1  christos 
    171           1.1  christos 	if (code == IRPD_GETNET_OK) {
    172           1.1  christos 		free_nw(nw);
    173           1.1  christos 		if (irp_unmarshall_nw(nw, body) != 0) {
    174           1.1  christos 			nw = NULL;
    175           1.1  christos 		}
    176           1.1  christos 	} else {
    177           1.1  christos 		nw = NULL;
    178           1.1  christos 	}
    179           1.1  christos 
    180           1.1  christos 	if (body != NULL) {
    181           1.1  christos 		memput(body, bodylen);
    182           1.1  christos 	}
    183           1.1  christos 
    184           1.1  christos 	return (nw);
    185           1.1  christos }
    186           1.1  christos 
    187           1.1  christos /*%
    188           1.1  christos  * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
    189           1.1  christos  *
    190           1.1  christos  */
    191           1.1  christos 
    192           1.1  christos static struct nwent *
    193           1.1  christos nw_byname(struct irs_nw *this, const char *name, int type) {
    194           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    195           1.1  christos 	struct nwent *nw = &pvt->net;
    196           1.1  christos 	char *body = NULL;
    197           1.1  christos 	size_t bodylen;
    198           1.1  christos 	int code;
    199           1.1  christos 	char text[256];
    200           1.1  christos 
    201           1.1  christos 	if (nw->n_name != NULL &&
    202           1.1  christos 	    strcmp(name, nw->n_name) == 0 &&
    203           1.1  christos 	    nw->n_addrtype == type) {
    204           1.1  christos 		return (nw);
    205           1.1  christos 	}
    206           1.1  christos 
    207           1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    208           1.1  christos 		return (NULL);
    209           1.1  christos 	}
    210           1.1  christos 
    211           1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0)
    212           1.1  christos 		return (NULL);
    213           1.1  christos 
    214           1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    215           1.1  christos 				      text, sizeof text,
    216           1.1  christos 				      &body, &bodylen) != 0) {
    217           1.1  christos 		return (NULL);
    218           1.1  christos 	}
    219           1.1  christos 
    220           1.1  christos 	if (code == IRPD_GETNET_OK) {
    221           1.1  christos 		free_nw(nw);
    222           1.1  christos 		if (irp_unmarshall_nw(nw, body) != 0) {
    223           1.1  christos 			nw = NULL;
    224           1.1  christos 		}
    225           1.1  christos 	} else {
    226           1.1  christos 		nw = NULL;
    227           1.1  christos 	}
    228           1.1  christos 
    229           1.1  christos 	if (body != NULL) {
    230           1.1  christos 		memput(body, bodylen);
    231           1.1  christos 	}
    232           1.1  christos 
    233           1.1  christos 	return (nw);
    234           1.1  christos }
    235           1.1  christos 
    236           1.1  christos /*%
    237           1.1  christos  * void nw_rewind(struct irs_nw *this)
    238           1.1  christos  *
    239           1.1  christos  */
    240           1.1  christos 
    241           1.1  christos static void
    242           1.1  christos nw_rewind(struct irs_nw *this) {
    243           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    244           1.1  christos 	char text[256];
    245           1.1  christos 	int code;
    246           1.1  christos 
    247           1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    248           1.1  christos 		return;
    249           1.1  christos 	}
    250           1.1  christos 
    251           1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "setnetent") != 0) {
    252           1.1  christos 		return;
    253           1.1  christos 	}
    254           1.1  christos 
    255           1.1  christos 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
    256           1.1  christos 	if (code != IRPD_GETNET_SETOK) {
    257           1.1  christos 		if (irp_log_errors) {
    258           1.1  christos 			syslog(LOG_WARNING, "setnetent failed: %s", text);
    259           1.1  christos 		}
    260           1.1  christos 	}
    261           1.1  christos 
    262           1.1  christos 	return;
    263           1.1  christos }
    264           1.1  christos 
    265           1.1  christos /*%
    266           1.1  christos  * 	Prepares the cache if necessary and returns the first, or
    267           1.1  christos  * 	next item from it.
    268           1.1  christos  */
    269           1.1  christos 
    270           1.1  christos static struct nwent *
    271           1.1  christos nw_next(struct irs_nw *this) {
    272           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    273           1.1  christos 	struct nwent *nw = &pvt->net;
    274           1.1  christos 	char *body;
    275           1.1  christos 	size_t bodylen;
    276           1.1  christos 	int code;
    277           1.1  christos 	char text[256];
    278           1.1  christos 
    279           1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    280           1.1  christos 		return (NULL);
    281           1.1  christos 	}
    282           1.1  christos 
    283           1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) {
    284           1.1  christos 		return (NULL);
    285           1.1  christos 	}
    286           1.1  christos 
    287           1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    288           1.1  christos 				      text, sizeof text,
    289           1.1  christos 				      &body, &bodylen) != 0) {
    290           1.1  christos 		return (NULL);
    291           1.1  christos 	}
    292           1.1  christos 
    293           1.1  christos 	if (code == IRPD_GETNET_OK) {
    294           1.1  christos 		free_nw(nw);
    295           1.1  christos 		if (irp_unmarshall_nw(nw, body) != 0) {
    296           1.1  christos 			nw = NULL;
    297           1.1  christos 		}
    298           1.1  christos 	} else {
    299           1.1  christos 		nw = NULL;
    300           1.1  christos 	}
    301           1.1  christos 
    302           1.1  christos 	if (body != NULL)
    303           1.1  christos 		memput(body, bodylen);
    304           1.1  christos 	return (nw);
    305           1.1  christos }
    306           1.1  christos 
    307           1.1  christos /*%
    308           1.1  christos  * void nw_minimize(struct irs_nw *this)
    309           1.1  christos  *
    310           1.1  christos  */
    311           1.1  christos 
    312           1.1  christos static void
    313           1.1  christos nw_minimize(struct irs_nw *this) {
    314           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    315           1.1  christos 
    316           1.1  christos 	irs_irp_disconnect(pvt->girpdata);
    317           1.1  christos }
    318           1.1  christos 
    319           1.1  christos 
    320           1.1  christos 
    321           1.1  christos 
    322           1.1  christos /* private. */
    323           1.1  christos 
    324           1.1  christos /*%
    325           1.1  christos  *	deallocate all the memory irp_unmarshall_pw allocated.
    326           1.1  christos  *
    327           1.1  christos  */
    328           1.1  christos 
    329           1.1  christos static void
    330           1.1  christos free_nw(struct nwent *nw) {
    331           1.1  christos 	char **p;
    332           1.1  christos 
    333           1.1  christos 	if (nw == NULL)
    334           1.1  christos 		return;
    335           1.1  christos 
    336           1.1  christos 	if (nw->n_name != NULL)
    337           1.1  christos 		free(nw->n_name);
    338           1.1  christos 
    339           1.1  christos 	if (nw->n_aliases != NULL) {
    340           1.1  christos 		for (p = nw->n_aliases ; *p != NULL ; p++) {
    341           1.1  christos 			free(*p);
    342           1.1  christos 		}
    343           1.1  christos 		free(nw->n_aliases);
    344           1.1  christos 	}
    345           1.1  christos 
    346           1.1  christos 	if (nw->n_addr != NULL)
    347           1.1  christos 		free(nw->n_addr);
    348           1.1  christos }
    349           1.1  christos 
    350           1.1  christos /*! \file */
    351