diff options
-rw-r--r-- | src/modules/extra/m_mssql.cpp | 132 |
1 files changed, 89 insertions, 43 deletions
diff --git a/src/modules/extra/m_mssql.cpp b/src/modules/extra/m_mssql.cpp index ab4d2aa71..972130396 100644 --- a/src/modules/extra/m_mssql.cpp +++ b/src/modules/extra/m_mssql.cpp @@ -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) @@ -469,16 +481,14 @@ class SQLConn : public classbase res->UpdateAffectedCount(); } - int OpenDB() + bool OpenDB() { CloseDB(); - TDSCONTEXT* cont; - cont = tds_alloc_context(this); - 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()); @@ -488,22 +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); - login = NULL; - sock = NULL; - conn = NULL; + sock = NULL; + } + if (context) + { + tds_free_context(context); + context = NULL; + } + if (login) + { + tds_free_login(login); + login = NULL; + } } SQLhost GetConfHost() @@ -544,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); } }; @@ -605,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"); |