move pcucontrol package into pcucontrol module.
[pcucontrol.git] / pcucontrol / models / intelamt / Include / CommonDefinitions.h
1 //----------------------------------------------------------------------------
2 //
3 //  Copyright (C) Intel Corporation, 2004 - 2006.
4 //
5 //  File:       CommonDefinitions.h
6 //
7 //  Contents:   Sample code for an IntelĀ® AMT Network client.
8 //
9 //  Notes:      This file contains type, function and constant definitions
10 //              used throughout the code of the all sample applications.
11 //
12 //----------------------------------------------------------------------------
13
14 #ifndef COMMON_DEFINITIONS_H
15 #define COMMON_DEFINITIONS_H
16
17
18 #include "StatusCodeDefinitions.h"
19 #ifdef _WIN32
20 /*
21  * gsoapWinHTTP.h for gSoap WinHTTP extension - needed for TLS support
22  */
23 #include "gsoapWinHttp.h"
24 #include "StatusStrings.h"
25 #include <conio.h>
26 #else
27 /*
28  * httpDigest.h for gSoap HTTP Digest support
29  */
30 #include "httpDigest.h"
31 #endif
32
33 /*
34  * Function prototypes
35  */
36 void PrintAuthenticationNote();
37 bool CheckReturnStatus(unsigned int res, unsigned long status,const char *message);
38 bool ValidateIP(const char *uri);
39 void GetString(char *msg, char *s, bool hidden);
40 bool ChangeService(const char *uri, const char *newService, char *newUri);
41 bool DisplayWarning(const char *msg);
42 bool GetNumber(int *number);
43 void ReplaceSubstring(const char *oldString,const char *oldSubstr, 
44                                         const char *newSubstr, char *newString);
45 void SetIPAddress(unsigned long &address, unsigned long bytes[]);
46 void NumberToIP(unsigned long address, unsigned long bytes[]);
47 void NumberToString(unsigned long number, char *string);
48 void IpToString(unsigned long address, char *string);
49 void StringToIP(const char *string, unsigned long &address);
50 void GuidToString(const unsigned char* guid, char* string);
51 bool ExtractIPFromUri(const char *uri, char *baseUrl);
52 void IsEmulator(const char *targetUrl, int *isEmulator);
53 bool GetOption(int *commandLineLength, char *argv[], int numOfArgs,
54                            char **option, char **commandLine[]);
55 void PrintSuccess(bool print = true);
56 void FunctionCall(const char *message);
57 #ifdef _WIN32
58 bool ParseCommandLine(int commandLineLength,char* commandLine[],char **target,
59                                           char **certName, bool *local, bool *krb,bool *verbose = NULL, 
60                       char **username = NULL, char **password = NULL);
61 #define CHAR _TCHAR
62 #else
63 bool ParseCommandLine(int commandLineLength,char* commandLine[],char **target, 
64                                           char **certName = NULL,char **certPass = NULL, 
65                       bool *verbose = NULL, 
66                                           char **username = NULL, char **password = NULL);
67 #define CHAR char
68 #endif
69
70 /*
71  * Constants for the common use
72  */
73 static const int MAX_LINE_LEN = 1024;
74 static const int TIMEOUT = 80;
75 static const char *DEFAULT_USERNAME = "admin";
76 static const char *DEFAULT_PASSWORD = "admin";
77 static const char *CERT_NAME = "-certName";
78 static const char *USER = "-user";
79 static const char *PASS = "-pass";
80 static const char *VERBOSE = "-verbose";
81 #ifdef _WIN32
82 static const char *LOCAL = "-local";
83 static const char *KRB = "-krb";
84 #else
85 static const char *CERT_PASS = "-certPass";
86 #endif
87
88 /*
89  * The structure that represents 
90  * the gSOAP rintime environment
91  */
92 class Soap
93 {
94 private:
95   struct soap *m_soap;
96   char *m_username;
97   char *m_password;
98   char *m_ip;
99
100 public:
101     // Constructor
102 #ifdef _WIN32
103   Soap(const char *url, const char *certName, 
104            const char *username, const char *password,
105        bool local, bool krb)
106 #else
107   Soap(const char *url, const char *certName, 
108            const char *certPass, const char *username, 
109        const char *password)
110 #endif
111   {
112     m_username = new char[MAX_LINE_LEN];
113     m_password = new char[MAX_LINE_LEN];
114     m_ip = new char[MAX_LINE_LEN];
115     SetIp(url);
116     SetUsername(DEFAULT_USERNAME);
117     SetPassword(DEFAULT_PASSWORD);
118
119         if (
120 #ifdef _WIN32
121         krb == false &&
122 #endif
123         !username)
124         {
125                 // To use the default user name, comment the following line:
126                 GetString("Username: ", m_username, false);  
127         }
128         else
129         {
130                 SetUsername(username);
131         }
132
133         if (
134 #ifdef _WIN32
135         krb == false &&
136 #endif
137         !password)
138         {
139                 // To use the default password, comment the following line:
140                 GetString("Password: ", m_password, true);
141         }
142         else
143         {
144                 SetPassword(password);
145         }
146         m_soap = soap_new();
147         if( m_soap )
148         {
149 #ifdef _WIN32
150           SetSoap(certName,local,krb);
151 #else
152           SetSoap(certName,certPass);
153 #endif
154
155         }
156   }
157
158   void Init(SOAP_NMAC struct Namespace *name = NULL)
159   {
160     m_soap->userid = m_username;
161     m_soap->passwd = m_password;
162
163     if(name != NULL)
164     {
165         // setting namespace for the runtime environment
166             soap_set_namespaces(m_soap, name);
167     }
168   }
169
170   char *GetIp()
171   {
172       return m_ip;
173   }
174
175   char *GetUsername()
176   {
177       return m_username;
178   }
179
180   char *GetPassword()
181   {
182       return m_password;
183   }
184
185   struct soap *GetSoap()
186   {
187       return m_soap;
188   }
189
190   void SetIp(const char *url)
191   {
192       memset(m_ip, 0, MAX_LINE_LEN);
193       if(url != NULL)
194       {
195         strncpy(m_ip, url, MAX_LINE_LEN - 1);
196       }
197   }
198
199   void SetUsername(const char *username)
200   {
201       memset(m_username,0,MAX_LINE_LEN);
202       if(username != NULL)
203       {
204         strncpy(m_username, username, MAX_LINE_LEN - 1);
205       }
206   }
207
208   void SetPassword(const char *password)
209   {
210       memset(m_password,0,MAX_LINE_LEN);
211       if(password != NULL)
212       {
213         strncpy(m_password, password, MAX_LINE_LEN - 1);
214       }
215   }
216
217 #ifdef _WIN32
218   void SetSoap(const CHAR *certName, bool local, bool krb)
219 #else
220   void SetSoap(const CHAR *certName, const char *certPass)
221 #endif
222   {
223     m_soap->recv_timeout    = TIMEOUT;
224     m_soap->send_timeout    = TIMEOUT;
225     m_soap->connect_timeout = TIMEOUT;
226     m_soap->accept_timeout  = TIMEOUT;
227
228 #ifdef _WIN32
229         // gsoap winhttp extension 
230         soap_register_plugin( m_soap, winhttp_plugin );
231     soap_omode(m_soap, SOAP_IO_KEEPALIVE);
232         if( certName )
233         {
234                 winhttp_set_certificate_name(m_soap, certName);
235         }
236
237     winhttp_set_local(m_soap,local);
238     winhttp_set_auth_scheme(m_soap,krb);
239 #else
240     // gsoap HTTP Digest plugin
241  if ( strncmp(m_ip+strlen(m_ip)-5, ".asmx", 5)) {
242         soap_register_plugin(m_soap, http_digest);
243     }
244   soap_omode(m_soap, SOAP_IO_KEEPALIVE);
245   soap_imode(m_soap, SOAP_IO_KEEPALIVE);
246         if ( !strncmp(m_ip, "https:", 6) )
247         {
248                 soap_ssl_client_context(m_soap,
249                                                                 SOAP_SSL_DEFAULT,
250                                                                 certName,
251                                                                 certPass,
252                                                                 "/usr/share/ssl/cert.pem",
253                                                                 "/usr/share/ssl/certs/", NULL);
254         }
255 #endif
256   }
257
258   // Destructor
259   ~Soap()
260   {
261       if(m_username)
262       {
263           delete [] m_username;
264           m_username = NULL;
265       }
266       if(m_password)
267       {
268           delete [] m_password;
269           m_password = NULL;
270       }
271       if(m_ip)
272       {
273           delete [] m_ip;
274           m_ip = NULL;
275       }
276           if( m_soap ){
277                 soap_destroy(m_soap);
278                 soap_end(m_soap);
279                 soap_done(m_soap);
280                 free(m_soap);
281                 m_soap = NULL;
282           }
283   }
284 };
285
286
287
288
289 #endif