InetAddress.h revision 65c497a3
165c497a3SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
265c497a3SShuo Chen// http://code.google.com/p/muduo/
365c497a3SShuo Chen//
465c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
565c497a3SShuo Chen// that can be found in the License file.
665c497a3SShuo Chen
765c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
865c497a3SShuo Chen//
965c497a3SShuo Chen// This is a public header file, it must only include public header files.
1065c497a3SShuo Chen
1165c497a3SShuo Chen#ifndef MUDUO_NET_INETADDRESS_H
1265c497a3SShuo Chen#define MUDUO_NET_INETADDRESS_H
1365c497a3SShuo Chen
1465c497a3SShuo Chen#include "datetime/copyable.h"
1565c497a3SShuo Chen
1665c497a3SShuo Chen#include <string>
1765c497a3SShuo Chen
1865c497a3SShuo Chen#include <netinet/in.h>
1965c497a3SShuo Chen
2065c497a3SShuo Chennamespace muduo
2165c497a3SShuo Chen{
2265c497a3SShuo Chen
2365c497a3SShuo Chen///
2465c497a3SShuo Chen/// Wrapper of sockaddr_in.
2565c497a3SShuo Chen///
2665c497a3SShuo Chen/// This is an POD interface class.
2765c497a3SShuo Chenclass InetAddress : public muduo::copyable
2865c497a3SShuo Chen{
2965c497a3SShuo Chen public:
3065c497a3SShuo Chen  /// Constructs an endpoint with given port number.
3165c497a3SShuo Chen  /// Mostly used in TcpServer listening.
3265c497a3SShuo Chen  explicit InetAddress(uint16_t port);
3365c497a3SShuo Chen
3465c497a3SShuo Chen  /// Constructs an endpoint with given ip and port.
3565c497a3SShuo Chen  /// @c ip should be "1.2.3.4"
3665c497a3SShuo Chen  InetAddress(const std::string& ip, uint16_t port);
3765c497a3SShuo Chen
3865c497a3SShuo Chen  /// Constructs an endpoint with given struct @c sockaddr_in
3965c497a3SShuo Chen  /// Mostly used when accepting new connections
4065c497a3SShuo Chen  InetAddress(const struct sockaddr_in& addr)
4165c497a3SShuo Chen    : addr_(addr)
4265c497a3SShuo Chen  { }
4365c497a3SShuo Chen
4465c497a3SShuo Chen  std::string toHostPort() const;
4565c497a3SShuo Chen
4665c497a3SShuo Chen  // default copy/assignment are Okay
4765c497a3SShuo Chen
4865c497a3SShuo Chen  const struct sockaddr_in& getSockAddrInet() const { return addr_; }
4965c497a3SShuo Chen  void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }
5065c497a3SShuo Chen
5165c497a3SShuo Chen private:
5265c497a3SShuo Chen  struct sockaddr_in addr_;
5365c497a3SShuo Chen};
5465c497a3SShuo Chen
5565c497a3SShuo Chen}
5665c497a3SShuo Chen
5765c497a3SShuo Chen#endif  // MUDUO_NET_INETADDRESS_H
58