UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches

Get information from a raw network socket file descriptor. More...

#include <socket.hpp>

+ Inheritance diagram for UPnPsdk::CSocket_basic:

Public Member Functions

 CSocket_basic ()
 Default constructor for an empty basic socket object with invalid socket file descriptor.
 
 CSocket_basic (SOCKET a_sfd)
 Constructor for the socket file descriptor. Before use, it must be load().
 
Setter

void load ()
 Load the raw socket file descriptor specified with the constructor into the object.
 
Getter

 operator SOCKET () const
 Get raw socket file descriptor.
 
bool local_saddr (SSockaddr *a_saddr=nullptr) const
 Get the local socket address the socket is bound to.
 
bool remote_saddr (SSockaddr *a_saddr=nullptr) const
 Get the remote socket address the socket is connected to.
 
int socktype () const
 Get the socket type.
 
int sockerr () const
 Get the error that is given from the socket as option.
 
bool is_reuse_addr () const
 Get status if reusing address is enabled.
 

Detailed Description

Get information from a raw network socket file descriptor.

For general information have a look at socket_module.

This class takes the resources and results as given by the platform (Unix, MacOS, MS Windows). It does not perform any emulations for unification. The behavior can be different on different platforms.

An object of this class does not take ownership of the raw socket file descriptor and will never close it. This is also the reason why you cannot modify the socket and only have getter available (except the setter 'load()' for the raw socket file descriptor itself). But it is helpful to easily get information about an existing raw socket file descriptor. Closing the file descriptor is in the responsibility of the caller who created the socket. If you need to manage a socket you must use CSocket.

Definition at line 129 of file socket.hpp.

Constructor & Destructor Documentation

◆ CSocket_basic() [1/2]

UPnPsdk::CSocket_basic::CSocket_basic ( )

Default constructor for an empty basic socket object with invalid socket file descriptor.

Definition at line 178 of file socket.cpp.

◆ CSocket_basic() [2/2]

UPnPsdk::CSocket_basic::CSocket_basic ( SOCKET  a_sfd)

Constructor for the socket file descriptor. Before use, it must be load().

Parameters
[in]a_sfdSocket file descriptor.

Definition at line 182 of file socket.cpp.

◆ ~CSocket_basic()

UPnPsdk::CSocket_basic::~CSocket_basic ( )
virtual

Definition at line 188 of file socket.cpp.

Member Function Documentation

◆ load()

void UPnPsdk::CSocket_basic::load ( )

Load the raw socket file descriptor specified with the constructor into the object.

Note
This function can only be used with the CSocket_basic class. A derived usage e.g. with the CSocket class will throw an exception.
// Usage e.g.:
SOCKET sfd = ::socket(PF_INET6, SOCK_STREAM);
{ // Scope for sockObj, sfd must longer exist than sockObj
CSocket_basic sockObj(sfd);
try {
sockObj.load();
} catch(xcp) { handle_error(); };
// Use the getter from sockObj
}
::close(sfd);
Get information from a raw network socket file descriptor.
Definition socket.hpp:129

The socket file descriptor was given with the constructor. This object does not take ownership of the socket file descriptor and will never close it. Closing is in the responsibility of the caller who created the socket. Initializing it again is possible but is only waste of resources. The result is the same as before.

Exceptions
std::runtime_errorGiven socket file descriptor is invalid.

Definition at line 196 of file socket.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator SOCKET()

UPnPsdk::CSocket_basic::operator SOCKET ( ) const

Get raw socket file descriptor.

// Usage e.g.:
CSocket_basic sockObj(valid_socket_fd);
try {
sockObj.load();
} catch(xcp) { handle_error(); };
SOCKET sfd = sockObj;

Definition at line 220 of file socket.cpp.

◆ local_saddr()

bool UPnPsdk::CSocket_basic::local_saddr ( SSockaddr a_saddr = nullptr) const

Get the local socket address the socket is bound to.

// Usage e.g.:
SOCKET sfd = ::socket(PF_INET6, SOCK_STREAM, 0);
SSockaddr saObj;
CSocket_basic sockbObj(sfd);
CSocket sockObj;
try {
sockbObj.load();
if (sockbObj.local_saddr(&saObj))
std::cout << "socket is bound to " << saObj << '\n';
else
std::cout << "unbound socket unspecified netaddr " << saObj << '\n';
if (sockbObj.remote_saddr(&saObj))
std::cout << "socket is connected to " << saObj << '\n';
else
std::cout << "unconnected socket unspecified netaddr " << saObj << '\n';
} catch(std::exception& ex) { handle_error(); };
close(sfd);
// CSocket inherit from CSocket_basic
CSocket sockObj;
try {
sockObj.bind(SOCK_STREAM, nullptr, AI_PASSIVE);
if (sockObj.local_saddr()) { // is socket bound?
std::cout << "socket prepared for listening on all local ip addresses.";
sockObj.listen();
} else
std::cerr << "failed to bind socket passive for listening.";
} catch(std::exception& ex) { handle_error(); };
void load()
Load the raw socket file descriptor specified with the constructor into the object.
Definition socket.cpp:196
Manage all aspects of a network socket.
Definition socket.hpp:320
void bind(const int a_socktype, const SSockaddr *const a_saddr=nullptr, const int a_flags=0)
Bind socket to an ip address of a local network adapter.
Definition socket.cpp:464
Trivial ::sockaddr structures enhanced with methods.
Definition sockaddr.hpp:94
Returns
true  if socket is bound to one or all ip address of local network adapters
false otherwise
Exceptions
std::runtime_errorsystem errors as specified.
Parameters
[out]a_saddrOptional: pointer to a socket address object that will be filled with the address information of a local network adapter that the socket is bound to. If an error is thrown, this socket address object is not modified. If no information is available (the socket is not bound to a local network adapter) an unspecified socket address (":0") is returned, possibly with address family ("[::]:0" or 0.0.0.0:0"). But working with IP-version is not intended.

Definition at line 231 of file socket.cpp.

+ Here is the caller graph for this function:

◆ remote_saddr()

bool UPnPsdk::CSocket_basic::remote_saddr ( SSockaddr a_saddr = nullptr) const

Get the remote socket address the socket is connected to.

For an example look at CSocket_basic::local_saddr().

Returns
true  if socket is connected
false otherwise
Exceptions
std::runtime_errorsystem errors as specified except "The socket is not connected".
Parameters
[out]a_saddrOptional: pointer to a socket address object that will be filled with the remote address information. If an error is thrown, this socket address object is not modified. If no information is available (the socket is not connected to a remote peer) an unspecified socket address (":0") is returned, possibly with address family ("[::]:0" or 0.0.0.0:0"). But working with IP-version is not intended.

Definition at line 287 of file socket.cpp.

+ Here is the call graph for this function:

◆ socktype()

int UPnPsdk::CSocket_basic::socktype ( ) const

Get the socket type.

Returns
SOCK_STREAM or SOCK_DGRAM.
Exceptions
std::runtime_errorif query option fails.

Definition at line 337 of file socket.cpp.

+ Here is the call graph for this function:

◆ sockerr()

int UPnPsdk::CSocket_basic::sockerr ( ) const

Get the error that is given from the socket as option.

This is not a system error from the operating system (with POSIX returned in errno). It is the error that can be queried as option from the socket.

Returns
error number.
Exceptions
std::runtime_errorif query option fails.

Definition at line 358 of file socket.cpp.

+ Here is the call graph for this function:

◆ is_reuse_addr()

bool UPnPsdk::CSocket_basic::is_reuse_addr ( ) const

Get status if reusing address is enabled.

For details to this option have a look at option "reuse address".

Returns
true  if reuse address is enabled
false otherwise
Exceptions
std::runtime_errorif query option fails.

Definition at line 379 of file socket.cpp.

+ Here is the call graph for this function:

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