Branch: Tag:

2017-08-28

2017-08-28 21:19:33 by Per Cederqvist <cederp@opera.com>

Add extensible error handling in Protocols.DNS.server

Some servers might want to report errors during other mechanisms than
werror. By moving the error reporting to the new
report_decode_error() method, it is easy to override that method and
do something else.

Likewise, some servers might want to not reply to malformed requests.
Move that code to the new handle_decode_error() method.

811:    // This is a stub intended to simplify servers which allow recursion    }    +  //! Report a failure to decode a DNS request. +  //! +  //! The default implementation writes a backtrace to stderr. This +  //! method exists so that derived servers can replace it with more +  //! appropriate error handling for their environment. +  protected void report_decode_error(mixed err, mapping m, Stdio.UDP udp) +  { +  werror("DNS: Failed to read UDP packet.\n%s\n", +  describe_backtrace(err)); +  } +  +  //! Respond to a query that cannot be decoded. +  //! +  //! This method exists so that servers can override the default behaviour. +  protected void handle_decode_error(mapping err, mapping m, Stdio.UDP udp) +  { +  if(m && m->data && sizeof(m->data)>=2) +  send_reply((["rcode":1]), +  mkmapping(({"id"}), array_sscanf(m->data, "%2c")), m, udp); +  } +     protected private void rec_data(mapping m, Stdio.UDP udp)    {    mixed err;
818:    if (err = catch {    q=decode_res(m->data);    }) { -  werror("DNS: Failed to read UDP packet.\n%s\n", -  describe_backtrace(err)); -  if(m && m->data && sizeof(m->data)>=2) -  send_reply((["rcode":1]), -  mkmapping(({"id"}), array_sscanf(m->data, "%2c")), m, udp); +  report_decode_error(err, m, udp); +  handle_decode_error(err, m, udp);    }    else if(q->qr)    handle_response(q, m, udp);