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

通讯编程

开发平台:

Visual C++

  1. /*-*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (c) 1997 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  * This product includes software developed by the Computer Systems
  17.  * Engineering Group at Lawrence Berkeley Laboratory.
  18.  * 4. Neither the name of the University nor of the Laboratory may be used
  19.  *    to endorse or promote products derived from this software without
  20.  *    specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34. /* Ported from CMU/Monarch's code, nov'98 -Padma.*/
  35. #ifndef __arp_h__
  36. #define __arp_h__
  37. #include "scheduler.h"
  38. #include "delay.h"
  39. #include "lib/bsd-list.h"
  40. #ifndef EADDRNOTAVAIL
  41. #define EADDRNOTAVAIL 125
  42. #endif /* !EADDRNOTAVAIL */
  43. class LinkDelay;
  44. class ARPEntry;
  45. class ARPTable;
  46. class LL;
  47. class Mac;
  48. LIST_HEAD(ARPTable_List, ARPTable);
  49. LIST_HEAD(ARPEntry_List, ARPEntry);
  50. /* ======================================================================
  51.    Address Resolution (ARP) Header
  52.    ====================================================================== */
  53. #define ARPHRD_ETHER 1 /* ethernet hardware format */
  54. #define ARPOP_REQUEST 1 /* request to resolve address */
  55. #define ARPOP_REPLY 2 /* response to previous request */
  56. #define ARPOP_REVREQUEST 3 /* request protocol address */
  57. #define ARPOP_REVREPLY 4 /* response giving protocol address */
  58. #define ARPOP_INVREQUEST 8 /* request to identify peer */
  59. #define ARPOP_INVREPLY 9 /* response identifying peer */
  60. #define ARP_HDR_LEN 28
  61. struct hdr_arp {
  62. u_int16_t arp_hrd;
  63. u_int16_t arp_pro;
  64. u_int8_t arp_hln;
  65. u_int8_t arp_pln;
  66. u_int16_t arp_op;
  67. int arp_sha;
  68. u_int16_t pad1; // so offsets are correct
  69. nsaddr_t arp_spa;
  70. int arp_tha;
  71. u_int16_t pad2; // so offsets are correct
  72. nsaddr_t arp_tpa;
  73. // Header access methods
  74. static int offset_; // required by PacketHeaderManager
  75. inline static int& offset() { return offset_; }
  76. inline static hdr_arp* access(const Packet* p) {
  77. return (hdr_arp*) p->access(offset_);
  78. }
  79. };
  80. class ARPEntry {
  81. friend class ARPTable;
  82. public:
  83. ARPEntry(ARPEntry_List* head, nsaddr_t dst) {
  84. up_ = macaddr_ = count_ = 0;
  85. ipaddr_ = dst;
  86. hold_ = 0;
  87. LIST_INSERT_HEAD(head, this, arp_link_);
  88. }
  89. inline ARPEntry* nextarp() { return arp_link_.le_next; }
  90. private:
  91. LIST_ENTRY(ARPEntry) arp_link_;
  92. int up_;
  93. nsaddr_t ipaddr_;
  94. int macaddr_;
  95. Packet *hold_;
  96. int count_;
  97. #define ARP_MAX_REQUEST_COUNT   3
  98. };
  99. class ARPTable : public LinkDelay {
  100. public:
  101. ARPTable(const char *tclnode, const char *tclmac);
  102.         int     command(int argc, const char*const* argv);
  103. int     arpresolve(nsaddr_t dst, Packet *p, LL *ll);
  104. void    arpinput(Packet *p, LL *ll);
  105. ARPEntry* arplookup(nsaddr_t dst);
  106. void arprequest(nsaddr_t src, nsaddr_t dst, LL *ll);
  107. void Terminate(void);
  108. private:
  109. inline int initialized() { return node_ && mac_; }
  110. ARPEntry_List arphead_;
  111. MobileNode *node_;
  112. Mac *mac_;
  113. /*
  114.  * Used to purge all of the ll->hold packets at the end of the
  115.  * simulation.
  116.  */
  117. public:
  118. LIST_ENTRY(ARPTable) link_;
  119. static ARPTable_List athead_;
  120. };
  121. #endif /* __arp_h__ */