Chinaunix首页 | 论坛 | 博客
  • 博客访问: 328582
  • 博文数量: 49
  • 博客积分: 653
  • 博客等级: 上士
  • 技术积分: 646
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-01 22:43
文章分类

全部博文(49)

文章存档

2018年(1)

2017年(4)

2015年(1)

2014年(6)

2013年(8)

2012年(24)

2011年(5)

分类: LINUX

2012-08-14 20:52:30

转载自


点击(此处)折叠或打开

  1. /* -*- C++ -*- */

  2. //=============================================================================
  3. /**
  4.  * @file INET_Addr.h
  5.  *
  6.  * $Id: INET_Addr.h 78617 2007-06-27 20:40:19Z mesnier_p $
  7.  *
  8.  * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
  9.  */
  10. //=============================================================================

  11. #ifndef ACE_INET_ADDR_H
  12. #define ACE_INET_ADDR_H
  13. #include /**/ "ace/pre.h"

  14. #include "ace/Sock_Connect.h"

  15. #if !defined (ACE_LACKS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif /* ACE_LACKS_PRAGMA_ONCE */

  18. #include "ace/Addr.h"

  19. #if defined(ACE_VXWORKS)
  20.    // Needed to get INET_ADDR_LEN
  21. # include /**/ "inetLib.h"
  22. #endif /* ACE_VXWORKS */

  23. ACE_BEGIN_VERSIONED_NAMESPACE_DECL

  24. /**
  25.  * @class ACE_INET_Addr
  26.  *
  27.  * @brief Defines a C++ wrapper facade for the Internet domain address
  28.  * family format.
  29.  */
  30. class ACE_Export ACE_INET_Addr : public ACE_Addr
  31. {
  32. public:
  33.   // = Initialization methods.

  34.   /// Default constructor.
  35.   ACE_INET_Addr (void);
  36.     // 默认构造函数

  37.   /// Copy constructor.
  38.   ACE_INET_Addr (const ACE_INET_Addr &);
  39.   //拷贝构造函数
  40.     
  41.   /// Creates an ACE_INET_Addr from a sockaddr_in structure.
  42.   //使用参数@addr 初始化ACE_INET_Addr
  43.   // 参数@len 的大小为结构sockaddr_in的长度,len = sizeof(struct sockaddr_in)
  44.   ACE_INET_Addr (const sockaddr_in *addr, int len);

  45.   /// Creates an ACE_INET_Addr from a <port_number> and the remote
  46.   /// <host_name>. The port number is assumed to be in host byte order.
  47.   /// To set a port already in network byte order, please @see set().
  48.   /// Use address_family to select IPv6 (PF_INET6) vs. IPv4 (PF_INET).
  49.   // 使用@port_number 和host_name 构造一个ACE_INET_Addr对象
  50.   // @port_number应该为主机字节序,构造函数需要把它转换了网络字节序。
  51.   // 如果port_number为网络字节序,请使用 set成员函数或者set_port_number成员函数。
  52.   // @address_family 用于指定ACE_INET_Addr的地址类型PF_INET(IPv4)或者PF_INET6(IPv6)
  53.   ACE_INET_Addr (u_short port_number,
  54.                  const char host_name[],
  55.                  int address_family = AF_UNSPEC);

  56.   /**
  57.    * Initializes an ACE_INET_Addr from the <address>, which can be
  58.    * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
  59.    * "128.252.166.57:1234"). If there is no ':' in the <address> it
  60.    * is assumed to be a port number, with the IP address being
  61.    * INADDR_ANY.
  62.    使用参数@address初始化一个ACE_INET_Addr对象。
  63.    * address的格式为 ip-number:port-number 或者 port-number.
  64.    * ip-number 可以为点分号的IP地址,也可是是域名地址。
  65.    * 如果参数@address没有提供 ip-number,只提供port-number时,地址被初始化为INADDR_ANY
  66.    * port-number 为主机字节序。
  67.    */
  68.   explicit ACE_INET_Addr (const char address[],
  69.                           int address_family = AF_UNSPEC);

  70.   /**
  71.    * Creates an ACE_INET_Addr from a <port_number> and an Internet
  72.    * <ip_addr>. This method assumes that <port_number> and <ip_addr>
  73.    * are in host byte order. If you have addressing information in
  74.    * network byte order, @see set().
  75.    使用主机字节序的@port_number 和 @ip_addr初始化一个ACE_INET_Addr对象。
  76.    * 如port_number或者 ip_addr为网络字节序时,请使用 set成员函数。
  77.    */
  78.   explicit ACE_INET_Addr (u_short port_number,
  79.                           ACE_UINT32 ip_addr = INADDR_ANY);

  80.   /// Uses <getservbyname> to create an ACE_INET_Addr from a
  81.   /// <port_name>, the remote <host_name>, and the <protocol>.
  82.   ACE_INET_Addr (const char port_name[],
  83.                  const char host_name[],
  84.                  const char protocol[] = "tcp");

  85.   /**
  86.    * Uses <getservbyname> to create an ACE_INET_Addr from a
  87.    * <port_name>, an Internet <ip_addr>, and the <protocol>. This
  88.    * method assumes that <ip_addr> is in host byte order.
  89.    */
  90.   ACE_INET_Addr (const char port_name[],
  91.                  ACE_UINT32 ip_addr,
  92.                  const char protocol[] = "tcp");

  93. #if defined (ACE_HAS_WCHAR)
  94.   ACE_INET_Addr (u_short port_number,
  95.                  const wchar_t host_name[],
  96.                  int address_family = AF_UNSPEC);

  97.   explicit ACE_INET_Addr (const wchar_t address[],
  98.                           int address_family = AF_UNSPEC);

  99.   ACE_INET_Addr (const wchar_t port_name[],
  100.                  const wchar_t host_name[],
  101.                  const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));

  102.   ACE_INET_Addr (const wchar_t port_name[],
  103.                  ACE_UINT32 ip_addr,
  104.                  const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
  105. #endif /* ACE_HAS_WCHAR */

  106.   /// Default dtor.
  107.   ~ACE_INET_Addr (void);

  108.   // = Direct initialization methods.

  109.   // These methods are useful after the object has been constructed.

  110.   /// Initializes from another ACE_INET_Addr.
  111.   //使用一个已经存在的ACE_INET_Addr对象来初始化本对象。
  112.   int set (const ACE_INET_Addr &);

  113.   /**
  114.    * Initializes an ACE_INET_Addr from a <port_number> and the
  115.    * remote <host_name>. If <encode> is non-zero then <port_number> is
  116.    * converted into network byte order, otherwise it is assumed to be
  117.    * in network byte order already and are passed straight through.
  118.    * address_family can be used to select IPv4/IPv6 if the OS has
  119.    * IPv6 capability (ACE_HAS_IPV6 is defined). To specify IPv6, use
  120.    * the value AF_INET6. To specify IPv4, use AF_INET.
  121.    */
  122.   int set (u_short port_number,
  123.            const char host_name[],
  124.            int encode = 1,
  125.            int address_family = AF_UNSPEC);

  126.   /**
  127.    * Initializes an ACE_INET_Addr from a @a port_number and an Internet
  128.    * @a ip_addr. If @a encode is non-zero then the port number and IP address
  129.    * are converted into network byte order, otherwise they are assumed to be
  130.    * in network byte order already and are passed straight through.
  131.    *
  132.    * If <map> is non-zero and IPv6 support has been compiled in,
  133.    * then this address will be set to the IPv4-mapped IPv6 address of it.
  134.    */
  135.   int set (u_short port_number,
  136.            ACE_UINT32 ip_addr = INADDR_ANY,
  137.            int encode = 1,
  138.            int map = 0);

  139.   /// Uses <getservbyname> to initialize an ACE_INET_Addr from a
  140.   /// <port_name>, the remote <host_name>, and the <protocol>.
  141.   int set (const char port_name[],
  142.            const char host_name[],
  143.            const char protocol[] = "tcp");

  144.   /**
  145.    * Uses <getservbyname> to initialize an ACE_INET_Addr from a
  146.    * <port_name>, an <ip_addr>, and the <protocol>. This assumes that
  147.    * <ip_addr> is already in network byte order.
  148.    */
  149.   int set (const char port_name[],
  150.            ACE_UINT32 ip_addr,
  151.            const char protocol[] = "tcp");

  152.   /**
  153.    * Initializes an ACE_INET_Addr from the @a addr, which can be
  154.    * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
  155.    * "128.252.166.57:1234"). If there is no ':' in the <address> it
  156.    * is assumed to be a port number, with the IP address being
  157.    * INADDR_ANY.
  158.    */
  159.   int set (const char addr[], int address_family = AF_UNSPEC);

  160.   /// Creates an ACE_INET_Addr from a sockaddr_in structure.
  161.   int set (const sockaddr_in *,
  162.            int len);

  163. #if defined (ACE_HAS_WCHAR)
  164.   int set (u_short port_number,
  165.            const wchar_t host_name[],
  166.            int encode = 1,
  167.            int address_family = AF_UNSPEC);

  168.   int set (const wchar_t port_name[],
  169.            const wchar_t host_name[],
  170.            const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));

  171.   int set (const wchar_t port_name[],
  172.            ACE_UINT32 ip_addr,
  173.            const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));

  174.   int set (const wchar_t addr[], int address_family = AF_UNSPEC);
  175. #endif /* ACE_HAS_WCHAR */

  176.   /// Return a pointer to the underlying network address.
  177.   virtual void *get_addr (void) const;
  178.   int get_addr_size(void) const;

  179.   /// Set a pointer to the address.
  180.   virtual void set_addr (void *, int len);

  181.   /// Set a pointer to the address.
  182.   virtual void set_addr (void *, int len, int map);

  183.   /**
  184.    * Transform the current ACE_INET_Addr address into string format.
  185.    * If <ipaddr_format> is non-0 this produces "ip-number:port-number"
  186.    * (e.g., "128.252.166.57:1234"), whereas if <ipaddr_format> is 0
  187.    * this produces "ip-name:port-number" (e.g.,
  188.    * "tango.cs.wustl.edu:1234"). Returns -1 if the @a size of the
  189.    * <buffer> is too small, else 0.
  190.    */
  191.   virtual int addr_to_string (ACE_TCHAR buffer[],
  192.                               size_t size,
  193.                               int ipaddr_format = 1) const;

  194.   /**
  195.    * Initializes an ACE_INET_Addr from the @a address, which can be
  196.    * "ip-addr:port-number" (e.g., "tango.cs.wustl.edu:1234"),
  197.    * "ip-addr:port-name" (e.g., "tango.cs.wustl.edu:telnet"),
  198.    * "ip-number:port-number" (e.g., "128.252.166.57:1234"), or
  199.    * "ip-number:port-name" (e.g., "128.252.166.57:telnet"). If there
  200.    * is no ':' in the <address> it is assumed to be a port number,
  201.    * with the IP address being INADDR_ANY.
  202.    */
  203.   virtual int string_to_addr (const char address[],
  204.                               int address_family = AF_UNSPEC);

  205. #if defined (ACE_HAS_WCHAR)
  206.   /*
  207.   virtual int string_to_addr (const char address[]);
  208.   */
  209. #endif /* ACE_HAS_WCHAR */

  210.   /**
  211.    * Sets the port number without affecting the host name. If
  212.    * <encode> is enabled then <port_number> is converted into network
  213.    * byte order, otherwise it is assumed to be in network byte order
  214.    * already and are passed straight through.
  215.    */
  216.   void set_port_number (u_short,
  217.                         int encode = 1);

  218.   /**
  219.    * Sets the address without affecting the port number. If
  220.    * <encode> is enabled then <ip_addr> is converted into network
  221.    * byte order, otherwise it is assumed to be in network byte order
  222.    * already and are passed straight through. The size of the address
  223.    * is specified in the @a len parameter.
  224.    * If <map> is non-zero, IPv6 support has been compiled in, and
  225.    * <ip_addr> is an IPv4 address, then this address is set to the IPv4-mapped
  226.    * IPv6 address of it.
  227.    */
  228.   int set_address (const char *ip_addr,
  229.                    int len,
  230.                    int encode = 1,
  231.                    int map = 0);

  232. #if (defined (__linux__) || defined (ACE_WIN32)) && defined (ACE_HAS_IPV6)
  233.   /**
  234.    * Sets the interface that should be used for this address. This only has
  235.    * an effect when the address is link local, otherwise it does nothing.
  236.    */
  237.   int set_interface (const char *intf_name);
  238. #endif /* (__linux__ || ACE_WIN32) && ACE_HAS_IPV6 */

  239.   /// Return the port number, converting it into host byte-order.
  240.   u_short get_port_number (void) const;

  241.   /**
  242.    * Return the character representation of the name of the host,
  243.    * storing it in the <hostname> (which is assumed to be
  244.    * <hostnamelen> bytes long). This version is reentrant. If
  245.    * <hostnamelen> is greater than 0 then <hostname> will be
  246.    * NUL-terminated even if -1 is returned.
  247.    */
  248.   int get_host_name (char hostname[],
  249.                      size_t hostnamelen) const;

  250. #if defined (ACE_HAS_WCHAR)
  251.   int get_host_name (wchar_t hostname[],
  252.                      size_t hostnamelen) const;
  253. #endif /* ACE_HAS_WCHAR */

  254.   /**
  255.    * Return the character representation of the hostname. This
  256.    * version is non-reentrant since it returns a pointer to a static
  257.    * data area. You should therefore either (1) do a "deep copy" of
  258.    * the address returned by get_host_name(), e.g., using strdup() or
  259.    * (2) use the "reentrant" version of get_host_name() described
  260.    * above.
  261.    */
  262.   const char *get_host_name (void) const;

  263.   /**
  264.    * Return the "dotted decimal" Internet address representation of
  265.    * the hostname storing it in the @a addr (which is assumed to be
  266.    * <addr_size> bytes long). This version is reentrant.
  267.    */
  268.   const char *get_host_addr (char *addr, int addr_size) const;

  269.   /**
  270.    * Return the "dotted decimal" Internet address representation of
  271.    * the hostname. This version is non-reentrant since it returns a
  272.    * pointer to a static data area. You should therefore either
  273.    * (1) do a "deep copy" of the address returned by get_host_addr(), e.g.,
  274.    * using strdup() or (2) use the "reentrant" version of
  275.    * get_host_addr() described above.
  276.    */
  277.   const char *get_host_addr (void) const;

  278.   /// Return the 4-byte IP address, converting it into host byte
  279.   /// order.
  280.   ACE_UINT32 get_ip_address (void) const;

  281.   /// Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY.
  282.   bool is_any (void) const;

  283.   /// Return @c true if the IP address is IPv4/IPv6 loopback address.
  284.   bool is_loopback (void) const;

  285.   /// Return @c true if the IP address is IPv4/IPv6 multicast address.
  286.   bool is_multicast (void) const;

  287. #if defined (ACE_HAS_IPV6)
  288.   /// Return @c true if the IP address is IPv6 linklocal address.
  289.   bool is_linklocal (void) const;

  290.   /// Return @c true if the IP address is IPv4-mapped IPv6 address.
  291.   bool is_ipv4_mapped_ipv6 (void) const;

  292.   /// Return @c true if the IP address is IPv4-compatible IPv6 address.
  293.   bool is_ipv4_compat_ipv6 (void) const;
  294. #endif /* ACE_HAS_IPV6 */

  295.   /**
  296.    * Returns @c true if @c this is less than @a rhs. In this context,
  297.    * "less than" is defined in terms of IP address and TCP port
  298.    * number. This operator makes it possible to use @c ACE_INET_Addrs
  299.    * in STL maps.
  300.    */
  301.   bool operator < (const ACE_INET_Addr &rhs) const;

  302.   /// Compare two addresses for equality. The addresses are considered
  303.   /// equal if they contain the same IP address and port number.
  304.   bool operator == (const ACE_INET_Addr &SAP) const;

  305.   /// Compare two addresses for inequality.
  306.   bool operator != (const ACE_INET_Addr &SAP) const;

  307.   /// A variation of the equality operator, this method only compares the
  308.   /// IP address and ignores the port number.
  309.   bool is_ip_equal (const ACE_INET_Addr &SAP) const;

  310.   /// Computes and returns hash value.
  311.   virtual u_long hash (void) const;

  312.   /// Dump the state of an object.
  313.   void dump (void) const;

  314.   /// Declare the dynamic allocation hooks.
  315.   ACE_ALLOC_HOOK_DECLARE;

  316. private:
  317.   /// Insure that @a hostname is properly null-terminated.
  318.   int get_host_name_i (char hostname[], size_t hostnamelen) const;

  319.   // Methods to gain access to the actual address of
  320.   // the underlying internet address structure.
  321.   void *ip_addr_pointer (void) const;
  322.   int ip_addr_size (void) const;
  323.   int determine_type (void) const;

  324.   /// Initialize underlying inet_addr_ to default values
  325.   void reset (void);

  326.   /// Underlying representation.
  327.   /// This union uses the knowledge that the two structures share the
  328.   /// first member, sa_family (as all sockaddr structures do).
  329.   union
  330.   {
  331.     sockaddr_in in4_;
  332. #if defined (ACE_HAS_IPV6)
  333.     sockaddr_in6 in6_;
  334. #endif /* ACE_HAS_IPV6 */
  335.   } inet_addr_;

  336. #if defined (ACE_VXWORKS)
  337.   char buf_[INET_ADDR_LEN];
  338. #endif
  339. };

  340. ACE_END_VERSIONED_NAMESPACE_DECL

  341. #if defined (__ACE_INLINE__)
  342. #include "ace/INET_Addr.inl"
  343. #endif /* __ACE_INLINE__ */

  344. #include /**/ "ace/post.h"
  345. #endif /* ACE_INET_ADDR_H */


总结:

1.在set、set_port_number、set_address中都提供了一个参数@encode,此参数在构造函数的参数中并没有提供。因为也意味着,使用任何一个构造函数来创建一个ACE_INET_Addr对象时,其地址和端口必须是主机字节序的。如果地址或者端口为网络字节序时,必须转换为主机字节序才可以使用构造函数。或者使用默认的构造函数,在通过set、set_port_number、set_address等成员函数,指定encode参数为FALSE(0)时,也可以直接把网络字节序的地址和端口设置给ACE_INET_Addr对象。如果地址和端口是2中不同的字节序,需要分别使用set_address和set_port_number进行设置,否则就可以使用set函数一次性设置地址和端口。

2.比较2个ACE_INET_Addr对象的相等性。 根据情况使用不同的成员函数。

  地址和端口都要相等时,使用 ==进行比较。

  只比较地址时,忽略端口时。使用 is_ip_equal 成员函数

阅读(2307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~