rttable.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* The AODV code developed by the CMU/MONARCH group was optimized
  2.  * and tuned by Samir Das (UTSA) and Mahesh Marina (UTSA). The 
  3.  * work was partially done in Sun Microsystems.
  4.  * 
  5.  * The original CMU copyright is below. 
  6.  */
  7. /*
  8. Copyright (c) 1997, 1998 Carnegie Mellon University.  All Rights
  9. Reserved. 
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. 3. The name of the author may not be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /* ported into VINT ns by Ya Xu, Sept. 1999 */
  31. #include <rttable.h>
  32. /* =====================================================================
  33.    The Routing Table
  34.    ===================================================================== */
  35. rt_entry::rt_entry()
  36. {
  37. int i;
  38.         rt_dst = 0;
  39.         rt_seqno = 0;
  40.         rt_nexthop = 0;
  41.         rt_expire = 0.0;
  42.         rt_hops = INFINITY2;
  43.         rt_flags = RTF_DOWN;
  44.         rt_errors = 0;
  45.         rt_error_time = 0.0;
  46.         rt_req_timeout = 0.0;
  47.         rt_req_cnt = 0;
  48. rt_req_last_ttl = 0;
  49. hist_indx = 0;
  50. for (i=0; i < MAX_HISTORY; i++) {
  51. // rt_length[i] = 0;
  52. rt_disc_latency[i] = 0.0;
  53. }
  54. error_propagate_counter = 0;
  55.         LIST_INIT(&rt_nblist);
  56. }
  57. rt_entry::~rt_entry()
  58. {
  59.         Neighbor *nb;
  60.         while((nb = rt_nblist.lh_first)) {
  61.                 LIST_REMOVE(nb, nb_link);
  62.                 delete nb;
  63.         }
  64. }
  65. void
  66. rt_entry::nb_insert(nsaddr_t id)
  67. {
  68.         Neighbor *nb = new Neighbor(id);
  69.         assert(nb);
  70.         nb->nb_expire = 0;
  71.         LIST_INSERT_HEAD(&rt_nblist, nb, nb_link);
  72. }
  73. Neighbor*
  74. rt_entry::nb_lookup(nsaddr_t id)
  75. {
  76.         Neighbor *nb = rt_nblist.lh_first;
  77.         for(; nb; nb = nb->nb_link.le_next) {
  78.                 if(nb->nb_addr == id)
  79.                         break;
  80.         }
  81.         return nb;
  82. }
  83. /* =====================================================================
  84.    The Routing Table
  85.    ===================================================================== */
  86. rt_entry*
  87. rttable::rt_lookup(nsaddr_t id)
  88. {
  89.         rt_entry *rt = rthead.lh_first;
  90.         for(; rt; rt = rt->rt_link.le_next) {
  91.                 if(rt->rt_dst == id)
  92.                         break;
  93.         }
  94.         return rt;
  95. }
  96. void
  97. rttable::rt_delete(nsaddr_t id)
  98. {
  99.         rt_entry *rt = rt_lookup(id);
  100.         if(rt) {
  101.                 LIST_REMOVE(rt, rt_link);
  102.                 delete rt;
  103.         }
  104. }
  105. rt_entry*
  106. rttable::rt_add(nsaddr_t id)
  107. {
  108.         rt_entry *rt;
  109.         assert(rt_lookup(id) == 0);
  110.         rt = new rt_entry;
  111.         assert(rt);
  112.         rt->rt_dst = id;
  113.         LIST_INSERT_HEAD(&rthead, rt, rt_link);
  114.         return rt;
  115. }