Segmentation fault in gSoap client - c++

I'm using gSoap for fetch the server's TimeZone from exchange server, My client code is:
#include "soapExchangeServiceBindingProxy.h"
#include "ExchangeServiceBinding.nsmap"
int main()
{
ExchangeServiceBindingProxy service;
service.soap_endpoint = "https://outlook.office365.com/ews/exchange.asmx";
ns1__GetServerTimeZonesType request;
__ns1__GetServerTimeZonesResponse response;
const std::string s("Eastern Standard Time");
request.Ids = (ns2__NonEmptyArrayOfTimeZoneIdType *)&s;
if(service.GetServerTimeZones(&request , response) == SOAP_OK){
std::cout << "Success Response: "<< response.ns1__GetServerTimeZonesResponse << std::endl;
}
else
service.soap_stream_fault(std::cerr);
service.destroy(); // delete data and release memory
return 0;
}
This code compiled successfully but when i tried to run it, it give Segmentation fault, I'm newbie in C++ and gSoap also so i don't know exactly what am i doing wrong here?
If you need more code for this, i'll surly provide it to you, please let me know for this. Any help appreciated. thank you.

Related

curlpp response is doubled

I'm just new to curlpp but I can't see what's wrong here with my code, so I hope someone can help me out.
I'm using curlpp in C++ to make Facebook Graph queries. That means the Facebook server will return Json data as result.
My code looks as follows:
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
...
curlpp::Easy myRequest;
bool error = false;
QString result = "";
try {
// Setting the URL to the Facebook server with the query
curlpp::options::Url myUrl(request->getURL().toStdString());
// Creating stream for the result
std::ostringstream os;
curlpp::options::WriteStream ws(&os);
// setting my opts: url and output stream
myRequest.setOpt(myUrl);
myRequest.setOpt(ws);
// perform the request
myRequest.perform();
// stream the result into my stream
os << myRequest;
result = QString::fromStdString(os.str());
} catch (curlpp::RuntimeError &e) {
error = true;
qWarning() << "Error in HttpRequest execution:" << e.what();
} catch (curlpp::LogicError &e) {
error = true;
qWarning() << "Error in HttpRequest execution:" << e.what();
} catch (...) {
error = true;
qWarning() << "Unknown error in HttpRequest execution.";
}
My problem now is that the resulting stream (and thus my result QString) do contain the Json object sent by the Facebook Graph server, but twice.
That means, directly two times the same object, one after the other. And that makes it invalid Json.
But that can not be what the server delivers. And it's not what I get when I do check it with the openssl command line tool and make a HTTP Get request on my own.
What's wrong with my code?
Best, Michael

C++ Http Request with POCO

I'm wondering how I can do a request to a URL (e.g. download a picture and save it) with POCO in C++?
I got this little code so far
#include <iostream>
#include <string>
#include "multiplication.h"
#include <vector>
#include <HTTPRequest.h>
using std::cout;
using std::cin;
using std::getline;
using namespace Poco;
using namespace Net;
int main() {
HTTPRequest *test = new HTTPRequest("HTTP_GET", "http://www.example.com", "HTTP/1.1");
}
Normally POCO has a great advantage to be very simple even when you know nothing about it and you do not need middle/advance C++ knowledge like you need for boost/asio ( e.g what means enable_share_from_this ... )
Under the poco "installation directory" you find the sample directory, (in my case under poco\poco-1.4.6p4\Net\samples\httpget\src ).
On-line help browsing is also easy and fast (for example browsing classes).
If your understanding of C++ in not enough at the present time go to the university library and borrow Scott Meyers books (Effective C++ and after More effective C++ )
So we adapt the sample code httpget.cpp to the minimal required.
Inside the main:
URI uri("http://pocoproject.org/images/front_banner.jpg");
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
HTTPResponse response;
if (!doRequest(session, request, response))
{
std::cerr << "Invalid username or password" << std::endl;
return 1;
}
and the function almost untouched:
bool doRequest(Poco::Net::HTTPClientSession& session,
Poco::Net::HTTPRequest& request,
Poco::Net::HTTPResponse& response)
{
session.sendRequest(request);
std::istream& rs = session.receiveResponse(response);
std::cout << response.getStatus() << " " << response.getReason() << std::endl;
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
{
std::ofstream ofs("Poco_banner.jpg",std::fstream::binary);
StreamCopier::copyStream(rs, ofs);
return true;
}
else
{
//it went wrong ?
return false;
}
}
I let you arrange things for you and see where the image lands on your disk.
Hope it will help

Remote control of C++ program in Ubuntu 12.04 via HTTPS

I have an emulator program written in C++ running on Ubuntu 12.04. There are some settings and options needed for running the program, which are given by the main's arguments. I need to query and control these options via HTTPS from a remote machine/mobile device. I was wondering if someone can help me with that.
There should probably be some libraries for the ease, for example libcurl. I'm not sure how suitable it is for my case, but here is any example of connection setup in libcurl. It's not a must to use any libraries though; just the most efficient/simplest way.
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
using namespace curlpp::options;
int main(int, char **)
{
try
{
// That's all that is needed to do cleanup of used resources (RAII style).
curlpp::Cleanup myCleanup;
// Our request to be sent.
curlpp::Easy myRequest;
// Set the URL.
myRequest.setOpt<Url>("http://example.com");
// Send request and get a result.
// By default the result goes to standard output.
myRequest.perform();
}
catch (curlpp::RuntimeError &e)
{
std::cout << e.what() << std::endl;
}
catch (curlpp::LogicError &e)
{
std::cout << e.what() << std::endl;
}
return 0;
}

Unable to connect to Websphere MQ Manager using XMS

This code reproduces the issue:
#include <iostream>
#include <xms.hpp>
int main()
{
try
{
xms::ConnectionFactory factory;
factory.setIntProperty(XMSC_CONNECTION_TYPE, XMSC_CT_WMQ);
factory.setIntProperty(XMSC_WMQ_CONNECTION_MODE, XMSC_WMQ_CM_CLIENT);
factory.setStringProperty(XMSC_WMQ_QUEUE_MANAGER, "my.queue.manager");
factory.setStringProperty(XMSC_WMQ_HOST_NAME, "my.dev.mq");
factory.setStringProperty(XMSC_WMQ_CHANNEL, "my.channel");
factory.setIntProperty(XMSC_WMQ_PORT, 1414);
std::cout << "Is Factory Null? => " << factory.isNull() << std::endl;
xms::Connection conn = factory.createConnection(); //THIS THROWS EXCEPTION
std::cout << "Is Factory Null? => " << factory.isNull() << std::endl;
}
catch(xms::Exception const & e)
{
e.dump(std::cout);
}
}
It throws exception, dumping the following message:
Is Factory Null? => 0
Exception:
ErrorData =
ErrorCode = 26 (XMS_E_CONNECT_FAILED)
JMS Type = 1 (XMS_X_GENERAL_EXCEPTION)
Linked Exception:
ErrorData = libmqic_r.so
ErrorCode = 0 (XMS_E_NONE)
JMS Type = 1 (XMS_X_GENERAL_EXCEPTION)
Any idea what is wrong with the code?
Note that devpmmq is just an alias to the actual IP address (host). If I put any random/nonsense value for it, I get the same error, which is bad because the API should have given a better error message such as "host not found" or something alone that line. Is there any way to enable more verbose diagnostics?
XMS C/C++ internally loads WebSphere MQ client libraries. The problem here is that XMS is unable to find WebSphere MQ client library libmqm_r.so.
What is puzzling me is that the code has set XMSC_WMQ_CONNECTION_MODE as XMSC_WMQ_CM_CLIENT but XMS is attempting to load libmqm_r.so. It should have attempted to load libmqic_r.so.
Have you installed WebSphere MQ client? Also what is the version of XMSClients can be downloaded from here.

Poco stops after SMTPClientSession.login

I just started with the Poco library and tried to create an email program (Which I knew virtually nothing about). The following is my code (There may be other problems with it besides the one I've encountered so far, but I just started working on it)
int main(int argc, char** argv)
{
Poco::Net::SocketAddress add("smtp.gmail.com:465");
Poco::Net::StreamSocket sock(add);
Poco::Net::SMTPClientSession sess(sock);
std::cout << "-";
sess.login(
"gmail.com",
Poco::Net::SMTPClientSession::AUTH_LOGIN,
"----",
"----"
);
Poco::Net::MailMessage msg;
Poco::Net::MailRecipient resp(Poco::Net::MailRecipient::PRIMARY_RECIPIENT,"michaelrgoldfine#gmail.com");
msg.addRecipient(resp);
std::string content("HELP SOS");
msg.encodeWord(content);
std::cout << msg.getContent() << "-";
}
When I go into the debugger, it runs fine until it gets to sess.login then suddenly the little bar that represents were I am in the code disappears but the program keeps running (I'm not experienced enough to know what that means). None of the cout stuff I put in actually prints, the debugger just goes past that line but nothing shows up. After a little while this comes up:
terminate called throwing an exception
So what's going on?
You are attempting to use SMTP over TLS (the port 465 passed to the SocketAddress). In one shot you have to learn (1) TLS and certificate handling in POCO, before focusing on (2) your goal: sending an email message.
I suggest to start learning POCO with simpler examples. You can find sample code in the various samples directories in the POCO source code.
I think that your code is just hanging on the TLS handshake, because it doesn't know what to do.
These are the fixes you should do before looking at the solution:
Place your code inside a try/catch block. POCO uses exceptions.
Replace StreamSocket with SecureStreamSocket.
The simplest way to properly initialize SecureStreamSocket is via the Application class. See the Applications slides and Util/samples/SampleApp/src/SampleApp.cpp.
See the documentation for the SSLManager for how to properly tell the Application which certificates to use.
Don't specify an hostname to the login() method. The hostname is optional and should be the client hostname, not the server (See the SMTP RFC).
Remember to actually send the message! Your code is not sending it :-)
OK, and now for the running code. I left steps 4 and 6 as an exercise, but this code will at least run the TLS handshake, will tell you that it cannot verify the server's certificate and, if you answer Yes on the terminal to the questions on the certificates, it will fail the SMTP authentication.
class MiniApp : public Poco::Util::Application {
int main(const vector <string>& args) {
try {
Poco::Net::SocketAddress add("smtp.gmail.com:465");
Poco::Net::SecureStreamSocket sock(add);
Poco::Net::SMTPClientSession session(sock);
session.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, "user", "pw");
Poco::Net::MailMessage msg;
Poco::Net::MailRecipient recipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT,
"michaelrgoldfine#gmail.com");
msg.addRecipient(recipient);
string content("HELP SOS");
msg.encodeWord(content);
} catch (Poco::Exception& e) {
cout << "Error: " << e.displayText() << endl;
return -1;
}
return 0;
}
};
POCO_APP_MAIN(MiniApp)
Yes, so I struggled with login(), trying to use smtp.gmail.com. This is the excerpt of the communication with the SSL session that made it work.
string host("smtp.gmail.com")
Poco::UInt16 port = 587;
SecureSMTPClientSession session(host, port);
session.open();
Poco::Net::initializeSSL();
SharedPtr<InvalidCertificateHandler> ptrHandler = new AcceptCertificateHandler(false);
Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH");
SSLManager::instance().initializeClient(0, ptrHandler, ptrContext);
try
{
// SSL
session.login();
if (session.startTLS(ptrContext))
{
session.login(SMTPClientSession::AUTH_LOGIN, "user#gmail.com", "yourpassword");
session.sendMessage(message);
}
session.close();
Poco::Net::uninitializeSSL();
}
catch (SMTPException &e)
{
cout << e.message() << endl;
session.close();
Poco::Net::uninitializeSSL();
return 0;
}
Original source:
http://www.axistasoft.sg/tutorials/cpp/poco/item/sending-email-messages-using-smtp-protocol