2007-07-16 17:30:04 +00:00
|
|
|
/* +------------------------------------+
|
|
|
|
* | Inspire Internet Relay Chat Daemon |
|
|
|
|
* +------------------------------------+
|
|
|
|
*
|
2009-01-02 18:16:05 +00:00
|
|
|
* InspIRCd: (C) 2002-2009 InspIRCd Development Team
|
2009-03-15 12:42:35 +00:00
|
|
|
* See: http://wiki.inspircd.org/Credits
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
|
|
|
* This program is free but copyrighted software; see
|
|
|
|
* the file COPYING for details.
|
|
|
|
*
|
|
|
|
* ---------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INSPIRCD_SOCKET_H
|
|
|
|
#define INSPIRCD_SOCKET_H
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include "inspircd_win32wrapper.h"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-06-12 21:04:37 +00:00
|
|
|
#include <cerrno>
|
2007-07-16 17:30:04 +00:00
|
|
|
#include "socketengine.h"
|
|
|
|
|
|
|
|
/* Contains irc-specific definitions */
|
|
|
|
namespace irc
|
|
|
|
{
|
|
|
|
/** This namespace contains various protocol-independent helper classes.
|
|
|
|
* It also contains some types which are often used by the core and modules
|
|
|
|
* in place of inet_* functions and types.
|
|
|
|
*/
|
|
|
|
namespace sockets
|
|
|
|
{
|
|
|
|
|
2009-04-01 17:14:04 +00:00
|
|
|
typedef union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in in4;
|
|
|
|
struct sockaddr_in6 in6;
|
|
|
|
} sockaddrs;
|
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Match raw binary data using CIDR rules.
|
2008-06-12 21:04:37 +00:00
|
|
|
*
|
2007-07-16 17:30:04 +00:00
|
|
|
* This function will use binary comparison to compare the
|
|
|
|
* two bit sequences, address and mask, up to mask_bits
|
|
|
|
* bits in size. If they match, it will return true.
|
|
|
|
* @param address The whole address, of 4 or 16 bytes in length
|
|
|
|
* @param mask The mask, from 1 to 16 bytes in length, anything
|
|
|
|
* from 1 to 128 bits of which is significant
|
|
|
|
* @param mask_Bits How many bits of the mask parameter are significant
|
|
|
|
* for this comparison.
|
|
|
|
* @returns True if the first mask_bits of address matches the first
|
|
|
|
* mask_bits of mask.
|
|
|
|
*/
|
2008-05-09 17:24:50 +00:00
|
|
|
CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
|
2007-07-16 17:30:04 +00:00
|
|
|
|
|
|
|
/** Match CIDR, without matching username/nickname parts.
|
|
|
|
*
|
|
|
|
* This function will compare a human-readable address against a human-
|
|
|
|
* readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
|
|
|
|
* method supports both IPV4 and IPV6 addresses.
|
|
|
|
* @param address The human readable address, e.g. 1.2.3.4
|
|
|
|
* @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
|
|
|
|
* @return True if the mask matches the address
|
|
|
|
*/
|
2008-05-09 17:24:50 +00:00
|
|
|
CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
|
2007-07-16 17:30:04 +00:00
|
|
|
|
|
|
|
/** Match CIDR, including an optional username/nickname part.
|
|
|
|
*
|
|
|
|
* This function will compare a human-readable address (plus
|
|
|
|
* optional username and nickname) against a human-readable
|
|
|
|
* CIDR mask, for example joe!bloggs\@1.2.3.4 against
|
|
|
|
* *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
|
|
|
|
* IPV6 addresses.
|
|
|
|
* @param address The human readable address, e.g. fred\@1.2.3.4
|
|
|
|
* @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
|
|
|
|
* @return True if the mask matches the address
|
|
|
|
*/
|
2008-05-09 17:24:50 +00:00
|
|
|
CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
|
2007-07-16 17:30:04 +00:00
|
|
|
|
|
|
|
/** Create a new valid file descriptor using socket()
|
|
|
|
* @return On return this function will return a value >= 0 for success,
|
|
|
|
* or a negative value upon failure (negative values are invalid file
|
|
|
|
* descriptors)
|
|
|
|
*/
|
2008-09-08 20:59:16 +00:00
|
|
|
CoreExport int OpenTCPSocket(const char* addr, int socktype = SOCK_STREAM);
|
2009-04-29 03:21:15 +00:00
|
|
|
|
2009-06-16 04:02:21 +00:00
|
|
|
/** Return the size of the structure for syscall passing */
|
2009-09-21 13:26:31 +00:00
|
|
|
CoreExport int sa_size(const irc::sockets::sockaddrs& sa);
|
2009-06-16 04:02:21 +00:00
|
|
|
|
2009-04-29 03:21:15 +00:00
|
|
|
/** Convert an address-port pair into a binary sockaddr
|
|
|
|
* @param addr The IP address, IPv4 or IPv6
|
|
|
|
* @param port The port, 0 for unspecified
|
|
|
|
* @param sa The structure to place the result in. Will be zeroed prior to conversion
|
|
|
|
* @return true if the conversion was successful, false if not.
|
|
|
|
*/
|
2009-09-01 15:08:30 +00:00
|
|
|
CoreExport bool aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa);
|
2009-04-29 03:21:15 +00:00
|
|
|
/** Convert a binary sockaddr to an address-port pair
|
|
|
|
* @param sa The structure to convert
|
|
|
|
* @param addr the IP address
|
|
|
|
* @param port the port
|
|
|
|
* @return true if the conversion was successful, false if unknown address family
|
|
|
|
*/
|
2009-09-01 15:08:30 +00:00
|
|
|
CoreExport bool satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port);
|
2009-09-02 00:48:16 +00:00
|
|
|
/** Convert a binary sockaddr to a user-readable string.
|
|
|
|
* This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
|
|
|
|
* @param sa The structure to convert
|
|
|
|
* @return The string; "<unknown>" if not a valid address
|
|
|
|
*/
|
|
|
|
CoreExport std::string satouser(const irc::sockets::sockaddrs* sa);
|
2007-07-16 17:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 18:31:21 +00:00
|
|
|
|
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** This class handles incoming connections on client ports.
|
2007-10-15 20:59:05 +00:00
|
|
|
* It will create a new User for every valid connection
|
2007-07-16 17:30:04 +00:00
|
|
|
* and assign it a file descriptor.
|
|
|
|
*/
|
2008-09-08 16:58:41 +00:00
|
|
|
class CoreExport ListenSocketBase : public EventHandler
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/** The creator/owner of this object
|
|
|
|
*/
|
|
|
|
InspIRCd* ServerInstance;
|
|
|
|
/** Socket description (shown in stats p) */
|
|
|
|
std::string desc;
|
2009-05-04 23:24:00 +00:00
|
|
|
|
2009-09-02 00:48:32 +00:00
|
|
|
/** Raw address socket is bound to */
|
2007-07-16 17:30:04 +00:00
|
|
|
std::string bind_addr;
|
|
|
|
/** Port socket is bound to */
|
|
|
|
int bind_port;
|
2009-09-02 00:48:32 +00:00
|
|
|
/** Human-readable address/port socket is bound to */
|
|
|
|
std::string bind_desc;
|
2008-04-07 16:18:32 +00:00
|
|
|
|
2009-05-04 23:24:00 +00:00
|
|
|
/** The client address if the most recently connected client.
|
|
|
|
* Should only be used when accepting a new client.
|
|
|
|
*/
|
|
|
|
static irc::sockets::sockaddrs client;
|
|
|
|
/** The server address used by the most recently connected client.
|
|
|
|
* This may differ from the bind address by having a nonzero address,
|
|
|
|
* if the port is wildcard bound, or being IPv4 on a 6to4 IPv6 port.
|
|
|
|
* The address family will always match that of "client"
|
|
|
|
*/
|
|
|
|
static irc::sockets::sockaddrs server;
|
2009-09-01 15:07:02 +00:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
public:
|
|
|
|
/** Create a new listening socket
|
|
|
|
*/
|
2008-09-08 20:59:16 +00:00
|
|
|
ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr);
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Handle an I/O event
|
|
|
|
*/
|
|
|
|
void HandleEvent(EventType et, int errornum = 0);
|
|
|
|
/** Close the socket
|
|
|
|
*/
|
2008-09-08 16:58:41 +00:00
|
|
|
~ListenSocketBase();
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Set descriptive text
|
|
|
|
*/
|
|
|
|
void SetDescription(const std::string &description)
|
|
|
|
{
|
|
|
|
desc = description;
|
|
|
|
}
|
|
|
|
/** Get description for socket
|
|
|
|
*/
|
2009-09-02 00:48:32 +00:00
|
|
|
const std::string& GetDescription() { return desc; }
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Get port number for socket
|
|
|
|
*/
|
2009-09-02 00:50:35 +00:00
|
|
|
int GetPort() const { return bind_port; }
|
2009-05-04 23:24:00 +00:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Get IP address socket is bound to
|
|
|
|
*/
|
2009-05-04 23:24:00 +00:00
|
|
|
const std::string &GetIP() { return bind_addr; }
|
2008-09-07 23:08:32 +00:00
|
|
|
|
2009-09-02 00:48:32 +00:00
|
|
|
const std::string &GetBindDesc() { return bind_desc; }
|
|
|
|
|
2008-09-07 23:08:32 +00:00
|
|
|
/** Handles sockets internals crap of a connection, convenience wrapper really
|
|
|
|
*/
|
|
|
|
void AcceptInternal();
|
|
|
|
|
|
|
|
/** Called when a new connection has successfully been accepted on this listener.
|
|
|
|
* @param fd The file descriptor of the new connection
|
|
|
|
*/
|
2009-09-01 15:08:00 +00:00
|
|
|
virtual void OnAcceptReady(int fd) = 0;
|
2007-07-16 17:30:04 +00:00
|
|
|
};
|
|
|
|
|
2008-09-08 16:58:41 +00:00
|
|
|
class CoreExport ClientListenSocket : public ListenSocketBase
|
|
|
|
{
|
2009-09-01 15:08:00 +00:00
|
|
|
virtual void OnAcceptReady(int fd);
|
2008-09-08 16:58:41 +00:00
|
|
|
public:
|
2008-09-08 20:59:16 +00:00
|
|
|
ClientListenSocket(InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr) { }
|
2008-09-08 16:58:41 +00:00
|
|
|
};
|
2008-09-07 23:08:32 +00:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
#endif
|
|
|
|
|