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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2