InetAddress.h revision bfe73648
1bfe73648SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
2bfe73648SShuo Chen// http://code.google.com/p/muduo/
3bfe73648SShuo Chen//
4bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license
5bfe73648SShuo Chen// that can be found in the License file.
6bfe73648SShuo Chen
7bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
8bfe73648SShuo Chen//
9bfe73648SShuo Chen// This is a public header file, it must only include public header files.
10bfe73648SShuo Chen
11bfe73648SShuo Chen#ifndef MUDUO_NET_INETADDRESS_H
12bfe73648SShuo Chen#define MUDUO_NET_INETADDRESS_H
13bfe73648SShuo Chen
14bfe73648SShuo Chen#include "datetime/copyable.h"
15bfe73648SShuo Chen
16bfe73648SShuo Chen#include <string>
17bfe73648SShuo Chen
18bfe73648SShuo Chen#include <netinet/in.h>
19bfe73648SShuo Chen
20bfe73648SShuo Chennamespace muduo
21bfe73648SShuo Chen{
22bfe73648SShuo Chen
23bfe73648SShuo Chen///
24bfe73648SShuo Chen/// Wrapper of sockaddr_in.
25bfe73648SShuo Chen///
26bfe73648SShuo Chen/// This is an POD interface class.
27bfe73648SShuo Chenclass InetAddress : public muduo::copyable
28bfe73648SShuo Chen{
29bfe73648SShuo Chen public:
30bfe73648SShuo Chen  /// Constructs an endpoint with given port number.
31bfe73648SShuo Chen  /// Mostly used in TcpServer listening.
32bfe73648SShuo Chen  explicit InetAddress(uint16_t port);
33bfe73648SShuo Chen
34bfe73648SShuo Chen  /// Constructs an endpoint with given ip and port.
35bfe73648SShuo Chen  /// @c ip should be "1.2.3.4"
36bfe73648SShuo Chen  InetAddress(const std::string& ip, uint16_t port);
37bfe73648SShuo Chen
38bfe73648SShuo Chen  /// Constructs an endpoint with given struct @c sockaddr_in
39bfe73648SShuo Chen  /// Mostly used when accepting new connections
40bfe73648SShuo Chen  InetAddress(const struct sockaddr_in& addr)
41bfe73648SShuo Chen    : addr_(addr)
42bfe73648SShuo Chen  { }
43bfe73648SShuo Chen
44bfe73648SShuo Chen  std::string toHostPort() const;
45bfe73648SShuo Chen
46bfe73648SShuo Chen  // default copy/assignment are Okay
47bfe73648SShuo Chen
48bfe73648SShuo Chen  const struct sockaddr_in& getSockAddrInet() const { return addr_; }
49bfe73648SShuo Chen  void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }
50bfe73648SShuo Chen
51bfe73648SShuo Chen private:
52bfe73648SShuo Chen  struct sockaddr_in addr_;
53bfe73648SShuo Chen};
54bfe73648SShuo Chen
55bfe73648SShuo Chen}
56bfe73648SShuo Chen
57bfe73648SShuo Chen#endif  // MUDUO_NET_INETADDRESS_H
58