Hier ist eine weitere interessante Möglichkeit:
#include <sys/types.h> // u_int32_t
// next 3 for inet_ntoa() call
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// C++ headers
#include <iostream> // std::cout
#include <string>
#include <sstream> // ostringstream
// class to aid in using the gdb(3) debugger to print a u_int32_t ip_address as a string.
// The toString() is a static method!!
// How to call a C++ method from the gdb debugger, to simulate the inet_nto(3) method
//
// From within gdb debugger, you must have a process, so first stop at main:
// b main
// r
//
// (gdb) p MYIP4::toString(0x0fffefdfc)
// $1 = "255.254.253.252"
//
// (gdb) p MYIP4::toString(-1)
// $2 = "255.255.255.255"
//
// (gdb) p MYIP4::toString(65536)
// $3 = "0.1.0.0"
//
// Drawbacks: the a.out executable that you are debugging needs the MYIP4 class already
// compiled and linked into the executable that you are debugging.
//
// PS: I don't know if there is a "slick way" where the MyIP4 class could dynamically be loaded,
// within gdb(), if the executable failed to contain the MYIP4 class.
//
// PS: I had trouble with my operator() idea.. If you know how to improve on it, post away!
//
// @author 1201207 dpb created
//=============================
class MYIP4
{
public:
static std::string toString(u_int32_t ipv4_address )
{
struct in_addr temp_addr;
// inet_ntoa() returns a char * to a buffer which may be overwritten
// soon, so convert char* to a string for extra safety.
temp_addr.s_addr = htonl(ipv4_address);
std::string ipv4String = std::string(inet_ntoa( temp_addr ));
return ipv4String;
}
#if 0
// this idea did NOT work, so it is commented out.
//
// overload the () operator, so that, within gdb, we can simply type:
// p MYIP4(0x01020304)
// instead of:
// p MYIP4::toString(0x01020304)
std::string operator() ( u_int32_t ipv4_address )
{
return toString(ipv4_address);
}
#endif
};
void test1();
int main()
{
test1();
return 0;
}
void test1()
{
u_int32_t ipv4Address = 0x01020304; // host byte order for 1.2.3.4
std::cout << "Test1: IPAddress=" << MYIP4::toString(ipv4Address) << "\n";
}