| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | type classRule struct {
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 	fromClass, toClass uint16
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | func newClassRule(fromS, toS string) (Rule, error) {
 | 
					
						
							|  |  |  | 	var from, to uint16
 | 
					
						
							|  |  |  | 	var ok bool
 | 
					
						
							|  |  |  | 	if from, ok = dns.StringToClass[strings.ToUpper(fromS)]; !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(fromS))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if to, ok = dns.StringToClass[strings.ToUpper(toS)]; !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(toS))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return &classRule{fromClass: from, toClass: to}, nil
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the the current request.
 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | func (rule *classRule) Rewrite(w dns.ResponseWriter, r *dns.Msg) Result {
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 	if rule.fromClass > 0 && rule.toClass > 0 {
 | 
					
						
							|  |  |  | 		if r.Question[0].Qclass == rule.fromClass {
 | 
					
						
							|  |  |  | 			r.Question[0].Qclass = rule.toClass
 | 
					
						
							|  |  |  | 			return RewriteDone
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Mode returns the processing mode
 | 
					
						
							|  |  |  | func (rule *classRule) Mode() string {
 | 
					
						
							|  |  |  | 	return Stop
 | 
					
						
							|  |  |  | }
 |