source: trash/old-modules/transport/protlib/network_message.h@ 6844

Last change on this file since 6844 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 5.3 KB
RevLine 
[5641]1/// ----------------------------------------*- mode: C++; -*--
2/// @file network_message.h
3/// generic class for network messages (PDUs coming from/going to network)
4/// ----------------------------------------------------------
5/// $Id: network_message.h 3247 2008-07-28 20:54:54Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/network_message.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
30/** @ingroup networkmsg
31 *
32 * This is a generic class for network messages.
33 * A network message consists of a number of bytes.
34 * There are no access member functions. So the user has full control over
35 * the buffer.
36 */
37
38#ifndef _PROTLIB__NETWORK_MESSAGE_H_
39#define _PROTLIB__NETWORK_MESSAGE_H_
40
41#include <netinet/in.h>
42#include <string>
43#include <ostream>
44
45#include "protlib_types.h"
46
47namespace protlib {
48
49/** @addtogroup networkmsg Network Messages
50 * @ingroup network
51 * @{
52 */
53
54class NetMsgError : public ProtLibException {
55public:
56 enum error_t {
57 ERROR_TOO_LONG,
58 ERROR_NO_MEM,
59 ERROR_INVALID_POS,
60 ERROR_NULL_POINTER,
61 ERROR_INVALID_START_OFFSET,
62 ERROR_TOO_SHORT,
63 ERROR_INVALID_BUFSIZE
64 };
65 NetMsgError(error_t e);
66 const char * getstr() const;
67 virtual const char *what() const throw() { return getstr(); }
68 const error_t err;
69private:
70 static const char* const errstr[];
71}; // end NetMsgError
72
73/// network message
74/** This class is used to exchange data between signalling and transport
75* protocol.
76*/
77class NetMsg {
78public:
79 static const uint32 max_size;
80 /// constructor
81 NetMsg(uint32 s);
82 /// constructor
83 NetMsg(uchar *b, uint32 s, bool copy = true);
84 /// copy constructor
85 NetMsg(const NetMsg& n);
86 /// destructor
87 ~NetMsg();
88 /// get buffer size
89 uint32 get_size() const;
90 /// get bytes left until buffer ends
91 uint32 get_bytes_left() const;
92 /// get pointer offset
93 uint32 get_pos() const;
94 /// move pointer to offset
95 NetMsg& set_pos(uint32 p);
96 /// move pointer relative
97 NetMsg& set_pos_r(int32 rp);
98 /// set pointer to beginning
99 NetMsg& to_start();
100 /// copy into NetMsg buffer
101 uint32 copy_from(const uchar *b, uint32 n);
102 /// copy into NetMsg buffer
103 uint32 copy_from(const uchar *b, uint32 start, uint32 end);
104 /// copy from NetMsg buffer
105 uint32 copy_to(uchar *b, uint32 n) const;
106 /// copy from NetMsg buffer
107 uint32 copy_to(uchar *b, uint32 start, uint32 n) const;
108 /// get pointer to buffer
109 uchar* get_buffer() const;
110 /// decode uint8
111 uint8 decode8(bool move = true);
112 /// decode uint16
113 uint16 decode16(bool move = true);
114 /// decode uint32
115 uint32 decode32(bool move = true);
116 /// decode uint64
117 uint64 decode64(bool move = true);
118 /// decode uint128
119 uint128 decode128(bool move = true);
120 /// encode uint8
121 void encode8(uint8 i, bool move = true);
122 /// encode uint16
123 void encode16(uint16 i, bool move = true);
124 /// encode uint32
125 void encode32(uint32 i, bool move = true);
126 /// encode uint64
127 void encode64(uint64 i, bool move = true);
128 /// encode uint128
129 void encode128(uint128 i, bool move = true);
130 /// decode uchars
131 void decode(uchar *c, uint32 len, bool move = true);
132 /// encode uchars
133 void encode(const uchar *c, uint32 len, bool move = true);
134 /// decode string
135 uint32 decode(string& s, uint32 len, bool move = true);
136 /// encode string
137 uint32 encode(const string& s, bool move = true);
138 /// decode IPv4
139 void decode(struct in_addr& in, bool move = true);
140 /// encode IPv4
141 void encode(const struct in_addr& in, bool move = true);
142 /// decode IPv6
143 void decode(struct in6_addr& in, bool move = true);
144 /// encode IPv6
145 void encode(const struct in6_addr& in, bool move = true);
146 /// truncate buffer
147 uint32 truncate();
148 /// truncate buffer
149 uint32 truncate(uint32 t);
150 /// apply padding
151 void padding(uint32 len, bool move = true);
152 /// test for equality
153 bool operator==(const NetMsg& n) const;
154 /// encode a NetMsg into this NetMsg
155 void encode(const NetMsg& m, uint32 len, bool move = true);
156 /// decode a NetMsg from this NetMsg
157 void decode(NetMsg& m, bool move = true);
158 /// print a raw hexdump of the buffer
159 ostream& hexdump(ostream& os, uchar *startpos=0, uint32 length=0) const;
160
161private:
162 /// buffer for data
163 uchar *buf;
164 /// buffer size
165 uint32 buf_len;
166 /// current reading/writing position
167 uchar *pos;
168 /// buffer end
169 /** Ponter to the last byte of the buffer. */
170 uchar *buf_end;
171};
172
173inline std::ostream &operator<<(std::ostream &out, const NetMsg &msg) {
174 msg.hexdump(out);
175 return out;
176}
177
178//@}
179
180} // namespace protlib
181
182#endif // _PROTLIB__NETWORK_MESSAGE_H_
Note: See TracBrowser for help on using the repository browser.