bdb_uri.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * $Id$
  3. *
  4. * BDB Database Driver for SIP-router
  5. *
  6. * Copyright (C) 2008 iptelorg GmbH
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SIP-router is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /** \addtogroup bdb
  25. * @{
  26. */
  27. /*! \file
  28. * Berkeley DB : The implementation of parser parsing bdb:.. URIs.
  29. *
  30. * \ingroup database
  31. */
  32. #include <string.h>
  33. #include "../../mem/mem.h"
  34. #include "../../ut.h"
  35. #include "bdb_uri.h"
  36. #ifndef CFG_DIR
  37. #define CFG_DIR "/tmp"
  38. #endif
  39. #define BDB_ID "bdb://"
  40. #define BDB_ID_LEN (sizeof(BDB_ID)-1)
  41. /** compare s1 & s2 with a function f (which should return 0 if ==);
  42. * s1 & s2 can be null
  43. * return 0 if match, 1 if not
  44. */
  45. #define cmpstr(s1, s2, f) \
  46. ((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
  47. /** Compares two BDB connection URIs.
  48. * This function is called whenever the database abstraction layer in
  49. * SER needs to compare to URIs with the bdb scheme. The function
  50. * compares hosts and port numbers of both URIs (host part comparison
  51. * is case insensitive). The URI comparison is mainly used to
  52. * by the connection pool to determine if a connection to a given
  53. * server already exists.
  54. **/
  55. static unsigned char bdb_uri_cmp(db_uri_t* uri1, db_uri_t* uri2)
  56. {
  57. bdb_uri_t * buri1, *buri2;
  58. if (!uri1 || !uri2) return 0;
  59. buri1 = DB_GET_PAYLOAD(uri1);
  60. buri2 = DB_GET_PAYLOAD(uri2);
  61. if (cmpstr(buri1->uri, buri2->uri, strcmp))
  62. return 0;
  63. return 1;
  64. }
  65. /*
  66. * Parse BDB URI of form
  67. * //path/to/dir
  68. *
  69. * Returns 0 if parsing was successful and -1 otherwise
  70. */
  71. int parse_bdb_uri(bdb_uri_t* res, str* uri)
  72. {
  73. str s;
  74. if(uri==NULL || uri->s==NULL)
  75. return -1;
  76. s = *uri;
  77. res->uri = (char*)pkg_malloc((s.len+1)*sizeof(char));
  78. if(res->uri == NULL)
  79. {
  80. ERR("bdb: no more pkg\n");
  81. return -1;
  82. }
  83. memcpy(res->uri, s.s, s.len);
  84. res->uri[s.len] = '\0';
  85. if(s.s[0]!='/')
  86. {
  87. res->path.s = (char*)pkg_malloc((sizeof(CFG_DIR)+s.len+2)*sizeof(char));
  88. memset(res->path.s, 0, (sizeof(CFG_DIR)+s.len+2)*sizeof(char));
  89. if(res->path.s==NULL)
  90. {
  91. ERR("bdb: no more pkg.\n");
  92. pkg_free(res->uri);
  93. res->uri = NULL;
  94. return -1;
  95. }
  96. strcpy(res->path.s, CFG_DIR);
  97. res->path.s[sizeof(CFG_DIR)] = '/';
  98. strncpy(&res->path.s[sizeof(CFG_DIR)+1], s.s, s.len);
  99. res->path.len = sizeof(CFG_DIR)+s.len;
  100. } else {
  101. res->path.s = res->uri;
  102. res->path.len = strlen(res->path.s);
  103. }
  104. return 0;
  105. }
  106. static void bdb_uri_free(db_uri_t* uri, bdb_uri_t* payload)
  107. {
  108. if (payload == NULL) return;
  109. if(payload->path.s && payload->path.s!=payload->uri)
  110. pkg_free(payload->path.s);
  111. if (payload->uri) pkg_free(payload->uri);
  112. db_drv_free(&payload->drv);
  113. pkg_free(payload);
  114. }
  115. int bdb_uri(db_uri_t* uri)
  116. {
  117. bdb_uri_t *buri;
  118. buri = (bdb_uri_t*)pkg_malloc(sizeof(bdb_uri_t));
  119. if (buri == NULL) {
  120. ERR("bdb: No memory left\n");
  121. goto error;
  122. }
  123. memset(buri, '\0', sizeof(bdb_uri_t));
  124. if (db_drv_init(&buri->drv, bdb_uri_free) < 0) goto error;
  125. if (parse_bdb_uri(buri, &uri->body) < 0) goto error;
  126. DB_SET_PAYLOAD(uri, buri);
  127. uri->cmp = bdb_uri_cmp;
  128. return 0;
  129. error:
  130. if (buri) {
  131. if (buri->uri) pkg_free(buri->uri);
  132. db_drv_free(&buri->drv);
  133. pkg_free(buri);
  134. }
  135. return -1;
  136. }
  137. /** @} */