]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Add irc::sockets::satouser for user-readable IP/port pairs
[user/henk/code/inspircd.git] / include / socket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef INSPIRCD_SOCKET_H
15 #define INSPIRCD_SOCKET_H
16
17 #ifndef WIN32
18
19 #include <arpa/inet.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <netdb.h>
29
30 #else
31
32 #include "inspircd_win32wrapper.h"
33
34 #endif
35
36 #include <cerrno>
37 #include "socketengine.h"
38
39 /* Contains irc-specific definitions */
40 namespace irc
41 {
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.
45          */
46         namespace sockets
47         {
48
49                 typedef union {
50                         struct sockaddr sa;
51                         struct sockaddr_in in4;
52                         struct sockaddr_in6 in6;
53                 } sockaddrs;
54
55                 /** Match raw binary data using CIDR rules.
56                  *
57                  * This function will use binary comparison to compare the
58                  * two bit sequences, address and mask, up to mask_bits
59                  * bits in size. If they match, it will return true.
60                  * @param address The whole address, of 4 or 16 bytes in length
61                  * @param mask The mask, from 1 to 16 bytes in length, anything
62                  * from 1 to 128 bits of which is significant
63                  * @param mask_Bits How many bits of the mask parameter are significant
64                  * for this comparison.
65                  * @returns True if the first mask_bits of address matches the first
66                  * mask_bits of mask.
67                  */
68                 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
69
70                 /** Match CIDR, without matching username/nickname parts.
71                  *
72                  * This function will compare a human-readable address against a human-
73                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
74                  * method supports both IPV4 and IPV6 addresses.
75                  * @param address The human readable address, e.g. 1.2.3.4
76                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
77                  * @return True if the mask matches the address
78                  */
79                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
80
81                 /** Match CIDR, including an optional username/nickname part.
82                  *
83                  * This function will compare a human-readable address (plus
84                  * optional username and nickname) against a human-readable
85                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
86                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
87                  * IPV6 addresses.
88                  * @param address The human readable address, e.g. fred\@1.2.3.4
89                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
90                  * @return True if the mask matches the address
91                  */
92                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
93
94                 /** Create a new valid file descriptor using socket()
95                  * @return On return this function will return a value >= 0 for success,
96                  * or a negative value upon failure (negative values are invalid file
97                  * descriptors)
98                  */
99                 CoreExport int OpenTCPSocket(const char* addr, int socktype = SOCK_STREAM);
100
101                 /** Return the size of the structure for syscall passing */
102                 CoreExport int sa_size(irc::sockets::sockaddrs& sa);
103
104                 /** Convert an address-port pair into a binary sockaddr
105                  * @param addr The IP address, IPv4 or IPv6
106                  * @param port The port, 0 for unspecified
107                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
108                  * @return true if the conversion was successful, false if not.
109                  */
110                 CoreExport bool aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa);
111                 /** Convert a binary sockaddr to an address-port pair
112                  * @param sa The structure to convert
113                  * @param addr the IP address
114                  * @param port the port
115                  * @return true if the conversion was successful, false if unknown address family
116                  */
117                 CoreExport bool satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port);
118                 /** Convert a binary sockaddr to a user-readable string.
119                  * This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
120                  * @param sa The structure to convert
121                  * @return The string; "<unknown>" if not a valid address
122                  */
123                 CoreExport std::string satouser(const irc::sockets::sockaddrs* sa);
124         }
125 }
126
127
128
129 /** This class handles incoming connections on client ports.
130  * It will create a new User for every valid connection
131  * and assign it a file descriptor.
132  */
133 class CoreExport ListenSocketBase : public EventHandler
134 {
135  protected:
136         /** The creator/owner of this object
137          */
138         InspIRCd* ServerInstance;
139         /** Socket description (shown in stats p) */
140         std::string desc;
141
142         /** Address socket is bound to */
143         std::string bind_addr;
144         /** Port socket is bound to */
145         int bind_port;
146
147         /** The client address if the most recently connected client.
148          * Should only be used when accepting a new client.
149          */
150         static irc::sockets::sockaddrs client;
151         /** The server address used by the most recently connected client.
152          * This may differ from the bind address by having a nonzero address,
153          * if the port is wildcard bound, or being IPv4 on a 6to4 IPv6 port.
154          * The address family will always match that of "client"
155          */
156         static irc::sockets::sockaddrs server;
157
158  public:
159         /** Create a new listening socket
160          */
161         ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr);
162         /** Handle an I/O event
163          */
164         void HandleEvent(EventType et, int errornum = 0);
165         /** Close the socket
166          */
167         ~ListenSocketBase();
168         /** Set descriptive text
169          */
170         void SetDescription(const std::string &description)
171         {
172                 desc = description;
173         }
174         /** Get description for socket
175          */
176         const std::string& GetDescription()
177         {
178                 return desc;
179         }
180         /** Get port number for socket
181          */
182         int GetPort() { return bind_port; }
183
184         /** Get IP address socket is bound to
185          */
186         const std::string &GetIP() { return bind_addr; }
187
188         /** Handles sockets internals crap of a connection, convenience wrapper really
189          */
190         void AcceptInternal();
191
192         /** Called when a new connection has successfully been accepted on this listener.
193          * @param fd The file descriptor of the new connection
194          */
195         virtual void OnAcceptReady(int fd) = 0;
196 };
197
198 class CoreExport ClientListenSocket : public ListenSocketBase
199 {
200         virtual void OnAcceptReady(int fd);
201  public:
202         ClientListenSocket(InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr) { }
203 };
204
205 #endif
206