1 /******************************************************************************** 2 * Nepenthes 3 * - finest collection - 4 * 5 * 6 * 7 * Copyright (C) 2005 Paul Baecher & Markus Koetter 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; either version 2 12 * of the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 * 23 * 24 * contact nepenthesdev@users.sourceforge.net 25 * 26 *******************************************************************************/ 27 28 /* $Id: Socket.hpp 1410 2007-10-12 13:07:23Z common $ */ 29 30 #ifndef HAVE_SOCKET_HPP 31 #define HAVE_SOCKET_HPP 32 33 #ifdef WIN32 34 #define socklen_t int32_t 35 #endif 36 37 #include <sys/types.h> 38 #include <sys/socket.h> 39 #include "Dialogue.hpp" 40 41 #include <list> 42 #include <string> 43 using namespace std; 44 45 46 #include "Responder.hpp" 47 48 49 50 #define ST_BIND 0x00001 51 #define ST_ACCEPT 0x00002 52 #define ST_CONNECT 0x00004 53 54 #define ST_TCP 0x00010 // tcp socket 55 #define ST_UDP 0x00020 // udp socket 56 #define ST_UDS 0x00040 // unix domain socket 57 #define ST_RAW 0x00080 // raw socket 58 #define ST_POLL 0x00100 // pollonly socket 59 #define ST_FILE 0x00200 // open a file (/dev/urandom) and check for readability 60 #define ST_NODEL 0x00400 // dont delete this socket 61 62 #define ST_RAW_UDP 0x00800 63 #define ST_RAW_TCP 0x01000 64 65 typedef enum 66 { 67 SS_CONNECTED, // cool sockets without problems 68 SS_CONNECTING, 69 SS_TIMEOUT, 70 SS_RECONNECT, 71 SS_CLOSED, // intended to use with udp&tftp 72 SS_CLEANQUIT // dont allow any more writing on the socket, if the send que is empty, close socket and set status to SS_CLOSED 73 74 } socket_state; 75 76 namespace nepenthes 77 { 78 class DialogueFactory; 79 class Dialogue; 80 class Socket; 81 class Nepenthes; 82 83 84 /** 85 * the Socket 86 * 87 * if you want to connect, use a Socket. 88 * 89 * this class can be derived to handle everything. 90 * there are TCPSocket UDPSocket RAWSocket FILESocket 91 * Sockets for everything. 92 */ 93 class Socket : public Responder 94 { 95 public: ~Socket()96 virtual ~Socket (){}; 97 98 virtual bool addDialogueFactory(DialogueFactory *diaf); 99 virtual bool addDialogue(Dialogue *dia); 100 101 virtual bool Init()=0; 102 virtual bool Exit()=0; 103 104 virtual bool connectHost()=0; 105 virtual bool bindPort()=0; 106 virtual Socket* acceptConnection()=0; 107 108 virtual bool wantSend()=0; 109 110 virtual int32_t doSend()=0; 111 virtual int32_t doRecv()=0; 112 113 virtual int32_t doWrite(char *msg,uint32_t len)=0; 114 // virtual int32_t doWrite(const char *msg,uint32_t len)=0; 115 116 virtual bool checkTimeout()=0; 117 virtual bool handleTimeout()=0; 118 119 /** 120 * get a description of the socket 121 * 122 * @return a string formated like this 123 * (tcp|udp|raw|uds|poll) (bind|connect|accept) ip:port <-> ip:port 124 */ 125 virtual string getDescription(); 126 127 virtual int32_t getStatus(); 128 virtual void setStatus(socket_state i); 129 virtual void setPolled(); 130 virtual void unsetPolled(); 131 virtual bool isPolled(); 132 133 virtual int32_t getsockOpt(int32_t level, int32_t optname,void *optval,socklen_t *optlen); 134 135 136 virtual int32_t getSocket(); 137 virtual void setSocket(int32_t i); 138 139 140 141 virtual int32_t getType(); 142 143 virtual uint16_t getLocalPort(); 144 virtual uint16_t getRemotePort(); 145 virtual void setLocalPort(uint16_t i); 146 virtual void setRemotePort(uint16_t i); 147 148 149 virtual void setRemoteHost(uint32_t i); 150 virtual void setLocalHost(uint32_t i); 151 virtual uint32_t getLocalHost(); 152 virtual uint32_t getRemoteHost(); 153 virtual bool getRemoteHWA(string *address); 154 virtual list <DialogueFactory *> * getFactories(); 155 virtual list <Dialogue *> * getDialogst(); 156 157 virtual time_t getBindTimeout(); 158 virtual time_t getTimeout(); 159 160 virtual Nepenthes *getNepenthes(); 161 162 163 164 virtual bool isAccept(); 165 virtual bool isConnect(); 166 virtual bool isBind(); 167 168 protected: 169 list <DialogueFactory *> m_DialogueFactories; 170 list <Dialogue *> m_Dialogues; 171 172 173 int32_t m_ReconnectMax; 174 int32_t m_ReconnectTries; 175 176 uint32_t m_Type; // udp / tcp // bind / connect / accept 177 int32_t m_Socket; 178 179 socket_state m_Status; 180 181 uint32_t m_RemoteHost; 182 uint16_t m_RemotePort; 183 string m_RemoteHostString; 184 185 uint32_t m_LocalHost; 186 uint16_t m_LocalPort; 187 string m_LocalHostString; 188 189 time_t m_TimeoutIntervall; // intervall between time(NULL) and m_tLastSocketAction 190 time_t m_BindTimeoutIntervall; // bind()' sockets can have a different timeout than their childs 191 time_t m_LastAction; 192 193 bool m_Polled; 194 bool m_CanSend; 195 196 Nepenthes *m_Nepenthes; 197 198 ConsumeLevel m_HighestConsumeLevel; 199 200 }; 201 202 203 204 } 205 206 207 #endif 208 209