MongoDB  2.6.0
hostandport.h
1 // hostandport.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "mongo/bson/util/builder.h"
21 #include "mongo/db/server_options.h"
22 #include "mongo/util/mongoutils/str.h"
23 #include "mongo/util/net/sock.h"
24 
25 namespace mongo {
26 
27  using namespace mongoutils;
28 
31  struct HostAndPort {
32  HostAndPort() : _port(-1) { }
33 
37  HostAndPort(const std::string& s);
38 
40  HostAndPort(const std::string& h, int p /*= -1*/) : _host(h), _port(p) {
41  verify(!mongoutils::str::startsWith(h, '#'));
42  }
43 
44  HostAndPort(const SockAddr& sock ) : _host( sock.getAddr() ) , _port( sock.getPort() ) { }
45 
46  static HostAndPort me();
47 
48  bool operator<(const HostAndPort& r) const {
49  const int cmp = host().compare(r.host());
50  if (cmp)
51  return cmp < 0;
52  return port() < r.port();
53  }
54 
55  bool operator==(const HostAndPort& r) const {
56  return host() == r.host() && port() == r.port();
57  }
58 
59  bool operator!=(const HostAndPort& r) const { return !(*this == r); }
60 
61  /* returns true if the host/port combo identifies this process instance. */
62  bool isSelf() const; // defined in isself.cpp
63 
64  bool isLocalHost() const;
65 
69  string toString( bool includePort=true ) const;
70 
71  operator string() const { return toString(); }
72 
73  void append( StringBuilder& ss ) const;
74 
75  bool empty() const {
76  return _host.empty() && _port < 0;
77  }
78  const string& host() const {
79  return _host;
80  }
81  int port() const {
82  if (hasPort())
83  return _port;
84  return ServerGlobalParams::DefaultDBPort;
85  }
86  bool hasPort() const {
87  return _port >= 0;
88  }
89  void setPort( int port ) {
90  _port = port;
91  }
92 
93  private:
94  void init(const char *);
95  string _host;
96  int _port; // -1 indicates unspecified
97  };
98 
99  inline HostAndPort HostAndPort::me() {
100  const char* ips = serverGlobalParams.bind_ip.c_str();
101  while(*ips) {
102  string ip;
103  const char * comma = strchr(ips, ',');
104  if (comma) {
105  ip = string(ips, comma - ips);
106  ips = comma + 1;
107  }
108  else {
109  ip = string(ips);
110  ips = "";
111  }
112  HostAndPort h = HostAndPort(ip, serverGlobalParams.port);
113  if (!h.isLocalHost()) {
114  return h;
115  }
116  }
117 
118  string h = getHostName();
119  verify( !h.empty() );
120  verify( h != "localhost" );
121  return HostAndPort(h, serverGlobalParams.port);
122  }
123 
124  inline string HostAndPort::toString( bool includePort ) const {
125  if ( ! includePort )
126  return host();
127 
128  StringBuilder ss;
129  append( ss );
130  return ss.str();
131  }
132 
133  inline void HostAndPort::append( StringBuilder& ss ) const {
134  ss << host();
135 
136  int p = port();
137 
138  if ( p != -1 ) {
139  ss << ':';
140 #if defined(_DEBUG)
141  if( p >= 44000 && p < 44100 ) {
142  log() << "warning: special debug port 44xxx used" << std::endl;
143  ss << p+1;
144  }
145  else
146  ss << p;
147 #else
148  ss << p;
149 #endif
150  }
151 
152  }
153 
154 
155  inline bool HostAndPort::isLocalHost() const {
156  string _host = host();
157  return ( _host == "localhost"
158  || mongoutils::str::startsWith(_host.c_str(), "127.")
159  || _host == "::1"
160  || _host == "anonymous unix socket"
161  || _host.c_str()[0] == '/' // unix socket
162  );
163  }
164 
165  inline void HostAndPort::init(const char *p) {
166  massert(13110, "HostAndPort: host is empty", *p);
167  const char *colon = strrchr(p, ':');
168  if( colon ) {
169  int port = atoi(colon+1);
170  massert(13095, "HostAndPort: bad port #", port > 0);
171  _host = string(p,colon-p);
172  _port = port;
173  }
174  else {
175  // no port specified.
176  _host = p;
177  _port = -1;
178  }
179  }
180 
181  inline HostAndPort::HostAndPort(const std::string& s) {
182  init(s.c_str());
183  }
184 
185 }
string toString(bool includePort=true) const
Definition: hostandport.h:124
wrapped around os representation of network address
Definition: sock.h:93
HostAndPort(const std::string &h, int p)
Definition: hostandport.h:40
LogstreamBuilder log()
Returns a LogstreamBuilder for logging a message with LogSeverity::Log().
Definition: log.h:69
string getHostName()
this is not cache and does a syscall
Definition: sock.cpp:337
helper for manipulating host:port connection endpoints.
Definition: hostandport.h:31
stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:61