X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocketengine.cpp;h=ce7b1c9deeb59603db68df075ad5fc58683031c7;hb=3593e33ce374caee9aec0575b49ec9b0001a8ec6;hp=976f578fd542adb5e4d69c9252eae590996eb042;hpb=31dc0f8489dfbf63efbf0960711b3bbaa5543e30;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 976f578fd..ce7b1c9de 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -2,124 +2,74 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. - * E-mail: - * - * + * InspIRCd: (C) 2002-2007 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits * - * Written by Craig Edwards, Craig McLure, and others. * This program is free but copyrighted software; see - * the file COPYING for details. + * the file COPYING for details. * * --------------------------------------------------- */ -#include "inspircd_config.h" -#include "globals.h" +/* $Core: libIRCDsocketengine */ + +/********* DEFAULTS **********/ +/* $ExtraSources: socketengines/socketengine_select.cpp */ +/* $ExtraObjects: socketengine_select.o */ + +/* $If: USE_KQUEUE */ +/* $ExtraSources: socketengines/socketengine_kqueue.cpp */ +/* $ExtraObjects: socketengine_kqueue.o */ +/* $EndIf */ + +/* $If: USE_EPOLL */ +/* $ExtraSources: socketengines/socketengine_epoll.cpp */ +/* $ExtraObjects: socketengine_epoll.o */ +/* $EndIf */ + +/* $If: USE_PORTS */ +/* $ExtraSources: socketengines/socketengine_ports.cpp */ +/* $ExtraObjects: socketengine_ports.o */ +/* $EndIf */ + #include "inspircd.h" -#ifdef USE_EPOLL -#include -#endif -#ifdef USE_KQUEUE -#include -#include -#include -#endif -#include -#include #include "socketengine.h" -char ref[MAX_DESCRIPTORS]; +int EventHandler::GetFd() +{ + return this->fd; +} -SocketEngine::SocketEngine() +void EventHandler::SetFd(int FD) { - log(DEBUG,"SocketEngine::SocketEngine()"); -#ifdef USE_EPOLL - EngineHandle = epoll_create(MAX_DESCRIPTORS); -#endif -#ifdef USE_KQUEUE - EngineHandle = kqueue(); -#endif -#ifdef USE_SELECT - EngineHandle = 0; -#endif - if (EngineHandle == -1) - { - log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); - log(SPARSE,"ERROR: this is a fatal error, exiting now."); - printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); - printf("ERROR: this is a fatal error, exiting now."); - Exit(0); - } - CurrentSetSize = 0; + this->fd = FD; } -SocketEngine::~SocketEngine() +bool EventHandler::Readable() { - log(DEBUG,"SocketEngine::~SocketEngine()"); -#ifdef USE_EPOLL - close(EngineHandle); -#endif -#ifdef USE_KQUEUE - close(EngineHandle); -#endif + return true; } -char SocketEngine::GetType(int fd) +bool EventHandler::Writeable() { - if ((fd < 0) || (fd > MAX_DESCRIPTORS)) - return X_EMPTY_SLOT; - /* Mask off the top bit used for 'read/write' state */ - return (ref[fd] & ~0x80); + return false; } -bool SocketEngine::AddFd(int fd, bool readable, char type) +void SocketEngine::WantWrite(EventHandler* eh) +{ +} + +SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance) +{ + memset(ref, 0, sizeof(ref)); +} + +SocketEngine::~SocketEngine() +{ +} + +bool SocketEngine::AddFd(EventHandler* eh) { - if ((fd < 0) || (fd > MAX_DESCRIPTORS)) - { - log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS); - return false; - } - if (GetRemainingFds() <= 1) - { - log(DEFAULT,"ERROR: System out of file descriptors!"); - return false; - } -#ifdef USE_SELECT - fds[fd] = fd; -#endif - ref[fd] = type; - if (readable) - { - log(DEBUG,"Set readbit"); - ref[fd] |= X_READBIT; - } - log(DEBUG,"Add socket %d",fd); -#ifdef USE_EPOLL - struct epoll_event ev; - memset(&ev,0,sizeof(struct epoll_event)); - log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd); - readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT; - ev.data.fd = fd; - int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev); - if (i < 0) - { - log(DEBUG,"epoll: List insertion failure!"); - return false; - } -#endif -#ifdef USE_KQUEUE - struct kevent ke; - log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd); - EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL); - int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL); - if (i == -1) - { - log(DEBUG,"kqueue: List insertion failure!"); - return false; - } -#endif - CurrentSetSize++; return true; } @@ -127,134 +77,142 @@ bool SocketEngine::HasFd(int fd) { if ((fd < 0) || (fd > MAX_DESCRIPTORS)) return false; - return (ref[fd] != 0); + return ref[fd]; } -bool SocketEngine::DelFd(int fd) +EventHandler* SocketEngine::GetRef(int fd) { - log(DEBUG,"SocketEngine::DelFd(%d)",fd); - if ((fd < 0) || (fd > MAX_DESCRIPTORS)) - return false; + return 0; + return ref[fd]; +} -#ifdef USE_SELECT - std::map::iterator t = fds.find(fd); - if (t != fds.end()) - { - fds.erase(t); - log(DEBUG,"Deleted fd %d",fd); - } -#endif -#ifdef USE_KQUEUE - struct kevent ke; - EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL); - if (i == -1) - { - log(DEBUG,"kqueue: Failed to remove socket from queue!"); - return false; - } -#endif -#ifdef USE_EPOLL - struct epoll_event ev; - memset(&ev,0,sizeof(struct epoll_event)); - ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT; - ev.data.fd = fd; - int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev); - if (i < 0) - { - log(DEBUG,"epoll: List deletion failure!"); - return false; - } -#endif - CurrentSetSize--; - ref[fd] = 0; +bool SocketEngine::DelFd(EventHandler* eh, bool force) +{ return true; } int SocketEngine::GetMaxFds() { -#ifdef USE_SELECT - return FD_SETSIZE; -#endif -#ifdef USE_KQUEUE - return MAX_DESCRIPTORS; -#endif -#ifdef USE_EPOLL - return MAX_DESCRIPTORS; -#endif + return 0; } int SocketEngine::GetRemainingFds() { -#ifdef USE_SELECT - return FD_SETSIZE - CurrentSetSize; -#endif -#ifdef USE_KQUEUE - return MAX_DESCRIPTORS - CurrentSetSize; -#endif -#ifdef USE_EPOLL - return MAX_DESCRIPTORS - CurrentSetSize; -#endif + return 0; } -int SocketEngine::Wait(int* fdlist) +int SocketEngine::DispatchEvents() { - int result = 0; -#ifdef USE_SELECT - FD_ZERO(&wfdset); - FD_ZERO(&rfdset); - timeval tval; - int sresult; - for (std::map::iterator a = fds.begin(); a != fds.end(); a++) - { - if (ref[a->second] & X_READBIT) - { - FD_SET (a->second, &rfdset); - } - else - { - FD_SET (a->second, &wfdset); - } - - } - tval.tv_sec = 0; - tval.tv_usec = 50L; - sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval); - if (sresult > 0) - { - for (std::map::iterator a = fds.begin(); a != fds.end(); a++) - { - if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset))) - fdlist[result++] = a->second; - } - } -#endif -#ifdef USE_KQUEUE - ts.tv_nsec = 5000L; - ts.tv_sec = 0; - int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts); - for (int j = 0; j < i; j++) - fdlist[result++] = ke_list[j].ident; -#endif -#ifdef USE_EPOLL - int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50); - for (int j = 0; j < i; j++) - fdlist[result++] = events[j].data.fd; -#endif - return result; + return 0; } std::string SocketEngine::GetName() { -#ifdef USE_SELECT - return "select"; -#endif -#ifdef USE_KQUEUE - return "kqueue"; -#endif -#ifdef USE_EPOLL - return "epoll"; -#endif return "misconfigured"; } + +bool SocketEngine::BoundsCheckFd(EventHandler* eh) +{ + if (!eh) + return false; + if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS)) + return false; + return true; +} + +#ifdef WINDOWS + +int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) { return -1; } +int SocketEngine::Close(int fd) { return -1; } +int SocketEngine::Close(EventHandler* fd) { return -1; } +int SocketEngine::Blocking(int fd) { return -1; } +int SocketEngine::NonBlocking(int fd) { return -1; } +int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen) { return -1; } +int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen) { return -1; } + +#else + +int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) +{ + return accept(fd->GetFd(), addr, addrlen); +} + +int SocketEngine::Close(EventHandler* fd) +{ + return close(fd->GetFd()); +} + +int SocketEngine::Close(int fd) +{ + return close(fd); +} + +int SocketEngine::Blocking(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK); +} + +int SocketEngine::NonBlocking(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen) +{ + return getsockname(fd->GetFd(), name, namelen); +} + +int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen) +{ + return recvfrom(fd->GetFd(), buf, len, flags, from, fromlen); +} + +#endif + +int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags) +{ + return send(fd->GetFd(), (const char*)buf, len, flags); +} + +int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags) +{ + return recv(fd->GetFd(), (char*)buf, len, flags); +} + +int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen) +{ + return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen); +} + +int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen) +{ + return connect(fd->GetFd(), serv_addr, addrlen); +} + +int SocketEngine::Shutdown(EventHandler* fd, int how) +{ + return shutdown(fd->GetFd(), how); +} + +int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen) +{ + return bind(fd, my_addr, addrlen); +} + +int SocketEngine::Listen(int sockfd, int backlog) +{ + return listen(sockfd, backlog); +} + +int SocketEngine::Shutdown(int fd, int how) +{ + return shutdown(fd, how); +} + +void SocketEngine::RecoverFromFork() +{ +} +