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 /* macros to the relevant system address description structs */
57 /** insp_sockaddr for ipv6
59 typedef struct sockaddr_in6 insp_sockaddr;
60 /** insp_inaddr for ipv6
62 typedef struct in6_addr insp_inaddr;
63 #define AF_FAMILY AF_INET6
64 #define PF_PROTOCOL PF_INET6
67 /** insp_sockaddr for ipv4
69 typedef struct sockaddr_in insp_sockaddr;
70 /** insp_inaddr for ipv4
72 typedef struct in_addr insp_inaddr;
73 #define AF_FAMILY AF_INET
74 #define PF_PROTOCOL PF_INET
77 /** Match raw binary data using CIDR rules.
79 * This function will use binary comparison to compare the
80 * two bit sequences, address and mask, up to mask_bits
81 * bits in size. If they match, it will return true.
82 * @param address The whole address, of 4 or 16 bytes in length
83 * @param mask The mask, from 1 to 16 bytes in length, anything
84 * from 1 to 128 bits of which is significant
85 * @param mask_Bits How many bits of the mask parameter are significant
86 * for this comparison.
87 * @returns True if the first mask_bits of address matches the first
90 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
92 /** Match CIDR, without matching username/nickname parts.
94 * This function will compare a human-readable address against a human-
95 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
96 * method supports both IPV4 and IPV6 addresses.
97 * @param address The human readable address, e.g. 1.2.3.4
98 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
99 * @return True if the mask matches the address
101 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
103 /** Match CIDR, including an optional username/nickname part.
105 * This function will compare a human-readable address (plus
106 * optional username and nickname) against a human-readable
107 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
108 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
110 * @param address The human readable address, e.g. fred\@1.2.3.4
111 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
112 * @return True if the mask matches the address
114 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
116 /** Convert an insp_inaddr into human readable form.
118 * @param n An insp_inaddr (IP address) structure
119 * @return A human-readable address. IPV6 addresses
120 * will be shortened to remove fields which are 0.
122 CoreExport const char* insp_ntoa(insp_inaddr n);
124 /** Convert a human-readable address into an insp_inaddr.
126 * @param a A human-readable address
127 * @param n An insp_inaddr struct which the result
128 * will be copied into on success.
129 * @return This method will return a negative value if address
130 * does not contain a valid address family. 0 if the address is
131 * does not contain a valid string representing a valid network
132 * address. A positive value is returned if the network address
133 * was successfully converted.
135 * or any other number upon failure.
137 CoreExport int insp_aton(const char* a, insp_inaddr* n);
139 /** Create a new valid file descriptor using socket()
140 * @return On return this function will return a value >= 0 for success,
141 * or a negative value upon failure (negative values are invalid file
144 CoreExport int OpenTCPSocket(const char* addr, int socktype = SOCK_STREAM);
146 /** Convert an address-port pair into a binary sockaddr
147 * @param addr The IP address, IPv4 or IPv6
148 * @param port The port, 0 for unspecified
149 * @param sa The structure to place the result in. Will be zeroed prior to conversion
150 * @return true if the conversion was successful, false if not.
152 CoreExport int aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa);
153 /** Convert a binary sockaddr to an address-port pair
154 * @param sa The structure to convert
155 * @param addr the IP address
156 * @param port the port
157 * @return true if the conversion was successful, false if unknown address family
159 CoreExport int satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port);
165 /** This class handles incoming connections on client ports.
166 * It will create a new User for every valid connection
167 * and assign it a file descriptor.
169 class CoreExport ListenSocketBase : public EventHandler
172 /** The creator/owner of this object
174 InspIRCd* ServerInstance;
175 /** Socket description (shown in stats p) */
177 /** Socket address family */
179 /** Address socket is bound to */
180 std::string bind_addr;
181 /** Port socket is bound to */
184 static sockaddr *sock_us;
186 static sockaddr *client;
188 static sockaddr *raddr;
190 static unsigned int socketcount;
193 /** Create a new listening socket
195 ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr);
196 /** Handle an I/O event
198 void HandleEvent(EventType et, int errornum = 0);
202 /** Set descriptive text
204 void SetDescription(const std::string &description)
208 /** Get description for socket
210 const std::string& GetDescription()
214 /** Get port number for socket
220 /** Get IP address socket is bound to
227 /** Handles sockets internals crap of a connection, convenience wrapper really
229 void AcceptInternal();
231 /** Called when a new connection has successfully been accepted on this listener.
232 * @param ipconnectedto The IP address the connection arrived on
233 * @param fd The file descriptor of the new connection
234 * @param incomingip The IP from which the connection was made
236 virtual void OnAcceptReady(const std::string &ipconnectedto, int fd, const std::string &incomingip) = 0;
239 class CoreExport ClientListenSocket : public ListenSocketBase
241 virtual void OnAcceptReady(const std::string &ipconnectedto, int fd, const std::string &incomingip);
243 ClientListenSocket(InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr) { }