1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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"
38 /* Contains irc-specific definitions */
41 /** This namespace contains various protocol-independent helper classes.
42 * It also contains some types which are often used by the core and modules
43 * in place of inet_* functions and types.
47 union CoreExport sockaddrs
50 struct sockaddr_in in4;
51 struct sockaddr_in6 in6;
52 /** Return the size of the structure for syscall passing */
54 /** Return port number or -1 if invalid */
57 std::string addr() const;
58 /** Return human-readable IP/port pair */
59 std::string str() const;
60 bool operator==(const sockaddrs& other) const;
61 inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
64 struct CoreExport cidr_mask
66 /** Type, AF_INET or AF_INET6 */
68 /** Length of the mask in bits (0-128) */
70 /** Raw bits. Unused bits must be zero */
71 unsigned char bits[16];
74 /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
75 cidr_mask(const std::string& mask);
76 /** Construct a CIDR mask of a given length from the given address */
77 cidr_mask(const irc::sockets::sockaddrs& addr, int len);
78 /** Equality of bits, type, and length */
79 bool operator==(const cidr_mask& other) const;
80 /** Ordering defined for maps */
81 bool operator<(const cidr_mask& other) const;
82 /** Match within this CIDR? */
83 bool match(const irc::sockets::sockaddrs& addr) const;
84 /** Human-readable string */
85 std::string str() const;
88 /** Match CIDR, including an optional username/nickname part.
90 * This function will compare a human-readable address (plus
91 * optional username and nickname) against a human-readable
92 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
93 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
95 * @param address The human readable address, e.g. fred\@1.2.3.4
96 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
97 * @return True if the mask matches the address
99 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
101 /** Return the size of the structure for syscall passing */
102 inline int sa_size(const irc::sockets::sockaddrs& sa) { return sa.sa_size(); }
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 inline std::string satouser(const irc::sockets::sockaddrs& sa) { return sa.str(); }
129 #include "socketengine.h"
130 /** This class handles incoming connections on client ports.
131 * It will create a new User for every valid connection
132 * and assign it a file descriptor.
134 class CoreExport ListenSocket : public EventHandler
137 const reference<ConfigTag> bind_tag;
138 std::string bind_addr;
140 /** Human-readable bind description */
141 std::string bind_desc;
142 /** Create a new listening socket
144 ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
145 /** Handle an I/O event
147 void HandleEvent(EventType et, int errornum = 0);
152 /** Handles sockets internals crap of a connection, convenience wrapper really
154 void AcceptInternal();