]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listensocket.cpp
Add incoming IP to OnAcceptReady
[user/henk/code/inspircd.git] / src / listensocket.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 /* $Core */
15
16 #include "inspircd.h"
17 #include "socket.h"
18 #include "socketengine.h"
19
20
21 /* Private static member data must be initialized in this manner */
22 unsigned int ListenSocket::socketcount = 0;
23 sockaddr* ListenSocket::sock_us = NULL;
24 sockaddr* ListenSocket::client = NULL;
25 sockaddr* ListenSocket::raddr = NULL;
26
27 ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
28 {
29         this->SetFd(irc::sockets::OpenTCPSocket(addr));
30         if (this->GetFd() > -1)
31         {
32                 if (!Instance->BindSocket(this->fd,port,addr))
33                         this->fd = -1;
34 #ifdef IPV6
35                 if ((!*addr) || (strchr(addr,':')))
36                         this->family = AF_INET6;
37                 else
38 #endif
39                 this->family = AF_INET;
40                 Instance->SE->AddFd(this);
41         }
42         /* Saves needless allocations */
43         if (socketcount == 0)
44         {
45                 /* All instances of ListenSocket share these, so reference count it */
46                 ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures");
47                 sock_us = new sockaddr[2];
48                 client = new sockaddr[2];
49                 raddr = new sockaddr[2];
50         }
51         socketcount++;
52 }
53
54 ListenSocket::~ListenSocket()
55 {
56         if (this->GetFd() > -1)
57         {
58                 ServerInstance->SE->DelFd(this);
59                 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
60                 if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
61                         ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
62                 this->fd = -1;
63         }
64         socketcount--;
65         if (socketcount == 0)
66         {
67                 delete[] sock_us;
68                 delete[] client;
69                 delete[] raddr;
70         }
71 }
72
73 /* Just seperated into another func for tidiness really.. */
74 void ListenSocket::AcceptInternal()
75 {
76         ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
77         socklen_t uslen, length;                // length of our port number
78         int incomingSockfd;
79
80 #ifdef IPV6
81         if (this->family == AF_INET6)
82         {
83                 uslen = sizeof(sockaddr_in6);
84                 length = sizeof(sockaddr_in6);
85         }
86         else
87 #endif
88         {
89                 uslen = sizeof(sockaddr_in);
90                 length = sizeof(sockaddr_in);
91         }
92
93         incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
94
95         if (incomingSockfd < 0 ||
96                   ServerInstance->SE->GetSockName(this, sock_us, &uslen) == -1)
97         {
98                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
99                 ServerInstance->SE->Close(incomingSockfd);
100                 ServerInstance->stats->statsRefused++;
101                 return;
102         }
103
104         static char buf[MAXBUF];
105         static char target[MAXBUF];     
106
107         *target = *buf = '\0';
108
109 #ifdef IPV6
110         if (this->family == AF_INET6)
111         {
112                 inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
113                 socklen_t raddrsz = sizeof(sockaddr_in6);
114                 if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
115                         inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
116                 else
117                         ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
118         }
119         else
120 #endif
121         {
122                 inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
123                 socklen_t raddrsz = sizeof(sockaddr_in);
124                 if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
125                         inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
126                 else
127                         ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
128         }
129
130         /*
131          * XXX -
132          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
133          * its a pretty big but for the moment valid assumption:
134          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
135          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
136          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
137          * which for the time being is a physical impossibility (even the largest networks dont have more
138          * than about 10,000 users on ONE server!)
139          */
140         if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
141         {
142                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
143                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
144                 ServerInstance->SE->Close(incomingSockfd);
145                 ServerInstance->stats->statsRefused++;
146                 return;
147         }
148
149         ServerInstance->SE->NonBlocking(incomingSockfd);
150         ServerInstance->stats->statsAccept++;
151         this->OnAcceptReady(target, incomingSockfd, buf);
152 }
153
154 void ListenSocket::HandleEvent(EventType e, int err)
155 {
156         switch (e)
157         {
158                 case EVENT_ERROR:
159                         ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
160                         break;
161                 case EVENT_WRITE:
162                         ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
163                         break;
164                 case EVENT_READ:
165                         this->AcceptInternal();
166                         break;
167         }
168 }
169
170 void ListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
171 {
172                 ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, this->family, client, ipconnectedto);
173 }