db_id.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2005 iptel.org
  5. * Copyright (C) 2007-2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * \file lib/srdb1/db_id.c
  25. * \ingroup db1
  26. * \brief Functions for parsing a database URL and work with db identifier.
  27. */
  28. #include "db.h"
  29. #include "db_id.h"
  30. #include "../../dprint.h"
  31. #include "../../mem/mem.h"
  32. #include "../../pt.h"
  33. #include "../../ut.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. /**
  37. * Duplicate a string
  38. * \param dst destination
  39. * \param begin start of the string
  40. * \param end end of the string
  41. */
  42. static int dupl_string(char** dst, const char* begin, const char* end)
  43. {
  44. if (*dst) pkg_free(*dst);
  45. *dst = pkg_malloc(end - begin + 1);
  46. if ((*dst) == NULL) {
  47. return -1;
  48. }
  49. memcpy(*dst, begin, end - begin);
  50. (*dst)[end - begin] = '\0';
  51. return 0;
  52. }
  53. /**
  54. * Parse a database URL of form
  55. * scheme://[username[:password]@]hostname[:port]/database
  56. *
  57. * \param id filled id struct
  58. * \param url parsed URL
  59. * \return 0 if parsing was successful and -1 otherwise
  60. */
  61. static int parse_db_url(struct db_id* id, const str* url)
  62. {
  63. #define SHORTEST_DB_URL "s://a/b"
  64. #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
  65. enum state {
  66. ST_SCHEME, /* Scheme part */
  67. ST_SLASH1, /* First slash */
  68. ST_SLASH2, /* Second slash */
  69. ST_USER_HOST, /* Username or hostname */
  70. ST_PASS_PORT, /* Password or port part */
  71. ST_HOST, /* Hostname part */
  72. ST_PORT, /* Port part */
  73. ST_DB /* Database part */
  74. };
  75. enum state st;
  76. unsigned int len, i;
  77. const char* begin;
  78. char* prev_token;
  79. prev_token = 0;
  80. if (!id || !url || !url->s) {
  81. goto err;
  82. }
  83. len = url->len;
  84. if (len < SHORTEST_DB_URL_LEN) {
  85. goto err;
  86. }
  87. /* Initialize all attributes to 0 */
  88. memset(id, 0, sizeof(struct db_id));
  89. st = ST_SCHEME;
  90. begin = url->s;
  91. for(i = 0; i < len; i++) {
  92. switch(st) {
  93. case ST_SCHEME:
  94. switch(url->s[i]) {
  95. case ':':
  96. st = ST_SLASH1;
  97. if (dupl_string(&id->scheme, begin, url->s + i) < 0) goto err;
  98. break;
  99. }
  100. break;
  101. case ST_SLASH1:
  102. switch(url->s[i]) {
  103. case '/':
  104. st = ST_SLASH2;
  105. break;
  106. default:
  107. goto err;
  108. }
  109. break;
  110. case ST_SLASH2:
  111. switch(url->s[i]) {
  112. case '/':
  113. st = ST_USER_HOST;
  114. begin = url->s + i + 1;
  115. break;
  116. default:
  117. goto err;
  118. }
  119. break;
  120. case ST_USER_HOST:
  121. switch(url->s[i]) {
  122. case '@':
  123. st = ST_HOST;
  124. if (dupl_string(&id->username, begin, url->s + i) < 0) goto err;
  125. begin = url->s + i + 1;
  126. break;
  127. case ':':
  128. st = ST_PASS_PORT;
  129. if (dupl_string(&prev_token, begin, url->s + i) < 0) goto err;
  130. begin = url->s + i + 1;
  131. break;
  132. case '/':
  133. if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
  134. if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
  135. return 0;
  136. }
  137. break;
  138. case ST_PASS_PORT:
  139. switch(url->s[i]) {
  140. case '@':
  141. st = ST_HOST;
  142. id->username = prev_token;
  143. prev_token = 0;
  144. if (dupl_string(&id->password, begin, url->s + i) < 0) goto err;
  145. begin = url->s + i + 1;
  146. break;
  147. case '/':
  148. id->host = prev_token;
  149. prev_token = 0;
  150. id->port = str2s(begin, url->s + i - begin, 0);
  151. if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
  152. return 0;
  153. }
  154. break;
  155. case ST_HOST:
  156. switch(url->s[i]) {
  157. case ':':
  158. st = ST_PORT;
  159. if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
  160. begin = url->s + i + 1;
  161. break;
  162. case '/':
  163. if (dupl_string(&id->host, begin, url->s + i) < 0) goto err;
  164. if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
  165. return 0;
  166. }
  167. break;
  168. case ST_PORT:
  169. switch(url->s[i]) {
  170. case '/':
  171. id->port = str2s(begin, url->s + i - begin, 0);
  172. if (dupl_string(&id->database, url->s + i + 1, url->s + len) < 0) goto err;
  173. return 0;
  174. }
  175. break;
  176. case ST_DB:
  177. break;
  178. }
  179. }
  180. if (st != ST_DB) goto err;
  181. return 0;
  182. err:
  183. if (!id) goto end;
  184. if (id->scheme) pkg_free(id->scheme);
  185. if (id->username) pkg_free(id->username);
  186. if (id->password) pkg_free(id->password);
  187. if (id->host) pkg_free(id->host);
  188. if (id->database) pkg_free(id->database);
  189. memset(id, 0, sizeof(struct db_id));
  190. if (prev_token) pkg_free(prev_token);
  191. end:
  192. return -1;
  193. }
  194. /**
  195. * Create a new connection identifier
  196. * \param url database URL
  197. * \param pooling whether or not a pooled connection may be used
  198. * \return connection identifier, or zero on error
  199. */
  200. struct db_id* new_db_id(const str* url, db_pooling_t pooling)
  201. {
  202. static int poolid=0;
  203. struct db_id* ptr;
  204. if (!url || !url->s) {
  205. LM_ERR("invalid parameter\n");
  206. return 0;
  207. }
  208. ptr = (struct db_id*)pkg_malloc(sizeof(struct db_id) + url->len + 1);
  209. if (!ptr) {
  210. LM_ERR("no private memory left\n");
  211. goto err;
  212. }
  213. memset(ptr, 0, sizeof(struct db_id)+url->len+1);
  214. if (parse_db_url(ptr, url) < 0) {
  215. LM_ERR("error while parsing database URL: '%.*s' \n", url->len, url->s);
  216. goto err;
  217. }
  218. if (pooling == DB_POOLING_NONE) ptr->poolid = ++poolid;
  219. else ptr->poolid = 0;
  220. ptr->pid = my_pid();
  221. ptr->url.s = (char*)ptr + sizeof(struct db_id);
  222. ptr->url.len = url->len;
  223. strncpy(ptr->url.s, url->s, url->len);
  224. ptr->url.s[url->len] = '\0';
  225. return ptr;
  226. err:
  227. if (ptr) pkg_free(ptr);
  228. return 0;
  229. }
  230. /**
  231. * Compare two connection identifiers
  232. * \param id1 first identifier
  233. * \param id2 second identifier
  234. * \return one if both are equal, zero otherwise
  235. */
  236. unsigned char cmp_db_id(const struct db_id* id1, const struct db_id* id2)
  237. {
  238. if (!id1 || !id2) return 0;
  239. if (id1->port != id2->port) return 0;
  240. if (strcmp(id1->scheme, id2->scheme)) return 0;
  241. if (id1->username!=0 && id2->username!=0) {
  242. if (strcmp(id1->username, id2->username)) return 0;
  243. } else {
  244. if (id1->username!=0 || id2->username!=0) return 0;
  245. }
  246. if (id1->password!=0 && id2->password!=0) {
  247. if(strcmp(id1->password, id2->password)) return 0;
  248. } else {
  249. if (id1->password!=0 || id2->password!=0) return 0;
  250. }
  251. if (strcasecmp(id1->host, id2->host)) return 0;
  252. if (strcmp(id1->database, id2->database)) return 0;
  253. if(id1->pid!=id2->pid) {
  254. LM_DBG("identical DB URLs, but different DB connection pid [%d/%d]\n",
  255. id1->pid, id2->pid);
  256. return 0;
  257. }
  258. if(id1->poolid!=id2->poolid) {
  259. LM_DBG("identical DB URLs, but different poolids [%d/%d]\n",
  260. id1->poolid, id2->poolid);
  261. return 0;
  262. }
  263. return 1;
  264. }
  265. /**
  266. * Free a connection identifier
  267. * \param id identifier
  268. */
  269. void free_db_id(struct db_id* id)
  270. {
  271. if (!id) return;
  272. if (id->scheme) pkg_free(id->scheme);
  273. if (id->username) pkg_free(id->username);
  274. if (id->password) pkg_free(id->password);
  275. if (id->host) pkg_free(id->host);
  276. if (id->database) pkg_free(id->database);
  277. pkg_free(id);
  278. }