Anope IRC Services  Version 2.0
sockets.h
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #ifndef SOCKETS_H
14 #define SOCKETS_H
15 
16 #ifndef _WIN32
17 #include <netinet/in.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #endif
21 
22 #include "anope.h"
23 
24 #define NET_BUFSIZE 65535
25 
29 {
30  sockaddr sa;
31  sockaddr_in sa4;
32  sockaddr_in6 sa6;
33 
36  sockaddrs(const Anope::string &address = "");
37 
40  void clear();
41 
45  size_t size() const;
46 
50  int port() const;
51 
55  Anope::string addr() const;
56 
57  /* Is this address ipv6? */
58  bool ipv6() const;
59 
62  bool valid() const;
63 
67  bool operator==(const sockaddrs &other) const;
68  /* The same as above but not */
69  inline bool operator!=(const sockaddrs &other) const { return !(*this == other); }
70 
77  void pton(int type, const Anope::string &address, int pport = 0);
78 
84  void ntop(int type, const void *src);
85 };
86 
88 {
91  unsigned short cidr_len;
92  public:
93  cidr(const Anope::string &ip);
94  cidr(const Anope::string &ip, unsigned char len);
95  cidr(const sockaddrs &ip, unsigned char len);
96  Anope::string mask() const;
97  bool match(const sockaddrs &other);
98  bool valid() const;
99 
100  bool operator<(const cidr &other) const;
101  bool operator==(const cidr &other) const;
102  bool operator!=(const cidr &other) const;
103 
105  {
106  size_t operator()(const cidr &s) const;
107  };
108 };
109 
111 {
112  public:
116  SocketException(const Anope::string &message) : CoreException(message) { }
117 
121  virtual ~SocketException() throw() { }
122 };
123 
125 {
126  SF_DEAD = 1,
134 };
135 
137 {
138  public:
139  virtual ~SocketIO() { }
140 
147  virtual int Recv(Socket *s, char *buf, size_t sz);
148 
154  virtual int Send(Socket *s, const char *buf, size_t sz);
155  int Send(Socket *s, const Anope::string &buf);
156 
161  virtual ClientSocket *Accept(ListenSocket *s);
162 
167  virtual SocketFlag FinishAccept(ClientSocket *cs);
168 
174  virtual void Bind(Socket *s, const Anope::string &ip, int port = 0);
175 
181  virtual void Connect(ConnectionSocket *s, const Anope::string &target, int port);
182 
187  virtual SocketFlag FinishConnect(ConnectionSocket *s);
188 
191  virtual void Destroy() { }
192 };
193 
195 {
196  protected:
197  /* Socket FD */
198  int sock;
199  /* Is this an IPv6 socket? */
200  bool ipv6;
201 
202  public:
203  std::bitset<SF_SIZE> flags;
204 
205  /* Sockaddrs for bind() (if it's bound) */
207 
208  /* I/O functions used for this socket */
210 
213  Socket();
214 
220  Socket(int sock, bool ipv6 = false, int type = SOCK_STREAM);
221 
224  virtual ~Socket();
225 
229  int GetFD() const;
230 
234  bool IsIPv6() const;
235 
240  bool SetBlocking(bool state);
241 
246  void Bind(const Anope::string &ip, int port = 0);
247 
251  virtual bool Process();
252 
256  virtual bool ProcessRead();
257 
261  virtual bool ProcessWrite();
262 
266  virtual void ProcessError();
267 };
268 
269 class CoreExport BufferedSocket : public virtual Socket
270 {
271  protected:
272  /* Things read from the socket */
274  /* Things to be written to the socket */
276  /* How much data was received from this socket on this recv() */
277  int recv_len;
278 
279  public:
280  BufferedSocket();
281  virtual ~BufferedSocket();
282 
287 
291  bool ProcessWrite() anope_override;
292 
295  const Anope::string GetLine();
296 
300  protected:
301  virtual void Write(const char *buffer, size_t l);
302  public:
303  void Write(const char *message, ...);
304  void Write(const Anope::string &message);
305 
309  int ReadBufferLen() const;
310 
314  int WriteBufferLen() const;
315 };
316 
317 class CoreExport BinarySocket : public virtual Socket
318 {
319  protected:
320  struct DataBlock
321  {
322  char *orig;
323  char *buf;
324  size_t len;
325 
326  DataBlock(const char *b, size_t l);
327  ~DataBlock();
328  };
329 
330  /* Data to be written out */
331  std::deque<DataBlock *> write_buffer;
332 
333  public:
334  BinarySocket();
335  virtual ~BinarySocket();
336 
340  bool ProcessRead() anope_override;
341 
345  bool ProcessWrite() anope_override;
346 
351  virtual void Write(const char *buffer, size_t l);
352  void Write(const char *message, ...);
353  void Write(const Anope::string &message);
354 
360  virtual bool Read(const char *buffer, size_t l);
361 };
362 
363 class CoreExport ListenSocket : public virtual Socket
364 {
365  public:
371  ListenSocket(const Anope::string &bindip, int port, bool ipv6);
372  virtual ~ListenSocket();
373 
377  bool ProcessRead();
378 
384  virtual ClientSocket *OnAccept(int fd, const sockaddrs &addr) = 0;
385 };
386 
387 class CoreExport ConnectionSocket : public virtual Socket
388 {
389  public:
390  /* Sockaddrs for connection ip/port */
392 
397  void Connect(const Anope::string &TargetHost, int Port);
398 
403  bool Process() anope_override;
404 
408  void ProcessError() anope_override;
409 
412  virtual void OnConnect();
413 
417  virtual void OnError(const Anope::string &error);
418 };
419 
420 class CoreExport ClientSocket : public virtual Socket
421 {
422  public:
423  /* Listen socket this connection came from */
425  /* Clients address */
427 
432  ClientSocket(ListenSocket *ls, const sockaddrs &addr);
433 
438  bool Process() anope_override;
439 
443  void ProcessError() anope_override;
444 
447  virtual void OnAccept();
448 
451  virtual void OnError(const Anope::string &error);
452 };
453 
454 class CoreExport Pipe : public Socket
455 {
456  public:
461 
462  Pipe();
463  ~Pipe();
464 
467  bool ProcessRead() anope_override;
468 
473  void Write(const char *data, size_t sz);
474  inline void Write(const Anope::string &data) { this->Write(data.c_str(), data.length() + 1); }
475 
481  int Read(char *data, size_t sz);
482 
487  bool SetWriteBlocking(bool state);
488 
492  void Notify();
493 
496  virtual void OnNotify() = 0;
497 };
498 
499 extern CoreExport uint32_t TotalRead;
500 extern CoreExport uint32_t TotalWritten;
502 
503 #endif // SOCKET_H
bool operator!=(const ci::string &leftval, const std::string &rightval)
Definition: hashcomp.h:188
std::bitset< SF_SIZE > flags
Definition: sockets.h:203
SocketIO * io
Definition: sockets.h:209
SocketFlag
Definition: sockets.h:124
sockaddr sa
Definition: sockets.h:30
CoreExport SocketIO NormalSocketIO
Definition: sockets.cpp:29
sockaddrs addr
Definition: sockets.h:89
virtual ~SocketException()
Definition: sockets.h:121
sockaddr_in sa4
Definition: sockets.h:31
virtual bool Process()
Definition: sockets.cpp:498
bool operator!=(const sockaddrs &other) const
Definition: sockets.h:69
virtual bool ProcessRead()
Definition: sockets.cpp:503
Anope::string write_buffer
Definition: sockets.h:275
virtual ~SocketIO()
Definition: sockets.h:139
int sock
Definition: sockets.h:198
Definition: sockets.h:87
sockaddrs clientaddr
Definition: sockets.h:426
Definition: sockets.h:454
sockaddrs conaddr
Definition: sockets.h:391
ListenSocket * ls
Definition: sockets.h:424
std::deque< DataBlock * > write_buffer
Definition: sockets.h:331
bool operator==(const std::string &leftval, const ci::string &rightval)
Definition: hashcomp.h:171
Anope::string cidr_ip
Definition: sockets.h:90
CoreExport bool Send(User *from, NickCore *to, BotInfo *service, const Anope::string &subject, const Anope::string &message)
Definition: mail.cpp:54
#define anope_override
Definition: services.h:56
#define CoreExport
Definition: services.h:62
CoreExport uint32_t TotalWritten
Definition: sockets.cpp:27
SocketException(const Anope::string &message)
Definition: sockets.h:116
int write_pipe
Definition: sockets.h:460
bool ipv6
Definition: sockets.h:200
CoreExport uint32_t TotalRead
Definition: sockets.cpp:26
virtual void Destroy()
Definition: sockets.h:191
void Write(const Anope::string &data)
Definition: sockets.h:474
unsigned short cidr_len
Definition: sockets.h:91
void Process(const Anope::string &)
Definition: process.cpp:19
Anope::string read_buffer
Definition: sockets.h:273
Definition: anope.h:20
sockaddrs bindaddr
Definition: sockets.h:206
sockaddr_in6 sa6
Definition: sockets.h:32