| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | // isNotify checks if state is a notify message and if so, will *also* check if it
 | 
					
						
							|  |  |  | // is from one of the configured masters. If not it will not be a valid notify
 | 
					
						
							|  |  |  | // message. If the zone z is not a secondary zone the message will also be ignored.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (z *Zone) isNotify(state request.Request) bool {
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	if state.Req.Opcode != dns.OpcodeNotify {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(z.TransferFrom) == 0 {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 08:03:57 +01:00
										 |  |  | 	remote := middleware.Addr(state.IP()).Normalize()
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	for _, from := range z.TransferFrom {
 | 
					
						
							|  |  |  | 		if from == remote {
 | 
					
						
							|  |  |  | 			return true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return false
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Notify will send notifies to all configured TransferTo IP addresses.
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | func (z *Zone) Notify() {
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	go notify(z.origin, z.TransferTo)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | // notify sends notifies to the configured remote servers. It will try up to three times
 | 
					
						
							|  |  |  | // before giving up on a specific remote. We will sequentially loop through "to"
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | // until they all have replied (or have 3 failed attempts).
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | func notify(zone string, to []string) error {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetNotify(zone)
 | 
					
						
							|  |  |  | 	c := new(dns.Client)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	for _, t := range to {
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		if t == "*" {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 		if err := notifyAddr(c, m, t); err != nil {
 | 
					
						
							|  |  |  | 			log.Printf("[ERROR] " + err.Error())
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			log.Printf("[INFO] Sent notify for zone %s to %s", zone, t)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | func notifyAddr(c *dns.Client, m *dns.Msg, s string) error {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	for i := 0; i < 3; i++ {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		ret, _, err := c.Exchange(m, s)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if ret.Rcode == dns.RcodeSuccess || ret.Rcode == dns.RcodeNotImplemented {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 	return fmt.Errorf("Failed to send notify for zone '%s' to '%s'", m.Question[0].Name, s)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | }
 |