1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 "inspircd_config.h"
38 #include "socketengine.h"
40 /* Contains irc-specific definitions */
43 /** This namespace contains various protocol-independent helper classes.
44 * It also contains some types which are often used by the core and modules
45 * in place of inet_* functions and types.
50 /* macros to the relevant system address description structs */
52 /** insp_sockaddr for ipv6
54 typedef struct sockaddr_in6 insp_sockaddr;
55 /** insp_inaddr for ipv6
57 typedef struct in6_addr insp_inaddr;
58 #define AF_FAMILY AF_INET6
59 #define PF_PROTOCOL PF_INET6
62 /** insp_sockaddr for ipv4
64 typedef struct sockaddr_in insp_sockaddr;
65 /** insp_inaddr for ipv4
67 typedef struct in_addr insp_inaddr;
68 #define AF_FAMILY AF_INET
69 #define PF_PROTOCOL PF_INET
72 /** Match raw binary data using CIDR rules.
74 * This function will use binary comparison to compare the
75 * two bit sequences, address and mask, up to mask_bits
76 * bits in size. If they match, it will return true.
77 * @param address The whole address, of 4 or 16 bytes in length
78 * @param mask The mask, from 1 to 16 bytes in length, anything
79 * from 1 to 128 bits of which is significant
80 * @param mask_Bits How many bits of the mask parameter are significant
81 * for this comparison.
82 * @returns True if the first mask_bits of address matches the first
85 CoreExport bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
87 /** Match CIDR, without matching username/nickname parts.
89 * This function will compare a human-readable address against a human-
90 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
91 * method supports both IPV4 and IPV6 addresses.
92 * @param address The human readable address, e.g. 1.2.3.4
93 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
94 * @return True if the mask matches the address
96 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask);
98 /** Match CIDR, including an optional username/nickname part.
100 * This function will compare a human-readable address (plus
101 * optional username and nickname) against a human-readable
102 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
103 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
105 * @param address The human readable address, e.g. fred\@1.2.3.4
106 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
107 * @return True if the mask matches the address
109 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
111 /** Convert an insp_inaddr into human readable form.
113 * @param n An insp_inaddr (IP address) structure
114 * @return A human-readable address. IPV6 addresses
115 * will be shortened to remove fields which are 0.
117 CoreExport const char* insp_ntoa(insp_inaddr n);
119 /** Convert a human-readable address into an insp_inaddr.
121 * @param a A human-readable address
122 * @param n An insp_inaddr struct which the result
123 * will be copied into on success.
124 * @return This method will return a negative value if address
125 * does not contain a valid address family. 0 if the address is
126 * does not contain a valid string representing a valid network
127 * address. A positive value is returned if the network address
128 * was successfully converted.
130 * or any other number upon failure.
132 CoreExport int insp_aton(const char* a, insp_inaddr* n);
134 /** Create a new valid file descriptor using socket()
135 * @return On return this function will return a value >= 0 for success,
136 * or a negative value upon failure (negative values are invalid file
139 CoreExport int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
143 /** This class handles incoming connections on client ports.
144 * It will create a new User for every valid connection
145 * and assign it a file descriptor.
147 class CoreExport ListenSocket : public EventHandler
150 /** The creator/owner of this object
152 InspIRCd* ServerInstance;
153 /** Socket description (shown in stats p) */
155 /** Socket address family */
157 /** Address socket is bound to */
158 std::string bind_addr;
159 /** Port socket is bound to */
162 /** Create a new listening socket
164 ListenSocket(InspIRCd* Instance, int port, char* addr);
165 /** Handle an I/O event
167 void HandleEvent(EventType et, int errornum = 0);
171 /** Set descriptive text
173 void SetDescription(const std::string &description)
177 /** Get description for socket
179 const std::string& GetDescription()
183 /** Get port number for socket
189 /** Get IP address socket is bound to