InspIRCd  4.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 Pippijn van Steenhoven <[email protected]>
11  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
12  * Copyright (C) 2007 Dennis Friis <[email protected]>
13  * Copyright (C) 2006, 2008, 2010 Craig Edwards <[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() = default;
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 {
147  int error;
148 
151 
153  std::shared_ptr<ConfigTag> tag;
154 
155  FailedPort(int err, irc::sockets::sockaddrs& ep, std::shared_ptr<ConfigTag> cfg)
156  : error(err)
157  , sa(ep)
158  , tag(cfg)
159  {
160  }
161 };
162 
164 typedef std::vector<FailedPort> FailedPortList;
165 
166 #include "socketengine.h"
167 
172 class CoreExport ListenSocket : public EventHandler
173 {
174  public:
175  std::shared_ptr<ConfigTag> bind_tag;
176  const irc::sockets::sockaddrs bind_sa;
177 
178  class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
179  {
180  public:
181  IOHookProvRef()
182  : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
183  {
184  }
185  };
186 
187  typedef std::array<IOHookProvRef, 2> IOHookProvList;
188 
192  IOHookProvList iohookprovs;
193 
196  ListenSocket(std::shared_ptr<ConfigTag> tag, const irc::sockets::sockaddrs& bind_to);
199  ~ListenSocket() override;
200 
203  void OnEventHandlerRead() override;
204 
208  void ResetIOHookProvider();
209 };
Definition: socketengine.h:154
Definition: socket.h:179
Definition: socket.h:173
IOHookProvList iohookprovs
Definition: socket.h:192
Definition: dynref.h:98
bool untosa(const std::string &path, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:187
bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
Definition: cidr.cpp:33
bool aptosa(const std::string &addr, int port, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:155
bool isunix(const std::string &file)
Definition: socket.cpp:198
Definition: hashcomp.h:64
Definition: socket.h:145
std::shared_ptr< ConfigTag > tag
Definition: socket.h:153
irc::sockets::sockaddrs sa
Definition: socket.h:150
int error
Definition: socket.h:147
Definition: socket.h:83
unsigned char length
Definition: socket.h:87
unsigned char type
Definition: socket.h:85
Definition: inspircd_win32wrapper.h:195
Definition: socket.h:63