InetAddress.h revision 354280cf
1354280cfSShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
2354280cfSShuo Chen// http://code.google.com/p/muduo/
3354280cfSShuo Chen//
4354280cfSShuo Chen// Use of this source code is governed by a BSD-style license
5354280cfSShuo Chen// that can be found in the License file.
6354280cfSShuo Chen
7354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
8354280cfSShuo Chen//
9354280cfSShuo Chen// This is a public header file, it must only include public header files.
10354280cfSShuo Chen
11354280cfSShuo Chen#ifndef MUDUO_NET_INETADDRESS_H
12354280cfSShuo Chen#define MUDUO_NET_INETADDRESS_H
13354280cfSShuo Chen
14354280cfSShuo Chen#include "datetime/copyable.h"
15354280cfSShuo Chen
16354280cfSShuo Chen#include <string>
17354280cfSShuo Chen
18354280cfSShuo Chen#include <netinet/in.h>
19354280cfSShuo Chen
20354280cfSShuo Chennamespace muduo
21354280cfSShuo Chen{
22354280cfSShuo Chen
23354280cfSShuo Chen///
24354280cfSShuo Chen/// Wrapper of sockaddr_in.
25354280cfSShuo Chen///
26354280cfSShuo Chen/// This is an POD interface class.
27354280cfSShuo Chenclass InetAddress : public muduo::copyable
28354280cfSShuo Chen{
29354280cfSShuo Chen public:
30354280cfSShuo Chen  /// Constructs an endpoint with given port number.
31354280cfSShuo Chen  /// Mostly used in TcpServer listening.
32354280cfSShuo Chen  explicit InetAddress(uint16_t port);
33354280cfSShuo Chen
34354280cfSShuo Chen  /// Constructs an endpoint with given ip and port.
35354280cfSShuo Chen  /// @c ip should be "1.2.3.4"
36354280cfSShuo Chen  InetAddress(const std::string& ip, uint16_t port);
37354280cfSShuo Chen
38354280cfSShuo Chen  /// Constructs an endpoint with given struct @c sockaddr_in
39354280cfSShuo Chen  /// Mostly used when accepting new connections
40354280cfSShuo Chen  InetAddress(const struct sockaddr_in& addr)
41354280cfSShuo Chen    : addr_(addr)
42354280cfSShuo Chen  { }
43354280cfSShuo Chen
44354280cfSShuo Chen  std::string toHostPort() const;
45354280cfSShuo Chen
46354280cfSShuo Chen  // default copy/assignment are Okay
47354280cfSShuo Chen
48354280cfSShuo Chen  const struct sockaddr_in& getSockAddrInet() const { return addr_; }
49354280cfSShuo Chen  void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }
50354280cfSShuo Chen
51354280cfSShuo Chen private:
52354280cfSShuo Chen  struct sockaddr_in addr_;
53354280cfSShuo Chen};
54354280cfSShuo Chen
55354280cfSShuo Chen}
56354280cfSShuo Chen
57354280cfSShuo Chen#endif  // MUDUO_NET_INETADDRESS_H
58