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