1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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.
49 /* macros to the relevant system address description structs */
51 /** insp_sockaddr for ipv6
53 typedef struct sockaddr_in6 insp_sockaddr;
54 /** insp_inaddr for ipv6
56 typedef struct in6_addr insp_inaddr;
57 #define AF_FAMILY AF_INET6
58 #define PF_PROTOCOL PF_INET6
61 /** insp_sockaddr for ipv4
63 typedef struct sockaddr_in insp_sockaddr;
64 /** insp_inaddr for ipv4
66 typedef struct in_addr insp_inaddr;
67 #define AF_FAMILY AF_INET
68 #define PF_PROTOCOL PF_INET
71 /** Match raw binary data using CIDR rules.
73 * This function will use binary comparison to compare the
74 * two bit sequences, address and mask, up to mask_bits
75 * bits in size. If they match, it will return true.
76 * @param address The whole address, of 4 or 16 bytes in length
77 * @param mask The mask, from 1 to 16 bytes in length, anything
78 * from 1 to 128 bits of which is significant
79 * @param mask_Bits How many bits of the mask parameter are significant
80 * for this comparison.
81 * @returns True if the first mask_bits of address matches the first
84 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
86 /** Match CIDR, without matching username/nickname parts.
88 * This function will compare a human-readable address against a human-
89 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
90 * method supports both IPV4 and IPV6 addresses.
91 * @param address The human readable address, e.g. 1.2.3.4
92 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
93 * @return True if the mask matches the address
95 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
97 /** Match CIDR, including an optional username/nickname part.
99 * This function will compare a human-readable address (plus
100 * optional username and nickname) against a human-readable
101 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
102 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
104 * @param address The human readable address, e.g. fred\@1.2.3.4
105 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
106 * @return True if the mask matches the address
108 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
110 /** Convert an insp_inaddr into human readable form.
112 * @param n An insp_inaddr (IP address) structure
113 * @return A human-readable address. IPV6 addresses
114 * will be shortened to remove fields which are 0.
116 CoreExport const char* insp_ntoa(insp_inaddr n);
118 /** Convert a human-readable address into an insp_inaddr.
120 * @param a A human-readable address
121 * @param n An insp_inaddr struct which the result
122 * will be copied into on success.
123 * @return This method will return a negative value if address
124 * does not contain a valid address family. 0 if the address is
125 * does not contain a valid string representing a valid network
126 * address. A positive value is returned if the network address
127 * was successfully converted.
129 * or any other number upon failure.
131 CoreExport int insp_aton(const char* a, insp_inaddr* n);
133 /** Create a new valid file descriptor using socket()
134 * @return On return this function will return a value >= 0 for success,
135 * or a negative value upon failure (negative values are invalid file
138 CoreExport int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
142 /** This class handles incoming connections on client ports.
143 * It will create a new User for every valid connection
144 * and assign it a file descriptor.
146 class CoreExport ListenSocket : public EventHandler
149 /** The creator/owner of this object
151 InspIRCd* ServerInstance;
152 /** Socket description (shown in stats p) */
154 /** Socket address family */
156 /** Address socket is bound to */
157 std::string bind_addr;
158 /** Port socket is bound to */
161 static sockaddr *sock_us;
163 static sockaddr *client;
165 static sockaddr *raddr;
167 static unsigned int socketcount;
170 /** Create a new listening socket
172 ListenSocket(InspIRCd* Instance, int port, char* addr);
173 /** Handle an I/O event
175 void HandleEvent(EventType et, int errornum = 0);
179 /** Set descriptive text
181 void SetDescription(const std::string &description)
185 /** Get description for socket
187 const std::string& GetDescription()
191 /** Get port number for socket
197 /** Get IP address socket is bound to