SimpleSocket  0.1
Some wrappers that make using sockets easier in C++
Classes | Public Member Functions | Public Attributes | List of all members
ComboAddress Union Reference

#include <comboaddress.hh>

Classes

struct  addressOnlyEqual
 Convenience comparator that compares regardless of port. More...
 
struct  addressOnlyLessThan
 Convenience comparator that compares regardless of port. More...
 

Public Member Functions

bool operator== (const ComboAddress &rhs) const
 Tests for equality, including port number. IPv4 and IPv6 are never equal.
 
bool operator!= (const ComboAddress &rhs) const
 Inequality.
 
bool operator< (const ComboAddress &rhs) const
 This ordering is intended to be fast and strict, but not necessarily human friendly.
 
bool operator> (const ComboAddress &rhs) const
 
socklen_t getSocklen () const
 it is vital to pass the correct socklen to the kernel
 
 ComboAddress ()
 Initializes an 'empty', impossible, ComboAddress.
 
 ComboAddress (const struct sockaddr *sa, socklen_t salen)
 Make a ComboAddress from a traditional sockaddr.
 
 ComboAddress (const struct sockaddr_in6 *sa)
 Make a ComboAddress from a traditional sockaddr_in6.
 
 ComboAddress (const struct sockaddr_in *sa)
 Make a ComboAddress from a traditional sockaddr_in.
 
 ComboAddress (const struct sockaddr_in &sa)
 Make a ComboAddress from a traditional sockaddr.
 
void setSockaddr (const struct sockaddr *sa, socklen_t salen)
 
 ComboAddress (const std::string &str, uint16_t port=0)
 
void setPort (uint16_t port)
 Sets port, deals with htons for you.
 
bool isIPv6 () const
 Is this an IPv6 address?
 
bool isIPv4 () const
 Is this an IPv4 address?
 
bool isMappedIPv4 () const
 Is this an ffff:: style IPv6 address?
 
ComboAddress mapToIPv4 () const
 Extract the IPv4 address from a mapped IPv6 address.
 
std::string toString () const
 Returns a string (human) represntation of the address.
 
std::string toStringWithPort () const
 Returns a string (human) represntation of the address, including port.
 
void truncate (unsigned int bits)
 

Public Attributes

struct sockaddr_in sin
 
struct sockaddr_in sin4
 
struct sockaddr_in6 sin6
 

Detailed Description

The ComboAddress holds an IPv4 or an IPv6 endpoint, including a source port. It is ABI-compatible with struct sockaddr, sockaddr_in and sockaddr_in6. This means it can be passed to the kernel via a pointer directly.

When doing so, it is imperative to pass the right length parameter, because some operating systems get confused if they see length longer than necessary.

Canonical use is: connect(sock, (struct sockaddr*)&combo, combo.getSocklen())

The union has methods for parsing string ('human') IP address representations, and these support all kinds of addresses, including scoped IPv6 and port numbers (1.2.3.4:80 and [::1]:80 or even [fe80::1eth0]:80).

Constructor & Destructor Documentation

ComboAddress::ComboAddress ( const std::string &  str,
uint16_t  port = 0 
)
inlineexplicit

"Human" representation constructor. The following are all identical: ComboAddress("1.2.3.4:80"); ComboAddress("1.2.3.4", 80); ComboAddress("1.2.3.4:80", 1234)

As are: ComboAddress("fe80::1%eth0", 80); ComboAddress("[fe80::1%eth0]:80"); ComboAddress("[fe::1%eth0]:80", 1234);


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