pidf.c 4.1 KB

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