#include "l2tp_tunnel.h" #include "l2tp_linux.h" static l2tp_monitor_t *monitor; void l2tpctl_monitor(ctrlfd_t *cfd, const char *user) { if (monitor) { cfd->printf("monitor: already active\n"); cfd->done(-2); return; } monitor = new l2tp_monitor_t(cfd, user); } void try_l2tp_monitor(l2tp_session_t *session, const char *user) { if (monitor && !strcmp(monitor->m_user, user)) monitor->try_monitor(session, user); return; } void l2tp_monitor_t::try_monitor(l2tp_session_t *session, const char *user) { if (m_session) { m_cfd->printf("monitor: unable to monitor %p -- already active\n", session); return; } m_session = session; session->m_monitor = this; m_cfd->printf("monitor: now monitoring session %p\n", session); } l2tp_monitor_t::l2tp_monitor_t(ctrlfd_t *cfd, const char *user) { m_cfd = cfd; m_user = strdup(user); m_session = NULL; monitor = this; m_cfd->die_notify(this); m_cfd->printf("monitoring for user '%s'\n", m_user); } l2tp_monitor_t::~l2tp_monitor_t() { if (m_session) m_session->m_monitor = NULL; m_session = NULL; m_cfd->die_unnotify(this); free((void *)m_user); m_user = NULL; m_cfd = NULL; fprintf(stderr, "~l2tp_monitor_t(%p)\n", this); if (monitor == this) monitor = NULL; } void l2tp_monitor_t::ctrlfd_die_notification(ctrlfd_t *cfd) { if (cfd != m_cfd) *(char *)0 = 0; delete this; } void l2tp_monitor_t::remove_session(l2tp_session_t *session) { if (m_session != session) *(char *)0 = 0; m_session = NULL; session->m_monitor = NULL; m_cfd->printf("monitor: no longer monitoring session %p\n", session); } void l2tp_monitor_t::monitor_l2tp_data_pkt(u8 *data, unsigned len, const char *rxtx) { unsigned i; m_cfd->printf("l2tp data packet(%s):\n", rxtx); for (i = 0; i < len; i++) { if (!(i & 15)) m_cfd->my_putchar('\t'); m_cfd->printf(" %02x", data[i]); if ((i & 15) == 15) m_cfd->my_putchar('\n'); } if ((i & 15) != 0) m_cfd->my_putchar('\n'); } void l2tp_monitor_t::monitor_l2tp_control_pkt(l2tp_packet_t *pkt, const char *rxtx) { if (!m_cfd) return; m_cfd->printf("l2tp control packet(%s):\n", rxtx); for (unsigned i = 0; i < MaxAVPs; i++) { if (!pkt->AVPs[i]) continue; int len = (int)ntohs(pkt->AVPs[i][-3]) & L2TP_AVP_LENGTH; len -= 6; m_cfd->printf("\t[%s/%d]:", RecognizedAVPs[i].name, len); if (len <= 0) { m_cfd->my_putchar('\n'); continue; } if (RecognizedAVPs[i].log_text) { char tmp[256]; strncpy(tmp, (char *)pkt->AVPs[i], len < 255 ? len : 255); if (len < 255) tmp[len] = 0; else tmp[255] = 0; m_cfd->printf(" '%s'\n", tmp); continue; } for (int j=0; jprintf(" %02x", ((u8 *)pkt->AVPs[i])[j]); m_cfd->my_putchar('\n'); } }