diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/config.h | 6 | ||||
| -rw-r--r-- | include/configreader.h | 8 | ||||
| -rw-r--r-- | include/extern.h | 28 | ||||
| -rw-r--r-- | include/modules.h | 10 | ||||
| -rw-r--r-- | include/services.h | 155 | ||||
| -rw-r--r-- | include/sockets.h | 191 |
6 files changed, 255 insertions, 143 deletions
diff --git a/include/config.h b/include/config.h index a43b17f3e..1aaf5a47c 100644 --- a/include/config.h +++ b/include/config.h @@ -25,9 +25,6 @@ /* Name of log file (in Services directory) */ #define LOG_FILENAME "services.log" -/* Maximum amount of data from/to the network to buffer (bytes). */ -#define NET_BUFSIZE 65536 - /******************* END OF USER-CONFIGURABLE SECTION ********************/ /* Size of input buffer (note: this is different from BUFSIZ) @@ -35,6 +32,9 @@ * things will happen. */ #define BUFSIZE 1024 +/* Maximum amount of data from/to the network to buffer (bytes). */ +#define NET_BUFSIZE 65536 + /**************************************************************************/ #endif /* CONFIG_H */ diff --git a/include/configreader.h b/include/configreader.h index a461c1f15..c6dbbefb8 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -384,8 +384,6 @@ class ServerConfig /* Host to connect to **/ char *LocalHost; - /* Port */ - unsigned LocalPort; /* List of uplink servers to try and connect to */ std::list<Uplink *> Uplinks; @@ -475,7 +473,7 @@ class ServerConfig bool ForceForbidReason; /* Services should use privmsgs instead of notices */ bool UsePrivmsg; - /* Services only respond to full PRIVMSG client@services.server.name messaegs */ + /* Services only respond to full PRIVMSG client@services.server.name messages */ bool UseStrictPrivMsg; /* Dump a core file if we crash */ bool DumpCore; @@ -490,6 +488,10 @@ class ServerConfig std::string MLock; /* Default botmodes on channels, defaults to ao */ std::string BotModes; + /* How many times to try and reconnect to the uplink before giving up */ + unsigned MaxRetries; + /* How long to wait between connection attempts */ + int RetryWait; /* Services can use email */ bool UseMail; diff --git a/include/extern.h b/include/extern.h index ccff00e58..983f61af5 100644 --- a/include/extern.h +++ b/include/extern.h @@ -244,14 +244,14 @@ E int protocoldebug; E int is44; E int quitting; -E int delayed_quit; +E int shutting_down; E const char *quitmsg; -E char inbuf[BUFSIZE]; -E int servsock; E int save_data; E int got_alarm; E time_t start_time; +E Socket *UplinkSock; + E void save_databases(); E void expire_all(); E void sighandler(int signum); @@ -455,7 +455,7 @@ E int delete_ignore(const char *nick); E int clear_ignores(); E int split_buf(char *buf, const char ***argv, int colon_special); -E void process(); +E void process(const std::string &buf); /**** send.c ****/ @@ -485,7 +485,7 @@ E void CapabParse(int ac, const char **av); E int is_ulined(const char *server); E int is_sync(Server *server); -E Server *new_server(Server * uplink, const char *name, const char *desc, ServerFlag flag, const char *suid); +E Server *new_server(Server * uplink, const char *name, const char *desc, ServerFlag flag, const std::string &suid); E Server *findserver(Server *s, const char *name); @@ -538,20 +538,10 @@ E void slist_pack(SList *slist); E int slist_remove(SList *slist, void *item); E int slist_setcapacity(SList *slist, int16 capacity); -/**** sockutil.c ****/ - -E int32 total_read, total_written; -E int32 read_buffer_len(); -E int32 write_buffer_len(); - -E int sgetc(ano_socket_t s); -E char *sgets(char *buf, int len, ano_socket_t s); -E char *sgets2(char *buf, int len, ano_socket_t s); -E int sread(ano_socket_t s, char *buf, int len); -E int sputs(char *str, ano_socket_t s); -E int sockprintf(ano_socket_t s, const char *fmt, ...); -E int conn(const char *host, int port, const char *lhost, int lport); -E void disconn(ano_socket_t s); +/**** sockets.cpp ****/ +E SocketEngine socketEngine; +E int32 TotalRead; +E int32 TotalWritten; /**** users.c ****/ diff --git a/include/modules.h b/include/modules.h index bf178d72d..88a605acd 100644 --- a/include/modules.h +++ b/include/modules.h @@ -671,10 +671,14 @@ class CoreExport Module */ virtual void OnPreServerConnect() { } - /** Called when anope connects to its uplink + /** Called when Anope connects to its uplink */ virtual void OnServerConnect() { } + /** Called when Anope disconnects from its uplink, before it tries to reconnect + */ + virtual void OnServerDisconnect() { } + /** Called before the database expire routines are called * Note: Code that is in seperate expiry routines should just be done * when we save the DB, theres no need to have both @@ -1202,8 +1206,8 @@ enum Implementation I_OnModuleLoad, I_OnModuleUnload, /* Other */ - I_OnReload, I_OnPreServerConnect, I_OnNewServer, I_OnServerConnect, I_OnPreCommandRun, I_OnPreCommand, I_OnPostCommand, - I_OnPreDatabaseExpire, I_OnPreRestart, I_OnRestart, I_OnPreShutdown, I_OnShutdown, I_OnSignal, + I_OnReload, I_OnPreServerConnect, I_OnNewServer, I_OnServerConnect, I_OnServerDisconnect, I_OnPreCommandRun, I_OnPreCommand, + I_OnPostCommand, I_OnPreDatabaseExpire, I_OnPreRestart, I_OnRestart, I_OnPreShutdown, I_OnShutdown, I_OnSignal, I_OnServerQuit, I_OnTopicUpdated, I_OnEncrypt, I_OnEncryptInPlace, I_OnEncryptCheckLen, I_OnDecrypt, I_OnCheckPassword, I_OnChannelModeSet, I_OnChannelModeUnset, I_OnUserModeSet, I_OnUserModeUnset, I_OnChannelModeAdd, I_OnUserModeAdd, diff --git a/include/services.h b/include/services.h index 7e6783404..ea6b89364 100644 --- a/include/services.h +++ b/include/services.h @@ -19,28 +19,23 @@ #include "sysconf.h" #include "config.h" -#ifndef MAX_CMD_HASH -#define MAX_CMD_HASH 1024 -#endif - /* Some SUN fixs */ #ifdef __sun -/* Solaris specific code, types that do not exist in Solaris' - * sys/types.h - **/ -#undef u_int8_t -#undef u_int16_t -#undef u_int32_t -#undef u_int_64_t -#define u_int8_t uint8_t -#define u_int16_t uint16_t -#define u_int32_t uint32_t -#define u_int64_t uint64_t - -#ifndef INADDR_NONE -#define INADDR_NONE (-1) -#endif - + /* Solaris specific code, types that do not exist in Solaris' + * sys/types.h + **/ +# undef u_int8_t +# undef u_int16_t +# undef u_int32_t +# undef u_int_64_t +# define u_int8_t uint8_t +# define u_int16_t uint16_t +# define u_int32_t uint32_t +# define u_int64_t uint64_t + +# ifndef INADDR_NONE +# define INADDR_NONE (-1) +# endif #endif @@ -49,63 +44,57 @@ #include <stdlib.h> #include <string.h> -/* Windows does not have: unistd.h, grp.h, netdb.h, netinet/in.h, sys/socket.h, sys/time.h - * Windows requires: winsock.h - * -- codemastr - */ - -#ifndef _WIN32 -#include <unistd.h> -#endif - #include <signal.h> #include <time.h> #include <errno.h> - -#ifndef _WIN32 -#include <grp.h> -#endif - #include <limits.h> -#ifndef _WIN32 -#include <netdb.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <sys/socket.h> -#else -#include <winsock.h> -#include <windows.h> -#endif - -#include <sys/stat.h> /* for umask() on some systems */ +#include <sys/stat.h> /* for umask() on some systems */ #include <sys/types.h> -#ifdef HAVE_GETTIMEOFDAY -#include <sys/time.h> -#endif - -#ifdef _WIN32 -#include <sys/timeb.h> -#include <direct.h> -#include <io.h> -#endif - #include <fcntl.h> #ifndef _WIN32 -#ifdef HAVE_BACKTRACE -#include <execinfo.h> -#endif -#endif - -#ifndef _WIN32 -#include <dirent.h> +# include <unistd.h> +# include <grp.h> +# include <netdb.h> +# include <netinet/in.h> +# include <arpa/inet.h> +# include <sys/socket.h> +# include <dirent.h> +# ifdef HAVE_BACKTRACE +# include <execinfo.h> +# endif +# define DllExport +# define CoreExport +# define MARK_DEPRECATED __attribute((deprecated)) +#else +# include <winsock2.h> +# include <ws2tcpip.h> +# include <windows.h> +# include <sys/timeb.h> +# include <direct.h> +# include <io.h> +# ifdef MODULE_COMPILE +# define CoreExport __declspec(dllimport) +# define DllExport __declspec(dllexport) +# else +# define CoreExport __declspec(dllexport) +# define DllExport __declspec(dllimport) +# endif +/* VS2008 hates having this define before its own */ +# define vsnprintf _vsnprintf +/* We have our own inet_pton and inet_ntop (Windows doesn't have its own) */ +# define inet_pton inet_pton_ +# define inet_ntop inet_ntop_ +# define MARK_DEPRECATED + +extern CoreExport int inet_pton(int af, const char *src, void *dst); +extern CoreExport const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif -#ifdef _WIN32 -/* VS2008 hates having this define before its own */ -#define vsnprintf _vsnprintf +#ifdef HAVE_GETTIMEOFDAY +# include <sys/time.h> #endif #if HAVE_STRINGS_H @@ -116,8 +105,6 @@ # include <sys/select.h> #endif -#include "sockets.h" - #ifndef va_copy # ifdef __va_copy # define VA_COPY(DEST,SRC) __va_copy((DEST),(SRC)) @@ -168,25 +155,6 @@ extern int strncasecmp(const char *, const char *, size_t); # undef int32 #endif -#ifndef _WIN32 - #define MARK_DEPRECATED __attribute((deprecated)) -#else - #define MARK_DEPRECATED -#endif - -#ifdef _WIN32 -# ifdef MODULE_COMPILE -# define CoreExport __declspec(dllimport) -# define DllExport __declspec(dllexport) -# else -# define CoreExport __declspec(dllexport) -# define DllExport __declspec(dllimport) -# endif -#else -# define DllExport -# define CoreExport -#endif - /** This definition is used as shorthand for the various classes * and functions needed to make a module loadable by the OS. * It defines the class factory and external init_module function. @@ -230,7 +198,7 @@ extern int strncasecmp(const char *, const char *, size_t); #include "defs.h" #include "slist.h" -/* pull in the various bits of STL to pull in */ +/* Pull in the various bits of STL */ #include <iostream> #include <string> #include <map> @@ -239,6 +207,7 @@ extern int strncasecmp(const char *, const char *, size_t); #include <vector> #include <deque> #include <bitset> +#include <set> /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception. * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or @@ -302,6 +271,13 @@ class ModuleException : public CoreException virtual ~ModuleException() throw() {} }; + +/*************************************************************************/ + +#include "sockets.h" + +/*************************************************************************/ + /** Class with the ability to keep flags on items, they should extend from this * where T is an enum. */ @@ -1107,11 +1083,14 @@ struct Uplink { char *host; unsigned port; char *password; - Uplink(const char *_host, int _port, const char *_password) + bool ipv6; + + Uplink(const char *_host, int _port, const char *_password, bool _ipv6) { host = sstrdup(_host); port = _port; password = sstrdup(_password); + ipv6 = _ipv6; } ~Uplink() { diff --git a/include/sockets.h b/include/sockets.h index c722fbb1e..3277c1b9f 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -15,34 +15,171 @@ #define SOCKETS_H #ifdef _WIN32 -typedef SOCKET ano_socket_t; -#define ano_sockread(fd, buf, len) recv(fd, buf, len, 0) -#define ano_sockwrite(fd, buf, len) send(fd, buf, len, 0) -#define ano_sockclose(fd) closesocket(fd) -#define ano_sockgeterr() WSAGetLastError() -#define ano_sockseterr(err) WSASetLastError(err) -/* ano_sockstrerror in sockutil.c */ -extern char *ano_sockstrerror(int); -/* ano_socksetnonb in sockutil.c */ -#define ano_sockerrnonb(err) (err == WSAEINPROGRESS || err == WSAEWOULDBLOCK) -#define SOCKERR_EBADF WSAENOTSOCK -#define SOCKERR_EINTR WSAEINTR -#define SOCKERR_EINVAL WSAEINVAL -#define SOCKERR_EINPROGRESS WSAEINPROGRESS +#define CloseSocket closesocket #else -typedef int ano_socket_t; -#define ano_sockread(fd, buf, len) read(fd, buf, len) -#define ano_sockwrite(fd, buf, len) write(fd, buf, len) -#define ano_sockclose(fd) close(fd) -#define ano_sockgeterr() errno -#define ano_sockseterr(err) errno = err -#define ano_sockstrerror(err) strerror(err) -#define ano_socksetnonb(fd) fcntl(fd, F_SETFL, O_NONBLOCK) -#define ano_sockerrnonb(err) (err == EINPROGRESS) -#define SOCKERR_EBADF EBADF -#define SOCKERR_EINTR EINTR -#define SOCKERR_EINVAL EINVAL -#define SOCKERR_EINPROGRESS EINPROGRESS +#define CloseSocket close #endif +class SocketException : public CoreException +{ + public: + /** Default constructor for socket exceptions + * @param message Error message + */ + SocketException(const std::string &message) : CoreException(message) { } + + /** Default destructor + * @throws Nothing + */ + virtual ~SocketException() throw() { } +}; + +class CoreExport Socket +{ + private: + /** Read from the socket + * @param buf Buffer to read to + * @param sz How much to read + * @return Number of bytes recieved + */ + virtual int RecvInternal(char *buf, size_t sz) const; + + /** Write to the socket + * @param buf What to write + * @return Number of bytes sent, -1 on error + */ + virtual int SendInternal(const std::string &buf) const; + + protected: + /* Socket FD */ + int Sock; + /* Host this socket is connected to */ + std::string TargetHost; + /* Port we're connected to */ + int Port; + /* IP this socket is bound to */ + std::string BindHost; + /* Is this an IPv6 socket? */ + bool IPv6; + + /* Messages to be written to the socket */ + std::string WriteBuffer; + /* Part of a message not totally yet recieved */ + std::string extrabuf; + /* How much data was recieved from the socket */ + size_t RecvLen; + + public: + /** Default constructor + * @param nTargetHost Hostname to connect to + * @param nPort Port to connect to + * @param nBindHos Host to bind to when connecting + * @param nIPv6 true to use IPv6 + */ + Socket(const std::string &nTargetHost, int nPort, const std::string &nBindHost = "", bool nIPv6 = false); + + /** Default destructor + */ + virtual ~Socket(); + + /** Get the socket FD for this socket + * @return The fd + */ + virtual int GetSock() const; + + /** Check if this socket is IPv6 + * @return true or false + */ + bool IsIPv6() const; + + /** Called when there is something to be read from thie socket + * @return true on success, false to kill this socket + */ + virtual bool ProcessRead(); + + /** Called when this socket becomes writeable + * @return true on success, false to drop this socket + */ + virtual bool ProcessWrite(); + + /** Called when there is an error on this socket + */ + virtual void ProcessError(); + + /** Called with a message recieved from the socket + * @param buf The message + * @return true on success, false to kill this socket + */ + virtual bool Read(const std::string &buf); + + /** Write to the socket + * @param message The message to write + */ + void Write(const char *message, ...); + void Write(std::string &message); + + /** Get the length of the read buffer + * @return The length of the read buffer + */ + size_t ReadBufferLen() const; + + /** Get the length of the write buffer + * @return The length of the write buffer + */ + size_t WriteBufferLen() const; +}; + +class CoreExport SocketEngine +{ + private: + /* List of sockets that need to be deleted */ + std::set<Socket *> OldSockets; + /* FDs to read */ + fd_set ReadFDs; + /* FDs that want writing */ + fd_set WriteFDs; + /* Max FD */ + int MaxFD; + + /** Unmark a socket as writeable + * @param s The socket + */ + void ClearWriteable(Socket *s); + public: + /* Set of sockets */ + std::set<Socket *> Sockets; + + /** Constructor + */ + SocketEngine(); + + /** Destructor + */ + virtual ~SocketEngine(); + + /** Add a socket to the socket engine + * @param s The socket + */ + void AddSocket(Socket *s); + + /** Delete a socket from the socket engine + * @param s The socket + */ + void DelSocket(Socket *s); + + /** Mark a socket as wanting to be written to + * @param s The socket + */ + void MarkWriteable(Socket *s); + + /** Called to iterate through each socket and check for activity + */ + void Process(); + + /** Get the last socket error + * @return The error + */ + const std::string GetError() const; +}; + #endif |
