1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_SOCKET_H
15 #define INSPIRCD_SOCKET_H
19 #include <arpa/inet.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
32 #include "inspircd_win32wrapper.h"
37 #include "socketengine.h"
39 /* Contains irc-specific definitions */
42 /** This namespace contains various protocol-independent helper classes.
43 * It also contains some types which are often used by the core and modules
44 * in place of inet_* functions and types.
51 struct sockaddr_in in4;
52 struct sockaddr_in6 in6;
55 /** Match raw binary data using CIDR rules.
57 * This function will use binary comparison to compare the
58 * two bit sequences, address and mask, up to mask_bits
59 * bits in size. If they match, it will return true.
60 * @param address The whole address, of 4 or 16 bytes in length
61 * @param mask The mask, from 1 to 16 bytes in length, anything
62 * from 1 to 128 bits of which is significant
63 * @param mask_Bits How many bits of the mask parameter are significant
64 * for this comparison.
65 * @returns True if the first mask_bits of address matches the first
68 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
70 /** Match CIDR, without matching username/nickname parts.
72 * This function will compare a human-readable address against a human-
73 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
74 * method supports both IPV4 and IPV6 addresses.
75 * @param address The human readable address, e.g. 1.2.3.4
76 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
77 * @return True if the mask matches the address
79 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
81 /** Match CIDR, including an optional username/nickname part.
83 * This function will compare a human-readable address (plus
84 * optional username and nickname) against a human-readable
85 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
86 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
88 * @param address The human readable address, e.g. fred\@1.2.3.4
89 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
90 * @return True if the mask matches the address
92 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
94 /** Create a new valid file descriptor using socket()
95 * @return On return this function will return a value >= 0 for success,
96 * or a negative value upon failure (negative values are invalid file
99 CoreExport int OpenTCPSocket(const std::string& addr, int socktype = SOCK_STREAM);
101 /** Return the size of the structure for syscall passing */
102 CoreExport int sa_size(const irc::sockets::sockaddrs& sa);
104 /** Convert an address-port pair into a binary sockaddr
105 * @param addr The IP address, IPv4 or IPv6
106 * @param port The port, 0 for unspecified
107 * @param sa The structure to place the result in. Will be zeroed prior to conversion
108 * @return true if the conversion was successful, false if not.
110 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
112 /** Convert a binary sockaddr to an address-port pair
113 * @param sa The structure to convert
114 * @param addr the IP address
115 * @param port the port
116 * @return true if the conversion was successful, false if unknown address family
118 CoreExport bool satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port);
120 /** Convert a binary sockaddr to a user-readable string.
121 * This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
122 * @param sa The structure to convert
123 * @return The string; "<unknown>" if not a valid address
125 CoreExport std::string satouser(const irc::sockets::sockaddrs& sa);
127 /** Create a CIDR mask from the given address, of length <range>
128 * Result will be of the form 192.0.5.0/24 or 2001:af35::/48
130 CoreExport std::string mask(irc::sockets::sockaddrs sa, unsigned int range);
134 /** This class handles incoming connections on client ports.
135 * It will create a new User for every valid connection
136 * and assign it a file descriptor.
138 class CoreExport ListenSocket : public EventHandler
141 const reference<ConfigTag> bind_tag;
142 std::string bind_addr;
144 /** Human-readable bind description */
145 std::string bind_desc;
146 /** Create a new listening socket
148 ListenSocket(ConfigTag* tag, const std::string& addr, int port);
149 /** Handle an I/O event
151 void HandleEvent(EventType et, int errornum = 0);
156 /** Handles sockets internals crap of a connection, convenience wrapper really
158 void AcceptInternal();