summaryrefslogtreecommitdiff
path: root/src/listensocket.cpp
blob: 313396ed7829b52a448b61069eaada1df711d274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

/* $Core */

#include "inspircd.h"
#include "socket.h"
#include "socketengine.h"


/* Private static member data must be initialized in this manner */
unsigned int ListenSocketBase::socketcount = 0;
sockaddr* ListenSocketBase::sock_us = NULL;
sockaddr* ListenSocketBase::client = NULL;
sockaddr* ListenSocketBase::raddr = NULL;

ListenSocketBase::ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
{
	this->SetFd(irc::sockets::OpenTCPSocket(addr.c_str()));
	if (this->GetFd() > -1)
	{
		if (!Instance->BindSocket(this->fd,port,addr.c_str()))
			this->fd = -1;
#ifdef IPV6
		if ((!*addr.c_str()) || (strchr(addr.c_str(),':')))
			this->family = AF_INET6;
		else
#endif
		this->family = AF_INET;
		Instance->SE->AddFd(this);
	}
	/* Saves needless allocations */
	if (socketcount == 0)
	{
		/* All instances of ListenSocket share these, so reference count it */
		ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures");
		sock_us = new sockaddr[2];
		client = new sockaddr[2];
		raddr = new sockaddr[2];
	}
	socketcount++;
}

ListenSocketBase::~ListenSocketBase()
{
	if (this->GetFd() > -1)
	{
		ServerInstance->SE->DelFd(this);
		ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
		if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
			ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
		this->fd = -1;
	}
	socketcount--;
	if (socketcount == 0)
	{
		delete[] sock_us;
		delete[] client;
		delete[] raddr;
	}
}

/* Just seperated into another func for tidiness really.. */
void ListenSocketBase::AcceptInternal()
{
	ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
	socklen_t uslen, length;		// length of our port number
	int incomingSockfd;

#ifdef IPV6
	if (this->family == AF_INET6)
	{
		uslen = sizeof(sockaddr_in6);
		length = sizeof(sockaddr_in6);
	}
	else
#endif
	{
		uslen = sizeof(sockaddr_in);
		length = sizeof(sockaddr_in);
	}

	incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);

	if (incomingSockfd < 0 ||
		  ServerInstance->SE->GetSockName(this, sock_us, &uslen) == -1)
	{
		ServerInstance->SE->Shutdown(incomingSockfd, 2);
		ServerInstance->SE->Close(incomingSockfd);
		ServerInstance->stats->statsRefused++;
		return;
	}
	/*
	 * XXX -
	 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
	 * its a pretty big but for the moment valid assumption:
	 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
	 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
	 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
	 * which for the time being is a physical impossibility (even the largest networks dont have more
	 * than about 10,000 users on ONE server!)
	 */
	if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
	{
		ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
		ServerInstance->SE->Shutdown(incomingSockfd, 2);
		ServerInstance->SE->Close(incomingSockfd);
		ServerInstance->stats->statsRefused++;
		return;
	}

	static char buf[MAXBUF];
	static char target[MAXBUF];	

	*target = *buf = '\0';

#ifdef IPV6
	if (this->family == AF_INET6)
	{
		inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
		if (!strncmp(buf, "::ffff:", 7))
		{
			memmove(buf, buf+7, sizeof(buf)-7);
		}
		socklen_t raddrsz = sizeof(sockaddr_in6);
		if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
			inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
		else
			ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
	}
	else
#endif
	{
		inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
		socklen_t raddrsz = sizeof(sockaddr_in);
		if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
			inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
		else
			ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
	}

	ServerInstance->SE->NonBlocking(incomingSockfd);
	ServerInstance->stats->statsAccept++;
	this->OnAcceptReady(target, incomingSockfd, buf);
}

void ListenSocketBase::HandleEvent(EventType e, int err)
{
	switch (e)
	{
		case EVENT_ERROR:
			ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
			break;
		case EVENT_WRITE:
			ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
			break;
		case EVENT_READ:
			this->AcceptInternal();
			break;
	}
}

void ClientListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
{
	ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, this->family, client, ipconnectedto);
}