]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_mysql.cpp
03dd9934281452148101773c6e2db1d43d2f06d0
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <mysql.h>
16 #include <pthread.h>
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "m_sqlv2.h"
21
22 /* VERSION 2 API: With nonblocking (threaded) requests */
23
24 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
25 /* $CompileFlags: exec("mysql_config --include") */
26 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
27 /* $ModDep: m_sqlv2.h */
28 /* $NoPedantic */
29
30 /* THE NONBLOCKING MYSQL API!
31  * 
32  * MySQL provides no nonblocking (asyncronous) API of its own, and its developers recommend
33  * that instead, you should thread your program. This is what i've done here to allow for
34  * asyncronous SQL requests via mysql. The way this works is as follows:
35  *
36  * The module spawns a thread via pthreads, and performs its mysql queries in this thread,
37  * using a queue with priorities. There is a mutex on either end which prevents two threads
38  * adjusting the queue at the same time, and crashing the ircd. Every 50 milliseconds, the
39  * worker thread wakes up, and checks if there is a request at the head of its queue.
40  * If there is, it processes this request, blocking the worker thread but leaving the ircd
41  * thread to go about its business as usual. During this period, the ircd thread is able
42  * to insert futher pending requests into the queue.
43  *
44  * Once the processing of a request is complete, it is removed from the incoming queue to
45  * an outgoing queue, and initialized as a 'response'. The worker thread then signals the
46  * ircd thread (via a loopback socket) of the fact a result is available, by sending the
47  * connection ID through the connection.
48  *
49  * The ircd thread then mutexes the queue once more, reads the outbound response off the head
50  * of the queue, and sends it on its way to the original calling module.
51  *
52  * XXX: You might be asking "why doesnt he just send the response from within the worker thread?"
53  * The answer to this is simple. The majority of InspIRCd, and in fact most ircd's are not
54  * threadsafe. This module is designed to be threadsafe and is careful with its use of threads,
55  * however, if we were to call a module's OnRequest even from within a thread which was not the
56  * one the module was originally instantiated upon, there is a chance of all hell breaking loose
57  * if a module is ever put in a re-enterant state (stack corruption could occur, crashes, data
58  * corruption, and worse, so DONT think about it until the day comes when InspIRCd is 100%
59  * gauranteed threadsafe!)
60  *
61  * For a diagram of this system please see http://www.inspircd.org/wiki/Mysql2
62  */
63
64
65 class SQLConnection;
66 class Notifier;
67
68
69 typedef std::map<std::string, SQLConnection*> ConnMap;
70 bool giveup = false;
71 bool threadfinished = false;
72 static Module* SQLModule = NULL;
73 static Notifier* MessagePipe = NULL;
74 int QueueFD = -1;
75
76
77 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
78 #define mysql_field_count mysql_num_fields
79 #endif
80
81 typedef std::deque<SQLresult*> ResultQueue;
82
83 /* A mutex to wrap around queue accesses */
84 pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
85
86 pthread_mutex_t results_mutex = PTHREAD_MUTEX_INITIALIZER;
87
88 pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
89
90 /** Represents a mysql result set
91  */
92 class MySQLresult : public SQLresult
93 {
94         int currentrow;
95         std::vector<std::string> colnames;
96         std::vector<SQLfieldList> fieldlists;
97         SQLfieldMap* fieldmap;
98         SQLfieldMap fieldmap2;
99         SQLfieldList emptyfieldlist;
100         int rows;
101  public:
102
103         MySQLresult(Module* self, Module* to, MYSQL_RES* res, int affected_rows, unsigned int rid) : SQLresult(self, to, rid), currentrow(0), fieldmap(NULL)
104         {
105                 /* A number of affected rows from from mysql_affected_rows.
106                  */
107                 fieldlists.clear();
108                 rows = 0;
109                 if (affected_rows >= 1)
110                 {
111                         rows = affected_rows;
112                         fieldlists.resize(rows);
113                 }
114                 unsigned int field_count = 0;
115                 if (res)
116                 {
117                         MYSQL_ROW row;
118                         int n = 0;
119                         while ((row = mysql_fetch_row(res)))
120                         {
121                                 if (fieldlists.size() < (unsigned int)rows+1)
122                                 {
123                                         fieldlists.resize(fieldlists.size()+1);
124                                 }
125                                 field_count = 0;
126                                 MYSQL_FIELD *fields = mysql_fetch_fields(res);
127                                 if(mysql_num_fields(res) == 0)
128                                         break;
129                                 if (fields && mysql_num_fields(res))
130                                 {
131                                         colnames.clear();
132                                         while (field_count < mysql_num_fields(res))
133                                         {
134                                                 std::string a = (fields[field_count].name ? fields[field_count].name : "");
135                                                 std::string b = (row[field_count] ? row[field_count] : "");
136                                                 SQLfield sqlf(b, !row[field_count]);
137                                                 colnames.push_back(a);
138                                                 fieldlists[n].push_back(sqlf); 
139                                                 field_count++;
140                                         }
141                                         n++;
142                                 }
143                                 rows++;
144                         }
145                         mysql_free_result(res);
146                 }
147         }
148
149         MySQLresult(Module* self, Module* to, SQLerror e, unsigned int rid) : SQLresult(self, to, rid), currentrow(0)
150         {
151                 rows = 0;
152                 error = e;
153         }
154
155         ~MySQLresult()
156         {
157         }
158
159         virtual int Rows()
160         {
161                 return rows;
162         }
163
164         virtual int Cols()
165         {
166                 return colnames.size();
167         }
168
169         virtual std::string ColName(int column)
170         {
171                 if (column < (int)colnames.size())
172                 {
173                         return colnames[column];
174                 }
175                 else
176                 {
177                         throw SQLbadColName();
178                 }
179                 return "";
180         }
181
182         virtual int ColNum(const std::string &column)
183         {
184                 for (unsigned int i = 0; i < colnames.size(); i++)
185                 {
186                         if (column == colnames[i])
187                                 return i;
188                 }
189                 throw SQLbadColName();
190                 return 0;
191         }
192
193         virtual SQLfield GetValue(int row, int column)
194         {
195                 if ((row >= 0) && (row < rows) && (column >= 0) && (column < Cols()))
196                 {
197                         return fieldlists[row][column];
198                 }
199
200                 throw SQLbadColName();
201
202                 /* XXX: We never actually get here because of the throw */
203                 return SQLfield("",true);
204         }
205
206         virtual SQLfieldList& GetRow()
207         {
208                 if (currentrow < rows)
209                         return fieldlists[currentrow];
210                 else
211                         return emptyfieldlist;
212         }
213
214         virtual SQLfieldMap& GetRowMap()
215         {
216                 fieldmap2.clear();
217
218                 if (currentrow < rows)
219                 {
220                         for (int i = 0; i < Cols(); i++)
221                         {
222                                 fieldmap2.insert(std::make_pair(colnames[i],GetValue(currentrow, i)));
223                         }
224                         currentrow++;
225                 }
226
227                 return fieldmap2;
228         }
229
230         virtual SQLfieldList* GetRowPtr()
231         {
232                 SQLfieldList* fieldlist = new SQLfieldList();
233
234                 if (currentrow < rows)
235                 {
236                         for (int i = 0; i < Rows(); i++)
237                         {
238                                 fieldlist->push_back(fieldlists[currentrow][i]);
239                         }
240                         currentrow++;
241                 }
242                 return fieldlist;
243         }
244
245         virtual SQLfieldMap* GetRowMapPtr()
246         {
247                 fieldmap = new SQLfieldMap();
248                 
249                 if (currentrow < rows)
250                 {
251                         for (int i = 0; i < Cols(); i++)
252                         {
253                                 fieldmap->insert(std::make_pair(colnames[i],GetValue(currentrow, i)));
254                         }
255                         currentrow++;
256                 }
257
258                 return fieldmap;
259         }
260
261         virtual void Free(SQLfieldMap* fm)
262         {
263                 delete fm;
264         }
265
266         virtual void Free(SQLfieldList* fl)
267         {
268                 delete fl;
269         }
270 };
271
272 class SQLConnection;
273
274 void NotifyMainThread(SQLConnection* connection_with_new_result);
275
276 /** Represents a connection to a mysql database
277  */
278 class SQLConnection : public classbase
279 {
280  protected:
281
282         MYSQL connection;
283         MYSQL_RES *res;
284         MYSQL_ROW row;
285         SQLhost host;
286         std::map<std::string,std::string> thisrow;
287         bool Enabled;
288
289  public:
290
291         QueryQueue queue;
292         ResultQueue rq;
293
294         // This constructor creates an SQLConnection object with the given credentials, but does not connect yet.
295         SQLConnection(const SQLhost &hi) : host(hi), Enabled(false)
296         {
297         }
298
299         ~SQLConnection()
300         {
301                 Close();
302         }
303
304         // This method connects to the database using the credentials supplied to the constructor, and returns
305         // true upon success.
306         bool Connect()
307         {
308                 unsigned int timeout = 1;
309                 mysql_init(&connection);
310                 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
311                 return mysql_real_connect(&connection, host.host.c_str(), host.user.c_str(), host.pass.c_str(), host.name.c_str(), host.port, NULL, 0);
312         }
313
314         void DoLeadingQuery()
315         {
316                 if (!CheckConnection())
317                         return;
318
319                 /* Parse the command string and dispatch it to mysql */
320                 SQLrequest& req = queue.front();
321
322                 /* Pointer to the buffer we screw around with substitution in */
323                 char* query;
324
325                 /* Pointer to the current end of query, where we append new stuff */
326                 char* queryend;
327
328                 /* Total length of the unescaped parameters */
329                 unsigned long paramlen;
330
331                 /* Total length of query, used for binary-safety in mysql_real_query */
332                 unsigned long querylength = 0;
333
334                 paramlen = 0;
335
336                 for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++)
337                 {
338                         paramlen += i->size();
339                 }
340
341                 /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be.
342                  * sizeofquery + (totalparamlength*2) + 1
343                  *
344                  * The +1 is for null-terminating the string for mysql_real_escape_string
345                  */
346
347                 query = new char[req.query.q.length() + (paramlen*2) + 1];
348                 queryend = query;
349
350                 /* Okay, now we have a buffer large enough we need to start copying the query into it and escaping and substituting
351                  * the parameters into it...
352                  */
353
354                 for(unsigned long i = 0; i < req.query.q.length(); i++)
355                 {
356                         if(req.query.q[i] == '?')
357                         {
358                                 /* We found a place to substitute..what fun.
359                                  * use mysql calls to escape and write the
360                                  * escaped string onto the end of our query buffer,
361                                  * then we "just" need to make sure queryend is
362                                  * pointing at the right place.
363                                  */
364                                 if(req.query.p.size())
365                                 {
366                                         unsigned long len = mysql_real_escape_string(&connection, queryend, req.query.p.front().c_str(), req.query.p.front().length());
367
368                                         queryend += len;
369                                         req.query.p.pop_front();
370                                 }
371                                 else
372                                         break;
373                         }
374                         else
375                         {
376                                 *queryend = req.query.q[i];
377                                 queryend++;
378                         }
379                         querylength++;
380                 }
381
382                 *queryend = 0;
383
384                 pthread_mutex_lock(&queue_mutex);
385                 req.query.q = query;
386                 pthread_mutex_unlock(&queue_mutex);
387
388                 if (!mysql_real_query(&connection, req.query.q.data(), req.query.q.length()))
389                 {
390                         /* Successfull query */
391                         res = mysql_use_result(&connection);
392                         unsigned long rows = mysql_affected_rows(&connection);
393                         MySQLresult* r = new MySQLresult(SQLModule, req.GetSource(), res, rows, req.id);
394                         r->dbid = this->GetID();
395                         r->query = req.query.q;
396                         /* Put this new result onto the results queue.
397                          * XXX: Remember to mutex the queue!
398                          */
399                         pthread_mutex_lock(&results_mutex);
400                         rq.push_back(r);
401                         pthread_mutex_unlock(&results_mutex);
402                 }
403                 else
404                 {
405                         /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
406                          * possible error numbers and error messages */
407                         SQLerror e(QREPLY_FAIL, ConvToStr(mysql_errno(&connection)) + std::string(": ") + mysql_error(&connection));
408                         MySQLresult* r = new MySQLresult(SQLModule, req.GetSource(), e, req.id);
409                         r->dbid = this->GetID();
410                         r->query = req.query.q;
411
412                         pthread_mutex_lock(&results_mutex);
413                         rq.push_back(r);
414                         pthread_mutex_unlock(&results_mutex);
415                 }
416
417                 /* Now signal the main thread that we've got a result to process.
418                  * Pass them this connection id as what to examine
419                  */
420
421                 delete[] query;
422
423                 NotifyMainThread(this);
424         }
425
426         bool ConnectionLost()
427         {
428                 if (&connection) {
429                         return (mysql_ping(&connection) != 0);
430                 }
431                 else return false;
432         }
433
434         bool CheckConnection()
435         {
436                 if (ConnectionLost()) {
437                         return Connect();
438                 }
439                 else return true;
440         }
441
442         std::string GetError()
443         {
444                 return mysql_error(&connection);
445         }
446
447         const std::string& GetID()
448         {
449                 return host.id;
450         }
451
452         std::string GetHost()
453         {
454                 return host.host;
455         }
456
457         void SetEnable(bool Enable)
458         {
459                 Enabled = Enable;
460         }
461
462         bool IsEnabled()
463         {
464                 return Enabled;
465         }
466
467         void Close()
468         {
469                 mysql_close(&connection);
470         }
471
472         const SQLhost& GetConfHost()
473         {
474                 return host;
475         }
476
477 };
478
479 ConnMap Connections;
480
481 bool HasHost(const SQLhost &host)
482 {
483         for (ConnMap::iterator iter = Connections.begin(); iter != Connections.end(); iter++)
484         {
485                 if (host == iter->second->GetConfHost())
486                         return true;
487         }
488         return false;
489 }
490
491 bool HostInConf(ConfigReader* conf, const SQLhost &h)
492 {
493         for(int i = 0; i < conf->Enumerate("database"); i++)
494         {
495                 SQLhost host;
496                 host.id         = conf->ReadValue("database", "id", i);
497                 host.host       = conf->ReadValue("database", "hostname", i);
498                 host.port       = conf->ReadInteger("database", "port", i, true);
499                 host.name       = conf->ReadValue("database", "name", i);
500                 host.user       = conf->ReadValue("database", "username", i);
501                 host.pass       = conf->ReadValue("database", "password", i);
502                 host.ssl        = conf->ReadFlag("database", "ssl", i);
503                 if (h == host)
504                         return true;
505         }
506         return false;
507 }
508
509 void ClearOldConnections(ConfigReader* conf)
510 {
511         ConnMap::iterator i,safei;
512         for (i = Connections.begin(); i != Connections.end(); i++)
513         {
514                 if (!HostInConf(conf, i->second->GetConfHost()))
515                 {
516                         delete i->second;
517                         safei = i;
518                         --i;
519                         Connections.erase(safei);
520                 }
521         }
522 }
523
524 void ClearAllConnections()
525 {
526         ConnMap::iterator i;
527         while ((i = Connections.begin()) != Connections.end())
528         {
529                 Connections.erase(i);
530                 delete i->second;
531         }
532 }
533
534 void ConnectDatabases(InspIRCd* ServerInstance)
535 {
536         for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
537         {
538                 if (i->second->IsEnabled())
539                         continue;
540
541                 i->second->SetEnable(true);
542                 if (!i->second->Connect())
543                 {
544                         /* XXX: MUTEX */
545                         pthread_mutex_lock(&logging_mutex);
546                         ServerInstance->Log(DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
547                         i->second->SetEnable(false);
548                         pthread_mutex_unlock(&logging_mutex);
549                 }
550         }
551 }
552
553 void LoadDatabases(ConfigReader* conf, InspIRCd* ServerInstance)
554 {
555         ClearOldConnections(conf);
556         for (int j =0; j < conf->Enumerate("database"); j++)
557         {
558                 SQLhost host;
559                 host.id         = conf->ReadValue("database", "id", j);
560                 host.host       = conf->ReadValue("database", "hostname", j);
561                 host.port       = conf->ReadInteger("database", "port", j, true);
562                 host.name       = conf->ReadValue("database", "name", j);
563                 host.user       = conf->ReadValue("database", "username", j);
564                 host.pass       = conf->ReadValue("database", "password", j);
565                 host.ssl        = conf->ReadFlag("database", "ssl", j);
566
567                 if (HasHost(host))
568                         continue;
569
570                 if (!host.id.empty() && !host.host.empty() && !host.name.empty() && !host.user.empty() && !host.pass.empty())
571                 {
572                         SQLConnection* ThisSQL = new SQLConnection(host);
573                         Connections[host.id] = ThisSQL;
574                 }
575         }
576         ConnectDatabases(ServerInstance);
577 }
578
579 char FindCharId(const std::string &id)
580 {
581         char i = 1;
582         for (ConnMap::iterator iter = Connections.begin(); iter != Connections.end(); ++iter, ++i)
583         {
584                 if (iter->first == id)
585                 {
586                         return i;
587                 }
588         }
589         return 0;
590 }
591
592 ConnMap::iterator GetCharId(char id)
593 {
594         char i = 1;
595         for (ConnMap::iterator iter = Connections.begin(); iter != Connections.end(); ++iter, ++i)
596         {
597                 if (i == id)
598                         return iter;
599         }
600         return Connections.end();
601 }
602
603 void NotifyMainThread(SQLConnection* connection_with_new_result)
604 {
605         /* Here we write() to the socket the main thread has open
606          * and we connect()ed back to before our thread became active.
607          * The main thread is using a nonblocking socket tied into
608          * the socket engine, so they wont block and they'll receive
609          * nearly instant notification. Because we're in a seperate
610          * thread, we can just use standard connect(), and we can
611          * block if we like. We just send the connection id of the
612          * connection back.
613          *
614          * NOTE: We only send a single char down the connection, this
615          * way we know it wont get a partial read at the other end if
616          * the system is especially congested (see bug #263).
617          * The function FindCharId translates a connection name into a
618          * one character id, and GetCharId translates a character id
619          * back into an iterator.
620          */
621         char id = FindCharId(connection_with_new_result->GetID());
622         send(QueueFD, &id, 1, 0);
623 }
624
625 void* DispatcherThread(void* arg);
626
627 /** Used by m_mysql to notify one thread when the other has a result
628  */
629 class Notifier : public BufferedSocket
630 {
631         insp_sockaddr sock_us;
632         socklen_t uslen;
633         
634
635  public:
636
637         /* Create a socket on a random port. Let the tcp stack allocate us an available port */
638 #ifdef IPV6
639         Notifier(InspIRCd* SI) : BufferedSocket(SI, "::1", 0, true, 3000)
640 #else
641         Notifier(InspIRCd* SI) : BufferedSocket(SI, "127.0.0.1", 0, true, 3000)
642 #endif
643         {
644                 uslen = sizeof(sock_us);
645                 if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
646                 {
647                         throw ModuleException("Could not create random listening port on localhost");
648                 }
649         }
650
651         Notifier(InspIRCd* SI, int newfd, char* ip) : BufferedSocket(SI, newfd, ip)
652         {
653         }
654
655         /* Using getsockname and ntohs, we can determine which port number we were allocated */
656         int GetPort()
657         {
658 #ifdef IPV6
659                 return ntohs(sock_us.sin6_port);
660 #else
661                 return ntohs(sock_us.sin_port);
662 #endif
663         }
664
665         virtual int OnIncomingConnection(int newsock, char* ip)
666         {
667                 Notifier* n = new Notifier(this->Instance, newsock, ip);
668                 n = n; /* Stop bitching at me, GCC */
669                 return true;
670         }
671
672         virtual bool OnDataReady()
673         {
674                 char data = 0;
675                 /* NOTE: Only a single character is read so we know we
676                  * cant get a partial read. (We've been told that theres
677                  * data waiting, so we wont ever get EAGAIN)
678                  * The function GetCharId translates a single character
679                  * back into an iterator.
680                  */
681                 if (Instance->SE->Recv(this, &data, 1, 0) > 0)
682                 {
683                         ConnMap::iterator iter = GetCharId(data);
684                         if (iter != Connections.end())
685                         {
686                                 /* Lock the mutex, send back the data */
687                                 pthread_mutex_lock(&results_mutex);
688                                 ResultQueue::iterator n = iter->second->rq.begin();
689                                 (*n)->Send();
690                                 iter->second->rq.pop_front();
691                                 pthread_mutex_unlock(&results_mutex);
692                                 return true;
693                         }
694                         /* No error, but unknown id */
695                         return true;
696                 }
697
698                 /* Erk, error on descriptor! */
699                 return false;
700         }
701 };
702
703 /** MySQL module
704  */
705 class ModuleSQL : public Module
706 {
707  public:
708         
709         ConfigReader *Conf;
710         InspIRCd* PublicServerInstance;
711         pthread_t Dispatcher;
712         int currid;
713         bool rehashing;
714
715         ModuleSQL(InspIRCd* Me)
716         : Module::Module(Me), rehashing(false)
717         {
718                 ServerInstance->Modules->UseInterface("SQLutils");
719
720                 Conf = new ConfigReader(ServerInstance);
721                 PublicServerInstance = ServerInstance;
722                 currid = 0;
723                 SQLModule = this;
724
725                 MessagePipe = new Notifier(ServerInstance);
726                 
727                 pthread_attr_t attribs;
728                 pthread_attr_init(&attribs);
729                 pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
730                 if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
731                 {
732                         throw ModuleException("m_mysql: Failed to create dispatcher thread: " + std::string(strerror(errno)));
733                 }
734                 pthread_attr_destroy(&attribs);
735
736                 if (!ServerInstance->Modules->PublishFeature("SQL", this))
737                 {
738                         /* Tell worker thread to exit NOW */
739                         int rc;
740                         void *status;
741                         giveup = true;
742                         rc = pthread_join(Dispatcher, &status);
743                         if (rc)
744                         {
745                                 ServerInstance->Log(DEFAULT,"SQL: Error code from pthread_join() is " + rc);
746                         }
747                         throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
748                 }
749
750                 ServerInstance->Modules->PublishInterface("SQL", this);
751                 Implementation eventlist[] = { I_OnRehash, I_OnRequest };
752                 ServerInstance->Modules->Attach(eventlist, this, 2);
753         }
754
755         virtual ~ModuleSQL()
756         {
757                 int rc;
758                 void *status;
759                 giveup = true;
760                 rc = pthread_join(Dispatcher, &status);
761                 if (rc)
762                 {
763                         ServerInstance->Log(DEFAULT,"SQL: Error code from pthread_join() is " + rc);
764                 }
765                 ClearAllConnections();
766                 delete Conf;
767                 ServerInstance->Modules->UnpublishInterface("SQL", this);
768                 ServerInstance->Modules->UnpublishFeature("SQL");
769                 ServerInstance->Modules->DoneWithInterface("SQLutils");
770         }
771
772
773
774         unsigned long NewID()
775         {
776                 if (currid+1 == 0)
777                         currid++;
778                 return ++currid;
779         }
780
781         virtual const char* OnRequest(Request* request)
782         {
783                 if(strcmp(SQLREQID, request->GetId()) == 0)
784                 {
785                         SQLrequest* req = (SQLrequest*)request;
786
787                         /* XXX: Lock */
788                         pthread_mutex_lock(&queue_mutex);
789
790                         ConnMap::iterator iter;
791
792                         const char* returnval = NULL;
793
794                         if((iter = Connections.find(req->dbid)) != Connections.end())
795                         {
796                                 req->id = NewID();
797                                 iter->second->queue.push(*req);
798                                 returnval = SQLSUCCESS;
799                         }
800                         else
801                         {
802                                 req->error.Id(BAD_DBID);
803                         }
804
805                         pthread_mutex_unlock(&queue_mutex);
806                         /* XXX: Unlock */
807
808                         return returnval;
809                 }
810
811                 return NULL;
812         }
813
814         virtual void OnRehash(User* user, const std::string &parameter)
815         {
816                 rehashing = true;
817         }
818         
819         virtual Version GetVersion()
820         {
821                 return Version(1,1,0,0,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
822         }
823         
824 };
825
826 void* DispatcherThread(void* arg)
827 {
828         ModuleSQL* thismodule = (ModuleSQL*)arg;
829         LoadDatabases(thismodule->Conf, thismodule->PublicServerInstance);
830
831         /* Connect back to the Notifier */
832
833         if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
834         {
835                 /* crap, we're out of sockets... */
836                 return NULL;
837         }
838
839         insp_sockaddr addr;
840
841 #ifdef IPV6
842         insp_aton("::1", &addr.sin6_addr);
843         addr.sin6_family = AF_FAMILY;
844         addr.sin6_port = htons(MessagePipe->GetPort());
845 #else
846         insp_inaddr ia;
847         insp_aton("127.0.0.1", &ia);
848         addr.sin_family = AF_FAMILY;
849         addr.sin_addr = ia;
850         addr.sin_port = htons(MessagePipe->GetPort());
851 #endif
852
853         if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
854         {
855                 /* wtf, we cant connect to it, but we just created it! */
856                 return NULL;
857         }
858
859         while (!giveup)
860         {
861                 if (thismodule->rehashing)
862                 {
863                 /* XXX: Lock */
864                         pthread_mutex_lock(&queue_mutex);
865                         thismodule->rehashing = false;
866                         LoadDatabases(thismodule->Conf, thismodule->PublicServerInstance);
867                         pthread_mutex_unlock(&queue_mutex);
868                         /* XXX: Unlock */
869                 }
870
871                 SQLConnection* conn = NULL;
872                 /* XXX: Lock here for safety */
873                 pthread_mutex_lock(&queue_mutex);
874                 for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
875                 {
876                         if (i->second->queue.totalsize())
877                         {
878                                 conn = i->second;
879                                 break;
880                         }
881                 }
882                 pthread_mutex_unlock(&queue_mutex);
883                 /* XXX: Unlock */
884
885                 /* Theres an item! */
886                 if (conn)
887                 {
888                         conn->DoLeadingQuery();
889
890                         /* XXX: Lock */
891                         pthread_mutex_lock(&queue_mutex);
892                         conn->queue.pop();
893                         pthread_mutex_unlock(&queue_mutex);
894                         /* XXX: Unlock */
895                 }
896
897                 usleep(1000);
898         }
899
900         pthread_exit((void *) 0);
901 }
902
903 MODULE_INIT(ModuleSQL)