1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
11 * Written by Craig Edwards, Craig McLure, and others.
12 * This program is free but copyrighted software; see
13 * the file COPYING for details.
15 * ---------------------------------------------------
27 #include "helperfuncs.h"
29 #include "configreader.h"
33 /* $ModDesc: PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API */
34 /* $CompileFlags: -I`pg_config --includedir` */
35 /* $LinkerFlags: -L`pg_config --libdir` -lpq */
37 /* UGH, UGH, UGH, UGH, UGH, UGH
38 * I'm having trouble seeing how I
39 * can avoid this. The core-defined
40 * constructors for InspSocket just
41 * aren't suitable...and if I'm
42 * reimplementing them I need this so
43 * I can access the socket engine :\
45 extern InspIRCd* ServerInstance;
46 InspSocket* socket_ref[MAX_DESCRIPTORS];
48 /* Forward declare, so we can have the typedef neatly at the top */
51 typedef std::map<std::string, SQLConn*> ConnMap;
53 /* CREAD, Connecting and wants read event
54 * CWRITE, Connecting and wants write event
55 * WREAD, Connected/Working and wants read event
56 * WWRITE, Connected/Working and wants write event
58 enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE };
60 /** QueryQueue, a queue of queries waiting to be executed.
61 * This maintains two queues internally, one for 'priority'
62 * queries and one for less important ones. Each queue has
63 * new queries appended to it and ones to execute are popped
64 * off the front. This keeps them flowing round nicely and no
65 * query should ever get 'stuck' for too long. If there are
66 * queries in the priority queue they will be executed first,
67 * 'unimportant' queries will only be executed when the
68 * priority queue is empty.
70 * These are lists of SQLresult so we can, from the moment the
71 * SQLrequest is recieved, be beginning to construct the result
72 * object. The copy in the deque can then be submitted in-situ
73 * and finally deleted from this queue. No copies of the SQLresult :)
75 * Because we work on the SQLresult in-situ, we need a way of accessing the
76 * result we are currently processing, QueryQueue::front(), but that call
77 * needs to always return the same element until that element is removed
78 * from the queue, this is what the 'which' variable is. New queries are
79 * always added to the back of one of the two queues, but if when front()
80 * is first called then the priority queue is empty then front() will return
81 * a query from the normal queue, but if a query is then added to the priority
82 * queue then front() must continue to return the front of the *normal* queue
83 * until pop() is called.
89 std::deque<SQLresult> priority; /* The priority queue */
90 std::deque<SQLresult> normal; /* The 'normal' queue */
91 enum { PRI, NOR, NON } which; /* Which queue the currently active element is at the front of */
100 void push(const Query &q, bool pri = false)
102 log(DEBUG, "QueryQueue::push_back(): Adding %s query to queue: %s", ((pri) ? "priority" : "non-priority"), q.c_str());
105 priority.push_back(q);
112 if((which == PRI) && priority.size())
114 priority.pop_front();
116 else if((which == NOR) && normal.size())
121 /* Silently do nothing if there was no element to pop() */
129 return priority.front();
131 return normal.front();
136 return priority.front();
142 return normal.front();
145 /* This will probably result in a segfault,
146 * but the caller should have checked totalsize()
147 * first so..meh - moron :p
150 return priority.front();
154 std::pair<int, int> size()
156 return std::make_pair(priority.size(), normal.size());
161 return priority.size() + normal.size();
165 /** SQLConn represents one SQL session.
166 * Each session has its own persistent connection to the database.
167 * This is a subclass of InspSocket so it can easily recieve read/write events from the core socket
168 * engine, unlike the original MySQL module this module does not block. Ever. It gets a mild stabbing
172 class SQLConn : public InspSocket
175 Server* Srv; /* Server* for..uhm..something, maybe */
176 std::string dbhost; /* Database server hostname */
177 unsigned int dbport; /* Database server port */
178 std::string dbname; /* Database name */
179 std::string dbuser; /* Database username */
180 std::string dbpass; /* Database password */
181 bool ssl; /* If we should require SSL */
182 PGconn* sql; /* PgSQL database connection handle */
183 SQLstatus status; /* PgSQL database connection status */
184 bool qinprog;/* If there is currently a query in progress */
185 QueryQueue queue; /* Queue of queries waiting to be executed on this connection */
186 Query query; /* The currently active query on this connection */
190 /* This class should only ever be created inside this module, using this constructor, so we don't have to worry about the default ones */
192 SQLConn(Server* srv, const std::string &h, unsigned int p, const std::string &d, const std::string &u, const std::string &pwd, bool s)
193 : InspSocket::InspSocket(), Srv(srv), dbhost(h), dbport(p), dbname(d), dbuser(u), dbpass(pwd), ssl(s), sql(NULL), status(CWRITE), qinprog(false)
195 log(DEBUG, "Creating new PgSQL connection to database %s on %s:%u (%s/%s)", dbname.c_str(), dbhost.c_str(), dbport, dbuser.c_str(), dbpass.c_str());
197 /* Some of this could be reviewed, unsure if I need to fill 'host' etc...
198 * just copied this over from the InspSocket constructor.
200 strlcpy(this->host, dbhost.c_str(), MAXBUF);
203 this->ClosePending = false;
205 if(!inet_aton(this->host, &this->addy))
207 /* Its not an ip, spawn the resolver.
208 * PgSQL doesn't do nonblocking DNS
209 * lookups, so we do it for it.
212 log(DEBUG,"Attempting to resolve %s", this->host);
214 this->dns.SetNS(Srv->GetConfig()->DNSServer);
215 this->dns.ForwardLookupWithFD(this->host, fd);
217 this->state = I_RESOLVING;
218 socket_ref[this->fd] = this;
224 log(DEBUG,"No need to resolve %s", this->host);
225 strlcpy(this->IP, this->host, MAXBUF);
227 if(!this->DoConnect())
229 throw ModuleException("Connect failed");
241 log(DEBUG, "Checking for DNS lookup result");
243 if(this->dns.HasResult())
245 std::string res_ip = dns.GetResultIP();
249 log(DEBUG, "Got result: %s", res_ip.c_str());
251 strlcpy(this->IP, res_ip.c_str(), MAXBUF);
254 socket_ref[this->fd] = NULL;
256 return this->DoConnect();
260 log(DEBUG, "DNS lookup failed, dying horribly");
267 log(DEBUG, "No result for lookup yet!");
274 log(DEBUG, "SQLConn::DoConnect()");
276 if(!(sql = PQconnectStart(MkInfoStr().c_str())))
278 log(DEBUG, "Couldn't allocate PGconn structure, aborting: %s", PQerrorMessage(sql));
283 if(PQstatus(sql) == CONNECTION_BAD)
285 log(DEBUG, "PQconnectStart failed: %s", PQerrorMessage(sql));
292 if(PQsetnonblocking(sql, 1) == -1)
294 log(DEBUG, "Couldn't set connection nonblocking: %s", PQerrorMessage(sql));
299 /* OK, we've initalised the connection, now to get it hooked into the socket engine
300 * and then start polling it.
303 log(DEBUG, "Old DNS socket: %d", this->fd);
304 this->fd = PQsocket(sql);
305 log(DEBUG, "New SQL socket: %d", this->fd);
309 log(DEBUG, "PQsocket says we have an invalid FD: %d", this->fd);
314 this->state = I_CONNECTING;
315 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
316 socket_ref[this->fd] = this;
318 /* Socket all hooked into the engine, now to tell PgSQL to start connecting */
325 log(DEBUG,"SQLConn::Close");
328 socket_ref[this->fd] = NULL;
330 this->state = I_ERROR;
331 this->OnError(I_ERR_SOCKET);
332 this->ClosePending = true;
345 switch(PQconnectPoll(sql))
347 case PGRES_POLLING_WRITING:
348 log(DEBUG, "PGconnectPoll: PGRES_POLLING_WRITING");
352 case PGRES_POLLING_READING:
353 log(DEBUG, "PGconnectPoll: PGRES_POLLING_READING");
356 case PGRES_POLLING_FAILED:
357 log(DEBUG, "PGconnectPoll: PGRES_POLLING_FAILED: %s", PQerrorMessage(sql));
359 case PGRES_POLLING_OK:
360 log(DEBUG, "PGconnectPoll: PGRES_POLLING_OK");
362 return DoConnectedPoll()
364 log(DEBUG, "PGconnectPoll: wtf?");
371 bool DoConnectedPoll()
373 if(!qinprog && queue.totalsize())
375 /* There's no query currently in progress, and there's queries in the queue. */
376 query = queue.pop_front();
380 if(PQconsumeInput(sql))
382 log(DEBUG, "PQconsumeInput succeeded");
386 log(DEBUG, "Still busy processing command though");
390 log(DEBUG, "Looks like we have a result to process!");
392 while(PGresult* result = PQgetResult(sql))
394 int cols = PQnfields(result);
396 log(DEBUG, "Got result! :D");
397 log(DEBUG, "%d rows, %d columns checking now what the column names are", PQntuples(result), cols);
399 for(int i = 0; i < cols; i++)
401 log(DEBUG, "Column name: %s (%d)", PQfname(result, i));
413 log(DEBUG, "PQconsumeInput failed: %s", PQerrorMessage(sql));
419 switch(PQstatus(sql))
421 case CONNECTION_STARTED:
422 log(DEBUG, "PQstatus: CONNECTION_STARTED: Waiting for connection to be made.");
425 case CONNECTION_MADE:
426 log(DEBUG, "PQstatus: CONNECTION_MADE: Connection OK; waiting to send.");
429 case CONNECTION_AWAITING_RESPONSE:
430 log(DEBUG, "PQstatus: CONNECTION_AWAITING_RESPONSE: Waiting for a response from the server.");
433 case CONNECTION_AUTH_OK:
434 log(DEBUG, "PQstatus: CONNECTION_AUTH_OK: Received authentication; waiting for backend start-up to finish.");
437 case CONNECTION_SSL_STARTUP:
438 log(DEBUG, "PQstatus: CONNECTION_SSL_STARTUP: Negotiating SSL encryption.");
441 case CONNECTION_SETENV:
442 log(DEBUG, "PQstatus: CONNECTION_SETENV: Negotiating environment-driven parameter settings.");
446 log(DEBUG, "PQstatus: ???");
450 virtual bool OnDataReady()
452 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
453 log(DEBUG, "OnDataReady(): status = %s", StatusStr());
458 virtual bool OnWriteReady()
460 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
461 log(DEBUG, "OnWriteReady(): status = %s", StatusStr());
466 virtual bool OnConnected()
468 log(DEBUG, "OnConnected(): status = %s", StatusStr());
477 if((status == CREAD) || (status == CWRITE))
483 ret = DoConnectedPoll();
489 log(DEBUG, "Error flushing write queue: %s", PQerrorMessage(sql));
492 log(DEBUG, "Successfully flushed write queue (or there was nothing to write)");
495 log(DEBUG, "Not all of the write queue written, triggering write event so we can have another go");
503 std::string MkInfoStr()
505 /* XXX - This needs nonblocking DNS lookups */
507 std::ostringstream conninfo("connect_timeout = '2'");
510 conninfo << " hostaddr = '" << dbhost << "'";
513 conninfo << " port = '" << dbport << "'";
516 conninfo << " dbname = '" << dbname << "'";
519 conninfo << " user = '" << dbuser << "'";
522 conninfo << " password = '" << dbpass << "'";
525 conninfo << " sslmode = 'require'";
527 return conninfo.str();
530 const char* StatusStr()
532 if(status == CREAD) return "CREAD";
533 if(status == CWRITE) return "CWRITE";
534 if(status == WREAD) return "WREAD";
535 if(status == WWRITE) return "WWRITE";
536 return "Err...what, erm..BUG!";
539 SQLerror Query(const Query &query, bool pri)
541 queue.push_back(query, pri);
543 if((status == WREAD) || (status == WWRITE))
547 if(PQsendQuery(sql, query.c_str()))
549 log(DEBUG, "Dispatched query: %s", query.c_str());
555 log(DEBUG, "Failed to dispatch query: %s", PQerrorMessage(sql));
556 return SQLerror(QSEND_FAIL, PQerrorMessage(sql));
561 log(DEBUG, "Can't query until connection is complete");
562 return SQLerror(BAD_CONN, "Can't query until connection is complete");
566 class ModulePgSQL : public Module
573 ModulePgSQL(Server* Me)
574 : Module::Module(Me), Srv(Me)
576 log(DEBUG, "%s 'SQL' feature", Srv->PublishFeature("SQL", this) ? "Published" : "Couldn't publish");
577 log(DEBUG, "%s 'PgSQL' feature", Srv->PublishFeature("PgSQL", this) ? "Published" : "Couldn't publish");
582 void Implements(char* List)
584 List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
587 virtual void OnRehash(const std::string ¶meter)
591 /* Delete all the SQLConn objects in the connection lists,
592 * this will call their destructors where they can handle
593 * closing connections and such.
595 for(ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
597 DELETE(iter->second);
600 /* Empty out our list of connections */
603 for(int i = 0; i < conf.Enumerate("database"); i++)
608 id = conf.ReadValue("database", "id", i);
609 newconn = new SQLConn(Srv, conf.ReadValue("database", "hostname", i),
610 conf.ReadInteger("database", "port", i, true),
611 conf.ReadValue("database", "name", i),
612 conf.ReadValue("database", "username", i),
613 conf.ReadValue("database", "password", i),
614 conf.ReadFlag("database", "ssl", i));
616 connections.insert(std::make_pair(id, newconn));
620 virtual char* OnRequest(Request* request)
622 if(strcmp(SQLREQID, request->GetData()) == 0)
624 SQLrequest* req = (SQLrequest*)request;
625 ConnMap::iterator iter;
627 log(DEBUG, "Got query: '%s' on id '%s'", req->query.c_str(), req->dbid.c_str());
629 if((iter = connections.find(req->dbid)) != connections.end())
632 req->error = iter->second->Query(Query(req->query, req->GetSource(), this), req->pri);
638 req->error.Id(BAD_DBID);
643 log(DEBUG, "Got unsupported API version string: %s", request->GetData());
648 virtual Version GetVersion()
650 return Version(1, 0, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER);
653 virtual ~ModulePgSQL()
658 class ModulePgSQLFactory : public ModuleFactory
665 ~ModulePgSQLFactory()
669 virtual Module * CreateModule(Server* Me)
671 return new ModulePgSQL(Me);
676 extern "C" void * init_module( void )
678 return new ModulePgSQLFactory;