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