| 1 | /// ----------------------------------------*- mode: C++; -*--
 | 
|---|
| 2 | /// @file tp.h
 | 
|---|
| 3 | /// generic interface for sending/receiving network messages via a transport protocol 
 | 
|---|
| 4 | /// ----------------------------------------------------------
 | 
|---|
| 5 | /// $Id: tp.h 2872 2008-02-18 10:58:03Z bless $
 | 
|---|
| 6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/tp.h $
 | 
|---|
| 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 | // ----------------------------------------*- mode: C++; -*--
 | 
|---|
| 30 | // tp.h - generic transport protocol interface module TP
 | 
|---|
| 31 | // ----------------------------------------------------------
 | 
|---|
| 32 | // $Id: tp.h 2872 2008-02-18 10:58:03Z bless $
 | 
|---|
| 33 | // $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/tp.h $
 | 
|---|
| 34 | // ==========================================================
 | 
|---|
| 35 | //                      
 | 
|---|
| 36 | // (C)opyright, all rights reserved by
 | 
|---|
| 37 | // - Institute of Telematics, University of Karlsruhe (TH)
 | 
|---|
| 38 | // ==========================================================
 | 
|---|
| 39 | /** @ingroup transport
 | 
|---|
| 40 |  * This is the interface for sending network messages over a transport 
 | 
|---|
| 41 |  * protocol. You can receive messages through queues.
 | 
|---|
| 42 |  */
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #ifndef _PROTLIB__TP_H_
 | 
|---|
| 45 | #define _PROTLIB__TP_H_
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include <string.h>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "protlib_types.h"
 | 
|---|
| 50 | #include "messages.h"
 | 
|---|
| 51 | #include "network_message.h"
 | 
|---|
| 52 | #include "address.h"
 | 
|---|
| 53 | #include "tperror.h"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | namespace protlib {
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /** @ingroup transport Transport Protocol
 | 
|---|
| 58 |  * @ingroup network
 | 
|---|
| 59 |  * @{
 | 
|---|
| 60 |  */
 | 
|---|
| 61 | 
 | 
|---|
| 62 | 
 | 
|---|
| 63 | /// transport protocol base class
 | 
|---|
| 64 | /** This is the interface to all transport protocols.
 | 
|---|
| 65 |  * It is used by the signaling module to send network messages. 
 | 
|---|
| 66 |    // what kinds of messages?
 | 
|---|
| 67 |  * Received messages are delivered through FastQueues.
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | class TP {
 | 
|---|
| 70 | public:
 | 
|---|
| 71 |         /// constructor
 | 
|---|
| 72 |         TP(protocol_t p, const string& pname, const string& tpn, 
 | 
|---|
| 73 |            const unsigned short common_header_length, 
 | 
|---|
| 74 |            bool (*const getmsglength)(NetMsg& netmsg, uint32& msglen),
 | 
|---|
| 75 |            uint32 mp = (uint32)-1);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         /// virtual destructor
 | 
|---|
| 78 |         virtual ~TP() = 0;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         /// send a message, message is deleted after it has been sent
 | 
|---|
| 81 |         /// use_existing_connection indicates whether a new connection will be established
 | 
|---|
| 82 |         /// if required (true means that no connection will be set up if none exists yet)
 | 
|---|
| 83 |         virtual void send(NetMsg* msg, const address& addr, bool use_existing_connection= false) = 0;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         /// terminates an existing signaling association/connection
 | 
|---|
| 86 |         virtual void terminate(const address& addr) = 0;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         /// check send arguments
 | 
|---|
| 89 |         void check_send_args(const NetMsg& msg,
 | 
|---|
| 90 |                 const address& addr) const;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         /// get protocol ID
 | 
|---|
| 93 |         protocol_t get_underlying_protocol() const;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         /// get protocol name
 | 
|---|
| 96 |         string get_underlying_protocol_name() const;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         /// get TP name
 | 
|---|
| 99 |         string get_tp_name() const;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         /// is it initialized?
 | 
|---|
| 102 |         bool is_init() const;
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         /// get maximum payload
 | 
|---|
| 105 |         uint32 get_max_payload() const;
 | 
|---|
| 106 | protected:
 | 
|---|
| 107 | 
 | 
|---|
| 108 |         /// transport protocol ID
 | 
|---|
| 109 |         const protocol_t protocol;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         /// transport protocol name
 | 
|---|
| 112 |         const string protoname;
 | 
|---|
| 113 | 
 | 
|---|
| 114 |         /// TP subclass name
 | 
|---|
| 115 |         const string tp_name;
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         /// what is the length of the common header
 | 
|---|
| 118 |         const unsigned short common_header_length;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         /// function pointer to a function that figures out the msg length in number of 4 byte words
 | 
|---|
| 121 |         /// it returns false if error occured (e.g., malformed header), result is returned in variable clen_words
 | 
|---|
| 122 |         bool (*const getmsglength) (NetMsg& m, uint32& clen_words);
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         /// init state
 | 
|---|
| 125 |         bool init;
 | 
|---|
| 126 |         const uint32 max_payload;
 | 
|---|
| 127 | }; // end class TP
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /// transport protcol message
 | 
|---|
| 130 | /** This message class is used to carry received network messages. */
 | 
|---|
| 131 | class TPMsg : public message {
 | 
|---|
| 132 | /***** inherited from message *****/
 | 
|---|
| 133 | public:
 | 
|---|
| 134 |         virtual void clear_pointers();
 | 
|---|
| 135 | /***** new *****/
 | 
|---|
| 136 | public:
 | 
|---|
| 137 |     /// constructor
 | 
|---|
| 138 |     TPMsg(NetMsg* m = NULL, address* peer = NULL, address* ownaddr = NULL, TPError* e = NULL, uint16 oif = 0);
 | 
|---|
| 139 |     /// destructor
 | 
|---|
| 140 |     virtual ~TPMsg();
 | 
|---|
| 141 |     /// get peer address
 | 
|---|
| 142 |     const address* get_peeraddress() const;
 | 
|---|
| 143 |     /// set own address
 | 
|---|
| 144 |     const address* get_ownaddress() const;
 | 
|---|
| 145 |     /// set peer address
 | 
|---|
| 146 |     address* set_peeraddress(address* a);
 | 
|---|
| 147 |     /// set own address
 | 
|---|
| 148 |     address* set_ownaddress(address* a);
 | 
|---|
| 149 |     /// get network message
 | 
|---|
| 150 |     NetMsg* get_message() const;
 | 
|---|
| 151 |     /// set network message
 | 
|---|
| 152 |     NetMsg* set_message(NetMsg* m);
 | 
|---|
| 153 |     /// get TP error
 | 
|---|
| 154 |     TPError* get_error() const;
 | 
|---|
| 155 |     /// set TP error
 | 
|---|
| 156 |     TPError* set_error(TPError* e);
 | 
|---|
| 157 |     /// set Outgoing Interface
 | 
|---|
| 158 |     void set_oif(uint16 iface) { oif=iface; }
 | 
|---|
| 159 |     /// get Outgoing Interface
 | 
|---|
| 160 |     uint16 get_oif() { return oif; }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | private:
 | 
|---|
| 163 |     /// peer address
 | 
|---|
| 164 |     address* peeraddr;
 | 
|---|
| 165 |     /// own address
 | 
|---|
| 166 |     address* ownaddr;
 | 
|---|
| 167 |     /// network message
 | 
|---|
| 168 |     NetMsg* msg;
 | 
|---|
| 169 |     TPError* err;
 | 
|---|
| 170 |     /// outgoing interface index
 | 
|---|
| 171 |     uint16 oif;
 | 
|---|
| 172 | }; // end class TPMsg
 | 
|---|
| 173 | 
 | 
|---|
| 174 | inline
 | 
|---|
| 175 | bool TP::is_init() const { return init; }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | /***** class TPMsg *****/
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /***** inherited from message *****/
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /** Clear all pointer fields. */
 | 
|---|
| 182 | inline
 | 
|---|
| 183 | void TPMsg::clear_pointers() {
 | 
|---|
| 184 |         peeraddr = NULL;
 | 
|---|
| 185 |         ownaddr = NULL;
 | 
|---|
| 186 |         msg = NULL;
 | 
|---|
| 187 |         err = NULL;
 | 
|---|
| 188 | } // end clear
 | 
|---|
| 189 | 
 | 
|---|
| 190 | /***** new in NetMsg *****/
 | 
|---|
| 191 | 
 | 
|---|
| 192 | /** Initialize and set message type and source to 'transport'. */
 | 
|---|
| 193 | inline
 | 
|---|
| 194 | TPMsg::TPMsg(NetMsg* m, address* p, address* o, TPError* e, uint16 oif) 
 | 
|---|
| 195 |   : message(type_transport,qaddr_transport),
 | 
|---|
| 196 |     peeraddr(p),
 | 
|---|
| 197 |     ownaddr(o),
 | 
|---|
| 198 |     msg(m),
 | 
|---|
| 199 |     err(e),
 | 
|---|
| 200 |     oif(oif)
 | 
|---|
| 201 | {} // end constructor TPMsg
 | 
|---|
| 202 | 
 | 
|---|
| 203 | /** Dispose NetMsg, address, err and then delete TPMsg. */
 | 
|---|
| 204 | inline
 | 
|---|
| 205 | TPMsg::~TPMsg() {
 | 
|---|
| 206 |         if (msg) delete msg;
 | 
|---|
| 207 |         if (peeraddr) delete peeraddr;
 | 
|---|
| 208 |         if (ownaddr) delete ownaddr;
 | 
|---|
| 209 |         if (err) delete err;
 | 
|---|
| 210 | } // end destructor TPMsg
 | 
|---|
| 211 | 
 | 
|---|
| 212 | inline
 | 
|---|
| 213 | const address* TPMsg::get_peeraddress() const { return peeraddr; }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | inline
 | 
|---|
| 216 | const address* TPMsg::get_ownaddress() const { return ownaddr; }
 | 
|---|
| 217 | 
 | 
|---|
| 218 | inline
 | 
|---|
| 219 | address* TPMsg::set_peeraddress(address* peer) {
 | 
|---|
| 220 |   register address* oa = peeraddr;
 | 
|---|
| 221 |   peeraddr = peer;
 | 
|---|
| 222 |   return oa;
 | 
|---|
| 223 | } // end set_address
 | 
|---|
| 224 | 
 | 
|---|
| 225 | inline
 | 
|---|
| 226 | address* TPMsg::set_ownaddress(address* o) {
 | 
|---|
| 227 |   register address* oa = ownaddr;
 | 
|---|
| 228 |   ownaddr = o;
 | 
|---|
| 229 |   return oa;
 | 
|---|
| 230 | } // end set_address
 | 
|---|
| 231 | 
 | 
|---|
| 232 | inline
 | 
|---|
| 233 | NetMsg* TPMsg::get_message() const { return msg; }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | inline
 | 
|---|
| 236 | NetMsg* TPMsg::set_message(NetMsg* m) {
 | 
|---|
| 237 |         register NetMsg* omsg = msg;
 | 
|---|
| 238 |         msg = m;
 | 
|---|
| 239 |         return omsg;
 | 
|---|
| 240 | } // end set_message
 | 
|---|
| 241 | 
 | 
|---|
| 242 | inline
 | 
|---|
| 243 | TPError* TPMsg::get_error() const { return err; }
 | 
|---|
| 244 | 
 | 
|---|
| 245 | inline
 | 
|---|
| 246 | TPError* TPMsg::set_error(TPError* e) {
 | 
|---|
| 247 |         register TPError* oe = err;
 | 
|---|
| 248 |         err = e;
 | 
|---|
| 249 |         return oe;
 | 
|---|
| 250 | } // end set_error
 | 
|---|
| 251 | 
 | 
|---|
| 252 | 
 | 
|---|
| 253 | //@}
 | 
|---|
| 254 | 
 | 
|---|
| 255 | } // end namespace protlib
 | 
|---|
| 256 | 
 | 
|---|
| 257 | #endif
 | 
|---|