forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Forest.h
Go to the documentation of this file.
1 
9 #ifndef FOREST_H
10 #define FOREST_H
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netinet/tcp.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <sys/time.h>
24 #include <stdlib.h>
25 #include <memory.h>
26 #include <exception>
27 
28 #include "stdinc.h"
29 #include "Util.h"
30 #include "Np4d.h"
31 
32 using namespace grafalgo;
33 
34 namespace forest {
35 
36 typedef int32_t fAdr_t;
37 typedef uint32_t comt_t;
38 typedef uint8_t flgs_t;
39 
40 
45 class Forest {
46 public:
55  enum ntyp_t {
56  UNDEF_NODE=0,
57  // untrusted node types
58  CLIENT=1,
59  SERVER=2,
60  // trusted node types
61  TRUSTED=100,
62  ROUTER=101,
63  CONTROLLER=102
64  };
65 
71  enum ptyp_t {
72  UNDEF_PKT=0,
73  // client packet types
74  CLIENT_DATA=1,
75  SUB_UNSUB=2,
76 
77  CLIENT_SIG=10,
78 
79  CONNECT=11,
80  DISCONNECT=12,
81 
82  UNKNOWN_DEST=13,
83 
84  // internal control packet types
85  NET_SIG=100,
86  RTE_REPLY=101,
87 
88  // router internal types
89  RTR_CTL=200,
90  VOQSTATUS=201
91  };
92 
98  NUL_CFG=0,
99  // client packet types
100  STATIC=1,
101  LEAFADJUST=2,
102  STEPADJUST=3
103  // adjust up by 30% when "full"
104  // adjust down by 20% when 60% full
105  };
106 
112  NUL_AXS=0,
113  // client packet types
114  OPEN=1,
115  BYPERMISSION=2,
116  BYPASSWORD=3
117  };
118 
119  // constants related to packet formats
120  static const uint8_t FOREST_VERSION = 1;
121  static const int HDR_LENG = 20;
122  static const int MAX_PLENG = 1450;
123  static const int OVERHEAD = 24;
124  static const flgs_t RTE_REQ = 0x01;
125  static const flgs_t ACK_FLAG = 0x02;
126  static const flgs_t NACK_FLAG = 0x02;
127 
128  // well-known ports
129  static const ipp_t NM_PORT = 30120;
130  static const ipp_t CC_PORT = 30121;
131  static const ipp_t CM_PORT = 30122;
132  static const ipp_t ROUTER_PORT = 30123;
133 
134  // router implementation parameters
135  static const short int MAXINTF= 20;
136  static const short int MAXLNK = 1000;
137  static const int MINBITRATE = 1;
138  static const int MAXBITRATE = 900000;
139  static const int MINPKTRATE = 1;
140  static const int MAXPKTRATE = 450000;
141  static const uint32_t BUF_SIZ = 1600;
142 
143  // comtrees used for control
144  static const comt_t NABOR_COMT = 1;
145  static const comt_t CLIENT_SIG_COMT = 2;
146  static const comt_t NET_SIG_COMT = 100;
147 
148  // methods for manipulating addresses
149  static bool validUcastAdr(fAdr_t);
150  static bool mcastAdr(fAdr_t);
151  static int zipCode(fAdr_t);
152  static int localAdr(fAdr_t);
153  static fAdr_t forestAdr(int,int);
154  static fAdr_t forestAdr(const char*);
155  static string fAdr2string(fAdr_t);
156  static bool readForestAdr(istream&, fAdr_t&);
157 
158  // miscellaneous
159  static int truPktLeng(int);
160  static string nodeType2string(ntyp_t);
161  static ntyp_t getNodeType(string&);
162  static bool isSigComt(comt_t);
163 };
164 
165 typedef uint32_t buffer_t[Forest::BUF_SIZ/sizeof(uint32_t)];
166 
172 inline bool Forest::validUcastAdr(fAdr_t adr) {
173  return adr > 0 && zipCode(adr) != 0 && localAdr(adr) != 0;
174 }
175 
180 inline bool Forest::mcastAdr(fAdr_t adr) { return adr < 0; }
181 
187 inline int Forest::zipCode(fAdr_t adr) { return (adr >> 16) & 0x7fff; }
188 
194 inline int Forest::localAdr(fAdr_t adr) { return adr & 0xffff; }
195 
202 inline fAdr_t Forest::forestAdr(int zip, int local ) {
203  return ((zip & 0xffff) << 16) | (local & 0xffff);
204 }
205 
216 inline fAdr_t Forest::forestAdr(const char *fas) {
217  int zip, local, mcAdr;
218  if (sscanf(fas,"%d.%d", &zip, &local) == 2 && zip > 0 && local > 0) {
219  return forestAdr(zip,local);
220  }
221  else if (sscanf(fas,"%d", &mcAdr) == 1 && mcAdr < 0)
222  return mcAdr;
223  else
224  return 0;
225 }
226 
232 inline string Forest::fAdr2string(fAdr_t fAdr) {
233  stringstream ss;
234  if (mcastAdr(fAdr)) ss << fAdr;
235  else ss << zipCode(fAdr) << "." << localAdr(fAdr);
236  return ss.str();
237 }
238 
245 inline int Forest::truPktLeng(int x) { return 70+x; }
246 
251 inline static bool isSigComt(comt_t comt) { return comt > 0 && comt < 1000; }
252 
253 
254 } // ends namespace
255 
256 
257 #endif