pidf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * presence module - presence server implementation
  3. *
  4. * Copyright (C) 2006 Voice Sistem S.R.L.
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /*! \file
  24. * \brief Kamailio :: PIDF handling
  25. * \ingroup utils
  26. */
  27. /**
  28. * make strptime available
  29. * use 600 for 'Single UNIX Specification, Version 3'
  30. * _XOPEN_SOURCE creates conflict in header definitions in Solaris
  31. */
  32. #ifndef __OS_solaris
  33. #define _XOPEN_SOURCE 600 /* glibc2 on linux, bsd */
  34. #else
  35. #define _XOPEN_SOURCE_EXTENDED 1 /* solaris */
  36. #endif
  37. #include <time.h>
  38. #undef _XOPEN_SOURCE
  39. #undef _XOPEN_SOURCE_EXTENDED
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #include <libxml/parser.h>
  43. #include "../../dprint.h"
  44. #include "pidf.h"
  45. xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
  46. {
  47. xmlAttrPtr attr = node->properties;
  48. while (attr) {
  49. if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
  50. return attr;
  51. attr = attr->next;
  52. }
  53. return NULL;
  54. }
  55. char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
  56. {
  57. xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
  58. if (attr)
  59. return (char*)xmlNodeGetContent(attr->children);
  60. else
  61. return NULL;
  62. }
  63. xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
  64. {
  65. xmlNodePtr cur = node->children;
  66. while (cur) {
  67. if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
  68. return cur;
  69. cur = cur->next;
  70. }
  71. return NULL;
  72. }
  73. xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
  74. const char *ns)
  75. {
  76. xmlNodePtr cur = node;
  77. while (cur) {
  78. xmlNodePtr match = NULL;
  79. if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0) {
  80. if (!ns || (cur->ns && xmlStrcasecmp(cur->ns->prefix,
  81. (unsigned char*)ns) == 0))
  82. return cur;
  83. }
  84. match = xmlNodeGetNodeByName(cur->children, name, ns);
  85. if (match)
  86. return match;
  87. cur = cur->next;
  88. }
  89. return NULL;
  90. }
  91. char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
  92. const char *ns)
  93. {
  94. xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
  95. if (node)
  96. return (char*)xmlNodeGetContent(node->children);
  97. else
  98. return NULL;
  99. }
  100. xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
  101. {
  102. xmlNodePtr cur = doc->children;
  103. return xmlNodeGetNodeByName(cur, name, ns);
  104. }
  105. char *xmlDocGetNodeContentByName(xmlDocPtr doc, const char *name,
  106. const char *ns)
  107. {
  108. xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
  109. if (node)
  110. return (char*)xmlNodeGetContent(node->children);
  111. else
  112. return NULL;
  113. }
  114. time_t xml_parse_dateTime(char* xml_time_str)
  115. {
  116. struct tm tm;
  117. char * p;
  118. int h, m;
  119. char h1, h2, m1, m2;
  120. int sign= 1;
  121. signed int timezone_diff= 0;
  122. p= strptime(xml_time_str, "%F", &tm);
  123. if(p== NULL)
  124. {
  125. printf("error: failed to parse time\n");
  126. return 0;
  127. }
  128. p++;
  129. p= strptime(p, "%T", &tm);
  130. if(p== NULL)
  131. {
  132. printf("error: failed to parse time\n");
  133. return 0;
  134. }
  135. if(*p== '\0')
  136. goto done;
  137. if(*p== '.')
  138. {
  139. p++;
  140. /* read the fractionar part of the seconds*/
  141. while(*p!= '\0' && *p>= '0' && *p<= '9')
  142. {
  143. p++;
  144. }
  145. }
  146. if(*p== '\0')
  147. goto done;
  148. /* read time zone */
  149. if(*p== 'Z')
  150. {
  151. goto done;
  152. }
  153. if(*p== '+')
  154. sign= -1;
  155. p++;
  156. sscanf(p, "%c%c:%c%c", &h1, &h2, &m1, &m2);
  157. h= (h1- '0')*10+ h2- '0';
  158. m= (m1- '0')*10+ m2- '0';
  159. timezone_diff= sign* ((m+ h* 60)* 60);
  160. done:
  161. return (mktime(&tm) + timezone_diff);
  162. }