'This method returns the next unleased IP from range;
also does lease expiry by overwrite.'

def next_ip(self):

    encode = (lambda x: struct.unpack('!I', socket.inet_aton(x))[0])
    decode = (lambda x: socket.inet_ntoa(struct.pack('!I', x)))
    from_host = encode(self.offer_from)
    to_host = encode(self.offer_to)
    leased = [self.leases[i]['ip'] for i in self.leases if (self.leases[i]['expire'] > time.time())]
    leased = map(encode, leased)
    for offset in xrange((to_host - from_host)):
        if (((from_host + offset) % 256) and ((from_host + offset) not in leased)):
            return decode((from_host + offset))
    raise OutOfLeasesError('Ran    out    of    IP    addresses    to    lease!')
