forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
CtlPktDoowon.cpp
1 
23 #include "CtlPkt.h"
24 
25 namespace forest {
26 
29 CtlPkt::CtlPkt() { reset(); }
30 
34 CtlPkt::CtlPkt(const Packet& p) { reset(p); }
35 
42 CtlPkt::CtlPkt(uint32_t* payload1, int len) {
43  reset(payload1,len);
44 }
45 
52 CtlPkt::CtlPkt(CpType type1, CpMode mode1, uint64_t seqNum1,
53  uint32_t* payload1) {
54  reset(type1, mode1, seqNum1, payload1);
55 }
56 
57 CtlPkt::CtlPkt(CpType type1, CpMode mode1, uint64_t seqNum1) {
58  reset(type1, mode1, seqNum1);
59 }
60 
62 CtlPkt::~CtlPkt() { }
63 
70 void CtlPkt::reset(CpType type1, CpMode mode1, uint64_t seqNum1,
71  uint32_t* payload1) {
72  reset();
73  type = type1; mode = mode1; seqNum = seqNum1;
74  payload = payload1;
75 }
76 
77 void CtlPkt::reset(CpType type1, CpMode mode1, uint64_t seqNum1) {
78  reset();
79  type = type1; mode = mode1; seqNum = seqNum1;
80 }
81 
88 void CtlPkt::reset(uint32_t* payload1, int len) {
89  reset(); payload = payload1; paylen = len;
90 }
91 
96 void CtlPkt::reset(const Packet& p) {
97  reset();
98  payload = p.payload(); paylen = p.length-Forest::OVERHEAD; unpack();
99 }
100 
102  // initialize all fields to undefined values
103  type = UNDEF_CPTYPE; mode = UNDEF_MODE; seqNum = 0;
104  adr1 = adr2 = adr3 = 0;
105  ip1 = 0; ip2 = 0;
106  port1 = 0; port2 = 0;
107  nonce = 0;
108  rspec1.set(-1); rspec2.set(-1);
109  coreFlag = -1;
110  iface = 0; link = 0; nodeType = Forest::UNDEF_NODE;
111  comtree = 0; comtreeOwner = 0;
112  count = -1;
113  queue = 0;
114  zipCode = 0;
115  errMsg.clear();
116  payload = 0; paylen = 0;
117  lt = new LinkTable(100);
118  firstLinkNum = 0; numOfLinks = 0; nextLinkNum = 0;
119 }
120 
121 #define packPair(x,y) ((payload[pp++] = htonl(x)), (payload[pp++] = htonl(y)))
122 #define packNonce(x,y) ((payload[pp++] = htonl(x)), \
123  (payload[pp++] = htonl((int) (((y)>>32)&0xffffffff))), \
124  (payload[pp++] = htonl((int) ((y)&0xffffffff))))
125 #define packRspec(x,y) ((payload[pp++] = htonl(x)), \
126  (payload[pp++] = htonl(y.bitRateUp)), \
127  (payload[pp++] = htonl(y.bitRateDown)), \
128  (payload[pp++] = htonl(y.pktRateUp)), \
129  (payload[pp++] = htonl(y.pktRateDown)) \
130  )
131 
137  if (payload == 0) return 0;
138 
139  int pp = 0;
140  payload[pp++] = htonl(type);
141  payload[pp++] = htonl(mode);
142  payload[pp++] = htonl((uint32_t) (seqNum >> 32));
143  payload[pp++] = htonl((uint32_t) (seqNum & 0xffffffff));
144 
145  if (mode == NEG_REPLY) {
146  int len = errMsg.length();
147  if (len > 0) {
148  len = min(len,MAX_STRING);
149  payload[pp++] = htonl(ERRMSG);
150  payload[pp++] = htonl(len);
151  errMsg.copy((char*) &payload[pp], len);
152  }
153  paylen = 4*(pp + (len+3)/4);
154  return paylen;
155  }
156 
157  switch (type) {
158  case CLIENT_ADD_COMTREE:
159  if (mode == REQUEST) {
160  if (zipCode == 0) return 0;
161  packPair(ZIPCODE,zipCode);
162  } else {
163  packPair(COMTREE,comtree);
164  }
165  break;
166  case CLIENT_DROP_COMTREE:
167  if (mode == REQUEST) {
168  if (comtree == 0) return 0;
169  packPair(COMTREE,comtree);
170  }
171  break;
172  case CLIENT_GET_COMTREE:
173  if (mode == REQUEST) {
174  packPair(COMTREE,comtree);
175  } else {
176  if (comtree == 0 || comtreeOwner == 0||
177  !rspec1.isSet() || !rspec2.isSet())
178  return 0;
179  packPair(COMTREE,comtree);
180  packPair(COMTREE_OWNER,comtreeOwner);
181  packRspec(RSPEC1,rspec1);
182  packRspec(RSPEC2,rspec2);
183  }
184  break;
185  case CLIENT_MOD_COMTREE:
186  if (mode == REQUEST) {
187  if (comtree == 0) return 0;
188  packPair(COMTREE,comtree);
189  if (rspec1.isSet()) packRspec(RSPEC1,rspec1);
190  if (rspec2.isSet()) packRspec(RSPEC2,rspec2);
191  } else {
192  if (comtree == 0) return 0;
193  packPair(COMTREE,comtree);
194  }
195  break;
196  case CLIENT_JOIN_COMTREE:
197  if (mode == REQUEST) {
198  if (comtree == 0 || ip1 == 0 || port1 == 0)
199  return 0;
200  packPair(COMTREE,comtree);
201  packPair(IP1,ip1); packPair(PORT1,port1);
202  }
203  break;
204  case CLIENT_LEAVE_COMTREE:
205  if (mode == REQUEST) {
206  if (comtree == 0) return 0;
207  packPair(COMTREE,comtree);
208  packPair(IP1,ip1); packPair(PORT1,port1);
209  }
210  break;
211  case CLIENT_RESIZE_COMTREE:
212  if (mode == REQUEST) {
213  if (comtree == 0) return 0;
214  packPair(COMTREE,comtree);
215  }
216  break;
217  case CLIENT_GET_LEAF_RATE:
218  if (mode == REQUEST) {
219  if (comtree == 0 || adr1 == 0)
220  return 0;
221  packPair(COMTREE,comtree);
222  packPair(ADR1,adr1);
223  } else {
224  if (!rspec1.isSet()) return 0;
225  packRspec(RSPEC1,rspec1);
226  }
227  break;
228  case CLIENT_MOD_LEAF_RATE:
229  if (mode == REQUEST) {
230  if (comtree == 0 || adr1 == 0 ||
231  !rspec1.isSet())
232  return 0;
233  packPair(COMTREE,comtree);
234  packPair(ADR1,adr1);
235  packRspec(RSPEC1,rspec1);
236  }
237  break;
238  case ADD_IFACE:
239  if (mode == REQUEST) {
240  if (iface == 0 || ip1 == 0 || !rspec1.isSet())
241  return 0;
242  packPair(IFACE,iface);
243  packPair(IP1,ip1);
244  packRspec(RSPEC1,rspec1);
245  } else {
246  if (ip1 == 0 || port1 == 0) return 0;
247  packPair(IP1,ip1);
248  packPair(PORT1,port1);
249  }
250  break;
251  case DROP_IFACE:
252  if (mode == REQUEST) {
253  if (iface == 0) return 0;
254  packPair(IFACE,iface);
255  }
256  break;
257  case GET_IFACE:
258  if (mode == REQUEST) {
259  if (iface == 0) return 0;
260  packPair(IFACE,iface);
261  } else {
262  if (iface == 0 || ip1 == 0 ||
263  !rspec1.isSet() || !rspec2.isSet())
264  return 0;
265  packPair(IFACE,iface);
266  packPair(IP1,ip1);
267  packPair(PORT1,port1);
268  packRspec(RSPEC1,rspec1);
269  packRspec(RSPEC2,rspec2);
270  }
271  break;
272  case MOD_IFACE:
273  if (mode == REQUEST) {
274  if (iface == 0 || !rspec1.isSet())
275  return 0;
276  packPair(IFACE,iface);
277  packRspec(RSPEC1,rspec1);
278  }
279  break;
280  case ADD_LINK:
281  if (mode == REQUEST) {
282  if (nodeType == 0 || iface == 0) return 0;
283  packPair(NODE_TYPE,nodeType);
284  packPair(IFACE,iface);
285  if (link != 0) packPair(LINK,link);
286  if (ip1 != 0) packPair(IP1,ip1);
287  if (port1 != 0) packPair(PORT1,port1);
288  if (adr1 != 0) packPair(ADR1,adr1);
289  if (nonce != 0) packNonce(NONCE,nonce);
290  } else {
291  if (link != 0) packPair(LINK,link);
292  if (adr1 != 0) packPair(ADR1,adr1);
293  }
294  break;
295  case DROP_LINK:
296  if (mode == REQUEST) {
297  if (link == 0 && adr1 == 0) return 0;
298  if (link != 0) packPair(LINK,link);
299  if (adr1 != 0) packPair(ADR1,adr1);
300  }
301  break;
302  case GET_LINK:
303  if (mode == REQUEST) {
304  if (link == 0) return 0;
305  packPair(LINK,link);
306  } else {
307  if (link == 0 || iface == 0 || nodeType == 0 ||
308  ip1 == 0 || port1 == 0 || adr1 == 0 ||
309  !rspec1.isSet() || !rspec2.isSet())
310  return 0;
311  packPair(LINK,link);
312  packPair(IFACE,iface);
313  packPair(NODE_TYPE,nodeType);
314  packPair(IP1,ip1);
315  packPair(PORT1,port1);
316  packPair(ADR1,adr1);
317  packRspec(RSPEC1,rspec1);
318  packRspec(RSPEC2,rspec2);
319  }
320  break;
321  case MOD_LINK:
322  if (mode == REQUEST) {
323  if (link == 0 || !rspec1.isSet()) return 0;
324  packPair(LINK,link);
325  packRspec(RSPEC1,rspec1);
326  }
327  break;
328  case ADD_COMTREE:
329  if (mode == REQUEST) {
330  if (comtree == 0) return 0;
331  packPair(COMTREE,comtree);
332  }
333  break;
334  case DROP_COMTREE:
335  if (mode == REQUEST) {
336  if (comtree == 0) return 0;
337  packPair(COMTREE,comtree);
338  }
339  break;
340  case GET_COMTREE:
341  if (mode == REQUEST) {
342  if (comtree == 0) return 0;
343  packPair(COMTREE,comtree);
344  } else {
345  if (comtree == 0 || coreFlag == -1 ||
346  link == 0 || count == -1)
347  return 0;
348  packPair(COMTREE,comtree);
349  packPair(CORE_FLAG,coreFlag);
350  packPair(LINK,link);
351  packPair(count,count);
352  }
353  break;
354  case MOD_COMTREE:
355  if (mode == REQUEST) {
356  if (comtree == 0) return 0;
357  packPair(COMTREE,comtree);
358  if (coreFlag != -1) packPair(CORE_FLAG,coreFlag);
359  if (link != 0) packPair(LINK,link);
360  }
361  break;
362  case ADD_COMTREE_LINK:
363  if (mode == REQUEST) {
364  if (comtree == 0) return 0;
365  packPair(COMTREE,comtree);
366  if (link != 0) packPair(LINK,link);
367  if (coreFlag != -1) packPair(CORE_FLAG,coreFlag);
368  if (ip1 != 0) packPair(IP1,ip1);
369  if (port1 != 0) packPair(PORT1,port1);
370  if (adr1 != 0) packPair(ADR1,adr1);
371  } else {
372  if (link == 0) return 0;
373  packPair(LINK,link);
374  }
375  break;
376  case DROP_COMTREE_LINK:
377  if (mode == REQUEST) {
378  if (comtree == 0) return 0;
379  packPair(COMTREE,comtree);
380  if (link != 0) packPair(LINK,link);
381  if (ip1 != 0) packPair(IP1,ip1);
382  if (port1 != 0) packPair(PORT1,port1);
383  if (adr1 != 0) packPair(ADR1,adr1);
384  }
385  break;
386  case MOD_COMTREE_LINK:
387  if (mode == REQUEST) {
388  if (comtree == 0 || link == 0) return 0;
389  packPair(COMTREE,comtree);
390  packPair(LINK,link);
391  if (rspec1.isSet()) packRspec(RSPEC1,rspec1);
392  }
393  break;
394  case GET_COMTREE_LINK:
395  if (mode == REQUEST) {
396  if (comtree == 0 || link == 0) return 0;
397  packPair(COMTREE,comtree);
398  packPair(LINK,link);
399  } else {
400  if (comtree == 0 || link == 0 ||
401  !rspec1.isSet() || queue == 0 || adr1 == 0)
402  return 0;
403  packPair(COMTREE,comtree);
404  packPair(LINK,link);
405  packRspec(RSPEC1,rspec1);
406  packPair(QUEUE,queue);
407  packPair(ADR1,adr1);
408  }
409  break;
410  case ADD_ROUTE:
411  if (mode == REQUEST) {
412  if (comtree == 0 || adr1 == 0 || link == 0) return 0;
413  packPair(COMTREE,comtree);
414  packPair(ADR1,adr1);
415  packPair(LINK,link);
416  if (queue != 0) packPair(QUEUE,queue);
417  }
418  break;
419  case DROP_ROUTE:
420  if (mode == REQUEST) {
421  if (comtree == 0 || adr1 == 0) return 0;
422  packPair(COMTREE,comtree);
423  packPair(ADR1,adr1);
424  }
425  break;
426  case GET_ROUTE:
427  if (mode == REQUEST) {
428  if (comtree == 0 || adr1 == 0) return 0;
429  packPair(COMTREE,comtree);
430  packPair(ADR1,adr1);
431  } else {
432  if (comtree == 0 || adr1 == 0 || link == 0)
433  return 0;
434  packPair(COMTREE,comtree);
435  packPair(ADR1,adr1);
436  packPair(LINK,link);
437  }
438  break;
439  case MOD_ROUTE:
440  if (mode == REQUEST) {
441  if (comtree == 0 || adr1 == 0) return 0;
442  packPair(COMTREE,comtree);
443  packPair(ADR1,adr1);
444  if (link != 0) packPair(LINK,link);
445  if (queue != 0) packPair(QUEUE,queue);
446  }
447  break;
448  case ADD_ROUTE_LINK:
449  if (mode == REQUEST) {
450  if (comtree == 0 || adr1 == 0 || link == 0) return 0;
451  packPair(COMTREE,comtree);
452  packPair(ADR1,adr1);
453  packPair(LINK,link);
454  }
455  break;
456  case DROP_ROUTE_LINK:
457  if (mode == REQUEST) {
458  if (comtree == 0 || adr1 == 0 || link == 0) return 0;
459  packPair(COMTREE,comtree);
460  packPair(ADR1,adr1);
461  packPair(LINK,link);
462  }
463  break;
464  case NEW_SESSION:
465  if (mode == REQUEST) {
466  if (ip1 == 0 || !rspec1.isSet()) return 0;
467  packPair(IP1,ip1);
468  packRspec(RSPEC1,rspec1);
469  } else {
470  if (adr1 == 0 || adr2 == 0 || adr3 == 0 ||
471  ip1 == 0 || nonce == 0)
472  return 0;
473  packPair(ADR1,adr1);
474  packPair(ADR2,adr2);
475  packPair(ADR3,adr3);
476  packPair(IP1,ip1);
477  packPair(PORT1,port1);
478  packNonce(NONCE,nonce);
479  }
480  break;
481  case CANCEL_SESSION:
482  if (mode == REQUEST) {
483  if (adr1 == 0 || adr2 == 0) return 0;
484  packPair(ADR1,adr1); packPair(ADR2,adr2);
485  }
486  case CLIENT_CONNECT:
487  if (mode == REQUEST) {
488  if (adr1 == 0 || adr2 == 0) return 0;
489  packPair(ADR1,adr1);
490  packPair(ADR2,adr2);
491  }
492  break;
493  case CLIENT_DISCONNECT:
494  if (mode == REQUEST) {
495  if (adr1 == 0 || adr2 == 0) return 0;
496  packPair(ADR1,adr1);
497  packPair(ADR2,adr2);
498  }
499  break;
500  case CONFIG_LEAF:
501  if (mode == REQUEST) {
502  if (adr1 == 0 || adr2 == 0 || ip1 == 0 ||
503  port1 == 0 || nonce == 0) return 0;
504  packPair(ADR1,adr1); packPair(ADR2,adr2);
505  packPair(IP1,ip1); packPair(PORT1,port1);
506  packNonce(NONCE,nonce);
507  }
508  break;
509  case SET_LEAF_RANGE:
510  if (mode == REQUEST) {
511  if (adr1 == 0 || adr2 == 0) return 0;
512  packPair(ADR1,adr1); packPair(ADR2,adr2);
513  }
514  break;
515  case GET_LINK_SET: //Fend and Doowon
516  if (mode == POS_REPLY) { //POS_REPLY
517  int num = 0, i = 0;
518  payload[pp++] = htonl(CtlPkt::LINKTABLE);
519  pp++; //for the space for number of links.
520  (firstLinkNum == 1) ? i = lt->firstLink() : i = firstLinkNum; //1 means the first
521  cout << __FILE__ << " " << __FUNCTION__ << " REPLY - first link num " << firstLinkNum << " numOfLinks " << numOfLinks << endl;
522  while (true){
523  // for (i = lt->firstLink(); i != 0; i = lt->nextLink(i)) {
524  packPair(LINK, i);
525  packPair(IFACE, lt->getIface(i));
526  packPair(IP1, lt->getPeerIpAdr(i));
527  packPair(PORT1, lt->getPeerPort(i));
528  packPair(NODE_TYPE, lt->getPeerType(i));
529  packPair(ADR1, lt->getPeerAdr(i));
530  packRspec(RSPEC1, lt->getRates(i));
531  packNonce(NONCE, nonce);
532  payload[pp++] = htonl(CtlPkt::LINKSET);
533  string peerType;
534  Forest::nodeType2string(lt->getPeerType(i), peerType);
535  cout << __FUNCTION__ << " GET_LINK_SET " << peerType << endl;
536  i = lt->nextLink(i);
537  cout << __FUNCTION__ << " GET_LINK_SET " << i << " Num " << num << " numOfLinks " << numOfLinks << endl;
538  if ((++num >= numOfLinks) || i == 0){
539  packPair(NEXT_LINK_NUM, i);
540  cout << __FUNCTION__ << " next link num " << i << endl;
541  break;
542  }
543  }
544  payload[5] = htonl(num);
545  } else if (mode == REQUEST){ //packing first link # and # of links
546  packPair(FIRST_LINK_NUM, firstLinkNum);
547  packPair(NUM_OF_LINK, numOfLinks);
548  cout << "REQUEST -" << " first link num " << firstLinkNum << " numOfLinks " << numOfLinks << endl;
549  }
550 
551  case BOOT_ROUTER:
552  case BOOT_LEAF:
553  case BOOT_COMPLETE:
554  case BOOT_ABORT:
555  break;
556  default: break;
557  }
558  paylen = 4*pp;
559  return paylen;
560 }
561 
562 #define unpackWord(x) (x = ntohl(payload[pp++]))
563 #define unpackRspec(x) ((x).set(ntohl(payload[pp++]), \
564  ntohl(payload[pp++]), \
565  ntohl(payload[pp++]), \
566  ntohl(payload[pp++]) ))
567 
572  if (payload == 0) return false;
573 
574  int pp = 0;
575  uint32_t x, y;
576  unpackWord(x); type = (CpType) x;
577  unpackWord(x); mode = (CpMode) x;
578  unpackWord(x); unpackWord(y);
579  seqNum = x; seqNum <<= 32; seqNum |= y;
580 
581  if (mode == NEG_REPLY) {
582  if (paylen > 4*pp && ntohl(payload[pp]) == ERRMSG) {
583  pp++;
584  int len = ntohl(payload[pp++]);
585  errMsg.assign((char*) &payload[pp], len);
586  }
587  return true;
588  }
589  RateSpec Rates;
590  uint32_t attr = 0, numLinks = 0;
591  while (4*pp < paylen) {
592  unpackWord(attr);
593 
594  switch (attr) {
595  case ADR1: unpackWord(adr1); break;
596  case ADR2: unpackWord(adr2); break;
597  case ADR3: unpackWord(adr3); break;
598  case IP1: unpackWord(ip1); break;
599  case IP2: unpackWord(ip2); break;
600  case PORT1: unpackWord(port1); break;
601  case PORT2: unpackWord(port2); break;
602  case NONCE: uint32_t hi,lo; unpackWord(hi); unpackWord(lo);
603  nonce = hi; nonce <<= 32; nonce |= lo; break;
604  case RSPEC1: unpackRspec(rspec1); break;
605  case RSPEC2: unpackRspec(rspec2); break;
606  case CORE_FLAG: unpackWord(coreFlag); break;
607  case IFACE: unpackWord(iface); break;
608  case LINK: unpackWord(link); break;
609  case NODE_TYPE: nodeType =(Forest::ntyp_t) ntohl(payload[pp++]);
610  break;
611  case COMTREE: unpackWord(comtree); break;
612  case COMTREE_OWNER: unpackWord(comtreeOwner); break;
613  case COUNT: unpackWord(count); break;
614  case QUEUE: unpackWord(queue); break;
615  case ZIPCODE: unpackWord(zipCode); break;
616  case LINKTABLE: //Feng and Doowon
617  unpackWord(numLinks);
618  // lt = new LinkTable(numLinks);
619  // cout << "Num of Links " << numLinks << endl;
620  break;
621  case FIRST_LINK_NUM: unpackWord(firstLinkNum); break;
622  case NUM_OF_LINK: unpackWord(numOfLinks); break;
623  case NEXT_LINK_NUM: unpackWord(nextLinkNum); break;
624  case LINKSET: //Doowon
625  cout << __FUNCTION__ << " " << link << " " << ip1 << " " << port1 << endl;
626  lt->addEntry(link, ip1, port1, nonce);
627  lt->setIface(link, iface);
628  lt->setPeerType(link, nodeType);
629  lt->setPeerAdr(link, adr1);
630  lt->getRates(link).set(rspec1.bitRateUp, rspec1.bitRateDown, rspec1.pktRateUp, rspec1.pktRateDown);
631  cout << __FUNCTION__ << " " << lt << endl;
632  break;
633  default: return false;
634  }
635  }
636 
637  switch (type) {
638  case CLIENT_ADD_COMTREE:
639  if ((mode == REQUEST && zipCode == 0) ||
640  (mode == POS_REPLY && comtree == 0))
641  return false;
642  break;
643  case CLIENT_DROP_COMTREE:
644  if ((mode == REQUEST && comtree == 0))
645  return false;
646  break;
647  case CLIENT_GET_COMTREE:
648  if ((mode == REQUEST && comtree == 0) ||
649  (mode == POS_REPLY &&
650  (comtree == 0 || comtreeOwner == 0 ||
651  !rspec1.isSet() || !rspec2.isSet())))
652  return false;
653  break;
654  case CLIENT_MOD_COMTREE:
655  if ((mode == REQUEST &&
656  (!rspec1.isSet() || !rspec2.isSet())) ||
657  (mode == POS_REPLY && comtree == 0))
658  return false;
659  break;
660  case CLIENT_JOIN_COMTREE:
661  if (mode == REQUEST && (comtree == 0 || ip1 == 0 || port1 == 0))
662  return false;
663  break;
664  case CLIENT_LEAVE_COMTREE:
665  if (mode == REQUEST && (comtree == 0 || ip1 == 0 || port1 == 0))
666  return false;
667  break;
668  case CLIENT_RESIZE_COMTREE:
669  if (mode == REQUEST && comtree == 0)
670  return false;
671  break;
672  case CLIENT_GET_LEAF_RATE:
673  if ((mode == REQUEST &&
674  (comtree == 0 || adr1 == 0)) ||
675  (mode == POS_REPLY &&
676  (comtree == 0 || adr1 == 0 || !rspec1.isSet())))
677  return false;
678  break;
679  case CLIENT_MOD_LEAF_RATE:
680  if ((mode == REQUEST &&
681  (comtree == 0 || adr1 == 0 || !rspec1.isSet())))
682  return false;
683  break;
684  case ADD_IFACE:
685  if ((mode == REQUEST &&
686  (iface == 0 || ip1 == 0 || !rspec1.isSet())) ||
687  (mode == POS_REPLY && (ip1 == 0 || port1 == 0)))
688  return false;
689  break;
690  case DROP_IFACE:
691  if (mode == REQUEST && iface == 0)
692  return false;
693  break;
694  case GET_IFACE:
695  if ((mode == REQUEST && iface == 0) ||
696  (mode == POS_REPLY &&
697  (iface == 0 || ip1 == 0 || port1 == 0 ||
698  !rspec1.isSet() || !rspec2.isSet())))
699  return false;
700  break;
701  case MOD_IFACE:
702  if ((mode == REQUEST &&
703  (iface == 0 || !rspec1.isSet())))
704  return false;
705  break;
706  case ADD_LINK:
707  if (mode == REQUEST && (nodeType == 0 || iface == 0))
708  return false;
709  break;
710  case DROP_LINK:
711  if (mode == REQUEST && link == 0 && adr1 == 0)
712  return false;
713  break;
714  case GET_LINK:
715  if ((mode == REQUEST && link == 0) ||
716  (mode == POS_REPLY &&
717  (link == 0 || iface == 0 || nodeType == 0 ||
718  ip1 == 0 || port1 == 0 || adr1 == 0 ||
719  !rspec1.isSet() || !rspec2.isSet())))
720  return false;
721  break;
722  case MOD_LINK:
723  if (mode == REQUEST && link == 0)
724  return false;
725  break;
726  case ADD_COMTREE:
727  if (mode == REQUEST && comtree == 0)
728  return false;
729  break;
730  case DROP_COMTREE:
731  if (mode == REQUEST && comtree == 0)
732  return false;
733  break;
734  case GET_COMTREE:
735  if ((mode == REQUEST && comtree == 0) ||
736  (mode == POS_REPLY &&
737  (comtree == 0 || coreFlag == -1 || link == 0 ||
738  count == 0)))
739  return false;
740  break;
741  case MOD_COMTREE:
742  if (mode == REQUEST && comtree == 0)
743  return false;
744  break;
745  case ADD_COMTREE_LINK:
746  if (mode == REQUEST && (comtree == 0 ||
747  (link == 0 && (ip1 == 0 || port1 == 0)
748  && (adr1 == 0))))
749  return false;
750  break;
751  case DROP_COMTREE_LINK:
752  if (mode == REQUEST && (comtree == 0 ||
753  (link == 0 && (ip1 == 0 || port1 == 0)
754  && (adr1 == 0))))
755  return false;
756  break;
757  case MOD_COMTREE_LINK:
758  if (mode == REQUEST && (comtree == 0 || link == 0))
759  return false;
760  break;
761  case GET_COMTREE_LINK:
762  if ((mode == REQUEST &&
763  (comtree == 0 || link == 0)) ||
764  (mode == POS_REPLY &&
765  (comtree == 0 || link == 0 || !rspec1.isSet() ||
766  queue == 0 || adr1 == 0)))
767  return false;
768  break;
769  case ADD_ROUTE:
770  if (mode == REQUEST && (comtree == 0 || adr1 == 0 || link == 0))
771  return false;
772  break;
773  case DROP_ROUTE:
774  if (mode == REQUEST && (comtree == 0 || adr1 == 0))
775  return false;
776  break;
777  case GET_ROUTE:
778  if ((mode == REQUEST &&
779  (comtree == 0 || adr1 == 0)) ||
780  (mode == POS_REPLY &&
781  (comtree == 0 || adr1 == 0 || link == 0)))
782  return false;
783  break;
784  case MOD_ROUTE:
785  if (mode == REQUEST && (comtree == 0 || adr1 == 0))
786  return false;
787  break;
788  case ADD_ROUTE_LINK:
789  if (mode == REQUEST &&
790  (comtree == 0 || adr1 == 0 || link == 0))
791  return false;
792  break;
793  case DROP_ROUTE_LINK:
794  if (mode == REQUEST &&
795  (comtree == 0 || adr1 == 0 || link == 0))
796  return false;
797  break;
798  case NEW_SESSION:
799  if ((mode == REQUEST &&
800  (ip1 == 0 || !rspec1.isSet())) ||
801  (mode == POS_REPLY &&
802  (adr1 == 0 || adr2 == 0 || adr3 == 0 ||
803  ip1 == 0 || nonce == 0)))
804  return false;
805  break;
806  case CANCEL_SESSION:
807  if (mode == REQUEST && (adr1 == 0 || adr2 == 0))
808  return false;
809  break;
810  case CLIENT_CONNECT:
811  if (mode == REQUEST && (adr1 == 0 || adr2 == 0))
812  return false;
813  break;
814  case CLIENT_DISCONNECT:
815  if (mode == REQUEST && (adr1 == 0 || adr2 == 0))
816  return false;
817  break;
818  case CONFIG_LEAF:
819  if (mode == REQUEST &&
820  (adr1 == 0 || adr2 == 0 || ip1 == 0 || port1 == 0 ||
821  nonce == 0))
822  return false;
823  break;
824  case SET_LEAF_RANGE:
825  if (mode == REQUEST && (adr1 == 0 || adr2 == 0)) return false;
826  break;
827  //feng
828  case GET_LINK_SET:
829  case BOOT_ROUTER:
830  case BOOT_LEAF:
831  case BOOT_COMPLETE:
832  case BOOT_ABORT:
833  break;
834  default: return false;
835  }
836  return true;
837 }
838 
844 string& CtlPkt::avPair2string(CpAttr attr, string& s) {
845  stringstream ss;
846  switch (attr) {
847  case ADR1:
848  if (adr1 != 0) ss << "adr1=" << Forest::fAdr2string(adr1,s);
849  break;
850  case ADR2:
851  if (adr2 != 0) ss << "adr2=" << Forest::fAdr2string(adr2,s);
852  break;
853  case ADR3:
854  if (adr3 != 0) ss << "adr3=" << Forest::fAdr2string(adr3,s);
855  break;
856  case IP1:
857  if (ip1 != 0) ss << "ip1=" << Np4d::ip2string(ip1,s);
858  break;
859  case IP2:
860  if (ip2 != 0) ss << "ip2=" << Np4d::ip2string(ip2,s);
861  break;
862  case PORT1:
863  if (port1 != 0) ss << "port1=" << port1;
864  break;
865  case PORT2:
866  if (port2 != 0) ss << "port2=" << port2;
867  break;
868  case RSPEC1:
869  if (rspec1.isSet()) ss << "rspec1=" << rspec1.toString(s);
870  break;
871  case RSPEC2:
872  if (rspec2.isSet()) ss << "rspec2=" << rspec2.toString(s);
873  break;
874  case CORE_FLAG:
875  if (coreFlag >= 0)
876  ss << "coreFlag=" << (coreFlag ? "true" : "false");
877  break;
878  case IFACE:
879  if (iface != 0) ss << "iface=" << iface;
880  break;
881  case LINK:
882  if (link != 0) ss << "link=" << link;
883  break;
884  case NODE_TYPE:
885  if (nodeType != Forest::UNDEF_NODE)
886  ss << "nodeType="
887  << Forest::nodeType2string(nodeType,s);
888  break;
889  case COMTREE:
890  if (comtree != 0) ss << "comtree=" << comtree;
891  break;
892  case COMTREE_OWNER:
893  ss << "comtreeOwner=" << Forest::fAdr2string(comtreeOwner,s);
894  break;
895  case COUNT:
896  if (count >= 0) ss << "count=" << count;
897  break;
898  case QUEUE:
899  if (queue != 0) ss << "queue=" << queue;
900  break;
901  case NONCE:
902  if (nonce != 0) ss << "nonce=" << nonce;
903  break;
904  case ZIPCODE:
905  if (zipCode != 0) ss << "zipCode=" << zipCode;
906  break;
907  case ERRMSG:
908  if (errMsg.length() != 0) ss << "errMsg=" << errMsg;
909  break;
910  default: break;
911  }
912  s = ss.str();
913  return s;
914 }
915 
916 string& CtlPkt::typeName(string& s) {
917  switch (type) {
918  case CLIENT_ADD_COMTREE: s = "client add comtree"; break;
919  case CLIENT_DROP_COMTREE: s = "client drop comtree"; break;
920  case CLIENT_GET_COMTREE: s = "client get comtree"; break;
921  case CLIENT_MOD_COMTREE: s = "client mod comtree"; break;
922  case CLIENT_JOIN_COMTREE: s = "client join comtree"; break;
923  case CLIENT_LEAVE_COMTREE: s = "client leave comtree"; break;
924  case CLIENT_RESIZE_COMTREE: s = "client resize comtree"; break;
925  case CLIENT_GET_LEAF_RATE: s = "client get leaf rate"; break;
926  case CLIENT_MOD_LEAF_RATE: s = "client mod leaf rate"; break;
927  case ADD_IFACE: s = "add iface"; break;
928  case DROP_IFACE: s = "drop iface"; break;
929  case GET_IFACE: s = "get iface"; break;
930  case MOD_IFACE: s = "mod iface"; break;
931  case ADD_LINK: s = "add link"; break;
932  case DROP_LINK: s = "drop link"; break;
933  case GET_LINK: s = "get link"; break;
934  case MOD_LINK: s = "mod link"; break;
935  case ADD_COMTREE: s = "add comtree"; break;
936  case DROP_COMTREE: s = "drop comtree"; break;
937  case GET_COMTREE: s = "get comtree"; break;
938  case MOD_COMTREE: s = "mod comtree"; break;
939  case ADD_COMTREE_LINK: s = "add comtree link"; break;
940  case DROP_COMTREE_LINK: s = "drop comtree link"; break;
941  case MOD_COMTREE_LINK: s = "mod comtree link"; break;
942  case GET_COMTREE_LINK: s = "get comtree link"; break;
943  case ADD_ROUTE: s = "add route"; break;
944  case DROP_ROUTE: s = "drop route"; break;
945  case GET_ROUTE: s = "get route"; break;
946  case MOD_ROUTE: s = "mod route"; break;
947  case ADD_ROUTE_LINK: s = "add route link"; break;
948  case DROP_ROUTE_LINK: s = "drop route link"; break;
949  case NEW_SESSION: s = "new session"; break;
950  case CANCEL_SESSION: s = "cancel session"; break;
951  case CLIENT_CONNECT: s = "client connect"; break;
952  case CLIENT_DISCONNECT: s = "client disconnect"; break;
953  case CONFIG_LEAF: s = "config leaf"; break;
954  case SET_LEAF_RANGE: s = "set leaf range"; break;
955  case BOOT_ROUTER: s = "boot router"; break;
956  case BOOT_LEAF: s = "boot leaf"; break;
957  case BOOT_COMPLETE: s = "boot complete"; break;
958  case BOOT_ABORT: s = "boot abort"; break;
959  default: break;
960  }
961  return s;
962 }
963 
964 string& CtlPkt::modeName(string& s) {
965  switch (mode) {
966  case REQUEST: s = "request"; break;
967  case POS_REPLY: s = "pos reply"; break;
968  case NEG_REPLY: s = "neg reply"; break;
969  default: break;
970  }
971  return s;
972 }
973 
974 string& CtlPkt::toString(string& s) {
975  stringstream ss;
976 
977  //if (payload != 0) unpack();
978  ss << typeName(s);
979  ss << " (" << modeName(s) << "," << seqNum << "): ";
980  if (mode == NEG_REPLY) {
981  ss << errMsg << endl;
982  s = ss.str();
983  return s;
984  }
985  switch (type) {
986  case CLIENT_ADD_COMTREE:
987  if (mode == REQUEST) {
988  ss << " " << avPair2string(ZIPCODE,s);
989  } else {
990  ss << " " << avPair2string(COMTREE,s);
991  }
992  break;
993  case CLIENT_DROP_COMTREE:
994  if (mode == REQUEST) {
995  ss << " " << avPair2string(COMTREE,s);
996  }
997  break;
998  case CLIENT_GET_COMTREE:
999  if (mode == REQUEST) {
1000  ss << " " << avPair2string(COMTREE,s);
1001  } else {
1002  ss << " " << avPair2string(COMTREE,s);
1003  ss << " " << avPair2string(COMTREE_OWNER,s);
1004  ss << " " << avPair2string(RSPEC1,s);
1005  ss << " " << avPair2string(RSPEC2,s);
1006  }
1007  break;
1008  case CLIENT_MOD_COMTREE:
1009  if (mode == REQUEST) {
1010  ss << " " << avPair2string(COMTREE,s);
1011  ss << " " << avPair2string(RSPEC1,s);
1012  ss << " " << avPair2string(RSPEC2,s);
1013  } else {
1014  ss << " " << avPair2string(COMTREE,s);
1015  }
1016  break;
1017  case CLIENT_JOIN_COMTREE:
1018  if (mode == REQUEST) {
1019  ss << " " << avPair2string(COMTREE,s);
1020  ss << " " << avPair2string(IP1,s);
1021  ss << " " << avPair2string(PORT1,s);
1022  }
1023  break;
1024  case CLIENT_LEAVE_COMTREE:
1025  if (mode == REQUEST) {
1026  ss << " " << avPair2string(COMTREE,s);
1027  }
1028  break;
1029  case CLIENT_RESIZE_COMTREE:
1030  if (mode == REQUEST) {
1031  ss << " " << avPair2string(COMTREE,s);
1032  }
1033  break;
1034  case CLIENT_GET_LEAF_RATE:
1035  if (mode == REQUEST) {
1036  ss << " " << avPair2string(COMTREE,s);
1037  ss << " " << avPair2string(ADR1,s);
1038  } else {
1039  ss << " " << avPair2string(RSPEC1,s);
1040  }
1041  break;
1042  case CLIENT_MOD_LEAF_RATE:
1043  if (mode == REQUEST) {
1044  ss << " " << avPair2string(COMTREE,s);
1045  ss << " " << avPair2string(ADR1,s);
1046  ss << " " << avPair2string(RSPEC1,s);
1047  }
1048  break;
1049  case ADD_IFACE:
1050  if (mode == REQUEST) {
1051  ss << " " << avPair2string(IFACE,s);
1052  ss << " " << avPair2string(IP1,s);
1053  ss << " " << avPair2string(RSPEC1,s);
1054  } else {
1055  ss << " " << avPair2string(IP1,s);
1056  ss << " " << avPair2string(PORT1,s);
1057  }
1058  break;
1059  case DROP_IFACE:
1060  if (mode == REQUEST) {
1061  ss << " " << avPair2string(IFACE,s);
1062  }
1063  break;
1064  case GET_IFACE:
1065  if (mode == REQUEST) {
1066  ss << " " << avPair2string(IFACE,s);
1067  } else {
1068  ss << " " << avPair2string(IFACE,s);
1069  ss << " " << avPair2string(IP1,s);
1070  ss << " " << avPair2string(RSPEC1,s);
1071  ss << " " << avPair2string(RSPEC2,s);
1072  }
1073  break;
1074  case MOD_IFACE:
1075  if (mode == REQUEST) {
1076  ss << " " << avPair2string(IFACE,s);
1077  ss << " " << avPair2string(RSPEC1,s);
1078  }
1079  break;
1080  case ADD_LINK:
1081  if (mode == REQUEST) {
1082  ss << " " << avPair2string(IFACE,s);
1083  ss << " " << avPair2string(LINK,s);
1084  ss << " " << avPair2string(NODE_TYPE,s);
1085  ss << " " << avPair2string(IP1,s);
1086  ss << " " << avPair2string(PORT1,s);
1087  ss << " " << avPair2string(ADR1,s);
1088  ss << " " << avPair2string(NONCE,s);
1089  } else {
1090  ss << " " << avPair2string(LINK,s);
1091  ss << " " << avPair2string(ADR1,s);
1092  ss << " " << avPair2string(IP1,s);
1093  }
1094  break;
1095  case DROP_LINK:
1096  if (mode == REQUEST) {
1097  if (link != 0) ss << " " << avPair2string(LINK,s);
1098  if (adr1 != 0) ss << " " << avPair2string(ADR1,s);
1099  }
1100  break;
1101  case GET_LINK:
1102  if (mode == REQUEST) {
1103  ss << " " << avPair2string(LINK,s);
1104  } else {
1105  ss << " " << avPair2string(LINK,s);
1106  ss << " " << avPair2string(IFACE,s);
1107  ss << " " << avPair2string(NODE_TYPE,s);
1108  ss << " " << avPair2string(IP1,s);
1109  ss << " " << avPair2string(PORT1,s);
1110  ss << " " << avPair2string(ADR1,s);
1111  ss << " " << avPair2string(RSPEC1,s);
1112  ss << " " << avPair2string(RSPEC2,s);
1113  }
1114  break;
1115  case MOD_LINK:
1116  if (mode == REQUEST) {
1117  ss << " " << avPair2string(LINK,s);
1118  ss << " " << avPair2string(RSPEC1,s);
1119  }
1120  break;
1121  case ADD_COMTREE:
1122  if (mode == REQUEST) {
1123  ss << " " << avPair2string(COMTREE,s);
1124  }
1125  break;
1126  case DROP_COMTREE:
1127  if (mode == REQUEST) {
1128  ss << " " << avPair2string(COMTREE,s);
1129  }
1130  break;
1131  case GET_COMTREE:
1132  if (mode == REQUEST) {
1133  ss << " " << avPair2string(COMTREE,s);
1134  } else {
1135  ss << " " << avPair2string(COMTREE,s);
1136  ss << " " << avPair2string(CORE_FLAG,s);
1137  ss << " " << avPair2string(LINK,s);
1138  ss << " " << avPair2string(COUNT,s);
1139  }
1140  break;
1141  case MOD_COMTREE:
1142  if (mode == REQUEST) {
1143  ss << " " << avPair2string(COMTREE,s);
1144  ss << " " << avPair2string(CORE_FLAG,s);
1145  ss << " " << avPair2string(LINK,s);
1146  }
1147  break;
1148  case ADD_COMTREE_LINK:
1149  if (mode == REQUEST) {
1150  ss << " " << avPair2string(COMTREE,s);
1151  ss << " " << avPair2string(LINK,s);
1152  ss << " " << avPair2string(CORE_FLAG,s);
1153  ss << " " << avPair2string(IP1,s);
1154  ss << " " << avPair2string(PORT1,s);
1155  ss << " " << avPair2string(ADR1,s);
1156  } else {
1157  ss << " " << avPair2string(LINK,s);
1158  }
1159  break;
1160  case DROP_COMTREE_LINK:
1161  if (mode == REQUEST) {
1162  ss << " " << avPair2string(COMTREE,s);
1163  ss << " " << avPair2string(LINK,s);
1164  ss << " " << avPair2string(IP1,s);
1165  ss << " " << avPair2string(PORT1,s);
1166  ss << " " << avPair2string(ADR1,s);
1167  }
1168  break;
1169  case MOD_COMTREE_LINK:
1170  if (mode == REQUEST) {
1171  ss << " " << avPair2string(COMTREE,s);
1172  ss << " " << avPair2string(LINK,s);
1173  ss << " " << avPair2string(RSPEC1,s);
1174  }
1175  break;
1176  case GET_COMTREE_LINK:
1177  if (mode == REQUEST) {
1178  ss << " " << avPair2string(COMTREE,s);
1179  ss << " " << avPair2string(LINK,s);
1180  } else {
1181  ss << " " << avPair2string(COMTREE,s);
1182  ss << " " << avPair2string(LINK,s);
1183  ss << " " << avPair2string(RSPEC1,s);
1184  ss << " " << avPair2string(QUEUE,s);
1185  ss << " " << avPair2string(ADR1,s);
1186  }
1187  break;
1188  case ADD_ROUTE:
1189  if (mode == REQUEST) {
1190  ss << " " << avPair2string(COMTREE,s);
1191  ss << " " << avPair2string(ADR1,s);
1192  ss << " " << avPair2string(LINK,s);
1193  ss << " " << avPair2string(QUEUE,s);
1194  }
1195  break;
1196  case DROP_ROUTE:
1197  if (mode == REQUEST) {
1198  ss << " " << avPair2string(COMTREE,s);
1199  ss << " " << avPair2string(ADR1,s);
1200  }
1201  break;
1202  case GET_ROUTE:
1203  if (mode == REQUEST) {
1204  ss << " " << avPair2string(COMTREE,s);
1205  ss << " " << avPair2string(ADR1,s);
1206  } else {
1207  ss << " " << avPair2string(COMTREE,s);
1208  ss << " " << avPair2string(ADR1,s);
1209  ss << " " << avPair2string(LINK,s);
1210  }
1211  break;
1212  case MOD_ROUTE:
1213  if (mode == REQUEST) {
1214  ss << " " << avPair2string(COMTREE,s);
1215  ss << " " << avPair2string(ADR1,s);
1216  ss << " " << avPair2string(LINK,s);
1217  ss << " " << avPair2string(QUEUE,s);
1218  }
1219  break;
1220  case ADD_ROUTE_LINK:
1221  if (mode == REQUEST) {
1222  ss << " " << avPair2string(COMTREE,s);
1223  ss << " " << avPair2string(ADR1,s);
1224  ss << " " << avPair2string(LINK,s);
1225  }
1226  break;
1227  case DROP_ROUTE_LINK:
1228  if (mode == REQUEST) {
1229  ss << " " << avPair2string(COMTREE,s);
1230  ss << " " << avPair2string(ADR1,s);
1231  ss << " " << avPair2string(LINK,s);
1232  }
1233  break;
1234  case NEW_SESSION:
1235  if (mode == REQUEST) {
1236  ss << " " << avPair2string(IP1,s);
1237  ss << " " << avPair2string(RSPEC1,s);
1238  } else {
1239  ss << " " << avPair2string(ADR1,s);
1240  ss << " " << avPair2string(ADR2,s);
1241  ss << " " << avPair2string(ADR3,s);
1242  ss << " " << avPair2string(IP1,s);
1243  ss << " " << avPair2string(PORT1,s);
1244  ss << " " << avPair2string(NONCE,s);
1245  }
1246  break;
1247  case CANCEL_SESSION:
1248  if (mode == REQUEST) {
1249  ss << " " << avPair2string(ADR1,s);
1250  ss << " " << avPair2string(ADR2,s);
1251  }
1252  break;
1253  case CLIENT_CONNECT:
1254  if (mode == REQUEST) {
1255  ss << " " << avPair2string(ADR1,s);
1256  ss << " " << avPair2string(ADR2,s);
1257  }
1258  break;
1259  case CLIENT_DISCONNECT:
1260  if (mode == REQUEST) {
1261  ss << " " << avPair2string(ADR1,s);
1262  ss << " " << avPair2string(ADR2,s);
1263  }
1264  break;
1265  case CONFIG_LEAF:
1266  if (mode == REQUEST) {
1267  ss << " " << avPair2string(ADR1,s);
1268  ss << " " << avPair2string(ADR2,s);
1269  ss << " " << avPair2string(IP1,s);
1270  ss << " " << avPair2string(PORT1,s);
1271  ss << " " << avPair2string(NONCE,s);
1272  }
1273  break;
1274  case SET_LEAF_RANGE:
1275  if (mode == REQUEST) {
1276  ss << " " << avPair2string(ADR1,s);
1277  ss << " " << avPair2string(ADR2,s);
1278  }
1279  break;
1280  //feng
1281  case GET_LINK_SET:
1282  break;
1283  case BOOT_ROUTER:
1284  case BOOT_LEAF:
1285  case BOOT_COMPLETE:
1286  case BOOT_ABORT:
1287  break;
1288  default: break;
1289  }
1290  ss << endl;
1291  s = ss.str();
1292  return s;
1293 }
1294 
1295 } // ends namespace
1296