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