km_pg_con.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2001-2004 iptel.org
  3. * Copyright (C) 2008 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*! \file
  22. * \brief DB_POSTGRES :: Core
  23. * \ingroup db_postgres
  24. * Module: \ref db_postgres
  25. */
  26. #include "km_pg_con.h"
  27. #include "pg_mod.h"
  28. #include "../../mem/mem.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../tls_hooks_init.h"
  32. #include <string.h>
  33. #include <time.h>
  34. #include <netinet/in.h>
  35. #include <netinet/tcp.h>
  36. /*!
  37. * \brief Create a new connection
  38. *
  39. * Create a new connection structure in private memory, open the PostgreSQL
  40. * connection and set reference count to 1
  41. * \param id database id
  42. * \return postgres connection structure, 0 on error
  43. */
  44. struct pg_con* db_postgres_new_connection(struct db_id* id)
  45. {
  46. struct pg_con* ptr;
  47. char *ports;
  48. int i = 0;
  49. const char *keywords[10], *values[10];
  50. char to[16];
  51. LM_DBG("db_id = %p\n", id);
  52. if (!id) {
  53. LM_ERR("invalid db_id parameter value\n");
  54. return 0;
  55. }
  56. ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
  57. if (!ptr) {
  58. LM_ERR("failed trying to allocated %lu bytes for connection structure."
  59. "\n", (unsigned long)sizeof(struct pg_con));
  60. return 0;
  61. }
  62. LM_DBG("%p=pkg_malloc(%lu)\n", ptr, (unsigned long)sizeof(struct pg_con));
  63. memset(ptr, 0, sizeof(struct pg_con));
  64. ptr->ref = 1;
  65. if (id->port) {
  66. ports = int2str(id->port, 0);
  67. keywords[i] = "port";
  68. values[i++] = ports;
  69. LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
  70. id->port, ZSW(id->database));
  71. } else {
  72. ports = NULL;
  73. LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
  74. ZSW(id->database));
  75. }
  76. keywords[i] = "host";
  77. values[i++] = id->host;
  78. keywords[i] = "dbname";
  79. values[i++] = id->database;
  80. keywords[i] = "user";
  81. values[i++] = id->username;
  82. keywords[i] = "password";
  83. values[i++] = id->password;
  84. if (pg_timeout > 0) {
  85. snprintf(to, sizeof(to)-1, "%d", pg_timeout + 3);
  86. keywords[i] = "connect_timeout";
  87. values[i++] = to;
  88. }
  89. keywords[i] = values[i] = NULL;
  90. /* don't attempt to re-init openssl if done already */
  91. if(tls_loaded()) PQinitSSL(0);
  92. ptr->con = PQconnectdbParams(keywords, values, 1);
  93. LM_DBG("PQconnectdbParams(%p)\n", ptr->con);
  94. if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
  95. {
  96. LM_ERR("%s\n", PQerrorMessage(ptr->con));
  97. PQfinish(ptr->con);
  98. goto err;
  99. }
  100. ptr->connected = 1;
  101. ptr->timestamp = time(0);
  102. ptr->id = id;
  103. #if defined(SO_KEEPALIVE) && defined(TCP_KEEPIDLE)
  104. if (pg_keepalive) {
  105. i = 1;
  106. setsockopt(PQsocket(ptr->con), SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i));
  107. setsockopt(PQsocket(ptr->con), IPPROTO_TCP, TCP_KEEPIDLE, &pg_keepalive, sizeof(pg_keepalive));
  108. }
  109. #endif
  110. return ptr;
  111. err:
  112. if (ptr) {
  113. LM_ERR("cleaning up %p=pkg_free()\n", ptr);
  114. pkg_free(ptr);
  115. }
  116. return 0;
  117. }
  118. /*!
  119. * \brief Close the connection and release memory
  120. * \param con connection
  121. */
  122. void db_postgres_free_connection(struct pool_con* con)
  123. {
  124. struct pg_con * _c;
  125. if (!con) return;
  126. _c = (struct pg_con*)con;
  127. if (_c->res) {
  128. LM_DBG("PQclear(%p)\n", _c->res);
  129. PQclear(_c->res);
  130. _c->res = 0;
  131. }
  132. if (_c->id) free_db_id(_c->id);
  133. if (_c->con) {
  134. LM_DBG("PQfinish(%p)\n", _c->con);
  135. PQfinish(_c->con);
  136. _c->con = 0;
  137. }
  138. LM_DBG("pkg_free(%p)\n", _c);
  139. pkg_free(_c);
  140. }