コード例 #1
0
ファイル: clients.go プロジェクト: adrian-bl/rna
func (cq *Cq) clientLookup(cr *clientRequest) {
	// Ensure that this query makes some sense
	if len(cr.Query.Questions) == 1 {
		q := cr.Query.Questions[0]
		c := make(chan *lookupRes)
		go cq.collapsedLookup(q, c)
		lres := <-c
		l.Debug("final lookup reply -> %v", lres)
		if lres != nil { // fixme: error
			cres := lres.cres
			p := &packet.ParsedPacket{}
			p.Header.Id = cr.Query.Header.Id
			p.Header.Response = true
			p.Header.ResponseCode = cres.ResponseCode
			p.Questions = cr.Query.Questions
			if lres.negative {
				p.Nameservers = append(p.Nameservers, cres.ResourceRecord...)
			} else {
				p.Answers = append(p.Answers, cres.ResourceRecord...)
			}
			cq.conn.WriteToUDP(packet.Assemble(p), cr.RemoteAddr)
		} else {
			l.Info("Lookup returned an error, should send it back to client (fixme): %+v", lres)
		}
	} else {
		l.Info("Dropping nonsense query")
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: adrian-bl/rna
func main() {
	flag.Parse()

	listenStr := fmt.Sprintf(":%d", *listenPort)
	l.Info("Starting up, listening on %s", listenStr)

	listenAddr, err := net.ResolveUDPAddr("udp", listenStr)
	if err != nil {
		l.Panic("ResolveUDPAddr failed: %v", err)
	}

	conn, err := net.ListenUDP("udp", listenAddr)
	if err != nil {
		l.Panic("listen failed: %v", err)
	}

	buf := make([]byte, constants.MAX_SIZE_UDP) // Upper limit as defined by RFC 1035 2.3.4
	nc := cache.NewNameCache()
	sq := queue.NewServerQueue(nc)
	cq := queue.NewClientQueue(conn, nc, sq)

	for {
		nread, remoteAddr, err := conn.ReadFromUDP(buf)
		if err != nil || nread < constants.FIX_SIZE_HEADER {
			l.Debug("%v dropping malformed datagram. Size=%d, err=%v", remoteAddr, nread, err)
			continue
		}

		p, err := packet.Parse(buf[0:nread])
		if err != nil {
			l.Debug("%v failed to parse datagram, err=%v", remoteAddr, err)
			continue
		}

		if p.Header.Response == false && p.Header.Opcode == constants.OP_QUERY && p.Header.RecDesired {
			// This is a query, requesting recursion
			cq.AddClientRequest(p, remoteAddr)
		} else if p.Header.Response == true && p.Header.Opcode == constants.OP_QUERY {
			// A reply, try to put it into our cache
			nc.Put(p, remoteAddr)
		} else {
			// DOES NOT COMPUTE.
			l.Info("%v dropped packet", remoteAddr)
		}

	}
}
コード例 #3
0
ファイル: clients_inflight.go プロジェクト: adrian-bl/rna
func (cq *Cq) blockForQuery(pp *packet.ParsedPacket) bool {
	cbi := &putCbItem{Key: pp.Questions[0].Name.ToKey(), Type: pp.Questions[0].Type}
	key := cbi.ToString()

	cq.Lock()
	c := make(chan bool)
	if cq.inflight[key] == nil {
		cq.inflight[key] = make([]chan bool, 0)
	}
	cq.inflight[key] = append(cq.inflight[key], c)
	cq.Unlock()

	l.Debug("Blocking for progress on %s", key)
	select {
	case <-c:
		l.Debug("%s progressed", key)
		return true
	case <-time.After(time.Second * 2):
		l.Debug("%s timed out!", key)
		return false
	}
}
コード例 #4
0
ファイル: clients_inflight.go プロジェクト: adrian-bl/rna
func (cq *Cq) handlePutCallback(isrc cache.InjectSource) {
	cbi := &putCbItem{Key: isrc.Name.ToKey(), Type: isrc.Type}
	key := cbi.ToString()

	cq.Lock()
	if cq.inflight[key] != nil {
		for _, c := range cq.inflight[key] {
			l.Debug("Broadcasting progress on %s", key)
			close(c)
		}
		cq.inflight[key] = nil
	}
	cq.Unlock()
}
コード例 #5
0
ファイル: cache.go プロジェクト: adrian-bl/rna
// Internal implementation of cache who works on multiple maps
func (c *Cache) injectInternal(m map[string]cmap, isrc InjectSource, item packet.ResourceRecordFormat, rcode uint8) {
	key := item.Name.ToKey()
	t := item.Type
	data := item.Data
	ttl := item.Ttl

	c.Lock()
	defer c.Unlock()

	if m[key] == nil {
		m[key] = make(cmap, 0)
	}
	if m[key][t] == nil {
		m[key][t] = make(centry, 0)
	}

	l.Debug("+ cache inject: %+v", item)

	cpy := make([]byte, len(data))
	copy(cpy, data)
	m[key][t][string(data)] = citem{data: cpy, deadline: time.Now().Add(time.Duration(ttl) * time.Second), rcode: rcode}
	c.notify(isrc)
}
コード例 #6
0
ファイル: clients.go プロジェクト: adrian-bl/rna
func (cq *Cq) advanceCache(q packet.QuestionFormat) *packet.ParsedPacket {
	// our hardcoded, not so redundant slist
	targetNS := "192.5.5.241:53"
	targetXH := &packet.Namelabel{}
	targetQT := q.Type

POP_LOOP:
	for i := 0; ; i++ {
		label := q.Name.PoppedLabel(i) // removes 'i' labels from the label list
		nsrec, _ := cq.cache.Lookup(*label, constants.TYPE_NS)

		if nsrec != nil { // we got an NS cache entry for this level
			var candidate_cres *cache.CacheResult
			var candidate_label packet.Namelabel

			// loop trough all NS servers for this record
			for _, candidate_data := range nsrec.ResourceRecord {
				name, err := packet.ParseName(candidate_data.Data)
				if err == nil {
					l.Debug("NS %v handles %v", name, label)
					cres, _ := cq.cache.Lookup(name, constants.TYPE_A)
					if cres != nil {
						candidate_cres = cres
					} else {
						candidate_label = name
					}
				}
			}

			// Fixme: We should try to resolve (yet unknown) nameservers
			// even if we got a candidate_res as the one we are contacting
			// might fail for some reason.

			if candidate_cres == nil && candidate_label.Len() > 0 {
				l.Debug("Looking up IP of known candidate: %v", candidate_label)
				c := make(chan *lookupRes)
				go cq.collapsedLookup(packet.QuestionFormat{Type: constants.TYPE_A, Class: constants.CLASS_IN, Name: candidate_label}, c)
				lres := <-c
				if lres != nil && lres.negative == false {
					candidate_cres = lres.cres
				}
			}
			if candidate_cres != nil {
				l.Debug("We got an RR: %v", candidate_cres)
				for _, v := range candidate_cres.ResourceRecord {
					if v.Type != constants.TYPE_A {
						l.Panic("Not an A type: %v", v)
					}
					targetNS = fmt.Sprintf("%d.%d.%d.%d:53", v.Data[0], v.Data[1], v.Data[2], v.Data[3])
					targetXH = label
					break POP_LOOP
				}
			}
		}
		if label.Len() == 1 {
			break
		}
	}

	pp := &packet.ParsedPacket{}
	pp.Header.Id = uint16(rand.Uint32()) // will simply overflow
	pp.Header.Opcode = constants.OP_QUERY
	pp.Header.QuestionCount = 1
	pp.Questions = []packet.QuestionFormat{{Name: q.Name, Class: constants.CLASS_IN, Type: targetQT}}
	remoteNs, err := net.ResolveUDPAddr("udp", targetNS)

	if err == nil {
		l.Info("+ op=query, remote=%s, type=%d, id=%d, name=%v", targetNS, targetQT, pp.Header.Id, q.Name)
		cq.conn.WriteToUDP(packet.Assemble(pp), remoteNs)
		cq.sq.registerQuery(pp.Questions[0], remoteNs, targetXH)
	}

	return pp
}