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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2