InspIRCd  3.0
socket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013, 2015-2016 Attila Molnar <[email protected]>
5  * Copyright (C) 2012-2013, 2017-2019 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2012 ChrisTX <[email protected]>
8  * Copyright (C) 2012 Adam <[email protected]>
9  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
10  * Copyright (C) 2008, 2010 Craig Edwards <[email protected]>
11  * Copyright (C) 2008 Pippijn van Steenhoven <[email protected]>
12  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
13  * Copyright (C) 2007 Dennis Friis <[email protected]>
14  *
15  * This file is part of InspIRCd. InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 
29 #pragma once
30 
31 #ifndef _WIN32
32 
33 #include <arpa/inet.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/un.h>
40 #include <netinet/in.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <netdb.h>
44 
45 #else
46 
47 #include "inspircd_win32wrapper.h"
48 
49 #endif
50 
51 #include <cerrno>
52 
53 /* Contains irc-specific definitions */
54 namespace irc
55 {
60  namespace sockets
61  {
62  union CoreExport sockaddrs
63  {
64  struct sockaddr sa;
65  struct sockaddr_in in4;
66  struct sockaddr_in6 in6;
67  struct sockaddr_un un;
69  int family() const;
71  socklen_t sa_size() const;
73  int port() const;
75  std::string addr() const;
77  std::string str() const;
78  bool operator==(const sockaddrs& other) const;
79  inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
80  };
81 
82  struct CoreExport cidr_mask
83  {
85  unsigned char type;
87  unsigned char length;
89  unsigned char bits[16];
90 
91  cidr_mask() {}
93  cidr_mask(const std::string& mask);
95  cidr_mask(const irc::sockets::sockaddrs& addr, unsigned char len);
97  bool operator==(const cidr_mask& other) const;
99  bool operator<(const cidr_mask& other) const;
101  bool match(const irc::sockets::sockaddrs& addr) const;
103  std::string str() const;
104  };
105 
118  CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
119 
126  CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
127 
133  CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
134 
139  CoreExport bool isunix(const std::string& file);
140  }
141 }
142 
144 struct CoreExport FailedPort
145 {
146 
148  int error;
149 
152 
155 
156  FailedPort(int err, irc::sockets::sockaddrs& ep, ConfigTag* cfg)
157  : error(err)
158  , sa(ep)
159  , tag(cfg)
160  {
161  }
162 };
163 
165 typedef std::vector<FailedPort> FailedPortList;
166 
167 #include "socketengine.h"
168 
173 class CoreExport ListenSocket : public EventHandler
174 {
175  public:
176  reference<ConfigTag> bind_tag;
177  const irc::sockets::sockaddrs bind_sa;
178 
179  class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
180  {
181  public:
182  IOHookProvRef()
183  : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
184  {
185  }
186  };
187 
188  typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList;
189 
193  IOHookProvList iohookprovs;
194 
197  ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
200  ~ListenSocket();
201 
204  void OnEventHandlerRead() CXX11_OVERRIDE;
205 
209  void ResetIOHookProvider();
210 };
irc::sockets::cidr_mask
Definition: socket.h:82
sockaddr_un
Definition: inspircd_win32wrapper.h:183
irc::sockets::aptosa
bool aptosa(const std::string &addr, int port, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:158
reference< ConfigTag >
FailedPort::error
int error
Definition: socket.h:148
dynamic_reference_nocheck
Definition: dynref.h:95
EventHandler
Definition: socketengine.h:157
irc::sockets::sockaddrs
Definition: socket.h:62
irc::sockets::cidr_mask::type
unsigned char type
Definition: socket.h:85
FailedPort::tag
ConfigTag * tag
Definition: socket.h:154
irc::sockets::isunix
bool isunix(const std::string &file)
Definition: socket.cpp:201
FailedPort::sa
irc::sockets::sockaddrs sa
Definition: socket.h:151
FailedPort
Definition: socket.h:144
ListenSocket::iohookprovs
IOHookProvList iohookprovs
Definition: socket.h:193
irc::sockets::MatchCIDR
bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
Definition: cidr.cpp:33
ListenSocket::IOHookProvRef
Definition: socket.h:179
ConfigTag
Definition: configreader.h:43
irc::sockets::untosa
bool untosa(const std::string &path, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:190
irc
Definition: hashcomp.h:75
irc::sockets::cidr_mask::length
unsigned char length
Definition: socket.h:87
ListenSocket
Definition: socket.h:173
EventHandler::OnEventHandlerRead
virtual void OnEventHandlerRead()=0