Permalink
Browse files

Use proper error in packing and unpacking

All the relevant functions now return an error instead of
a simple boolean. This greatly approves the feedback to coders.

Spotted some fishy error handling along the way and fix that too.
  • Loading branch information...
1 parent 099c19d commit 570bf8dc69acf2fe3f3b9bf81f11108428632eb0 @miekg committed Oct 9, 2012
Showing with 211 additions and 252 deletions.
  1. +10 −11 client.go
  2. +14 −14 dnssec.go
  3. +153 −199 msg.go
  4. +4 −4 nsecx.go
  5. +6 −9 server.go
  6. +24 −15 tsig.go
View
@@ -101,9 +101,9 @@ func (c *Client) Exchange(m *Msg, a string) (r *Msg, err error) {
func (c *Client) ExchangeRtt(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
var n int
var w *reply
- out, ok := m.Pack()
- if !ok {
- return nil, 0, ErrPack
+ out, err := m.Pack()
+ if err != nil {
+ return nil, 0, err
}
var in []byte
switch c.Net {
@@ -123,8 +123,8 @@ func (c *Client) ExchangeRtt(m *Msg, a string) (r *Msg, rtt time.Duration, err e
}
r = new(Msg)
r.Size = n
- if ok := r.Unpack(in[:n]); !ok {
- return nil, w.rtt, ErrUnpack
+ if err := r.Unpack(in[:n]); err != nil {
+ return nil, w.rtt, err
}
return r, w.rtt, nil
}
@@ -158,8 +158,8 @@ func (w *reply) receive() (*Msg, error) {
return nil, err
}
p = p[:n]
- if ok := m.Unpack(p); !ok {
- return nil, ErrUnpack
+ if err := m.Unpack(p); err != nil {
+ return nil, err
}
w.rtt = time.Since(w.t)
m.Size = n
@@ -260,10 +260,9 @@ func (w *reply) send(m *Msg) (err error) {
}
w.tsigRequestMAC = mac
} else {
- ok := false
- out, ok = m.Pack()
- if !ok {
- return ErrPack
+ out, err = m.Pack()
+ if err != nil {
+ return err
}
}
w.t = time.Now()
View
@@ -119,8 +119,8 @@ func (k *RR_DNSKEY) KeyTag() uint16 {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
- n, ok := PackStruct(keywire, wire, 0)
- if !ok {
+ n, err := PackStruct(keywire, wire, 0)
+ if err != nil {
return 0
}
wire = wire[:n]
@@ -157,15 +157,15 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
- n, ok := PackStruct(keywire, wire, 0)
- if !ok {
+ n, err := PackStruct(keywire, wire, 0)
+ if err != nil {
return nil
}
wire = wire[:n]
owner := make([]byte, 255)
- off, ok1 := PackDomainName(k.Hdr.Name, owner, 0, nil, false)
- if !ok1 {
+ off, err1 := PackDomainName(k.Hdr.Name, owner, 0, nil, false)
+ if err1 != nil {
return nil
}
owner = owner[:off]
@@ -237,9 +237,9 @@ func (rr *RR_RRSIG) Sign(k PrivateKey, rrset []RR) error {
// Create the desired binary blob
signdata := make([]byte, DefaultMsgSize)
- n, ok := PackStruct(sigwire, signdata, 0)
- if !ok {
- return ErrPack
+ n, err := PackStruct(sigwire, signdata, 0)
+ if err != nil {
+ return err
}
signdata = signdata[:n]
wire := rawSignatureData(rrset, rr)
@@ -349,9 +349,9 @@ func (rr *RR_RRSIG) Verify(k *RR_DNSKEY, rrset []RR) error {
sigwire.SignerName = strings.ToLower(rr.SignerName)
// Create the desired binary blob
signeddata := make([]byte, DefaultMsgSize)
- n, ok := PackStruct(sigwire, signeddata, 0)
- if !ok {
- return ErrPack
+ n, err := PackStruct(sigwire, signeddata, 0)
+ if err != nil {
+ return err
}
signeddata = signeddata[:n]
wire := rawSignatureData(rrset, rr)
@@ -684,8 +684,8 @@ func rawSignatureData(rrset []RR, s *RR_RRSIG) (buf []byte) {
}
// 6.2. Canonical RR Form. (5) - origTTL
wire := make([]byte, r.Len()*2)
- off, ok1 := PackRR(r1, wire, 0, nil, false)
- if !ok1 {
+ off, err1 := PackRR(r1, wire, 0, nil, false)
+ if err1 != nil {
return nil
}
wire = wire[:off]
Oops, something went wrong.

0 comments on commit 570bf8d

Please sign in to comment.