]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listensocket.cpp
936dd85e9e1f0911616974f1418a9df8d8ade2de
[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 void ListenSocket::HandleEvent(EventType e, int err)
74 {
75         switch (e)
76         {
77                 case EVENT_ERROR:
78                         ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
79                 break;
80                 case EVENT_WRITE:
81                         ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
82                 break;
83                 case EVENT_READ:
84                 {
85                         ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
86                         socklen_t uslen, length;                // length of our port number
87                         int incomingSockfd, in_port;
88
89 #ifdef IPV6
90                         if (this->family == AF_INET6)
91                         {
92                                 uslen = sizeof(sockaddr_in6);
93                                 length = sizeof(sockaddr_in6);
94                         }
95                         else
96 #endif
97                         {
98                                 uslen = sizeof(sockaddr_in);
99                                 length = sizeof(sockaddr_in);
100                         }
101
102                         incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
103
104                         if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen)))
105                         {
106                                 char buf[MAXBUF];
107                                 char target[MAXBUF];    
108
109                                 *target = *buf = '\0';
110
111 #ifdef IPV6
112                                 if (this->family == AF_INET6)
113                                 {
114                                         in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
115                                         inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
116                                         socklen_t raddrsz = sizeof(sockaddr_in6);
117                                         if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
118                                                 inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
119                                         else
120                                                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
121                                 }
122                                 else
123 #endif
124                                 {
125                                         inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
126                                         in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
127                                         socklen_t raddrsz = sizeof(sockaddr_in);
128                                         if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
129                                                 inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
130                                         else
131                                                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
132                                 }
133                                 ServerInstance->SE->NonBlocking(incomingSockfd);
134                                 ServerInstance->stats->statsAccept++;
135                                 ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);   
136                         }
137                         else
138                         {
139                                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
140                                 ServerInstance->SE->Close(incomingSockfd);
141                                 ServerInstance->stats->statsRefused++;
142                         }
143                 }
144                 break;
145         }
146 }