CPN
Computational Process Networks
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
SocketAddress Class Reference

An abstraction of a socket address with convenience methods. More...

#include <SocketAddress.h>

+ Collaboration diagram for SocketAddress:

Classes

union  address_
 

Public Types

enum  Type_t { IPV4, IPV6, LOCAL }
 

Public Member Functions

 SocketAddress ()
 
 SocketAddress (addrinfo *info)
 
 SocketAddress (addrinfo *info, unsigned portnum)
 
 SocketAddress (sockaddr *addr, socklen_t len)
 
 ~SocketAddress ()
 
std::string GetHostName (bool numerichost=true) const
 
std::string GetServName () const
 
unsigned GetServ () const
 
Type_t GetType () const
 
sockaddr * GetAddr ()
 
socklen_t & GetLen ()
 
sa_family_t & Family ()
 
sa_family_t Family () const
 
void SetFromSockName (int fd)
 Fill this SocketAddress with data from this side of the connection represented by fd. More...
 
void SetFromPeerName (int fd)
 Fill this SocketAddress with data from the other side of the connection represented by fd. More...
 

Static Public Member Functions

static SockAddrList CreateIPFromServ (const char *servname)
 Return a list of valid socket addresses for the given service name All CreateIP functions create IP addresses. More...
 
static SockAddrList CreateIPFromServ (const std::string &servname)
 
static SockAddrList CreateIPFromHost (const char *hostname)
 
static SockAddrList CreateIPFromHost (const std::string &hostname)
 
static SockAddrList CreateIP (unsigned serv)
 Return a list of valid socket address for the given service number or port number. More...
 
static SockAddrList CreateIP (const char *hostname, const char *servname)
 
static SockAddrList CreateIP (const std::string &hostname, const std::string &servname)
 
static SockAddrList CreateIP (const char *hostname, unsigned serv)
 
static SockAddrList CreateIP (const std::string &hostname, unsigned serv)
 
static SockAddrList CreateIP (const std::string &hostname)
 

Static Private Member Functions

static SockAddrList Lookup (const char *const hostname, const char *const port, int family, unsigned portnum)
 

Private Attributes

socklen_t length
 
union SocketAddress::address_ address
 

Detailed Description

An abstraction of a socket address with convenience methods.

Definition at line 42 of file SocketAddress.h.

Member Enumeration Documentation

Enumerator
IPV4 
IPV6 
LOCAL 

Definition at line 45 of file SocketAddress.h.

Constructor & Destructor Documentation

SocketAddress::SocketAddress ( )

Definition at line 33 of file SocketAddress.cc.

References address.

Referenced by Lookup().

34  : length(0)
35 {
36  memset(&address, 0, sizeof(address));
37 }
socklen_t length
union SocketAddress::address_ address

+ Here is the caller graph for this function:

SocketAddress::SocketAddress ( addrinfo *  info)

Definition at line 174 of file SocketAddress.cc.

References address, and length.

174  {
175  length = info->ai_addrlen;
176  memcpy(&address, info->ai_addr, length);
177 }
socklen_t length
union SocketAddress::address_ address
SocketAddress::SocketAddress ( addrinfo *  info,
unsigned  portnum 
)

Definition at line 179 of file SocketAddress.cc.

References address, Family(), SocketAddress::address_::in, SocketAddress::address_::in6, and length.

179  {
180  length = info->ai_addrlen;
181  memcpy(&address, info->ai_addr, length);
182  uint16_t port = (uint16_t)portnum;
183  port = htons(port);
184  switch (Family()) {
185  case AF_INET:
186  address.in.sin_port = port;
187  break;
188  case AF_INET6:
189  address.in6.sin6_port = port;
190  break;
191  default:
192  break;
193  }
194 }
socklen_t length
sa_family_t & Family()
union SocketAddress::address_ address

+ Here is the call graph for this function:

SocketAddress::SocketAddress ( sockaddr *  addr,
socklen_t  len 
)

Create a SocketAddress from the underlying sockaddr and length

Parameters
addrpointer tot he address structure
lenthe length of the address structure

Definition at line 196 of file SocketAddress.cc.

References address, and length.

196  {
197  memcpy(&address, addr, len);
198  length = len;
199 }
socklen_t length
union SocketAddress::address_ address
SocketAddress::~SocketAddress ( )
inline

Definition at line 94 of file SocketAddress.h.

94 {}

Member Function Documentation

SockAddrList SocketAddress::CreateIP ( unsigned  serv)
static

Return a list of valid socket address for the given service number or port number.

Definition at line 103 of file SocketAddress.cc.

References address, Family(), SocketAddress::address_::in, and length.

Referenced by CPN::ConnectionServer::ConnectWriter(), CPN::Kernel::Kernel(), and CPN::VariantCPNLoader::LoadContext().

103  {
104  SocketAddress addr;
105  addr.Family() = AF_INET;
106  addr.address.in.sin_port = htons(serv);
107  addr.address.in.sin_addr.s_addr = INADDR_ANY;
108  addr.length = sizeof(sockaddr_in);
109  SockAddrList list;
110  list.push_back(addr);
111  return list;
112 }
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
socklen_t length
sa_family_t & Family()
std::vector< SocketAddress > SockAddrList
Definition: SocketAddress.h:35
union SocketAddress::address_ address

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

SockAddrList SocketAddress::CreateIP ( const char *  hostname,
const char *  servname 
)
static
Parameters
hostnameThe hostname to lookup
servnamethe service name to lookup, may be a number string for the port
Returns
a list of valid addresses to connect to the given host/port

Definition at line 114 of file SocketAddress.cc.

References Lookup().

114  {
115  return Lookup(hostname, servname, AF_UNSPEC, 0);
116 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIP ( const std::string &  hostname,
const std::string &  servname 
)
static

Definition at line 118 of file SocketAddress.cc.

References Lookup().

118  {
119  return Lookup(hostname.c_str(), servname.c_str(), AF_UNSPEC, 0);
120 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIP ( const char *  hostname,
unsigned  serv 
)
static
Parameters
hostnamethe hostname to lookup
servthe port number
Returns
a list of valid address to connect to the given host/port

Definition at line 122 of file SocketAddress.cc.

References Lookup().

122  {
123  return Lookup(hostname, 0, AF_UNSPEC, serv);
124 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIP ( const std::string &  hostname,
unsigned  serv 
)
static

Definition at line 126 of file SocketAddress.cc.

References Lookup().

126  {
127  return Lookup(hostname.c_str(), 0, AF_UNSPEC, serv);
128 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIP ( const std::string &  hostname)
static

Create socket addresses from hostname:port, [hostname]:port type strings as described in RFC 2732.

Parameters
hostname

Definition at line 130 of file SocketAddress.cc.

References Lookup().

130  {
131  std::string host;
132  std::string port;
133  std::string::const_iterator it, e;
134  e = hostname.end();
135  it = hostname.begin();
136  bool in_address = false;
137  bool at_port = false;
138  while (it != e) {
139  if (in_address) {
140  if (*it == ']') {
141  in_address = false;
142  } else {
143  host.push_back(*it);
144  }
145  }
146  if (at_port) {
147  port.push_back(*it);
148  } else {
149  switch (*it) {
150  case '[':
151  in_address = true;
152  break;
153  case ':':
154  at_port = true;
155  break;
156  default:
157  host.push_back(*it);
158  break;
159  }
160  }
161  ++it;
162  }
163  const char *h = 0;
164  const char *p = 0;
165  if (!host.empty()) {
166  h = host.c_str();
167  }
168  if (!port.empty()) {
169  p = port.c_str();
170  }
171  return Lookup(h, p, AF_UNSPEC, 0);
172 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIPFromHost ( const char *  hostname)
static

Definition at line 95 of file SocketAddress.cc.

References Lookup().

95  {
96  return Lookup(hostname, 0, AF_UNSPEC, 0);
97 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIPFromHost ( const std::string &  hostname)
static

Definition at line 99 of file SocketAddress.cc.

References Lookup().

99  {
100  return Lookup(hostname.c_str(), 0, AF_UNSPEC, 0);
101 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIPFromServ ( const char *  servname)
static

Return a list of valid socket addresses for the given service name All CreateIP functions create IP addresses.

Returns
a list of SocketAddresses

Definition at line 87 of file SocketAddress.cc.

References Lookup().

87  {
88  return Lookup(0, servname, AF_UNSPEC, 0);
89 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

SockAddrList SocketAddress::CreateIPFromServ ( const std::string &  servname)
static

Definition at line 91 of file SocketAddress.cc.

References Lookup().

91  {
92  return Lookup(0, servname.c_str(), AF_UNSPEC, 0);
93 }
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)

+ Here is the call graph for this function:

sa_family_t& SocketAddress::Family ( )
inline

Definition at line 115 of file SocketAddress.h.

References address, and SocketAddress::address_::storage.

Referenced by SocketHandle::Connect(), CreateIP(), GetServ(), GetType(), ServerSocketHandle::Listen(), and SocketAddress().

115 { return address.storage.ss_family; }
sockaddr_storage storage
union SocketAddress::address_ address

+ Here is the caller graph for this function:

sa_family_t SocketAddress::Family ( ) const
inline

Definition at line 116 of file SocketAddress.h.

References address, and SocketAddress::address_::storage.

116 { return address.storage.ss_family; }
sockaddr_storage storage
union SocketAddress::address_ address
sockaddr* SocketAddress::GetAddr ( )
inline

Definition at line 113 of file SocketAddress.h.

References SocketAddress::address_::addr, and address.

Referenced by ServerSocketHandle::Accept(), SocketHandle::Connect(), ServerSocketHandle::Listen(), SetFromPeerName(), and SetFromSockName().

113 { return &address.addr; }
union SocketAddress::address_ address

+ Here is the caller graph for this function:

std::string SocketAddress::GetHostName ( bool  numerichost = true) const
Parameters
numerichosttrue to force no resolution of host names and just return the number
Returns
a string representation of the host name

Definition at line 201 of file SocketAddress.cc.

References SocketAddress::address_::addr, address, and length.

Referenced by CPN::RemoteContextDaemon::Client::Client(), CPN::Kernel::Kernel(), and CPN::RemoteContextDaemon::Run().

201  {
202  std::vector<char> hostname(NI_MAXHOST, '\0');
203  bool loop = true;
204  std::ostringstream oss;
205  while (loop) {
206  int flags = NI_NUMERICSERV;
207  if (numerichost) { flags |= NI_NUMERICHOST; }
208  int lookupstatus = getnameinfo(&address.addr, length,
209  &hostname[0], hostname.size(),
210  0, 0,
211  flags);
212  switch (lookupstatus) {
213  case 0:
214  oss << &hostname[0];
215  loop = false;
216  break;
217  case EAI_AGAIN:
218  break;
219 #if !defined(__APPLE__)
220  case EAI_OVERFLOW:
221  hostname.resize(hostname.size()*2, '\0');
222  break;
223 #endif
224  case EAI_BADFLAGS:
225  case EAI_FAIL:
226  case EAI_FAMILY:
227  case EAI_MEMORY:
228  case EAI_NONAME:
229  case EAI_SYSTEM:
230  default:
231  throw ErrnoException(gai_strerror(lookupstatus), lookupstatus);
232  }
233  }
234  return oss.str();
235 }
socklen_t length
union SocketAddress::address_ address

+ Here is the caller graph for this function:

socklen_t& SocketAddress::GetLen ( )
inline

Definition at line 114 of file SocketAddress.h.

References length.

Referenced by ServerSocketHandle::Accept(), SocketHandle::Connect(), ServerSocketHandle::Listen(), SetFromPeerName(), and SetFromSockName().

114 { return length; }
socklen_t length

+ Here is the caller graph for this function:

unsigned SocketAddress::GetServ ( ) const
Returns
the service (port) number

Definition at line 271 of file SocketAddress.cc.

References address, Family(), SocketAddress::address_::in, and SocketAddress::address_::in6.

271  {
272  uint16_t retval = 0;
273  switch (Family()) {
274  case AF_INET:
275  retval = address.in.sin_port;
276  break;
277  case AF_INET6:
278  retval = address.in6.sin6_port;
279  break;
280  default:
281  break;
282  }
283  return (unsigned)ntohs(retval);
284 }
sa_family_t & Family()
union SocketAddress::address_ address

+ Here is the call graph for this function:

std::string SocketAddress::GetServName ( ) const
Returns
the service (port) name or number as a string.

Definition at line 237 of file SocketAddress.cc.

References SocketAddress::address_::addr, address, and length.

Referenced by CPN::RemoteContextDaemon::Client::Client(), CPN::Kernel::Kernel(), and CPN::RemoteContextDaemon::Run().

237  {
238  std::vector<char> servname(NI_MAXSERV, '\0');
239  bool loop = true;
240  std::ostringstream oss;
241  while (loop) {
242  int lookupstatus = getnameinfo(&address.addr, length,
243  0, 0,
244  &servname[0], servname.size(),
245  NI_NUMERICSERV);
246  switch (lookupstatus) {
247  case 0:
248  oss << &servname[0];
249  loop = false;
250  break;
251  case EAI_AGAIN:
252  break;
253 #if !defined(__APPLE__)
254  case EAI_OVERFLOW:
255  servname.resize(servname.size()*2, '\0');
256  break;
257 #endif
258  case EAI_BADFLAGS:
259  case EAI_FAIL:
260  case EAI_FAMILY:
261  case EAI_MEMORY:
262  case EAI_NONAME:
263  case EAI_SYSTEM:
264  default:
265  throw ErrnoException(gai_strerror(lookupstatus), lookupstatus);
266  }
267  }
268  return oss.str();
269 }
socklen_t length
union SocketAddress::address_ address

+ Here is the caller graph for this function:

SocketAddress::Type_t SocketAddress::GetType ( ) const

Definition at line 286 of file SocketAddress.cc.

References ASSERT, Family(), IPV4, IPV6, and LOCAL.

286  {
287  Type_t type;
288  switch (Family()) {
289  case AF_INET:
290  type = IPV4;
291  break;
292  case AF_INET6:
293  type = IPV6;
294  break;
295  case AF_UNIX:
296  type = LOCAL;
297  break;
298  default:
299  ASSERT(false, "Unknown address type.");
300  }
301  return type;
302 }
sa_family_t & Family()
#define ASSERT(exp,...)

+ Here is the call graph for this function:

SockAddrList SocketAddress::Lookup ( const char *const  hostname,
const char *const  port,
int  family,
unsigned  portnum 
)
staticprivate

Definition at line 39 of file SocketAddress.cc.

References SocketAddress().

Referenced by CreateIP(), CreateIPFromHost(), and CreateIPFromServ().

40  {
41  addrinfo hints = {0};
42  // Get only supported types.
43  hints.ai_flags = AI_ADDRCONFIG;
44  hints.ai_family = family;
45  hints.ai_socktype = SOCK_STREAM;
46  if (0 == hostname || *hostname == '\0') {
47  hints.ai_flags |= AI_PASSIVE;
48  }
49  addrinfo *res = 0;
50  int lookupstatus = 0;
51  do {
52  lookupstatus = getaddrinfo(hostname, port, &hints, &res);
53  switch (lookupstatus) {
54  case EAI_AGAIN:
55  // try again
56  case EAI_MEMORY:
57  // our of memory, try again?
58  case 0:
59  // Success.
60  break;
61  case EAI_ADDRFAMILY:
62  case EAI_BADFLAGS:
63  case EAI_FAIL:
64  case EAI_FAMILY:
65  case EAI_NODATA:
66  case EAI_NONAME:
67  case EAI_SERVICE:
68  case EAI_SOCKTYPE:
69  case EAI_SYSTEM:
70  default:
71  throw ErrnoException(gai_strerror(lookupstatus), lookupstatus);
72  }
73  } while (lookupstatus != 0);
74 
75  SockAddrList results;
76  for (addrinfo *rp = res; rp != 0; rp = rp->ai_next) {
77  if (port) {
78  results.push_back(SocketAddress(rp));
79  } else {
80  results.push_back(SocketAddress(rp, portnum));
81  }
82  }
83  freeaddrinfo(res);
84  return results;
85 }
std::vector< SocketAddress > SockAddrList
Definition: SocketAddress.h:35

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SocketAddress::SetFromPeerName ( int  fd)

Fill this SocketAddress with data from the other side of the connection represented by fd.

Parameters
fdthe socket to use

Definition at line 311 of file SocketAddress.cc.

References GetAddr(), and GetLen().

Referenced by CPN::RemoteContextDaemon::Client::Client().

311  {
312  GetLen() = sizeof(sockaddr_storage);
313  if (getpeername(fd, GetAddr(), &GetLen()) != 0) {
314  throw ErrnoException();
315  }
316 }
socklen_t & GetLen()
sockaddr * GetAddr()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SocketAddress::SetFromSockName ( int  fd)

Fill this SocketAddress with data from this side of the connection represented by fd.

Parameters
fdthe socket to use

Definition at line 304 of file SocketAddress.cc.

References GetAddr(), and GetLen().

Referenced by CPN::ConnectionServer::GetAddress(), and CPN::RemoteContextDaemon::Run().

304  {
305  GetLen() = sizeof(sockaddr_storage);
306  if (getsockname(fd, GetAddr(), &GetLen()) != 0) {
307  throw ErrnoException();
308  }
309 }
socklen_t & GetLen()
sockaddr * GetAddr()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

union SocketAddress::address_ SocketAddress::address
private
socklen_t SocketAddress::length
private

Definition at line 135 of file SocketAddress.h.

Referenced by CreateIP(), GetHostName(), GetLen(), GetServName(), and SocketAddress().


The documentation for this class was generated from the following files: