00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00038 #ifndef _PROTLIB__NETWORK_MESSAGE_H_
00039 #define _PROTLIB__NETWORK_MESSAGE_H_
00040
00041 #include <netinet/in.h>
00042 #include <string>
00043 #include <ostream>
00044
00045 #include "protlib_types.h"
00046
00047 namespace protlib {
00048
00054 class NetMsgError : public ProtLibException {
00055 public:
00056 enum error_t {
00057 ERROR_TOO_LONG,
00058 ERROR_NO_MEM,
00059 ERROR_INVALID_POS,
00060 ERROR_NULL_POINTER,
00061 ERROR_INVALID_START_OFFSET,
00062 ERROR_TOO_SHORT,
00063 ERROR_INVALID_BUFSIZE
00064 };
00065 NetMsgError(error_t e);
00066 const char * getstr() const;
00067 virtual const char *what() const throw() { return getstr(); }
00068 const error_t err;
00069 private:
00070 static const char* const errstr[];
00071 };
00072
00074
00077 class NetMsg {
00078 public:
00079 static const uint32 max_size;
00081 NetMsg(uint32 s);
00083 NetMsg(uchar *b, uint32 s, bool copy = true);
00085 NetMsg(const NetMsg& n);
00087 ~NetMsg();
00089 uint32 get_size() const;
00091 uint32 get_bytes_left() const;
00093 uint32 get_pos() const;
00095 NetMsg& set_pos(uint32 p);
00097 NetMsg& set_pos_r(int32 rp);
00099 NetMsg& to_start();
00101 uint32 copy_from(const uchar *b, uint32 n);
00103 uint32 copy_from(const uchar *b, uint32 start, uint32 end);
00105 uint32 copy_to(uchar *b, uint32 n) const;
00107 uint32 copy_to(uchar *b, uint32 start, uint32 n) const;
00109 uchar* get_buffer() const;
00111 uint8 decode8(bool move = true);
00113 uint16 decode16(bool move = true);
00115 uint32 decode32(bool move = true);
00117 uint64 decode64(bool move = true);
00119 uint128 decode128(bool move = true);
00121 void encode8(uint8 i, bool move = true);
00123 void encode16(uint16 i, bool move = true);
00125 void encode32(uint32 i, bool move = true);
00127 void encode64(uint64 i, bool move = true);
00129 void encode128(uint128 i, bool move = true);
00131 void decode(uchar *c, uint32 len, bool move = true);
00133 void encode(const uchar *c, uint32 len, bool move = true);
00135 uint32 decode(string& s, uint32 len, bool move = true);
00137 uint32 encode(const string& s, bool move = true);
00139 void decode(struct in_addr& in, bool move = true);
00141 void encode(const struct in_addr& in, bool move = true);
00143 void decode(struct in6_addr& in, bool move = true);
00145 void encode(const struct in6_addr& in, bool move = true);
00147 uint32 truncate();
00149 uint32 truncate(uint32 t);
00151 void padding(uint32 len, bool move = true);
00153 bool operator==(const NetMsg& n) const;
00155 void encode(const NetMsg& m, uint32 len, bool move = true);
00157 void decode(NetMsg& m, bool move = true);
00159 ostream& hexdump(ostream& os, uchar *startpos=0, uint32 length=0) const;
00160
00161 private:
00163 uchar *buf;
00165 uint32 buf_len;
00167 uchar *pos;
00169
00170 uchar *buf_end;
00171 };
00172
00173 inline std::ostream &operator<<(std::ostream &out, const NetMsg &msg) {
00174 msg.hexdump(out);
00175 return out;
00176 }
00177
00179
00180 }
00181
00182 #endif // _PROTLIB__NETWORK_MESSAGE_H_