3 Handling for client classes. */
6 * Copyright (c) 1998-2000 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
45 static char copyright
[] =
46 "$Id: class.c,v 1.19 2000/03/18 02:15:52 mellon Exp $ Copyright (c) 1998-2000 The Internet Software Consortium. All rights reserved.\n";
52 struct collection default_collection
= {
53 (struct collection
*)0,
58 struct collection
*collections
= &default_collection
;
59 struct executable_statement
*default_classification_rules
;
61 int have_billing_classes
;
63 /* Build the default classification rule tree. */
65 void classification_setup ()
67 struct executable_statement
*rules
;
70 rules
= (struct executable_statement
*)0;
71 if (!executable_statement_allocate (&rules
, MDL
))
72 log_fatal ("Can't allocate check of default collection");
73 rules
-> op
= eval_statement
;
75 /* check-collection "default" */
76 if (!expression_allocate (&rules
-> data
.eval
, MDL
))
77 log_fatal ("Can't allocate default check expression");
78 rules
-> data
.eval
-> op
= expr_check
;
79 rules
-> data
.eval
-> data
.check
= &default_collection
;
81 default_classification_rules
= rules
;
84 void classify_client (packet
)
85 struct packet
*packet
;
87 execute_statements (packet
, (struct lease
*)0, packet
-> options
,
88 (struct option_state
*)0, &global_scope
,
89 default_classification_rules
);
92 int check_collection (packet
, lease
, collection
)
93 struct packet
*packet
;
95 struct collection
*collection
;
97 struct class *class, *nc
;
98 struct data_string data
;
103 for (class = collection
-> classes
; class; class = class -> nic
) {
104 #if defined (DEBUG_CLASS_MATCHING)
105 log_info ("checking against class %s...", class -> name
);
107 memset (&data
, 0, sizeof data
);
108 /* If a class is for billing, don't put the client in the
109 class if we've already billed it to a different class. */
110 if (class -> submatch
) {
111 status
= (evaluate_data_expression
112 (&data
, packet
, lease
,
113 packet
-> options
, (struct option_state
*)0,
114 &lease
-> scope
, class -> submatch
));
116 if ((nc
= ((struct class *)
117 hash_lookup (class -> hash
,
120 #if defined (DEBUG_CLASS_MATCHING)
121 log_info ("matches subclass %s.",
122 print_hex_1 (data
.len
,
125 data_string_forget (&data
, MDL
);
126 classify (packet
, nc
);
130 if (!class -> spawning
) {
131 data_string_forget (&data
, MDL
);
134 #if defined (DEBUG_CLASS_MATCHING)
135 log_info ("spawning subclass %s.",
136 print_hex_1 (data
.len
, data
.data
, 60));
138 nc
= (struct class *)
139 dmalloc (sizeof (struct class), MDL
);
140 memset (nc
, 0, sizeof *nc
);
141 nc
-> group
= class -> group
;
142 nc
-> superclass
= class;
143 nc
-> lease_limit
= class -> lease_limit
;
145 if (nc
-> lease_limit
) {
146 nc
-> billed_leases
=
149 sizeof (struct lease
*),
151 if (!nc
-> billed_leases
) {
152 log_error ("no memory for%s",
158 data_string_forget (&data
,
162 memset (nc
-> billed_leases
, 0,
164 sizeof nc
-> billed_leases
));
166 data_string_copy (&nc
-> hash_string
, &data
,
168 data_string_forget (&data
, MDL
);
170 class -> hash
= new_hash (0, 0, 0);
171 add_hash (class -> hash
,
172 nc
-> hash_string
.data
,
173 nc
-> hash_string
.len
,
174 (unsigned char *)nc
);
175 classify (packet
, nc
);
179 status
= (evaluate_boolean_expression_result
180 (&ignorep
, packet
, lease
,
181 packet
-> options
, (struct option_state
*)0,
182 &lease
-> scope
, class -> expr
));
185 #if defined (DEBUG_CLASS_MATCHING)
186 log_info ("matches class.");
188 classify (packet
, class);
194 void classify (packet
, class)
195 struct packet
*packet
;
198 if (packet
-> class_count
< PACKET_MAX_CLASSES
)
199 packet
-> classes
[packet
-> class_count
++] = class;
201 log_error ("too many groups for %s",
202 print_hw_addr (packet
-> raw
-> htype
,
203 packet
-> raw
-> hlen
,
204 packet
-> raw
-> chaddr
));
207 struct class *find_class (name
)
210 struct collection
*lp
;
213 for (lp
= collections
; lp
; lp
= lp
-> next
) {
214 for (cp
= lp
-> classes
; cp
; cp
= cp
-> nic
)
215 if (cp
-> name
&& !strcmp (name
, cp
-> name
))
218 return (struct class *)0;
221 int unbill_class (lease
, class)
227 for (i
= 0; i
< class -> lease_limit
; i
++)
228 if (class -> billed_leases
[i
] == lease
)
230 if (i
== class -> lease_limit
) {
231 log_error ("lease %s unbilled with no billing arrangement.",
232 piaddr (lease
-> ip_addr
));
235 lease
-> billing_class
= (struct class *)0;
236 class -> billed_leases
[i
] = (struct lease
*)0;
237 class -> leases_consumed
--;
241 int bill_class (lease
, class)
247 if (class -> leases_consumed
== class -> lease_limit
)
250 for (i
= 0; i
< class -> lease_limit
; i
++)
251 if (!class -> billed_leases
[i
])
254 if (i
== class -> lease_limit
) {
255 log_error ("class billing consumption disagrees with leases.");
259 class -> billed_leases
[i
] = lease
;
260 lease
-> billing_class
= class;
261 class -> leases_consumed
++;