PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/networklayer/xmipv6/BindingUpdateList.cc

https://github.com/zarrar/xMIPv6
C++ | 454 lines | 285 code | 109 blank | 60 comment | 50 complexity | 42c07ab42aad29af0c080ecf734d0cdf MD5 | raw file
  1. /**
  2. * Copyright (C) 2007
  3. * Faqir Zarrar Yousaf
  4. * Communication Networks Institute, Technical University Dortmund (TU Dortmund), Germany.
  5. * Christian Bauer
  6. * Institute of Communications and Navigation, German Aerospace Center (DLR)
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <algorithm>
  22. #include "opp_utils.h"
  23. #include "BindingUpdateList.h"
  24. #include "IPv6InterfaceData.h"
  25. // secret key used in RR by CN
  26. #define KCN 1
  27. Define_Module(BindingUpdateList);
  28. std::ostream& operator<<(std::ostream& os, const BindingUpdateList::BindingUpdateListEntry& bul)
  29. {
  30. os << "Destination: " <<bul.destAddress <<" HoA of MN: " << bul.homeAddress <<" CoA of MN: "<<bul.careOfAddress <<"\n"
  31. << "Binding Lifetime: "<<bul.bindingLifetime << " binding expiry: "<<SIMTIME_STR(bul.bindingExpiry) <<" BU Sequence#: "<<bul.sequenceNumber
  32. << " Sent Time: "<<SIMTIME_STR(bul.sentTime) <</*" Next_Tx_Time: "<<bul.nextBUTx <<*/" BU_Ack: "<<bul.BAck <<"\n";
  33. // this part will only be displayed if the BUL entry is for CN registration
  34. if ( bul.sentHoTI != 0 )
  35. {
  36. os << "Sent Time HoTI: " << SIMTIME_STR(bul.sentHoTI) << " HoTI cookie: " << bul.cookieHoTI
  37. << " home token: " << bul.tokenH << "\n";
  38. }
  39. if ( bul.sentCoTI != 0 )
  40. {
  41. os << " Sent Time CoTI: " << SIMTIME_STR(bul.sentCoTI) << " CoTI cookie: " << bul.cookieCoTI
  42. << " care-of token: " << bul.tokenC << "\n";
  43. }
  44. os << "State: ";
  45. switch (bul.state)
  46. {
  47. case BindingUpdateList::NONE:
  48. os << "none";
  49. break;
  50. case BindingUpdateList::RR:
  51. os << "Return Routability";
  52. break;
  53. case BindingUpdateList::RR_COMPLETE:
  54. os << "Return Routability completed";
  55. break;
  56. case BindingUpdateList::REGISTER:
  57. os << "Registering";
  58. break;
  59. case BindingUpdateList::REGISTERED:
  60. os << "Registered";
  61. break;
  62. case BindingUpdateList::DEREGISTER:
  63. os << "Deregistering";
  64. break;
  65. default:
  66. os << "Unknown";
  67. break;
  68. }
  69. os << endl;
  70. return os;
  71. }
  72. BindingUpdateList::BindingUpdateList()
  73. {
  74. }
  75. BindingUpdateList::~BindingUpdateList()
  76. {
  77. // for (unsigned int i=0; i<bindingUpdateList.size(); i++)
  78. // delete bindingUpdateList[i];
  79. }
  80. void BindingUpdateList::initialize(int stage)
  81. {
  82. if (stage==1)
  83. {
  84. WATCH_MAP(bindingUpdateList); //added by Zarrar Yousaf
  85. }
  86. }
  87. void BindingUpdateList::handleMessage(cMessage *msg)
  88. {
  89. opp_error("This module doesn't process messages");
  90. }
  91. void BindingUpdateList::addOrUpdateBUL(const IPv6Address& dest, const IPv6Address& hoa, const IPv6Address& coa, const uint lifetime, const uint seq, const simtime_t buSentTime)//,const simtime_t& nextBUSentTime)
  92. {
  93. // modified structure - CB
  94. // search for entry
  95. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  96. // if it is not yet existing, create it
  97. if (entry == NULL)
  98. {
  99. /*bindingUpdateList[dest].destAddress = dest;
  100. entry = & bindingUpdateList[dest];
  101. initializeBUValues(*entry);*/
  102. entry = createBULEntry(dest);
  103. }
  104. EV<<"\n++++++++++++++++++++Binding Update List Being Updated in Routing Table6 ++++++++++++++\n";
  105. entry->homeAddress = hoa;
  106. entry->careOfAddress = coa;
  107. // update lifetime 11.06.08 - CB
  108. entry->bindingLifetime = lifetime; // for the moment a constant but then it is supposed to decrement with time.
  109. entry->bindingExpiry = simTime() + lifetime; // binding expires at this point in time
  110. //TODO bindingUpdateList[dest].remainingLifetime = ;
  111. entry->sentTime = buSentTime; //the time at which the BU, whose ack is awaited is sent
  112. //entry->nextBUTx = nextBUSentTime; //the nextScgheduledTime at which the BU will be sent in case of timeout.
  113. entry->sequenceNumber = seq; //seq number of the last BU sent.
  114. entry->BAck = false;
  115. }
  116. BindingUpdateList::BindingUpdateListEntry* BindingUpdateList::createBULEntry(const IPv6Address& dest)
  117. {
  118. bindingUpdateList[dest].destAddress = dest;
  119. BindingUpdateListEntry& entry = bindingUpdateList[dest];
  120. //BindingUpdateList::BindingUpdateListEntry* entry = & bindingUpdateList[dest];
  121. initializeBUValues(entry);
  122. return &entry;
  123. }
  124. void BindingUpdateList::initializeBUValues(BindingUpdateListEntry& entry)
  125. {
  126. // normal BU values
  127. entry.bindingLifetime = 0;
  128. entry.bindingExpiry = 0;
  129. //TODO bindingUpdateList[dest].remainingLifetime = ;
  130. entry.sentTime = 0;
  131. //entry.nextBUTx = 0;
  132. entry.sequenceNumber = 0;
  133. entry.BAck = false;
  134. // RR specific values
  135. entry.sentHoTI = 0;
  136. entry.sentCoTI = 0;
  137. entry.cookieHoTI = UNDEFINED_COOKIE;
  138. entry.cookieCoTI = UNDEFINED_COOKIE;
  139. //entry.sendNext = 0;
  140. entry.tokenH = UNDEFINED_TOKEN;
  141. entry.tokenC = UNDEFINED_TOKEN;
  142. // 21.07.08 - CB
  143. entry.state = NONE;
  144. }
  145. void BindingUpdateList::addOrUpdateBUL(const IPv6Address& dest, const IPv6Address& addr, const simtime_t sentTime, const int cookie, bool HoTI = false)
  146. {
  147. EV<<"\n++++++++++++++++++++Binding Update List for HoTI/CoTI Being Updated in Routing Table6 ++++++++++++++\n";
  148. // search for entry
  149. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  150. // if it is not yet existing, create it
  151. if (entry == NULL)
  152. {
  153. bindingUpdateList[dest].destAddress = dest;
  154. entry = & bindingUpdateList[dest];
  155. initializeBUValues(*entry);
  156. }
  157. if (HoTI) // those values are from the HoTI message
  158. {
  159. entry->homeAddress = addr;
  160. entry->sentHoTI = sentTime;
  161. entry->cookieHoTI = cookie;
  162. }
  163. else // and those from the CoTI
  164. {
  165. entry->careOfAddress = addr;
  166. entry->sentCoTI = sentTime;
  167. entry->cookieCoTI = cookie;
  168. }
  169. }
  170. BindingUpdateList::BindingUpdateListEntry* BindingUpdateList::lookup(const IPv6Address& dest)
  171. {
  172. BindingUpdateList6::iterator i = bindingUpdateList.find(dest);
  173. return ( i == bindingUpdateList.end() ) ? NULL : &(i->second);
  174. }
  175. BindingUpdateList::BindingUpdateListEntry* BindingUpdateList::fetch(const IPv6Address& dest)
  176. {
  177. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  178. if (entry == NULL)
  179. return createBULEntry(dest);
  180. else
  181. return entry;
  182. }
  183. BindingUpdateList::MobilityState BindingUpdateList::getMobilityState(const IPv6Address& dest)
  184. {
  185. BindingUpdateList6::iterator i = bindingUpdateList.find(dest);
  186. if ( i == bindingUpdateList.end() )
  187. return NONE;
  188. else
  189. return i->second.state;
  190. }
  191. void BindingUpdateList::setMobilityState(const IPv6Address& dest, BindingUpdateList::MobilityState state)
  192. {
  193. BindingUpdateList6::iterator i = bindingUpdateList.find(dest);
  194. if ( i != bindingUpdateList.end() )
  195. i->second.state = state;
  196. }
  197. int BindingUpdateList::generateBAuthData(const IPv6Address& dest, const IPv6Address& CoA)
  198. {
  199. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  200. if ( entry == NULL )
  201. {
  202. EV << "Impossible to generate Binding Authorization Data as CN is not existing in BUL!\n";
  203. return 0;
  204. }
  205. // generate the key
  206. return BindingUpdateList::generateKey(entry->tokenH, entry->tokenC, CoA);
  207. }
  208. int BindingUpdateList::generateKey(int homeToken, int careOfToken, const IPv6Address& CoA)
  209. {
  210. // use a dummy value
  211. return homeToken+careOfToken;
  212. }
  213. int BindingUpdateList::generateHomeToken(const IPv6Address& HoA, int nonce)
  214. {
  215. return HO_TOKEN;
  216. }
  217. int BindingUpdateList::generateCareOfToken(const IPv6Address& CoA, int nonce)
  218. {
  219. return CO_TOKEN;
  220. }
  221. void BindingUpdateList::resetHomeToken(const IPv6Address& dest, const IPv6Address& hoa)
  222. {
  223. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  224. ASSERT(entry != NULL);
  225. entry->tokenH = UNDEFINED_TOKEN;
  226. //entry->sentHoTI = 0;
  227. }
  228. void BindingUpdateList::resetCareOfToken(const IPv6Address& dest, const IPv6Address& hoa)
  229. {
  230. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  231. ASSERT(entry != NULL);
  232. entry->tokenC = UNDEFINED_TOKEN;
  233. //entry->sentCoTI = 0;
  234. }
  235. bool BindingUpdateList::isHomeTokenAvailable(const IPv6Address& dest, InterfaceEntry* ie)
  236. {
  237. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  238. ASSERT(entry != NULL);
  239. return entry->tokenH != UNDEFINED_TOKEN &&
  240. (entry->sentHoTI + ie->ipv6Data()->_maxTokenLifeTime()) > simTime();
  241. }
  242. bool BindingUpdateList::isCareOfTokenAvailable(const IPv6Address& dest, InterfaceEntry* ie)
  243. {
  244. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  245. ASSERT(entry != NULL);
  246. return entry->tokenC != UNDEFINED_TOKEN &&
  247. (entry->sentCoTI + ie->ipv6Data()->_maxTokenLifeTime()) > simTime();
  248. }
  249. bool BindingUpdateList::isInBindingUpdateList(const IPv6Address& dest)
  250. {
  251. return bindingUpdateList.find(dest) != bindingUpdateList.end();
  252. }
  253. uint BindingUpdateList::getSequenceNumber(const IPv6Address& dest)
  254. {
  255. // search for entry
  256. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  257. if (entry == NULL)
  258. return 0;
  259. else
  260. return entry->sequenceNumber;
  261. }
  262. const IPv6Address& BindingUpdateList::getCoA(const IPv6Address& dest)
  263. {
  264. // search for entry
  265. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  266. ASSERT(entry!=NULL);
  267. return entry->careOfAddress;
  268. }
  269. bool BindingUpdateList::isInBindingUpdateList(const IPv6Address& dest, const IPv6Address& HoA)
  270. {
  271. BindingUpdateList6::iterator pos = bindingUpdateList.find(dest);
  272. if ( pos == bindingUpdateList.end() )
  273. return false;
  274. else
  275. return pos->second.homeAddress == HoA;
  276. }
  277. bool BindingUpdateList::isValidBinding(const IPv6Address& dest)
  278. {
  279. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  280. if (entry == NULL)
  281. return false;
  282. return entry->BAck && ( entry->bindingLifetime < SIMTIME_DBL(simTime()) );
  283. }
  284. bool BindingUpdateList::isBindingAboutToExpire(const IPv6Address& dest)
  285. {
  286. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  287. if (entry == NULL)
  288. return true;
  289. return entry->bindingLifetime < SIMTIME_DBL(simTime()) - PRE_BINDING_EXPIRY;
  290. }
  291. bool BindingUpdateList::sentBindingUpdate(const IPv6Address& dest)
  292. {
  293. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  294. if (entry == NULL)
  295. return false;
  296. return (entry->BAck || (entry->tokenH != UNDEFINED_TOKEN && entry->tokenC != UNDEFINED_TOKEN) )
  297. && entry->sentTime != 0;
  298. }
  299. void BindingUpdateList::removeBinding(const IPv6Address& dest)
  300. {
  301. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  302. ASSERT(entry!=NULL);
  303. if ( (entry->tokenH != UNDEFINED_TOKEN) || (entry->tokenC != UNDEFINED_TOKEN) )
  304. // for CNs, we just delete all entries
  305. resetBindingCacheEntry(*entry);
  306. else
  307. // the BUL entry to the HA is completely deleted
  308. bindingUpdateList.erase(dest);
  309. }
  310. void BindingUpdateList::suspendBinding(const IPv6Address& dest)
  311. {
  312. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  313. ASSERT(entry!=NULL);
  314. entry->BAck = false;
  315. }
  316. bool BindingUpdateList::recentlySentCOTI(const IPv6Address& dest, InterfaceEntry* ie)
  317. {
  318. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  319. ASSERT(entry!=NULL);
  320. return entry->sentCoTI + ie->ipv6Data()->_maxTokenLifeTime() / 3 > simTime();
  321. }
  322. bool BindingUpdateList::recentlySentHOTI(const IPv6Address& dest, InterfaceEntry* ie)
  323. {
  324. BindingUpdateList::BindingUpdateListEntry* entry = lookup(dest);
  325. ASSERT(entry!=NULL);
  326. return entry->sentHoTI + ie->ipv6Data()->_maxTokenLifeTime() / 3 > simTime();
  327. }
  328. void BindingUpdateList::resetBindingCacheEntry(BindingUpdateListEntry& entry)
  329. {
  330. entry.bindingLifetime = 0;
  331. entry.bindingExpiry = 0;
  332. //entry.remainingLifetime = 0;
  333. //entry.sequenceNumber = 0;
  334. entry.sentTime = 0;
  335. //entry.nextBUTx = 0;
  336. entry.BAck = false;
  337. entry.state = NONE;
  338. // if tokens should sustain handovers then comment out the following lines of code
  339. // (this could eventually allow for parallel CN and HA registration)
  340. /*entry.sentHoTI = 0;
  341. entry.sentCoTI = 0;
  342. entry.tokenH = 0;
  343. entry.tokenC = 0;*/
  344. }