xorp

iptuple.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2009 XORP, Inc.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/bgp/iptuple.hh,v 1.16 2008/10/02 21:56:16 bms Exp $
00021 
00022 #ifndef __BGP_IPTUPLE_HH__
00023 #define __BGP_IPTUPLE_HH__
00024 
00025 #include "bgp_module.h"
00026 #include "libxorp/xorp.h"
00027 #include "libxorp/xlog.h"
00028 #include "libxorp/ipvx.hh"
00029 
00030 class UnresolvableHost : public XorpReasonedException {
00031 public:
00032     UnresolvableHost(const char* file, size_t line, const string init_why = "")
00033     : XorpReasonedException("UnresolvableHost", file, line, init_why) {}
00034 };
00035 
00036 class AddressFamilyMismatch : public XorpReasonedException {
00037 public:
00038     AddressFamilyMismatch(const char* file, size_t line,
00039               const string init_why = "")
00040     : XorpReasonedException("AddressFamilyMismatch",
00041                 file, line, init_why) {}
00042 };
00043 
00059 class Iptuple {
00060 public:
00061     Iptuple();
00062     Iptuple(const char* local_dev, const char *local_addr, uint16_t local_port,
00063         const char *peer_addr, uint16_t peer_port)
00064     throw(UnresolvableHost,AddressFamilyMismatch);
00065 
00066     Iptuple(const Iptuple&);
00067     Iptuple& operator=(const Iptuple&);
00068     void copy(const Iptuple&);
00069 
00070     bool operator==(const Iptuple&) const;
00071 
00072     const struct sockaddr *get_local_socket(size_t& len) const;
00073     string get_local_addr() const;
00074     bool get_local_addr(IPv4& addr) const;
00075     bool get_local_addr(IPv6& addr) const;
00076     uint16_t get_local_port() const;
00077 
00078     const struct sockaddr *get_bind_socket(size_t& len) const;
00079 
00080     const struct sockaddr *get_peer_socket(size_t& len) const;
00081     string get_peer_addr() const;
00082     const string& get_local_interface() const { return _local_dev; }
00083     bool get_peer_addr(IPv4& addr) const;
00084     bool get_peer_addr(IPv6& addr) const;
00085     uint16_t get_peer_port() const;
00086 
00087     string str() const;
00088 private:
00089     void
00090     fill_address(const char *addr, uint16_t local_port,
00091          struct sockaddr_storage& ss, size_t& len,
00092          string& addr_numeric)
00093     throw(UnresolvableHost);
00094 
00095     string _local_dev; // The interface (device) name.  NOT IP.
00096     string _local_addr; // String representation only for debugging.  IP Address.
00097     string _peer_addr;  // String representation only for debugging.  IP Address
00098 
00099     // For listen().
00100     struct sockaddr_storage _local_sock;     // Local socket
00101     size_t          _local_sock_len; // Length of local socket
00102 
00103     // For bind() before connect.
00104     struct sockaddr_storage _bind_sock; // Bind socket
00105     size_t          _bind_sock_len; // Length of bind socket
00106 
00107     // For connect().
00108     struct sockaddr_storage _peer_sock; // Peer socket
00109     size_t          _peer_sock_len; // Length of peer socket
00110 
00111     /*
00112     ** The local address and the peer address are stored twice once in
00113     ** string format and one in IPvX format. Originally the addresses
00114     ** were stored only as strings as they are rarely used, however
00115     ** the policy code may repeatedly request the peer address in IPv4
00116     ** or IPv6 format so keep it in the internal format for
00117     ** performance reasons.
00118     */
00119     string _local_address;  // Local address in numeric form
00120     IPvX   _local_address_ipvx; // Local address in IPvX form
00121     string _peer_address;   // Peer address in numeric form
00122     IPvX   _peer_address_ipvx;  // Peer address in IPvX form
00123 
00124     /*
00125     ** Held in host byte order
00126     */
00127     uint16_t _local_port;   // Local port
00128     uint16_t _peer_port;    // Peer port
00129 };
00130 
00131 #endif // __BGP_IPTUPLE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations