QT check if interface is real - c++

I am tryng to get the mac adress using QT. Right now I am using this code:
QStringList Util::getMac(){
QStringList items;
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
if (interface.flags().testFlag(QNetworkInterface::IsRunning))
foreach (QNetworkAddressEntry entry, interface.addressEntries())
{
if (interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains(".")){
items << interface.hardwareAddress();
}
}
}
return items;}
But the problem is that it also gives me mac adress of a hamachi interface. So is there any way to check if that interface is internet-accessible? Like that I would know that it is real and not virtual.

You could check the IP of the Interface via http://qt-project.org/doc/qt-4.8/qnetworkinterface.html#allAddresses and then see if it is an address that can connect to the internet

Related

Qpid proton c++ - proton::make_work

I'm trying to add proton::work function (opening a new sender) inside the work queue of the proton::connection object. I have a pointer to the working queue, but my problem is how to bind the open_sender function correctly.
I'm aware of the real problem here : the parameter of the function :
sender open_sender(const std::string& addr);
As the string is passed by reference, I have to de-reference it. I'm ok with that, but how to do it with the proton tools ?
Here my line of code :
proton::work w = proton::make_work( &proton::connection::open_sender, &m_connection, p_url);
Note :
Of course I'm not using C++11 in my project, it would be too simple
to ask ;) !
Of course I cannot change to C++11
If you have a better idea on how to create a new sender in a multi-threaded program let me know.
Usually you will use the proton::open_sender API from within the handler for connection open or container start so you will not have to use proton::make_work in most cases. If you look at the Proton C++ examples, a good place to start is simple_send.cpp.
Abbreviated code might look like this:
class simple_send : public proton::messaging_handler {
private:
proton::sender sender;
const std::string url;
const std::string addr;
...
public:
simple_send(...) :
url(...),
addr(...)
{}
...
// This handler is called when the container starts
void on_container_start(proton::container &c) {
c.connect(url);
}
// This handler is called when the connection is open
void on_connection_open(proton::connection& c) {
sender = c.open_sender(addr);
}
...
}
int main() {
...
simple_send send(...);
proton::container(send).run();
...
}
There are other examples that come with Proton C++, that should help you figure out other ways to use Proton C++. See https://github.com/apache/qpid-proton/tree/master/examples/cpp.
There is also API documentation you can find at http://qpid.apache.org/releases/qpid-proton-0.20.0/proton/cpp/api/index.html (for the current release as of February 2018).

How to get client IP Address with C++ in thrift

I am implementing a thrift-based (0.4.0) service in C++ at the moment and encountered a question:
Is there a way to get the client's IP address from inside a service method implementation? I am using a TNonblockingServer.
Thanks in advance!
In TNonblockingServer, When TProcessor::process() is called the TProtocol.transport is a TMemoryBuffer, so aquiring client ip address is impossible.
But We can extend class TServerEventHandler, method TServerEventHandler::processContext() is called when a client is about to call the processor.
static boost::thread_specific_ptr<std::string> thrift_client_ip; // thread specific
class MyServerEventHandler : public TServerEventHandler
{
virtual void processContext(void* serverContext, boost::shared_ptr<TTransport> transport)
{
TSocket *sock = static_cast<TSocket *>(transport.get());
if (sock)
{
//thrift_client_ip.reset(new string(sock->getPeerAddress())); // 0.9.2, reused TNonblockingServer::TConnection return dirty address, see https://issues.apache.org/jira/browse/THRIFT-3270
sock->getCachedAddress(); // use this api instead
}
}
};
// create nonblocking server
TNonblockingServer server(processor, protocolFactory, port, threadManager);
boost::shared_ptr<MyServerEventHandler> eventHandler(new MyServerEventHandler());
server.setServerEventHandler(eventHandler);
Ticket THRIFT-1053 describes a similar request for Java. The solution is basically to allow access to the inner (endpoint) transport and retrieve the data from it. Without having it really tested, building a similar solution for C++ should be easy. Since you are operating on Thrift 0.4.0, I'd strongly recommend to look at current trunk (0.9.3) first. The TBufferedTransport, TFramedTransport and TShortReadTransport already implement
boost::shared_ptr<TTransport> getUnderlyingTransport();
so the patch mentioned above may not be necessary at all.
Your TProcessor-derived class gets a hold of both transports when process() gets called. If you overwrite that method you should be able to manage access to the data you are interested in:
/**
* A processor is a generic object that acts upon two streams of data, one
* an input and the other an output. The definition of this object is loose,
* though the typical case is for some sort of server that either generates
* responses to an input stream or forwards data from one pipe onto another.
*
*/
class TProcessor {
public:
// more code
virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
boost::shared_ptr<protocol::TProtocol> out,
void* connectionContext) = 0;
// more code
#ifndef NONBLOCK_SERVER_EVENT_HANDLER_H
#define NONBLOCK_SERVER_EVENT_HANDLER_H
#include <thrift/transport/TSocket.h>
#include <thrift/server/TServer.h>
namespace apache{
namespace thrift{
namespace server{
class ServerEventHandler:public TServerEventHandler{
void* createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output){
(void)input;
(void)output;
return (void*)(new char[32]);//TODO
}
virtual void deleteContext(void* serverContext,
boost::shared_ptr<TProtocol>input,
boost::shared_ptr<TProtocol>output) {
delete [](char*)serverContext;
}
virtual void processContext(void *serverContext, boost::shared_ptr<TTransport> transport){
TSocket *tsocket = static_cast<TSocket*>(transport.get());
if(socket){
struct sockaddr* addrPtr;
socklen_t addrLen;
addrPtr = tsocket->getCachedAddress(&addrLen);
if (addrPtr){
getnameinfo((sockaddr*)addrPtr,addrLen,(char*)serverContext,32,NULL,0,0) ;
}
}
}
};
}
}
}
#endif
boost::shared_ptr<ServerEventHandler> serverEventHandler(new ServerEventHandler()
server.setServerEventHandler(serverEventHandler);

How to parse network hostname + port in qt?

I am currently writing a network application in Qt and need to seperate network adresses in the form:
example.org:1234
into seperate hostname and port QStrings.
Is there a Qt function to easily parse this and check if the given input is correct?
Thanks in advance!
This is quite simple; you just use the QUrl class for this with the constructor, host() and port() methods as follows:
QUrl url("http://example.org:1234")
qDebug() << "Host:" << url.host();
qDebug() << "Port:" << url.port();
As for your comment for avoiding the scheme usage in each url, you could use this:
url.setScheme("ftp");
or
url.setScheme("http");
Yes, you should use the QUrl::fromUserInput function to parse the string, and then the host and port methods of the QUrl object to get the QStrings that you want.
auto url{ QUrl::fromUserInput(address) };
auto host{ url.host() };
auto port{ QString::number(url.port()) };

Segmentation fault when returning a sequence CORBA C++ Server Java Client

I'm developing a project in which I need to implement a Chat server in C++ and Chat clients in Java using swing for implementing the GUI.
I'm having exactly this problem, implementing nearly the same project:
Segmentation fault CORBA C++
This is the part of my IDL where the problem is:
typedef sequence<ICliente> ListaClientes;
interface IServer
{
attribute ListaClientes lista;
void registrar_usuario (in ICliente cliente) raises (usuarioRegistrado);
IConversacion crear_conversacion (in ICliente cliente);
ListaClientes obtener_lista();
void dar_baja(in ICliente cliente);
};
And then the part of the code where I'm getting the segmentation fault is this one:
ListaClientes* Server_i::obtener_lista() {
return userList._retn();
}
Where:
ListaClientes_var userList=new ListaClientes(size);
Or:
ListaClientes* userList=new ListaClientes(size);
ListaClientes* Server_i::obtener_lista() {
return userList;
}
It should be
ListaClientes* Server_i::obtener_lista() {
ListaClientes_var my_list = new ListaClientes(size);
my_list->length (size);
// Fill the elements in the newly created sequence by doing a duplicate of each object reference as it exists in the member variable
for(CORBA::ULong i=0;i<userList->length();i++){
my_list[i] = ICliente::_duplicate(userList[i]);
}
return my_list._retn();
}

C++ List Iterator Incompatible while Printing a list

Well guys, I've been looking for an answer to this error but I haven't got a specific one for my case.
I have a class User, each User has its own list of Computers, the class Computer is composed by these three classes ( Operative Sistem, Memory, and Processor). So Computer has its own toString that calls the specific toString from its components named above.
So...User has his atribute list computerList;
In other class, that I called Controler, I have a function for printing the computer list from a specific user.
Here is my function:
void printComputerList(User* u){
list<Computer*>::iterator itr;
for(itr=u->getComputerList().begin(); itr!=u->getComputerList().end(); itr++){
cout<<(*itr)->toString(); //(*itr) calls its own toString implemented in the class Computer
}
}
So, when I'm running the program, when I choose to print the list that I've already filled
I get the error from the title.
I think it is maybe some kind of confusion between the totrings ?
PD: I can post the rest of the code if it is necesary
Thanks!
There is (at least) one problem with temporary list. Fixed version would look like:
void printComputerList(User* u){
list<Computer*> const computers = u->getComputerList();
list<Computer*>::const_iterator it = computers.begin();
while (it != computers.end())
{
cout << (*it)->toString(); //(*it) calls its own toString implemented in the class Computer
++it;
}
}
Are you sure, that pointers on list are valid (non-null, not dangling ones)?