]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xmlsocket.cpp
Refactor port binding, warning not yet tested fully
[user/henk/code/inspircd.git] / src / modules / m_xmlsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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_config.h"
15 #include "configreader.h"
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "hashcomp.h"
20 #include "inspircd.h"
21
22 /* $ModDesc: Provides XMLSocket support for clients */
23
24 class ModuleXMLSocket : public Module
25 {
26         ConfigReader* Conf;
27         std::vector<int> listenports;
28
29  public:
30
31         ModuleXMLSocket(InspIRCd* Me)
32                 : Module::Module(Me)
33         {
34                 OnRehash(NULL,"");
35         }
36
37         virtual void OnRehash(userrec* user, const std::string &param)
38         {
39
40                 Conf = new ConfigReader(ServerInstance);
41
42                 for (unsigned int i = 0; i < listenports.size(); i++)
43                 {
44                         ServerInstance->Config->DelIOHook(listenports[i]);
45                 }
46
47                 listenports.clear();
48
49                 for (int i = 0; i < Conf->Enumerate("bind"); i++)
50                 {
51                         // For each <bind> tag
52                         if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
53                         {
54                                 // Get the port we're meant to be listening on with SSL
55                                 std::string port = Conf->ReadValue("bind", "port", i);
56                                 irc::portparser portrange(port, false);
57                                 long portno = -1;
58                                 while ((portno = portrange.GetToken()))
59                                 {
60                                         try
61                                         {
62                                                 if (ServerInstance->Config->AddIOHook(portno, this))
63                                                 {
64                                                         listenports.push_back(portno);
65                                                                 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
66                                                                 if (ServerInstance->Config->ports[i]->GetPort() == portno)
67                                                                         ServerInstance->Config->ports[i]->SetDescription("xml");
68                                                 }
69                                                 else
70                                                 {
71                                                         ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
72                                                 }
73                                         }
74                                         catch (ModuleException &e)
75                                         {
76                                                 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d: %s. Maybe it's already hooked by the same port on a different IP, or you have another similar module loaded?", portno, e.GetReason());
77                                         }
78                                 }
79                         }
80                 }
81
82                 DELETE(Conf);
83         }
84
85         virtual ~ModuleXMLSocket()
86         {
87         }
88
89         virtual void OnUnloadModule(Module* mod, const std::string &name)
90         {
91                 if (mod == this)
92                 {
93                         for(unsigned int i = 0; i < listenports.size(); i++)
94                         {
95                                 ServerInstance->Config->DelIOHook(listenports[i]);
96                                 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
97                                         if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
98                                                 ServerInstance->Config->ports[j]->SetDescription("plaintext");
99                         }
100                 }
101         }
102
103         virtual Version GetVersion()
104         {
105                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
106         }
107
108         void Implements(char* List)
109         {
110                 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
111         }
112
113         virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
114         {
115                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
116
117                 if (user == NULL)
118                         return -1;
119
120                 int result = user->ReadData(buffer, count);
121
122                 if ((result == -1) && (errno == EAGAIN))
123                         return -1;
124                 else if (result < 1)
125                         return 0;
126
127                 /* XXX: The core is more than happy to split lines purely on an \n
128                  * rather than a \r\n. This is good for us as it means that the size
129                  * of data we are receiving is exactly the same as the size of data
130                  * we asked for, and we dont need to re-implement our own socket
131                  * buffering (See below)
132                  */
133                 for (int n = 0; n < result; n++)
134                         if (buffer[n] == 0)
135                                 buffer[n] = '\n';
136
137                 readresult = result;
138                 return result;
139         }
140
141         virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
142         {
143                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
144
145                 if (user == NULL)
146                         return -1;
147
148                 /* We want to alter the buffer, so we have to make a copy */
149                 char tmpbuffer[count+1];
150                 memcpy(tmpbuffer, buffer, count);
151
152                 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
153                  * rather than lines "looking\0like\0this". This shouldnt be a problem
154                  * to the client, but it saves us a TON of processing and the need
155                  * to re-implement socket buffering, as the data we are sending is
156                  * exactly the same length as the data we are receiving.
157                  */
158                 for (int n = 0; n < count; n++)
159                         if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
160                                 tmpbuffer[n] = 0;
161
162                 user->AddWriteBuf(std::string(tmpbuffer,count));
163
164                 return 1;
165         }
166
167 };
168
169 class ModuleXMLSocketFactory : public ModuleFactory
170 {
171  public:
172         ModuleXMLSocketFactory()
173         {
174         }
175
176         ~ModuleXMLSocketFactory()
177         {
178         }
179
180         virtual Module * CreateModule(InspIRCd* Me)
181         {
182                 return new ModuleXMLSocket(Me);
183         }
184 };
185
186
187 extern "C" void * init_module( void )
188 {
189         return new ModuleXMLSocketFactory;
190 }