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"
41 #ifdef CONFIG_USE_IOCP
42 #define _accept(s, addr, addrlen) __accept_socket(s, addr, addrlen, m_acceptEvent)
43 #define _getsockname(fd, sockptr, socklen) __getsockname(fd, sockptr, socklen, m_acceptEvent)
44 #define _recvfrom(s, buf, len, flags, from, fromlen) __recvfrom(s, buf, len, flags, from, fromlen, ((IOCPEngine*)ServerInstance->SE)->udp_ov)
46 #define _recvfrom recvfrom
47 #define _accept accept
48 #define _getsockname getsockname
53 /** This namespace contains various protocol-independent helper classes.
54 * It also contains some types which are often used by the core and modules
55 * in place of inet_* functions and types.
60 /* macros to the relevant system address description structs */
62 /** insp_sockaddr for ipv6
64 typedef struct sockaddr_in6 insp_sockaddr;
65 /** insp_inaddr for ipv6
67 typedef struct in6_addr insp_inaddr;
68 #define AF_FAMILY AF_INET6
69 #define PF_PROTOCOL PF_INET6
72 /** insp_sockaddr for ipv4
74 typedef struct sockaddr_in insp_sockaddr;
75 /** insp_inaddr for ipv4
77 typedef struct in_addr insp_inaddr;
78 #define AF_FAMILY AF_INET
79 #define PF_PROTOCOL PF_INET
82 /** Match raw binary data using CIDR rules.
84 * This function will use binary comparison to compare the
85 * two bit sequences, address and mask, up to mask_bits
86 * bits in size. If they match, it will return true.
87 * @param address The whole address, of 4 or 16 bytes in length
88 * @param mask The mask, from 1 to 16 bytes in length, anything
89 * from 1 to 128 bits of which is significant
90 * @param mask_Bits How many bits of the mask parameter are significant
91 * for this comparison.
92 * @returns True if the first mask_bits of address matches the first
95 CoreExport bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
97 /** Match CIDR, without matching username/nickname parts.
99 * This function will compare a human-readable address against a human-
100 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
101 * method supports both IPV4 and IPV6 addresses.
102 * @param address The human readable address, e.g. 1.2.3.4
103 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
104 * @return True if the mask matches the address
106 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask);
108 /** Match CIDR, including an optional username/nickname part.
110 * This function will compare a human-readable address (plus
111 * optional username and nickname) against a human-readable
112 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
113 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
115 * @param address The human readable address, e.g. fred\@1.2.3.4
116 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
117 * @return True if the mask matches the address
119 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
121 /** Convert an insp_inaddr into human readable form.
123 * @param n An insp_inaddr (IP address) structure
124 * @return A human-readable address. IPV6 addresses
125 * will be shortened to remove fields which are 0.
127 CoreExport const char* insp_ntoa(insp_inaddr n);
129 /** Convert a human-readable address into an insp_inaddr.
131 * @param a A human-readable address
132 * @param n An insp_inaddr struct which the result
133 * will be copied into on success.
134 * @return This method will return a negative value if address
135 * does not contain a valid address family. 0 if the address is
136 * does not contain a valid string representing a valid network
137 * address. A positive value is returned if the network address
138 * was successfully converted.
140 * or any other number upon failure.
142 CoreExport int insp_aton(const char* a, insp_inaddr* n);
144 /** Make a socket file descriptor a blocking socket
145 * @param s A valid file descriptor
147 CoreExport void Blocking(int s);
149 /** Make a socket file descriptor into a nonblocking socket
150 * @param s A valid file descriptor
152 CoreExport void NonBlocking(int s);
154 /** Create a new valid file descriptor using socket()
155 * @return On return this function will return a value >= 0 for success,
156 * or a negative value upon failure (negative values are invalid file
159 CoreExport int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
163 /** This class handles incoming connections on client ports.
164 * It will create a new userrec for every valid connection
165 * and assign it a file descriptor.
167 class CoreExport ListenSocket : public EventHandler
170 /** The creator/owner of this object
172 InspIRCd* ServerInstance;
175 std::string bind_addr;
178 /** Create a new listening socket
180 ListenSocket(InspIRCd* Instance, int port, char* addr);
181 /** Handle an I/O event
183 void HandleEvent(EventType et, int errornum = 0);
187 /** Set descriptive text
189 void SetDescription(const std::string &description)
194 const std::string& GetDescription()