Home | History | Annotate | Line # | Download | only in mrouted
cfparse.y revision 1.13
      1   1.1  mycroft %{
      2  1.13      dsl /*	$NetBSD: cfparse.y,v 1.13 2003/05/16 22:59:50 dsl Exp $	*/
      3   1.3  thorpej 
      4   1.1  mycroft /*
      5   1.1  mycroft  * Configuration file parser for mrouted.
      6   1.1  mycroft  *
      7   1.1  mycroft  * Written by Bill Fenner, NRL, 1994
      8   1.7   itojun  * Copyright (c) 1994
      9   1.7   itojun  * Naval Research Laboratory (NRL/CCS)
     10   1.7   itojun  *                    and the
     11   1.7   itojun  * Defense Advanced Research Projects Agency (DARPA)
     12   1.7   itojun  *
     13   1.7   itojun  * All Rights Reserved.
     14   1.7   itojun  *
     15   1.7   itojun  * Permission to use, copy, modify and distribute this software and its
     16   1.7   itojun  * documentation is hereby granted, provided that both the copyright notice and
     17   1.7   itojun  * this permission notice appear in all copies of the software, derivative
     18   1.7   itojun  * works or modified versions, and any portions thereof, and that both notices
     19   1.7   itojun  * appear in supporting documentation.
     20   1.7   itojun  *
     21   1.7   itojun  * NRL AND DARPA ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
     22   1.7   itojun  * DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM
     23   1.7   itojun  * THE USE OF THIS SOFTWARE.
     24   1.1  mycroft  */
     25   1.1  mycroft #include <stdio.h>
     26   1.4  mycroft #include <stdarg.h>
     27   1.1  mycroft #include "defs.h"
     28   1.4  mycroft #include <netdb.h>
     29   1.4  mycroft 
     30   1.4  mycroft /*
     31   1.4  mycroft  * Local function declarations
     32   1.4  mycroft  */
     33   1.8      wiz static void		fatal(char *fmt, ...)
     34   1.6       is     __attribute__((__format__(__printf__, 1, 2)));
     35   1.8      wiz static void		warn(char *fmt, ...)
     36   1.6       is         __attribute__((__format__(__printf__, 1, 2)));
     37   1.8      wiz static void		yyerror(char *s);
     38   1.8      wiz static char *		next_word(void);
     39   1.8      wiz static int		yylex(void);
     40   1.8      wiz static u_int32_t	valid_if(char *s);
     41   1.8      wiz static struct ifreq *	ifconfaddr(struct ifconf *ifcp, u_int32_t a);
     42   1.8      wiz int			yyparse(void);
     43   1.1  mycroft 
     44   1.5      mrg static FILE *f __attribute__((__unused__));	/* XXX egcs */
     45   1.1  mycroft extern int udp_socket;
     46   1.1  mycroft char *configfilename = _PATH_MROUTED_CONF;
     47   1.1  mycroft 
     48   1.1  mycroft extern int cache_lifetime;
     49   1.1  mycroft extern int max_prune_lifetime;
     50   1.1  mycroft 
     51   1.1  mycroft static int lineno;
     52   1.1  mycroft static struct ifreq ifbuf[32];
     53   1.1  mycroft static struct ifconf ifc;
     54   1.1  mycroft 
     55   1.1  mycroft static struct uvif *v;
     56   1.1  mycroft 
     57   1.1  mycroft static int order;
     58   1.1  mycroft 
     59   1.1  mycroft struct addrmask {
     60   1.1  mycroft 	u_int32_t	addr;
     61   1.1  mycroft 	int	mask;
     62   1.1  mycroft };
     63   1.1  mycroft 
     64   1.1  mycroft struct boundnam {
     65   1.1  mycroft 	char		*name;
     66   1.1  mycroft 	struct addrmask	 bound;
     67   1.1  mycroft };
     68   1.1  mycroft 
     69   1.1  mycroft #define MAXBOUNDS 20
     70   1.1  mycroft 
     71   1.1  mycroft struct boundnam boundlist[MAXBOUNDS];	/* Max. of 20 named boundaries */
     72   1.1  mycroft int numbounds = 0;			/* Number of named boundaries */
     73   1.1  mycroft 
     74   1.1  mycroft %}
     75   1.1  mycroft 
     76   1.1  mycroft %union
     77   1.1  mycroft {
     78   1.1  mycroft 	int num;
     79   1.1  mycroft 	char *ptr;
     80   1.1  mycroft 	struct addrmask addrmask;
     81   1.1  mycroft 	u_int32_t addr;
     82   1.1  mycroft };
     83   1.1  mycroft 
     84   1.1  mycroft %token CACHE_LIFETIME PRUNING
     85   1.1  mycroft %token PHYINT TUNNEL NAME
     86   1.4  mycroft %token DISABLE IGMPV1 SRCRT
     87   1.4  mycroft %token METRIC THRESHOLD RATE_LIMIT BOUNDARY NETMASK ALTNET
     88   1.4  mycroft %token SYSNAM SYSCONTACT SYSVERSION SYSLOCATION
     89   1.1  mycroft %token <num> BOOLEAN
     90   1.1  mycroft %token <num> NUMBER
     91   1.1  mycroft %token <ptr> STRING
     92   1.1  mycroft %token <addrmask> ADDRMASK
     93   1.1  mycroft %token <addr> ADDR
     94   1.1  mycroft 
     95   1.4  mycroft %type <addr> interface addrname
     96   1.1  mycroft %type <addrmask> bound boundary addrmask
     97   1.1  mycroft 
     98   1.1  mycroft %start conf
     99   1.1  mycroft 
    100   1.1  mycroft %%
    101   1.1  mycroft 
    102   1.1  mycroft conf	: stmts
    103   1.1  mycroft 	;
    104   1.1  mycroft 
    105   1.1  mycroft stmts	: /* Empty */
    106   1.1  mycroft 	| stmts stmt
    107   1.1  mycroft 	;
    108   1.1  mycroft 
    109   1.1  mycroft stmt	: error
    110   1.1  mycroft 	| PHYINT interface 		{
    111   1.1  mycroft 
    112   1.1  mycroft 			vifi_t vifi;
    113   1.1  mycroft 
    114   1.1  mycroft 			if (order)
    115   1.1  mycroft 			    fatal("phyints must appear before tunnels");
    116   1.1  mycroft 
    117   1.1  mycroft 			for (vifi = 0, v = uvifs;
    118   1.1  mycroft 			     vifi < numvifs;
    119   1.1  mycroft 			     ++vifi, ++v)
    120   1.1  mycroft 			    if (!(v->uv_flags & VIFF_TUNNEL) &&
    121   1.1  mycroft 				$2 == v->uv_lcl_addr)
    122   1.1  mycroft 				break;
    123   1.1  mycroft 
    124   1.1  mycroft 			if (vifi == numvifs)
    125   1.1  mycroft 			    fatal("%s is not a configured interface",
    126  1.13      dsl 				inet_fmt($2));
    127   1.1  mycroft 
    128   1.1  mycroft 					}
    129   1.1  mycroft 		ifmods
    130   1.4  mycroft 	| TUNNEL interface addrname	{
    131   1.1  mycroft 
    132   1.1  mycroft 			struct ifreq *ifr;
    133   1.1  mycroft 			struct ifreq ffr;
    134   1.1  mycroft 			vifi_t vifi;
    135   1.1  mycroft 
    136   1.1  mycroft 			order++;
    137   1.1  mycroft 
    138   1.1  mycroft 			ifr = ifconfaddr(&ifc, $2);
    139   1.1  mycroft 			if (ifr == 0)
    140   1.1  mycroft 			    fatal("Tunnel local address %s is not mine",
    141  1.13      dsl 				inet_fmt($2));
    142   1.1  mycroft 
    143   1.1  mycroft 			strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
    144   1.1  mycroft 			if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
    145   1.1  mycroft 			    fatal("ioctl SIOCGIFFLAGS on %s",ffr.ifr_name);
    146   1.1  mycroft 			if (ffr.ifr_flags & IFF_LOOPBACK)
    147   1.1  mycroft 			    fatal("Tunnel local address %s is a loopback interface",
    148  1.13      dsl 				inet_fmt($2));
    149   1.1  mycroft 
    150   1.1  mycroft 			if (ifconfaddr(&ifc, $3) != 0)
    151   1.1  mycroft 			    fatal("Tunnel remote address %s is one of mine",
    152  1.13      dsl 				inet_fmt($3));
    153   1.1  mycroft 
    154   1.1  mycroft 			for (vifi = 0, v = uvifs;
    155   1.1  mycroft 			     vifi < numvifs;
    156   1.1  mycroft 			     ++vifi, ++v)
    157   1.1  mycroft 			    if (v->uv_flags & VIFF_TUNNEL) {
    158   1.1  mycroft 				if ($3 == v->uv_rmt_addr)
    159   1.1  mycroft 				    fatal("Duplicate tunnel to %s",
    160  1.13      dsl 					inet_fmt($3));
    161   1.1  mycroft 			    } else if (!(v->uv_flags & VIFF_DISABLED)) {
    162   1.1  mycroft 				if (($3 & v->uv_subnetmask) == v->uv_subnet)
    163   1.1  mycroft 				    fatal("Unnecessary tunnel to %s",
    164  1.13      dsl 					inet_fmt($3));
    165   1.1  mycroft 			    }
    166   1.1  mycroft 
    167   1.1  mycroft 			if (numvifs == MAXVIFS)
    168   1.1  mycroft 			    fatal("too many vifs");
    169   1.1  mycroft 
    170   1.1  mycroft 			v = &uvifs[numvifs];
    171   1.1  mycroft 			v->uv_flags	= VIFF_TUNNEL;
    172   1.1  mycroft 			v->uv_metric	= DEFAULT_METRIC;
    173   1.1  mycroft 			v->uv_rate_limit= DEFAULT_TUN_RATE_LIMIT;
    174   1.1  mycroft 			v->uv_threshold	= DEFAULT_THRESHOLD;
    175   1.1  mycroft 			v->uv_lcl_addr	= $2;
    176   1.1  mycroft 			v->uv_rmt_addr	= $3;
    177   1.1  mycroft 			v->uv_subnet	= 0;
    178   1.1  mycroft 			v->uv_subnetmask= 0;
    179   1.1  mycroft 			v->uv_subnetbcast= 0;
    180   1.1  mycroft 			strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ);
    181   1.1  mycroft 			v->uv_groups	= NULL;
    182   1.1  mycroft 			v->uv_neighbors	= NULL;
    183   1.1  mycroft 			v->uv_acl	= NULL;
    184   1.1  mycroft 			v->uv_addrs	= NULL;
    185   1.1  mycroft 
    186   1.1  mycroft 			if (!(ffr.ifr_flags & IFF_UP)) {
    187   1.1  mycroft 			    v->uv_flags |= VIFF_DOWN;
    188   1.1  mycroft 			    vifs_down = TRUE;
    189   1.1  mycroft 			}
    190   1.1  mycroft 					}
    191   1.1  mycroft 		tunnelmods
    192   1.1  mycroft 					{
    193  1.10      wiz 			logit(LOG_INFO, 0,
    194   1.1  mycroft 			    "installing tunnel from %s to %s as vif #%u - rate=%d",
    195  1.13      dsl 			    inet_fmt($2),
    196  1.13      dsl 			    inet_fmt($3),
    197   1.1  mycroft 			    numvifs, v->uv_rate_limit);
    198   1.1  mycroft 
    199   1.1  mycroft 			++numvifs;
    200   1.1  mycroft 					}
    201   1.1  mycroft 	| PRUNING BOOLEAN	    { pruning = $2; }
    202   1.1  mycroft 	| CACHE_LIFETIME NUMBER     { cache_lifetime = $2;
    203   1.1  mycroft 				      max_prune_lifetime = cache_lifetime * 2;
    204   1.1  mycroft 				    }
    205   1.1  mycroft 	| NAME STRING boundary	    { if (numbounds >= MAXBOUNDS) {
    206   1.1  mycroft 					fatal("Too many named boundaries (max %d)", MAXBOUNDS);
    207   1.1  mycroft 				      }
    208   1.1  mycroft 
    209   1.1  mycroft 				      boundlist[numbounds].name = malloc(strlen($2) + 1);
    210   1.1  mycroft 				      strcpy(boundlist[numbounds].name, $2);
    211   1.1  mycroft 				      boundlist[numbounds++].bound = $3;
    212   1.1  mycroft 				    }
    213   1.4  mycroft 	| SYSNAM STRING    {
    214   1.4  mycroft #ifdef SNMP
    215   1.4  mycroft 			    set_sysName($2);
    216   1.4  mycroft #endif /* SNMP */
    217   1.4  mycroft 			    }
    218   1.4  mycroft 	| SYSCONTACT STRING {
    219   1.4  mycroft #ifdef SNMP
    220   1.4  mycroft 			    set_sysContact($2);
    221   1.4  mycroft #endif /* SNMP */
    222   1.4  mycroft 			    }
    223   1.4  mycroft         | SYSVERSION STRING {
    224   1.4  mycroft #ifdef SNMP
    225   1.4  mycroft 			    set_sysVersion($2);
    226   1.4  mycroft #endif /* SNMP */
    227   1.4  mycroft 			    }
    228   1.4  mycroft 	| SYSLOCATION STRING {
    229   1.4  mycroft #ifdef SNMP
    230   1.4  mycroft 			    set_sysLocation($2);
    231   1.4  mycroft #endif /* SNMP */
    232   1.4  mycroft 			    }
    233   1.1  mycroft 	;
    234   1.1  mycroft 
    235   1.1  mycroft tunnelmods	: /* empty */
    236   1.4  mycroft 	| tunnelmods tunnelmod
    237   1.1  mycroft 	;
    238   1.1  mycroft 
    239   1.1  mycroft tunnelmod	: mod
    240   1.1  mycroft 	| SRCRT			{ fatal("Source-route tunnels not supported"); }
    241   1.1  mycroft 	;
    242   1.1  mycroft 
    243   1.1  mycroft ifmods	: /* empty */
    244   1.4  mycroft 	| ifmods ifmod
    245   1.1  mycroft 	;
    246   1.1  mycroft 
    247   1.1  mycroft ifmod	: mod
    248   1.1  mycroft 	| DISABLE		{ v->uv_flags |= VIFF_DISABLED; }
    249   1.4  mycroft 	| IGMPV1		{ v->uv_flags |= VIFF_IGMPV1; }
    250   1.4  mycroft 	| NETMASK addrname	{
    251   1.4  mycroft 				  u_int32_t subnet, mask;
    252   1.4  mycroft 
    253   1.4  mycroft 				  mask = $2;
    254   1.4  mycroft 				  subnet = v->uv_lcl_addr & mask;
    255   1.4  mycroft 				  if (!inet_valid_subnet(subnet, mask))
    256   1.4  mycroft 					fatal("Invalid netmask");
    257   1.4  mycroft 				  v->uv_subnet = subnet;
    258   1.4  mycroft 				  v->uv_subnetmask = mask;
    259   1.4  mycroft 				  v->uv_subnetbcast = subnet | ~mask;
    260   1.4  mycroft 				}
    261   1.4  mycroft 	| NETMASK		{
    262   1.4  mycroft 
    263   1.4  mycroft 		    warn("Expected address after netmask keyword, ignored");
    264   1.4  mycroft 
    265   1.4  mycroft 				}
    266   1.1  mycroft 	| ALTNET addrmask	{
    267   1.1  mycroft 
    268   1.1  mycroft 		    struct phaddr *ph;
    269   1.1  mycroft 
    270   1.1  mycroft 		    ph = (struct phaddr *)malloc(sizeof(struct phaddr));
    271   1.1  mycroft 		    if (ph == NULL)
    272   1.1  mycroft 			fatal("out of memory");
    273   1.1  mycroft 		    if ($2.mask) {
    274   1.4  mycroft 			VAL_TO_MASK(ph->pa_subnetmask, $2.mask);
    275   1.1  mycroft 		    } else
    276   1.4  mycroft 			ph->pa_subnetmask = v->uv_subnetmask;
    277   1.4  mycroft 		    ph->pa_subnet = $2.addr & ph->pa_subnetmask;
    278   1.4  mycroft 		    ph->pa_subnetbcast = ph->pa_subnet | ~ph->pa_subnetmask;
    279   1.4  mycroft 		    if ($2.addr & ~ph->pa_subnetmask)
    280   1.4  mycroft 			warn("Extra subnet %s/%d has host bits set",
    281  1.13      dsl 				inet_fmt($2.addr), $2.mask);
    282   1.1  mycroft 		    ph->pa_next = v->uv_addrs;
    283   1.1  mycroft 		    v->uv_addrs = ph;
    284   1.1  mycroft 
    285   1.1  mycroft 				}
    286   1.4  mycroft 	| ALTNET		{
    287   1.4  mycroft 
    288   1.4  mycroft 		    warn("Expected address after altnet keyword, ignored");
    289   1.4  mycroft 
    290   1.4  mycroft 				}
    291   1.1  mycroft 	;
    292   1.1  mycroft 
    293   1.1  mycroft mod	: THRESHOLD NUMBER	{ if ($2 < 1 || $2 > 255)
    294   1.1  mycroft 				    fatal("Invalid threshold %d",$2);
    295   1.1  mycroft 				  v->uv_threshold = $2;
    296   1.1  mycroft 				}
    297   1.1  mycroft 	| THRESHOLD		{
    298   1.1  mycroft 
    299   1.4  mycroft 		    warn("Expected number after threshold keyword, ignored");
    300   1.1  mycroft 
    301   1.1  mycroft 				}
    302   1.1  mycroft 	| METRIC NUMBER		{ if ($2 < 1 || $2 > UNREACHABLE)
    303   1.1  mycroft 				    fatal("Invalid metric %d",$2);
    304   1.1  mycroft 				  v->uv_metric = $2;
    305   1.1  mycroft 				}
    306   1.1  mycroft 	| METRIC		{
    307   1.1  mycroft 
    308   1.4  mycroft 		    warn("Expected number after metric keyword, ignored");
    309   1.1  mycroft 
    310   1.1  mycroft 				}
    311   1.1  mycroft 	| RATE_LIMIT NUMBER	{ if ($2 > MAX_RATE_LIMIT)
    312   1.1  mycroft 				    fatal("Invalid rate_limit %d",$2);
    313   1.1  mycroft 				  v->uv_rate_limit = $2;
    314   1.1  mycroft 				}
    315   1.1  mycroft 	| RATE_LIMIT		{
    316   1.1  mycroft 
    317   1.4  mycroft 		    warn("Expected number after rate_limit keyword, ignored");
    318   1.1  mycroft 
    319   1.1  mycroft 				}
    320   1.1  mycroft 	| BOUNDARY bound	{
    321   1.1  mycroft 
    322   1.1  mycroft 		    struct vif_acl *v_acl;
    323   1.1  mycroft 
    324   1.1  mycroft 		    v_acl = (struct vif_acl *)malloc(sizeof(struct vif_acl));
    325   1.1  mycroft 		    if (v_acl == NULL)
    326   1.1  mycroft 			fatal("out of memory");
    327   1.1  mycroft 		    VAL_TO_MASK(v_acl->acl_mask, $2.mask);
    328   1.1  mycroft 		    v_acl->acl_addr = $2.addr & v_acl->acl_mask;
    329   1.1  mycroft 		    if ($2.addr & ~v_acl->acl_mask)
    330   1.1  mycroft 			warn("Boundary spec %s/%d has host bits set",
    331  1.13      dsl 				inet_fmt($2.addr),$2.mask);
    332   1.1  mycroft 		    v_acl->acl_next = v->uv_acl;
    333   1.1  mycroft 		    v->uv_acl = v_acl;
    334   1.1  mycroft 
    335   1.1  mycroft 				}
    336   1.1  mycroft 	| BOUNDARY		{
    337   1.1  mycroft 
    338   1.4  mycroft 		warn("Expected boundary spec after boundary keyword, ignored");
    339   1.1  mycroft 
    340   1.1  mycroft 				}
    341   1.1  mycroft 	;
    342   1.1  mycroft 
    343   1.1  mycroft interface	: ADDR		{ $$ = $1; }
    344   1.1  mycroft 	| STRING		{
    345   1.1  mycroft 				  $$ = valid_if($1);
    346   1.1  mycroft 				  if ($$ == 0)
    347   1.1  mycroft 					fatal("Invalid interface name %s",$1);
    348   1.1  mycroft 				}
    349   1.1  mycroft 	;
    350   1.1  mycroft 
    351   1.4  mycroft addrname	: ADDR		{ $$ = $1; }
    352   1.4  mycroft 	| STRING		{ struct hostent *hp;
    353   1.4  mycroft 
    354   1.4  mycroft 				  if ((hp = gethostbyname($1)) == NULL)
    355   1.4  mycroft 				    fatal("No such host %s", $1);
    356   1.4  mycroft 
    357   1.4  mycroft 				  if (hp->h_addr_list[1])
    358   1.4  mycroft 				    fatal("Hostname %s does not %s",
    359   1.4  mycroft 					$1, "map to a unique address");
    360   1.4  mycroft 
    361   1.4  mycroft 				  bcopy(hp->h_addr_list[0], &$$,
    362   1.4  mycroft 					    hp->h_length);
    363   1.4  mycroft 				}
    364   1.4  mycroft 
    365   1.1  mycroft bound	: boundary		{ $$ = $1; }
    366   1.1  mycroft 	| STRING		{ int i;
    367   1.1  mycroft 
    368   1.1  mycroft 				  for (i=0; i < numbounds; i++) {
    369   1.1  mycroft 				    if (!strcmp(boundlist[i].name, $1)) {
    370   1.1  mycroft 					$$ = boundlist[i].bound;
    371   1.1  mycroft 					break;
    372   1.1  mycroft 				    }
    373   1.1  mycroft 				  }
    374   1.1  mycroft 				  if (i == numbounds) {
    375   1.1  mycroft 				    fatal("Invalid boundary name %s",$1);
    376   1.1  mycroft 				  }
    377   1.1  mycroft 				}
    378   1.1  mycroft 	;
    379   1.1  mycroft 
    380   1.1  mycroft boundary	: ADDRMASK	{
    381   1.1  mycroft 
    382   1.1  mycroft 			if ((ntohl($1.addr) & 0xff000000) != 0xef000000) {
    383   1.1  mycroft 			    fatal("Boundaries must be 239.x.x.x, not %s/%d",
    384  1.13      dsl 				inet_fmt($1.addr), $1.mask);
    385   1.1  mycroft 			}
    386   1.1  mycroft 			$$ = $1;
    387   1.1  mycroft 
    388   1.1  mycroft 				}
    389   1.1  mycroft 	;
    390   1.1  mycroft 
    391   1.1  mycroft addrmask	: ADDRMASK	{ $$ = $1; }
    392   1.1  mycroft 	| ADDR			{ $$.addr = $1; $$.mask = 0; }
    393   1.1  mycroft 	;
    394   1.1  mycroft %%
    395   1.4  mycroft static void
    396   1.4  mycroft fatal(char *fmt, ...)
    397   1.4  mycroft {
    398   1.4  mycroft 	va_list ap;
    399   1.4  mycroft 	char buf[200];
    400   1.4  mycroft 
    401   1.4  mycroft 	va_start(ap, fmt);
    402  1.11      wiz 	vsnprintf(buf, sizeof(buf), fmt, ap);
    403   1.1  mycroft 	va_end(ap);
    404   1.1  mycroft 
    405  1.10      wiz 	logit(LOG_ERR,0,"%s: %s near line %d", configfilename, buf, lineno);
    406   1.1  mycroft }
    407   1.1  mycroft 
    408   1.4  mycroft static void
    409   1.4  mycroft warn(char *fmt, ...)
    410   1.4  mycroft {
    411   1.4  mycroft 	va_list ap;
    412   1.4  mycroft 	char buf[200];
    413   1.4  mycroft 
    414   1.4  mycroft 	va_start(ap, fmt);
    415  1.11      wiz 	vsnprintf(buf, sizeof(buf), fmt, ap);
    416   1.1  mycroft 	va_end(ap);
    417   1.1  mycroft 
    418  1.10      wiz 	logit(LOG_WARNING,0,"%s: %s near line %d", configfilename, buf, lineno);
    419   1.1  mycroft }
    420   1.1  mycroft 
    421   1.4  mycroft static void
    422   1.4  mycroft yyerror(s)
    423   1.1  mycroft char *s;
    424   1.1  mycroft {
    425  1.10      wiz 	logit(LOG_ERR, 0, "%s: %s near line %d", configfilename, s, lineno);
    426   1.1  mycroft }
    427   1.1  mycroft 
    428   1.4  mycroft static char *
    429   1.4  mycroft next_word()
    430   1.1  mycroft {
    431   1.1  mycroft 	static char buf[1024];
    432   1.1  mycroft 	static char *p=NULL;
    433   1.1  mycroft 	extern FILE *f;
    434   1.1  mycroft 	char *q;
    435   1.1  mycroft 
    436   1.1  mycroft 	while (1) {
    437   1.1  mycroft 	    if (!p || !*p) {
    438   1.1  mycroft 		lineno++;
    439   1.1  mycroft 		if (fgets(buf, sizeof(buf), f) == NULL)
    440   1.1  mycroft 		    return NULL;
    441   1.1  mycroft 		p = buf;
    442   1.1  mycroft 	    }
    443   1.1  mycroft 	    while (*p && (*p == ' ' || *p == '\t'))	/* skip whitespace */
    444   1.1  mycroft 		p++;
    445   1.1  mycroft 	    if (*p == '#') {
    446   1.1  mycroft 		p = NULL;		/* skip comments */
    447   1.1  mycroft 		continue;
    448   1.1  mycroft 	    }
    449   1.1  mycroft 	    q = p;
    450   1.4  mycroft #ifdef SNMP
    451   1.4  mycroft        if (*p == '"') {
    452   1.4  mycroft           p++;
    453   1.4  mycroft 	       while (*p && *p != '"' && *p != '\n')
    454   1.4  mycroft 		      p++;		/* find next whitespace */
    455   1.4  mycroft           if (*p == '"')
    456   1.4  mycroft              p++;
    457   1.4  mycroft        } else
    458   1.4  mycroft #endif
    459   1.1  mycroft 	    while (*p && *p != ' ' && *p != '\t' && *p != '\n')
    460   1.1  mycroft 		p++;		/* find next whitespace */
    461   1.1  mycroft 	    *p++ = '\0';	/* null-terminate string */
    462   1.1  mycroft 
    463   1.1  mycroft 	    if (!*q) {
    464   1.1  mycroft 		p = NULL;
    465   1.1  mycroft 		continue;	/* if 0-length string, read another line */
    466   1.1  mycroft 	    }
    467   1.1  mycroft 
    468   1.1  mycroft 	    return q;
    469   1.1  mycroft 	}
    470   1.1  mycroft }
    471   1.1  mycroft 
    472   1.4  mycroft static int
    473   1.4  mycroft yylex()
    474   1.1  mycroft {
    475   1.1  mycroft 	int n;
    476   1.1  mycroft 	u_int32_t addr;
    477   1.1  mycroft 	char *q;
    478  1.13      dsl 	char c;
    479   1.1  mycroft 
    480   1.1  mycroft 	if ((q = next_word()) == NULL) {
    481   1.1  mycroft 		return 0;
    482   1.1  mycroft 	}
    483   1.1  mycroft 
    484   1.1  mycroft 	if (!strcmp(q,"cache_lifetime"))
    485   1.1  mycroft 		return CACHE_LIFETIME;
    486   1.1  mycroft 	if (!strcmp(q,"pruning"))
    487   1.1  mycroft 		return PRUNING;
    488   1.1  mycroft 	if (!strcmp(q,"phyint"))
    489   1.1  mycroft 		return PHYINT;
    490   1.1  mycroft 	if (!strcmp(q,"tunnel"))
    491   1.1  mycroft 		return TUNNEL;
    492   1.1  mycroft 	if (!strcmp(q,"disable"))
    493   1.1  mycroft 		return DISABLE;
    494   1.1  mycroft 	if (!strcmp(q,"metric"))
    495   1.1  mycroft 		return METRIC;
    496   1.1  mycroft 	if (!strcmp(q,"threshold"))
    497   1.1  mycroft 		return THRESHOLD;
    498   1.1  mycroft 	if (!strcmp(q,"rate_limit"))
    499   1.1  mycroft 		return RATE_LIMIT;
    500   1.1  mycroft 	if (!strcmp(q,"srcrt") || !strcmp(q,"sourceroute"))
    501   1.1  mycroft 		return SRCRT;
    502   1.1  mycroft 	if (!strcmp(q,"boundary"))
    503   1.1  mycroft 		return BOUNDARY;
    504   1.1  mycroft 	if (!strcmp(q,"netmask"))
    505   1.1  mycroft 		return NETMASK;
    506   1.4  mycroft 	if (!strcmp(q,"igmpv1"))
    507   1.4  mycroft 		return IGMPV1;
    508   1.4  mycroft 	if (!strcmp(q,"altnet"))
    509   1.4  mycroft 		return ALTNET;
    510   1.1  mycroft 	if (!strcmp(q,"name"))
    511   1.1  mycroft 		return NAME;
    512   1.1  mycroft 	if (!strcmp(q,"on") || !strcmp(q,"yes")) {
    513   1.1  mycroft 		yylval.num = 1;
    514   1.1  mycroft 		return BOOLEAN;
    515   1.1  mycroft 	}
    516   1.1  mycroft 	if (!strcmp(q,"off") || !strcmp(q,"no")) {
    517   1.1  mycroft 		yylval.num = 0;
    518   1.1  mycroft 		return BOOLEAN;
    519   1.1  mycroft 	}
    520  1.13      dsl 	if ((addr = inet_parse(q, &n)) != 0xffffffff) {
    521  1.13      dsl 		yylval.addrmask.mask = n;
    522  1.13      dsl 		yylval.addrmask.addr = addr;
    523  1.13      dsl 		return ADDRMASK;
    524   1.1  mycroft 	}
    525  1.13      dsl 	if ((addr = inet_parse(q,0)) != 0xffffffff &&
    526  1.13      dsl 	    inet_valid_host(addr)) {
    527  1.13      dsl 		yylval.addr = addr;
    528  1.13      dsl 		return ADDR;
    529   1.1  mycroft 	}
    530  1.13      dsl 	if (sscanf(q,"0x%8x%c",&n,&c) == 1) {
    531   1.1  mycroft 		yylval.addr = n;
    532   1.1  mycroft 		return ADDR;
    533   1.1  mycroft 	}
    534  1.13      dsl 	if (sscanf(q,"%d%c",&n,&c) == 1) {
    535   1.1  mycroft 		yylval.num = n;
    536   1.1  mycroft 		return NUMBER;
    537   1.1  mycroft 	}
    538   1.4  mycroft #ifdef SNMP
    539   1.4  mycroft 	if (!strcmp(q,"sysName"))
    540   1.4  mycroft 		return SYSNAM;
    541   1.4  mycroft 	if (!strcmp(q,"sysContact"))
    542   1.4  mycroft 		return SYSCONTACT;
    543   1.4  mycroft 	if (!strcmp(q,"sysVersion"))
    544   1.4  mycroft 		return SYSVERSION;
    545   1.4  mycroft 	if (!strcmp(q,"sysLocation"))
    546   1.4  mycroft 		return SYSLOCATION;
    547   1.4  mycroft    if (*q=='"') {
    548   1.4  mycroft       if (q[ strlen(q)-1 ]=='"')
    549   1.4  mycroft          q[ strlen(q)-1 ]='\0'; /* trash trailing quote */
    550   1.4  mycroft       yylval.ptr = q+1;
    551   1.4  mycroft       return STRING;
    552   1.4  mycroft    }
    553   1.4  mycroft #endif
    554   1.1  mycroft 	yylval.ptr = q;
    555   1.1  mycroft 	return STRING;
    556   1.1  mycroft }
    557   1.1  mycroft 
    558   1.4  mycroft void
    559   1.4  mycroft config_vifs_from_file()
    560   1.1  mycroft {
    561   1.1  mycroft 	extern FILE *f;
    562   1.1  mycroft 
    563   1.1  mycroft 	order = 0;
    564   1.1  mycroft 	numbounds = 0;
    565   1.1  mycroft 	lineno = 0;
    566   1.1  mycroft 
    567   1.1  mycroft 	if ((f = fopen(configfilename, "r")) == NULL) {
    568   1.1  mycroft 	    if (errno != ENOENT)
    569  1.10      wiz 		logit(LOG_ERR, errno, "can't open %s", configfilename);
    570   1.1  mycroft 	    return;
    571   1.1  mycroft 	}
    572   1.1  mycroft 
    573   1.1  mycroft 	ifc.ifc_buf = (char *)ifbuf;
    574   1.1  mycroft 	ifc.ifc_len = sizeof(ifbuf);
    575   1.1  mycroft 	if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
    576  1.10      wiz 	    logit(LOG_ERR, errno, "ioctl SIOCGIFCONF");
    577   1.1  mycroft 
    578   1.1  mycroft 	yyparse();
    579   1.1  mycroft 
    580   1.4  mycroft 	fclose(f);
    581   1.1  mycroft }
    582   1.1  mycroft 
    583   1.1  mycroft static u_int32_t
    584   1.1  mycroft valid_if(s)
    585   1.1  mycroft char *s;
    586   1.1  mycroft {
    587   1.8      wiz 	vifi_t vifi;
    588   1.8      wiz 	struct uvif *v;
    589   1.1  mycroft 
    590   1.1  mycroft 	for (vifi=0, v=uvifs; vifi<numvifs; vifi++, v++)
    591   1.1  mycroft 	    if (!strcmp(v->uv_name, s))
    592   1.1  mycroft 		return v->uv_lcl_addr;
    593   1.1  mycroft 
    594   1.1  mycroft 	return 0;
    595   1.1  mycroft }
    596   1.1  mycroft 
    597   1.1  mycroft static struct ifreq *
    598   1.1  mycroft ifconfaddr(ifcp, a)
    599   1.1  mycroft     struct ifconf *ifcp;
    600   1.1  mycroft     u_int32_t a;
    601   1.1  mycroft {
    602   1.1  mycroft     int n;
    603   1.1  mycroft     struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
    604   1.1  mycroft     struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
    605   1.1  mycroft 
    606   1.1  mycroft     while (ifrp < ifend) {
    607   1.1  mycroft 	    if (ifrp->ifr_addr.sa_family == AF_INET &&
    608   1.1  mycroft 		((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
    609   1.1  mycroft 		    return (ifrp);
    610   1.1  mycroft #if (defined(BSD) && (BSD >= 199006))
    611   1.1  mycroft 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    612   1.1  mycroft 		if (n < sizeof(*ifrp))
    613   1.1  mycroft 			++ifrp;
    614   1.1  mycroft 		else
    615   1.1  mycroft 			ifrp = (struct ifreq *)((char *)ifrp + n);
    616   1.1  mycroft #else
    617   1.1  mycroft 		++ifrp;
    618   1.1  mycroft #endif
    619   1.1  mycroft     }
    620   1.1  mycroft     return (0);
    621   1.1  mycroft }
    622