#include #include "multihop.h" #include "ctrlfd.h" #include "l2tpd.h" #include "bvirt_ns.h" multihop_t::multihop_t() { nr_multihop = 0; memset(multihop_domains, 0, sizeof(multihop_domains)); memset(multihop_lhs, 0, sizeof(multihop_lhs)); memset(multihop_peers, 0, sizeof(multihop_peers)); memset(multihop_tunnel_group, 0, sizeof(multihop_tunnel_group)); } void multihop_t::show_running_config(ctrlfd_t *cfd, int verbose, const char *scope) { int i; for (i=0; iprintf("add-multihop %s%s%s%s%s%s%s %s%s%s %s\n", scope, peer->m_secret ? " --secret=" : "", peer->m_secret ? peer->m_secret : "", grp ? " --tunnel-group=" : "", grp ? grp : "", lhs ? " --lhs=" : "", lhs ? lhs : "", peer->m_netns ? peer->m_netns->m_name : "", peer->m_netns ? "^" : "", sau_to_str(&peer->remote_sau), domain ); } } l2tp_peer_t *multihop_t::lookup_multihop_peer(const char *tunnel_group, const char *full_id, const char *domain, int insert) { for (int i=0; ifind_avail_tunnel(); return NULL; } l2tp_tunnel_t *multihop_t::try_multihop(const char *user) { if (nr_multihop) { const char *str = strchr(user, '@'); if (str && *++str) { return try_multihop(user, str); } } return NULL; } l2tp_tunnel_t *multihop_t::try_multihop_tunnel_group(const char *grp, unsigned len) { for (int i=0; ifind_avail_tunnel(); } return NULL; } void multihop_t::del_multihop_index(int index) { int i; free((void *)multihop_domains[index]); free((void *)multihop_lhs[index]); free((void *)multihop_tunnel_group[index]); /* peers persist as there may be other references to them. */ multihop_domains[index] = NULL; multihop_peers[index] = NULL; multihop_lhs[index] = NULL; multihop_tunnel_group[index] = NULL; for (i=index; i<(nr_multihop-1); i++) { multihop_domains[i] = multihop_domains[i + 1]; multihop_peers[i] = multihop_peers[i + 1]; multihop_lhs[i] = multihop_lhs[i + 1]; multihop_tunnel_group[i] = multihop_tunnel_group[i + 1]; } nr_multihop--; } int multihop_t::find_multihop_index(const char *tunnel_group, char *domain, l2tp_peer_t *peer, char *lhs) { int i; for (i=0; iprintf("couldn't find multihop entry!\n"); cfd->done(-2); } else { cfd->printf("deleted %d entries\n", count); cfd->done(0); } } void multihop_t::add_multihop(ctrlfd_t *cfd, const char *tunnel_group, char *domain, l2tp_peer_t *peer, char *lhs, union sockaddr_union *sau, int insert) { int index; if (!tunnel_group) tunnel_group = "default"; index = find_multihop_index(tunnel_group, domain, peer, lhs); if (index != -1) { cfd->printf("multihop entry already exists, not adding\n"); cfd->done(-2); return; } index = nr_multihop++; if (insert) { int i; for (i=nr_multihop - 1; i > 0; i--) { multihop_domains[i] = multihop_domains[i-1]; multihop_peers[i] = multihop_peers[i-1]; multihop_lhs[i] = multihop_lhs[i-1]; multihop_tunnel_group[i] = multihop_tunnel_group[i-1]; } index = 0; } multihop_domains[index] = strdup(domain); multihop_peers[index] = peer; multihop_lhs[index] = lhs ? strdup(lhs) : NULL; multihop_tunnel_group[index] = tunnel_group ? strdup(tunnel_group) : NULL; cfd->printf("added multihop entry: "); if (insert) cfd->printf("--insert "); if (lhs) cfd->printf("--lhs=%s ", lhs); if (tunnel_group) cfd->printf("--tunnel-group=%s ", tunnel_group); cfd->printf("'%s' to %s\n", domain, sau_to_str(sau)); cfd->done(0); } void multihop_t::dump_multihop(ctrlfd_t *cfd) { cfd->printf("Number of multihop domains: %d\n", nr_multihop); for (int i=0; iprintf(" [%3d] '%s' %s", i, multihop_domains[i], sau_to_str(&peer->remote_sau)); if (peer->m_secret) cfd->printf(" secret '%s'", peer->m_secret); if (multihop_lhs[i]) cfd->printf(" lhs '%s'", multihop_lhs[i]); if (multihop_tunnel_group[i]) cfd->printf(" tunnel_group '%s'", multihop_tunnel_group[i]); cfd->my_putchar('\n'); } cfd->done(0); } void multihop_t::dump_full_state(ctrlfd_t *cfd, int do_config) { for (int i=0; iprintf("add-multihop "); if (multihop_lhs[i]) cfd->printf("--lhs=%s ", multihop_lhs[i]); cfd->printf("%s %s\n", sau_to_str(&multihop_peers[i]->remote_sau), multihop_domains[i]); } }