1 karl 1.2 //%2003////////////////////////////////////////////////////////////////////////
|
2 tony 1.1 //
|
3 karl 1.2 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
|
7 tony 1.1 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Tony Fiorentino (fiorentino_tony@emc.com)
27 //
|
28 tony 1.3 // Modified By: Keith Petley (keithp@veritas.com)
|
29 tony 1.1 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include "OpenSLPWrapper.h"
|
33 tony 1.3 #include <ctype.h>
|
34 tony 1.1
35 PEGASUS_NAMESPACE_BEGIN
36
|
37 tony 1.3 // preprocessAttrList basically strips and folds the whitespace in the supplied
38 // list. It overwrites the existing list with its output as it goes.
39 //
40 // (The output will always be equal or shorter to the original in length).
41 //
42 // The input string is of the form:
43 // [WS]Tag[WS] and
44 // [WS]([WS]Tag[WS]=[WS]Attr[EWS]Val[WS],[WS]val2[WS])[WS]
45 // The input string consists of a 1 or more instances of the above strings
46 // in a comma separated list.
47 // [WS] = WhiteSpace and is deleted
48 // [EWS] = Embedded WhiteSpace and will be folded to a single space.
49
50 static void
51 preprocessAttrList(char *list)
52 {
53 bool eatWhiteSpace = false;
54 char *ptr = list;
55 char *wptr = list;
56 char last;
57
58 tony 1.3 while(*ptr != '\0') {
59 // Outer switch case deals with everything outsde of brackets ( )
60 switch(*ptr) {
61 // We delete all whitespace outside of ( )
62 case ' ':
63 case '\t':
64 last = ' ';
65 break;
66
67 case '(':
68 // Start of a ( ) section
69 last = '(';
70 *wptr++ = *ptr;
71 // Process everything until closing )
72 while(*++ptr != '\0' && *ptr != ')') {
73 switch(*ptr) {
74 case ' ':
75 case '\t':
76 // if we're deleting WS
77 if(eatWhiteSpace == true){
78 last = ' ';
79 tony 1.3 break;
80 }
81 // if we're folding WS
82 if(last == ' ') {
83 break;
84 }
85 last = ' ';
86 *wptr++ = ' ';
87 break;
88
89 case ',':
90 case '=':
91 // delete any preceding WS.
92 // It will have been folded to a
93 // single space
94 if(last == ' '){
95 wptr--;
96 }
97 *wptr++ = *ptr;
98 last = *ptr;
99 // flag to delete any following WS
100 tony 1.3 eatWhiteSpace = true;
101 break;
102
103 default:
104 // tag or attribute character
105 // flag to fold embedded WS
106 eatWhiteSpace = false;
107 last = *ptr;
108 *wptr++ = *ptr;
109 }
110 }
111
112 // delete any WS before the closing )
113 if(*ptr == ')' && last == ' ') {
114 wptr--;
115 }
116
117 // we're outside the ( ), so flag to delete WS
118 eatWhiteSpace = true;
119 *wptr++ = *ptr;
120 break;
121 tony 1.3
122 default:
123 // Not WS, not inside ( ), so just copy it
124 *wptr++ = *ptr;
125 }
126 ptr++;
127 }
128 // Make sure "new" string is properly terminated.
129 *wptr = '\0';
130 }
131
132 // Assumes a list that has already been whitespace stripped/folded
133 // Overwrites input string with output.
134
135 static void
136 preprocessAttrValList(char *list)
137 {
138 char *wptr = list;
139 char *ptr = list;
140 char last = '\0';
141 bool opaque = false;
142 tony 1.3
143 while(*ptr != '\0') {
144
145 if(opaque == true) {
146 *wptr++ = *ptr++;
147 continue;
148 }
149 #ifdef STRIP_QUOTES
150 if(*ptr == '"') {
151 ptr++;
152 continue;
153 }
154 #endif
155
156 // Escape character or opaque sequence
157 if(*ptr == '\\') {
158 if( *(ptr+1) != '\0' && *(ptr+2) != '\0') {
159 if(*(ptr+1) == 'F' && *(ptr+2) == 'F') {
160 opaque = true;
161 *wptr++ = *ptr++;
162 } else {
163 tony 1.3 // Allow any character to be escaped
164 // rfc2608 says to only escape
165 // reserved characters
166 //
167 Uint8 n = *++ptr;
168 if(isdigit(*ptr))
169 n = (*ptr - '0');
170 else if(isupper(*ptr))
171 n = (*ptr - 'A' + 10);
172 else
173 n = (*ptr - 'a' + 10);
174 *wptr = ((n & 0xf) << 4);
175
176 // Do second nibble
177 n = *++ptr;
178 if(isdigit(*ptr))
179 n = (*ptr - '0');
180 else if(isupper(*ptr))
181 n = (*ptr - 'A' + 10);
182 else
183 n = (*ptr - 'a' + 10);
184 tony 1.3 *wptr += (n & 0xf);
185 wptr++;
186 ptr++;
187 continue;
188 }
189 }
190 }
191 // nothing special, just copy it
192 *wptr++ = *ptr++;
193 }
194 *wptr = '\0';
195 }
196
|
197 tony 1.1 static SLPBoolean
198 wbemSrvUrlCallback(SLPHandle hslp,
199 const char* srvurl,
200 unsigned short lifetime,
201 SLPError errcode,
202 void* cookie)
203 {
204 if (errcode == SLP_OK)
205 {
206 Array<String> *url_cookie = static_cast<Array<String> *>(cookie);
207 url_cookie->append(srvurl);
208 }
209
210 return SLP_TRUE;
211 }
212
213 static SLPBoolean
214 wbemAttrCallback( SLPHandle hslp,
|
215 tony 1.3 const char* attrlist,
216 SLPError errcode,
217 void* cookie)
|
218 tony 1.1 {
|
219 tony 1.3 if (errcode == SLP_OK)
220 {
221 Array<Attribute> *attr_cookie = static_cast<Array<Attribute> *>(cookie);
222
223 // Allocate memory to hold working copy of list
224 char *list = new char[strlen(attrlist) + 1];
225
226 if(list == (char *)NULL) {
227 // Ignore out of memory, just go.
228 return SLP_TRUE;
229 }
230
231 strcpy (list, attrlist);
232
233 // Remove/fold whitespace
234 preprocessAttrList(list);
235
236 char *end = list + strlen(list);
237 char *ptr = list;
238 char *nptr;
239
240 tony 1.3 while (ptr <= end) {
241 if(*ptr == '(') {
242 ptr++;
243 nptr = ptr;
244 while(nptr <= end && *nptr != '=') {
245 nptr++;
246 }
247 *nptr = '\0';
248 preprocessAttrValList(ptr);
249 Attribute newAttr(ptr);
250 ptr = nptr+1;
251 while(nptr <= end) {
252 if(*nptr == ',' || *nptr == ')') {
253 bool isBracket = false;
254 if(*nptr == ')'){
255 isBracket = true;
256 }
257 *nptr = '\0';
258 preprocessAttrValList(ptr);
259 newAttr.addValue(ptr);
260 ptr = nptr + 1;
261 tony 1.3 if(isBracket){
262 break;
263 }
264 } else {
265 nptr++;
266 }
267 }
268 attr_cookie->append(newAttr);
269 }
270
271 if(*ptr == ',') {
272 ptr++;
273 continue;
274 }
275
276 nptr = ptr;
277 while(nptr <= end && *nptr != ',') {
278 nptr++;
279 }
280 *nptr = '\0';
281 attr_cookie->append(Attribute(ptr));
282 tony 1.3 ptr = nptr+1;
283 continue;
284 }
285 delete [] list;
286 }
287 return SLP_TRUE;
|
288 tony 1.1 }
289
290 CIMServerDiscoveryRep::CIMServerDiscoveryRep()
291 {
292 }
293
294 CIMServerDiscoveryRep::~CIMServerDiscoveryRep()
295 {
296 }
297
298 Array<CIMServerDescription>
|
299 karl 1.4 CIMServerDiscoveryRep::lookup(const Array<Attribute> & criteria, const SLPClientOptions* options)
|
300 tony 1.1 {
301 SLPError result;
302 SLPHandle hslp;
303 Array<CIMServerDescription> connections;
304
305 // SLPOpen()
306 // @param1 - language - NULL is the default locale
307 // @param2 - async - FALSE is synchronous slp handle
308 // @param3 - handle - pointer to slp handle
309 if (SLPOpen(NULL, SLP_FALSE, &hslp) == SLP_OK)
310 {
311 Attribute attrServiceId;
312 for (Uint32 idx=0; idx<criteria.size(); idx++)
313 {
|
314 tony 1.3 if (criteria[idx].getTag() == PEG_WBEM_SLP_SERVICE_ID)
|
315 tony 1.1 {
316 attrServiceId = criteria[idx];
317 }
318 }
319
320 String serviceType(PEG_WBEM_SLP_TYPE);
|
321 tony 1.3 Array <String> serviceAttrList = attrServiceId.getValues();
322
323 if(serviceAttrList.size() != 0 && serviceAttrList[0] != String::EMPTY)
324 {
325 serviceType = serviceAttrList[0];
326 }
|
327 tony 1.1
328 // SLPFindSrvs()
329 // @param1 - handle - slp handle
330 // @param2 - service type - wbem
331 // @param3 - scope list - NULL is all localhost can query
332 // @param4 - filter - NULL is all that match type
333 // @param5 - pointer to custom data to use in callback
334 Array<String> urls;
|
335 tony 1.3 result = SLPFindSrvs(hslp,
|
336 tony 1.1 (const char *)serviceType.getCString(),
337 NULL,
338 NULL,
339 wbemSrvUrlCallback,
340 (void *)&urls);
341
342 if (result == SLP_OK)
343 {
344 for (Uint32 i=0; i<urls.size(); i++)
345 {
346 CIMServerDescription connection(urls[i]);
347
348 Array<Attribute> attributes;
349
350 // SLPFindAttrs()
351 // @param1 - handle - slp handle
352 // @param2 - service url or type
353 // @param3 - scope list - NULL is all localhost can query
354 // @param4 - attribute list - NULL is all attributes
355 // @param5 - pointer to custom data to use in callback
|
356 tony 1.3 result = SLPFindAttrs( hslp,
|
357 tony 1.1 (const char *)urls[i].getCString(),
358 NULL,
359 NULL,
360 wbemAttrCallback,
361 (void *)&attributes);
362
363 // SLPParseSrvURL()
364 // @param1 - url - obtained from SLPFindSrvs()
365 // @param2 - parsed url - output param
366 SLPSrvURL *pSrvUrl = NULL;
|
367 tony 1.3 if ( SLP_OK == SLPParseSrvURL(
368 (char *)((const char *)urls[i].getCString()),
|
369 tony 1.1 &pSrvUrl))
370 {
371 // add to the end to protect against existing attributes of the same name.
|
372 tony 1.3 Attribute tmpAttr(PEG_CUSTOM_ATTR_HOST);
373 tmpAttr.addValue(pSrvUrl->s_pcHost);
374 attributes.append(tmpAttr);
375
376 Attribute tmpAttr2(PEG_CUSTOM_ATTR_PORT);
|
377 tony 1.1 CIMValue value(Uint32(pSrvUrl->s_iPort));
|
378 tony 1.3 tmpAttr2.addValue(value.toString());
379 attributes.append(tmpAttr2);
|
380 tony 1.1
381 // free up slp library memory
|
382 tony 1.3 SLPFree(pSrvUrl);
|
383 tony 1.1 }
384 connection.setAttributes(attributes);
385 connections.append(connection);
386 }
387 }
388
389 // SLPClose()
390 // @param1 - handle - slp handle
391 SLPClose(hslp);
392 }
393
394 return connections;
395 }
396
397 PEGASUS_NAMESPACE_END
398
|