kamailio cleanup and refactoring

These patches should introduce no behavioral changes to the kamailio agent.