xorp

vrrp_packet.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2008-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 //
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/vrrp/vrrp_packet.hh,v 1.5 2008/12/18 11:34:51 abittau Exp $
00022 
00023 #ifndef __VRRP_VRRP_PACKET_HH__
00024 #define __VRRP_VRRP_PACKET_HH__
00025 
00026 #include "libxorp/ipv4.hh"
00027 #include "libproto/packet.hh"
00028 
00029 typedef vector<uint8_t> PAYLOAD;
00030 
00034 struct VrrpHeader {
00035     enum Versions {
00036     VRRP_VERSION = 2
00037     };
00038     enum PktTypes {
00039     VRRP_TYPE_ADVERTISEMENT = 1
00040     };
00041     enum AuthTypes {
00042     VRRP_AUTH_NONE = 0
00043     };
00044 
00052     static VrrpHeader&        assign(uint8_t* data);
00053 
00060     static const VrrpHeader&  assign(const PAYLOAD& payload);
00061 
00069     uint32_t              finalize();
00070 
00076     void              add_ip(const IPv4& ip);
00077 
00084     IPv4              ip(unsigned index) const;
00085 
00086 #if __BYTE_ORDER == __BIG_ENDIAN
00087     uint8_t     vh_v:4;
00088     uint8_t     vh_type:4;
00089 #elif __BYTE_ORDER == __LITTLE_ENDIAN
00090     uint8_t     vh_type:4;
00091     uint8_t     vh_v:4;
00092 #else
00093 #error "Unknown endianness"
00094 #endif
00095     uint8_t     vh_vrid;
00096     uint8_t     vh_priority;
00097     uint8_t     vh_ipcount;
00098     uint8_t     vh_auth;
00099     uint8_t     vh_interval;
00100     uint16_t        vh_sum;
00101     struct in_addr  vh_addr[0];
00102 };
00103 
00107 struct VrrpAuth {
00108     uint8_t     va_data[8];
00109 };
00110 
00111 #define IP_HEADER_MIN_SIZE  20
00112 #define VRRP_MAX_PACKET_SIZE    (IP_HEADER_MIN_SIZE             \
00113                      + sizeof(VrrpHeader) + sizeof(VrrpAuth)    \
00114                  + sizeof(struct in_addr) * 255)
00115 
00119 class VrrpPacket {
00120 public:
00121     static const IPv4 mcast_group;
00122 
00123     VrrpPacket();
00124 
00130     void        set_source(const IPv4& ip);
00131 
00137     void        set_vrid(uint8_t vrid);
00138 
00144     void        set_priority(uint8_t priority);
00145 
00151     void        set_interval(uint8_t interval);
00152 
00156     void        clear_ips();
00157 
00163     void        add_ip(const IPv4& ip);
00164 
00169     void        finalize();
00170 
00176     const PAYLOAD&  data() const;
00177 
00183     uint32_t        size() const;
00184 
00190     void set_size(uint32_t size);
00191 
00197     template<class T> void set_ips(const T& ips)
00198     {
00199     clear_ips();
00200     for (typename T::const_iterator i = ips.begin(); i != ips.end(); ++i)
00201         add_ip(*i);
00202     }
00203 
00204 private:
00205     PAYLOAD     _data;
00206     IpHeader4Writer _ip;
00207     VrrpHeader&     _vrrp;
00208 };
00209 
00210 #endif // __VRRP_VRRP_PACKET_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations