km_pg_con.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. memset(keywords, 0, (sizeof(char*) * 10));
  66. memset(values, 0, (sizeof(char*) * 10));
  67. memset(to, 0, (sizeof(char) * 16));
  68. if (id->port) {
  69. ports = int2str(id->port, 0);
  70. keywords[i] = "port";
  71. values[i++] = ports;
  72. LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
  73. id->port, ZSW(id->database));
  74. } else {
  75. ports = NULL;
  76. LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
  77. ZSW(id->database));
  78. }
  79. keywords[i] = "host";
  80. values[i++] = id->host;
  81. keywords[i] = "dbname";
  82. values[i++] = id->database;
  83. keywords[i] = "user";
  84. values[i++] = id->username;
  85. keywords[i] = "password";
  86. values[i++] = id->password;
  87. if (pg_timeout > 0) {
  88. snprintf(to, sizeof(to)-1, "%d", pg_timeout + 3);
  89. keywords[i] = "connect_timeout";
  90. values[i++] = to;
  91. }
  92. keywords[i] = values[i] = NULL;
  93. /* don't attempt to re-init openssl if done already */
  94. if(tls_loaded()) PQinitSSL(0);
  95. ptr->con = PQconnectdbParams(keywords, values, 1);
  96. LM_DBG("PQconnectdbParams(%p)\n", ptr->con);
  97. if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
  98. {
  99. LM_ERR("%s\n", PQerrorMessage(ptr->con));
  100. PQfinish(ptr->con);
  101. goto err;
  102. }
  103. ptr->connected = 1;
  104. ptr->timestamp = time(0);
  105. ptr->id = id;
  106. #if defined(SO_KEEPALIVE) && defined(TCP_KEEPIDLE)
  107. if (pg_keepalive) {
  108. i = 1;
  109. setsockopt(PQsocket(ptr->con), SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i));
  110. setsockopt(PQsocket(ptr->con), IPPROTO_TCP, TCP_KEEPIDLE, &pg_keepalive, sizeof(pg_keepalive));
  111. }
  112. #endif
  113. return ptr;
  114. err:
  115. if (ptr) {
  116. LM_ERR("cleaning up %p=pkg_free()\n", ptr);
  117. pkg_free(ptr);
  118. }
  119. return 0;
  120. }
  121. /*!
  122. * \brief Close the connection and release memory
  123. * \param con connection
  124. */
  125. void db_postgres_free_connection(struct pool_con* con)
  126. {
  127. struct pg_con * _c;
  128. if (!con) return;
  129. _c = (struct pg_con*)con;
  130. if (_c->res) {
  131. LM_DBG("PQclear(%p)\n", _c->res);
  132. PQclear(_c->res);
  133. _c->res = 0;
  134. }
  135. if (_c->id) free_db_id(_c->id);
  136. if (_c->con) {
  137. LM_DBG("PQfinish(%p)\n", _c->con);
  138. PQfinish(_c->con);
  139. _c->con = 0;
  140. }
  141. LM_DBG("pkg_free(%p)\n", _c);
  142. pkg_free(_c);
  143. }