(file) Return to OpenSLPWrapper.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Client

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2