| 1 | /// ----------------------------------------*- mode: C++; -*-- | 
|---|
| 2 | /// @file tp.cpp | 
|---|
| 3 | /// generic interface for sending/receiving network messages via a transport protocol | 
|---|
| 4 | /// ---------------------------------------------------------- | 
|---|
| 5 | /// $Id: tp.cpp 2872 2008-02-18 10:58:03Z bless $ | 
|---|
| 6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/src/tp.cpp $ | 
|---|
| 7 | // =========================================================== | 
|---|
| 8 | // | 
|---|
| 9 | // Copyright (C) 2005-2007, all rights reserved by | 
|---|
| 10 | // - Institute of Telematics, Universitaet Karlsruhe (TH) | 
|---|
| 11 | // | 
|---|
| 12 | // More information and contact: | 
|---|
| 13 | // https://projekte.tm.uka.de/trac/NSIS | 
|---|
| 14 | // | 
|---|
| 15 | // This program is free software; you can redistribute it and/or modify | 
|---|
| 16 | // it under the terms of the GNU General Public License as published by | 
|---|
| 17 | // the Free Software Foundation; version 2 of the License | 
|---|
| 18 | // | 
|---|
| 19 | // This program is distributed in the hope that it will be useful, | 
|---|
| 20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 22 | // GNU General Public License for more details. | 
|---|
| 23 | // | 
|---|
| 24 | // You should have received a copy of the GNU General Public License along | 
|---|
| 25 | // with this program; if not, write to the Free Software Foundation, Inc., | 
|---|
| 26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|---|
| 27 | // | 
|---|
| 28 | // =========================================================== | 
|---|
| 29 |  | 
|---|
| 30 | /** @ingroup protlib | 
|---|
| 31 | * This is the interface for sending network messages over a transport | 
|---|
| 32 | * protocol. You can receive messages through queues. | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include "tp.h" | 
|---|
| 36 | #include "threadsafe_db.h" | 
|---|
| 37 | #include "logfile.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace protlib { | 
|---|
| 40 |  | 
|---|
| 41 | /** @addtogroup protlib | 
|---|
| 42 | * @ingroup protlib | 
|---|
| 43 | * @{ | 
|---|
| 44 | */ | 
|---|
| 45 |  | 
|---|
| 46 | using namespace log; | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | /***** class TP *****/ | 
|---|
| 50 |  | 
|---|
| 51 | /** Set ID of the underlying transport protocol, e.g. TCP, SCTP or UDP. | 
|---|
| 52 | * Init is set false here, set to true in constructors of derived classes if | 
|---|
| 53 | * initialization is done. | 
|---|
| 54 | * If the maximum payload is greater than the maximum size of a network | 
|---|
| 55 | * message, it is decreased. | 
|---|
| 56 | */ | 
|---|
| 57 | TP::TP(protocol_t p, const string& pname, const string& tpn, | 
|---|
| 58 | const unsigned short common_header_length, | 
|---|
| 59 | bool (*const getmsglength)(NetMsg& netmsg, uint32& msglen), | 
|---|
| 60 | uint32 mp) | 
|---|
| 61 | : protocol(p), protoname(pname), tp_name(tpn), | 
|---|
| 62 | common_header_length(common_header_length), | 
|---|
| 63 | getmsglength(getmsglength), | 
|---|
| 64 | init(false), | 
|---|
| 65 | max_payload((mp<NetMsg::max_size)?mp:(NetMsg::max_size)) | 
|---|
| 66 | {} | 
|---|
| 67 |  | 
|---|
| 68 | /** TP destructor does nothing. */ | 
|---|
| 69 | TP::~TP() { init = false; } | 
|---|
| 70 |  | 
|---|
| 71 | /** Get the ID of the underlying transport protocol. */ | 
|---|
| 72 | protocol_t TP::get_underlying_protocol() const { return protocol; } | 
|---|
| 73 |  | 
|---|
| 74 | /** Get the name of the underlying transport protocol. */ | 
|---|
| 75 | string TP::get_underlying_protocol_name() const { return protoname; } | 
|---|
| 76 |  | 
|---|
| 77 | /** Get the name of this TP implementation. */ | 
|---|
| 78 | string TP::get_tp_name() const { return tp_name; } | 
|---|
| 79 |  | 
|---|
| 80 | uint32 TP::get_max_payload() const { return max_payload; } | 
|---|
| 81 |  | 
|---|
| 82 | /* @param msg NetMsg to send | 
|---|
| 83 | * @param addr destination address | 
|---|
| 84 | * @return true if args are OK. | 
|---|
| 85 | */ | 
|---|
| 86 |  | 
|---|
| 87 | void TP::check_send_args(const NetMsg& msg, const address& addr) | 
|---|
| 88 | const { | 
|---|
| 89 | if (!init) { | 
|---|
| 90 | Log(ERROR_LOG,LOG_NORMAL, "TP", "TP::check_send_args: " << tp_name << " not initialized"); | 
|---|
| 91 | throw TPErrorArgsNotInit(); | 
|---|
| 92 | } // end if not init | 
|---|
| 93 |  | 
|---|
| 94 | if ((msg.get_size()==0) || (msg.get_size()>max_payload)) { | 
|---|
| 95 | Log(ERROR_LOG,LOG_NORMAL, "TP", "TP::check_send_args: NetMsg empty or too big. Size: " << msg.get_size() << ", " << tp_name << ", max_payload " << max_payload); | 
|---|
| 96 | throw TPErrorPayload(); | 
|---|
| 97 | } // end if too big | 
|---|
| 98 |  | 
|---|
| 99 | } // end check_send_args | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | //@} | 
|---|
| 103 |  | 
|---|
| 104 | } // end namespace protlib | 
|---|