dns/server.go

105 lines
2.3 KiB
Go
Raw Normal View History

2011-02-09 07:25:01 +11:00
// Copyright 2011 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// DNS server implementation
package dns
2011-02-09 07:25:01 +11:00
import (
"os"
"net"
)
2011-03-24 05:37:07 +11:00
// HandleUDP handles one UDP connection. It reads the incoming
// message and then calls the function f.
2011-03-24 19:24:24 +11:00
// The function f is executed in a seperate goroutine at which point
// HandleUDP returns.
2011-03-22 08:53:15 +11:00
func HandleUDP(l *net.UDPConn, f func(*Conn, *Msg)) os.Error {
2011-02-09 07:25:01 +11:00
for {
2011-03-16 09:12:20 +11:00
m := make([]byte, DefaultMsgSize)
2011-03-21 05:58:55 +11:00
n, addr, e := l.ReadFromUDP(m)
2011-03-16 09:12:20 +11:00
if e != nil {
continue
}
m = m[:n]
2011-03-21 05:58:55 +11:00
2011-03-24 19:24:24 +11:00
d := new(Conn)
d.UDP = l
d.Addr = addr
d.Port = addr.Port
2011-03-21 05:58:55 +11:00
2011-03-16 09:12:20 +11:00
msg := new(Msg)
if !msg.Unpack(m) {
continue
}
2011-03-21 05:58:55 +11:00
go f(d, msg)
2011-02-09 07:25:01 +11:00
}
2011-02-10 03:59:06 +11:00
panic("not reached")
2011-02-09 07:25:01 +11:00
}
2011-03-24 05:37:07 +11:00
// HandleTCP handles one TCP connection. It reads the incoming
// message and then calls the function f.
2011-03-24 19:24:24 +11:00
// The function f is executed in a seperate goroutine at which point
// HandleTCP returns.
2011-03-22 08:53:15 +11:00
func HandleTCP(l *net.TCPListener, f func(*Conn, *Msg)) os.Error {
2011-02-10 03:59:06 +11:00
for {
2011-03-16 09:12:20 +11:00
c, e := l.AcceptTCP()
if e != nil {
return e
}
2011-03-24 19:24:24 +11:00
d := new(Conn)
d.TCP = c
d.Addr = c.RemoteAddr()
d.Port = d.TCP.RemoteAddr().(*net.TCPAddr).Port
2011-02-09 07:25:01 +11:00
2011-03-16 09:12:20 +11:00
msg := new(Msg)
2011-03-24 19:24:24 +11:00
err := d.ReadMsg(msg)
if err != nil {
2011-03-24 19:24:24 +11:00
// Logging??
2011-03-16 09:12:20 +11:00
continue
}
2011-03-21 05:58:55 +11:00
go f(d, msg)
2011-02-10 03:59:06 +11:00
}
panic("not reached")
2011-02-09 07:25:01 +11:00
}
2011-03-24 05:37:07 +11:00
// ListenAndServerTCP listens on the TCP network address addr and
// then calls HandleTCP with f to handle requests on incoming
// connections. The function f may not be nil.
2011-03-21 05:58:55 +11:00
func ListenAndServeTCP(addr string, f func(*Conn, *Msg)) os.Error {
2011-03-24 19:24:24 +11:00
if f == nil {
return &Error{Error: "The handle function may not be nil"}
}
2011-02-12 06:29:04 +11:00
a, err := net.ResolveTCPAddr(addr)
2011-02-10 03:59:06 +11:00
if err != nil {
2011-02-12 06:29:04 +11:00
return err
2011-02-10 03:59:06 +11:00
}
2011-02-12 06:29:04 +11:00
l, err := net.ListenTCP("tcp", a)
2011-02-10 03:59:06 +11:00
if err != nil {
2011-02-12 06:29:04 +11:00
return err
2011-02-10 03:59:06 +11:00
}
2011-03-22 08:53:15 +11:00
err = HandleTCP(l, f)
2011-02-12 06:29:04 +11:00
return err
}
2011-02-09 07:25:01 +11:00
2011-03-24 05:37:07 +11:00
// ListenAndServerUDP listens on the UDP network address addr and
// then calls HandleUDP with f to handle requests on incoming
// connections. The function f may not be nil.
2011-03-21 05:58:55 +11:00
func ListenAndServeUDP(addr string, f func(*Conn, *Msg)) os.Error {
2011-03-24 19:24:24 +11:00
if f == nil {
return &Error{Error: "The handle function may not be nil"}
}
2011-02-12 06:29:04 +11:00
a, err := net.ResolveUDPAddr(addr)
if err != nil {
return err
}
2011-02-12 07:29:40 +11:00
l, err := net.ListenUDP("udp", a)
2011-02-12 06:29:04 +11:00
if err != nil {
return err
}
2011-03-22 08:53:15 +11:00
err = HandleUDP(l, f)
2011-02-12 06:29:04 +11:00
return err
2011-02-09 07:25:01 +11:00
}