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
17 #include <arpa/inet.h>
19 #include <sys/resource.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
28 #include "inspircd_config.h"
29 #include "socketengine.h"
33 /** This namespace contains various protocol-independent helper classes.
34 * It also contains some types which are often used by the core and modules
35 * in place of inet_* functions and types.
40 /* macros to the relevant system address description structs */
42 /** insp_sockaddr for ipv6
44 typedef struct sockaddr_in6 insp_sockaddr;
45 /** insp_inaddr for ipv6
47 typedef struct in6_addr insp_inaddr;
48 #define AF_FAMILY AF_INET6
49 #define PF_PROTOCOL PF_INET6
52 /** insp_sockaddr for ipv4
54 typedef struct sockaddr_in insp_sockaddr;
55 /** insp_inaddr for ipv4
57 typedef struct in_addr insp_inaddr;
58 #define AF_FAMILY AF_INET
59 #define PF_PROTOCOL PF_INET
62 /** Match raw binary data using CIDR rules.
64 * This function will use binary comparison to compare the
65 * two bit sequences, address and mask, up to mask_bits
66 * bits in size. If they match, it will return true.
67 * @param address The whole address, of 4 or 16 bytes in length
68 * @param mask The mask, from 1 to 16 bytes in length, anything
69 * from 1 to 128 bits of which is significant
70 * @param mask_Bits How many bits of the mask parameter are significant
71 * for this comparison.
72 * @returns True if the first mask_bits of address matches the first
75 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
77 /** Match CIDR, without matching username/nickname parts.
79 * This function will compare a human-readable address against a human-
80 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
81 * method supports both IPV4 and IPV6 addresses.
82 * @param address The human readable address, e.g. 1.2.3.4
83 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
84 * @return True if the mask matches the address
86 bool MatchCIDR(const char* address, const char* cidr_mask);
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 bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
101 /** Convert an insp_inaddr into human readable form.
103 * @param n An insp_inaddr (IP address) structure
104 * @return A human-readable address. IPV6 addresses
105 * will be shortened to remove fields which are 0.
107 const char* insp_ntoa(insp_inaddr n);
109 /** Convert a human-readable address into an insp_inaddr.
111 * @param a A human-readable address
112 * @param n An insp_inaddr struct which the result
113 * will be copied into on success.
114 * @return This function will return 0 upon success,
115 * or any other number upon failure.
117 int insp_aton(const char* a, insp_inaddr* n);
119 /** Make a socket file descriptor a blocking socket
120 * @param s A valid file descriptor
122 void Blocking(int s);
124 /** Make a socket file descriptor into a nonblocking socket
125 * @param s A valid file descriptor
127 void NonBlocking(int s);
129 /** Create a new valid file descriptor using socket()
130 * @return On return this function will return a value >= 0 for success,
131 * or a negative value upon failure (negative values are invalid file
138 /** This class handles incoming connections on client ports.
139 * It will create a new userrec for every valid connection
140 * and assign it a file descriptor.
142 class ListenSocket : public EventHandler
145 /** The creator/owner of this object
147 InspIRCd* ServerInstance;
149 /** Create a new listening socket
151 ListenSocket(InspIRCd* Instance, int sockfd, irc::sockets::insp_sockaddr client, irc::sockets::insp_sockaddr server, int port, char* addr);
152 /** Handle an I/O event
154 void HandleEvent(EventType et, int errornum = 0);