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