]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mssql.cpp
httpd connection close fixes (these need to be backported to 1.1 at some point?)...
[user/henk/code/inspircd.git] / src / modules / extra / m_mssql.cpp
index 2038052055bec3f5502f1fb6bc68eb47597a4796..97213039663fa1dc7f9426c9d1b69acfcc61cf54 100644 (file)
@@ -33,7 +33,8 @@ typedef std::deque<classbase*> paramlist;
 typedef std::deque<MsSQLResult*> ResultQueue;
 
 ResultNotifier* resultnotify = NULL;
-
+ResultNotifier* resultdispatch = NULL;
+int QueueFD = -1;
 
 class ResultNotifier : public BufferedSocket
 {
@@ -72,7 +73,18 @@ class ResultNotifier : public BufferedSocket
 
        virtual int OnIncomingConnection(int newsock, char* ip)
        {
-               Dispatch();
+               resultdispatch = new ResultNotifier(Instance, mod, newsock, ip);
+               return true;
+       }
+
+       virtual bool OnDataReady()
+       {
+               char data = 0;
+               if (Instance->SE->Recv(this, &data, 1, 0) > 0)
+               {
+                       Dispatch();
+                       return true;
+               }
                return false;
        }
 
@@ -257,13 +269,13 @@ class SQLConn : public classbase
        SQLhost host;
        TDSLOGIN* login;
        TDSSOCKET* sock;
-       TDSCONNECTION* conn;
+       TDSCONTEXT* context;
 
   public:
        SQLConn(InspIRCd* SI, Module* m, const SQLhost& hi)
-       : Instance(SI), mod(m), host(hi)
+       : Instance(SI), mod(m), host(hi), login(NULL), sock(NULL), context(NULL)
        {
-               if (OpenDB() == TDS_SUCCEED)
+               if (OpenDB())
                {
                        std::string query("USE " + host.name);
                        if (tds_submit_query(sock, query.c_str()) == TDS_SUCCEED)
@@ -294,6 +306,9 @@ class SQLConn : public classbase
 
        SQLerror Query(SQLrequest &req)
        {
+               if (!sock)
+                       return SQLerror(BAD_CONN, "Socket was NULL, check if SQL server is running.");
+                       
                /* Pointer to the buffer we screw around with substitution in */
                char* query;
 
@@ -368,17 +383,18 @@ class SQLConn : public classbase
                res->dbid = host.id;
                res->query = req.query.q;
 
-               const char* msquery = strdup(req.query.q.data());
+               char* msquery = strdup(req.query.q.data());
                Instance->Logs->Log("m_mssql",DEBUG,"doing Query: %s",msquery);
                if (tds_submit_query(sock, msquery) != TDS_SUCCEED)
                {
                        std::string error("failed to execute: "+std::string(req.query.q.data()));
                        delete[] query;
                        delete res;
+                       free(msquery);
                        return SQLerror(QSEND_FAIL, error);
                }
                delete[] query;
-               delete[] msquery;
+               free(msquery);
                
                int tds_res;
                while (tds_process_tokens(sock, &tds_res, NULL, TDS_TOKEN_RESULTS) == TDS_SUCCEED)
@@ -443,17 +459,15 @@ class SQLConn : public classbase
 
        static int HandleMessage(const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
        {
-               /* TODO: FIXME */
-               //Instance->Logs->Log("m_mssql",DEBUG,pMessage->message);
-               //printf("Message: %s\n", pMessage->message);
+               SQLConn* sc = (SQLConn*)pContext->parent;
+               sc->Instance->Logs->Log("m_mssql", DEBUG, "Message for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
                return 0;
        }
 
        static int HandleError(const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
        {
-               /* TODO: FIXME */
-               //Instance->Logs->Log("m_mssql",DEBUG,pMessage->message);
-               //printf("Error: %s\n", pMessage->message);
+               SQLConn* sc = (SQLConn*)pContext->parent;
+               sc->Instance->Logs->Log("m_mssql", DEFAULT, "Error for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
                return 0;
        }
 
@@ -467,16 +481,14 @@ class SQLConn : public classbase
                res->UpdateAffectedCount();
        }
 
-       int OpenDB()
+       bool OpenDB()
        {
                CloseDB();
 
-               TDSCONTEXT* cont;
-               cont = tds_alloc_context(NULL);
-               cont->msg_handler = HandleMessage;
-               cont->err_handler = HandleError;
+               TDSCONNECTION* conn = NULL;
 
                login = tds_alloc_login();
+               tds_set_app(login, "TSQL");
                tds_set_library(login,"TDS-Library");
                tds_set_host(login, "");
                tds_set_server(login, host.host.c_str());
@@ -486,19 +498,41 @@ class SQLConn : public classbase
                tds_set_port(login, host.port);
                tds_set_packet(login, 512);
 
-               sock = tds_alloc_socket(cont, 512);
-               conn = tds_read_config_info(NULL, login, cont->locale);
-               return tds_connect(sock, conn);
+               context = tds_alloc_context(this);
+               context->msg_handler = HandleMessage;
+               context->err_handler = HandleError;
+
+               sock = tds_alloc_socket(context, 512);
+               tds_set_parent(sock, NULL);
+
+               conn = tds_read_config_info(NULL, login, context->locale);
+
+               if (tds_connect(sock, conn) == TDS_SUCCEED)
+               {
+                       tds_free_connection(conn);
+                       return 1;
+               }
+               tds_free_connection(conn);
+               return 0;
        }
 
        void CloseDB()
        {
-               if (login)
-                       tds_free_login(login);
                if (sock)
+               {
                        tds_free_socket(sock);
-               if (conn)
-                       tds_free_connection(conn);
+                       sock = NULL;
+               }
+               if (context)
+               {
+                       tds_free_context(context);
+                       context = NULL;
+               }
+               if (login)
+               {
+                       tds_free_login(login);
+                       login = NULL;
+               }
        }
 
        SQLhost GetConfHost()
@@ -539,32 +573,36 @@ class SQLConn : public classbase
 
        void SendNotify()
        {
-               int QueueFD;
-               if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
+               if (QueueFD < 0)
                {
-                       /* crap, we're out of sockets... */
-                       return;
-               }
+                       if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
+                       {
+                               /* crap, we're out of sockets... */
+                               return;
+                       }
 
-               insp_sockaddr addr;
+                       insp_sockaddr addr;
 
 #ifdef IPV6
-               insp_aton("::1", &addr.sin6_addr);
-               addr.sin6_family = AF_FAMILY;
-               addr.sin6_port = htons(resultnotify->GetPort());
+                       insp_aton("::1", &addr.sin6_addr);
+                       addr.sin6_family = AF_FAMILY;
+                       addr.sin6_port = htons(resultnotify->GetPort());
 #else
-               insp_inaddr ia;
-               insp_aton("127.0.0.1", &ia);
-               addr.sin_family = AF_FAMILY;
-               addr.sin_addr = ia;
-               addr.sin_port = htons(resultnotify->GetPort());
+                       insp_inaddr ia;
+                       insp_aton("127.0.0.1", &ia);
+                       addr.sin_family = AF_FAMILY;
+                       addr.sin_addr = ia;
+                       addr.sin_port = htons(resultnotify->GetPort());
 #endif
 
-               if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
-               {
-                       /* wtf, we cant connect to it, but we just created it! */
-                       return;
+                       if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
+                       {
+                               /* wtf, we cant connect to it, but we just created it! */
+                               return;
+                       }
                }
+               char id = 0;
+               send(QueueFD, &id, 1, 0);
        }
 
 };
@@ -600,11 +638,24 @@ class ModuleMsSQL : public Module
        {
                ClearQueue();
                ClearAllConnections();
-               resultnotify->SetFd(-1);
-               resultnotify->state = I_ERROR;
-               resultnotify->OnError(I_ERR_SOCKET);
-               resultnotify->ClosePending = true;
-               delete resultnotify;
+
+               ServerInstance->SE->DelFd(resultnotify);
+               resultnotify->Close();
+               ServerInstance->BufferedSocketCull();
+
+               if (QueueFD >= 0)
+               {
+                       shutdown(QueueFD, 2);
+                       close(QueueFD);
+               }
+
+               if (resultdispatch)
+               {
+                       ServerInstance->SE->DelFd(resultdispatch);
+                       resultdispatch->Close();
+                       ServerInstance->BufferedSocketCull();
+               }
+
                ServerInstance->Modules->UnpublishInterface("SQL", this);
                ServerInstance->Modules->UnpublishFeature("SQL");
                ServerInstance->Modules->DoneWithInterface("SQLutils");
@@ -649,7 +700,6 @@ class ModuleMsSQL : public Module
                        host.name       = conf.ReadValue("database", "name", i);
                        host.user       = conf.ReadValue("database", "username", i);
                        host.pass       = conf.ReadValue("database", "password", i);
-                       host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
                        if (h == host)
                                return true;
                }
@@ -671,7 +721,6 @@ class ModuleMsSQL : public Module
                        host.name       = conf.ReadValue("database", "name", i);
                        host.user       = conf.ReadValue("database", "username", i);
                        host.pass       = conf.ReadValue("database", "password", i);
-                       host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
 
                        if (HasHost(host))
                                continue;