140161064SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
240161064SShuo Chen// http://code.google.com/p/muduo/
340161064SShuo Chen//
440161064SShuo Chen// Use of this source code is governed by a BSD-style license
540161064SShuo Chen// that can be found in the License file.
640161064SShuo Chen
740161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
840161064SShuo Chen//
940161064SShuo Chen// This is a public header file, it must only include public header files.
1040161064SShuo Chen
1140161064SShuo Chen#ifndef MUDUO_NET_INETADDRESS_H
1240161064SShuo Chen#define MUDUO_NET_INETADDRESS_H
1340161064SShuo Chen
1440161064SShuo Chen#include "datetime/copyable.h"
1540161064SShuo Chen
1640161064SShuo Chen#include <string>
1740161064SShuo Chen
1840161064SShuo Chen#include <netinet/in.h>
1940161064SShuo Chen
2040161064SShuo Chennamespace muduo
2140161064SShuo Chen{
2240161064SShuo Chen
2340161064SShuo Chen///
2440161064SShuo Chen/// Wrapper of sockaddr_in.
2540161064SShuo Chen///
2640161064SShuo Chen/// This is an POD interface class.
2740161064SShuo Chenclass InetAddress : public muduo::copyable
2840161064SShuo Chen{
2940161064SShuo Chen public:
3040161064SShuo Chen  /// Constructs an endpoint with given port number.
3140161064SShuo Chen  /// Mostly used in TcpServer listening.
3240161064SShuo Chen  explicit InetAddress(uint16_t port);
3340161064SShuo Chen
3440161064SShuo Chen  /// Constructs an endpoint with given ip and port.
3540161064SShuo Chen  /// @c ip should be "1.2.3.4"
3640161064SShuo Chen  InetAddress(const std::string& ip, uint16_t port);
3740161064SShuo Chen
3840161064SShuo Chen  /// Constructs an endpoint with given struct @c sockaddr_in
3940161064SShuo Chen  /// Mostly used when accepting new connections
4040161064SShuo Chen  InetAddress(const struct sockaddr_in& addr)
4140161064SShuo Chen    : addr_(addr)
4240161064SShuo Chen  { }
4340161064SShuo Chen
4440161064SShuo Chen  std::string toHostPort() const;
4540161064SShuo Chen
4640161064SShuo Chen  // default copy/assignment are Okay
4740161064SShuo Chen
4840161064SShuo Chen  const struct sockaddr_in& getSockAddrInet() const { return addr_; }
4940161064SShuo Chen  void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }
5040161064SShuo Chen
5140161064SShuo Chen private:
5240161064SShuo Chen  struct sockaddr_in addr_;
5340161064SShuo Chen};
5440161064SShuo Chen
5540161064SShuo Chen}
5640161064SShuo Chen
5740161064SShuo Chen#endif  // MUDUO_NET_INETADDRESS_H
58