'Add a TSIG signature to the message.
@param keyname: the TSIG key name
@type keyname: dns.name.Name object
@param secret: the secret to use
@type secret: string
@param fudge: TSIG time fudge
@type fudge: int
@param id: the message id to encode in the tsig signature
@type id: int
@param tsig_error: TSIG error code; default is 0.
@type tsig_error: int
@param other_data: TSIG other data.
@type other_data: string
@param request_mac: This message is a response to the request which
had the specified MAC.
@type request_mac: string
@param algorithm: the TSIG algorithm to use
@type algorithm: dns.name.Name object'

def add_tsig(self, keyname, secret, fudge, id, tsig_error, other_data, request_mac, algorithm=dns.tsig.default_algorithm):

    self._set_section(ADDITIONAL)
    before = self.output.tell()
    s = self.output.getvalue()
    (tsig_rdata, self.mac, ctx) = dns.tsig.sign(s, keyname, secret, int(time.time()), fudge, id, tsig_error, other_data, request_mac, algorithm=algorithm)
    keyname.to_wire(self.output, self.compress, self.origin)
    self.output.write(struct.pack('!HHIH', dns.rdatatype.TSIG, dns.rdataclass.ANY, 0, 0))
    rdata_start = self.output.tell()
    self.output.write(tsig_rdata)
    after = self.output.tell()
    assert ((after - rdata_start) < 65536)
    if (after >= self.max_size):
        self._rollback(before)
        raise dns.exception.TooBig
    self.output.seek((rdata_start - 2))
    self.output.write(struct.pack('!H', (after - rdata_start)))
    self.counts[ADDITIONAL] += 1
    self.output.seek(10)
    self.output.write(struct.pack('!H', self.counts[ADDITIONAL]))
    self.output.seek(0, 2)
