forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Packet.cpp
Go to the documentation of this file.
1 
9 #include "Packet.h"
10 #include "CtlPkt.h"
11 
12 namespace forest {
13 
14 Packet::Packet() {
15  version = 1; buffer = 0;
16 }
17 
18 Packet::~Packet() {}
19 
24  if (buffer == 0) return false;
25  buffer_t& b = *buffer;
26  uint32_t x = ntohl(b[0]);
27  version = (x >> 28) & 0xf;
28  length = (x >> 16) & 0xfff;
29  if (version != 1 || length < Forest::OVERHEAD)
30  return false;
31  type = (Forest::ptyp_t) ((x >> 8) & 0xff);
32  flags = (x & 0xff);
33  comtree = ntohl(b[1]);
34  srcAdr = ntohl(b[2]);
35  dstAdr = ntohl(b[3]);
36  return true;
37 }
38 
42 bool Packet::pack() {
43  if (buffer == 0) return false;
44  buffer_t& b = *buffer;
45  uint32_t x = (version << 28)
46  | ((length & 0xfff) << 16)
47  | ((type & 0xff) << 8)
48  | (flags & 0xff);
49  b[0] = htonl(x);
50  b[1] = htonl(comtree);
51  b[2] = htonl(srcAdr);
52  b[3] = htonl(dstAdr);
53  return true;
54 }
55 
59 bool Packet::hdrErrCheck() const {
60  return true;
61 }
62 
66 bool Packet::payErrCheck() const {
67  return true;
68 }
69 
74 }
75 
80 }
81 
85 bool Packet::read(istream& in) {
86  int flgs, comt; string ptypString;
87 
88  Util::skipBlank(in);
89  if (!Util::readInt(in,length) ||
90  !Util::readWord(in,ptypString) ||
91  !Util::readInt(in,flgs) ||
92  !Util::readInt(in,comt) ||
95  return false;
96  flags = (flgs_t) flgs; comtree = (comt_t) comt;
97 
98  if (ptypString == "data") type = Forest::CLIENT_DATA;
99  else if (ptypString == "sub_unsub") type = Forest::SUB_UNSUB;
100  else if (ptypString == "connect") type = Forest::CONNECT;
101  else if (ptypString == "disconnect") type = Forest::DISCONNECT;
102  else if (ptypString == "rteRep") type = Forest::RTE_REPLY;
103  else if (ptypString == "client_sig") type = Forest::CLIENT_SIG;
104  else if (ptypString == "net_sig") type = Forest::NET_SIG;
105  else Util::fatal("Packet::getPacket: invalid packet type");
106 
107  if (buffer == 0) return true;
108  buffer_t& b = *buffer;
109  pack(); int32_t x;
110  for (int i = 0; i < min(8,(length-HDRLEN)/4); i++) {
111  if (Util::readInt(in,x)) b[(HDRLEN/4)+i] = htonl(x);
112  else b[(HDRLEN/4)+i] = 0;
113  }
115  return true;
116 }
117 
118 string Packet::pktTyp2string(Forest::ptyp_t type) {
119  string s;
120  if (type == Forest::CLIENT_DATA) s = "data ";
121  else if (type == Forest::SUB_UNSUB) s = "sub_unsub ";
122  else if (type == Forest::CLIENT_SIG) s = "client_sig";
123  else if (type == Forest::CONNECT) s = "connect ";
124  else if (type == Forest::DISCONNECT) s = "disconnect";
125  else if (type == Forest::NET_SIG) s = "net_sig ";
126  else if (type == Forest::RTE_REPLY) s = "rteReply ";
127  else if (type == Forest::RTR_CTL) s = "rtr_ctl ";
128  else if (type == Forest::VOQSTATUS) s = "voq_status";
129  else s = "undef ";
130  return s;
131 }
132 
133 bool Packet::string2pktTyp(string& s, Forest::ptyp_t& type) {
134  if (s == "data") type = Forest::CLIENT_DATA;
135  else if (s == "sub_unsub") type = Forest::SUB_UNSUB;
136  else if (s == "client_sig") type = Forest::CLIENT_SIG;
137  else if (s == "connect") type = Forest::CONNECT;
138  else if (s == "disconnect") type = Forest::DISCONNECT;
139  else if (s == "net_sig") type = Forest::NET_SIG;
140  else if (s == "rteReply") type = Forest::RTE_REPLY;
141  else if (s == "rtr_ctl") type = Forest::RTR_CTL;
142  else if (s == "voq_status") type = Forest::VOQSTATUS;
143  else if (s == "undef") type = Forest::UNDEF_PKT;
144  else return false;
145  return true;
146 }
147 
152 string Packet::toString() const {
153  stringstream ss;
154  ss << "len=" << setw(3) << length;
155  ss << " typ=";
156 
157  if (type == Forest::CLIENT_DATA) ss << "data ";
158  else if (type == Forest::SUB_UNSUB) ss << "sub_unsub ";
159  else if (type == Forest::CLIENT_SIG) ss << "client_sig";
160  else if (type == Forest::CONNECT) ss << "connect ";
161  else if (type == Forest::DISCONNECT) ss << "disconnect";
162  else if (type == Forest::NET_SIG) ss << "net_sig ";
163  else if (type == Forest::RTE_REPLY) ss << "rteRep ";
164  else if (type == Forest::RTR_CTL) ss << "rtr_ctl ";
165  else if (type == Forest::VOQSTATUS) ss << "voq_status";
166  else ss << "--------- ";
167  ss << " flags=" << int(flags);
168  ss << " comt=" << setw(3) << comtree;
169  ss << " sadr=" << Forest::fAdr2string(srcAdr);
170  ss << " dadr=" << Forest::fAdr2string(dstAdr);
171 
172  if (buffer == 0) {
173  ss << endl; return ss.str();
174  }
175  buffer_t& b = *buffer;
176  int32_t x;
177  for (int i = 0; i < min(8,(length-HDRLEN)/4); i++) {
178  x = ntohl(b[(HDRLEN/4)+i]);
179  ss << " " << x;
180  }
181  ss << endl;
182  if (type == Forest::CLIENT_SIG || type == Forest::NET_SIG) {
183  CtlPkt cp(*this);
184  ss << cp.toString();
185  }
186  return ss.str();
187 }
188 
189 } // ends namespace
190