| 423 | index = index % len(ip_list) |
| 424 | |
| 425 | def match(self, req_type, domain, addr): |
| 426 | # assert that the query type and domain match |
| 427 | try: |
| 428 | req_type = TYPE[req_type] |
| 429 | except KeyError: |
| 430 | return None |
| 431 | |
| 432 | try: |
| 433 | assert self.type == req_type |
| 434 | except AssertionError: |
| 435 | return None |
| 436 | |
| 437 | try: |
| 438 | assert self.domain.match(domain.decode()) |
| 439 | except AssertionError: |
| 440 | return None |
| 441 | |
| 442 | # Check to see if we have a rebind rule and if we do, return that addr first |
| 443 | if self.rebinds: |
| 444 | if self.match_history.get(addr) is not None: |
| 445 | |
| 446 | # passed the threshold - start doing a rebind |
| 447 | if self.match_history[addr] >= self.rebind_threshold: |
| 448 | return next(self.rebinds) |
| 449 | |
| 450 | # plus one |
| 451 | else: |
| 452 | self.match_history[addr] += 1 |
| 453 | |
| 454 | # add new client to this match history |
| 455 | else: |
| 456 | self.match_history[addr] = 1 |
| 457 | |
| 458 | # We didn't trip on any rebind rules (or didnt have any) |
| 459 | # but we're returning a rule-based entry based on the match |
| 460 | return next(self.ips) |
| 461 | |
| 462 | |
| 463 | # Error classes for handling rule issues |