InspIRCd  3.0
socket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2005-2007 Craig Edwards <[email protected]>
6  * Copyright (C) 2007 Dennis Friis <[email protected]>
7  * Copyright (C) 2007 Robin Burchell <[email protected]>
8  * Copyright (C) 2006 Oliver Lupton <[email protected]>
9  * Copyright (C) 2006 William Pitcock <[email protected]>
10  *
11  * This file is part of InspIRCd. InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 
25 #pragma once
26 
27 #ifndef _WIN32
28 
29 #include <arpa/inet.h>
30 #include <sys/time.h>
31 #include <sys/resource.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <netdb.h>
40 
41 #else
42 
43 #include "inspircd_win32wrapper.h"
44 
45 #endif
46 
47 #include <cerrno>
48 
49 /* Contains irc-specific definitions */
50 namespace irc
51 {
56  namespace sockets
57  {
58  union CoreExport sockaddrs
59  {
60  struct sockaddr sa;
61  struct sockaddr_in in4;
62  struct sockaddr_in6 in6;
63  struct sockaddr_un un;
65  int family() const;
67  socklen_t sa_size() const;
69  int port() const;
71  std::string addr() const;
73  std::string str() const;
74  bool operator==(const sockaddrs& other) const;
75  inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
76  };
77 
78  struct CoreExport cidr_mask
79  {
81  unsigned char type;
83  unsigned char length;
85  unsigned char bits[16];
86 
87  cidr_mask() {}
89  cidr_mask(const std::string& mask);
91  cidr_mask(const irc::sockets::sockaddrs& addr, unsigned char len);
93  bool operator==(const cidr_mask& other) const;
95  bool operator<(const cidr_mask& other) const;
97  bool match(const irc::sockets::sockaddrs& addr) const;
99  std::string str() const;
100  };
101 
114  CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
115 
122  CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
123 
129  CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
130 
135  CoreExport bool isunix(const std::string& file);
136  }
137 }
138 
140 typedef std::vector<std::pair<irc::sockets::sockaddrs, int> > FailedPortList;
141 
142 #include "socketengine.h"
147 class CoreExport ListenSocket : public EventHandler
148 {
149  public:
150  reference<ConfigTag> bind_tag;
151  const irc::sockets::sockaddrs bind_sa;
152 
153  class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
154  {
155  public:
156  IOHookProvRef()
157  : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
158  {
159  }
160  };
161 
162  typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList;
163 
167  IOHookProvList iohookprovs;
168 
171  ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
174  ~ListenSocket();
175 
178  void OnEventHandlerRead() CXX11_OVERRIDE;
179 
183  void ResetIOHookProvider();
184 };
irc::sockets::cidr_mask
Definition: socket.h:78
sockaddr_un
Definition: inspircd_win32wrapper.h:187
irc::sockets::aptosa
bool aptosa(const std::string &addr, int port, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:152
reference< ConfigTag >
dynamic_reference_nocheck
Definition: dynref.h:95
EventHandler
Definition: socketengine.h:151
irc::sockets::sockaddrs
Definition: socket.h:58
irc::sockets::cidr_mask::type
unsigned char type
Definition: socket.h:81
irc::sockets::isunix
bool isunix(const std::string &file)
Definition: socket.cpp:195
ListenSocket::iohookprovs
IOHookProvList iohookprovs
Definition: socket.h:167
irc::sockets::MatchCIDR
bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
Definition: cidr.cpp:29
ListenSocket::IOHookProvRef
Definition: socket.h:153
ConfigTag
Definition: configreader.h:37
irc::sockets::untosa
bool untosa(const std::string &path, irc::sockets::sockaddrs &sa)
Definition: socket.cpp:184
irc
Definition: hashcomp.h:68
irc::sockets::cidr_mask::length
unsigned char length
Definition: socket.h:83
ListenSocket
Definition: socket.h:147
EventHandler::OnEventHandlerRead
virtual void OnEventHandlerRead()=0