294 lines
6.0 KiB
C++
Raw Permalink Blame History

//----------------------------------------------------------------------------
//
// Copyright (C) 2004 Intel Corporation
//
// File: SoapCommonDefinitions.h
//
// Contents: Sample code for an Intel<65> AMT Network client.
//
// Notes: This file contains type, function and constant definitions
// used throughout the code of the all SOAP sample applications.
//
//----------------------------------------------------------------------------
#ifndef SOAP_COMMON_DEFINITIONS_H
#define SOAP_COMMON_DEFINITIONS_H
#ifdef _WIN32
/*
* gsoapWinHTTP.h for gSoap WinHTTP extension - needed for TLS support
*/
#include "gsoapWinHttp.h"
#else
/*
* httpDigest.h for gSoap HTTP Digest support
*/
#include "httpDigest.h"
#endif
#include "CommonDefinitions.h"
#include <sstream>
#include <string>
using namespace std;
/*
* Function prototypes
*/
bool ChangeService(const char *uri, const char *newService, char *newUri);
/*
* The structure that represents
* the gSOAP runtime environment
*/
class Soap
{
private:
struct soap *m_soap;
char *m_username;
char *m_password;
char *m_ip;
char* m_proxy;
public:
// Constructor
#ifdef _WIN32
Soap(const char *url, const char *certName,
const char *username, const char *password,
bool local, bool krb)
#else
Soap(const char *url, const char *certName,
const char *certPass, const char *username,
const char *password)
#endif
{
m_username = new char[MAX_LINE_LEN];
m_password = new char[MAX_LINE_LEN];
m_ip = new char[MAX_LINE_LEN];
m_proxy = NULL;
SetIp(url);
SetUsername(DEFAULT_USERNAME);
SetPassword(DEFAULT_PASSWORD);
if (
#ifdef _WIN32
krb == false &&
#endif
!username)
{
// To use the default user name, comment the following line:
GetString("Username: ", m_username, false);
}
else
{
SetUsername(username);
}
if (
#ifdef _WIN32
krb == false &&
#endif
!password)
{
// To use the default password, comment the following line:
GetString("Password: ", m_password, true);
}
else
{
SetPassword(password);
}
m_soap = soap_new();
if( m_soap )
{
#ifdef _WIN32
SetSoap(certName,local,krb);
#else
SetSoap(certName,certPass);
#endif
}
}
void Init(SOAP_NMAC struct Namespace *name = NULL)
{
m_soap->userid = m_username;
m_soap->passwd = m_password;
if(name != NULL)
{
// setting namespace for the runtime environment
soap_set_namespaces(m_soap, name);
}
}
char *GetIp()
{
return m_ip;
}
char *GetUsername()
{
return m_username;
}
char *GetPassword()
{
return m_password;
}
struct soap *GetSoap()
{
return m_soap;
}
void SetIp(const char *url)
{
memset(m_ip, 0, MAX_LINE_LEN);
if(url != NULL)
{
if (strncpy_s(m_ip, MAX_LINE_LEN, url, MAX_LINE_LEN - 1)) {
printf("Error: Failed to set IP address");
exit(1);
}
}
}
void SetUsername(const char *username)
{
memset(m_username,0,MAX_LINE_LEN);
if(username != NULL)
{
if (strncpy_s(m_username, MAX_LINE_LEN, username, MAX_LINE_LEN - 1)) {
printf("Error: Failed to set user name");
exit(1);
}
}
}
void SetPassword(const char *password)
{
memset(m_password,0,MAX_LINE_LEN);
if(password != NULL)
{
if (strncpy_s(m_password, MAX_LINE_LEN, password, MAX_LINE_LEN - 1)) {
printf("Error: Failed to set password");
exit(1);
}
}
}
bool parseProxy(const char *proxy, int& port, string& proxyHost)
{
string uri = string(proxy);
string::size_type pos = uri.find_last_of(":");
if(pos == string::npos)
return false;
proxyHost = uri.substr(0, pos);
string port_section = uri.substr(pos+1, uri.length() - pos+2);
stringstream s(stringstream::in | stringstream::out);
s << port_section;
s >> port;
return true;
}
void SetProxy(const char *proxy,const char *proxy_username= NULL,const char *proxy_password=NULL)
{
int port = 0;
string proxyHost = "";
parseProxy(proxy, port, proxyHost);
if(m_proxy)
{
delete [] m_proxy;
m_proxy = NULL;
}
m_proxy = new char[MAX_LINE_LEN];
if (strncpy_s(m_proxy, MAX_LINE_LEN, proxyHost.c_str(), proxyHost.size() + 1))
{
printf("Error: Failed to set proxy host");
exit(1);
}
m_soap->proxy_host = m_proxy;
m_soap->proxy_port = port;
m_soap->proxy_userid = proxy_username;
m_soap->proxy_passwd = proxy_password;
#ifdef _WIN32
winhttp_set_proxy(m_soap);
#endif
}
#ifdef _WIN32
void SetSoap(const CHAR *certName, bool local, bool krb)
#else
void SetSoap(const CHAR *certName, const char *certPass)
#endif
{
m_soap->recv_timeout = TIMEOUT;
m_soap->send_timeout = TIMEOUT;
m_soap->connect_timeout = TIMEOUT;
m_soap->accept_timeout = TIMEOUT;
#ifdef _WIN32
// gsoap winhttp extension
soap_register_plugin( m_soap, winhttp_plugin );
soap_omode(m_soap, SOAP_IO_KEEPALIVE);
if( certName )
{
winhttp_set_certificate_name(m_soap, certName);
}
winhttp_set_local(m_soap,local);
winhttp_set_auth_scheme(m_soap,krb);
#else
// gsoap HTTP Digest plugin
if ( strncmp(m_ip+strlen(m_ip)-5, ".asmx", 5)) {
soap_register_plugin(m_soap, http_digest);
}
soap_omode(m_soap, SOAP_IO_KEEPALIVE);
soap_imode(m_soap, SOAP_IO_KEEPALIVE);
if ( !strncmp(m_ip, "https:", 6) )
{
soap_ssl_client_context(m_soap,
SOAP_SSL_DEFAULT,
certName,
certPass,
"/opt/intel/cert.pem",
"/opt/intel/", NULL);
}
#endif
}
// Destructor
~Soap()
{
if(m_username)
{
delete [] m_username;
m_username = NULL;
}
if(m_password)
{
delete [] m_password;
m_password = NULL;
}
if(m_proxy)
{
delete [] m_proxy;
m_proxy = NULL;
}
if(m_ip)
{
delete [] m_ip;
m_ip = NULL;
}
if( m_soap ){
soap_destroy(m_soap);
soap_end(m_soap);
soap_done(m_soap);
free(m_soap);
m_soap = NULL;
}
}
};
#endif