1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #ifndef INSPIRCD_SOCKET_H
18 #define INSPIRCD_SOCKET_H
20 #include <arpa/inet.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
31 #include "inspircd_config.h"
32 #include "socketengine.h"
36 /** This namespace contains various protocol-independent helper classes.
37 * It also contains some types which are often used by the core and modules
38 * in place of inet_* functions and types.
43 /* macros to the relevant system address description structs */
45 /** insp_sockaddr for ipv6
47 typedef struct sockaddr_in6 insp_sockaddr;
48 /** insp_inaddr for ipv6
50 typedef struct in6_addr insp_inaddr;
51 #define AF_FAMILY AF_INET6
52 #define PF_PROTOCOL PF_INET6
55 /** insp_sockaddr for ipv4
57 typedef struct sockaddr_in insp_sockaddr;
58 /** insp_inaddr for ipv4
60 typedef struct in_addr insp_inaddr;
61 #define AF_FAMILY AF_INET
62 #define PF_PROTOCOL PF_INET
65 /** Match raw binary data using CIDR rules.
67 * This function will use binary comparison to compare the
68 * two bit sequences, address and mask, up to mask_bits
69 * bits in size. If they match, it will return true.
70 * @param address The whole address, of 4 or 16 bytes in length
71 * @param mask The mask, from 1 to 16 bytes in length, anything
72 * from 1 to 128 bits of which is significant
73 * @param mask_Bits How many bits of the mask parameter are significant
74 * for this comparison.
75 * @returns True if the first mask_bits of address matches the first
78 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
80 /** Match CIDR, without matching username/nickname parts.
82 * This function will compare a human-readable address against a human-
83 * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
84 * method supports both IPV4 and IPV6 addresses.
85 * @param address The human readable address, e.g. 1.2.3.4
86 * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
87 * @return True if the mask matches the address
89 bool MatchCIDR(const char* address, const char* cidr_mask);
91 /** Match CIDR, including an optional username/nickname part.
93 * This function will compare a human-readable address (plus
94 * optional username and nickname) against a human-readable
95 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
96 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
98 * @param address The human readable address, e.g. fred\@1.2.3.4
99 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
100 * @return True if the mask matches the address
102 bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
104 /** Convert an insp_inaddr into human readable form.
106 * @param n An insp_inaddr (IP address) structure
107 * @return A human-readable address. IPV6 addresses
108 * will be shortened to remove fields which are 0.
110 const char* insp_ntoa(insp_inaddr n);
112 /** Convert a human-readable address into an insp_inaddr.
114 * @param a A human-readable address
115 * @param n An insp_inaddr struct which the result
116 * will be copied into on success.
117 * @return This function will return 0 upon success,
118 * or any other number upon failure.
120 int insp_aton(const char* a, insp_inaddr* n);
122 /** Make a socket file descriptor a blocking socket
123 * @param s A valid file descriptor
125 void Blocking(int s);
127 /** Make a socket file descriptor into a nonblocking socket
128 * @param s A valid file descriptor
130 void NonBlocking(int s);
132 /** Create a new valid file descriptor using socket()
133 * @return On return this function will return a value >= 0 for success,
134 * or a negative value upon failure (negative values are invalid file
141 /** This class handles incoming connections on client ports.
142 * It will create a new userrec for every valid connection
143 * and assign it a file descriptor.
145 class ListenSocket : public EventHandler
148 /** The creator/owner of this object
150 InspIRCd* ServerInstance;
152 /** Create a new listening socket
154 ListenSocket(InspIRCd* Instance, int sockfd, irc::sockets::insp_sockaddr client, irc::sockets::insp_sockaddr server, int port, char* addr);
155 /** Handle an I/O event
157 void HandleEvent(EventType et);