#include <ipsocket.h>
Public Member Functions | |
IPSocket (int type=0, int protocol=0) | |
Constructor. | |
IPSocket (IPSocket const &ips) | |
Copy constructor. | |
virtual IPSocket & | operator= (IPSocket const &right) |
Assignment operator. | |
~IPSocket () | |
Destructor. | |
virtual bool | assign (int fd, uint32_t status) |
Assigns a socket file descriptor. | |
virtual bool | bind () |
Binds the socket. | |
virtual bool | set_local (char const *address, char const *port, char const **error=0) |
Sets local address and port. | |
virtual bool | set_local (char const *address, unsigned short port, char const **error=0) |
Same as the other set_local(), but port is an integer. | |
virtual bool | set_local (char const *file) |
Sets local file. | |
virtual bool | set_remote (char const *address, char const *port, char const **error=0) |
Sets remote address and port. | |
virtual bool | set_remote (char const *address, unsigned short port, char const **error=0) |
Same as the other set_remote(), but port is an integer. | |
virtual bool | set_remote (char const *file) |
Sets remote file. | |
virtual char const * | laddress () const |
Returns the local address. | |
virtual unsigned short | lport () const |
Returns the local port. | |
virtual char const * | lfile () const |
Returns the local file. | |
virtual char const * | raddress () const |
Returns the remote address. | |
virtual unsigned short | rport () const |
Returns the remote port. | |
virtual char const * | rfile () const |
Returns the remote file. | |
Protected Member Functions | |
virtual bool | create () |
Overrides Socket::create() to set the socket as non-blocking. | |
bool | resolve (char const *address, char const *port, char const **error, IPInfo **ipi) |
Resolves an address and port. | |
IPInfo * | local () |
Returns the local information. | |
IPInfo const * | local () const |
Returns the local information. | |
IPInfo * | remote () |
Returns the remote information. | |
IPInfo const * | remote () const |
Returns the remote information. | |
unsigned short | port (IPInfo const *ipi) const |
Returns the port out of an IPInfo object. | |
Friends | |
class | TCPSocket |
For TCPSocket::add_remote(). | |
Classes | |
class | IPInfo |
IP information class. More... |
This class provides support to set up and retrieve information about the local and remote end of a connection, as well as a function to bind the socket to a specific address and/or port.
Both IPv4 and IPv6 are supported.
Update: This supports Unix sockets as well now (so, the class name kinda sucks, but oh well :P) with special versions of set_remote() and set_local() along with the lfile() and rfile() methods.
sockets::IPSocket::IPSocket | ( | int | type = 0 , |
|
int | protocol = 0 | |||
) | [inline, explicit] |
Constructor.
[in] | type | The socket type (one of the SOCK_... constants defined in the system header files). May be set to 0 if you want to set it later. |
[in] | protocol | The socket protocol that needs to be used. This is seldom used, as there is generally only one protocol for a given family/type combination; use the value 0 to use the default protocol. |
virtual bool sockets::IPSocket::assign | ( | int | fd, | |
uint32_t | status | |||
) | [virtual] |
Assigns a socket file descriptor to the socket object, that is then used by the object for all operations on the socket.
This function overrides Socket::assign() to set the socket as non-blocking.
[in] | fd | The socket file descriptor to assign. |
[in] | status | The status flags that are added to the socket. If the ssMonitored flag is among them, the watch() function is automatically called. |
Reimplemented from sockets::Socket.
Reimplemented in sockets::TCPClient.
virtual bool sockets::IPSocket::bind | ( | ) | [virtual] |
Binds the socket to the local address and port that have been specified through the set_local() function.
This function creates the socket file descriptor if it is not created already. The ssCreated and ssBound flags are guaranteed to be set if this function succeeds.
virtual bool sockets::IPSocket::set_local | ( | char const * | address, | |
char const * | port, | |||
char const ** | error = 0 | |||
) | [virtual] |
Sets local address and port. It can't be changed once the ssBound, ssListening, ssConnecting or ssConnected status flags are set on the socket.
This function determines the socket protocol family if the ssRemoteInfo and ssCreated status flags are not set on the socket.
[in] | address | The address to bind to, either as a hostname that will be resolved or an IP address expressed as a character string. |
[in] | port | The port to bind to, either as a service listed in /etc/services or a port number expressed as a character string. |
[out] | error | (optional) Points to a character wtring describing the error that happened if the function fails. |
virtual bool sockets::IPSocket::set_local | ( | char const * | file | ) | [virtual] |
Sets local file. It can't be changed once the ssBound, ssListening, ssConnecting or ssConnected status flags are set on the socket.
This function sets the socket protocol family to PF_UNIX if the ssRemoteInfo and ssCreated status flags are not set on the socket.
[in] | file | The path to the file. |
virtual bool sockets::IPSocket::set_remote | ( | char const * | address, | |
char const * | port, | |||
char const ** | error = 0 | |||
) | [virtual] |
Sets remote address and port. It can't be changed once the ssListening, ssConnecting or ssConnected status flags are set on the socket.
This function determines the socket protocol family if the ssLocalInfo and ssCreated status flags are not set on the socket.
[in] | address | The address to connect to, either as a hostname that will be resolved or an IP address expressed as a character string. |
[in] | port | The port to connect to, either as a service listed in /etc/services or a port number expressed as a character string. |
[out] | error | (optional) Points to a character wtring describing the error that happened if the function fails. |
Reimplemented in sockets::TCPSocket.
virtual bool sockets::IPSocket::set_remote | ( | char const * | file | ) | [virtual] |
Sets remote file. It can't be changed once the ssListening, ssConnecting or ssConnected status flags are set on the socket.
This function sets the socket protocol family to PF_UNIX if the ssRemoteInfo and ssCreated status flags are not set on the socket.
[in] | file | The path to the file. |
virtual char const* sockets::IPSocket::laddress | ( | ) | const [virtual] |
Returns the local address. This is either the address set through the set_local() function if it was used, or the one the socket is actually bound to if the ssCreated status flag is set and ssLocalInfo is not.
This always returns 0 if the protocol family is not PF_INET or PF_INET6.
virtual unsigned short sockets::IPSocket::lport | ( | ) | const [virtual] |
Returns the local port. This is the port set through the set_local() function if it was used, or the one the socket is actually bound to if the ssCreated status flag is set and ssLocalInfo is not.
This always returns 0 if the protocol family is not PF_INET or PF_INET6.
virtual char const* sockets::IPSocket::lfile | ( | ) | const [virtual] |
Returns the local file. This is either the address set through the set_local() function if it was used, or the one the socket is actually bound to if the ssCreated status flag is set and ssLocalInfo is not.
This always returns 0 if the protocol family is not PF_UNIX. or unix sockets are not available.
virtual char const* sockets::IPSocket::raddress | ( | ) | const [virtual] |
Returns the remote address. This is the address set through the set_remote() function.
This always returns 0 if the protocol family is not PF_INET or PF_INET6.
Reimplemented in sockets::TCPSocket.
virtual unsigned short sockets::IPSocket::rport | ( | ) | const [virtual] |
Returns the remote port. This is the port set through the set_remote() function.
This always returns 0 if the protocol family is not PF_INET or PF_INET6.
Reimplemented in sockets::TCPSocket.
virtual char const* sockets::IPSocket::rfile | ( | ) | const [virtual] |
Returns the remote file. This is the file set through the set_remote() function.
This always returns 0 if the protocol family is not PF_UNIX. or unix sockets are not available.
bool sockets::IPSocket::resolve | ( | char const * | address, | |
char const * | port, | |||
char const ** | error, | |||
IPInfo ** | ipi | |||
) | [protected] |
Resolves an address and port.
[in] | address | The address to resolve. It may either be an IP address or a hostname. |
[in] | port | The port to resolve. It may either be a numeric port or a service name. |
[out] | error | A pointer to a character string receiving an error message when the function returns false. May be NULL. |
[in] | ipi | On input, points to either a NULL pointer or a pointer to an IPInfo object that will be the next object in the resolved list. On output, points to either the original pointer or to the first new object of the resolved list. |