Example #1
0
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)
		}

	}
}
Example #2
0
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
}