]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
2b6f84cd35d3637856008c35d4d08ad56a29e0b9
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <string>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sstream>
29 #include <iostream>
30 #include <fstream>
31 #include "socket.h"
32 #include "inspircd.h"
33 #include "inspircd_io.h"
34 #include "inspircd_util.h"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
37
38 extern FILE *log_file;
39 extern int boundPortCount;
40 extern int openSockfd[MAXSOCKS];
41 extern time_t TIME;
42 extern bool unlimitcore;
43 extern int MaxConn;
44
45 InspSocket::InspSocket()
46 {
47         this->state = I_DISCONNECTED;
48 }
49
50 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
51 {
52         if (listening) {
53         } else {
54                 char* ip;
55                 this->host = host;
56                 hostent* hoste = gethostbyname(host.c_str());
57                 if (!hoste) {
58                         ip = (char*)host.c_str();
59                 } else {
60                         struct in_addr* ia = (in_addr*)hoste->h_addr;
61                         ip = inet_ntoa(*ia);
62                 }
63
64                 timeout_end = time(NULL)+maxtime;
65                 timeout = false;
66                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
67                 {
68                         this->state = I_ERROR;
69                         return;
70                 }
71                 this->port = port;
72                 inet_aton(ip,&addy);
73                 addr.sin_family = AF_INET;
74                 addr.sin_addr = addy;
75                 addr.sin_port = htons(this->port);
76
77                 int flags;
78                 flags = fcntl(this->fd, F_GETFL, 0);
79                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
80
81                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
82                 {
83                         if (errno != EINPROGRESS)
84                         {
85                                 shutdown(this->fd,2);
86                                 close(this->fd);
87                                 this->fd = -1;
88                                 this->state = I_ERROR;
89                                 return;
90                         }
91                 }
92                 this->state = I_CONNECTING;
93                 return;
94         }
95 }
96
97 void InspSocket::EngineTrigger()
98 {
99         switch (this->state)
100         {
101                 case I_CONNECTING:
102                         this->OnConnected();
103                 break;
104                 case I_LISTENING:
105                         this->OnIncomingConnection();
106                 break;
107                 case I_CONNECTED:
108                         this->OnDataReady();
109                 break;
110                 default:
111                 break;
112         }
113 }
114
115 void InspSocket::SetState(InspSocketState s)
116 {
117         this->state = s;
118 }
119
120 int InspSocket::OnConnected() { return 0; }
121 int InspSocket::OnError() { return 0; }
122 int InspSocket::OnDisconnect() { return 0; }
123 int InspSocket::OnIncomingConnection() { return 0; }
124 int InspSocket::OnDataReady() { return 0; }
125
126 InspSocket::~InspSocket()
127 {
128 }
129
130 /*
131 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
132 int OpenTCPSocket (void)
133 */