diff options
author | Christian Pointner <equinox@anytun.org> | 2008-02-29 19:55:02 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2008-02-29 19:55:02 +0000 |
commit | 341e86693229de761e5241ed2de68001c0dbdffe (patch) | |
tree | 6cdf26ec4d67f4ef9d19ba7c48006e2dd3643588 /anyrtpproxy | |
parent | removed debug statements (diff) |
redone the bad anyrtpproxy
Diffstat (limited to 'anyrtpproxy')
-rw-r--r-- | anyrtpproxy/anyrtpproxy.cpp | 121 | ||||
-rw-r--r-- | anyrtpproxy/options.cpp | 105 | ||||
-rw-r--r-- | anyrtpproxy/options.h | 30 |
3 files changed, 94 insertions, 162 deletions
diff --git a/anyrtpproxy/anyrtpproxy.cpp b/anyrtpproxy/anyrtpproxy.cpp index e7be929..41cb643 100644 --- a/anyrtpproxy/anyrtpproxy.cpp +++ b/anyrtpproxy/anyrtpproxy.cpp @@ -16,44 +16,38 @@ #define MAX_PACKET_SIZE 1500 -class OpenSerHost +class ControlHost { public: - OpenSerHost() : host_("",0) {}; + ControlHost() : host_("",0) {}; - IfListElement getHost() { + Host getHost() { Lock lock(mutex); return host_; } - u_int16_t getLocalPort() { - Lock lock(mutex); - return local_port_; - } - - void setHost(std::string host, u_int16_t port, u_int16_t local_port) + void setHost(std::string addr, u_int16_t port) { Lock lock(mutex); - if(host_.host_ != host || host_.port_ != port) - cLog.msg(Log::PRIO_NOTICE) << "openSer Host detected at " << host << ":" << port - << " received at local port " << local_port; + if(host_.addr_ != addr || host_.port_ != port) + cLog.msg(Log::PRIO_NOTICE) << "control Host detected at " << addr << ":" << port; - host_.host_ = host; + host_.addr_ = addr; host_.port_ = port; - local_port_ = local_port; } private: Mutex mutex; - IfListElement host_; - u_int16_t local_port_; + Host host_; }; struct ThreadParam { - OpenSerHost& open_ser_; - IfListElement interface_; + ControlHost& control_; + UDPSocket& control_sock_; + UDPSocket& sock_; + Host first_receiver_; }; void* sender(void* p) @@ -62,31 +56,26 @@ void* sender(void* p) try { - UDPSocket recv_sock(param->interface_.host_, param->interface_.port_); - UDPSocket send_sock(gOpt.getSendPort()); - IfList remote_host_list(gOpt.getRemoteHosts()); - - cLog.msg(Log::PRIO_NOTICE) << "sender listening on: " << param->interface_.toString(); + HostList remote_host_list(gOpt.getRemoteHosts()); Buffer buf(u_int32_t(MAX_PACKET_SIZE)); + string remote_host; + u_int16_t remote_port; while(1) { - string remote_host; - u_int16_t remote_port; - buf.setLength(MAX_PACKET_SIZE); - u_int32_t len = recv_sock.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port); + u_int32_t len = param->control_sock_.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port); buf.setLength(len); - param->open_ser_.setHost(remote_host, remote_port, param->interface_.port_); + param->control_.setHost(remote_host, remote_port); - IfList::const_iterator it = remote_host_list.begin(); + HostList::const_iterator it = remote_host_list.begin(); for(;it != remote_host_list.end(); it++) - send_sock.sendTo(buf.getBuf(), buf.getLength(), it->host_, it->port_); + param->sock_.sendTo(buf.getBuf(), buf.getLength(), it->addr_, it->port_); } } catch(std::exception &e) { - cLog.msg(Log::PRIO_ERR) << "sender(" << param->interface_.toString() << ") exiting because: " << e.what() << std::endl; + cLog.msg(Log::PRIO_ERR) << "sender exiting because: " << e.what() << std::endl; } pthread_exit(NULL); } @@ -99,42 +88,31 @@ void* receiver(void* p) try { - UDPSocket recv_sock(gOpt.getSendPort()); - UDPSocket send_sock; - u_int16_t local_port = 0; - - cLog.msg(Log::PRIO_NOTICE) << "receiver listening for packets from: " << param->interface_.toString(); - Buffer buf(u_int32_t(MAX_PACKET_SIZE)); + string remote_host; + u_int16_t remote_port; + while(1) { - string remote_host; - u_int16_t remote_port; - buf.setLength(MAX_PACKET_SIZE); - u_int32_t len = recv_sock.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port); + u_int32_t len = param->sock_.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port); buf.setLength(len); - if(remote_host != param->interface_.host_ || remote_port != param->interface_.port_) + if(remote_host != param->first_receiver_.addr_ || remote_port != param->first_receiver_.port_) continue; - IfListElement openSerHost = param->open_ser_.getHost(); - if(openSerHost.host_ == "" || !openSerHost.port_) + Host control_host = param->control_.getHost(); + if(control_host.addr_ == "" || !control_host.port_) { - cLog.msg(Log::PRIO_NOTICE) << "no openser host detected till now, ignoring packet"; + cLog.msg(Log::PRIO_NOTICE) << "no control host detected till now, ignoring packet"; continue; } - if(local_port != param->open_ser_.getLocalPort()) - { - local_port = param->open_ser_.getLocalPort(); - send_sock.setLocalPort(local_port); - } - send_sock.sendTo(buf.getBuf(), buf.getLength(), openSerHost.host_, openSerHost.port_); + param->control_sock_.sendTo(buf.getBuf(), buf.getLength(), control_host.addr_, control_host.port_); } } catch(std::exception &e) { - cLog.msg(Log::PRIO_ERR) << "sender(" << param->interface_.toString() << ") exiting because: " << e.what() << std::endl; + cLog.msg(Log::PRIO_ERR) << "receiver exiting because: " << e.what() << std::endl; } pthread_exit(NULL); } @@ -212,27 +190,28 @@ int main(int argc, char* argv[]) SignalController sig; sig.init(); - OpenSerHost open_ser; - std::list<ThreadParam> params; - IfList listeners(gOpt.getLocalInterfaces()); - IfList::iterator it = listeners.begin(); - for(;it != listeners.end();++it) - { - ThreadParam param = {open_ser, *it}; - params.push_back(param); + try { + ControlHost control_host; + UDPSocket control_sock(gOpt.getControlInterface().addr_, gOpt.getControlInterface().port_); + UDPSocket sock(gOpt.getSendPort()); + + ThreadParam senderParam = {control_host, control_sock, sock, gOpt.getRemoteHosts().front()}; pthread_t senderThread; - pthread_create(&senderThread, NULL, sender, &(params.back())); + pthread_create(&senderThread, NULL, sender, &senderParam); pthread_detach(senderThread); - } - - ThreadParam param = {open_ser, gOpt.getRemoteHosts().front()}; - params.push_back(param); - pthread_t receiverThread; - pthread_create(&receiverThread, NULL, receiver, &(params.back())); - pthread_detach(receiverThread); + + ThreadParam receiverParam = {control_host, control_sock, sock, gOpt.getRemoteHosts().front()}; + pthread_t receiverThread; + pthread_create(&receiverThread, NULL, receiver, &receiverParam); + pthread_detach(receiverThread); - int ret = sig.run(); - - return ret; + int ret = sig.run(); + return ret; + } + catch(std::exception& e) + { + cLog.msg(Log::PRIO_ERR) << "an error occurred: " << e.what(); + return -1; + } } diff --git a/anyrtpproxy/options.cpp b/anyrtpproxy/options.cpp index 6e7f291..2acc597 100644 --- a/anyrtpproxy/options.cpp +++ b/anyrtpproxy/options.cpp @@ -49,16 +49,16 @@ Options& Options::instance() return *inst; } -Options::Options() +Options::Options() : control_interface_("0.0.0.0", 22220) + { progname_ = "anyrtpproxy"; chroot_ = false; username_ = "nobody"; chroot_dir_ = "/var/run"; daemonize_ = true; - send_port_ = 22220; - local_interfaces_.push_back(IfListElement("0.0.0.0", 22221)); - remote_hosts_.push_back(IfListElement("127.0.0.1", 22222)); + send_port_ = 22221; + remote_hosts_.push_back(Host("127.0.0.1", 22222)); } Options::~Options() @@ -99,6 +99,16 @@ Options::~Options() i+=2; \ } +#define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1 || argv[i+1][0] == '-') \ + return false; \ + VALUE = std::string(argv[i+1]); \ + argc--; \ + i++; \ + } + #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ { \ @@ -109,6 +119,7 @@ Options::~Options() i++; \ } + #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \ else if(str == SHORT || str == LONG) \ { \ @@ -143,9 +154,9 @@ bool Options::parse(int argc, char* argv[]) PARSE_SCALAR_PARAM("-u","--user", username_) PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_) PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_) + PARSE_STRING_PARAM("-s","--control", control_interface_) PARSE_SCALAR_PARAM("-p","--port", send_port_) - PARSE_CSLIST_PARAM("-l","--listen", local_interfaces_) - PARSE_CSLIST_PARAM("-r","--hosts", remote_hosts_) + PARSE_CSLIST_PARAM("-r","--remote-hosts", remote_hosts_) else return false; } @@ -155,11 +166,9 @@ bool Options::parse(int argc, char* argv[]) bool Options::sanityCheck() { - IfList::iterator it=local_interfaces_.begin(); - for(u_int32_t i=0; it != local_interfaces_.end(); ++it, ++i) - if(!it->port_) it->port_ = 22221; + if(control_interface_.port_) control_interface_.port_ = 22220; - it=remote_hosts_.begin(); + HostList::iterator it=remote_hosts_.begin(); for(u_int32_t i=0; it != remote_hosts_.end(); ++it, ++i) if(!it->port_) it->port_ = 22222; @@ -169,14 +178,14 @@ bool Options::sanityCheck() void Options::printUsage() { std::cout << "USAGE:" << std::endl; - std::cout << "anyrtpproxy [-h|--help] prints this..." << std::endl; - std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl; - std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl; - std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl; - std::cout << " [-d|--nodaemonize] don't run in background" << std::endl; - std::cout << " [-p|--port] <port> use this port to send out packets" << std::endl; - std::cout << " [-l|--listen] <host[:port]>[,<host>[:<port> ..] a list of local interfaces to listen on" << std::endl; - std::cout << " [-r|--hosts] <host[:port]>[,<host>[:<port> ..] a list of remote hosts to send duplicates to" << std::endl; + std::cout << "anyrtpproxy [-h|--help] prints this..." << std::endl; + std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl; + std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl; + std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl; + std::cout << " [-d|--nodaemonize] don't run in background" << std::endl; + std::cout << " [-s|--control] <addr[:port]> the address/port to listen on for control commands" << std::endl; + std::cout << " [-p|--port] <port> use this port to send out packets to remote hosts" << std::endl; + std::cout << " [-r|--remote-hosts] <addr[:port]>[,<addr[:<port]> .. ] a list of remote hosts to send duplicates to" << std::endl; } void Options::printOptions() @@ -187,17 +196,10 @@ void Options::printOptions() std::cout << "username='" << username_ << "'" << std::endl; std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl; std::cout << "daemonize='" << daemonize_ << "'" << std::endl; + std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl; std::cout << "send-port='" << send_port_ << "'" << std::endl; - std::cout << "local interfaces='"; - IfList::const_iterator it=local_interfaces_.begin(); - for(u_int32_t i=0; it != local_interfaces_.end(); ++it, ++i) - { - if(i) std::cout << "','"; - std::cout << it->toString(); - } - std::cout << "'" << std::endl; std::cout << "remote hosts='"; - it=remote_hosts_.begin(); + HostList::const_iterator it=remote_hosts_.begin(); for(u_int32_t i=0; it != remote_hosts_.end(); ++it, ++i) { if(i) std::cout << "','"; @@ -212,86 +214,43 @@ std::string Options::getProgname() return progname_; } - -Options& Options::setProgname(std::string p) -{ - Lock lock(mutex); - progname_ = p; - return *this; -} - bool Options::getChroot() { Lock lock(mutex); return chroot_; } -Options& Options::setChroot(bool c) -{ - Lock lock(mutex); - chroot_ = c; - return *this; -} - std::string Options::getUsername() { Lock lock(mutex); return username_; } -Options& Options::setUsername(std::string u) -{ - Lock lock(mutex); - username_ = u; - return *this; -} - std::string Options::getChrootDir() { Lock lock(mutex); return chroot_dir_; } -Options& Options::setChrootDir(std::string c) -{ - Lock lock(mutex); - chroot_dir_ = c; - return *this; -} - bool Options::getDaemonize() { Lock lock(mutex); return daemonize_; } -Options& Options::setDaemonize(bool d) -{ - Lock lock(mutex); - daemonize_ = d; - return *this; -} - u_int16_t Options::getSendPort() { Lock lock(mutex); return send_port_; } -Options& Options::setSendPort(u_int16_t p) -{ - Lock lock(mutex); - send_port_ = p; - return *this; -} - -IfList Options::getLocalInterfaces() +Host Options::getControlInterface() { Lock lock(mutex); - return local_interfaces_; + return control_interface_; } -IfList Options::getRemoteHosts() +HostList Options::getRemoteHosts() { Lock lock(mutex); return remote_hosts_; diff --git a/anyrtpproxy/options.h b/anyrtpproxy/options.h index 48139f7..c75da6a 100644 --- a/anyrtpproxy/options.h +++ b/anyrtpproxy/options.h @@ -35,28 +35,28 @@ #include <list> #include <sstream> -class IfListElement +class Host { public: - IfListElement(std::string host, u_int16_t port) : host_(host), port_(port) {} - IfListElement(std::string host_port) + Host(std::string addr, u_int16_t port) : addr_(addr), port_(port) {} + Host(std::string addr_port) { - std::istringstream iss(host_port); - getline(iss, host_, ':'); + std::istringstream iss(addr_port); + getline(iss, addr_, ':'); if(!(iss >> port_)) port_ = 0; } std::string toString() const { std::ostringstream oss; - oss << host_ << ":" << port_; + oss << addr_ << ":" << port_; return oss.str(); } - std::string host_; + std::string addr_; u_int16_t port_; }; -typedef std::list<IfListElement> IfList; +typedef std::list<Host> HostList; class Options { @@ -68,19 +68,13 @@ public: void printOptions(); std::string getProgname(); - Options& setProgname(std::string p); bool getChroot(); - Options& setChroot(bool c); std::string getUsername(); - Options& setUsername(std::string u); std::string getChrootDir(); - Options& setChrootDir(std::string c); bool getDaemonize(); - Options& setDaemonize(bool d); u_int16_t getSendPort(); - Options& setSendPort(u_int16_t p); - IfList getLocalInterfaces(); - IfList getRemoteHosts(); + Host getControlInterface(); + HostList getRemoteHosts(); private: Options(); @@ -107,8 +101,8 @@ private: std::string chroot_dir_; bool daemonize_; u_int16_t send_port_; - IfList local_interfaces_; - IfList remote_hosts_; + Host control_interface_; + HostList remote_hosts_; }; extern Options& gOpt; |