summaryrefslogtreecommitdiff
path: root/src/modules/extra/m_mssql.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/extra/m_mssql.cpp')
-rw-r--r--src/modules/extra/m_mssql.cpp132
1 files changed, 43 insertions, 89 deletions
diff --git a/src/modules/extra/m_mssql.cpp b/src/modules/extra/m_mssql.cpp
index 972130396..ab4d2aa71 100644
--- a/src/modules/extra/m_mssql.cpp
+++ b/src/modules/extra/m_mssql.cpp
@@ -33,8 +33,7 @@ typedef std::deque<classbase*> paramlist;
typedef std::deque<MsSQLResult*> ResultQueue;
ResultNotifier* resultnotify = NULL;
-ResultNotifier* resultdispatch = NULL;
-int QueueFD = -1;
+
class ResultNotifier : public BufferedSocket
{
@@ -73,18 +72,7 @@ class ResultNotifier : public BufferedSocket
virtual int OnIncomingConnection(int newsock, char* ip)
{
- 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;
- }
+ Dispatch();
return false;
}
@@ -269,13 +257,13 @@ class SQLConn : public classbase
SQLhost host;
TDSLOGIN* login;
TDSSOCKET* sock;
- TDSCONTEXT* context;
+ TDSCONNECTION* conn;
public:
SQLConn(InspIRCd* SI, Module* m, const SQLhost& hi)
- : Instance(SI), mod(m), host(hi), login(NULL), sock(NULL), context(NULL)
+ : Instance(SI), mod(m), host(hi)
{
- if (OpenDB())
+ if (OpenDB() == TDS_SUCCEED)
{
std::string query("USE " + host.name);
if (tds_submit_query(sock, query.c_str()) == TDS_SUCCEED)
@@ -481,14 +469,16 @@ class SQLConn : public classbase
res->UpdateAffectedCount();
}
- bool OpenDB()
+ int OpenDB()
{
CloseDB();
- TDSCONNECTION* conn = NULL;
+ TDSCONTEXT* cont;
+ cont = tds_alloc_context(this);
+ cont->msg_handler = HandleMessage;
+ cont->err_handler = HandleError;
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());
@@ -498,41 +488,22 @@ class SQLConn : public classbase
tds_set_port(login, host.port);
tds_set_packet(login, 512);
- 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;
+ sock = tds_alloc_socket(cont, 512);
+ conn = tds_read_config_info(NULL, login, cont->locale);
+ return tds_connect(sock, conn);
}
void CloseDB()
{
- if (sock)
- {
- tds_free_socket(sock);
- sock = NULL;
- }
- if (context)
- {
- tds_free_context(context);
- context = NULL;
- }
if (login)
- {
tds_free_login(login);
- login = NULL;
- }
+ if (sock)
+ tds_free_socket(sock);
+ if (conn)
+ tds_free_connection(conn);
+ login = NULL;
+ sock = NULL;
+ conn = NULL;
}
SQLhost GetConfHost()
@@ -573,36 +544,32 @@ class SQLConn : public classbase
void SendNotify()
{
- if (QueueFD < 0)
+ int QueueFD;
+ if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
{
- if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
- {
- /* crap, we're out of sockets... */
- return;
- }
+ /* 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);
}
};
@@ -638,24 +605,11 @@ class ModuleMsSQL : public Module
{
ClearQueue();
ClearAllConnections();
-
- 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();
- }
-
+ resultnotify->SetFd(-1);
+ resultnotify->state = I_ERROR;
+ resultnotify->OnError(I_ERR_SOCKET);
+ resultnotify->ClosePending = true;
+ delete resultnotify;
ServerInstance->Modules->UnpublishInterface("SQL", this);
ServerInstance->Modules->UnpublishFeature("SQL");
ServerInstance->Modules->DoneWithInterface("SQLutils");